xref: /aosp_15_r20/external/skia/src/gpu/graphite/dawn/DawnUtils.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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)21 size_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         // Note: We don't actually know the size of external formats, so this
45         // is an arbitrary value. We will see external formats only in wrapped
46         // SkImages, so this won't impact Skia's internal budgeting.
47         case wgpu::TextureFormat::External:
48             return 4;
49 #endif
50         default:
51             SkUNREACHABLE;
52     }
53 }
54 
DawnFormatToCompressionType(wgpu::TextureFormat format)55 SkTextureCompressionType DawnFormatToCompressionType(wgpu::TextureFormat format) {
56     switch (format) {
57         case wgpu::TextureFormat::ETC2RGB8Unorm: return SkTextureCompressionType::kETC2_RGB8_UNORM;
58         case wgpu::TextureFormat::BC1RGBAUnorm:  return SkTextureCompressionType::kBC1_RGBA8_UNORM;
59         default:                                 return SkTextureCompressionType::kNone;
60     }
61 }
62 
DawnFormatChannels(wgpu::TextureFormat format)63 uint32_t DawnFormatChannels(wgpu::TextureFormat format) {
64     switch (format) {
65         case wgpu::TextureFormat::RGBA8Unorm:   return kRGBA_SkColorChannelFlags;
66         case wgpu::TextureFormat::BGRA8Unorm:   return kRGBA_SkColorChannelFlags;
67         case wgpu::TextureFormat::R8Unorm:      return kRed_SkColorChannelFlag;
68         case wgpu::TextureFormat::RGBA16Float:  return kRGBA_SkColorChannelFlags;
69         case wgpu::TextureFormat::R16Float:     return kRed_SkColorChannelFlag;
70         case wgpu::TextureFormat::RG8Unorm:     return kRG_SkColorChannelFlags;
71         case wgpu::TextureFormat::RGB10A2Unorm: return kRGBA_SkColorChannelFlags;
72         case wgpu::TextureFormat::RG16Float:    return kRG_SkColorChannelFlags;
73 
74 #if !defined(__EMSCRIPTEN__)
75         case wgpu::TextureFormat::R16Unorm:     return kRed_SkColorChannelFlag;
76         case wgpu::TextureFormat::RG16Unorm:    return kRG_SkColorChannelFlags;
77 #endif
78 
79         default:                                return 0;
80     }
81     SkUNREACHABLE;
82 }
83 
84 } // namespace skgpu::graphite
85