| 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 |
|
| 27 |
bool useAsyncPipelineCreation() const { return fUseAsyncPipelineCreation; } |
| 28 |
bool allowScopedErrorChecks() const { return fAllowScopedErrorChecks; } |
| 29 |
|
| 30 |
// If this has no value then loading the resolve texture via a LoadOp is not supported. |
| 31 |
std::optional<wgpu::LoadOp> resolveTextureLoadOp() const { |
| 32 |
return fSupportedResolveTextureLoadOp; |
| 33 |
} |
| 34 |
bool supportsPartialLoadResolve() const { return fSupportsPartialLoadResolve; } |
| 35 |
|
| 36 |
bool isSampleCountSupported(TextureFormat, SampleCount requestedSampleCount) const override; |
| 37 |
TextureFormat getDepthStencilFormat(SkEnumBitMask<DepthStencilFlags>) const override; |
| 38 |
|
| 39 |
TextureInfo getDefaultAttachmentTextureInfo(AttachmentDesc, |
| 40 |
Protected, |
| 41 |
Discardable) const override; |
| 42 |
TextureInfo getDefaultSampledTextureInfo(SkColorType, |
| 43 |
Mipmapped, |
| 44 |
Protected, |
| 45 |
Renderable) const override; |
| 46 |
TextureInfo getTextureInfoForSampledCopy(const TextureInfo&, Mipmapped) const override; |
| 47 |
TextureInfo getDefaultCompressedTextureInfo(SkTextureCompressionType, |
| 48 |
Mipmapped, |
| 49 |
Protected) const override; |
| 50 |
TextureInfo getDefaultStorageTextureInfo(SkColorType) const override; |
| 51 |
SkISize getDepthAttachmentDimensions(const TextureInfo&, |
| 52 |
const SkISize colorAttachmentDimensions) const override; |
| 53 |
|
| 54 |
UniqueKey makeGraphicsPipelineKey(const GraphicsPipelineDesc&, |
| 55 |
const RenderPassDesc&) const override; |
| 56 |
bool extractGraphicsDescs(const UniqueKey&, |
| 57 |
GraphicsPipelineDesc*, |
| 58 |
RenderPassDesc*, |
| 59 |
const RendererProvider*) const override; |
| 60 |
UniqueKey makeComputePipelineKey(const ComputePipelineDesc&) const override; |
| 61 |
ImmutableSamplerInfo getImmutableSamplerInfo(const TextureInfo&) const override; |
| 62 |
std::string toString(const ImmutableSamplerInfo&) const override; |
| 63 |
|
| 64 |
bool isRenderable(const TextureInfo&) const override; |
| 65 |
bool isStorage(const TextureInfo&) const override; |
| 66 |
|
| 67 |
bool loadOpAffectsMSAAPipelines() const override { |
| 68 |
return fSupportedResolveTextureLoadOp.has_value(); |
| 69 |
} |
| 70 |
|
| 71 |
void buildKeyForTexture(SkISize dimensions, |
| 72 |
const TextureInfo&, |
| 73 |
ResourceType, |
| 74 |
GraphiteResourceKey*) const override; |
| 75 |
// Compute render pass desc's key as 32 bits key. The key has room for additional flag which can |
| 76 |
// optionally be provided. |
| 77 |
uint32_t getRenderPassDescKeyForPipeline(const RenderPassDesc&, |
| 78 |
bool additionalFlag = false) const; |
| 79 |
|
| 80 |
bool supportsCommandBufferTimestamps() const { return fSupportsCommandBufferTimestamps; } |
| 81 |
|
| 82 |
// Whether we should emulate load/resolve with separate render passes. |
| 83 |
// TODO(b/399640773): This is currently used until Dawn supports true partial resolve feature |
| 84 |
// that can resolve a MSAA texture to a resolve texture with different size. |
| 85 |
bool emulateLoadStoreResolve() const { return fEmulateLoadStoreResolve; } |
| 86 |
|
| 87 |
// Check whether the texture is texturable, ignoring its sample count. This is needed |
| 88 |
// instead of isTextureable() because graphite frontend treats multisampled textures as |
| 89 |
// non-textureable. |
| 90 |
bool isTexturableIgnoreSampleCount(const TextureInfo& info) const; |
| 91 |
|
| 92 |
private: |
| 93 |
SkSpan<const ColorTypeInfo> getColorTypeInfos(const TextureInfo&) const override; |
| 94 |
bool onIsTexturable(const TextureInfo&) const override; |
| 95 |
bool supportsWritePixels(const TextureInfo&) const override; |
| 96 |
bool supportsReadPixels(const TextureInfo&) const override; |
| 97 |
|
| 98 |
void initCaps(const DawnBackendContext&, const ContextOptions&); |
| 99 |
void initShaderCaps(const wgpu::Device&); |
| 100 |
void initFormatTable(const wgpu::Device&); |
| 101 |
|
| 102 |
wgpu::TextureFormat getFormatFromColorType(SkColorType colorType) const { |
| 103 |
int idx = static_cast<int>(colorType); |
| 104 |
return fColorTypeToFormatTable[idx]; |
| 105 |
} |
| 106 |
|
| 107 |
struct FormatInfo { |
| 108 |
uint32_t colorTypeFlags(SkColorType colorType) const { |
| 109 |
for (int i = 0; i < fColorTypeInfoCount; ++i) { |
| 110 |
if (fColorTypeInfos[i].fColorType == colorType) { |
| 111 |
return fColorTypeInfos[i].fFlags; |
| 112 |
} |
| 113 |
} |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
enum { |
| 118 |
kTexturable_Flag = 0x01, |
| 119 |
kRenderable_Flag = 0x02, // Render attachment (color or depth/stencil) |
| 120 |
kMSAA_Flag = 0x04, |
| 121 |
kResolve_Flag = 0x08, |
| 122 |
kStorage_Flag = 0x10, |
| 123 |
}; |
| 124 |
static const uint16_t kAllFlags = |
| 125 |
kTexturable_Flag | kRenderable_Flag | kMSAA_Flag | kResolve_Flag | kStorage_Flag; |
| 126 |
|
| 127 |
uint16_t fFlags = 0; |
| 128 |
|
| 129 |
std::unique_ptr<ColorTypeInfo[]> fColorTypeInfos; |
| 130 |
int fColorTypeInfoCount = 0; |
| 131 |
}; |
| 132 |
// Size here must be at least the size of kFormats in DawnCaps.cpp. |
| 133 |
static constexpr size_t kFormatCount = 17; |
| 134 |
std::array<FormatInfo, kFormatCount> fFormatTable; |
| 135 |
|
| 136 |
static size_t GetFormatIndex(wgpu::TextureFormat format); |
| 137 |
const FormatInfo& getFormatInfo(wgpu::TextureFormat format) const { |
| 138 |
size_t index = GetFormatIndex(format); |
| 139 |
return fFormatTable[index]; |
| 140 |
} |
| 141 |
|
| 142 |
wgpu::TextureFormat fColorTypeToFormatTable[kSkColorTypeCnt]; |
| 143 |
void setColorType(SkColorType, std::initializer_list<wgpu::TextureFormat> formats); |
| 144 |
|
| 145 |
// When supported, this value will hold the TransientAttachment usage symbol that is only |
| 146 |
// defined in Dawn native builds and not EMSCRIPTEN but this avoids having to #define guard it. |
| 147 |
wgpu::TextureUsage fSupportedTransientAttachmentUsage = wgpu::TextureUsage::None; |
| 148 |
// When supported this holds the ExpandResolveTexture load op, otherwise holds no value. |
| 149 |
std::optional<wgpu::LoadOp> fSupportedResolveTextureLoadOp; |
| 150 |
// When 'fSupportedResolveTextureLoadOp' is supported, it by default performs full size expand |
| 151 |
// and resolve. With this feature, we can do that partially according to the actual damage |
| 152 |
// region. |
| 153 |
bool fSupportsPartialLoadResolve = false; |
| 154 |
|
| 155 |
bool fEmulateLoadStoreResolve = false; |
| 156 |
|
| 157 |
bool fUseAsyncPipelineCreation = true; |
| 158 |
bool fAllowScopedErrorChecks = true; |
| 159 |
|
| 160 |
bool fSupportsCommandBufferTimestamps = false; |
| 161 |
}; |
| 162 |
|
| 163 |
} // namespace skgpu::graphite |
| 164 |
|
| 165 |
#endif // skgpu_graphite_DawnCaps_DEFINED |