| 1 |
/* |
| 2 |
* Copyright 2025 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/gpu/graphite/TextureFormat.h" |
| 9 |
|
| 10 |
#include "include/core/SkColor.h" |
| 11 |
|
| 12 |
namespace skgpu::graphite { |
| 13 |
|
| 14 |
const char* TextureFormatName(TextureFormat format) { |
| 15 |
switch (format) { |
| 16 |
case TextureFormat::kUnsupported: return "Unsupported"; |
| 17 |
case TextureFormat::kR8: return "R8"; |
| 18 |
case TextureFormat::kR16: return "R16"; |
| 19 |
case TextureFormat::kR16F: return "R16F"; |
| 20 |
case TextureFormat::kR32F: return "R32F"; |
| 21 |
case TextureFormat::kA8: return "A8"; |
| 22 |
case TextureFormat::kRG8: return "RG8"; |
| 23 |
case TextureFormat::kRG16: return "RG16"; |
| 24 |
case TextureFormat::kRG16F: return "RG16F"; |
| 25 |
case TextureFormat::kRG32F: return "RG32F"; |
| 26 |
case TextureFormat::kRGB8: return "RGB8"; |
| 27 |
case TextureFormat::kBGR8: return "BGR8"; |
| 28 |
case TextureFormat::kB5_G6_R5: return "B5_G6_R5"; |
| 29 |
case TextureFormat::kR5_G6_B5: return "R5_G6_B5"; |
| 30 |
case TextureFormat::kRGB16: return "RGB16"; |
| 31 |
case TextureFormat::kRGB16F: return "RGB16F"; |
| 32 |
case TextureFormat::kRGB32F: return "RGB32F"; |
| 33 |
case TextureFormat::kRGB8_sRGB: return "RGB8_sRGB"; |
| 34 |
case TextureFormat::kBGR10_XR: return "BGR10_XR"; |
| 35 |
case TextureFormat::kRGBA8: return "RGBA8"; |
| 36 |
case TextureFormat::kRGBA16: return "RBGA16"; |
| 37 |
case TextureFormat::kRGBA16F: return "RGBA16F"; |
| 38 |
case TextureFormat::kRGBA32F: return "RGBA32F"; |
| 39 |
case TextureFormat::kRGB10_A2: return "RGB10_A2"; |
| 40 |
case TextureFormat::kRGBA10x6: return "RGBA10x6"; |
| 41 |
case TextureFormat::kRGBA8_sRGB: return "RGBA8_sRGB"; |
| 42 |
case TextureFormat::kBGRA8: return "BGRA8"; |
| 43 |
case TextureFormat::kBGR10_A2: return "BGR10_A2"; |
| 44 |
case TextureFormat::kBGRA8_sRGB: return "BGRA8_sRGB"; |
| 45 |
case TextureFormat::kABGR4: return "ABGR4"; |
| 46 |
case TextureFormat::kARGB4: return "ARGB4"; |
| 47 |
case TextureFormat::kBGRA10x6_XR: return "BGRA10x6_XR"; |
| 48 |
case TextureFormat::kRGB8_ETC2: return "RGB8_ETC2"; |
| 49 |
case TextureFormat::kRGB8_ETC2_sRGB: return "RGB8_ETC2_sRGB"; |
| 50 |
case TextureFormat::kRGB8_BC1: return "RGB8_BC1"; |
| 51 |
case TextureFormat::kRGBA8_BC1: return "RGBA8_BC1"; |
| 52 |
case TextureFormat::kRGBA8_BC1_sRGB: return "RGBA8_BC1_sRGB"; |
| 53 |
case TextureFormat::kYUV8_P2_420: return "YUV8_P2_420"; |
| 54 |
case TextureFormat::kYUV8_P3_420: return "YUV8_P3_420"; |
| 55 |
case TextureFormat::kYUV10x6_P2_420: return "YUV10x6_P2_420"; |
| 56 |
case TextureFormat::kExternal: return "External"; |
| 57 |
case TextureFormat::kS8: return "S8"; |
| 58 |
case TextureFormat::kD16: return "D16"; |
| 59 |
case TextureFormat::kD32F: return "D32F"; |
| 60 |
case TextureFormat::kD24_S8: return "D24_S8"; |
| 61 |
case TextureFormat::kD32F_S8: return "D32F_S8"; |
| 62 |
} |
| 63 |
SkUNREACHABLE; |
| 64 |
} |
| 65 |
|
| 66 |
SkTextureCompressionType TextureFormatCompressionType(TextureFormat format) { |
| 67 |
switch (format) { |
| 68 |
case TextureFormat::kRGB8_ETC2: [[fallthrough]]; |
| 69 |
case TextureFormat::kRGB8_ETC2_sRGB: return SkTextureCompressionType::kETC2_RGB8_UNORM; |
| 70 |
case TextureFormat::kRGB8_BC1: return SkTextureCompressionType::kBC1_RGB8_UNORM; |
| 71 |
case TextureFormat::kRGBA8_BC1: [[fallthrough]]; |
| 72 |
case TextureFormat::kRGBA8_BC1_sRGB: return SkTextureCompressionType::kBC1_RGBA8_UNORM; |
| 73 |
default: return SkTextureCompressionType::kNone; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
size_t TextureFormatBytesPerBlock(TextureFormat format) { |
| 78 |
switch (format) { |
| 79 |
case TextureFormat::kUnsupported: return 0; |
| 80 |
case TextureFormat::kR8: return 1; |
| 81 |
case TextureFormat::kR16: return 2; |
| 82 |
case TextureFormat::kR16F: return 2; |
| 83 |
case TextureFormat::kR32F: return 4; |
| 84 |
case TextureFormat::kA8: return 1; |
| 85 |
case TextureFormat::kRG8: return 2; |
| 86 |
case TextureFormat::kRG16: return 4; |
| 87 |
case TextureFormat::kRG16F: return 4; |
| 88 |
case TextureFormat::kRG32F: return 8; |
| 89 |
case TextureFormat::kRGB8: return 3; |
| 90 |
case TextureFormat::kBGR8: return 3; |
| 91 |
case TextureFormat::kB5_G6_R5: return 2; |
| 92 |
case TextureFormat::kR5_G6_B5: return 2; |
| 93 |
case TextureFormat::kRGB16: return 6; |
| 94 |
case TextureFormat::kRGB16F: return 6; |
| 95 |
case TextureFormat::kRGB32F: return 12; |
| 96 |
case TextureFormat::kRGB8_sRGB: return 3; |
| 97 |
case TextureFormat::kBGR10_XR: return 4; |
| 98 |
case TextureFormat::kRGBA8: return 4; |
| 99 |
case TextureFormat::kRGBA16: return 8; |
| 100 |
case TextureFormat::kRGBA16F: return 8; |
| 101 |
case TextureFormat::kRGBA32F: return 16; |
| 102 |
case TextureFormat::kRGB10_A2: return 4; |
| 103 |
case TextureFormat::kRGBA10x6: return 8; |
| 104 |
case TextureFormat::kRGBA8_sRGB: return 4; |
| 105 |
case TextureFormat::kBGRA8: return 4; |
| 106 |
case TextureFormat::kBGR10_A2: return 4; |
| 107 |
case TextureFormat::kBGRA8_sRGB: return 4; |
| 108 |
case TextureFormat::kABGR4: return 2; |
| 109 |
case TextureFormat::kARGB4: return 2; |
| 110 |
case TextureFormat::kBGRA10x6_XR: return 8; |
| 111 |
case TextureFormat::kS8: return 1; |
| 112 |
case TextureFormat::kD16: return 2; |
| 113 |
case TextureFormat::kD32F: return 4; |
| 114 |
case TextureFormat::kD24_S8: return 4; |
| 115 |
case TextureFormat::kD32F_S8: return 8; |
| 116 |
// NOTE: For compressed formats, the block size refers to an actual compressed block of |
| 117 |
// multiple texels, whereas with other formats the block size represents a single pixel. |
| 118 |
case TextureFormat::kRGB8_ETC2: |
| 119 |
case TextureFormat::kRGB8_ETC2_sRGB: |
| 120 |
case TextureFormat::kRGB8_BC1: |
| 121 |
case TextureFormat::kRGBA8_BC1: |
| 122 |
case TextureFormat::kRGBA8_BC1_sRGB: |
| 123 |
return 8; |
| 124 |
// NOTE: We don't actually know the size of external formats, so this is an arbitrary value. |
| 125 |
// We will see external formats only in wrapped SkImages, so this won't impact Skia's |
| 126 |
// internal budgeting. |
| 127 |
case TextureFormat::kExternal: |
| 128 |
return 4; |
| 129 |
// TODO(b/401016699): We are just over estimating this value to be used in gpu size |
| 130 |
// calculations even though the actually size is probably less. We should instead treat |
| 131 |
// planar formats similar to compressed textures that go through their own special query for |
| 132 |
// calculating size. |
| 133 |
case TextureFormat::kYUV8_P2_420: |
| 134 |
case TextureFormat::kYUV8_P3_420: |
| 135 |
return 3; |
| 136 |
case TextureFormat::kYUV10x6_P2_420: |
| 137 |
return 6; |
| 138 |
} |
| 139 |
SkUNREACHABLE; |
| 140 |
} |
| 141 |
|
| 142 |
uint32_t TextureFormatChannelMask(TextureFormat format) { |
| 143 |
switch (format) { |
| 144 |
case TextureFormat::kA8: return kAlpha_SkColorChannelFlag; |
| 145 |
|
| 146 |
case TextureFormat::kR8: [[fallthrough]]; |
| 147 |
case TextureFormat::kR16: |
| 148 |
case TextureFormat::kR16F: |
| 149 |
case TextureFormat::kR32F: return kRed_SkColorChannelFlag; |
| 150 |
|
| 151 |
case TextureFormat::kRG8: [[fallthrough]]; |
| 152 |
case TextureFormat::kRG16: |
| 153 |
case TextureFormat::kRG16F: |
| 154 |
case TextureFormat::kRG32F: return kRG_SkColorChannelFlags; |
| 155 |
|
| 156 |
case TextureFormat::kRGB8: [[fallthrough]]; |
| 157 |
case TextureFormat::kBGR8: |
| 158 |
case TextureFormat::kB5_G6_R5: |
| 159 |
case TextureFormat::kR5_G6_B5: |
| 160 |
case TextureFormat::kRGB16: |
| 161 |
case TextureFormat::kRGB16F: |
| 162 |
case TextureFormat::kRGB32F: |
| 163 |
case TextureFormat::kRGB8_sRGB: |
| 164 |
case TextureFormat::kBGR10_XR: |
| 165 |
case TextureFormat::kRGB8_ETC2: |
| 166 |
case TextureFormat::kRGB8_ETC2_sRGB: |
| 167 |
case TextureFormat::kRGB8_BC1: |
| 168 |
case TextureFormat::kYUV8_P2_420: |
| 169 |
case TextureFormat::kYUV8_P3_420: |
| 170 |
case TextureFormat::kYUV10x6_P2_420: return kRGB_SkColorChannelFlags; |
| 171 |
|
| 172 |
case TextureFormat::kRGBA8: [[fallthrough]]; |
| 173 |
case TextureFormat::kRGBA16: |
| 174 |
case TextureFormat::kRGBA16F: |
| 175 |
case TextureFormat::kRGBA32F: |
| 176 |
case TextureFormat::kRGB10_A2: |
| 177 |
case TextureFormat::kRGBA10x6: |
| 178 |
case TextureFormat::kRGBA8_sRGB: |
| 179 |
case TextureFormat::kBGRA8: |
| 180 |
case TextureFormat::kBGR10_A2: |
| 181 |
case TextureFormat::kBGRA8_sRGB: |
| 182 |
case TextureFormat::kABGR4: |
| 183 |
case TextureFormat::kARGB4: |
| 184 |
case TextureFormat::kBGRA10x6_XR: |
| 185 |
case TextureFormat::kRGBA8_BC1: |
| 186 |
case TextureFormat::kRGBA8_BC1_sRGB: |
| 187 |
case TextureFormat::kExternal: return kRGBA_SkColorChannelFlags; |
| 188 |
|
| 189 |
case TextureFormat::kS8: [[fallthrough]]; |
| 190 |
case TextureFormat::kD16: |
| 191 |
case TextureFormat::kD32F: |
| 192 |
case TextureFormat::kD24_S8: |
| 193 |
case TextureFormat::kD32F_S8: |
| 194 |
case TextureFormat::kUnsupported: return 0; |
| 195 |
} |
| 196 |
SkUNREACHABLE; |
| 197 |
} |
| 198 |
|
| 199 |
bool TextureFormatAutoClamps(TextureFormat format) { |
| 200 |
// Floating point formats, extended range formats, and non-normalized integer formats do not |
| 201 |
// auto-clamp. Everything behaves like an unsigned normalized number. |
| 202 |
return !(TextureFormatIsFloatingPoint(format) || |
| 203 |
format == TextureFormat::kBGR10_XR || |
| 204 |
format == TextureFormat::kBGRA10x6_XR || |
| 205 |
format == TextureFormat::kS8); |
| 206 |
} |
| 207 |
|
| 208 |
bool TextureFormatIsFloatingPoint(TextureFormat format) { |
| 209 |
switch (format) { |
| 210 |
// Floating point formats |
| 211 |
case TextureFormat::kR16F: [[fallthrough]]; |
| 212 |
case TextureFormat::kR32F: |
| 213 |
case TextureFormat::kRG16F: |
| 214 |
case TextureFormat::kRG32F: |
| 215 |
case TextureFormat::kRGB16F: |
| 216 |
case TextureFormat::kRGB32F: |
| 217 |
case TextureFormat::kRGBA16F: |
| 218 |
case TextureFormat::kRGBA32F: |
| 219 |
case TextureFormat::kD32F: |
| 220 |
case TextureFormat::kD32F_S8: return true; |
| 221 |
|
| 222 |
// Everything else is unorm, unorm-srgb, fixed point, or integral |
| 223 |
case TextureFormat::kUnsupported: [[fallthrough]]; |
| 224 |
case TextureFormat::kR8: |
| 225 |
case TextureFormat::kR16: |
| 226 |
case TextureFormat::kA8: |
| 227 |
case TextureFormat::kRG8: |
| 228 |
case TextureFormat::kRG16: |
| 229 |
case TextureFormat::kRGB8: |
| 230 |
case TextureFormat::kBGR8: |
| 231 |
case TextureFormat::kB5_G6_R5: |
| 232 |
case TextureFormat::kR5_G6_B5: |
| 233 |
case TextureFormat::kRGB16: |
| 234 |
case TextureFormat::kRGB8_sRGB: |
| 235 |
case TextureFormat::kBGR10_XR: |
| 236 |
case TextureFormat::kRGBA8: |
| 237 |
case TextureFormat::kRGBA16: |
| 238 |
case TextureFormat::kRGB10_A2: |
| 239 |
case TextureFormat::kRGBA10x6: |
| 240 |
case TextureFormat::kRGBA8_sRGB: |
| 241 |
case TextureFormat::kBGRA8: |
| 242 |
case TextureFormat::kBGR10_A2: |
| 243 |
case TextureFormat::kBGRA8_sRGB: |
| 244 |
case TextureFormat::kABGR4: |
| 245 |
case TextureFormat::kARGB4: |
| 246 |
case TextureFormat::kBGRA10x6_XR: |
| 247 |
case TextureFormat::kRGB8_ETC2: |
| 248 |
case TextureFormat::kRGB8_ETC2_sRGB: |
| 249 |
case TextureFormat::kRGB8_BC1: |
| 250 |
case TextureFormat::kRGBA8_BC1: |
| 251 |
case TextureFormat::kRGBA8_BC1_sRGB: |
| 252 |
case TextureFormat::kYUV8_P2_420: |
| 253 |
case TextureFormat::kYUV8_P3_420: |
| 254 |
case TextureFormat::kYUV10x6_P2_420: |
| 255 |
case TextureFormat::kExternal: |
| 256 |
case TextureFormat::kS8: |
| 257 |
case TextureFormat::kD16: |
| 258 |
case TextureFormat::kD24_S8: return false; |
| 259 |
} |
| 260 |
SkUNREACHABLE; |
| 261 |
} |
| 262 |
|
| 263 |
bool TextureFormatIsDepthOrStencil(TextureFormat format) { |
| 264 |
switch (format) { |
| 265 |
case TextureFormat::kS8: [[fallthrough]]; |
| 266 |
case TextureFormat::kD16: |
| 267 |
case TextureFormat::kD32F: |
| 268 |
case TextureFormat::kD24_S8: |
| 269 |
case TextureFormat::kD32F_S8: |
| 270 |
return true; |
| 271 |
default: |
| 272 |
return false; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
bool TextureFormatHasDepth(TextureFormat format) { |
| 277 |
switch (format) { |
| 278 |
case TextureFormat::kD16: [[fallthrough]]; |
| 279 |
case TextureFormat::kD32F: |
| 280 |
case TextureFormat::kD24_S8: |
| 281 |
case TextureFormat::kD32F_S8: |
| 282 |
return true; |
| 283 |
default: |
| 284 |
return false; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
bool TextureFormatHasStencil(TextureFormat format) { |
| 289 |
switch (format) { |
| 290 |
case TextureFormat::kS8: [[fallthrough]]; |
| 291 |
case TextureFormat::kD24_S8: |
| 292 |
case TextureFormat::kD32F_S8: |
| 293 |
return true; |
| 294 |
default: |
| 295 |
return false; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
bool TextureFormatIsMultiplanar(TextureFormat format) { |
| 300 |
switch (format) { |
| 301 |
case TextureFormat::kYUV8_P2_420: [[fallthrough]]; |
| 302 |
case TextureFormat::kYUV8_P3_420: |
| 303 |
case TextureFormat::kYUV10x6_P2_420: |
| 304 |
return true; |
| 305 |
default: |
| 306 |
return false; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
} // namespace skgpu::graphite |