blob: 84e4067883de59c0f1953d4ff89e25468b413660 [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;
312 uint32_t geometryUniformSize = builder.fUniformHandler.fCurrentGeometryUBOOffset;
313 uint32_t fragmentUniformSize = builder.fUniformHandler.fCurrentFragmentUBOOffset;
314 sk_sp<GrDawnProgram> result(
315 new GrDawnProgram(uniforms, geometryUniformSize, fragmentUniformSize));
Stephen White20c626a2019-10-15 13:35:37 -0400316 bool flipY = programInfo.origin() != kTopLeft_GrSurfaceOrigin;
317 auto vsModule = builder.createShaderModule(builder.fVS, SkSL::Program::kVertex_Kind, flipY,
Stephen Whitee2641312019-08-29 15:10:50 -0400318 &vertInputs);
Stephen White20c626a2019-10-15 13:35:37 -0400319 auto fsModule = builder.createShaderModule(builder.fFS, SkSL::Program::kFragment_Kind, flipY,
Stephen Whitee2641312019-08-29 15:10:50 -0400320 &fragInputs);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400321 result->fGeometryProcessor = std::move(builder.fGeometryProcessor);
322 result->fXferProcessor = std::move(builder.fXferProcessor);
323 result->fFragmentProcessors = std::move(builder.fFragmentProcessors);
324 result->fFragmentProcessorCnt = builder.fFragmentProcessorCnt;
325 std::vector<dawn::BindGroupLayoutBinding> layoutBindings;
Stephen White170d9902019-08-15 16:48:24 -0400326 if (0 != geometryUniformSize) {
327 layoutBindings.push_back({ GrDawnUniformHandler::kGeometryBinding,
Stephen White20c626a2019-10-15 13:35:37 -0400328 dawn::ShaderStage::Vertex,
Stephen White170d9902019-08-15 16:48:24 -0400329 dawn::BindingType::UniformBuffer});
330 }
331 if (0 != fragmentUniformSize) {
332 layoutBindings.push_back({ GrDawnUniformHandler::kFragBinding,
Stephen White20c626a2019-10-15 13:35:37 -0400333 dawn::ShaderStage::Fragment,
Stephen White170d9902019-08-15 16:48:24 -0400334 dawn::BindingType::UniformBuffer});
335 }
336 uint32_t binding = GrDawnUniformHandler::kSamplerBindingBase;
337 for (int i = 0; i < builder.fUniformHandler.fSamplers.count(); ++i) {
Stephen White20c626a2019-10-15 13:35:37 -0400338 layoutBindings.push_back({ binding++, dawn::ShaderStage::Fragment,
Stephen White170d9902019-08-15 16:48:24 -0400339 dawn::BindingType::Sampler});
Stephen White20c626a2019-10-15 13:35:37 -0400340 layoutBindings.push_back({ binding++, dawn::ShaderStage::Fragment,
Stephen White170d9902019-08-15 16:48:24 -0400341 dawn::BindingType::SampledTexture});
342 }
343 dawn::BindGroupLayoutDescriptor bindGroupLayoutDesc;
344 bindGroupLayoutDesc.bindingCount = layoutBindings.size();
345 bindGroupLayoutDesc.bindings = layoutBindings.data();
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400346 result->fBindGroupLayout = gpu->device().CreateBindGroupLayout(&bindGroupLayoutDesc);
Stephen White170d9902019-08-15 16:48:24 -0400347 dawn::PipelineLayoutDescriptor pipelineLayoutDesc;
348 pipelineLayoutDesc.bindGroupLayoutCount = 1;
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400349 pipelineLayoutDesc.bindGroupLayouts = &result->fBindGroupLayout;
Stephen Whitee2641312019-08-29 15:10:50 -0400350 auto pipelineLayout = gpu->device().CreatePipelineLayout(&pipelineLayoutDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400351 result->fBuiltinUniformHandles = builder.fUniformHandles;
Stephen White729c78d2019-10-14 12:42:59 -0400352 const GrPipeline& pipeline = programInfo.pipeline();
Stephen Whitee2641312019-08-29 15:10:50 -0400353 auto colorState = create_color_state(gpu, pipeline, colorFormat);
354 dawn::DepthStencilStateDescriptor depthStencilState;
Stephen Whitef813ef72019-08-09 12:28:37 -0400355 GrStencilSettings stencil;
356 if (pipeline.isStencilEnabled()) {
357 int numStencilBits = renderTarget->renderTargetPriv().numStencilBits();
358 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(), numStencilBits);
359 }
Stephen White729c78d2019-10-14 12:42:59 -0400360 depthStencilState = create_depth_stencil_state(stencil, depthStencilFormat,
361 programInfo.origin());
Stephen Whitee2641312019-08-29 15:10:50 -0400362
363 std::vector<dawn::VertexBufferDescriptor> inputs;
364
365 std::vector<dawn::VertexAttributeDescriptor> vertexAttributes;
Stephen White729c78d2019-10-14 12:42:59 -0400366 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Stephen Whitee2641312019-08-29 15:10:50 -0400367 if (primProc.numVertexAttributes() > 0) {
368 size_t offset = 0;
369 int i = 0;
370 for (const auto& attrib : primProc.vertexAttributes()) {
371 dawn::VertexAttributeDescriptor attribute;
372 attribute.shaderLocation = i;
373 attribute.offset = offset;
374 attribute.format = to_dawn_vertex_format(attrib.cpuType());
375 vertexAttributes.push_back(attribute);
376 offset += attrib.sizeAlign4();
377 i++;
378 }
379 dawn::VertexBufferDescriptor input;
380 input.stride = offset;
381 input.stepMode = dawn::InputStepMode::Vertex;
382 input.attributeCount = vertexAttributes.size();
383 input.attributes = &vertexAttributes.front();
384 inputs.push_back(input);
385 }
386 std::vector<dawn::VertexAttributeDescriptor> instanceAttributes;
387 if (primProc.numInstanceAttributes() > 0) {
388 size_t offset = 0;
389 int i = 0;
390 for (const auto& attrib : primProc.instanceAttributes()) {
391 dawn::VertexAttributeDescriptor attribute;
392 attribute.shaderLocation = i;
393 attribute.offset = offset;
394 attribute.format = to_dawn_vertex_format(attrib.cpuType());
395 instanceAttributes.push_back(attribute);
396 offset += attrib.sizeAlign4();
397 i++;
398 }
399 dawn::VertexBufferDescriptor input;
400 input.stride = offset;
401 input.stepMode = dawn::InputStepMode::Instance;
402 input.attributeCount = instanceAttributes.size();
403 input.attributes = &instanceAttributes.front();
404 inputs.push_back(input);
405 }
406 dawn::VertexInputDescriptor vertexInput;
407 vertexInput.indexFormat = dawn::IndexFormat::Uint16;
408 vertexInput.bufferCount = inputs.size();
409 vertexInput.buffers = &inputs.front();
410
Stephen White20c626a2019-10-15 13:35:37 -0400411 dawn::ProgrammableStageDescriptor vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400412 vsDesc.module = vsModule;
413 vsDesc.entryPoint = "main";
414
Stephen White20c626a2019-10-15 13:35:37 -0400415 dawn::ProgrammableStageDescriptor fsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400416 fsDesc.module = fsModule;
417 fsDesc.entryPoint = "main";
418
419 dawn::RenderPipelineDescriptor rpDesc;
420 rpDesc.layout = pipelineLayout;
Stephen White20c626a2019-10-15 13:35:37 -0400421 rpDesc.vertexStage = vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400422 rpDesc.fragmentStage = &fsDesc;
423 rpDesc.vertexInput = &vertexInput;
424 rpDesc.primitiveTopology = to_dawn_primitive_topology(primitiveType);
425 if (hasDepthStencil) {
426 rpDesc.depthStencilState = &depthStencilState;
427 }
428 rpDesc.colorStateCount = 1;
Stephen White20c626a2019-10-15 13:35:37 -0400429 rpDesc.colorStates = &colorState;
Stephen Whitee2641312019-08-29 15:10:50 -0400430 result->fRenderPipeline = gpu->device().CreateRenderPipeline(&rpDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400431 return result;
432}
433
434GrDawnProgramBuilder::GrDawnProgramBuilder(GrDawnGpu* gpu,
435 GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400436 const GrProgramInfo& programInfo,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400437 GrProgramDesc* desc)
Stephen White729c78d2019-10-14 12:42:59 -0400438 : INHERITED(renderTarget, programInfo, desc)
Stephen Whitebb6bed12019-08-02 09:57:55 -0400439 , fGpu(gpu)
440 , fVaryingHandler(this)
441 , fUniformHandler(this) {
442}
443
444dawn::ShaderModule GrDawnProgramBuilder::createShaderModule(const GrGLSLShaderBuilder& builder,
445 SkSL::Program::Kind kind,
Stephen White20c626a2019-10-15 13:35:37 -0400446 bool flipY,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400447 SkSL::Program::Inputs* inputs) {
448 dawn::Device device = fGpu->device();
449 SkString source(builder.fCompilerString.c_str());
450
451#if 0
452 SkSL::String sksl = GrShaderUtils::PrettyPrint(builder.fCompilerString);
453 printf("converting program:\n%s\n", sksl.c_str());
454#endif
455
Stephen White20c626a2019-10-15 13:35:37 -0400456 SkSL::String spirvSource = sksl_to_spirv(fGpu, source.c_str(), kind, flipY, inputs);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400457
458 dawn::ShaderModuleDescriptor desc;
459 desc.codeSize = spirvSource.size() / 4;
460 desc.code = reinterpret_cast<const uint32_t*>(spirvSource.c_str());
461
462 return device.CreateShaderModule(&desc);
463};
464
465const GrCaps* GrDawnProgramBuilder::caps() const {
466 return fGpu->caps();
467}
468
469void GrDawnProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) {
470 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
471 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
472 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
473 fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
474 }
475
476 // set RT adjustment
477 SkISize size;
478 size.set(rt->width(), rt->height());
479 SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid());
480 if (fRenderTargetState.fRenderTargetOrigin != origin ||
481 fRenderTargetState.fRenderTargetSize != size) {
482 fRenderTargetState.fRenderTargetSize = size;
483 fRenderTargetState.fRenderTargetOrigin = origin;
484
485 float rtAdjustmentVec[4];
486 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
487 fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
488 }
489}
490
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400491static void setTexture(GrDawnGpu* gpu, const GrSamplerState& state, GrTexture* texture,
492 std::vector<dawn::BindGroupBinding> *bindings, int* binding) {
493 // FIXME: could probably cache samplers in GrDawnProgram
Stephen Whitec0c05042019-09-10 16:15:23 -0400494 dawn::Sampler sampler = gpu->getOrCreateSampler(state);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400495 bindings->push_back(make_bind_group_binding((*binding)++, sampler));
496 GrDawnTexture* tex = static_cast<GrDawnTexture*>(texture);
497 dawn::TextureView textureView = tex->textureView();
498 bindings->push_back(make_bind_group_binding((*binding)++, textureView));
499}
500
501dawn::BindGroup GrDawnProgram::setData(GrDawnGpu* gpu, const GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400502 const GrProgramInfo& programInfo) {
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400503 std::vector<dawn::BindGroupBinding> bindings;
504 GrDawnRingBuffer::Slice geom, frag;
505 uint32_t geometryUniformSize = fDataManager.geometryUniformSize();
506 uint32_t fragmentUniformSize = fDataManager.fragmentUniformSize();
507 if (0 != geometryUniformSize) {
508 geom = gpu->allocateUniformRingBufferSlice(geometryUniformSize);
509 bindings.push_back(make_bind_group_binding(GrDawnUniformHandler::kGeometryBinding,
510 geom.fBuffer, geom.fOffset,
511 geometryUniformSize));
512 }
513 if (0 != fragmentUniformSize) {
514 frag = gpu->allocateUniformRingBufferSlice(fragmentUniformSize);
515 bindings.push_back(make_bind_group_binding(GrDawnUniformHandler::kFragBinding,
516 frag.fBuffer, frag.fOffset,
517 fragmentUniformSize));
518 }
Stephen White729c78d2019-10-14 12:42:59 -0400519 this->setRenderTargetState(renderTarget, programInfo.origin());
520 const GrPipeline& pipeline = programInfo.pipeline();
521 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400522 fGeometryProcessor->setData(fDataManager, primProc,
523 GrFragmentProcessor::CoordTransformIter(pipeline));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400524 int binding = GrDawnUniformHandler::kSamplerBindingBase;
Stephen White729c78d2019-10-14 12:42:59 -0400525 auto primProcTextures = programInfo.hasFixedPrimProcTextures() ?
526 programInfo.fixedPrimProcTextures() : nullptr;
527
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400528 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
529 auto& sampler = primProc.textureSampler(i);
530 setTexture(gpu, sampler.samplerState(), primProcTextures[i]->peekTexture(), &bindings,
531 &binding);
532 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400533 GrFragmentProcessor::Iter iter(pipeline);
534 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
535 const GrFragmentProcessor* fp = iter.next();
536 GrGLSLFragmentProcessor* glslFP = glslIter.next();
537 while (fp && glslFP) {
538 glslFP->setData(fDataManager, *fp);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400539 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
540 auto& s = fp->textureSampler(i);
541 setTexture(gpu, s.samplerState(), s.peekTexture(), &bindings, &binding);
542 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400543 fp = iter.next();
544 glslFP = glslIter.next();
545 }
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400546 SkIPoint offset;
547 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
548 fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset);
549 if (GrTextureProxy* proxy = pipeline.dstTextureProxy()) {
550 GrFragmentProcessor::TextureSampler sampler(sk_ref_sp(proxy));
551 setTexture(gpu, sampler.samplerState(), sampler.peekTexture(), &bindings, &binding);
552 }
Stephen White7fba36b2019-09-10 13:05:22 -0400553 fDataManager.uploadUniformBuffers(gpu, geom, frag);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400554 dawn::BindGroupDescriptor descriptor;
555 descriptor.layout = fBindGroupLayout;
556 descriptor.bindingCount = bindings.size();
557 descriptor.bindings = bindings.data();
558 return gpu->device().CreateBindGroup(&descriptor);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400559}