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 } supportsPartialLoadResolve()34 bool supportsPartialLoadResolve() const { return fSupportsPartialLoadResolve; } 35 36 TextureInfo getDefaultSampledTextureInfo(SkColorType, 37 Mipmapped mipmapped, 38 Protected, 39 Renderable) const override; 40 TextureInfo getTextureInfoForSampledCopy(const TextureInfo& textureInfo, 41 Mipmapped mipmapped) const override; 42 TextureInfo getDefaultCompressedTextureInfo(SkTextureCompressionType, 43 Mipmapped mipmapped, 44 Protected) const override; 45 TextureInfo getDefaultMSAATextureInfo(const TextureInfo& singleSampledInfo, 46 Discardable discardable) const override; 47 TextureInfo getDefaultDepthStencilTextureInfo(SkEnumBitMask<DepthStencilFlags>, 48 uint32_t sampleCount, 49 Protected) const override; 50 TextureInfo getDefaultStorageTextureInfo(SkColorType) const override; 51 SkISize getDepthAttachmentDimensions(const TextureInfo&, 52 const SkISize colorAttachmentDimensions) const override; 53 UniqueKey makeGraphicsPipelineKey(const GraphicsPipelineDesc&, 54 const RenderPassDesc&) const override; 55 bool extractGraphicsDescs(const UniqueKey&, 56 GraphicsPipelineDesc*, 57 RenderPassDesc*, 58 const RendererProvider*) const override; 59 UniqueKey makeComputePipelineKey(const ComputePipelineDesc&) const override; 60 ImmutableSamplerInfo getImmutableSamplerInfo(const TextureProxy* proxy) const override; 61 GraphiteResourceKey makeSamplerKey(const SamplerDesc&) const override; 62 uint32_t channelMask(const TextureInfo&) const override; 63 bool isRenderable(const TextureInfo&) const override; 64 bool isStorage(const TextureInfo&) const override; 65 loadOpAffectsMSAAPipelines()66 bool loadOpAffectsMSAAPipelines() const override { 67 return fSupportedResolveTextureLoadOp.has_value(); 68 } 69 70 void buildKeyForTexture(SkISize dimensions, 71 const TextureInfo&, 72 ResourceType, 73 Shareable, 74 GraphiteResourceKey*) const override; 75 uint32_t getRenderPassDescKeyForPipeline(const RenderPassDesc& renderPassDesc) const; 76 supportsCommandBufferTimestamps()77 bool supportsCommandBufferTimestamps() const { return fSupportsCommandBufferTimestamps; } 78 79 private: 80 const ColorTypeInfo* getColorTypeInfo(SkColorType, const TextureInfo&) const override; 81 bool onIsTexturable(const TextureInfo&) const override; 82 bool supportsWritePixels(const TextureInfo& textureInfo) const override; 83 bool supportsReadPixels(const TextureInfo& textureInfo) const override; 84 std::pair<SkColorType, bool /*isRGBFormat*/> supportedWritePixelsColorType( 85 SkColorType dstColorType, 86 const TextureInfo& dstTextureInfo, 87 SkColorType srcColorType) const override; 88 std::pair<SkColorType, bool /*isRGBFormat*/> supportedReadPixelsColorType( 89 SkColorType srcColorType, 90 const TextureInfo& srcTextureInfo, 91 SkColorType dstColorType) const override; 92 93 void initCaps(const DawnBackendContext& backendContext, const ContextOptions& options); 94 void initShaderCaps(const wgpu::Device& device); 95 void initFormatTable(const wgpu::Device& device); 96 getFormatFromColorType(SkColorType colorType)97 wgpu::TextureFormat getFormatFromColorType(SkColorType colorType) const { 98 int idx = static_cast<int>(colorType); 99 return fColorTypeToFormatTable[idx]; 100 } 101 102 uint32_t maxRenderTargetSampleCount(wgpu::TextureFormat format) const; 103 bool isTexturable(wgpu::TextureFormat format) const; 104 bool isRenderable(wgpu::TextureFormat format, uint32_t numSamples) const; 105 106 struct FormatInfo { colorTypeFlagsFormatInfo107 uint32_t colorTypeFlags(SkColorType colorType) const { 108 for (int i = 0; i < fColorTypeInfoCount; ++i) { 109 if (fColorTypeInfos[i].fColorType == colorType) { 110 return fColorTypeInfos[i].fFlags; 111 } 112 } 113 return 0; 114 } 115 116 enum { 117 kTexturable_Flag = 0x01, 118 kRenderable_Flag = 0x02, // Color attachment and blendable 119 kMSAA_Flag = 0x04, 120 kResolve_Flag = 0x08, 121 kStorage_Flag = 0x10, 122 }; 123 static const uint16_t kAllFlags = 124 kTexturable_Flag | kRenderable_Flag | kMSAA_Flag | kResolve_Flag | kStorage_Flag; 125 126 uint16_t fFlags = 0; 127 128 std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos; 129 int fColorTypeInfoCount = 0; 130 }; 131 // Size here must be at least the size of kFormats in DawnCaps.cpp. 132 static constexpr size_t kFormatCount = 17; 133 std::array<FormatInfo, kFormatCount> fFormatTable; 134 135 static size_t GetFormatIndex(wgpu::TextureFormat format); getFormatInfo(wgpu::TextureFormat format)136 const FormatInfo& getFormatInfo(wgpu::TextureFormat format) const { 137 size_t index = GetFormatIndex(format); 138 return fFormatTable[index]; 139 } 140 141 wgpu::TextureFormat fColorTypeToFormatTable[kSkColorTypeCnt]; 142 void setColorType(SkColorType, std::initializer_list<wgpu::TextureFormat> formats); 143 144 // When supported, this value will hold the TransientAttachment usage symbol that is only 145 // defined in Dawn native builds and not EMSCRIPTEN but this avoids having to #define guard it. 146 wgpu::TextureUsage fSupportedTransientAttachmentUsage = wgpu::TextureUsage::None; 147 // When supported this holds the ExpandResolveTexture load op, otherwise holds no value. 148 std::optional<wgpu::LoadOp> fSupportedResolveTextureLoadOp; 149 // When 'fSupportedResolveTextureLoadOp' is supported, it by default performs full size expand 150 // and resolve. With this feature, we can do that partially according to the actual damage 151 // region. 152 bool fSupportsPartialLoadResolve = false; 153 154 bool fUseAsyncPipelineCreation = true; 155 bool fAllowScopedErrorChecks = true; 156 157 bool fSupportsCommandBufferTimestamps = false; 158 }; 159 160 } // namespace skgpu::graphite 161 162 #endif // skgpu_graphite_DawnCaps_DEFINED 163