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 #ifndef UTILS_TEXTURE_UTILS_H_ 16 #define UTILS_TEXTURE_UTILS_H_ 17 18 #include <array> 19 20 #include <dawn/webgpu_cpp.h> 21 22 #include "common/Assert.h" 23 24 namespace utils { 25 // TODO(dawn:666): Add Stencil8 format when it's implemented. 26 static constexpr std::array<wgpu::TextureFormat, 94> kAllTextureFormats = { 27 wgpu::TextureFormat::R8Unorm, 28 wgpu::TextureFormat::R8Snorm, 29 wgpu::TextureFormat::R8Uint, 30 wgpu::TextureFormat::R8Sint, 31 wgpu::TextureFormat::R16Uint, 32 wgpu::TextureFormat::R16Sint, 33 wgpu::TextureFormat::R16Float, 34 wgpu::TextureFormat::RG8Unorm, 35 wgpu::TextureFormat::RG8Snorm, 36 wgpu::TextureFormat::RG8Uint, 37 wgpu::TextureFormat::RG8Sint, 38 wgpu::TextureFormat::R32Float, 39 wgpu::TextureFormat::R32Uint, 40 wgpu::TextureFormat::R32Sint, 41 wgpu::TextureFormat::RG16Uint, 42 wgpu::TextureFormat::RG16Sint, 43 wgpu::TextureFormat::RG16Float, 44 wgpu::TextureFormat::RGBA8Unorm, 45 wgpu::TextureFormat::RGBA8UnormSrgb, 46 wgpu::TextureFormat::RGBA8Snorm, 47 wgpu::TextureFormat::RGBA8Uint, 48 wgpu::TextureFormat::RGBA8Sint, 49 wgpu::TextureFormat::BGRA8Unorm, 50 wgpu::TextureFormat::BGRA8UnormSrgb, 51 wgpu::TextureFormat::RGB10A2Unorm, 52 wgpu::TextureFormat::RG11B10Ufloat, 53 wgpu::TextureFormat::RGB9E5Ufloat, 54 wgpu::TextureFormat::RG32Float, 55 wgpu::TextureFormat::RG32Uint, 56 wgpu::TextureFormat::RG32Sint, 57 wgpu::TextureFormat::RGBA16Uint, 58 wgpu::TextureFormat::RGBA16Sint, 59 wgpu::TextureFormat::RGBA16Float, 60 wgpu::TextureFormat::RGBA32Float, 61 wgpu::TextureFormat::RGBA32Uint, 62 wgpu::TextureFormat::RGBA32Sint, 63 wgpu::TextureFormat::Depth16Unorm, 64 wgpu::TextureFormat::Depth32Float, 65 wgpu::TextureFormat::Depth24Plus, 66 wgpu::TextureFormat::Depth24PlusStencil8, 67 wgpu::TextureFormat::Depth24UnormStencil8, 68 wgpu::TextureFormat::Depth32FloatStencil8, 69 wgpu::TextureFormat::BC1RGBAUnorm, 70 wgpu::TextureFormat::BC1RGBAUnormSrgb, 71 wgpu::TextureFormat::BC2RGBAUnorm, 72 wgpu::TextureFormat::BC2RGBAUnormSrgb, 73 wgpu::TextureFormat::BC3RGBAUnorm, 74 wgpu::TextureFormat::BC3RGBAUnormSrgb, 75 wgpu::TextureFormat::BC4RUnorm, 76 wgpu::TextureFormat::BC4RSnorm, 77 wgpu::TextureFormat::BC5RGUnorm, 78 wgpu::TextureFormat::BC5RGSnorm, 79 wgpu::TextureFormat::BC6HRGBUfloat, 80 wgpu::TextureFormat::BC6HRGBFloat, 81 wgpu::TextureFormat::BC7RGBAUnorm, 82 wgpu::TextureFormat::BC7RGBAUnormSrgb, 83 wgpu::TextureFormat::ETC2RGB8Unorm, 84 wgpu::TextureFormat::ETC2RGB8UnormSrgb, 85 wgpu::TextureFormat::ETC2RGB8A1Unorm, 86 wgpu::TextureFormat::ETC2RGB8A1UnormSrgb, 87 wgpu::TextureFormat::ETC2RGBA8Unorm, 88 wgpu::TextureFormat::ETC2RGBA8UnormSrgb, 89 wgpu::TextureFormat::EACR11Unorm, 90 wgpu::TextureFormat::EACR11Snorm, 91 wgpu::TextureFormat::EACRG11Unorm, 92 wgpu::TextureFormat::EACRG11Snorm, 93 wgpu::TextureFormat::ASTC4x4Unorm, 94 wgpu::TextureFormat::ASTC4x4UnormSrgb, 95 wgpu::TextureFormat::ASTC5x4Unorm, 96 wgpu::TextureFormat::ASTC5x4UnormSrgb, 97 wgpu::TextureFormat::ASTC5x5Unorm, 98 wgpu::TextureFormat::ASTC5x5UnormSrgb, 99 wgpu::TextureFormat::ASTC6x5Unorm, 100 wgpu::TextureFormat::ASTC6x5UnormSrgb, 101 wgpu::TextureFormat::ASTC6x6Unorm, 102 wgpu::TextureFormat::ASTC6x6UnormSrgb, 103 wgpu::TextureFormat::ASTC8x5Unorm, 104 wgpu::TextureFormat::ASTC8x5UnormSrgb, 105 wgpu::TextureFormat::ASTC8x6Unorm, 106 wgpu::TextureFormat::ASTC8x6UnormSrgb, 107 wgpu::TextureFormat::ASTC8x8Unorm, 108 wgpu::TextureFormat::ASTC8x8UnormSrgb, 109 wgpu::TextureFormat::ASTC10x5Unorm, 110 wgpu::TextureFormat::ASTC10x5UnormSrgb, 111 wgpu::TextureFormat::ASTC10x6Unorm, 112 wgpu::TextureFormat::ASTC10x6UnormSrgb, 113 wgpu::TextureFormat::ASTC10x8Unorm, 114 wgpu::TextureFormat::ASTC10x8UnormSrgb, 115 wgpu::TextureFormat::ASTC10x10Unorm, 116 wgpu::TextureFormat::ASTC10x10UnormSrgb, 117 wgpu::TextureFormat::ASTC12x10Unorm, 118 wgpu::TextureFormat::ASTC12x10UnormSrgb, 119 wgpu::TextureFormat::ASTC12x12Unorm, 120 wgpu::TextureFormat::ASTC12x12UnormSrgb}; 121 122 static constexpr std::array<wgpu::TextureFormat, 14> kBCFormats = { 123 wgpu::TextureFormat::BC1RGBAUnorm, wgpu::TextureFormat::BC1RGBAUnormSrgb, 124 wgpu::TextureFormat::BC2RGBAUnorm, wgpu::TextureFormat::BC2RGBAUnormSrgb, 125 wgpu::TextureFormat::BC3RGBAUnorm, wgpu::TextureFormat::BC3RGBAUnormSrgb, 126 wgpu::TextureFormat::BC4RUnorm, wgpu::TextureFormat::BC4RSnorm, 127 wgpu::TextureFormat::BC5RGUnorm, wgpu::TextureFormat::BC5RGSnorm, 128 wgpu::TextureFormat::BC6HRGBUfloat, wgpu::TextureFormat::BC6HRGBFloat, 129 wgpu::TextureFormat::BC7RGBAUnorm, wgpu::TextureFormat::BC7RGBAUnormSrgb}; 130 131 static constexpr std::array<wgpu::TextureFormat, 10> kETC2Formats = { 132 wgpu::TextureFormat::ETC2RGB8Unorm, wgpu::TextureFormat::ETC2RGB8UnormSrgb, 133 wgpu::TextureFormat::ETC2RGB8A1Unorm, wgpu::TextureFormat::ETC2RGB8A1UnormSrgb, 134 wgpu::TextureFormat::ETC2RGBA8Unorm, wgpu::TextureFormat::ETC2RGBA8UnormSrgb, 135 wgpu::TextureFormat::EACR11Unorm, wgpu::TextureFormat::EACR11Snorm, 136 wgpu::TextureFormat::EACRG11Unorm, wgpu::TextureFormat::EACRG11Snorm}; 137 138 static constexpr std::array<wgpu::TextureFormat, 28> kASTCFormats = { 139 wgpu::TextureFormat::ASTC4x4Unorm, wgpu::TextureFormat::ASTC4x4UnormSrgb, 140 wgpu::TextureFormat::ASTC5x4Unorm, wgpu::TextureFormat::ASTC5x4UnormSrgb, 141 wgpu::TextureFormat::ASTC5x5Unorm, wgpu::TextureFormat::ASTC5x5UnormSrgb, 142 wgpu::TextureFormat::ASTC6x5Unorm, wgpu::TextureFormat::ASTC6x5UnormSrgb, 143 wgpu::TextureFormat::ASTC6x6Unorm, wgpu::TextureFormat::ASTC6x6UnormSrgb, 144 wgpu::TextureFormat::ASTC8x5Unorm, wgpu::TextureFormat::ASTC8x5UnormSrgb, 145 wgpu::TextureFormat::ASTC8x6Unorm, wgpu::TextureFormat::ASTC8x6UnormSrgb, 146 wgpu::TextureFormat::ASTC8x8Unorm, wgpu::TextureFormat::ASTC8x8UnormSrgb, 147 wgpu::TextureFormat::ASTC10x5Unorm, wgpu::TextureFormat::ASTC10x5UnormSrgb, 148 wgpu::TextureFormat::ASTC10x6Unorm, wgpu::TextureFormat::ASTC10x6UnormSrgb, 149 wgpu::TextureFormat::ASTC10x8Unorm, wgpu::TextureFormat::ASTC10x8UnormSrgb, 150 wgpu::TextureFormat::ASTC10x10Unorm, wgpu::TextureFormat::ASTC10x10UnormSrgb, 151 wgpu::TextureFormat::ASTC12x10Unorm, wgpu::TextureFormat::ASTC12x10UnormSrgb, 152 wgpu::TextureFormat::ASTC12x12Unorm, wgpu::TextureFormat::ASTC12x12UnormSrgb, 153 }; 154 155 static constexpr std::array<wgpu::TextureFormat, 52> kCompressedFormats = { 156 wgpu::TextureFormat::BC1RGBAUnorm, wgpu::TextureFormat::BC1RGBAUnormSrgb, 157 wgpu::TextureFormat::BC2RGBAUnorm, wgpu::TextureFormat::BC2RGBAUnormSrgb, 158 wgpu::TextureFormat::BC3RGBAUnorm, wgpu::TextureFormat::BC3RGBAUnormSrgb, 159 wgpu::TextureFormat::BC4RUnorm, wgpu::TextureFormat::BC4RSnorm, 160 wgpu::TextureFormat::BC5RGUnorm, wgpu::TextureFormat::BC5RGSnorm, 161 wgpu::TextureFormat::BC6HRGBUfloat, wgpu::TextureFormat::BC6HRGBFloat, 162 wgpu::TextureFormat::BC7RGBAUnorm, wgpu::TextureFormat::BC7RGBAUnormSrgb, 163 wgpu::TextureFormat::ETC2RGB8Unorm, wgpu::TextureFormat::ETC2RGB8UnormSrgb, 164 wgpu::TextureFormat::ETC2RGB8A1Unorm, wgpu::TextureFormat::ETC2RGB8A1UnormSrgb, 165 wgpu::TextureFormat::ETC2RGBA8Unorm, wgpu::TextureFormat::ETC2RGBA8UnormSrgb, 166 wgpu::TextureFormat::EACR11Unorm, wgpu::TextureFormat::EACR11Snorm, 167 wgpu::TextureFormat::EACRG11Unorm, wgpu::TextureFormat::EACRG11Snorm, 168 wgpu::TextureFormat::ASTC4x4Unorm, wgpu::TextureFormat::ASTC4x4UnormSrgb, 169 wgpu::TextureFormat::ASTC5x4Unorm, wgpu::TextureFormat::ASTC5x4UnormSrgb, 170 wgpu::TextureFormat::ASTC5x5Unorm, wgpu::TextureFormat::ASTC5x5UnormSrgb, 171 wgpu::TextureFormat::ASTC6x5Unorm, wgpu::TextureFormat::ASTC6x5UnormSrgb, 172 wgpu::TextureFormat::ASTC6x6Unorm, wgpu::TextureFormat::ASTC6x6UnormSrgb, 173 wgpu::TextureFormat::ASTC8x5Unorm, wgpu::TextureFormat::ASTC8x5UnormSrgb, 174 wgpu::TextureFormat::ASTC8x6Unorm, wgpu::TextureFormat::ASTC8x6UnormSrgb, 175 wgpu::TextureFormat::ASTC8x8Unorm, wgpu::TextureFormat::ASTC8x8UnormSrgb, 176 wgpu::TextureFormat::ASTC10x5Unorm, wgpu::TextureFormat::ASTC10x5UnormSrgb, 177 wgpu::TextureFormat::ASTC10x6Unorm, wgpu::TextureFormat::ASTC10x6UnormSrgb, 178 wgpu::TextureFormat::ASTC10x8Unorm, wgpu::TextureFormat::ASTC10x8UnormSrgb, 179 wgpu::TextureFormat::ASTC10x10Unorm, wgpu::TextureFormat::ASTC10x10UnormSrgb, 180 wgpu::TextureFormat::ASTC12x10Unorm, wgpu::TextureFormat::ASTC12x10UnormSrgb, 181 wgpu::TextureFormat::ASTC12x12Unorm, wgpu::TextureFormat::ASTC12x12UnormSrgb}; 182 static_assert(kCompressedFormats.size() == 183 kBCFormats.size() + kETC2Formats.size() + kASTCFormats.size(), 184 "Number of compressed format must equal number of BC, ETC2, and ASTC formats."); 185 186 // TODO(dawn:666): Add Stencil8 format when it's implemented. 187 static constexpr std::array<wgpu::TextureFormat, 6> kDepthFormats = { 188 wgpu::TextureFormat::Depth16Unorm, wgpu::TextureFormat::Depth32Float, 189 wgpu::TextureFormat::Depth24Plus, wgpu::TextureFormat::Depth24PlusStencil8, 190 wgpu::TextureFormat::Depth24UnormStencil8, wgpu::TextureFormat::Depth32FloatStencil8, 191 }; 192 static constexpr std::array<wgpu::TextureFormat, 3> kStencilFormats = { 193 wgpu::TextureFormat::Depth24PlusStencil8, 194 wgpu::TextureFormat::Depth24UnormStencil8, 195 wgpu::TextureFormat::Depth32FloatStencil8, 196 }; 197 static constexpr std::array<wgpu::TextureFormat, 3> kDepthAndStencilFormats = { 198 wgpu::TextureFormat::Depth24PlusStencil8, 199 wgpu::TextureFormat::Depth24UnormStencil8, 200 wgpu::TextureFormat::Depth32FloatStencil8, 201 }; 202 203 bool TextureFormatSupportsStorageTexture(wgpu::TextureFormat format); 204 205 bool IsBCTextureFormat(wgpu::TextureFormat textureFormat); 206 bool IsETC2TextureFormat(wgpu::TextureFormat textureFormat); 207 bool IsASTCTextureFormat(wgpu::TextureFormat textureFormat); 208 209 bool IsDepthOnlyFormat(wgpu::TextureFormat textureFormat); 210 211 uint32_t GetTexelBlockSizeInBytes(wgpu::TextureFormat textureFormat); 212 uint32_t GetTextureFormatBlockWidth(wgpu::TextureFormat textureFormat); 213 uint32_t GetTextureFormatBlockHeight(wgpu::TextureFormat textureFormat); 214 215 const char* GetWGSLColorTextureComponentType(wgpu::TextureFormat textureFormat); 216 const char* GetWGSLImageFormatQualifier(wgpu::TextureFormat textureFormat); 217 uint32_t GetWGSLRenderableColorTextureComponentCount(wgpu::TextureFormat textureFormat); 218 219 wgpu::TextureDimension ViewDimensionToTextureDimension( 220 const wgpu::TextureViewDimension dimension); 221 } // namespace utils 222 223 #endif 224