blob: 1008eac3d0b0ce72f9494a7045c1c24522d78a5b [file] [log] [blame]
Stephen Whitebb6bed12019-08-02 09:57:55 -04001/*
2 * Copyright 2019 Google Inc.
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/dawn/GrDawnProgramBuilder.h"
9
Brian Salomon201cdbb2019-08-14 17:00:30 -040010#include "src/gpu/GrRenderTarget.h"
Stephen Whitef813ef72019-08-09 12:28:37 -040011#include "src/gpu/GrShaderUtils.h"
12#include "src/gpu/GrStencilSettings.h"
Stephen Whitebb6bed12019-08-02 09:57:55 -040013#include "src/gpu/dawn/GrDawnGpu.h"
Stephen White170d9902019-08-15 16:48:24 -040014#include "src/gpu/dawn/GrDawnTexture.h"
Stephen Whitebb6bed12019-08-02 09:57:55 -040015#include "src/sksl/SkSLCompiler.h"
16
Stephen Whitebb6bed12019-08-02 09:57:55 -040017static SkSL::String sksl_to_spirv(const GrDawnGpu* gpu, const char* shaderString,
Stephen White40c47e12019-11-01 13:13:03 -040018 SkSL::Program::Kind kind, bool flipY, uint32_t rtHeightOffset,
Stephen White20c626a2019-10-15 13:35:37 -040019 SkSL::Program::Inputs* inputs) {
Stephen Whitebb6bed12019-08-02 09:57:55 -040020 SkSL::Program::Settings settings;
Stephen White170d9902019-08-15 16:48:24 -040021 settings.fCaps = gpu->caps()->shaderCaps();
Stephen White20c626a2019-10-15 13:35:37 -040022 settings.fFlipY = flipY;
Stephen White40c47e12019-11-01 13:13:03 -040023 settings.fRTHeightOffset = rtHeightOffset;
Greg Daniel49f920e2020-04-16 10:33:39 -040024 settings.fRTHeightBinding = 0;
25 settings.fRTHeightSet = 0;
Stephen Whitebb6bed12019-08-02 09:57:55 -040026 std::unique_ptr<SkSL::Program> program = gpu->shaderCompiler()->convertProgram(
27 kind,
28 shaderString,
29 settings);
30 if (!program) {
31 SkDebugf("SkSL error:\n%s\n", gpu->shaderCompiler()->errorText().c_str());
32 SkASSERT(false);
33 return "";
34 }
35 *inputs = program->fInputs;
36 SkSL::String code;
37 if (!gpu->shaderCompiler()->toSPIRV(*program, &code)) {
38 return "";
39 }
40 return code;
41}
42
Stephen White3cc8d4f2019-10-30 09:56:23 -040043static wgpu::BlendFactor to_dawn_blend_factor(GrBlendCoeff coeff) {
Stephen Whitebb6bed12019-08-02 09:57:55 -040044 switch (coeff) {
Greg Daniel6e2af5c2020-03-30 15:07:05 -040045 case kZero_GrBlendCoeff:
46 return wgpu::BlendFactor::Zero;
47 case kOne_GrBlendCoeff:
48 return wgpu::BlendFactor::One;
49 case kSC_GrBlendCoeff:
50 return wgpu::BlendFactor::SrcColor;
51 case kISC_GrBlendCoeff:
52 return wgpu::BlendFactor::OneMinusSrcColor;
53 case kDC_GrBlendCoeff:
54 return wgpu::BlendFactor::DstColor;
55 case kIDC_GrBlendCoeff:
56 return wgpu::BlendFactor::OneMinusDstColor;
57 case kSA_GrBlendCoeff:
58 return wgpu::BlendFactor::SrcAlpha;
59 case kISA_GrBlendCoeff:
60 return wgpu::BlendFactor::OneMinusSrcAlpha;
61 case kDA_GrBlendCoeff:
62 return wgpu::BlendFactor::DstAlpha;
63 case kIDA_GrBlendCoeff:
64 return wgpu::BlendFactor::OneMinusDstAlpha;
65 case kConstC_GrBlendCoeff:
66 return wgpu::BlendFactor::BlendColor;
67 case kIConstC_GrBlendCoeff:
68 return wgpu::BlendFactor::OneMinusBlendColor;
69 case kS2C_GrBlendCoeff:
70 case kIS2C_GrBlendCoeff:
71 case kS2A_GrBlendCoeff:
72 case kIS2A_GrBlendCoeff:
73 default:
74 SkASSERT(!"unsupported blend coefficient");
75 return wgpu::BlendFactor::One;
76 }
Stephen Whitebb6bed12019-08-02 09:57:55 -040077}
78
Stephen White3cc8d4f2019-10-30 09:56:23 -040079static wgpu::BlendFactor to_dawn_blend_factor_for_alpha(GrBlendCoeff coeff) {
Stephen Whitef813ef72019-08-09 12:28:37 -040080 switch (coeff) {
81 // Force all srcColor used in alpha slot to alpha version.
82 case kSC_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040083 return wgpu::BlendFactor::SrcAlpha;
Stephen Whitef813ef72019-08-09 12:28:37 -040084 case kISC_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040085 return wgpu::BlendFactor::OneMinusSrcAlpha;
Stephen Whitef813ef72019-08-09 12:28:37 -040086 case kDC_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040087 return wgpu::BlendFactor::DstAlpha;
Stephen Whitef813ef72019-08-09 12:28:37 -040088 case kIDC_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040089 return wgpu::BlendFactor::OneMinusDstAlpha;
Stephen Whitef813ef72019-08-09 12:28:37 -040090 default:
91 return to_dawn_blend_factor(coeff);
92 }
93}
94
Stephen White3cc8d4f2019-10-30 09:56:23 -040095static wgpu::BlendOperation to_dawn_blend_operation(GrBlendEquation equation) {
Stephen Whitebb6bed12019-08-02 09:57:55 -040096 switch (equation) {
97 case kAdd_GrBlendEquation:
Stephen White3cc8d4f2019-10-30 09:56:23 -040098 return wgpu::BlendOperation::Add;
Stephen Whitebb6bed12019-08-02 09:57:55 -040099 case kSubtract_GrBlendEquation:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400100 return wgpu::BlendOperation::Subtract;
Stephen Whitef813ef72019-08-09 12:28:37 -0400101 case kReverseSubtract_GrBlendEquation:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400102 return wgpu::BlendOperation::ReverseSubtract;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400103 default:
104 SkASSERT(!"unsupported blend equation");
Stephen White3cc8d4f2019-10-30 09:56:23 -0400105 return wgpu::BlendOperation::Add;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400106 }
107}
108
Stephen White3cc8d4f2019-10-30 09:56:23 -0400109static wgpu::CompareFunction to_dawn_compare_function(GrStencilTest test) {
Stephen Whitef813ef72019-08-09 12:28:37 -0400110 switch (test) {
111 case GrStencilTest::kAlways:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400112 return wgpu::CompareFunction::Always;
Stephen Whitef813ef72019-08-09 12:28:37 -0400113 case GrStencilTest::kNever:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400114 return wgpu::CompareFunction::Never;
Stephen Whitef813ef72019-08-09 12:28:37 -0400115 case GrStencilTest::kGreater:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400116 return wgpu::CompareFunction::Greater;
Stephen Whitef813ef72019-08-09 12:28:37 -0400117 case GrStencilTest::kGEqual:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400118 return wgpu::CompareFunction::GreaterEqual;
Stephen Whitef813ef72019-08-09 12:28:37 -0400119 case GrStencilTest::kLess:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400120 return wgpu::CompareFunction::Less;
Stephen Whitef813ef72019-08-09 12:28:37 -0400121 case GrStencilTest::kLEqual:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400122 return wgpu::CompareFunction::LessEqual;
Stephen Whitef813ef72019-08-09 12:28:37 -0400123 case GrStencilTest::kEqual:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400124 return wgpu::CompareFunction::Equal;
Stephen Whitef813ef72019-08-09 12:28:37 -0400125 case GrStencilTest::kNotEqual:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400126 return wgpu::CompareFunction::NotEqual;
Stephen Whitef813ef72019-08-09 12:28:37 -0400127 default:
128 SkASSERT(!"unsupported stencil test");
Stephen White3cc8d4f2019-10-30 09:56:23 -0400129 return wgpu::CompareFunction::Always;
Stephen Whitef813ef72019-08-09 12:28:37 -0400130 }
131}
132
Stephen White3cc8d4f2019-10-30 09:56:23 -0400133static wgpu::StencilOperation to_dawn_stencil_operation(GrStencilOp op) {
Stephen Whitef813ef72019-08-09 12:28:37 -0400134 switch (op) {
135 case GrStencilOp::kKeep:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400136 return wgpu::StencilOperation::Keep;
Stephen Whitef813ef72019-08-09 12:28:37 -0400137 case GrStencilOp::kZero:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400138 return wgpu::StencilOperation::Zero;
Stephen Whitef813ef72019-08-09 12:28:37 -0400139 case GrStencilOp::kReplace:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400140 return wgpu::StencilOperation::Replace;
Stephen Whitef813ef72019-08-09 12:28:37 -0400141 case GrStencilOp::kInvert:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400142 return wgpu::StencilOperation::Invert;
Stephen Whitef813ef72019-08-09 12:28:37 -0400143 case GrStencilOp::kIncClamp:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400144 return wgpu::StencilOperation::IncrementClamp;
Stephen Whitef813ef72019-08-09 12:28:37 -0400145 case GrStencilOp::kDecClamp:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400146 return wgpu::StencilOperation::DecrementClamp;
Stephen Whitef813ef72019-08-09 12:28:37 -0400147 case GrStencilOp::kIncWrap:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400148 return wgpu::StencilOperation::IncrementWrap;
Stephen Whitef813ef72019-08-09 12:28:37 -0400149 case GrStencilOp::kDecWrap:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400150 return wgpu::StencilOperation::DecrementWrap;
Stephen Whitef813ef72019-08-09 12:28:37 -0400151 default:
152 SkASSERT(!"unsupported stencil function");
Stephen White3cc8d4f2019-10-30 09:56:23 -0400153 return wgpu::StencilOperation::Keep;
Stephen Whitef813ef72019-08-09 12:28:37 -0400154 }
155}
156
Stephen White3cc8d4f2019-10-30 09:56:23 -0400157static wgpu::PrimitiveTopology to_dawn_primitive_topology(GrPrimitiveType primitiveType) {
Stephen Whitee2641312019-08-29 15:10:50 -0400158 switch (primitiveType) {
159 case GrPrimitiveType::kTriangles:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400160 return wgpu::PrimitiveTopology::TriangleList;
Stephen Whitee2641312019-08-29 15:10:50 -0400161 case GrPrimitiveType::kTriangleStrip:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400162 return wgpu::PrimitiveTopology::TriangleStrip;
Stephen Whitee2641312019-08-29 15:10:50 -0400163 case GrPrimitiveType::kPoints:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400164 return wgpu::PrimitiveTopology::PointList;
Stephen Whitee2641312019-08-29 15:10:50 -0400165 case GrPrimitiveType::kLines:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400166 return wgpu::PrimitiveTopology::LineList;
Stephen Whitee2641312019-08-29 15:10:50 -0400167 case GrPrimitiveType::kLineStrip:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400168 return wgpu::PrimitiveTopology::LineStrip;
Robert Phillips571177f2019-10-04 14:41:49 -0400169 case GrPrimitiveType::kPath:
Stephen Whitee2641312019-08-29 15:10:50 -0400170 default:
171 SkASSERT(!"unsupported primitive topology");
Stephen White3cc8d4f2019-10-30 09:56:23 -0400172 return wgpu::PrimitiveTopology::TriangleList;
Stephen Whitee2641312019-08-29 15:10:50 -0400173 }
174}
175
Stephen White3cc8d4f2019-10-30 09:56:23 -0400176static wgpu::VertexFormat to_dawn_vertex_format(GrVertexAttribType type) {
Stephen Whitee2641312019-08-29 15:10:50 -0400177 switch (type) {
178 case kFloat_GrVertexAttribType:
179 case kHalf_GrVertexAttribType:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400180 return wgpu::VertexFormat::Float;
Stephen Whitee2641312019-08-29 15:10:50 -0400181 case kFloat2_GrVertexAttribType:
182 case kHalf2_GrVertexAttribType:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400183 return wgpu::VertexFormat::Float2;
Stephen Whitee2641312019-08-29 15:10:50 -0400184 case kFloat3_GrVertexAttribType:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400185 return wgpu::VertexFormat::Float3;
Stephen Whitee2641312019-08-29 15:10:50 -0400186 case kFloat4_GrVertexAttribType:
187 case kHalf4_GrVertexAttribType:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400188 return wgpu::VertexFormat::Float4;
Stephen Whitee2641312019-08-29 15:10:50 -0400189 case kUShort2_GrVertexAttribType:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400190 return wgpu::VertexFormat::UShort2;
Stephen Whitee2641312019-08-29 15:10:50 -0400191 case kInt_GrVertexAttribType:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400192 return wgpu::VertexFormat::Int;
Stephen Whitee2641312019-08-29 15:10:50 -0400193 case kUByte4_norm_GrVertexAttribType:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400194 return wgpu::VertexFormat::UChar4Norm;
Stephen Whitee2641312019-08-29 15:10:50 -0400195 default:
196 SkASSERT(!"unsupported vertex format");
Stephen White3cc8d4f2019-10-30 09:56:23 -0400197 return wgpu::VertexFormat::Float4;
Stephen Whitee2641312019-08-29 15:10:50 -0400198 }
199}
200
Stephen White3cc8d4f2019-10-30 09:56:23 -0400201static wgpu::ColorStateDescriptor create_color_state(const GrDawnGpu* gpu,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400202 const GrPipeline& pipeline,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400203 wgpu::TextureFormat colorFormat) {
Stephen Whitebb6bed12019-08-02 09:57:55 -0400204 GrXferProcessor::BlendInfo blendInfo = pipeline.getXferProcessor().getBlendInfo();
205 GrBlendEquation equation = blendInfo.fEquation;
206 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
207 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
208
Stephen White3cc8d4f2019-10-30 09:56:23 -0400209 wgpu::BlendFactor srcFactor = to_dawn_blend_factor(srcCoeff);
210 wgpu::BlendFactor dstFactor = to_dawn_blend_factor(dstCoeff);
211 wgpu::BlendFactor srcFactorAlpha = to_dawn_blend_factor_for_alpha(srcCoeff);
212 wgpu::BlendFactor dstFactorAlpha = to_dawn_blend_factor_for_alpha(dstCoeff);
213 wgpu::BlendOperation operation = to_dawn_blend_operation(equation);
214 auto mask = blendInfo.fWriteColor ? wgpu::ColorWriteMask::All : wgpu::ColorWriteMask::None;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400215
Stephen White3cc8d4f2019-10-30 09:56:23 -0400216 wgpu::BlendDescriptor colorDesc = {operation, srcFactor, dstFactor};
217 wgpu::BlendDescriptor alphaDesc = {operation, srcFactorAlpha, dstFactorAlpha};
Stephen Whitebb6bed12019-08-02 09:57:55 -0400218
Stephen White3cc8d4f2019-10-30 09:56:23 -0400219 wgpu::ColorStateDescriptor descriptor;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400220 descriptor.format = colorFormat;
221 descriptor.alphaBlend = alphaDesc;
222 descriptor.colorBlend = colorDesc;
223 descriptor.nextInChain = nullptr;
224 descriptor.writeMask = mask;
225
226 return descriptor;
227}
228
Stephen White3cc8d4f2019-10-30 09:56:23 -0400229static wgpu::StencilStateFaceDescriptor to_stencil_state_face(const GrStencilSettings::Face& face) {
230 wgpu::StencilStateFaceDescriptor desc;
Stephen Whitef813ef72019-08-09 12:28:37 -0400231 desc.compare = to_dawn_compare_function(face.fTest);
232 desc.failOp = desc.depthFailOp = to_dawn_stencil_operation(face.fFailOp);
233 desc.passOp = to_dawn_stencil_operation(face.fPassOp);
234 return desc;
235}
236
Stephen White3cc8d4f2019-10-30 09:56:23 -0400237static wgpu::DepthStencilStateDescriptor create_depth_stencil_state(
Robert Phillipsa87c5292019-11-12 10:12:42 -0500238 const GrProgramInfo& programInfo,
239 wgpu::TextureFormat depthStencilFormat) {
240 GrStencilSettings stencilSettings = programInfo.nonGLStencilSettings();
241 GrSurfaceOrigin origin = programInfo.origin();
242
Stephen White3cc8d4f2019-10-30 09:56:23 -0400243 wgpu::DepthStencilStateDescriptor state;
Stephen Whitef813ef72019-08-09 12:28:37 -0400244 state.format = depthStencilFormat;
Stephen Whitee2641312019-08-29 15:10:50 -0400245 if (!stencilSettings.isDisabled()) {
Stephen Whitef813ef72019-08-09 12:28:37 -0400246 if (stencilSettings.isTwoSided()) {
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500247 auto front = stencilSettings.postOriginCCWFace(origin);
Stephen White3a62ed42019-11-11 16:56:02 -0500248 auto back = stencilSettings.postOriginCWFace(origin);
Stephen Whitef4b3d6b2019-10-18 07:54:25 -0400249 state.stencilFront = to_stencil_state_face(front);
250 state.stencilBack = to_stencil_state_face(back);
251 state.stencilReadMask = front.fTestMask;
252 state.stencilWriteMask = front.fWriteMask;
Stephen Whitef813ef72019-08-09 12:28:37 -0400253 } else {
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500254 auto frontAndBack = stencilSettings.singleSidedFace();
Stephen Whitef4b3d6b2019-10-18 07:54:25 -0400255 state.stencilBack = state.stencilFront = to_stencil_state_face(frontAndBack);
256 state.stencilReadMask = frontAndBack.fTestMask;
257 state.stencilWriteMask = frontAndBack.fWriteMask;
Stephen Whitef813ef72019-08-09 12:28:37 -0400258 }
259 }
260 return state;
261}
262
Stephen White3d2dd262020-04-27 15:43:59 -0400263static wgpu::BindGroupEntry make_bind_group_entry(uint32_t binding, const wgpu::Buffer& buffer,
264 uint32_t offset, uint32_t size, const
265 wgpu::Sampler& sampler,
266 const wgpu::TextureView& textureView) {
267 wgpu::BindGroupEntry result;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400268 result.binding = binding;
269 result.buffer = buffer;
270 result.offset = offset;
271 result.size = size;
272 result.sampler = sampler;
273 result.textureView = textureView;
274 return result;
275}
276
Stephen White3d2dd262020-04-27 15:43:59 -0400277static wgpu::BindGroupEntry make_bind_group_entry(uint32_t binding, const wgpu::Buffer& buffer,
278 uint32_t offset, uint32_t size) {
279 return make_bind_group_entry(binding, buffer, offset, size, nullptr, nullptr);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400280}
281
Stephen White3d2dd262020-04-27 15:43:59 -0400282static wgpu::BindGroupEntry make_bind_group_entry(uint32_t binding,
283 const wgpu::Sampler& sampler) {
284 return make_bind_group_entry(binding, nullptr, 0, 0, sampler, nullptr);
Stephen White170d9902019-08-15 16:48:24 -0400285}
286
Stephen White3d2dd262020-04-27 15:43:59 -0400287static wgpu::BindGroupEntry make_bind_group_entry(uint32_t binding,
288 const wgpu::TextureView& textureView) {
289 return make_bind_group_entry(binding, nullptr, 0, 0, nullptr, textureView);
Stephen White170d9902019-08-15 16:48:24 -0400290}
291
Stephen Whitebb6bed12019-08-02 09:57:55 -0400292sk_sp<GrDawnProgram> GrDawnProgramBuilder::Build(GrDawnGpu* gpu,
293 GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400294 const GrProgramInfo& programInfo,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400295 wgpu::TextureFormat colorFormat,
Stephen Whitef813ef72019-08-09 12:28:37 -0400296 bool hasDepthStencil,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400297 wgpu::TextureFormat depthStencilFormat,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400298 GrProgramDesc* desc) {
Stephen White729c78d2019-10-14 12:42:59 -0400299 GrDawnProgramBuilder builder(gpu, renderTarget, programInfo, desc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400300 if (!builder.emitAndInstallProcs()) {
301 return nullptr;
302 }
303
304 builder.fVS.extensions().appendf("#extension GL_ARB_separate_shader_objects : enable\n");
305 builder.fFS.extensions().appendf("#extension GL_ARB_separate_shader_objects : enable\n");
306 builder.fVS.extensions().appendf("#extension GL_ARB_shading_language_420pack : enable\n");
307 builder.fFS.extensions().appendf("#extension GL_ARB_shading_language_420pack : enable\n");
308
309 builder.finalizeShaders();
310
311 SkSL::Program::Inputs vertInputs, fragInputs;
Stephen White20c626a2019-10-15 13:35:37 -0400312 bool flipY = programInfo.origin() != kTopLeft_GrSurfaceOrigin;
313 auto vsModule = builder.createShaderModule(builder.fVS, SkSL::Program::kVertex_Kind, flipY,
Stephen Whitee2641312019-08-29 15:10:50 -0400314 &vertInputs);
Stephen White20c626a2019-10-15 13:35:37 -0400315 auto fsModule = builder.createShaderModule(builder.fFS, SkSL::Program::kFragment_Kind, flipY,
Stephen Whitee2641312019-08-29 15:10:50 -0400316 &fragInputs);
Greg Danield59a91d2020-04-23 13:22:47 -0400317 GrSPIRVUniformHandler::UniformInfoArray& uniforms = builder.fUniformHandler.fUniforms;
Stephen White40c47e12019-11-01 13:13:03 -0400318 uint32_t uniformBufferSize = builder.fUniformHandler.fCurrentUBOOffset;
319 sk_sp<GrDawnProgram> result(new GrDawnProgram(uniforms, uniformBufferSize));
Stephen Whitebb6bed12019-08-02 09:57:55 -0400320 result->fGeometryProcessor = std::move(builder.fGeometryProcessor);
321 result->fXferProcessor = std::move(builder.fXferProcessor);
322 result->fFragmentProcessors = std::move(builder.fFragmentProcessors);
323 result->fFragmentProcessorCnt = builder.fFragmentProcessorCnt;
Stephen White3d2dd262020-04-27 15:43:59 -0400324 std::vector<wgpu::BindGroupLayoutEntry> uniformLayoutEntries;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400325 if (0 != uniformBufferSize) {
Stephen White3d2dd262020-04-27 15:43:59 -0400326 uniformLayoutEntries.push_back({ GrSPIRVUniformHandler::kUniformBinding,
327 wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment,
Stephen White768e91f2020-06-22 13:45:20 -0400328 wgpu::BindingType::UniformBuffer });
Stephen White170d9902019-08-15 16:48:24 -0400329 }
Stephen White7cb52cb2019-11-12 10:40:27 -0500330 wgpu::BindGroupLayoutDescriptor uniformBindGroupLayoutDesc;
Stephen White3d2dd262020-04-27 15:43:59 -0400331 uniformBindGroupLayoutDesc.entryCount = uniformLayoutEntries.size();
332 uniformBindGroupLayoutDesc.entries = uniformLayoutEntries.data();
Stephen White768e91f2020-06-22 13:45:20 -0400333 result->fBindGroupLayouts.push_back(
334 gpu->device().CreateBindGroupLayout(&uniformBindGroupLayoutDesc));
Stephen White7cb52cb2019-11-12 10:40:27 -0500335 uint32_t binding = 0;
Stephen White3d2dd262020-04-27 15:43:59 -0400336 std::vector<wgpu::BindGroupLayoutEntry> textureLayoutEntries;
Stephen White768e91f2020-06-22 13:45:20 -0400337 int textureCount = builder.fUniformHandler.fSamplers.count();
338 if (textureCount > 0) {
339 for (int i = 0; i < textureCount; ++i) {
340 textureLayoutEntries.push_back({ binding++, wgpu::ShaderStage::Fragment,
341 wgpu::BindingType::Sampler });
342 textureLayoutEntries.push_back({ binding++, wgpu::ShaderStage::Fragment,
343 wgpu::BindingType::SampledTexture });
344 }
345 wgpu::BindGroupLayoutDescriptor textureBindGroupLayoutDesc;
346 textureBindGroupLayoutDesc.entryCount = textureLayoutEntries.size();
347 textureBindGroupLayoutDesc.entries = textureLayoutEntries.data();
348 result->fBindGroupLayouts.push_back(
349 gpu->device().CreateBindGroupLayout(&textureBindGroupLayoutDesc));
Stephen White170d9902019-08-15 16:48:24 -0400350 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400351 wgpu::PipelineLayoutDescriptor pipelineLayoutDesc;
Stephen White768e91f2020-06-22 13:45:20 -0400352 pipelineLayoutDesc.bindGroupLayoutCount = result->fBindGroupLayouts.size();
353 pipelineLayoutDesc.bindGroupLayouts = result->fBindGroupLayouts.data();
Stephen Whitee2641312019-08-29 15:10:50 -0400354 auto pipelineLayout = gpu->device().CreatePipelineLayout(&pipelineLayoutDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400355 result->fBuiltinUniformHandles = builder.fUniformHandles;
Stephen White729c78d2019-10-14 12:42:59 -0400356 const GrPipeline& pipeline = programInfo.pipeline();
Stephen Whitee2641312019-08-29 15:10:50 -0400357 auto colorState = create_color_state(gpu, pipeline, colorFormat);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400358 wgpu::DepthStencilStateDescriptor depthStencilState;
Robert Phillipsa87c5292019-11-12 10:12:42 -0500359
360#ifdef SK_DEBUG
Stephen Whitef813ef72019-08-09 12:28:37 -0400361 if (pipeline.isStencilEnabled()) {
Robert Phillipsa87c5292019-11-12 10:12:42 -0500362 SkASSERT(renderTarget->renderTargetPriv().numStencilBits() == 8);
Stephen Whitef813ef72019-08-09 12:28:37 -0400363 }
Robert Phillipsa87c5292019-11-12 10:12:42 -0500364#endif
365 depthStencilState = create_depth_stencil_state(programInfo, depthStencilFormat);
Stephen Whitee2641312019-08-29 15:10:50 -0400366
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500367 std::vector<wgpu::VertexBufferLayoutDescriptor> inputs;
Stephen Whitee2641312019-08-29 15:10:50 -0400368
Stephen White3cc8d4f2019-10-30 09:56:23 -0400369 std::vector<wgpu::VertexAttributeDescriptor> vertexAttributes;
Stephen White729c78d2019-10-14 12:42:59 -0400370 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Stephen White768e91f2020-06-22 13:45:20 -0400371 int i = 0;
Stephen Whitee2641312019-08-29 15:10:50 -0400372 if (primProc.numVertexAttributes() > 0) {
373 size_t offset = 0;
Stephen Whitee2641312019-08-29 15:10:50 -0400374 for (const auto& attrib : primProc.vertexAttributes()) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400375 wgpu::VertexAttributeDescriptor attribute;
Stephen Whitee2641312019-08-29 15:10:50 -0400376 attribute.shaderLocation = i;
377 attribute.offset = offset;
378 attribute.format = to_dawn_vertex_format(attrib.cpuType());
379 vertexAttributes.push_back(attribute);
380 offset += attrib.sizeAlign4();
381 i++;
382 }
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500383 wgpu::VertexBufferLayoutDescriptor input;
384 input.arrayStride = offset;
Stephen White3cc8d4f2019-10-30 09:56:23 -0400385 input.stepMode = wgpu::InputStepMode::Vertex;
Stephen Whitee2641312019-08-29 15:10:50 -0400386 input.attributeCount = vertexAttributes.size();
387 input.attributes = &vertexAttributes.front();
388 inputs.push_back(input);
389 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400390 std::vector<wgpu::VertexAttributeDescriptor> instanceAttributes;
Stephen Whitee2641312019-08-29 15:10:50 -0400391 if (primProc.numInstanceAttributes() > 0) {
392 size_t offset = 0;
Stephen Whitee2641312019-08-29 15:10:50 -0400393 for (const auto& attrib : primProc.instanceAttributes()) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400394 wgpu::VertexAttributeDescriptor attribute;
Stephen Whitee2641312019-08-29 15:10:50 -0400395 attribute.shaderLocation = i;
396 attribute.offset = offset;
397 attribute.format = to_dawn_vertex_format(attrib.cpuType());
398 instanceAttributes.push_back(attribute);
399 offset += attrib.sizeAlign4();
400 i++;
401 }
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500402 wgpu::VertexBufferLayoutDescriptor input;
403 input.arrayStride = offset;
Stephen White3cc8d4f2019-10-30 09:56:23 -0400404 input.stepMode = wgpu::InputStepMode::Instance;
Stephen Whitee2641312019-08-29 15:10:50 -0400405 input.attributeCount = instanceAttributes.size();
406 input.attributes = &instanceAttributes.front();
407 inputs.push_back(input);
408 }
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500409 wgpu::VertexStateDescriptor vertexState;
410 vertexState.indexFormat = wgpu::IndexFormat::Uint16;
411 vertexState.vertexBufferCount = inputs.size();
412 vertexState.vertexBuffers = &inputs.front();
Stephen Whitee2641312019-08-29 15:10:50 -0400413
Stephen White3cc8d4f2019-10-30 09:56:23 -0400414 wgpu::ProgrammableStageDescriptor vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400415 vsDesc.module = vsModule;
416 vsDesc.entryPoint = "main";
417
Stephen White3cc8d4f2019-10-30 09:56:23 -0400418 wgpu::ProgrammableStageDescriptor fsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400419 fsDesc.module = fsModule;
420 fsDesc.entryPoint = "main";
421
Stephen White3cc8d4f2019-10-30 09:56:23 -0400422 wgpu::RenderPipelineDescriptor rpDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400423 rpDesc.layout = pipelineLayout;
Stephen White20c626a2019-10-15 13:35:37 -0400424 rpDesc.vertexStage = vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400425 rpDesc.fragmentStage = &fsDesc;
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500426 rpDesc.vertexState = &vertexState;
Robert Phillipsfcaae482019-11-07 10:17:03 -0500427 rpDesc.primitiveTopology = to_dawn_primitive_topology(programInfo.primitiveType());
Stephen Whitee2641312019-08-29 15:10:50 -0400428 if (hasDepthStencil) {
429 rpDesc.depthStencilState = &depthStencilState;
430 }
431 rpDesc.colorStateCount = 1;
Stephen White20c626a2019-10-15 13:35:37 -0400432 rpDesc.colorStates = &colorState;
Stephen Whitee2641312019-08-29 15:10:50 -0400433 result->fRenderPipeline = gpu->device().CreateRenderPipeline(&rpDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400434 return result;
435}
436
437GrDawnProgramBuilder::GrDawnProgramBuilder(GrDawnGpu* gpu,
438 GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400439 const GrProgramInfo& programInfo,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400440 GrProgramDesc* desc)
Stephen White511af2e2020-02-07 11:04:58 -0500441 : INHERITED(renderTarget, *desc, programInfo)
Stephen Whitebb6bed12019-08-02 09:57:55 -0400442 , fGpu(gpu)
443 , fVaryingHandler(this)
444 , fUniformHandler(this) {
445}
446
Stephen White3cc8d4f2019-10-30 09:56:23 -0400447wgpu::ShaderModule GrDawnProgramBuilder::createShaderModule(const GrGLSLShaderBuilder& builder,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400448 SkSL::Program::Kind kind,
Stephen White20c626a2019-10-15 13:35:37 -0400449 bool flipY,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400450 SkSL::Program::Inputs* inputs) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400451 wgpu::Device device = fGpu->device();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400452 SkString source(builder.fCompilerString.c_str());
453
454#if 0
455 SkSL::String sksl = GrShaderUtils::PrettyPrint(builder.fCompilerString);
456 printf("converting program:\n%s\n", sksl.c_str());
457#endif
458
Stephen White40c47e12019-11-01 13:13:03 -0400459 SkSL::String spirvSource = sksl_to_spirv(fGpu, source.c_str(), kind, flipY,
460 fUniformHandler.getRTHeightOffset(), inputs);
461 if (inputs->fRTHeight) {
462 this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
463 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400464
Stephen White3d2dd262020-04-27 15:43:59 -0400465 wgpu::ShaderModuleSPIRVDescriptor desc;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400466 desc.codeSize = spirvSource.size() / 4;
467 desc.code = reinterpret_cast<const uint32_t*>(spirvSource.c_str());
468
Stephen White3d2dd262020-04-27 15:43:59 -0400469 wgpu::ShaderModuleDescriptor smDesc;
470 smDesc.nextInChain = &desc;
471
472 return device.CreateShaderModule(&smDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400473};
474
475const GrCaps* GrDawnProgramBuilder::caps() const {
476 return fGpu->caps();
477}
478
479void GrDawnProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) {
480 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
481 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
482 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
483 fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
484 }
485
486 // set RT adjustment
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400487 SkISize dimensions = rt->dimensions();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400488 SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid());
489 if (fRenderTargetState.fRenderTargetOrigin != origin ||
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400490 fRenderTargetState.fRenderTargetSize != dimensions) {
491 fRenderTargetState.fRenderTargetSize = dimensions;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400492 fRenderTargetState.fRenderTargetOrigin = origin;
493
494 float rtAdjustmentVec[4];
495 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
496 fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
497 }
498}
499
Brian Salomonccb61422020-01-09 10:46:36 -0500500static void set_texture(GrDawnGpu* gpu, GrSamplerState state, GrTexture* texture,
Stephen White3d2dd262020-04-27 15:43:59 -0400501 std::vector<wgpu::BindGroupEntry>* bindings, int* binding) {
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400502 // FIXME: could probably cache samplers in GrDawnProgram
Stephen White3cc8d4f2019-10-30 09:56:23 -0400503 wgpu::Sampler sampler = gpu->getOrCreateSampler(state);
Stephen White3d2dd262020-04-27 15:43:59 -0400504 bindings->push_back(make_bind_group_entry((*binding)++, sampler));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400505 GrDawnTexture* tex = static_cast<GrDawnTexture*>(texture);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400506 wgpu::TextureView textureView = tex->textureView();
Stephen White3d2dd262020-04-27 15:43:59 -0400507 bindings->push_back(make_bind_group_entry((*binding)++, textureView));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400508}
509
Stephen White7cb52cb2019-11-12 10:40:27 -0500510wgpu::BindGroup GrDawnProgram::setUniformData(GrDawnGpu* gpu, const GrRenderTarget* renderTarget,
511 const GrProgramInfo& programInfo) {
Stephen White3d2dd262020-04-27 15:43:59 -0400512 std::vector<wgpu::BindGroupEntry> bindings;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400513 GrDawnRingBuffer::Slice slice;
514 uint32_t uniformBufferSize = fDataManager.uniformBufferSize();
515 if (0 != uniformBufferSize) {
516 slice = gpu->allocateUniformRingBufferSlice(uniformBufferSize);
Stephen White3d2dd262020-04-27 15:43:59 -0400517 bindings.push_back(make_bind_group_entry(GrSPIRVUniformHandler::kUniformBinding,
Stephen Whitedd78efd2019-10-23 15:00:20 -0400518 slice.fBuffer, slice.fOffset,
519 uniformBufferSize));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400520 }
Stephen White729c78d2019-10-14 12:42:59 -0400521 this->setRenderTargetState(renderTarget, programInfo.origin());
522 const GrPipeline& pipeline = programInfo.pipeline();
523 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Brian Salomonc241b582019-11-27 08:57:17 -0500524 GrFragmentProcessor::PipelineCoordTransformRange transformRange(pipeline);
525 fGeometryProcessor->setData(fDataManager, primProc, transformRange);
Brian Salomon7eabfe82019-12-02 14:20:20 -0500526 GrFragmentProcessor::CIter fpIter(pipeline);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400527 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
Brian Salomonc241b582019-11-27 08:57:17 -0500528 for (; fpIter && glslIter; ++fpIter, ++glslIter) {
529 glslIter->setData(fDataManager, *fpIter);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400530 }
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400531 SkIPoint offset;
532 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
533 fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset);
Stephen White8eda4992020-03-17 11:44:48 -0400534 if (0 != uniformBufferSize) {
535 fDataManager.uploadUniformBuffers(slice.fData);
536 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400537 wgpu::BindGroupDescriptor descriptor;
Stephen White7cb52cb2019-11-12 10:40:27 -0500538 descriptor.layout = fBindGroupLayouts[0];
Stephen White3d2dd262020-04-27 15:43:59 -0400539 descriptor.entryCount = bindings.size();
540 descriptor.entries = bindings.data();
Stephen White7cb52cb2019-11-12 10:40:27 -0500541 return gpu->device().CreateBindGroup(&descriptor);
542}
543
544wgpu::BindGroup GrDawnProgram::setTextures(GrDawnGpu* gpu,
Stephen White6e8ceee2020-02-25 16:25:43 -0500545 const GrPrimitiveProcessor& primProc,
546 const GrPipeline& pipeline,
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500547 const GrSurfaceProxy* const primProcTextures[]) {
Stephen White768e91f2020-06-22 13:45:20 -0400548 if (fBindGroupLayouts.size() < 2) {
549 return nullptr;
550 }
Stephen White3d2dd262020-04-27 15:43:59 -0400551 std::vector<wgpu::BindGroupEntry> bindings;
Stephen White7cb52cb2019-11-12 10:40:27 -0500552 int binding = 0;
Stephen White7cb52cb2019-11-12 10:40:27 -0500553 if (primProcTextures) {
554 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500555 SkASSERT(primProcTextures[i]->asTextureProxy());
Stephen White7cb52cb2019-11-12 10:40:27 -0500556 auto& sampler = primProc.textureSampler(i);
557 set_texture(gpu, sampler.samplerState(), primProcTextures[i]->peekTexture(), &bindings,
558 &binding);
559 }
560 }
Stephen Whitea521c962019-12-04 09:57:48 -0500561 GrFragmentProcessor::CIter fpIter(pipeline);
Stephen White7cb52cb2019-11-12 10:40:27 -0500562 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
Brian Salomonc241b582019-11-27 08:57:17 -0500563 for (; fpIter && glslIter; ++fpIter, ++glslIter) {
564 for (int i = 0; i < fpIter->numTextureSamplers(); ++i) {
565 auto& s = fpIter->textureSampler(i);
Stephen White7cb52cb2019-11-12 10:40:27 -0500566 set_texture(gpu, s.samplerState(), s.peekTexture(), &bindings, &binding);
567 }
Stephen White7cb52cb2019-11-12 10:40:27 -0500568 }
569 SkIPoint offset;
570 if (GrTexture* dstTexture = pipeline.peekDstTexture(&offset)) {
Brian Salomonccb61422020-01-09 10:46:36 -0500571 set_texture(gpu, GrSamplerState::Filter::kNearest, dstTexture, &bindings, &binding);
Stephen White7cb52cb2019-11-12 10:40:27 -0500572 }
573 wgpu::BindGroupDescriptor descriptor;
574 descriptor.layout = fBindGroupLayouts[1];
Stephen White3d2dd262020-04-27 15:43:59 -0400575 descriptor.entryCount = bindings.size();
576 descriptor.entries = bindings.data();
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400577 return gpu->device().CreateBindGroup(&descriptor);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400578}