1 /* 2 * Copyright 2023 Google Inc. 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/dawn/DawnUtilsPriv.h" 9 10 #include "include/core/SkColor.h" 11 #include "include/core/SkTypes.h" 12 #include "src/gpu/graphite/dawn/DawnSharedContext.h" 13 14 #if defined(__EMSCRIPTEN__) 15 #include <emscripten.h> 16 #endif // __EMSCRIPTEN__ 17 18 namespace skgpu::graphite { 19 20 // TODO: A lot of these values are not correct DawnFormatBytesPerBlock(wgpu::TextureFormat format)21size_t DawnFormatBytesPerBlock(wgpu::TextureFormat format) { 22 switch (format) { 23 case wgpu::TextureFormat::RGBA8Unorm: return 4; 24 case wgpu::TextureFormat::BGRA8Unorm: return 4; 25 case wgpu::TextureFormat::R8Unorm: return 1; 26 case wgpu::TextureFormat::RGBA16Float: return 8; 27 case wgpu::TextureFormat::R16Float: return 2; 28 case wgpu::TextureFormat::RG8Unorm: return 2; 29 case wgpu::TextureFormat::RGB10A2Unorm: return 4; 30 case wgpu::TextureFormat::RG16Float: return 4; 31 // The depth stencil values are not neccessarily correct in Dawn since Dawn is allowed to 32 // implement Stencil8 as a real stencil8 or depth24stencil8 format. Similarly the depth in 33 // Depth24PlusStencil8 can either be a 24 bit value or Depth32Float value. There is also 34 // currently no way to query this in WebGPU so we just use the highest values here. 35 case wgpu::TextureFormat::Stencil8: return 4; // could be backed by d24s8 36 case wgpu::TextureFormat::Depth16Unorm: return 2; 37 case wgpu::TextureFormat::Depth32Float: return 4; 38 case wgpu::TextureFormat::Depth32FloatStencil8: return 5; 39 case wgpu::TextureFormat::Depth24PlusStencil8: return 5; // could be backed by d32s8 40 41 #if !defined(__EMSCRIPTEN__) 42 case wgpu::TextureFormat::R16Unorm: return 2; 43 case wgpu::TextureFormat::RG16Unorm: return 4; 44 #endif 45 default: 46 SkUNREACHABLE; 47 } 48 } 49 DawnFormatToCompressionType(wgpu::TextureFormat format)50SkTextureCompressionType DawnFormatToCompressionType(wgpu::TextureFormat format) { 51 switch (format) { 52 case wgpu::TextureFormat::ETC2RGB8Unorm: return SkTextureCompressionType::kETC2_RGB8_UNORM; 53 case wgpu::TextureFormat::BC1RGBAUnorm: return SkTextureCompressionType::kBC1_RGBA8_UNORM; 54 default: return SkTextureCompressionType::kNone; 55 } 56 } 57 DawnFormatChannels(wgpu::TextureFormat format)58uint32_t DawnFormatChannels(wgpu::TextureFormat format) { 59 switch (format) { 60 case wgpu::TextureFormat::RGBA8Unorm: return kRGBA_SkColorChannelFlags; 61 case wgpu::TextureFormat::BGRA8Unorm: return kRGBA_SkColorChannelFlags; 62 case wgpu::TextureFormat::R8Unorm: return kRed_SkColorChannelFlag; 63 case wgpu::TextureFormat::RGBA16Float: return kRGBA_SkColorChannelFlags; 64 case wgpu::TextureFormat::R16Float: return kRed_SkColorChannelFlag; 65 case wgpu::TextureFormat::RG8Unorm: return kRG_SkColorChannelFlags; 66 case wgpu::TextureFormat::RGB10A2Unorm: return kRGBA_SkColorChannelFlags; 67 case wgpu::TextureFormat::RG16Float: return kRG_SkColorChannelFlags; 68 69 #if !defined(__EMSCRIPTEN__) 70 case wgpu::TextureFormat::R16Unorm: return kRed_SkColorChannelFlag; 71 case wgpu::TextureFormat::RG16Unorm: return kRG_SkColorChannelFlags; 72 #endif 73 74 default: return 0; 75 } 76 SkUNREACHABLE; 77 } 78 79 } // namespace skgpu::graphite 80