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" 16 17 namespace skgpu::graphite { 18 struct ContextOptions; 19 20 class DawnCaps final : public Caps { 21 public: 22 DawnCaps(const wgpu::Device&, const ContextOptions&); 23 ~DawnCaps() override; 24 25 TextureInfo getDefaultSampledTextureInfo(SkColorType, 26 Mipmapped mipmapped, 27 Protected, 28 Renderable) const override; 29 TextureInfo getDefaultMSAATextureInfo(const TextureInfo& singleSampledInfo, 30 Discardable discardable) const override; 31 TextureInfo getDefaultDepthStencilTextureInfo(SkEnumBitMask<DepthStencilFlags>, 32 uint32_t sampleCount, 33 Protected) const override; 34 UniqueKey makeGraphicsPipelineKey(const GraphicsPipelineDesc&, 35 const RenderPassDesc&) const override; 36 UniqueKey makeComputePipelineKey(const ComputePipelineDesc&) const override; 37 bool isRenderable(const TextureInfo&) const override; 38 void buildKeyForTexture(SkISize dimensions, 39 const TextureInfo&, 40 ResourceType, 41 Shareable, 42 GraphiteResourceKey*) const override; 43 uint64_t getRenderPassDescKey(const RenderPassDesc& renderPassDesc) const; 44 45 private: 46 const ColorTypeInfo* getColorTypeInfo(SkColorType, const TextureInfo&) const override; 47 bool onIsTexturable(const TextureInfo&) const override; 48 bool supportsWritePixels(const TextureInfo& textureInfo) const override; 49 bool supportsReadPixels(const TextureInfo& textureInfo) const override; 50 SkColorType supportedWritePixelsColorType(SkColorType dstColorType, 51 const TextureInfo& dstTextureInfo, 52 SkColorType srcColorType) const override; 53 SkColorType supportedReadPixelsColorType(SkColorType srcColorType, 54 const TextureInfo& srcTextureInfo, 55 SkColorType dstColorType) const override; 56 57 void initCaps(const wgpu::Device& device); 58 void initShaderCaps(); 59 void initFormatTable(const wgpu::Device& device); 60 getFormatFromColorType(SkColorType colorType)61 wgpu::TextureFormat getFormatFromColorType(SkColorType colorType) const { 62 int idx = static_cast<int>(colorType); 63 return fColorTypeToFormatTable[idx]; 64 } 65 66 uint32_t maxRenderTargetSampleCount(wgpu::TextureFormat format) const; 67 bool isTexturable(wgpu::TextureFormat format) const; 68 bool isRenderable(wgpu::TextureFormat format, uint32_t numSamples) const; 69 70 struct FormatInfo { colorTypeFlagsFormatInfo71 uint32_t colorTypeFlags(SkColorType colorType) const { 72 for (int i = 0; i < fColorTypeInfoCount; ++i) { 73 if (fColorTypeInfos[i].fColorType == colorType) { 74 return fColorTypeInfos[i].fFlags; 75 } 76 } 77 return 0; 78 } 79 80 enum { 81 kTexturable_Flag = 0x1, 82 kRenderable_Flag = 0x2, // Color attachment and blendable 83 kMSAA_Flag = 0x4, 84 kResolve_Flag = 0x8, 85 }; 86 static const uint16_t kAllFlags = kTexturable_Flag | kRenderable_Flag | 87 kMSAA_Flag | kResolve_Flag; 88 89 uint16_t fFlags = 0; 90 91 std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos; 92 int fColorTypeInfoCount = 0; 93 }; 94 std::array<FormatInfo, 8> fFormatTable; 95 96 static size_t GetFormatIndex(wgpu::TextureFormat format); getFormatInfo(wgpu::TextureFormat format)97 const FormatInfo& getFormatInfo(wgpu::TextureFormat format) const { 98 size_t index = GetFormatIndex(format); 99 return fFormatTable[index]; 100 } 101 102 wgpu::TextureFormat fColorTypeToFormatTable[kSkColorTypeCnt]; 103 void setColorType(SkColorType, std::initializer_list<wgpu::TextureFormat> formats); 104 }; 105 106 } // namespace skgpu::graphite 107 108 #endif // skgpu_graphite_DawnCaps_DEFINED 109