1 // Copyright 2020 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 "TextureFormatUtils.h" 16 17 namespace utils { GetColorTextureComponentTypePrefix(wgpu::TextureFormat textureFormat)18 const char* GetColorTextureComponentTypePrefix(wgpu::TextureFormat textureFormat) { 19 switch (textureFormat) { 20 case wgpu::TextureFormat::R8Unorm: 21 case wgpu::TextureFormat::R8Snorm: 22 case wgpu::TextureFormat::R16Float: 23 case wgpu::TextureFormat::RG8Unorm: 24 case wgpu::TextureFormat::RG8Snorm: 25 case wgpu::TextureFormat::R32Float: 26 case wgpu::TextureFormat::RG16Float: 27 case wgpu::TextureFormat::RGBA8Unorm: 28 case wgpu::TextureFormat::RGBA8Snorm: 29 case wgpu::TextureFormat::RGB10A2Unorm: 30 case wgpu::TextureFormat::RG11B10Ufloat: 31 case wgpu::TextureFormat::RGB9E5Ufloat: 32 case wgpu::TextureFormat::RG32Float: 33 case wgpu::TextureFormat::RGBA16Float: 34 case wgpu::TextureFormat::RGBA32Float: 35 case wgpu::TextureFormat::BGRA8Unorm: 36 case wgpu::TextureFormat::BGRA8UnormSrgb: 37 case wgpu::TextureFormat::RGBA8UnormSrgb: 38 return ""; 39 40 case wgpu::TextureFormat::R8Uint: 41 case wgpu::TextureFormat::R16Uint: 42 case wgpu::TextureFormat::RG8Uint: 43 case wgpu::TextureFormat::R32Uint: 44 case wgpu::TextureFormat::RG16Uint: 45 case wgpu::TextureFormat::RGBA8Uint: 46 case wgpu::TextureFormat::RG32Uint: 47 case wgpu::TextureFormat::RGBA16Uint: 48 case wgpu::TextureFormat::RGBA32Uint: 49 return "u"; 50 51 case wgpu::TextureFormat::R8Sint: 52 case wgpu::TextureFormat::R16Sint: 53 case wgpu::TextureFormat::RG8Sint: 54 case wgpu::TextureFormat::R32Sint: 55 case wgpu::TextureFormat::RG16Sint: 56 case wgpu::TextureFormat::RGBA8Sint: 57 case wgpu::TextureFormat::RG32Sint: 58 case wgpu::TextureFormat::RGBA16Sint: 59 case wgpu::TextureFormat::RGBA32Sint: 60 return "i"; 61 62 default: 63 UNREACHABLE(); 64 } 65 } 66 TextureFormatSupportsStorageTexture(wgpu::TextureFormat format)67 bool TextureFormatSupportsStorageTexture(wgpu::TextureFormat format) { 68 switch (format) { 69 case wgpu::TextureFormat::R32Uint: 70 case wgpu::TextureFormat::R32Sint: 71 case wgpu::TextureFormat::R32Float: 72 case wgpu::TextureFormat::RGBA8Unorm: 73 case wgpu::TextureFormat::RGBA8Snorm: 74 case wgpu::TextureFormat::RGBA8Uint: 75 case wgpu::TextureFormat::RGBA8Sint: 76 case wgpu::TextureFormat::RG32Uint: 77 case wgpu::TextureFormat::RG32Sint: 78 case wgpu::TextureFormat::RG32Float: 79 case wgpu::TextureFormat::RGBA16Uint: 80 case wgpu::TextureFormat::RGBA16Sint: 81 case wgpu::TextureFormat::RGBA16Float: 82 case wgpu::TextureFormat::RGBA32Uint: 83 case wgpu::TextureFormat::RGBA32Sint: 84 case wgpu::TextureFormat::RGBA32Float: 85 return true; 86 87 default: 88 return false; 89 } 90 } 91 GetTexelBlockSizeInBytes(wgpu::TextureFormat textureFormat)92 uint32_t GetTexelBlockSizeInBytes(wgpu::TextureFormat textureFormat) { 93 switch (textureFormat) { 94 case wgpu::TextureFormat::R8Unorm: 95 case wgpu::TextureFormat::R8Snorm: 96 case wgpu::TextureFormat::R8Uint: 97 case wgpu::TextureFormat::R8Sint: 98 return 1u; 99 100 case wgpu::TextureFormat::R16Uint: 101 case wgpu::TextureFormat::R16Sint: 102 case wgpu::TextureFormat::R16Float: 103 case wgpu::TextureFormat::RG8Unorm: 104 case wgpu::TextureFormat::RG8Snorm: 105 case wgpu::TextureFormat::RG8Uint: 106 case wgpu::TextureFormat::RG8Sint: 107 return 2u; 108 109 case wgpu::TextureFormat::R32Float: 110 case wgpu::TextureFormat::R32Uint: 111 case wgpu::TextureFormat::R32Sint: 112 case wgpu::TextureFormat::RG16Uint: 113 case wgpu::TextureFormat::RG16Sint: 114 case wgpu::TextureFormat::RG16Float: 115 case wgpu::TextureFormat::RGBA8Unorm: 116 case wgpu::TextureFormat::RGBA8UnormSrgb: 117 case wgpu::TextureFormat::RGBA8Snorm: 118 case wgpu::TextureFormat::RGBA8Uint: 119 case wgpu::TextureFormat::RGBA8Sint: 120 case wgpu::TextureFormat::BGRA8Unorm: 121 case wgpu::TextureFormat::BGRA8UnormSrgb: 122 case wgpu::TextureFormat::RGB10A2Unorm: 123 case wgpu::TextureFormat::RG11B10Ufloat: 124 case wgpu::TextureFormat::RGB9E5Ufloat: 125 case wgpu::TextureFormat::Depth32Float: 126 return 4u; 127 128 case wgpu::TextureFormat::RG32Float: 129 case wgpu::TextureFormat::RG32Uint: 130 case wgpu::TextureFormat::RG32Sint: 131 case wgpu::TextureFormat::RGBA16Uint: 132 case wgpu::TextureFormat::RGBA16Sint: 133 case wgpu::TextureFormat::RGBA16Float: 134 return 8u; 135 136 case wgpu::TextureFormat::RGBA32Float: 137 case wgpu::TextureFormat::RGBA32Uint: 138 case wgpu::TextureFormat::RGBA32Sint: 139 return 16u; 140 141 case wgpu::TextureFormat::BC1RGBAUnorm: 142 case wgpu::TextureFormat::BC1RGBAUnormSrgb: 143 case wgpu::TextureFormat::BC4RUnorm: 144 case wgpu::TextureFormat::BC4RSnorm: 145 return 8u; 146 147 case wgpu::TextureFormat::BC2RGBAUnorm: 148 case wgpu::TextureFormat::BC2RGBAUnormSrgb: 149 case wgpu::TextureFormat::BC3RGBAUnorm: 150 case wgpu::TextureFormat::BC3RGBAUnormSrgb: 151 case wgpu::TextureFormat::BC5RGUnorm: 152 case wgpu::TextureFormat::BC5RGSnorm: 153 case wgpu::TextureFormat::BC6HRGBUfloat: 154 case wgpu::TextureFormat::BC6HRGBFloat: 155 case wgpu::TextureFormat::BC7RGBAUnorm: 156 case wgpu::TextureFormat::BC7RGBAUnormSrgb: 157 return 16u; 158 159 case wgpu::TextureFormat::Depth24Plus: 160 case wgpu::TextureFormat::Depth24PlusStencil8: 161 case wgpu::TextureFormat::Undefined: 162 UNREACHABLE(); 163 } 164 } 165 GetTextureFormatBlockWidth(wgpu::TextureFormat textureFormat)166 uint32_t GetTextureFormatBlockWidth(wgpu::TextureFormat textureFormat) { 167 switch (textureFormat) { 168 case wgpu::TextureFormat::R8Unorm: 169 case wgpu::TextureFormat::R8Snorm: 170 case wgpu::TextureFormat::R8Uint: 171 case wgpu::TextureFormat::R8Sint: 172 case wgpu::TextureFormat::R16Uint: 173 case wgpu::TextureFormat::R16Sint: 174 case wgpu::TextureFormat::R16Float: 175 case wgpu::TextureFormat::RG8Unorm: 176 case wgpu::TextureFormat::RG8Snorm: 177 case wgpu::TextureFormat::RG8Uint: 178 case wgpu::TextureFormat::RG8Sint: 179 case wgpu::TextureFormat::R32Float: 180 case wgpu::TextureFormat::R32Uint: 181 case wgpu::TextureFormat::R32Sint: 182 case wgpu::TextureFormat::RG16Uint: 183 case wgpu::TextureFormat::RG16Sint: 184 case wgpu::TextureFormat::RG16Float: 185 case wgpu::TextureFormat::RGBA8Unorm: 186 case wgpu::TextureFormat::RGBA8UnormSrgb: 187 case wgpu::TextureFormat::RGBA8Snorm: 188 case wgpu::TextureFormat::RGBA8Uint: 189 case wgpu::TextureFormat::RGBA8Sint: 190 case wgpu::TextureFormat::BGRA8Unorm: 191 case wgpu::TextureFormat::BGRA8UnormSrgb: 192 case wgpu::TextureFormat::RGB10A2Unorm: 193 case wgpu::TextureFormat::RG11B10Ufloat: 194 case wgpu::TextureFormat::RGB9E5Ufloat: 195 case wgpu::TextureFormat::RG32Float: 196 case wgpu::TextureFormat::RG32Uint: 197 case wgpu::TextureFormat::RG32Sint: 198 case wgpu::TextureFormat::RGBA16Uint: 199 case wgpu::TextureFormat::RGBA16Sint: 200 case wgpu::TextureFormat::RGBA16Float: 201 case wgpu::TextureFormat::RGBA32Float: 202 case wgpu::TextureFormat::RGBA32Uint: 203 case wgpu::TextureFormat::RGBA32Sint: 204 case wgpu::TextureFormat::Depth32Float: 205 case wgpu::TextureFormat::Depth24Plus: 206 case wgpu::TextureFormat::Depth24PlusStencil8: 207 return 1u; 208 209 case wgpu::TextureFormat::BC1RGBAUnorm: 210 case wgpu::TextureFormat::BC1RGBAUnormSrgb: 211 case wgpu::TextureFormat::BC4RUnorm: 212 case wgpu::TextureFormat::BC4RSnorm: 213 case wgpu::TextureFormat::BC2RGBAUnorm: 214 case wgpu::TextureFormat::BC2RGBAUnormSrgb: 215 case wgpu::TextureFormat::BC3RGBAUnorm: 216 case wgpu::TextureFormat::BC3RGBAUnormSrgb: 217 case wgpu::TextureFormat::BC5RGUnorm: 218 case wgpu::TextureFormat::BC5RGSnorm: 219 case wgpu::TextureFormat::BC6HRGBUfloat: 220 case wgpu::TextureFormat::BC6HRGBFloat: 221 case wgpu::TextureFormat::BC7RGBAUnorm: 222 case wgpu::TextureFormat::BC7RGBAUnormSrgb: 223 return 4u; 224 225 case wgpu::TextureFormat::Undefined: 226 UNREACHABLE(); 227 } 228 } 229 GetTextureFormatBlockHeight(wgpu::TextureFormat textureFormat)230 uint32_t GetTextureFormatBlockHeight(wgpu::TextureFormat textureFormat) { 231 switch (textureFormat) { 232 case wgpu::TextureFormat::R8Unorm: 233 case wgpu::TextureFormat::R8Snorm: 234 case wgpu::TextureFormat::R8Uint: 235 case wgpu::TextureFormat::R8Sint: 236 case wgpu::TextureFormat::R16Uint: 237 case wgpu::TextureFormat::R16Sint: 238 case wgpu::TextureFormat::R16Float: 239 case wgpu::TextureFormat::RG8Unorm: 240 case wgpu::TextureFormat::RG8Snorm: 241 case wgpu::TextureFormat::RG8Uint: 242 case wgpu::TextureFormat::RG8Sint: 243 case wgpu::TextureFormat::R32Float: 244 case wgpu::TextureFormat::R32Uint: 245 case wgpu::TextureFormat::R32Sint: 246 case wgpu::TextureFormat::RG16Uint: 247 case wgpu::TextureFormat::RG16Sint: 248 case wgpu::TextureFormat::RG16Float: 249 case wgpu::TextureFormat::RGBA8Unorm: 250 case wgpu::TextureFormat::RGBA8UnormSrgb: 251 case wgpu::TextureFormat::RGBA8Snorm: 252 case wgpu::TextureFormat::RGBA8Uint: 253 case wgpu::TextureFormat::RGBA8Sint: 254 case wgpu::TextureFormat::BGRA8Unorm: 255 case wgpu::TextureFormat::BGRA8UnormSrgb: 256 case wgpu::TextureFormat::RGB10A2Unorm: 257 case wgpu::TextureFormat::RG11B10Ufloat: 258 case wgpu::TextureFormat::RGB9E5Ufloat: 259 case wgpu::TextureFormat::RG32Float: 260 case wgpu::TextureFormat::RG32Uint: 261 case wgpu::TextureFormat::RG32Sint: 262 case wgpu::TextureFormat::RGBA16Uint: 263 case wgpu::TextureFormat::RGBA16Sint: 264 case wgpu::TextureFormat::RGBA16Float: 265 case wgpu::TextureFormat::RGBA32Float: 266 case wgpu::TextureFormat::RGBA32Uint: 267 case wgpu::TextureFormat::RGBA32Sint: 268 case wgpu::TextureFormat::Depth32Float: 269 case wgpu::TextureFormat::Depth24Plus: 270 case wgpu::TextureFormat::Depth24PlusStencil8: 271 return 1u; 272 273 case wgpu::TextureFormat::BC1RGBAUnorm: 274 case wgpu::TextureFormat::BC1RGBAUnormSrgb: 275 case wgpu::TextureFormat::BC4RUnorm: 276 case wgpu::TextureFormat::BC4RSnorm: 277 case wgpu::TextureFormat::BC2RGBAUnorm: 278 case wgpu::TextureFormat::BC2RGBAUnormSrgb: 279 case wgpu::TextureFormat::BC3RGBAUnorm: 280 case wgpu::TextureFormat::BC3RGBAUnormSrgb: 281 case wgpu::TextureFormat::BC5RGUnorm: 282 case wgpu::TextureFormat::BC5RGSnorm: 283 case wgpu::TextureFormat::BC6HRGBUfloat: 284 case wgpu::TextureFormat::BC6HRGBFloat: 285 case wgpu::TextureFormat::BC7RGBAUnorm: 286 case wgpu::TextureFormat::BC7RGBAUnormSrgb: 287 return 4u; 288 289 case wgpu::TextureFormat::Undefined: 290 UNREACHABLE(); 291 } 292 } 293 GetGLSLImageFormatQualifier(wgpu::TextureFormat textureFormat)294 const char* GetGLSLImageFormatQualifier(wgpu::TextureFormat textureFormat) { 295 switch (textureFormat) { 296 case wgpu::TextureFormat::R8Unorm: 297 return "r8"; 298 case wgpu::TextureFormat::R8Snorm: 299 return "r8_snorm"; 300 case wgpu::TextureFormat::R8Uint: 301 return "r8ui"; 302 case wgpu::TextureFormat::R8Sint: 303 return "r8i"; 304 case wgpu::TextureFormat::R16Uint: 305 return "r16ui"; 306 case wgpu::TextureFormat::R16Sint: 307 return "r16i"; 308 case wgpu::TextureFormat::R16Float: 309 return "r16f"; 310 case wgpu::TextureFormat::RG8Unorm: 311 return "rg8"; 312 case wgpu::TextureFormat::RG8Snorm: 313 return "rg8_snorm"; 314 case wgpu::TextureFormat::RG8Uint: 315 return "rg8ui"; 316 case wgpu::TextureFormat::RG8Sint: 317 return "rg8i"; 318 case wgpu::TextureFormat::R32Float: 319 return "r32f"; 320 case wgpu::TextureFormat::R32Uint: 321 return "r32ui"; 322 case wgpu::TextureFormat::R32Sint: 323 return "r32i"; 324 case wgpu::TextureFormat::RG16Uint: 325 return "rg16ui"; 326 case wgpu::TextureFormat::RG16Sint: 327 return "rg16i"; 328 case wgpu::TextureFormat::RG16Float: 329 return "rg16f"; 330 case wgpu::TextureFormat::RGBA8Unorm: 331 return "rgba8"; 332 case wgpu::TextureFormat::RGBA8Snorm: 333 return "rgba8_snorm"; 334 case wgpu::TextureFormat::RGBA8Uint: 335 return "rgba8ui"; 336 case wgpu::TextureFormat::RGBA8Sint: 337 return "rgba8i"; 338 case wgpu::TextureFormat::RGB10A2Unorm: 339 return "rgb10_a2"; 340 case wgpu::TextureFormat::RG11B10Ufloat: 341 return "r11f_g11f_b10f"; 342 case wgpu::TextureFormat::RG32Float: 343 return "rg32f"; 344 case wgpu::TextureFormat::RG32Uint: 345 return "rg32ui"; 346 case wgpu::TextureFormat::RG32Sint: 347 return "rg32i"; 348 case wgpu::TextureFormat::RGBA16Uint: 349 return "rgba16ui"; 350 case wgpu::TextureFormat::RGBA16Sint: 351 return "rgba16i"; 352 case wgpu::TextureFormat::RGBA16Float: 353 return "rgba16f"; 354 case wgpu::TextureFormat::RGBA32Float: 355 return "rgba32f"; 356 case wgpu::TextureFormat::RGBA32Uint: 357 return "rgba32ui"; 358 case wgpu::TextureFormat::RGBA32Sint: 359 return "rgba32i"; 360 361 case wgpu::TextureFormat::RGB9E5Ufloat: 362 case wgpu::TextureFormat::RGBA8UnormSrgb: 363 case wgpu::TextureFormat::BGRA8Unorm: 364 case wgpu::TextureFormat::BGRA8UnormSrgb: 365 case wgpu::TextureFormat::BC1RGBAUnorm: 366 case wgpu::TextureFormat::BC1RGBAUnormSrgb: 367 case wgpu::TextureFormat::BC4RUnorm: 368 case wgpu::TextureFormat::BC4RSnorm: 369 case wgpu::TextureFormat::BC2RGBAUnorm: 370 case wgpu::TextureFormat::BC2RGBAUnormSrgb: 371 case wgpu::TextureFormat::BC3RGBAUnorm: 372 case wgpu::TextureFormat::BC3RGBAUnormSrgb: 373 case wgpu::TextureFormat::BC5RGUnorm: 374 case wgpu::TextureFormat::BC5RGSnorm: 375 case wgpu::TextureFormat::BC6HRGBUfloat: 376 case wgpu::TextureFormat::BC6HRGBFloat: 377 case wgpu::TextureFormat::BC7RGBAUnorm: 378 case wgpu::TextureFormat::BC7RGBAUnormSrgb: 379 case wgpu::TextureFormat::Depth32Float: 380 case wgpu::TextureFormat::Depth24Plus: 381 case wgpu::TextureFormat::Depth24PlusStencil8: 382 case wgpu::TextureFormat::Undefined: 383 UNREACHABLE(); 384 } 385 } 386 } // namespace utils 387