• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 #ifndef skgpu_graphite_DawnCaps_DEFINED
9 #define skgpu_graphite_DawnCaps_DEFINED
10 
11 #include "src/gpu/graphite/Caps.h"
12 
13 #include <array>
14 
15 #include "webgpu/webgpu_cpp.h"  // NO_G3_REWRITE
16 
17 namespace skgpu::graphite {
18 struct ContextOptions;
19 
20 struct DawnBackendContext;
21 
22 class DawnCaps final : public Caps {
23 public:
24     DawnCaps(const DawnBackendContext&, const ContextOptions&);
25     ~DawnCaps() override;
26 
useAsyncPipelineCreation()27     bool useAsyncPipelineCreation() const { return fUseAsyncPipelineCreation; }
allowScopedErrorChecks()28     bool allowScopedErrorChecks() const { return fAllowScopedErrorChecks; }
29 
30     // If this has no value then loading the resolve texture via a LoadOp is not supported.
resolveTextureLoadOp()31     std::optional<wgpu::LoadOp> resolveTextureLoadOp() const {
32         return fSupportedResolveTextureLoadOp;
33     }
34 
35     TextureInfo getDefaultSampledTextureInfo(SkColorType,
36                                              Mipmapped mipmapped,
37                                              Protected,
38                                              Renderable) const override;
39     TextureInfo getTextureInfoForSampledCopy(const TextureInfo& textureInfo,
40                                              Mipmapped mipmapped) const override;
41     TextureInfo getDefaultCompressedTextureInfo(SkTextureCompressionType,
42                                                 Mipmapped mipmapped,
43                                                 Protected) const override;
44     TextureInfo getDefaultMSAATextureInfo(const TextureInfo& singleSampledInfo,
45                                           Discardable discardable) const override;
46     TextureInfo getDefaultDepthStencilTextureInfo(SkEnumBitMask<DepthStencilFlags>,
47                                                   uint32_t sampleCount,
48                                                   Protected) const override;
49     TextureInfo getDefaultStorageTextureInfo(SkColorType) const override;
50     SkISize getDepthAttachmentDimensions(const TextureInfo&,
51                                          const SkISize colorAttachmentDimensions) const override;
52     UniqueKey makeGraphicsPipelineKey(const GraphicsPipelineDesc&,
53                                       const RenderPassDesc&) const override;
54     UniqueKey makeComputePipelineKey(const ComputePipelineDesc&) const override;
55     uint32_t channelMask(const TextureInfo&) const override;
56     bool isRenderable(const TextureInfo&) const override;
57     bool isStorage(const TextureInfo&) const override;
58     void buildKeyForTexture(SkISize dimensions,
59                             const TextureInfo&,
60                             ResourceType,
61                             Shareable,
62                             GraphiteResourceKey*) const override;
63     uint64_t getRenderPassDescKeyForPipeline(const RenderPassDesc& renderPassDesc) const;
64 
65     static constexpr size_t kFormatCnt = 18;
66 
67 private:
68     const ColorTypeInfo* getColorTypeInfo(SkColorType, const TextureInfo&) const override;
69     bool onIsTexturable(const TextureInfo&) const override;
70     bool supportsWritePixels(const TextureInfo& textureInfo) const override;
71     bool supportsReadPixels(const TextureInfo& textureInfo) const override;
72     std::pair<SkColorType, bool /*isRGBFormat*/> supportedWritePixelsColorType(
73             SkColorType dstColorType,
74             const TextureInfo& dstTextureInfo,
75             SkColorType srcColorType) const override;
76     std::pair<SkColorType, bool /*isRGBFormat*/> supportedReadPixelsColorType(
77             SkColorType srcColorType,
78             const TextureInfo& srcTextureInfo,
79             SkColorType dstColorType) const override;
80 
81     void initCaps(const DawnBackendContext& backendContext, const ContextOptions& options);
82     void initShaderCaps(const wgpu::Device& device);
83     void initFormatTable(const wgpu::Device& device);
84 
getFormatFromColorType(SkColorType colorType)85     wgpu::TextureFormat getFormatFromColorType(SkColorType colorType) const {
86         int idx = static_cast<int>(colorType);
87         return fColorTypeToFormatTable[idx];
88     }
89 
90     uint32_t maxRenderTargetSampleCount(wgpu::TextureFormat format) const;
91     bool isTexturable(wgpu::TextureFormat format) const;
92     bool isRenderable(wgpu::TextureFormat format, uint32_t numSamples) const;
93 
94     struct FormatInfo {
colorTypeFlagsFormatInfo95         uint32_t colorTypeFlags(SkColorType colorType) const {
96             for (int i = 0; i < fColorTypeInfoCount; ++i) {
97                 if (fColorTypeInfos[i].fColorType == colorType) {
98                     return fColorTypeInfos[i].fFlags;
99                 }
100             }
101             return 0;
102         }
103 
104         enum {
105             kTexturable_Flag  = 0x01,
106             kRenderable_Flag  = 0x02, // Color attachment and blendable
107             kMSAA_Flag        = 0x04,
108             kResolve_Flag     = 0x08,
109             kStorage_Flag     = 0x10,
110         };
111         static const uint16_t kAllFlags =
112                 kTexturable_Flag | kRenderable_Flag | kMSAA_Flag | kResolve_Flag | kStorage_Flag;
113 
114         uint16_t fFlags = 0;
115 
116         std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos;
117         int fColorTypeInfoCount = 0;
118     };
119     // Size here must match size of kFormats in DawnCaps.cpp
120     std::array<FormatInfo, kFormatCnt> fFormatTable;
121 
122     static size_t GetFormatIndex(wgpu::TextureFormat format);
getFormatInfo(wgpu::TextureFormat format)123     const FormatInfo& getFormatInfo(wgpu::TextureFormat format) const {
124         size_t index = GetFormatIndex(format);
125         return fFormatTable[index];
126     }
127 
128     wgpu::TextureFormat fColorTypeToFormatTable[kSkColorTypeCnt];
129     void setColorType(SkColorType, std::initializer_list<wgpu::TextureFormat> formats);
130 
131     // When supported, this value will hold the TransientAttachment usage symbol that is only
132     // defined in Dawn native builds and not EMSCRIPTEN but this avoids having to #define guard it.
133     wgpu::TextureUsage fSupportedTransientAttachmentUsage = wgpu::TextureUsage::None;
134     // When supported this holds the ExpandResolveTexture load op, otherwise holds no value.
135     std::optional<wgpu::LoadOp> fSupportedResolveTextureLoadOp;
136 
137     bool fUseAsyncPipelineCreation = true;
138     bool fAllowScopedErrorChecks = true;
139 };
140 
141 } // namespace skgpu::graphite
142 
143 #endif // skgpu_graphite_DawnCaps_DEFINED
144