blob: 90b27905fb1ca7de00f11b015dbc3b662a374e40 [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 White3cc8d4f2019-10-30 09:56:23 -0400263static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, const wgpu::Buffer& buffer,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400264 uint32_t offset, uint32_t size, const
Stephen White3cc8d4f2019-10-30 09:56:23 -0400265 wgpu::Sampler& sampler,
266 const wgpu::TextureView& textureView) {
267 wgpu::BindGroupBinding 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 White3cc8d4f2019-10-30 09:56:23 -0400277static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, const wgpu::Buffer& buffer,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400278 uint32_t offset, uint32_t size) {
279 return make_bind_group_binding(binding, buffer, offset, size, nullptr, nullptr);
280}
281
Stephen White3cc8d4f2019-10-30 09:56:23 -0400282static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding,
283 const wgpu::Sampler& sampler) {
Stephen White170d9902019-08-15 16:48:24 -0400284 return make_bind_group_binding(binding, nullptr, 0, 0, sampler, nullptr);
285}
286
Stephen White3cc8d4f2019-10-30 09:56:23 -0400287static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding,
288 const wgpu::TextureView& textureView) {
Stephen White170d9902019-08-15 16:48:24 -0400289 return make_bind_group_binding(binding, nullptr, 0, 0, nullptr, textureView);
290}
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);
Stephen White40c47e12019-11-01 13:13:03 -0400317 GrDawnUniformHandler::UniformInfoArray& uniforms = builder.fUniformHandler.fUniforms;
318 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 White7cb52cb2019-11-12 10:40:27 -0500324 std::vector<wgpu::BindGroupLayoutBinding> uniformLayoutBindings;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400325 if (0 != uniformBufferSize) {
Stephen White7cb52cb2019-11-12 10:40:27 -0500326 uniformLayoutBindings.push_back({ GrDawnUniformHandler::kUniformBinding,
327 wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment,
328 wgpu::BindingType::UniformBuffer});
Stephen White170d9902019-08-15 16:48:24 -0400329 }
Stephen White7cb52cb2019-11-12 10:40:27 -0500330 wgpu::BindGroupLayoutDescriptor uniformBindGroupLayoutDesc;
331 uniformBindGroupLayoutDesc.bindingCount = uniformLayoutBindings.size();
332 uniformBindGroupLayoutDesc.bindings = uniformLayoutBindings.data();
333 result->fBindGroupLayouts[0] =
334 gpu->device().CreateBindGroupLayout(&uniformBindGroupLayoutDesc);
335 uint32_t binding = 0;
336 std::vector<wgpu::BindGroupLayoutBinding> textureLayoutBindings;
Stephen White170d9902019-08-15 16:48:24 -0400337 for (int i = 0; i < builder.fUniformHandler.fSamplers.count(); ++i) {
Stephen White7cb52cb2019-11-12 10:40:27 -0500338 textureLayoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment,
339 wgpu::BindingType::Sampler});
340 textureLayoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment,
341 wgpu::BindingType::SampledTexture});
Stephen White170d9902019-08-15 16:48:24 -0400342 }
Stephen White7cb52cb2019-11-12 10:40:27 -0500343 wgpu::BindGroupLayoutDescriptor textureBindGroupLayoutDesc;
344 textureBindGroupLayoutDesc.bindingCount = textureLayoutBindings.size();
345 textureBindGroupLayoutDesc.bindings = textureLayoutBindings.data();
346 result->fBindGroupLayouts[1] =
347 gpu->device().CreateBindGroupLayout(&textureBindGroupLayoutDesc);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400348 wgpu::PipelineLayoutDescriptor pipelineLayoutDesc;
Stephen White7cb52cb2019-11-12 10:40:27 -0500349 pipelineLayoutDesc.bindGroupLayoutCount = 2;
350 pipelineLayoutDesc.bindGroupLayouts = &result->fBindGroupLayouts[0];
Stephen Whitee2641312019-08-29 15:10:50 -0400351 auto pipelineLayout = gpu->device().CreatePipelineLayout(&pipelineLayoutDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400352 result->fBuiltinUniformHandles = builder.fUniformHandles;
Stephen White729c78d2019-10-14 12:42:59 -0400353 const GrPipeline& pipeline = programInfo.pipeline();
Stephen Whitee2641312019-08-29 15:10:50 -0400354 auto colorState = create_color_state(gpu, pipeline, colorFormat);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400355 wgpu::DepthStencilStateDescriptor depthStencilState;
Robert Phillipsa87c5292019-11-12 10:12:42 -0500356
357#ifdef SK_DEBUG
Stephen Whitef813ef72019-08-09 12:28:37 -0400358 if (pipeline.isStencilEnabled()) {
Robert Phillipsa87c5292019-11-12 10:12:42 -0500359 SkASSERT(renderTarget->renderTargetPriv().numStencilBits() == 8);
Stephen Whitef813ef72019-08-09 12:28:37 -0400360 }
Robert Phillipsa87c5292019-11-12 10:12:42 -0500361#endif
362 depthStencilState = create_depth_stencil_state(programInfo, depthStencilFormat);
Stephen Whitee2641312019-08-29 15:10:50 -0400363
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500364 std::vector<wgpu::VertexBufferLayoutDescriptor> inputs;
Stephen Whitee2641312019-08-29 15:10:50 -0400365
Stephen White3cc8d4f2019-10-30 09:56:23 -0400366 std::vector<wgpu::VertexAttributeDescriptor> vertexAttributes;
Stephen White729c78d2019-10-14 12:42:59 -0400367 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Stephen Whitee2641312019-08-29 15:10:50 -0400368 if (primProc.numVertexAttributes() > 0) {
369 size_t offset = 0;
370 int i = 0;
371 for (const auto& attrib : primProc.vertexAttributes()) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400372 wgpu::VertexAttributeDescriptor attribute;
Stephen Whitee2641312019-08-29 15:10:50 -0400373 attribute.shaderLocation = i;
374 attribute.offset = offset;
375 attribute.format = to_dawn_vertex_format(attrib.cpuType());
376 vertexAttributes.push_back(attribute);
377 offset += attrib.sizeAlign4();
378 i++;
379 }
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500380 wgpu::VertexBufferLayoutDescriptor input;
381 input.arrayStride = offset;
Stephen White3cc8d4f2019-10-30 09:56:23 -0400382 input.stepMode = wgpu::InputStepMode::Vertex;
Stephen Whitee2641312019-08-29 15:10:50 -0400383 input.attributeCount = vertexAttributes.size();
384 input.attributes = &vertexAttributes.front();
385 inputs.push_back(input);
386 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400387 std::vector<wgpu::VertexAttributeDescriptor> instanceAttributes;
Stephen Whitee2641312019-08-29 15:10:50 -0400388 if (primProc.numInstanceAttributes() > 0) {
389 size_t offset = 0;
390 int i = 0;
391 for (const auto& attrib : primProc.instanceAttributes()) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400392 wgpu::VertexAttributeDescriptor attribute;
Stephen Whitee2641312019-08-29 15:10:50 -0400393 attribute.shaderLocation = i;
394 attribute.offset = offset;
395 attribute.format = to_dawn_vertex_format(attrib.cpuType());
396 instanceAttributes.push_back(attribute);
397 offset += attrib.sizeAlign4();
398 i++;
399 }
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500400 wgpu::VertexBufferLayoutDescriptor input;
401 input.arrayStride = offset;
Stephen White3cc8d4f2019-10-30 09:56:23 -0400402 input.stepMode = wgpu::InputStepMode::Instance;
Stephen Whitee2641312019-08-29 15:10:50 -0400403 input.attributeCount = instanceAttributes.size();
404 input.attributes = &instanceAttributes.front();
405 inputs.push_back(input);
406 }
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500407 wgpu::VertexStateDescriptor vertexState;
408 vertexState.indexFormat = wgpu::IndexFormat::Uint16;
409 vertexState.vertexBufferCount = inputs.size();
410 vertexState.vertexBuffers = &inputs.front();
Stephen Whitee2641312019-08-29 15:10:50 -0400411
Stephen White3cc8d4f2019-10-30 09:56:23 -0400412 wgpu::ProgrammableStageDescriptor vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400413 vsDesc.module = vsModule;
414 vsDesc.entryPoint = "main";
415
Stephen White3cc8d4f2019-10-30 09:56:23 -0400416 wgpu::ProgrammableStageDescriptor fsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400417 fsDesc.module = fsModule;
418 fsDesc.entryPoint = "main";
419
Stephen White3cc8d4f2019-10-30 09:56:23 -0400420 wgpu::RenderPipelineDescriptor rpDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400421 rpDesc.layout = pipelineLayout;
Stephen White20c626a2019-10-15 13:35:37 -0400422 rpDesc.vertexStage = vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400423 rpDesc.fragmentStage = &fsDesc;
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500424 rpDesc.vertexState = &vertexState;
Robert Phillipsfcaae482019-11-07 10:17:03 -0500425 rpDesc.primitiveTopology = to_dawn_primitive_topology(programInfo.primitiveType());
Stephen Whitee2641312019-08-29 15:10:50 -0400426 if (hasDepthStencil) {
427 rpDesc.depthStencilState = &depthStencilState;
428 }
429 rpDesc.colorStateCount = 1;
Stephen White20c626a2019-10-15 13:35:37 -0400430 rpDesc.colorStates = &colorState;
Stephen Whitee2641312019-08-29 15:10:50 -0400431 result->fRenderPipeline = gpu->device().CreateRenderPipeline(&rpDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400432 return result;
433}
434
435GrDawnProgramBuilder::GrDawnProgramBuilder(GrDawnGpu* gpu,
436 GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400437 const GrProgramInfo& programInfo,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400438 GrProgramDesc* desc)
Stephen White511af2e2020-02-07 11:04:58 -0500439 : INHERITED(renderTarget, *desc, programInfo)
Stephen Whitebb6bed12019-08-02 09:57:55 -0400440 , fGpu(gpu)
441 , fVaryingHandler(this)
442 , fUniformHandler(this) {
443}
444
Stephen White3cc8d4f2019-10-30 09:56:23 -0400445wgpu::ShaderModule GrDawnProgramBuilder::createShaderModule(const GrGLSLShaderBuilder& builder,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400446 SkSL::Program::Kind kind,
Stephen White20c626a2019-10-15 13:35:37 -0400447 bool flipY,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400448 SkSL::Program::Inputs* inputs) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400449 wgpu::Device device = fGpu->device();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400450 SkString source(builder.fCompilerString.c_str());
451
452#if 0
453 SkSL::String sksl = GrShaderUtils::PrettyPrint(builder.fCompilerString);
454 printf("converting program:\n%s\n", sksl.c_str());
455#endif
456
Stephen White40c47e12019-11-01 13:13:03 -0400457 SkSL::String spirvSource = sksl_to_spirv(fGpu, source.c_str(), kind, flipY,
458 fUniformHandler.getRTHeightOffset(), inputs);
459 if (inputs->fRTHeight) {
460 this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
461 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400462
Stephen White3cc8d4f2019-10-30 09:56:23 -0400463 wgpu::ShaderModuleDescriptor desc;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400464 desc.codeSize = spirvSource.size() / 4;
465 desc.code = reinterpret_cast<const uint32_t*>(spirvSource.c_str());
466
467 return device.CreateShaderModule(&desc);
468};
469
470const GrCaps* GrDawnProgramBuilder::caps() const {
471 return fGpu->caps();
472}
473
474void GrDawnProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) {
475 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
476 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
477 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
478 fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
479 }
480
481 // set RT adjustment
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400482 SkISize dimensions = rt->dimensions();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400483 SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid());
484 if (fRenderTargetState.fRenderTargetOrigin != origin ||
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400485 fRenderTargetState.fRenderTargetSize != dimensions) {
486 fRenderTargetState.fRenderTargetSize = dimensions;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400487 fRenderTargetState.fRenderTargetOrigin = origin;
488
489 float rtAdjustmentVec[4];
490 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
491 fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
492 }
493}
494
Brian Salomonccb61422020-01-09 10:46:36 -0500495static void set_texture(GrDawnGpu* gpu, GrSamplerState state, GrTexture* texture,
496 std::vector<wgpu::BindGroupBinding>* bindings, int* binding) {
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400497 // FIXME: could probably cache samplers in GrDawnProgram
Stephen White3cc8d4f2019-10-30 09:56:23 -0400498 wgpu::Sampler sampler = gpu->getOrCreateSampler(state);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400499 bindings->push_back(make_bind_group_binding((*binding)++, sampler));
500 GrDawnTexture* tex = static_cast<GrDawnTexture*>(texture);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400501 wgpu::TextureView textureView = tex->textureView();
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400502 bindings->push_back(make_bind_group_binding((*binding)++, textureView));
503}
504
Stephen White7cb52cb2019-11-12 10:40:27 -0500505wgpu::BindGroup GrDawnProgram::setUniformData(GrDawnGpu* gpu, const GrRenderTarget* renderTarget,
506 const GrProgramInfo& programInfo) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400507 std::vector<wgpu::BindGroupBinding> bindings;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400508 GrDawnRingBuffer::Slice slice;
509 uint32_t uniformBufferSize = fDataManager.uniformBufferSize();
510 if (0 != uniformBufferSize) {
511 slice = gpu->allocateUniformRingBufferSlice(uniformBufferSize);
512 bindings.push_back(make_bind_group_binding(GrDawnUniformHandler::kUniformBinding,
513 slice.fBuffer, slice.fOffset,
514 uniformBufferSize));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400515 }
Stephen White729c78d2019-10-14 12:42:59 -0400516 this->setRenderTargetState(renderTarget, programInfo.origin());
517 const GrPipeline& pipeline = programInfo.pipeline();
518 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Brian Salomonc241b582019-11-27 08:57:17 -0500519 GrFragmentProcessor::PipelineCoordTransformRange transformRange(pipeline);
520 fGeometryProcessor->setData(fDataManager, primProc, transformRange);
Brian Salomon7eabfe82019-12-02 14:20:20 -0500521 GrFragmentProcessor::CIter fpIter(pipeline);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400522 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
Brian Salomonc241b582019-11-27 08:57:17 -0500523 for (; fpIter && glslIter; ++fpIter, ++glslIter) {
524 glslIter->setData(fDataManager, *fpIter);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400525 }
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400526 SkIPoint offset;
527 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
528 fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset);
Stephen White8eda4992020-03-17 11:44:48 -0400529 if (0 != uniformBufferSize) {
530 fDataManager.uploadUniformBuffers(slice.fData);
531 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400532 wgpu::BindGroupDescriptor descriptor;
Stephen White7cb52cb2019-11-12 10:40:27 -0500533 descriptor.layout = fBindGroupLayouts[0];
534 descriptor.bindingCount = bindings.size();
535 descriptor.bindings = bindings.data();
536 return gpu->device().CreateBindGroup(&descriptor);
537}
538
539wgpu::BindGroup GrDawnProgram::setTextures(GrDawnGpu* gpu,
Stephen White6e8ceee2020-02-25 16:25:43 -0500540 const GrPrimitiveProcessor& primProc,
541 const GrPipeline& pipeline,
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500542 const GrSurfaceProxy* const primProcTextures[]) {
Stephen White7cb52cb2019-11-12 10:40:27 -0500543 std::vector<wgpu::BindGroupBinding> bindings;
544 int binding = 0;
Stephen White7cb52cb2019-11-12 10:40:27 -0500545 if (primProcTextures) {
546 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500547 SkASSERT(primProcTextures[i]->asTextureProxy());
Stephen White7cb52cb2019-11-12 10:40:27 -0500548 auto& sampler = primProc.textureSampler(i);
549 set_texture(gpu, sampler.samplerState(), primProcTextures[i]->peekTexture(), &bindings,
550 &binding);
551 }
552 }
Stephen Whitea521c962019-12-04 09:57:48 -0500553 GrFragmentProcessor::CIter fpIter(pipeline);
Stephen White7cb52cb2019-11-12 10:40:27 -0500554 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
Brian Salomonc241b582019-11-27 08:57:17 -0500555 for (; fpIter && glslIter; ++fpIter, ++glslIter) {
556 for (int i = 0; i < fpIter->numTextureSamplers(); ++i) {
557 auto& s = fpIter->textureSampler(i);
Stephen White7cb52cb2019-11-12 10:40:27 -0500558 set_texture(gpu, s.samplerState(), s.peekTexture(), &bindings, &binding);
559 }
Stephen White7cb52cb2019-11-12 10:40:27 -0500560 }
561 SkIPoint offset;
562 if (GrTexture* dstTexture = pipeline.peekDstTexture(&offset)) {
Brian Salomonccb61422020-01-09 10:46:36 -0500563 set_texture(gpu, GrSamplerState::Filter::kNearest, dstTexture, &bindings, &binding);
Stephen White7cb52cb2019-11-12 10:40:27 -0500564 }
565 wgpu::BindGroupDescriptor descriptor;
566 descriptor.layout = fBindGroupLayouts[1];
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400567 descriptor.bindingCount = bindings.size();
568 descriptor.bindings = bindings.data();
569 return gpu->device().CreateBindGroup(&descriptor);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400570}