| 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 |
#include "src/gpu/graphite/dawn/DawnSharedContext.h" |
| 9 |
|
| 10 |
#include "include/gpu/graphite/Context.h" |
| 11 |
#include "include/gpu/graphite/ContextOptions.h" |
| 12 |
#include "include/gpu/graphite/dawn/DawnBackendContext.h" |
| 13 |
#include "src/gpu/graphite/Log.h" |
| 14 |
#include "src/gpu/graphite/dawn/DawnGraphicsPipeline.h" |
| 15 |
#include "src/gpu/graphite/dawn/DawnResourceProvider.h" |
| 16 |
|
| 17 |
#include "webgpu/webgpu_cpp.h" // NO_G3_REWRITE |
| 18 |
|
| 19 |
namespace skgpu::graphite { |
| 20 |
namespace { |
| 21 |
|
| 22 |
wgpu::ShaderModule CreateNoopFragment(const wgpu::Device& device) { |
| 23 |
#if defined(__EMSCRIPTEN__) |
| 24 |
wgpu::ShaderModuleWGSLDescriptor wgslDesc; |
| 25 |
#else |
| 26 |
wgpu::ShaderSourceWGSL wgslDesc; |
| 27 |
#endif |
| 28 |
wgslDesc.code = |
| 29 |
"@fragment\n" |
| 30 |
"fn main() {}\n"; |
| 31 |
wgpu::ShaderModuleDescriptor smDesc; |
| 32 |
smDesc.nextInChain = &wgslDesc; |
| 33 |
smDesc.label = "no-op"; |
| 34 |
auto fsModule = device.CreateShaderModule(&smDesc); |
| 35 |
return fsModule; |
| 36 |
} |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
sk_sp<SharedContext> DawnSharedContext::Make(const DawnBackendContext& backendContext, |
| 41 |
const ContextOptions& options) { |
| 42 |
if (!backendContext.fDevice || !backendContext.fQueue) { |
| 43 |
return {}; |
| 44 |
} |
| 45 |
|
| 46 |
auto noopFragment = CreateNoopFragment(backendContext.fDevice); |
| 47 |
if (!noopFragment) { |
| 48 |
return {}; |
| 49 |
} |
| 50 |
|
| 51 |
auto caps = std::make_unique<const DawnCaps>(backendContext, options); |
| 52 |
|
| 53 |
return sk_sp<SharedContext>(new DawnSharedContext(backendContext, |
| 54 |
std::move(caps), |
| 55 |
std::move(noopFragment), |
| 56 |
options.fExecutor, |
| 57 |
options.fUserDefinedKnownRuntimeEffects)); |
| 58 |
} |
| 59 |
|
| 60 |
DawnSharedContext::DawnSharedContext(const DawnBackendContext& backendContext, |
| 61 |
std::unique_ptr<const DawnCaps> caps, |
| 62 |
wgpu::ShaderModule noopFragment, |
| 63 |
SkExecutor* executor, |
| 64 |
SkSpan<sk_sp<SkRuntimeEffect>> userDefinedKnownRuntimeEffects) |
| 65 |
: SharedContext(std::move(caps), |
| 66 |
BackendApi::kDawn, |
| 67 |
executor, |
| 68 |
userDefinedKnownRuntimeEffects) |
| 69 |
, fInstance(backendContext.fInstance) |
| 70 |
, fDevice(backendContext.fDevice) |
| 71 |
, fQueue(backendContext.fQueue) |
| 72 |
, fTick(backendContext.fTick) |
| 73 |
, fNoopFragment(std::move(noopFragment)) { |
| 74 |
fThreadSafeResourceProvider = std::make_unique<DawnThreadSafeResourceProvider>( |
| 75 |
this->makeResourceProvider(&fSingleOwner, |
| 76 |
SK_InvalidGenID, |
| 77 |
kThreadedSafeResourceBudget)); |
| 78 |
|
| 79 |
this->createUniformBuffersBindGroupLayout(); |
| 80 |
this->createSingleTextureSamplerBindGroupLayout(); |
| 81 |
} |
| 82 |
|
| 83 |
DawnSharedContext::~DawnSharedContext() { |
| 84 |
fThreadSafeResourceProvider.reset(); |
| 85 |
|
| 86 |
// need to clear out resources before any allocator is removed |
| 87 |
this->globalCache()->deleteResources(); |
| 88 |
} |
| 89 |
|
| 90 |
DawnThreadSafeResourceProvider* DawnSharedContext::threadSafeResourceProvider() const { |
| 91 |
return static_cast<DawnThreadSafeResourceProvider*>(fThreadSafeResourceProvider.get()); |
| 92 |
} |
| 93 |
|
| 94 |
std::unique_ptr<ResourceProvider> DawnSharedContext::makeResourceProvider( |
| 95 |
SingleOwner* singleOwner, |
| 96 |
uint32_t recorderID, |
| 97 |
size_t resourceBudget) { |
| 98 |
return std::unique_ptr<ResourceProvider>(new DawnResourceProvider(this, |
| 99 |
singleOwner, |
| 100 |
recorderID, |
| 101 |
resourceBudget)); |
| 102 |
} |
| 103 |
|
| 104 |
void DawnSharedContext::deviceTick(Context* context) { |
| 105 |
#if !defined(__EMSCRIPTEN__) |
| 106 |
this->device().Tick(); |
| 107 |
#endif |
| 108 |
context->checkAsyncWorkCompletion(); |
| 109 |
} |
| 110 |
|
| 111 |
void DawnSharedContext::createUniformBuffersBindGroupLayout() { |
| 112 |
const Caps* caps = this->caps(); |
| 113 |
|
| 114 |
std::array<wgpu::BindGroupLayoutEntry, DawnGraphicsPipeline::kNumUniformBuffers> entries; |
| 115 |
entries[0].binding = DawnGraphicsPipeline::kIntrinsicUniformBufferIndex; |
| 116 |
entries[0].visibility = wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment; |
| 117 |
entries[0].buffer.type = wgpu::BufferBindingType::Uniform; |
| 118 |
entries[0].buffer.hasDynamicOffset = true; |
| 119 |
entries[0].buffer.minBindingSize = 0; |
| 120 |
|
| 121 |
entries[1].binding = DawnGraphicsPipeline::kCombinedUniformIndex; |
| 122 |
entries[1].visibility = wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment; |
| 123 |
entries[1].buffer.type = caps->storageBufferSupport() |
| 124 |
? wgpu::BufferBindingType::ReadOnlyStorage |
| 125 |
: wgpu::BufferBindingType::Uniform; |
| 126 |
entries[1].buffer.hasDynamicOffset = true; |
| 127 |
entries[1].buffer.minBindingSize = 0; |
| 128 |
|
| 129 |
// Gradient buffer will only be used when storage buffers are preferred, else large |
| 130 |
// gradients use a texture fallback, set binding type as a uniform when not in use to |
| 131 |
// satisfy any binding type restrictions for non-supported ssbo devices. |
| 132 |
entries[2].binding = DawnGraphicsPipeline::kGradientBufferIndex; |
| 133 |
entries[2].visibility = wgpu::ShaderStage::Fragment; |
| 134 |
entries[2].buffer.type = caps->storageBufferSupport() |
| 135 |
? wgpu::BufferBindingType::ReadOnlyStorage |
| 136 |
: wgpu::BufferBindingType::Uniform; |
| 137 |
entries[2].buffer.hasDynamicOffset = true; |
| 138 |
entries[2].buffer.minBindingSize = 0; |
| 139 |
|
| 140 |
wgpu::BindGroupLayoutDescriptor groupLayoutDesc; |
| 141 |
if (caps->setBackendLabels()) { |
| 142 |
groupLayoutDesc.label = "Uniform buffers bind group layout"; |
| 143 |
} |
| 144 |
|
| 145 |
groupLayoutDesc.entryCount = entries.size(); |
| 146 |
groupLayoutDesc.entries = entries.data(); |
| 147 |
fUniformBuffersBindGroupLayout = this->device().CreateBindGroupLayout(&groupLayoutDesc); |
| 148 |
} |
| 149 |
|
| 150 |
void DawnSharedContext::createSingleTextureSamplerBindGroupLayout() { |
| 151 |
const Caps* caps = this->caps(); |
| 152 |
|
| 153 |
std::array<wgpu::BindGroupLayoutEntry, 2> entries; |
| 154 |
|
| 155 |
entries[0].binding = 0; |
| 156 |
entries[0].visibility = wgpu::ShaderStage::Fragment; |
| 157 |
entries[0].sampler.type = wgpu::SamplerBindingType::Filtering; |
| 158 |
|
| 159 |
entries[1].binding = 1; |
| 160 |
entries[1].visibility = wgpu::ShaderStage::Fragment; |
| 161 |
entries[1].texture.sampleType = wgpu::TextureSampleType::Float; |
| 162 |
entries[1].texture.viewDimension = wgpu::TextureViewDimension::e2D; |
| 163 |
entries[1].texture.multisampled = false; |
| 164 |
|
| 165 |
wgpu::BindGroupLayoutDescriptor groupLayoutDesc; |
| 166 |
if (caps->setBackendLabels()) { |
| 167 |
groupLayoutDesc.label = "Single texture + sampler bind group layout"; |
| 168 |
} |
| 169 |
|
| 170 |
groupLayoutDesc.entryCount = entries.size(); |
| 171 |
groupLayoutDesc.entries = entries.data(); |
| 172 |
fSingleTextureSamplerBindGroupLayout = this->device().CreateBindGroupLayout(&groupLayoutDesc); |
| 173 |
} |
| 174 |
|
| 175 |
sk_sp<GraphicsPipeline> DawnSharedContext::createGraphicsPipeline( |
| 176 |
const RuntimeEffectDictionary* runtimeDict, |
| 177 |
const UniqueKey& pipelineKey, |
| 178 |
const GraphicsPipelineDesc& pipelineDesc, |
| 179 |
const RenderPassDesc& renderPassDesc, |
| 180 |
SkEnumBitMask<PipelineCreationFlags> pipelineCreationFlags, |
| 181 |
uint32_t compilationID) { |
| 182 |
return DawnGraphicsPipeline::Make(this, |
| 183 |
runtimeDict, |
| 184 |
pipelineKey, |
| 185 |
pipelineDesc, |
| 186 |
renderPassDesc, |
| 187 |
pipelineCreationFlags, |
| 188 |
compilationID); |
| 189 |
} |
| 190 |
|
| 191 |
} // namespace skgpu::graphite |