• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "dawn_native/Format.h"
16 
17 #include <bitset>
18 
19 namespace dawn_native {
20 
21     // Format
22 
IsColor() const23     bool Format::IsColor() const {
24         return aspect == Aspect::Color;
25     }
26 
HasDepth() const27     bool Format::HasDepth() const {
28         return aspect == Depth || aspect == DepthStencil;
29     }
30 
HasStencil() const31     bool Format::HasStencil() const {
32         return aspect == Stencil || aspect == DepthStencil;
33     }
34 
HasDepthOrStencil() const35     bool Format::HasDepthOrStencil() const {
36         return aspect != Color;
37     }
38 
GetIndex() const39     size_t Format::GetIndex() const {
40         return ComputeFormatIndex(format);
41     }
42 
43     // Implementation details of the format table of the DeviceBase
44 
45     // For the enum for formats are packed but this might change when we have a broader extension
46     // mechanism for webgpu.h
ComputeFormatIndex(dawn::TextureFormat format)47     size_t ComputeFormatIndex(dawn::TextureFormat format) {
48         return static_cast<size_t>(static_cast<uint32_t>(format));
49     }
50 
BuildFormatTable(const DeviceBase *)51     FormatTable BuildFormatTable(const DeviceBase*) {
52         FormatTable table;
53         std::bitset<kKnownFormatCount> formatsSet;
54 
55         auto AddFormat = [&table, &formatsSet](Format format) {
56             size_t index = ComputeFormatIndex(format.format);
57             ASSERT(index < table.size());
58 
59             // This checks that each format is set at most once, the first part of checking that all
60             // formats are set exactly once.
61             ASSERT(!formatsSet[index]);
62 
63             table[index] = format;
64             formatsSet.set(index);
65         };
66 
67         auto AddColorFormat = [&AddFormat](dawn::TextureFormat format, bool renderable,
68                                            uint32_t byteSize) {
69             Format internalFormat;
70             internalFormat.format = format;
71             internalFormat.isRenderable = renderable;
72             internalFormat.isCompressed = false;
73             internalFormat.isSupported = true;
74             internalFormat.aspect = Format::Aspect::Color;
75             internalFormat.blockByteSize = byteSize;
76             internalFormat.blockWidth = 1;
77             internalFormat.blockHeight = 1;
78             AddFormat(internalFormat);
79         };
80 
81         auto AddDepthStencilFormat = [&AddFormat](dawn::TextureFormat format, Format::Aspect aspect,
82                                                   uint32_t byteSize) {
83             Format internalFormat;
84             internalFormat.format = format;
85             internalFormat.isRenderable = true;
86             internalFormat.isCompressed = false;
87             internalFormat.isSupported = true;
88             internalFormat.aspect = aspect;
89             internalFormat.blockByteSize = byteSize;
90             internalFormat.blockWidth = 1;
91             internalFormat.blockHeight = 1;
92             AddFormat(internalFormat);
93         };
94 
95         auto AddCompressedFormat = [&AddFormat](dawn::TextureFormat format, uint32_t byteSize,
96                                                 uint32_t width, uint32_t height) {
97             Format internalFormat;
98             internalFormat.format = format;
99             internalFormat.isRenderable = false;
100             internalFormat.isCompressed = true;
101             internalFormat.isSupported = true;
102             internalFormat.aspect = Format::Aspect::Color;
103             internalFormat.blockByteSize = byteSize;
104             internalFormat.blockWidth = width;
105             internalFormat.blockHeight = height;
106             AddFormat(internalFormat);
107         };
108 
109         // clang-format off
110 
111         // 1 byte color formats
112         AddColorFormat(dawn::TextureFormat::R8Unorm, true, 1);
113         AddColorFormat(dawn::TextureFormat::R8Snorm, true, 1);
114         AddColorFormat(dawn::TextureFormat::R8Uint, true, 1);
115         AddColorFormat(dawn::TextureFormat::R8Sint, true, 1);
116 
117         // 2 bytes color formats
118         AddColorFormat(dawn::TextureFormat::R16Unorm, true, 2);
119         AddColorFormat(dawn::TextureFormat::R16Snorm, true, 2);
120         AddColorFormat(dawn::TextureFormat::R16Uint, true, 2);
121         AddColorFormat(dawn::TextureFormat::R16Sint, true, 2);
122         AddColorFormat(dawn::TextureFormat::R16Float, true, 2);
123         AddColorFormat(dawn::TextureFormat::RG8Unorm, true, 2);
124         AddColorFormat(dawn::TextureFormat::RG8Snorm, true, 2);
125         AddColorFormat(dawn::TextureFormat::RG8Uint, true, 2);
126         AddColorFormat(dawn::TextureFormat::RG8Sint, true, 2);
127 
128         // 4 bytes color formats
129         AddColorFormat(dawn::TextureFormat::R32Uint, true, 4);
130         AddColorFormat(dawn::TextureFormat::R32Sint, true, 4);
131         AddColorFormat(dawn::TextureFormat::R32Float, true, 4);
132         AddColorFormat(dawn::TextureFormat::RG16Unorm, true, 4);
133         AddColorFormat(dawn::TextureFormat::RG16Snorm, true, 4);
134         AddColorFormat(dawn::TextureFormat::RG16Uint, true, 4);
135         AddColorFormat(dawn::TextureFormat::RG16Sint, true, 4);
136         AddColorFormat(dawn::TextureFormat::RG16Float, true, 4);
137         AddColorFormat(dawn::TextureFormat::RGBA8Unorm, true, 4);
138         AddColorFormat(dawn::TextureFormat::RGBA8UnormSrgb, true, 4);
139         AddColorFormat(dawn::TextureFormat::RGBA8Snorm, true, 4);
140         AddColorFormat(dawn::TextureFormat::RGBA8Uint, true, 4);
141         AddColorFormat(dawn::TextureFormat::RGBA8Sint, true, 4);
142         AddColorFormat(dawn::TextureFormat::BGRA8Unorm, true, 4);
143         AddColorFormat(dawn::TextureFormat::BGRA8UnormSrgb, true, 4);
144         AddColorFormat(dawn::TextureFormat::RGB10A2Unorm, true, 4);
145 
146         AddColorFormat(dawn::TextureFormat::RG11B10Float, false, 4);
147 
148         // 8 bytes color formats
149         AddColorFormat(dawn::TextureFormat::RG32Uint, true, 8);
150         AddColorFormat(dawn::TextureFormat::RG32Sint, true, 8);
151         AddColorFormat(dawn::TextureFormat::RG32Float, true, 8);
152         AddColorFormat(dawn::TextureFormat::RGBA16Unorm, true, 8);
153         AddColorFormat(dawn::TextureFormat::RGBA16Snorm, true, 8);
154         AddColorFormat(dawn::TextureFormat::RGBA16Uint, true, 8);
155         AddColorFormat(dawn::TextureFormat::RGBA16Sint, true, 8);
156         AddColorFormat(dawn::TextureFormat::RGBA16Float, true, 8);
157 
158         // 16 bytes color formats
159         AddColorFormat(dawn::TextureFormat::RGBA32Uint, true, 16);
160         AddColorFormat(dawn::TextureFormat::RGBA32Sint, true, 16);
161         AddColorFormat(dawn::TextureFormat::RGBA32Float, true, 16);
162 
163         // Depth stencil formats
164         AddDepthStencilFormat(dawn::TextureFormat::Depth32Float, Format::Aspect::Depth, 4);
165         AddDepthStencilFormat(dawn::TextureFormat::Depth24Plus, Format::Aspect::Depth, 4);
166         // TODO(cwallez@chromium.org): It isn't clear if this format should be copyable
167         // because its size isn't well defined, is it 4, 5 or 8?
168         AddDepthStencilFormat(dawn::TextureFormat::Depth24PlusStencil8, Format::Aspect::DepthStencil, 4);
169 
170         // BC compressed formats
171         AddCompressedFormat(dawn::TextureFormat::BC1RGBAUnorm, 8, 4, 4);
172         AddCompressedFormat(dawn::TextureFormat::BC1RGBAUnormSrgb, 8, 4, 4);
173         AddCompressedFormat(dawn::TextureFormat::BC4RSnorm, 8, 4, 4);
174         AddCompressedFormat(dawn::TextureFormat::BC4RUnorm, 8, 4, 4);
175         AddCompressedFormat(dawn::TextureFormat::BC2RGBAUnorm, 16, 4, 4);
176         AddCompressedFormat(dawn::TextureFormat::BC2RGBAUnormSrgb, 16, 4, 4);
177         AddCompressedFormat(dawn::TextureFormat::BC3RGBAUnorm, 16, 4, 4);
178         AddCompressedFormat(dawn::TextureFormat::BC3RGBAUnormSrgb, 16, 4, 4);
179         AddCompressedFormat(dawn::TextureFormat::BC5RGSnorm, 16, 4, 4);
180         AddCompressedFormat(dawn::TextureFormat::BC5RGUnorm, 16, 4, 4);
181         AddCompressedFormat(dawn::TextureFormat::BC6HRGBSfloat, 16, 4, 4);
182         AddCompressedFormat(dawn::TextureFormat::BC6HRGBUfloat, 16, 4, 4);
183         AddCompressedFormat(dawn::TextureFormat::BC7RGBAUnorm, 16, 4, 4);
184         AddCompressedFormat(dawn::TextureFormat::BC7RGBAUnormSrgb, 16, 4, 4);
185 
186         // clang-format on
187 
188         // This checks that each format is set at least once, the second part of checking that all
189         // formats are checked exactly once.
190         ASSERT(formatsSet.all());
191 
192         return table;
193     }
194 
195 }  // namespace dawn_native
196