blob: d8c0e0b9482120ea624e6f5b6f092ff31ff500a0 [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 White20c626a2019-10-15 13:35:37 -040018 SkSL::Program::Kind kind, bool flipY,
19 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 Whitebb6bed12019-08-02 09:57:55 -040023 std::unique_ptr<SkSL::Program> program = gpu->shaderCompiler()->convertProgram(
24 kind,
25 shaderString,
26 settings);
27 if (!program) {
28 SkDebugf("SkSL error:\n%s\n", gpu->shaderCompiler()->errorText().c_str());
29 SkASSERT(false);
30 return "";
31 }
32 *inputs = program->fInputs;
33 SkSL::String code;
34 if (!gpu->shaderCompiler()->toSPIRV(*program, &code)) {
35 return "";
36 }
37 return code;
38}
39
40static dawn::BlendFactor to_dawn_blend_factor(GrBlendCoeff coeff) {
41 switch (coeff) {
42 case kZero_GrBlendCoeff:
43 return dawn::BlendFactor::Zero;
44 case kOne_GrBlendCoeff:
45 return dawn::BlendFactor::One;
46 case kSC_GrBlendCoeff:
47 return dawn::BlendFactor::SrcColor;
48 case kISC_GrBlendCoeff:
49 return dawn::BlendFactor::OneMinusSrcColor;
50 case kDC_GrBlendCoeff:
51 return dawn::BlendFactor::DstColor;
52 case kIDC_GrBlendCoeff:
53 return dawn::BlendFactor::OneMinusDstColor;
54 case kSA_GrBlendCoeff:
55 return dawn::BlendFactor::SrcAlpha;
56 case kISA_GrBlendCoeff:
57 return dawn::BlendFactor::OneMinusSrcAlpha;
58 case kDA_GrBlendCoeff:
59 return dawn::BlendFactor::DstAlpha;
60 case kIDA_GrBlendCoeff:
61 return dawn::BlendFactor::OneMinusDstAlpha;
62 case kConstC_GrBlendCoeff:
63 return dawn::BlendFactor::BlendColor;
64 case kIConstC_GrBlendCoeff:
65 return dawn::BlendFactor::OneMinusBlendColor;
66 case kConstA_GrBlendCoeff:
67 case kIConstA_GrBlendCoeff:
68 case kS2C_GrBlendCoeff:
69 case kIS2C_GrBlendCoeff:
70 case kS2A_GrBlendCoeff:
71 case kIS2A_GrBlendCoeff:
72 default:
73 SkASSERT(!"unsupported blend coefficient");
74 return dawn::BlendFactor::One;
75 }
76}
77
Stephen Whitef813ef72019-08-09 12:28:37 -040078static dawn::BlendFactor to_dawn_blend_factor_for_alpha(GrBlendCoeff coeff) {
79 switch (coeff) {
80 // Force all srcColor used in alpha slot to alpha version.
81 case kSC_GrBlendCoeff:
82 return dawn::BlendFactor::SrcAlpha;
83 case kISC_GrBlendCoeff:
84 return dawn::BlendFactor::OneMinusSrcAlpha;
85 case kDC_GrBlendCoeff:
86 return dawn::BlendFactor::DstAlpha;
87 case kIDC_GrBlendCoeff:
88 return dawn::BlendFactor::OneMinusDstAlpha;
89 default:
90 return to_dawn_blend_factor(coeff);
91 }
92}
93
Stephen Whitebb6bed12019-08-02 09:57:55 -040094static dawn::BlendOperation to_dawn_blend_operation(GrBlendEquation equation) {
95 switch (equation) {
96 case kAdd_GrBlendEquation:
97 return dawn::BlendOperation::Add;
98 case kSubtract_GrBlendEquation:
99 return dawn::BlendOperation::Subtract;
Stephen Whitef813ef72019-08-09 12:28:37 -0400100 case kReverseSubtract_GrBlendEquation:
101 return dawn::BlendOperation::ReverseSubtract;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400102 default:
103 SkASSERT(!"unsupported blend equation");
104 return dawn::BlendOperation::Add;
105 }
106}
107
Stephen Whitef813ef72019-08-09 12:28:37 -0400108static dawn::CompareFunction to_dawn_compare_function(GrStencilTest test) {
109 switch (test) {
110 case GrStencilTest::kAlways:
111 return dawn::CompareFunction::Always;
112 case GrStencilTest::kNever:
113 return dawn::CompareFunction::Never;
114 case GrStencilTest::kGreater:
115 return dawn::CompareFunction::Greater;
116 case GrStencilTest::kGEqual:
117 return dawn::CompareFunction::GreaterEqual;
118 case GrStencilTest::kLess:
119 return dawn::CompareFunction::Less;
120 case GrStencilTest::kLEqual:
121 return dawn::CompareFunction::LessEqual;
122 case GrStencilTest::kEqual:
123 return dawn::CompareFunction::Equal;
124 case GrStencilTest::kNotEqual:
125 return dawn::CompareFunction::NotEqual;
126 default:
127 SkASSERT(!"unsupported stencil test");
128 return dawn::CompareFunction::Always;
129 }
130}
131
132static dawn::StencilOperation to_dawn_stencil_operation(GrStencilOp op) {
133 switch (op) {
134 case GrStencilOp::kKeep:
135 return dawn::StencilOperation::Keep;
136 case GrStencilOp::kZero:
137 return dawn::StencilOperation::Zero;
138 case GrStencilOp::kReplace:
139 return dawn::StencilOperation::Replace;
140 case GrStencilOp::kInvert:
141 return dawn::StencilOperation::Invert;
142 case GrStencilOp::kIncClamp:
143 return dawn::StencilOperation::IncrementClamp;
144 case GrStencilOp::kDecClamp:
145 return dawn::StencilOperation::DecrementClamp;
146 case GrStencilOp::kIncWrap:
147 return dawn::StencilOperation::IncrementWrap;
148 case GrStencilOp::kDecWrap:
149 return dawn::StencilOperation::DecrementWrap;
150 default:
151 SkASSERT(!"unsupported stencil function");
152 return dawn::StencilOperation::Keep;
153 }
154}
155
Stephen Whitee2641312019-08-29 15:10:50 -0400156static dawn::PrimitiveTopology to_dawn_primitive_topology(GrPrimitiveType primitiveType) {
157 switch (primitiveType) {
158 case GrPrimitiveType::kTriangles:
159 return dawn::PrimitiveTopology::TriangleList;
160 case GrPrimitiveType::kTriangleStrip:
161 return dawn::PrimitiveTopology::TriangleStrip;
162 case GrPrimitiveType::kPoints:
163 return dawn::PrimitiveTopology::PointList;
164 case GrPrimitiveType::kLines:
165 return dawn::PrimitiveTopology::LineList;
166 case GrPrimitiveType::kLineStrip:
167 return dawn::PrimitiveTopology::LineStrip;
Robert Phillips571177f2019-10-04 14:41:49 -0400168 case GrPrimitiveType::kPath:
Stephen Whitee2641312019-08-29 15:10:50 -0400169 default:
170 SkASSERT(!"unsupported primitive topology");
171 return dawn::PrimitiveTopology::TriangleList;
172 }
173}
174
175static dawn::VertexFormat to_dawn_vertex_format(GrVertexAttribType type) {
176 switch (type) {
177 case kFloat_GrVertexAttribType:
178 case kHalf_GrVertexAttribType:
179 return dawn::VertexFormat::Float;
180 case kFloat2_GrVertexAttribType:
181 case kHalf2_GrVertexAttribType:
182 return dawn::VertexFormat::Float2;
183 case kFloat3_GrVertexAttribType:
184 case kHalf3_GrVertexAttribType:
185 return dawn::VertexFormat::Float3;
186 case kFloat4_GrVertexAttribType:
187 case kHalf4_GrVertexAttribType:
188 return dawn::VertexFormat::Float4;
189 case kUShort2_GrVertexAttribType:
190 return dawn::VertexFormat::UShort2;
191 case kInt_GrVertexAttribType:
192 return dawn::VertexFormat::Int;
193 case kUByte4_norm_GrVertexAttribType:
194 return dawn::VertexFormat::UChar4Norm;
195 default:
196 SkASSERT(!"unsupported vertex format");
197 return dawn::VertexFormat::Float4;
198 }
199}
200
Stephen Whitebb6bed12019-08-02 09:57:55 -0400201static dawn::ColorStateDescriptor create_color_state(const GrDawnGpu* gpu,
202 const GrPipeline& pipeline,
203 dawn::TextureFormat colorFormat) {
204 GrXferProcessor::BlendInfo blendInfo = pipeline.getXferProcessor().getBlendInfo();
205 GrBlendEquation equation = blendInfo.fEquation;
206 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
207 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
208
209 dawn::BlendFactor srcFactor = to_dawn_blend_factor(srcCoeff);
210 dawn::BlendFactor dstFactor = to_dawn_blend_factor(dstCoeff);
Stephen Whitef813ef72019-08-09 12:28:37 -0400211 dawn::BlendFactor srcFactorAlpha = to_dawn_blend_factor_for_alpha(srcCoeff);
212 dawn::BlendFactor dstFactorAlpha = to_dawn_blend_factor_for_alpha(dstCoeff);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400213 dawn::BlendOperation operation = to_dawn_blend_operation(equation);
214 auto mask = blendInfo.fWriteColor ? dawn::ColorWriteMask::All : dawn::ColorWriteMask::None;
215
216 dawn::BlendDescriptor colorDesc = {operation, srcFactor, dstFactor};
Stephen Whitef813ef72019-08-09 12:28:37 -0400217 dawn::BlendDescriptor alphaDesc = {operation, srcFactorAlpha, dstFactorAlpha};
Stephen Whitebb6bed12019-08-02 09:57:55 -0400218
219 dawn::ColorStateDescriptor descriptor;
220 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 Whitef813ef72019-08-09 12:28:37 -0400229static dawn::StencilStateFaceDescriptor to_stencil_state_face(const GrStencilSettings::Face& face) {
230 dawn::StencilStateFaceDescriptor desc;
231 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
237static dawn::DepthStencilStateDescriptor create_depth_stencil_state(
238 const GrStencilSettings& stencilSettings,
239 dawn::TextureFormat depthStencilFormat,
240 GrSurfaceOrigin origin) {
241 dawn::DepthStencilStateDescriptor state;
242 state.format = depthStencilFormat;
Stephen Whitee2641312019-08-29 15:10:50 -0400243 if (!stencilSettings.isDisabled()) {
Stephen Whitef813ef72019-08-09 12:28:37 -0400244 if (stencilSettings.isTwoSided()) {
Stephen Whitef4b3d6b2019-10-18 07:54:25 -0400245 auto front = stencilSettings.front(origin);
246 auto back = stencilSettings.front(origin);
247 state.stencilFront = to_stencil_state_face(front);
248 state.stencilBack = to_stencil_state_face(back);
249 state.stencilReadMask = front.fTestMask;
250 state.stencilWriteMask = front.fWriteMask;
Stephen Whitef813ef72019-08-09 12:28:37 -0400251 } else {
Stephen Whitef4b3d6b2019-10-18 07:54:25 -0400252 auto frontAndBack = stencilSettings.frontAndBack();
253 state.stencilBack = state.stencilFront = to_stencil_state_face(frontAndBack);
254 state.stencilReadMask = frontAndBack.fTestMask;
255 state.stencilWriteMask = frontAndBack.fWriteMask;
Stephen Whitef813ef72019-08-09 12:28:37 -0400256 }
257 }
258 return state;
259}
260
Stephen Whitebb6bed12019-08-02 09:57:55 -0400261static dawn::BindGroupBinding make_bind_group_binding(uint32_t binding, const dawn::Buffer& buffer,
262 uint32_t offset, uint32_t size, const
263 dawn::Sampler& sampler,
264 const dawn::TextureView& textureView) {
265 dawn::BindGroupBinding result;
266 result.binding = binding;
267 result.buffer = buffer;
268 result.offset = offset;
269 result.size = size;
270 result.sampler = sampler;
271 result.textureView = textureView;
272 return result;
273}
274
275static dawn::BindGroupBinding make_bind_group_binding(uint32_t binding, const dawn::Buffer& buffer,
276 uint32_t offset, uint32_t size) {
277 return make_bind_group_binding(binding, buffer, offset, size, nullptr, nullptr);
278}
279
Stephen White170d9902019-08-15 16:48:24 -0400280static dawn::BindGroupBinding make_bind_group_binding(uint32_t binding,
281 const dawn::Sampler& sampler) {
282 return make_bind_group_binding(binding, nullptr, 0, 0, sampler, nullptr);
283}
284
285static dawn::BindGroupBinding make_bind_group_binding(uint32_t binding,
286 const dawn::TextureView& textureView) {
287 return make_bind_group_binding(binding, nullptr, 0, 0, nullptr, textureView);
288}
289
Stephen Whitebb6bed12019-08-02 09:57:55 -0400290sk_sp<GrDawnProgram> GrDawnProgramBuilder::Build(GrDawnGpu* gpu,
291 GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400292 const GrProgramInfo& programInfo,
Stephen Whitee2641312019-08-29 15:10:50 -0400293 GrPrimitiveType primitiveType,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400294 dawn::TextureFormat colorFormat,
Stephen Whitef813ef72019-08-09 12:28:37 -0400295 bool hasDepthStencil,
296 dawn::TextureFormat depthStencilFormat,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400297 GrProgramDesc* desc) {
Stephen White729c78d2019-10-14 12:42:59 -0400298 GrDawnProgramBuilder builder(gpu, renderTarget, programInfo, desc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400299 if (!builder.emitAndInstallProcs()) {
300 return nullptr;
301 }
302
303 builder.fVS.extensions().appendf("#extension GL_ARB_separate_shader_objects : enable\n");
304 builder.fFS.extensions().appendf("#extension GL_ARB_separate_shader_objects : enable\n");
305 builder.fVS.extensions().appendf("#extension GL_ARB_shading_language_420pack : enable\n");
306 builder.fFS.extensions().appendf("#extension GL_ARB_shading_language_420pack : enable\n");
307
308 builder.finalizeShaders();
309
310 SkSL::Program::Inputs vertInputs, fragInputs;
311 GrDawnUniformHandler::UniformInfoArray& uniforms = builder.fUniformHandler.fUniforms;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400312 uint32_t uniformBufferSize = builder.fUniformHandler.fCurrentUBOOffset;
313 sk_sp<GrDawnProgram> result(new GrDawnProgram(uniforms, uniformBufferSize));
Stephen White20c626a2019-10-15 13:35:37 -0400314 bool flipY = programInfo.origin() != kTopLeft_GrSurfaceOrigin;
315 auto vsModule = builder.createShaderModule(builder.fVS, SkSL::Program::kVertex_Kind, flipY,
Stephen Whitee2641312019-08-29 15:10:50 -0400316 &vertInputs);
Stephen White20c626a2019-10-15 13:35:37 -0400317 auto fsModule = builder.createShaderModule(builder.fFS, SkSL::Program::kFragment_Kind, flipY,
Stephen Whitee2641312019-08-29 15:10:50 -0400318 &fragInputs);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400319 result->fGeometryProcessor = std::move(builder.fGeometryProcessor);
320 result->fXferProcessor = std::move(builder.fXferProcessor);
321 result->fFragmentProcessors = std::move(builder.fFragmentProcessors);
322 result->fFragmentProcessorCnt = builder.fFragmentProcessorCnt;
323 std::vector<dawn::BindGroupLayoutBinding> layoutBindings;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400324 if (0 != uniformBufferSize) {
325 layoutBindings.push_back({ GrDawnUniformHandler::kUniformBinding,
326 dawn::ShaderStage::Vertex | dawn::ShaderStage::Fragment,
Stephen White170d9902019-08-15 16:48:24 -0400327 dawn::BindingType::UniformBuffer});
328 }
329 uint32_t binding = GrDawnUniformHandler::kSamplerBindingBase;
330 for (int i = 0; i < builder.fUniformHandler.fSamplers.count(); ++i) {
Stephen White20c626a2019-10-15 13:35:37 -0400331 layoutBindings.push_back({ binding++, dawn::ShaderStage::Fragment,
Stephen White170d9902019-08-15 16:48:24 -0400332 dawn::BindingType::Sampler});
Stephen White20c626a2019-10-15 13:35:37 -0400333 layoutBindings.push_back({ binding++, dawn::ShaderStage::Fragment,
Stephen White170d9902019-08-15 16:48:24 -0400334 dawn::BindingType::SampledTexture});
335 }
336 dawn::BindGroupLayoutDescriptor bindGroupLayoutDesc;
337 bindGroupLayoutDesc.bindingCount = layoutBindings.size();
338 bindGroupLayoutDesc.bindings = layoutBindings.data();
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400339 result->fBindGroupLayout = gpu->device().CreateBindGroupLayout(&bindGroupLayoutDesc);
Stephen White170d9902019-08-15 16:48:24 -0400340 dawn::PipelineLayoutDescriptor pipelineLayoutDesc;
341 pipelineLayoutDesc.bindGroupLayoutCount = 1;
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400342 pipelineLayoutDesc.bindGroupLayouts = &result->fBindGroupLayout;
Stephen Whitee2641312019-08-29 15:10:50 -0400343 auto pipelineLayout = gpu->device().CreatePipelineLayout(&pipelineLayoutDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400344 result->fBuiltinUniformHandles = builder.fUniformHandles;
Stephen White729c78d2019-10-14 12:42:59 -0400345 const GrPipeline& pipeline = programInfo.pipeline();
Stephen Whitee2641312019-08-29 15:10:50 -0400346 auto colorState = create_color_state(gpu, pipeline, colorFormat);
347 dawn::DepthStencilStateDescriptor depthStencilState;
Stephen Whitef813ef72019-08-09 12:28:37 -0400348 GrStencilSettings stencil;
349 if (pipeline.isStencilEnabled()) {
350 int numStencilBits = renderTarget->renderTargetPriv().numStencilBits();
351 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(), numStencilBits);
352 }
Stephen White729c78d2019-10-14 12:42:59 -0400353 depthStencilState = create_depth_stencil_state(stencil, depthStencilFormat,
354 programInfo.origin());
Stephen Whitee2641312019-08-29 15:10:50 -0400355
356 std::vector<dawn::VertexBufferDescriptor> inputs;
357
358 std::vector<dawn::VertexAttributeDescriptor> vertexAttributes;
Stephen White729c78d2019-10-14 12:42:59 -0400359 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Stephen Whitee2641312019-08-29 15:10:50 -0400360 if (primProc.numVertexAttributes() > 0) {
361 size_t offset = 0;
362 int i = 0;
363 for (const auto& attrib : primProc.vertexAttributes()) {
364 dawn::VertexAttributeDescriptor attribute;
365 attribute.shaderLocation = i;
366 attribute.offset = offset;
367 attribute.format = to_dawn_vertex_format(attrib.cpuType());
368 vertexAttributes.push_back(attribute);
369 offset += attrib.sizeAlign4();
370 i++;
371 }
372 dawn::VertexBufferDescriptor input;
373 input.stride = offset;
374 input.stepMode = dawn::InputStepMode::Vertex;
375 input.attributeCount = vertexAttributes.size();
376 input.attributes = &vertexAttributes.front();
377 inputs.push_back(input);
378 }
379 std::vector<dawn::VertexAttributeDescriptor> instanceAttributes;
380 if (primProc.numInstanceAttributes() > 0) {
381 size_t offset = 0;
382 int i = 0;
383 for (const auto& attrib : primProc.instanceAttributes()) {
384 dawn::VertexAttributeDescriptor attribute;
385 attribute.shaderLocation = i;
386 attribute.offset = offset;
387 attribute.format = to_dawn_vertex_format(attrib.cpuType());
388 instanceAttributes.push_back(attribute);
389 offset += attrib.sizeAlign4();
390 i++;
391 }
392 dawn::VertexBufferDescriptor input;
393 input.stride = offset;
394 input.stepMode = dawn::InputStepMode::Instance;
395 input.attributeCount = instanceAttributes.size();
396 input.attributes = &instanceAttributes.front();
397 inputs.push_back(input);
398 }
399 dawn::VertexInputDescriptor vertexInput;
400 vertexInput.indexFormat = dawn::IndexFormat::Uint16;
401 vertexInput.bufferCount = inputs.size();
402 vertexInput.buffers = &inputs.front();
403
Stephen White20c626a2019-10-15 13:35:37 -0400404 dawn::ProgrammableStageDescriptor vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400405 vsDesc.module = vsModule;
406 vsDesc.entryPoint = "main";
407
Stephen White20c626a2019-10-15 13:35:37 -0400408 dawn::ProgrammableStageDescriptor fsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400409 fsDesc.module = fsModule;
410 fsDesc.entryPoint = "main";
411
412 dawn::RenderPipelineDescriptor rpDesc;
413 rpDesc.layout = pipelineLayout;
Stephen White20c626a2019-10-15 13:35:37 -0400414 rpDesc.vertexStage = vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400415 rpDesc.fragmentStage = &fsDesc;
416 rpDesc.vertexInput = &vertexInput;
417 rpDesc.primitiveTopology = to_dawn_primitive_topology(primitiveType);
418 if (hasDepthStencil) {
419 rpDesc.depthStencilState = &depthStencilState;
420 }
421 rpDesc.colorStateCount = 1;
Stephen White20c626a2019-10-15 13:35:37 -0400422 rpDesc.colorStates = &colorState;
Stephen Whitee2641312019-08-29 15:10:50 -0400423 result->fRenderPipeline = gpu->device().CreateRenderPipeline(&rpDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400424 return result;
425}
426
427GrDawnProgramBuilder::GrDawnProgramBuilder(GrDawnGpu* gpu,
428 GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400429 const GrProgramInfo& programInfo,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400430 GrProgramDesc* desc)
Stephen White729c78d2019-10-14 12:42:59 -0400431 : INHERITED(renderTarget, programInfo, desc)
Stephen Whitebb6bed12019-08-02 09:57:55 -0400432 , fGpu(gpu)
433 , fVaryingHandler(this)
434 , fUniformHandler(this) {
435}
436
437dawn::ShaderModule GrDawnProgramBuilder::createShaderModule(const GrGLSLShaderBuilder& builder,
438 SkSL::Program::Kind kind,
Stephen White20c626a2019-10-15 13:35:37 -0400439 bool flipY,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400440 SkSL::Program::Inputs* inputs) {
441 dawn::Device device = fGpu->device();
442 SkString source(builder.fCompilerString.c_str());
443
444#if 0
445 SkSL::String sksl = GrShaderUtils::PrettyPrint(builder.fCompilerString);
446 printf("converting program:\n%s\n", sksl.c_str());
447#endif
448
Stephen White20c626a2019-10-15 13:35:37 -0400449 SkSL::String spirvSource = sksl_to_spirv(fGpu, source.c_str(), kind, flipY, inputs);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400450
451 dawn::ShaderModuleDescriptor desc;
452 desc.codeSize = spirvSource.size() / 4;
453 desc.code = reinterpret_cast<const uint32_t*>(spirvSource.c_str());
454
455 return device.CreateShaderModule(&desc);
456};
457
458const GrCaps* GrDawnProgramBuilder::caps() const {
459 return fGpu->caps();
460}
461
462void GrDawnProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) {
463 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
464 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
465 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
466 fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
467 }
468
469 // set RT adjustment
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400470 SkISize dimensions = rt->dimensions();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400471 SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid());
472 if (fRenderTargetState.fRenderTargetOrigin != origin ||
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400473 fRenderTargetState.fRenderTargetSize != dimensions) {
474 fRenderTargetState.fRenderTargetSize = dimensions;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400475 fRenderTargetState.fRenderTargetOrigin = origin;
476
477 float rtAdjustmentVec[4];
478 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
479 fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
480 }
481}
482
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400483static void setTexture(GrDawnGpu* gpu, const GrSamplerState& state, GrTexture* texture,
484 std::vector<dawn::BindGroupBinding> *bindings, int* binding) {
485 // FIXME: could probably cache samplers in GrDawnProgram
Stephen Whitec0c05042019-09-10 16:15:23 -0400486 dawn::Sampler sampler = gpu->getOrCreateSampler(state);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400487 bindings->push_back(make_bind_group_binding((*binding)++, sampler));
488 GrDawnTexture* tex = static_cast<GrDawnTexture*>(texture);
489 dawn::TextureView textureView = tex->textureView();
490 bindings->push_back(make_bind_group_binding((*binding)++, textureView));
491}
492
493dawn::BindGroup GrDawnProgram::setData(GrDawnGpu* gpu, const GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400494 const GrProgramInfo& programInfo) {
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400495 std::vector<dawn::BindGroupBinding> bindings;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400496 GrDawnRingBuffer::Slice slice;
497 uint32_t uniformBufferSize = fDataManager.uniformBufferSize();
498 if (0 != uniformBufferSize) {
499 slice = gpu->allocateUniformRingBufferSlice(uniformBufferSize);
500 bindings.push_back(make_bind_group_binding(GrDawnUniformHandler::kUniformBinding,
501 slice.fBuffer, slice.fOffset,
502 uniformBufferSize));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400503 }
Stephen White729c78d2019-10-14 12:42:59 -0400504 this->setRenderTargetState(renderTarget, programInfo.origin());
505 const GrPipeline& pipeline = programInfo.pipeline();
506 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400507 fGeometryProcessor->setData(fDataManager, primProc,
508 GrFragmentProcessor::CoordTransformIter(pipeline));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400509 int binding = GrDawnUniformHandler::kSamplerBindingBase;
Stephen White729c78d2019-10-14 12:42:59 -0400510 auto primProcTextures = programInfo.hasFixedPrimProcTextures() ?
511 programInfo.fixedPrimProcTextures() : nullptr;
512
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400513 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
514 auto& sampler = primProc.textureSampler(i);
515 setTexture(gpu, sampler.samplerState(), primProcTextures[i]->peekTexture(), &bindings,
516 &binding);
517 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400518 GrFragmentProcessor::Iter iter(pipeline);
519 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
520 const GrFragmentProcessor* fp = iter.next();
521 GrGLSLFragmentProcessor* glslFP = glslIter.next();
522 while (fp && glslFP) {
523 glslFP->setData(fDataManager, *fp);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400524 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
525 auto& s = fp->textureSampler(i);
526 setTexture(gpu, s.samplerState(), s.peekTexture(), &bindings, &binding);
527 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400528 fp = iter.next();
529 glslFP = glslIter.next();
530 }
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400531 SkIPoint offset;
532 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
533 fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset);
534 if (GrTextureProxy* proxy = pipeline.dstTextureProxy()) {
535 GrFragmentProcessor::TextureSampler sampler(sk_ref_sp(proxy));
536 setTexture(gpu, sampler.samplerState(), sampler.peekTexture(), &bindings, &binding);
537 }
Stephen Whitedd78efd2019-10-23 15:00:20 -0400538 fDataManager.uploadUniformBuffers(gpu, slice);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400539 dawn::BindGroupDescriptor descriptor;
540 descriptor.layout = fBindGroupLayout;
541 descriptor.bindingCount = bindings.size();
542 descriptor.bindings = bindings.data();
543 return gpu->device().CreateBindGroup(&descriptor);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400544}