| 1 |
/* |
| 2 |
* Copyright 2023 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/DawnComputePipeline.h" |
| 9 |
|
| 10 |
#include "include/gpu/GpuTypes.h" |
| 11 |
#include "src/gpu/SkSLToBackend.h" |
| 12 |
#include "src/gpu/graphite/Caps.h" |
| 13 |
#include "src/gpu/graphite/ComputePipelineDesc.h" |
| 14 |
#include "src/gpu/graphite/ContextUtils.h" |
| 15 |
#include "src/gpu/graphite/TextureInfoPriv.h" |
| 16 |
#include "src/gpu/graphite/dawn/DawnAsyncWait.h" |
| 17 |
#include "src/gpu/graphite/dawn/DawnErrorChecker.h" |
| 18 |
#include "src/gpu/graphite/dawn/DawnGraphiteUtils.h" |
| 19 |
#include "src/gpu/graphite/dawn/DawnSharedContext.h" |
| 20 |
#include "src/sksl/SkSLProgramSettings.h" |
| 21 |
|
| 22 |
namespace skgpu::graphite { |
| 23 |
namespace { |
| 24 |
|
| 25 |
struct ShaderInfo { |
| 26 |
wgpu::ShaderModule fModule; |
| 27 |
std::string fEntryPoint; |
| 28 |
|
| 29 |
bool isValid() const { return static_cast<bool>(fModule); } |
| 30 |
}; |
| 31 |
|
| 32 |
static ShaderInfo compile_shader_module(const DawnSharedContext* sharedContext, |
| 33 |
const ComputePipelineDesc& pipelineDesc) { |
| 34 |
SkASSERT(sharedContext); |
| 35 |
|
| 36 |
ShaderInfo info; |
| 37 |
|
| 38 |
const Caps* caps = sharedContext->caps(); |
| 39 |
const ComputeStep* step = pipelineDesc.computeStep(); |
| 40 |
ShaderErrorHandler* errorHandler = caps->shaderErrorHandler(); |
| 41 |
|
| 42 |
if (step->supportsNativeShader()) { |
| 43 |
auto nativeShader = step->nativeShaderSource(ComputeStep::NativeShaderFormat::kWGSL); |
| 44 |
SkSL::NativeShader wgsl; |
| 45 |
wgsl.fText = nativeShader.fSource; |
| 46 |
if (!DawnCompileWGSLShaderModule( |
| 47 |
sharedContext, step->name(), wgsl, &info.fModule, errorHandler)) { |
| 48 |
return {}; |
| 49 |
} |
| 50 |
info.fEntryPoint = std::move(nativeShader.fEntryPoint); |
| 51 |
} else { |
| 52 |
SkSL::NativeShader wgsl; |
| 53 |
SkSL::Program::Interface interface; |
| 54 |
SkSL::ProgramSettings settings; |
| 55 |
// TODO(michaelludwig): Set this to false when "shader-f16" is available. |
| 56 |
settings.fForceHighPrecision = true; |
| 57 |
|
| 58 |
std::string sksl = BuildComputeSkSL(caps, step, BackendApi::kDawn); |
| 59 |
if (skgpu::SkSLToWGSL(caps->shaderCaps(), |
| 60 |
sksl, |
| 61 |
SkSL::ProgramKind::kCompute, |
| 62 |
settings, |
| 63 |
&wgsl, |
| 64 |
&interface, |
| 65 |
errorHandler)) { |
| 66 |
if (!DawnCompileWGSLShaderModule(sharedContext, step->name(), wgsl, |
| 67 |
&info.fModule, errorHandler)) { |
| 68 |
return {}; |
| 69 |
} |
| 70 |
info.fEntryPoint = "main"; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return info; |
| 75 |
} |
| 76 |
|
| 77 |
} // namespace |
| 78 |
|
| 79 |
sk_sp<DawnComputePipeline> DawnComputePipeline::Make(const DawnSharedContext* sharedContext, |
| 80 |
const ComputePipelineDesc& pipelineDesc) { |
| 81 |
auto [shaderModule, entryPointName] = compile_shader_module(sharedContext, pipelineDesc); |
| 82 |
if (!shaderModule) { |
| 83 |
return nullptr; |
| 84 |
} |
| 85 |
|
| 86 |
const ComputeStep* step = pipelineDesc.computeStep(); |
| 87 |
|
| 88 |
// ComputeStep resources are listed in the order that they must be declared in the shader. This |
| 89 |
// order is then used for the index assignment using an "indexed by order" policy that has |
| 90 |
// backend-specific semantics. The semantics on Dawn is to assign the index number in increasing |
| 91 |
// order. |
| 92 |
// |
| 93 |
// For compute pipelines, all resources get assigned to a single bind group at index 0 (ignoring |
| 94 |
// bind group indices assigned in by DawnCaps's ResourceBindingRequirements, which are for |
| 95 |
// non-compute shaders). |
| 96 |
std::vector<wgpu::BindGroupLayoutEntry> bindGroupLayoutEntries; |
| 97 |
auto resources = step->resources(); |
| 98 |
|
| 99 |
// Sampled textures count as 2 resources (1 texture and 1 sampler). All other types count as 1. |
| 100 |
size_t resourceCount = 0; |
| 101 |
for (const ComputeStep::ResourceDesc& r : resources) { |
| 102 |
resourceCount++; |
| 103 |
if (r.fType == ComputeStep::ResourceType::kSampledTexture) { |
| 104 |
resourceCount++; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
bindGroupLayoutEntries.reserve(resourceCount); |
| 109 |
int declarationIndex = 0; |
| 110 |
for (const ComputeStep::ResourceDesc& r : resources) { |
| 111 |
bindGroupLayoutEntries.emplace_back(); |
| 112 |
uint32_t bindingIndex = bindGroupLayoutEntries.size() - 1; |
| 113 |
|
| 114 |
wgpu::BindGroupLayoutEntry& entry = bindGroupLayoutEntries.back(); |
| 115 |
entry.binding = bindingIndex; |
| 116 |
entry.visibility = wgpu::ShaderStage::Compute; |
| 117 |
switch (r.fType) { |
| 118 |
case ComputeStep::ResourceType::kUniformBuffer: |
| 119 |
entry.buffer.type = wgpu::BufferBindingType::Uniform; |
| 120 |
break; |
| 121 |
case ComputeStep::ResourceType::kStorageBuffer: |
| 122 |
case ComputeStep::ResourceType::kIndirectBuffer: |
| 123 |
entry.buffer.type = wgpu::BufferBindingType::Storage; |
| 124 |
break; |
| 125 |
case ComputeStep::ResourceType::kReadOnlyStorageBuffer: |
| 126 |
entry.buffer.type = wgpu::BufferBindingType::ReadOnlyStorage; |
| 127 |
break; |
| 128 |
case ComputeStep::ResourceType::kReadOnlyTexture: |
| 129 |
entry.texture.sampleType = wgpu::TextureSampleType::Float; |
| 130 |
entry.texture.viewDimension = wgpu::TextureViewDimension::e2D; |
| 131 |
break; |
| 132 |
case ComputeStep::ResourceType::kWriteOnlyStorageTexture: { |
| 133 |
entry.storageTexture.access = wgpu::StorageTextureAccess::WriteOnly; |
| 134 |
entry.storageTexture.viewDimension = wgpu::TextureViewDimension::e2D; |
| 135 |
|
| 136 |
auto [_, colorType] = step->calculateTextureParameters(declarationIndex, r); |
| 137 |
auto textureInfo = sharedContext->caps()->getDefaultStorageTextureInfo(colorType); |
| 138 |
entry.storageTexture.format = |
| 139 |
TextureInfoPriv::Get<DawnTextureInfo>(textureInfo).getViewFormat(); |
| 140 |
break; |
| 141 |
} |
| 142 |
case ComputeStep::ResourceType::kSampledTexture: { |
| 143 |
entry.sampler.type = wgpu::SamplerBindingType::Filtering; |
| 144 |
|
| 145 |
// Add an additional entry for the texture. |
| 146 |
bindGroupLayoutEntries.emplace_back(); |
| 147 |
wgpu::BindGroupLayoutEntry& texEntry = bindGroupLayoutEntries.back(); |
| 148 |
texEntry.binding = bindingIndex + 1; |
| 149 |
texEntry.visibility = wgpu::ShaderStage::Compute; |
| 150 |
texEntry.texture.sampleType = wgpu::TextureSampleType::Float; |
| 151 |
texEntry.texture.viewDimension = wgpu::TextureViewDimension::e2D; |
| 152 |
break; |
| 153 |
} |
| 154 |
} |
| 155 |
declarationIndex++; |
| 156 |
} |
| 157 |
|
| 158 |
const wgpu::Device& device = sharedContext->device(); |
| 159 |
|
| 160 |
// All resources of a ComputeStep currently get assigned to a single bind group at index 0. |
| 161 |
wgpu::BindGroupLayoutDescriptor bindGroupLayoutDesc; |
| 162 |
bindGroupLayoutDesc.entryCount = bindGroupLayoutEntries.size(); |
| 163 |
bindGroupLayoutDesc.entries = bindGroupLayoutEntries.data(); |
| 164 |
wgpu::BindGroupLayout bindGroupLayout = device.CreateBindGroupLayout(&bindGroupLayoutDesc); |
| 165 |
if (!bindGroupLayout) { |
| 166 |
return nullptr; |
| 167 |
} |
| 168 |
|
| 169 |
wgpu::PipelineLayoutDescriptor pipelineLayoutDesc; |
| 170 |
if (sharedContext->caps()->setBackendLabels()) { |
| 171 |
pipelineLayoutDesc.label = step->name(); |
| 172 |
} |
| 173 |
pipelineLayoutDesc.bindGroupLayoutCount = 1; |
| 174 |
pipelineLayoutDesc.bindGroupLayouts = &bindGroupLayout; |
| 175 |
wgpu::PipelineLayout layout = device.CreatePipelineLayout(&pipelineLayoutDesc); |
| 176 |
if (!layout) { |
| 177 |
return nullptr; |
| 178 |
} |
| 179 |
|
| 180 |
wgpu::ComputePipelineDescriptor descriptor; |
| 181 |
// Always set the label for pipelines, dawn may need it for tracing. |
| 182 |
descriptor.label = step->name(); |
| 183 |
descriptor.compute.module = std::move(shaderModule); |
| 184 |
descriptor.compute.entryPoint = entryPointName.c_str(); |
| 185 |
descriptor.layout = std::move(layout); |
| 186 |
|
| 187 |
std::optional<DawnErrorChecker> errorChecker; |
| 188 |
if (sharedContext->dawnCaps()->allowScopedErrorChecks()) { |
| 189 |
errorChecker.emplace(sharedContext); |
| 190 |
} |
| 191 |
wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&descriptor); |
| 192 |
SkASSERT(pipeline); |
| 193 |
if (errorChecker.has_value() && errorChecker->popErrorScopes() != DawnErrorType::kNoError) { |
| 194 |
return nullptr; |
| 195 |
} |
| 196 |
|
| 197 |
return sk_sp<DawnComputePipeline>(new DawnComputePipeline( |
| 198 |
sharedContext, std::move(pipeline), std::move(bindGroupLayout))); |
| 199 |
} |
| 200 |
|
| 201 |
DawnComputePipeline::DawnComputePipeline(const SharedContext* sharedContext, |
| 202 |
wgpu::ComputePipeline pso, |
| 203 |
wgpu::BindGroupLayout groupLayout) |
| 204 |
: ComputePipeline(sharedContext) |
| 205 |
, fPipeline(std::move(pso)) |
| 206 |
, fGroupLayout(std::move(groupLayout)) {} |
| 207 |
|
| 208 |
void DawnComputePipeline::freeGpuData() { fPipeline = nullptr; } |
| 209 |
|
| 210 |
} // namespace skgpu::graphite |