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 "TextureUtils.h" 16 17 namespace utils { TextureFormatSupportsStorageTexture(wgpu::TextureFormat format)18 bool TextureFormatSupportsStorageTexture(wgpu::TextureFormat format) { 19 switch (format) { 20 case wgpu::TextureFormat::R32Uint: 21 case wgpu::TextureFormat::R32Sint: 22 case wgpu::TextureFormat::R32Float: 23 case wgpu::TextureFormat::RGBA8Unorm: 24 case wgpu::TextureFormat::RGBA8Snorm: 25 case wgpu::TextureFormat::RGBA8Uint: 26 case wgpu::TextureFormat::RGBA8Sint: 27 case wgpu::TextureFormat::RG32Uint: 28 case wgpu::TextureFormat::RG32Sint: 29 case wgpu::TextureFormat::RG32Float: 30 case wgpu::TextureFormat::RGBA16Uint: 31 case wgpu::TextureFormat::RGBA16Sint: 32 case wgpu::TextureFormat::RGBA16Float: 33 case wgpu::TextureFormat::RGBA32Uint: 34 case wgpu::TextureFormat::RGBA32Sint: 35 case wgpu::TextureFormat::RGBA32Float: 36 return true; 37 38 default: 39 return false; 40 } 41 } 42 IsBCTextureFormat(wgpu::TextureFormat textureFormat)43 bool IsBCTextureFormat(wgpu::TextureFormat textureFormat) { 44 switch (textureFormat) { 45 case wgpu::TextureFormat::BC1RGBAUnorm: 46 case wgpu::TextureFormat::BC1RGBAUnormSrgb: 47 case wgpu::TextureFormat::BC4RUnorm: 48 case wgpu::TextureFormat::BC4RSnorm: 49 case wgpu::TextureFormat::BC2RGBAUnorm: 50 case wgpu::TextureFormat::BC2RGBAUnormSrgb: 51 case wgpu::TextureFormat::BC3RGBAUnorm: 52 case wgpu::TextureFormat::BC3RGBAUnormSrgb: 53 case wgpu::TextureFormat::BC5RGUnorm: 54 case wgpu::TextureFormat::BC5RGSnorm: 55 case wgpu::TextureFormat::BC6HRGBUfloat: 56 case wgpu::TextureFormat::BC6HRGBFloat: 57 case wgpu::TextureFormat::BC7RGBAUnorm: 58 case wgpu::TextureFormat::BC7RGBAUnormSrgb: 59 return true; 60 61 default: 62 return false; 63 } 64 } 65 IsETC2TextureFormat(wgpu::TextureFormat textureFormat)66 bool IsETC2TextureFormat(wgpu::TextureFormat textureFormat) { 67 switch (textureFormat) { 68 case wgpu::TextureFormat::ETC2RGB8Unorm: 69 case wgpu::TextureFormat::ETC2RGB8UnormSrgb: 70 case wgpu::TextureFormat::ETC2RGB8A1Unorm: 71 case wgpu::TextureFormat::ETC2RGB8A1UnormSrgb: 72 case wgpu::TextureFormat::EACR11Unorm: 73 case wgpu::TextureFormat::EACR11Snorm: 74 case wgpu::TextureFormat::ETC2RGBA8Unorm: 75 case wgpu::TextureFormat::ETC2RGBA8UnormSrgb: 76 case wgpu::TextureFormat::EACRG11Unorm: 77 case wgpu::TextureFormat::EACRG11Snorm: 78 return true; 79 80 default: 81 return false; 82 } 83 } 84 IsASTCTextureFormat(wgpu::TextureFormat textureFormat)85 bool IsASTCTextureFormat(wgpu::TextureFormat textureFormat) { 86 switch (textureFormat) { 87 case wgpu::TextureFormat::ASTC4x4Unorm: 88 case wgpu::TextureFormat::ASTC4x4UnormSrgb: 89 case wgpu::TextureFormat::ASTC5x4Unorm: 90 case wgpu::TextureFormat::ASTC5x4UnormSrgb: 91 case wgpu::TextureFormat::ASTC5x5Unorm: 92 case wgpu::TextureFormat::ASTC5x5UnormSrgb: 93 case wgpu::TextureFormat::ASTC6x5Unorm: 94 case wgpu::TextureFormat::ASTC6x5UnormSrgb: 95 case wgpu::TextureFormat::ASTC6x6Unorm: 96 case wgpu::TextureFormat::ASTC6x6UnormSrgb: 97 case wgpu::TextureFormat::ASTC8x5Unorm: 98 case wgpu::TextureFormat::ASTC8x5UnormSrgb: 99 case wgpu::TextureFormat::ASTC8x6Unorm: 100 case wgpu::TextureFormat::ASTC8x6UnormSrgb: 101 case wgpu::TextureFormat::ASTC8x8Unorm: 102 case wgpu::TextureFormat::ASTC8x8UnormSrgb: 103 case wgpu::TextureFormat::ASTC10x5Unorm: 104 case wgpu::TextureFormat::ASTC10x5UnormSrgb: 105 case wgpu::TextureFormat::ASTC10x6Unorm: 106 case wgpu::TextureFormat::ASTC10x6UnormSrgb: 107 case wgpu::TextureFormat::ASTC10x8Unorm: 108 case wgpu::TextureFormat::ASTC10x8UnormSrgb: 109 case wgpu::TextureFormat::ASTC10x10Unorm: 110 case wgpu::TextureFormat::ASTC10x10UnormSrgb: 111 case wgpu::TextureFormat::ASTC12x10Unorm: 112 case wgpu::TextureFormat::ASTC12x10UnormSrgb: 113 case wgpu::TextureFormat::ASTC12x12Unorm: 114 case wgpu::TextureFormat::ASTC12x12UnormSrgb: 115 return true; 116 117 default: 118 return false; 119 } 120 } 121 IsDepthOnlyFormat(wgpu::TextureFormat textureFormat)122 bool IsDepthOnlyFormat(wgpu::TextureFormat textureFormat) { 123 switch (textureFormat) { 124 case wgpu::TextureFormat::Depth16Unorm: 125 case wgpu::TextureFormat::Depth24Plus: 126 case wgpu::TextureFormat::Depth32Float: 127 return true; 128 default: 129 return false; 130 } 131 } 132 GetTexelBlockSizeInBytes(wgpu::TextureFormat textureFormat)133 uint32_t GetTexelBlockSizeInBytes(wgpu::TextureFormat textureFormat) { 134 switch (textureFormat) { 135 case wgpu::TextureFormat::R8Unorm: 136 case wgpu::TextureFormat::R8Snorm: 137 case wgpu::TextureFormat::R8Uint: 138 case wgpu::TextureFormat::R8Sint: 139 return 1u; 140 141 case wgpu::TextureFormat::R16Uint: 142 case wgpu::TextureFormat::R16Sint: 143 case wgpu::TextureFormat::R16Float: 144 case wgpu::TextureFormat::RG8Unorm: 145 case wgpu::TextureFormat::RG8Snorm: 146 case wgpu::TextureFormat::RG8Uint: 147 case wgpu::TextureFormat::RG8Sint: 148 return 2u; 149 150 case wgpu::TextureFormat::R32Float: 151 case wgpu::TextureFormat::R32Uint: 152 case wgpu::TextureFormat::R32Sint: 153 case wgpu::TextureFormat::RG16Uint: 154 case wgpu::TextureFormat::RG16Sint: 155 case wgpu::TextureFormat::RG16Float: 156 case wgpu::TextureFormat::RGBA8Unorm: 157 case wgpu::TextureFormat::RGBA8UnormSrgb: 158 case wgpu::TextureFormat::RGBA8Snorm: 159 case wgpu::TextureFormat::RGBA8Uint: 160 case wgpu::TextureFormat::RGBA8Sint: 161 case wgpu::TextureFormat::BGRA8Unorm: 162 case wgpu::TextureFormat::BGRA8UnormSrgb: 163 case wgpu::TextureFormat::RGB10A2Unorm: 164 case wgpu::TextureFormat::RG11B10Ufloat: 165 case wgpu::TextureFormat::RGB9E5Ufloat: 166 return 4u; 167 168 case wgpu::TextureFormat::RG32Float: 169 case wgpu::TextureFormat::RG32Uint: 170 case wgpu::TextureFormat::RG32Sint: 171 case wgpu::TextureFormat::RGBA16Uint: 172 case wgpu::TextureFormat::RGBA16Sint: 173 case wgpu::TextureFormat::RGBA16Float: 174 return 8u; 175 176 case wgpu::TextureFormat::RGBA32Float: 177 case wgpu::TextureFormat::RGBA32Uint: 178 case wgpu::TextureFormat::RGBA32Sint: 179 return 16u; 180 181 case wgpu::TextureFormat::Depth16Unorm: 182 return 2u; 183 184 case wgpu::TextureFormat::Depth24Plus: 185 case wgpu::TextureFormat::Depth24UnormStencil8: 186 case wgpu::TextureFormat::Depth32Float: 187 return 4u; 188 189 case wgpu::TextureFormat::BC1RGBAUnorm: 190 case wgpu::TextureFormat::BC1RGBAUnormSrgb: 191 case wgpu::TextureFormat::BC4RUnorm: 192 case wgpu::TextureFormat::BC4RSnorm: 193 return 8u; 194 195 case wgpu::TextureFormat::BC2RGBAUnorm: 196 case wgpu::TextureFormat::BC2RGBAUnormSrgb: 197 case wgpu::TextureFormat::BC3RGBAUnorm: 198 case wgpu::TextureFormat::BC3RGBAUnormSrgb: 199 case wgpu::TextureFormat::BC5RGUnorm: 200 case wgpu::TextureFormat::BC5RGSnorm: 201 case wgpu::TextureFormat::BC6HRGBUfloat: 202 case wgpu::TextureFormat::BC6HRGBFloat: 203 case wgpu::TextureFormat::BC7RGBAUnorm: 204 case wgpu::TextureFormat::BC7RGBAUnormSrgb: 205 return 16u; 206 207 case wgpu::TextureFormat::ETC2RGB8Unorm: 208 case wgpu::TextureFormat::ETC2RGB8UnormSrgb: 209 case wgpu::TextureFormat::ETC2RGB8A1Unorm: 210 case wgpu::TextureFormat::ETC2RGB8A1UnormSrgb: 211 case wgpu::TextureFormat::EACR11Unorm: 212 case wgpu::TextureFormat::EACR11Snorm: 213 return 8u; 214 215 case wgpu::TextureFormat::ETC2RGBA8Unorm: 216 case wgpu::TextureFormat::ETC2RGBA8UnormSrgb: 217 case wgpu::TextureFormat::EACRG11Unorm: 218 case wgpu::TextureFormat::EACRG11Snorm: 219 return 16u; 220 221 case wgpu::TextureFormat::ASTC4x4Unorm: 222 case wgpu::TextureFormat::ASTC4x4UnormSrgb: 223 case wgpu::TextureFormat::ASTC5x4Unorm: 224 case wgpu::TextureFormat::ASTC5x4UnormSrgb: 225 case wgpu::TextureFormat::ASTC5x5Unorm: 226 case wgpu::TextureFormat::ASTC5x5UnormSrgb: 227 case wgpu::TextureFormat::ASTC6x5Unorm: 228 case wgpu::TextureFormat::ASTC6x5UnormSrgb: 229 case wgpu::TextureFormat::ASTC6x6Unorm: 230 case wgpu::TextureFormat::ASTC6x6UnormSrgb: 231 case wgpu::TextureFormat::ASTC8x5Unorm: 232 case wgpu::TextureFormat::ASTC8x5UnormSrgb: 233 case wgpu::TextureFormat::ASTC8x6Unorm: 234 case wgpu::TextureFormat::ASTC8x6UnormSrgb: 235 case wgpu::TextureFormat::ASTC8x8Unorm: 236 case wgpu::TextureFormat::ASTC8x8UnormSrgb: 237 case wgpu::TextureFormat::ASTC10x5Unorm: 238 case wgpu::TextureFormat::ASTC10x5UnormSrgb: 239 case wgpu::TextureFormat::ASTC10x6Unorm: 240 case wgpu::TextureFormat::ASTC10x6UnormSrgb: 241 case wgpu::TextureFormat::ASTC10x8Unorm: 242 case wgpu::TextureFormat::ASTC10x8UnormSrgb: 243 case wgpu::TextureFormat::ASTC10x10Unorm: 244 case wgpu::TextureFormat::ASTC10x10UnormSrgb: 245 case wgpu::TextureFormat::ASTC12x10Unorm: 246 case wgpu::TextureFormat::ASTC12x10UnormSrgb: 247 case wgpu::TextureFormat::ASTC12x12Unorm: 248 case wgpu::TextureFormat::ASTC12x12UnormSrgb: 249 return 16u; 250 251 case wgpu::TextureFormat::Depth24PlusStencil8: 252 case wgpu::TextureFormat::Depth32FloatStencil8: 253 254 // Block size of a multi-planar format depends on aspect. 255 case wgpu::TextureFormat::R8BG8Biplanar420Unorm: 256 257 // TODO(dawn:666): implement stencil8 258 case wgpu::TextureFormat::Stencil8: 259 case wgpu::TextureFormat::Undefined: 260 break; 261 } 262 UNREACHABLE(); 263 } 264 GetTextureFormatBlockWidth(wgpu::TextureFormat textureFormat)265 uint32_t GetTextureFormatBlockWidth(wgpu::TextureFormat textureFormat) { 266 switch (textureFormat) { 267 case wgpu::TextureFormat::R8Unorm: 268 case wgpu::TextureFormat::R8Snorm: 269 case wgpu::TextureFormat::R8Uint: 270 case wgpu::TextureFormat::R8Sint: 271 case wgpu::TextureFormat::R16Uint: 272 case wgpu::TextureFormat::R16Sint: 273 case wgpu::TextureFormat::R16Float: 274 case wgpu::TextureFormat::RG8Unorm: 275 case wgpu::TextureFormat::RG8Snorm: 276 case wgpu::TextureFormat::RG8Uint: 277 case wgpu::TextureFormat::RG8Sint: 278 case wgpu::TextureFormat::R32Float: 279 case wgpu::TextureFormat::R32Uint: 280 case wgpu::TextureFormat::R32Sint: 281 case wgpu::TextureFormat::RG16Uint: 282 case wgpu::TextureFormat::RG16Sint: 283 case wgpu::TextureFormat::RG16Float: 284 case wgpu::TextureFormat::RGBA8Unorm: 285 case wgpu::TextureFormat::RGBA8UnormSrgb: 286 case wgpu::TextureFormat::RGBA8Snorm: 287 case wgpu::TextureFormat::RGBA8Uint: 288 case wgpu::TextureFormat::RGBA8Sint: 289 case wgpu::TextureFormat::BGRA8Unorm: 290 case wgpu::TextureFormat::BGRA8UnormSrgb: 291 case wgpu::TextureFormat::RGB10A2Unorm: 292 case wgpu::TextureFormat::RG11B10Ufloat: 293 case wgpu::TextureFormat::RGB9E5Ufloat: 294 case wgpu::TextureFormat::RG32Float: 295 case wgpu::TextureFormat::RG32Uint: 296 case wgpu::TextureFormat::RG32Sint: 297 case wgpu::TextureFormat::RGBA16Uint: 298 case wgpu::TextureFormat::RGBA16Sint: 299 case wgpu::TextureFormat::RGBA16Float: 300 case wgpu::TextureFormat::RGBA32Float: 301 case wgpu::TextureFormat::RGBA32Uint: 302 case wgpu::TextureFormat::RGBA32Sint: 303 case wgpu::TextureFormat::Depth32Float: 304 case wgpu::TextureFormat::Depth24Plus: 305 case wgpu::TextureFormat::Depth24PlusStencil8: 306 case wgpu::TextureFormat::Depth16Unorm: 307 case wgpu::TextureFormat::Depth24UnormStencil8: 308 case wgpu::TextureFormat::Depth32FloatStencil8: 309 return 1u; 310 311 case wgpu::TextureFormat::BC1RGBAUnorm: 312 case wgpu::TextureFormat::BC1RGBAUnormSrgb: 313 case wgpu::TextureFormat::BC4RUnorm: 314 case wgpu::TextureFormat::BC4RSnorm: 315 case wgpu::TextureFormat::BC2RGBAUnorm: 316 case wgpu::TextureFormat::BC2RGBAUnormSrgb: 317 case wgpu::TextureFormat::BC3RGBAUnorm: 318 case wgpu::TextureFormat::BC3RGBAUnormSrgb: 319 case wgpu::TextureFormat::BC5RGUnorm: 320 case wgpu::TextureFormat::BC5RGSnorm: 321 case wgpu::TextureFormat::BC6HRGBUfloat: 322 case wgpu::TextureFormat::BC6HRGBFloat: 323 case wgpu::TextureFormat::BC7RGBAUnorm: 324 case wgpu::TextureFormat::BC7RGBAUnormSrgb: 325 case wgpu::TextureFormat::ETC2RGB8Unorm: 326 case wgpu::TextureFormat::ETC2RGB8UnormSrgb: 327 case wgpu::TextureFormat::ETC2RGB8A1Unorm: 328 case wgpu::TextureFormat::ETC2RGB8A1UnormSrgb: 329 case wgpu::TextureFormat::ETC2RGBA8Unorm: 330 case wgpu::TextureFormat::ETC2RGBA8UnormSrgb: 331 case wgpu::TextureFormat::EACR11Unorm: 332 case wgpu::TextureFormat::EACR11Snorm: 333 case wgpu::TextureFormat::EACRG11Unorm: 334 case wgpu::TextureFormat::EACRG11Snorm: 335 return 4u; 336 337 case wgpu::TextureFormat::ASTC4x4Unorm: 338 case wgpu::TextureFormat::ASTC4x4UnormSrgb: 339 return 4u; 340 case wgpu::TextureFormat::ASTC5x4Unorm: 341 case wgpu::TextureFormat::ASTC5x4UnormSrgb: 342 case wgpu::TextureFormat::ASTC5x5Unorm: 343 case wgpu::TextureFormat::ASTC5x5UnormSrgb: 344 return 5u; 345 case wgpu::TextureFormat::ASTC6x5Unorm: 346 case wgpu::TextureFormat::ASTC6x5UnormSrgb: 347 case wgpu::TextureFormat::ASTC6x6Unorm: 348 case wgpu::TextureFormat::ASTC6x6UnormSrgb: 349 return 6u; 350 case wgpu::TextureFormat::ASTC8x5Unorm: 351 case wgpu::TextureFormat::ASTC8x5UnormSrgb: 352 case wgpu::TextureFormat::ASTC8x6Unorm: 353 case wgpu::TextureFormat::ASTC8x6UnormSrgb: 354 case wgpu::TextureFormat::ASTC8x8Unorm: 355 case wgpu::TextureFormat::ASTC8x8UnormSrgb: 356 return 8u; 357 case wgpu::TextureFormat::ASTC10x5Unorm: 358 case wgpu::TextureFormat::ASTC10x5UnormSrgb: 359 case wgpu::TextureFormat::ASTC10x6Unorm: 360 case wgpu::TextureFormat::ASTC10x6UnormSrgb: 361 case wgpu::TextureFormat::ASTC10x8Unorm: 362 case wgpu::TextureFormat::ASTC10x8UnormSrgb: 363 case wgpu::TextureFormat::ASTC10x10Unorm: 364 case wgpu::TextureFormat::ASTC10x10UnormSrgb: 365 return 10u; 366 case wgpu::TextureFormat::ASTC12x10Unorm: 367 case wgpu::TextureFormat::ASTC12x10UnormSrgb: 368 case wgpu::TextureFormat::ASTC12x12Unorm: 369 case wgpu::TextureFormat::ASTC12x12UnormSrgb: 370 return 12u; 371 372 // Block size of a multi-planar format depends on aspect. 373 case wgpu::TextureFormat::R8BG8Biplanar420Unorm: 374 375 // TODO(dawn:666): implement stencil8 376 case wgpu::TextureFormat::Stencil8: 377 case wgpu::TextureFormat::Undefined: 378 break; 379 } 380 UNREACHABLE(); 381 } 382 GetTextureFormatBlockHeight(wgpu::TextureFormat textureFormat)383 uint32_t GetTextureFormatBlockHeight(wgpu::TextureFormat textureFormat) { 384 switch (textureFormat) { 385 case wgpu::TextureFormat::R8Unorm: 386 case wgpu::TextureFormat::R8Snorm: 387 case wgpu::TextureFormat::R8Uint: 388 case wgpu::TextureFormat::R8Sint: 389 case wgpu::TextureFormat::R16Uint: 390 case wgpu::TextureFormat::R16Sint: 391 case wgpu::TextureFormat::R16Float: 392 case wgpu::TextureFormat::RG8Unorm: 393 case wgpu::TextureFormat::RG8Snorm: 394 case wgpu::TextureFormat::RG8Uint: 395 case wgpu::TextureFormat::RG8Sint: 396 case wgpu::TextureFormat::R32Float: 397 case wgpu::TextureFormat::R32Uint: 398 case wgpu::TextureFormat::R32Sint: 399 case wgpu::TextureFormat::RG16Uint: 400 case wgpu::TextureFormat::RG16Sint: 401 case wgpu::TextureFormat::RG16Float: 402 case wgpu::TextureFormat::RGBA8Unorm: 403 case wgpu::TextureFormat::RGBA8UnormSrgb: 404 case wgpu::TextureFormat::RGBA8Snorm: 405 case wgpu::TextureFormat::RGBA8Uint: 406 case wgpu::TextureFormat::RGBA8Sint: 407 case wgpu::TextureFormat::BGRA8Unorm: 408 case wgpu::TextureFormat::BGRA8UnormSrgb: 409 case wgpu::TextureFormat::RGB10A2Unorm: 410 case wgpu::TextureFormat::RG11B10Ufloat: 411 case wgpu::TextureFormat::RGB9E5Ufloat: 412 case wgpu::TextureFormat::RG32Float: 413 case wgpu::TextureFormat::RG32Uint: 414 case wgpu::TextureFormat::RG32Sint: 415 case wgpu::TextureFormat::RGBA16Uint: 416 case wgpu::TextureFormat::RGBA16Sint: 417 case wgpu::TextureFormat::RGBA16Float: 418 case wgpu::TextureFormat::RGBA32Float: 419 case wgpu::TextureFormat::RGBA32Uint: 420 case wgpu::TextureFormat::RGBA32Sint: 421 case wgpu::TextureFormat::Depth32Float: 422 case wgpu::TextureFormat::Depth24Plus: 423 case wgpu::TextureFormat::Depth24PlusStencil8: 424 case wgpu::TextureFormat::Depth16Unorm: 425 case wgpu::TextureFormat::Depth24UnormStencil8: 426 case wgpu::TextureFormat::Depth32FloatStencil8: 427 return 1u; 428 429 case wgpu::TextureFormat::BC1RGBAUnorm: 430 case wgpu::TextureFormat::BC1RGBAUnormSrgb: 431 case wgpu::TextureFormat::BC4RUnorm: 432 case wgpu::TextureFormat::BC4RSnorm: 433 case wgpu::TextureFormat::BC2RGBAUnorm: 434 case wgpu::TextureFormat::BC2RGBAUnormSrgb: 435 case wgpu::TextureFormat::BC3RGBAUnorm: 436 case wgpu::TextureFormat::BC3RGBAUnormSrgb: 437 case wgpu::TextureFormat::BC5RGUnorm: 438 case wgpu::TextureFormat::BC5RGSnorm: 439 case wgpu::TextureFormat::BC6HRGBUfloat: 440 case wgpu::TextureFormat::BC6HRGBFloat: 441 case wgpu::TextureFormat::BC7RGBAUnorm: 442 case wgpu::TextureFormat::BC7RGBAUnormSrgb: 443 case wgpu::TextureFormat::ETC2RGB8Unorm: 444 case wgpu::TextureFormat::ETC2RGB8UnormSrgb: 445 case wgpu::TextureFormat::ETC2RGB8A1Unorm: 446 case wgpu::TextureFormat::ETC2RGB8A1UnormSrgb: 447 case wgpu::TextureFormat::ETC2RGBA8Unorm: 448 case wgpu::TextureFormat::ETC2RGBA8UnormSrgb: 449 case wgpu::TextureFormat::EACR11Unorm: 450 case wgpu::TextureFormat::EACR11Snorm: 451 case wgpu::TextureFormat::EACRG11Unorm: 452 case wgpu::TextureFormat::EACRG11Snorm: 453 return 4u; 454 455 case wgpu::TextureFormat::ASTC4x4Unorm: 456 case wgpu::TextureFormat::ASTC4x4UnormSrgb: 457 case wgpu::TextureFormat::ASTC5x4Unorm: 458 case wgpu::TextureFormat::ASTC5x4UnormSrgb: 459 return 4u; 460 case wgpu::TextureFormat::ASTC5x5Unorm: 461 case wgpu::TextureFormat::ASTC5x5UnormSrgb: 462 case wgpu::TextureFormat::ASTC6x5Unorm: 463 case wgpu::TextureFormat::ASTC6x5UnormSrgb: 464 case wgpu::TextureFormat::ASTC8x5Unorm: 465 case wgpu::TextureFormat::ASTC8x5UnormSrgb: 466 case wgpu::TextureFormat::ASTC10x5Unorm: 467 case wgpu::TextureFormat::ASTC10x5UnormSrgb: 468 return 5u; 469 case wgpu::TextureFormat::ASTC6x6Unorm: 470 case wgpu::TextureFormat::ASTC6x6UnormSrgb: 471 case wgpu::TextureFormat::ASTC8x6Unorm: 472 case wgpu::TextureFormat::ASTC8x6UnormSrgb: 473 case wgpu::TextureFormat::ASTC10x6Unorm: 474 case wgpu::TextureFormat::ASTC10x6UnormSrgb: 475 return 6u; 476 case wgpu::TextureFormat::ASTC8x8Unorm: 477 case wgpu::TextureFormat::ASTC8x8UnormSrgb: 478 case wgpu::TextureFormat::ASTC10x8Unorm: 479 case wgpu::TextureFormat::ASTC10x8UnormSrgb: 480 return 8u; 481 case wgpu::TextureFormat::ASTC10x10Unorm: 482 case wgpu::TextureFormat::ASTC10x10UnormSrgb: 483 case wgpu::TextureFormat::ASTC12x10Unorm: 484 case wgpu::TextureFormat::ASTC12x10UnormSrgb: 485 return 10u; 486 case wgpu::TextureFormat::ASTC12x12Unorm: 487 case wgpu::TextureFormat::ASTC12x12UnormSrgb: 488 return 12u; 489 490 // Block size of a multi-planar format depends on aspect. 491 case wgpu::TextureFormat::R8BG8Biplanar420Unorm: 492 493 // TODO(dawn:666): implement stencil8 494 case wgpu::TextureFormat::Stencil8: 495 case wgpu::TextureFormat::Undefined: 496 break; 497 } 498 UNREACHABLE(); 499 } 500 GetWGSLColorTextureComponentType(wgpu::TextureFormat textureFormat)501 const char* GetWGSLColorTextureComponentType(wgpu::TextureFormat textureFormat) { 502 switch (textureFormat) { 503 case wgpu::TextureFormat::R8Unorm: 504 case wgpu::TextureFormat::R8Snorm: 505 case wgpu::TextureFormat::R16Float: 506 case wgpu::TextureFormat::RG8Unorm: 507 case wgpu::TextureFormat::RG8Snorm: 508 case wgpu::TextureFormat::R32Float: 509 case wgpu::TextureFormat::RG16Float: 510 case wgpu::TextureFormat::RGBA8Unorm: 511 case wgpu::TextureFormat::RGBA8Snorm: 512 case wgpu::TextureFormat::RGB10A2Unorm: 513 case wgpu::TextureFormat::RG11B10Ufloat: 514 case wgpu::TextureFormat::RGB9E5Ufloat: 515 case wgpu::TextureFormat::RG32Float: 516 case wgpu::TextureFormat::RGBA16Float: 517 case wgpu::TextureFormat::RGBA32Float: 518 case wgpu::TextureFormat::BGRA8Unorm: 519 case wgpu::TextureFormat::BGRA8UnormSrgb: 520 case wgpu::TextureFormat::RGBA8UnormSrgb: 521 return "f32"; 522 523 case wgpu::TextureFormat::R8Uint: 524 case wgpu::TextureFormat::R16Uint: 525 case wgpu::TextureFormat::RG8Uint: 526 case wgpu::TextureFormat::R32Uint: 527 case wgpu::TextureFormat::RG16Uint: 528 case wgpu::TextureFormat::RGBA8Uint: 529 case wgpu::TextureFormat::RG32Uint: 530 case wgpu::TextureFormat::RGBA16Uint: 531 case wgpu::TextureFormat::RGBA32Uint: 532 return "u32"; 533 534 case wgpu::TextureFormat::R8Sint: 535 case wgpu::TextureFormat::R16Sint: 536 case wgpu::TextureFormat::RG8Sint: 537 case wgpu::TextureFormat::R32Sint: 538 case wgpu::TextureFormat::RG16Sint: 539 case wgpu::TextureFormat::RGBA8Sint: 540 case wgpu::TextureFormat::RG32Sint: 541 case wgpu::TextureFormat::RGBA16Sint: 542 case wgpu::TextureFormat::RGBA32Sint: 543 return "i32"; 544 545 default: 546 UNREACHABLE(); 547 } 548 } 549 GetWGSLRenderableColorTextureComponentCount(wgpu::TextureFormat textureFormat)550 uint32_t GetWGSLRenderableColorTextureComponentCount(wgpu::TextureFormat textureFormat) { 551 switch (textureFormat) { 552 case wgpu::TextureFormat::R8Unorm: 553 case wgpu::TextureFormat::R8Uint: 554 case wgpu::TextureFormat::R8Sint: 555 case wgpu::TextureFormat::R16Uint: 556 case wgpu::TextureFormat::R16Sint: 557 case wgpu::TextureFormat::R16Float: 558 case wgpu::TextureFormat::R32Float: 559 case wgpu::TextureFormat::R32Uint: 560 case wgpu::TextureFormat::R32Sint: 561 return 1u; 562 case wgpu::TextureFormat::RG8Unorm: 563 case wgpu::TextureFormat::RG8Uint: 564 case wgpu::TextureFormat::RG8Sint: 565 case wgpu::TextureFormat::RG16Uint: 566 case wgpu::TextureFormat::RG16Sint: 567 case wgpu::TextureFormat::RG16Float: 568 case wgpu::TextureFormat::RG32Float: 569 case wgpu::TextureFormat::RG32Uint: 570 case wgpu::TextureFormat::RG32Sint: 571 return 2u; 572 case wgpu::TextureFormat::RGBA8Unorm: 573 case wgpu::TextureFormat::RGBA8UnormSrgb: 574 case wgpu::TextureFormat::RGBA8Uint: 575 case wgpu::TextureFormat::RGBA8Sint: 576 case wgpu::TextureFormat::BGRA8Unorm: 577 case wgpu::TextureFormat::BGRA8UnormSrgb: 578 case wgpu::TextureFormat::RGB10A2Unorm: 579 case wgpu::TextureFormat::RGBA16Uint: 580 case wgpu::TextureFormat::RGBA16Sint: 581 case wgpu::TextureFormat::RGBA16Float: 582 case wgpu::TextureFormat::RGBA32Float: 583 case wgpu::TextureFormat::RGBA32Uint: 584 case wgpu::TextureFormat::RGBA32Sint: 585 return 4u; 586 default: 587 UNREACHABLE(); 588 } 589 } 590 GetWGSLImageFormatQualifier(wgpu::TextureFormat textureFormat)591 const char* GetWGSLImageFormatQualifier(wgpu::TextureFormat textureFormat) { 592 switch (textureFormat) { 593 case wgpu::TextureFormat::RGBA8Unorm: 594 return "rgba8unorm"; 595 case wgpu::TextureFormat::RGBA8Snorm: 596 return "rgba8snorm"; 597 case wgpu::TextureFormat::RGBA8Uint: 598 return "rgba8uint"; 599 case wgpu::TextureFormat::RGBA8Sint: 600 return "rgba8sint"; 601 case wgpu::TextureFormat::RGBA16Uint: 602 return "rgba16uint"; 603 case wgpu::TextureFormat::RGBA16Sint: 604 return "rgba16sint"; 605 case wgpu::TextureFormat::RGBA16Float: 606 return "rgba16float"; 607 case wgpu::TextureFormat::R32Uint: 608 return "r32uint"; 609 case wgpu::TextureFormat::R32Sint: 610 return "r32sint"; 611 case wgpu::TextureFormat::R32Float: 612 return "r32float"; 613 case wgpu::TextureFormat::RG32Uint: 614 return "rg32uint"; 615 case wgpu::TextureFormat::RG32Sint: 616 return "rg32sint"; 617 case wgpu::TextureFormat::RG32Float: 618 return "rg32float"; 619 case wgpu::TextureFormat::RGBA32Uint: 620 return "rgba32uint"; 621 case wgpu::TextureFormat::RGBA32Sint: 622 return "rgba32sint"; 623 case wgpu::TextureFormat::RGBA32Float: 624 return "rgba32float"; 625 626 // The below do not currently exist in the WGSL spec, but are used 627 // for tests that expect compilation failure. 628 case wgpu::TextureFormat::R8Unorm: 629 return "r8unorm"; 630 case wgpu::TextureFormat::R8Snorm: 631 return "r8snorm"; 632 case wgpu::TextureFormat::R8Uint: 633 return "r8uint"; 634 case wgpu::TextureFormat::R8Sint: 635 return "r8sint"; 636 case wgpu::TextureFormat::R16Uint: 637 return "r16uint"; 638 case wgpu::TextureFormat::R16Sint: 639 return "r16sint"; 640 case wgpu::TextureFormat::R16Float: 641 return "r16float"; 642 case wgpu::TextureFormat::RG8Unorm: 643 return "rg8unorm"; 644 case wgpu::TextureFormat::RG8Snorm: 645 return "rg8snorm"; 646 case wgpu::TextureFormat::RG8Uint: 647 return "rg8uint"; 648 case wgpu::TextureFormat::RG8Sint: 649 return "rg8sint"; 650 case wgpu::TextureFormat::RG16Uint: 651 return "rg16uint"; 652 case wgpu::TextureFormat::RG16Sint: 653 return "rg16sint"; 654 case wgpu::TextureFormat::RG16Float: 655 return "rg16float"; 656 case wgpu::TextureFormat::RGB10A2Unorm: 657 return "rgb10a2unorm"; 658 case wgpu::TextureFormat::RG11B10Ufloat: 659 return "rg11b10ufloat"; 660 661 default: 662 UNREACHABLE(); 663 } 664 } 665 ViewDimensionToTextureDimension(const wgpu::TextureViewDimension dimension)666 wgpu::TextureDimension ViewDimensionToTextureDimension( 667 const wgpu::TextureViewDimension dimension) { 668 switch (dimension) { 669 case wgpu::TextureViewDimension::e2D: 670 case wgpu::TextureViewDimension::e2DArray: 671 case wgpu::TextureViewDimension::Cube: 672 case wgpu::TextureViewDimension::CubeArray: 673 return wgpu::TextureDimension::e2D; 674 case wgpu::TextureViewDimension::e3D: 675 return wgpu::TextureDimension::e3D; 676 // TODO(crbug.com/dawn/814): Implement for 1D texture. 677 case wgpu::TextureViewDimension::e1D: 678 default: 679 UNREACHABLE(); 680 break; 681 } 682 } 683 684 } // namespace utils 685