blob: 9415d1a475326d538d525875a77a41bc991f0d31 [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,
18 SkSL::Program::Kind kind, SkSL::Program::Inputs* inputs) {
19 SkSL::Program::Settings settings;
Stephen White170d9902019-08-15 16:48:24 -040020 settings.fCaps = gpu->caps()->shaderCaps();
Stephen Whitebb6bed12019-08-02 09:57:55 -040021 std::unique_ptr<SkSL::Program> program = gpu->shaderCompiler()->convertProgram(
22 kind,
23 shaderString,
24 settings);
25 if (!program) {
26 SkDebugf("SkSL error:\n%s\n", gpu->shaderCompiler()->errorText().c_str());
27 SkASSERT(false);
28 return "";
29 }
30 *inputs = program->fInputs;
31 SkSL::String code;
32 if (!gpu->shaderCompiler()->toSPIRV(*program, &code)) {
33 return "";
34 }
35 return code;
36}
37
38static dawn::BlendFactor to_dawn_blend_factor(GrBlendCoeff coeff) {
39 switch (coeff) {
40 case kZero_GrBlendCoeff:
41 return dawn::BlendFactor::Zero;
42 case kOne_GrBlendCoeff:
43 return dawn::BlendFactor::One;
44 case kSC_GrBlendCoeff:
45 return dawn::BlendFactor::SrcColor;
46 case kISC_GrBlendCoeff:
47 return dawn::BlendFactor::OneMinusSrcColor;
48 case kDC_GrBlendCoeff:
49 return dawn::BlendFactor::DstColor;
50 case kIDC_GrBlendCoeff:
51 return dawn::BlendFactor::OneMinusDstColor;
52 case kSA_GrBlendCoeff:
53 return dawn::BlendFactor::SrcAlpha;
54 case kISA_GrBlendCoeff:
55 return dawn::BlendFactor::OneMinusSrcAlpha;
56 case kDA_GrBlendCoeff:
57 return dawn::BlendFactor::DstAlpha;
58 case kIDA_GrBlendCoeff:
59 return dawn::BlendFactor::OneMinusDstAlpha;
60 case kConstC_GrBlendCoeff:
61 return dawn::BlendFactor::BlendColor;
62 case kIConstC_GrBlendCoeff:
63 return dawn::BlendFactor::OneMinusBlendColor;
64 case kConstA_GrBlendCoeff:
65 case kIConstA_GrBlendCoeff:
66 case kS2C_GrBlendCoeff:
67 case kIS2C_GrBlendCoeff:
68 case kS2A_GrBlendCoeff:
69 case kIS2A_GrBlendCoeff:
70 default:
71 SkASSERT(!"unsupported blend coefficient");
72 return dawn::BlendFactor::One;
73 }
74}
75
Stephen Whitef813ef72019-08-09 12:28:37 -040076static dawn::BlendFactor to_dawn_blend_factor_for_alpha(GrBlendCoeff coeff) {
77 switch (coeff) {
78 // Force all srcColor used in alpha slot to alpha version.
79 case kSC_GrBlendCoeff:
80 return dawn::BlendFactor::SrcAlpha;
81 case kISC_GrBlendCoeff:
82 return dawn::BlendFactor::OneMinusSrcAlpha;
83 case kDC_GrBlendCoeff:
84 return dawn::BlendFactor::DstAlpha;
85 case kIDC_GrBlendCoeff:
86 return dawn::BlendFactor::OneMinusDstAlpha;
87 default:
88 return to_dawn_blend_factor(coeff);
89 }
90}
91
Stephen Whitebb6bed12019-08-02 09:57:55 -040092static dawn::BlendOperation to_dawn_blend_operation(GrBlendEquation equation) {
93 switch (equation) {
94 case kAdd_GrBlendEquation:
95 return dawn::BlendOperation::Add;
96 case kSubtract_GrBlendEquation:
97 return dawn::BlendOperation::Subtract;
Stephen Whitef813ef72019-08-09 12:28:37 -040098 case kReverseSubtract_GrBlendEquation:
99 return dawn::BlendOperation::ReverseSubtract;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400100 default:
101 SkASSERT(!"unsupported blend equation");
102 return dawn::BlendOperation::Add;
103 }
104}
105
Stephen Whitef813ef72019-08-09 12:28:37 -0400106static dawn::CompareFunction to_dawn_compare_function(GrStencilTest test) {
107 switch (test) {
108 case GrStencilTest::kAlways:
109 return dawn::CompareFunction::Always;
110 case GrStencilTest::kNever:
111 return dawn::CompareFunction::Never;
112 case GrStencilTest::kGreater:
113 return dawn::CompareFunction::Greater;
114 case GrStencilTest::kGEqual:
115 return dawn::CompareFunction::GreaterEqual;
116 case GrStencilTest::kLess:
117 return dawn::CompareFunction::Less;
118 case GrStencilTest::kLEqual:
119 return dawn::CompareFunction::LessEqual;
120 case GrStencilTest::kEqual:
121 return dawn::CompareFunction::Equal;
122 case GrStencilTest::kNotEqual:
123 return dawn::CompareFunction::NotEqual;
124 default:
125 SkASSERT(!"unsupported stencil test");
126 return dawn::CompareFunction::Always;
127 }
128}
129
130static dawn::StencilOperation to_dawn_stencil_operation(GrStencilOp op) {
131 switch (op) {
132 case GrStencilOp::kKeep:
133 return dawn::StencilOperation::Keep;
134 case GrStencilOp::kZero:
135 return dawn::StencilOperation::Zero;
136 case GrStencilOp::kReplace:
137 return dawn::StencilOperation::Replace;
138 case GrStencilOp::kInvert:
139 return dawn::StencilOperation::Invert;
140 case GrStencilOp::kIncClamp:
141 return dawn::StencilOperation::IncrementClamp;
142 case GrStencilOp::kDecClamp:
143 return dawn::StencilOperation::DecrementClamp;
144 case GrStencilOp::kIncWrap:
145 return dawn::StencilOperation::IncrementWrap;
146 case GrStencilOp::kDecWrap:
147 return dawn::StencilOperation::DecrementWrap;
148 default:
149 SkASSERT(!"unsupported stencil function");
150 return dawn::StencilOperation::Keep;
151 }
152}
153
Stephen Whitee2641312019-08-29 15:10:50 -0400154static dawn::PrimitiveTopology to_dawn_primitive_topology(GrPrimitiveType primitiveType) {
155 switch (primitiveType) {
156 case GrPrimitiveType::kTriangles:
157 return dawn::PrimitiveTopology::TriangleList;
158 case GrPrimitiveType::kTriangleStrip:
159 return dawn::PrimitiveTopology::TriangleStrip;
160 case GrPrimitiveType::kPoints:
161 return dawn::PrimitiveTopology::PointList;
162 case GrPrimitiveType::kLines:
163 return dawn::PrimitiveTopology::LineList;
164 case GrPrimitiveType::kLineStrip:
165 return dawn::PrimitiveTopology::LineStrip;
Robert Phillips571177f2019-10-04 14:41:49 -0400166 case GrPrimitiveType::kPath:
Stephen Whitee2641312019-08-29 15:10:50 -0400167 default:
168 SkASSERT(!"unsupported primitive topology");
169 return dawn::PrimitiveTopology::TriangleList;
170 }
171}
172
173static dawn::VertexFormat to_dawn_vertex_format(GrVertexAttribType type) {
174 switch (type) {
175 case kFloat_GrVertexAttribType:
176 case kHalf_GrVertexAttribType:
177 return dawn::VertexFormat::Float;
178 case kFloat2_GrVertexAttribType:
179 case kHalf2_GrVertexAttribType:
180 return dawn::VertexFormat::Float2;
181 case kFloat3_GrVertexAttribType:
182 case kHalf3_GrVertexAttribType:
183 return dawn::VertexFormat::Float3;
184 case kFloat4_GrVertexAttribType:
185 case kHalf4_GrVertexAttribType:
186 return dawn::VertexFormat::Float4;
187 case kUShort2_GrVertexAttribType:
188 return dawn::VertexFormat::UShort2;
189 case kInt_GrVertexAttribType:
190 return dawn::VertexFormat::Int;
191 case kUByte4_norm_GrVertexAttribType:
192 return dawn::VertexFormat::UChar4Norm;
193 default:
194 SkASSERT(!"unsupported vertex format");
195 return dawn::VertexFormat::Float4;
196 }
197}
198
Stephen Whitebb6bed12019-08-02 09:57:55 -0400199static dawn::ColorStateDescriptor create_color_state(const GrDawnGpu* gpu,
200 const GrPipeline& pipeline,
201 dawn::TextureFormat colorFormat) {
202 GrXferProcessor::BlendInfo blendInfo = pipeline.getXferProcessor().getBlendInfo();
203 GrBlendEquation equation = blendInfo.fEquation;
204 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
205 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
206
207 dawn::BlendFactor srcFactor = to_dawn_blend_factor(srcCoeff);
208 dawn::BlendFactor dstFactor = to_dawn_blend_factor(dstCoeff);
Stephen Whitef813ef72019-08-09 12:28:37 -0400209 dawn::BlendFactor srcFactorAlpha = to_dawn_blend_factor_for_alpha(srcCoeff);
210 dawn::BlendFactor dstFactorAlpha = to_dawn_blend_factor_for_alpha(dstCoeff);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400211 dawn::BlendOperation operation = to_dawn_blend_operation(equation);
212 auto mask = blendInfo.fWriteColor ? dawn::ColorWriteMask::All : dawn::ColorWriteMask::None;
213
214 dawn::BlendDescriptor colorDesc = {operation, srcFactor, dstFactor};
Stephen Whitef813ef72019-08-09 12:28:37 -0400215 dawn::BlendDescriptor alphaDesc = {operation, srcFactorAlpha, dstFactorAlpha};
Stephen Whitebb6bed12019-08-02 09:57:55 -0400216
217 dawn::ColorStateDescriptor descriptor;
218 descriptor.format = colorFormat;
219 descriptor.alphaBlend = alphaDesc;
220 descriptor.colorBlend = colorDesc;
221 descriptor.nextInChain = nullptr;
222 descriptor.writeMask = mask;
223
224 return descriptor;
225}
226
Stephen Whitef813ef72019-08-09 12:28:37 -0400227static dawn::StencilStateFaceDescriptor to_stencil_state_face(const GrStencilSettings::Face& face) {
228 dawn::StencilStateFaceDescriptor desc;
229 desc.compare = to_dawn_compare_function(face.fTest);
230 desc.failOp = desc.depthFailOp = to_dawn_stencil_operation(face.fFailOp);
231 desc.passOp = to_dawn_stencil_operation(face.fPassOp);
232 return desc;
233}
234
235static dawn::DepthStencilStateDescriptor create_depth_stencil_state(
236 const GrStencilSettings& stencilSettings,
237 dawn::TextureFormat depthStencilFormat,
238 GrSurfaceOrigin origin) {
239 dawn::DepthStencilStateDescriptor state;
240 state.format = depthStencilFormat;
Stephen Whitee2641312019-08-29 15:10:50 -0400241 if (!stencilSettings.isDisabled()) {
Stephen Whitef813ef72019-08-09 12:28:37 -0400242 const GrStencilSettings::Face& front = stencilSettings.front(origin);
243 state.stencilReadMask = front.fTestMask;
244 state.stencilWriteMask = front.fWriteMask;
245 state.stencilFront = to_stencil_state_face(stencilSettings.front(origin));
246 if (stencilSettings.isTwoSided()) {
247 state.stencilBack = to_stencil_state_face(stencilSettings.back(origin));
248 } else {
249 state.stencilBack = state.stencilFront;
250 }
251 }
252 return state;
253}
254
Stephen Whitebb6bed12019-08-02 09:57:55 -0400255static dawn::BindGroupBinding make_bind_group_binding(uint32_t binding, const dawn::Buffer& buffer,
256 uint32_t offset, uint32_t size, const
257 dawn::Sampler& sampler,
258 const dawn::TextureView& textureView) {
259 dawn::BindGroupBinding result;
260 result.binding = binding;
261 result.buffer = buffer;
262 result.offset = offset;
263 result.size = size;
264 result.sampler = sampler;
265 result.textureView = textureView;
266 return result;
267}
268
269static dawn::BindGroupBinding make_bind_group_binding(uint32_t binding, const dawn::Buffer& buffer,
270 uint32_t offset, uint32_t size) {
271 return make_bind_group_binding(binding, buffer, offset, size, nullptr, nullptr);
272}
273
Stephen White170d9902019-08-15 16:48:24 -0400274static dawn::BindGroupBinding make_bind_group_binding(uint32_t binding,
275 const dawn::Sampler& sampler) {
276 return make_bind_group_binding(binding, nullptr, 0, 0, sampler, nullptr);
277}
278
279static dawn::BindGroupBinding make_bind_group_binding(uint32_t binding,
280 const dawn::TextureView& textureView) {
281 return make_bind_group_binding(binding, nullptr, 0, 0, nullptr, textureView);
282}
283
Stephen Whitebb6bed12019-08-02 09:57:55 -0400284sk_sp<GrDawnProgram> GrDawnProgramBuilder::Build(GrDawnGpu* gpu,
285 GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400286 const GrProgramInfo& programInfo,
Stephen Whitee2641312019-08-29 15:10:50 -0400287 GrPrimitiveType primitiveType,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400288 dawn::TextureFormat colorFormat,
Stephen Whitef813ef72019-08-09 12:28:37 -0400289 bool hasDepthStencil,
290 dawn::TextureFormat depthStencilFormat,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400291 GrProgramDesc* desc) {
Stephen White729c78d2019-10-14 12:42:59 -0400292 GrDawnProgramBuilder builder(gpu, renderTarget, programInfo, desc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400293 if (!builder.emitAndInstallProcs()) {
294 return nullptr;
295 }
296
297 builder.fVS.extensions().appendf("#extension GL_ARB_separate_shader_objects : enable\n");
298 builder.fFS.extensions().appendf("#extension GL_ARB_separate_shader_objects : enable\n");
299 builder.fVS.extensions().appendf("#extension GL_ARB_shading_language_420pack : enable\n");
300 builder.fFS.extensions().appendf("#extension GL_ARB_shading_language_420pack : enable\n");
301
302 builder.finalizeShaders();
303
304 SkSL::Program::Inputs vertInputs, fragInputs;
305 GrDawnUniformHandler::UniformInfoArray& uniforms = builder.fUniformHandler.fUniforms;
306 uint32_t geometryUniformSize = builder.fUniformHandler.fCurrentGeometryUBOOffset;
307 uint32_t fragmentUniformSize = builder.fUniformHandler.fCurrentFragmentUBOOffset;
308 sk_sp<GrDawnProgram> result(
309 new GrDawnProgram(uniforms, geometryUniformSize, fragmentUniformSize));
Stephen Whitee2641312019-08-29 15:10:50 -0400310 auto vsModule = builder.createShaderModule(builder.fVS, SkSL::Program::kVertex_Kind,
311 &vertInputs);
312 auto fsModule = builder.createShaderModule(builder.fFS, SkSL::Program::kFragment_Kind,
313 &fragInputs);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400314 result->fGeometryProcessor = std::move(builder.fGeometryProcessor);
315 result->fXferProcessor = std::move(builder.fXferProcessor);
316 result->fFragmentProcessors = std::move(builder.fFragmentProcessors);
317 result->fFragmentProcessorCnt = builder.fFragmentProcessorCnt;
318 std::vector<dawn::BindGroupLayoutBinding> layoutBindings;
Stephen White170d9902019-08-15 16:48:24 -0400319 if (0 != geometryUniformSize) {
320 layoutBindings.push_back({ GrDawnUniformHandler::kGeometryBinding,
321 dawn::ShaderStageBit::Vertex,
322 dawn::BindingType::UniformBuffer});
323 }
324 if (0 != fragmentUniformSize) {
325 layoutBindings.push_back({ GrDawnUniformHandler::kFragBinding,
326 dawn::ShaderStageBit::Fragment,
327 dawn::BindingType::UniformBuffer});
328 }
329 uint32_t binding = GrDawnUniformHandler::kSamplerBindingBase;
330 for (int i = 0; i < builder.fUniformHandler.fSamplers.count(); ++i) {
331 layoutBindings.push_back({ binding++, dawn::ShaderStageBit::Fragment,
332 dawn::BindingType::Sampler});
333 layoutBindings.push_back({ binding++, dawn::ShaderStageBit::Fragment,
334 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
404 dawn::PipelineStageDescriptor vsDesc;
405 vsDesc.module = vsModule;
406 vsDesc.entryPoint = "main";
407
408 dawn::PipelineStageDescriptor fsDesc;
409 fsDesc.module = fsModule;
410 fsDesc.entryPoint = "main";
411
412 dawn::RenderPipelineDescriptor rpDesc;
413 rpDesc.layout = pipelineLayout;
414 rpDesc.vertexStage = &vsDesc;
415 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;
422 dawn::ColorStateDescriptor* colorStatesPtr[] = { &colorState };
423 rpDesc.colorStates = colorStatesPtr;
424 result->fRenderPipeline = gpu->device().CreateRenderPipeline(&rpDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400425 return result;
426}
427
428GrDawnProgramBuilder::GrDawnProgramBuilder(GrDawnGpu* gpu,
429 GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400430 const GrProgramInfo& programInfo,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400431 GrProgramDesc* desc)
Stephen White729c78d2019-10-14 12:42:59 -0400432 : INHERITED(renderTarget, programInfo, desc)
Stephen Whitebb6bed12019-08-02 09:57:55 -0400433 , fGpu(gpu)
434 , fVaryingHandler(this)
435 , fUniformHandler(this) {
436}
437
438dawn::ShaderModule GrDawnProgramBuilder::createShaderModule(const GrGLSLShaderBuilder& builder,
439 SkSL::Program::Kind kind,
440 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
449 SkSL::String spirvSource = sksl_to_spirv(fGpu, source.c_str(), kind, inputs);
450
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
470 SkISize size;
471 size.set(rt->width(), rt->height());
472 SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid());
473 if (fRenderTargetState.fRenderTargetOrigin != origin ||
474 fRenderTargetState.fRenderTargetSize != size) {
475 fRenderTargetState.fRenderTargetSize = size;
476 fRenderTargetState.fRenderTargetOrigin = origin;
477
478 float rtAdjustmentVec[4];
479 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
480 fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
481 }
482}
483
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400484static void setTexture(GrDawnGpu* gpu, const GrSamplerState& state, GrTexture* texture,
485 std::vector<dawn::BindGroupBinding> *bindings, int* binding) {
486 // FIXME: could probably cache samplers in GrDawnProgram
Stephen Whitec0c05042019-09-10 16:15:23 -0400487 dawn::Sampler sampler = gpu->getOrCreateSampler(state);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400488 bindings->push_back(make_bind_group_binding((*binding)++, sampler));
489 GrDawnTexture* tex = static_cast<GrDawnTexture*>(texture);
490 dawn::TextureView textureView = tex->textureView();
491 bindings->push_back(make_bind_group_binding((*binding)++, textureView));
492}
493
494dawn::BindGroup GrDawnProgram::setData(GrDawnGpu* gpu, const GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400495 const GrProgramInfo& programInfo) {
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400496 std::vector<dawn::BindGroupBinding> bindings;
497 GrDawnRingBuffer::Slice geom, frag;
498 uint32_t geometryUniformSize = fDataManager.geometryUniformSize();
499 uint32_t fragmentUniformSize = fDataManager.fragmentUniformSize();
500 if (0 != geometryUniformSize) {
501 geom = gpu->allocateUniformRingBufferSlice(geometryUniformSize);
502 bindings.push_back(make_bind_group_binding(GrDawnUniformHandler::kGeometryBinding,
503 geom.fBuffer, geom.fOffset,
504 geometryUniformSize));
505 }
506 if (0 != fragmentUniformSize) {
507 frag = gpu->allocateUniformRingBufferSlice(fragmentUniformSize);
508 bindings.push_back(make_bind_group_binding(GrDawnUniformHandler::kFragBinding,
509 frag.fBuffer, frag.fOffset,
510 fragmentUniformSize));
511 }
Stephen White729c78d2019-10-14 12:42:59 -0400512 this->setRenderTargetState(renderTarget, programInfo.origin());
513 const GrPipeline& pipeline = programInfo.pipeline();
514 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400515 fGeometryProcessor->setData(fDataManager, primProc,
516 GrFragmentProcessor::CoordTransformIter(pipeline));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400517 int binding = GrDawnUniformHandler::kSamplerBindingBase;
Stephen White729c78d2019-10-14 12:42:59 -0400518 auto primProcTextures = programInfo.hasFixedPrimProcTextures() ?
519 programInfo.fixedPrimProcTextures() : nullptr;
520
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400521 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
522 auto& sampler = primProc.textureSampler(i);
523 setTexture(gpu, sampler.samplerState(), primProcTextures[i]->peekTexture(), &bindings,
524 &binding);
525 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400526 GrFragmentProcessor::Iter iter(pipeline);
527 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
528 const GrFragmentProcessor* fp = iter.next();
529 GrGLSLFragmentProcessor* glslFP = glslIter.next();
530 while (fp && glslFP) {
531 glslFP->setData(fDataManager, *fp);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400532 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
533 auto& s = fp->textureSampler(i);
534 setTexture(gpu, s.samplerState(), s.peekTexture(), &bindings, &binding);
535 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400536 fp = iter.next();
537 glslFP = glslIter.next();
538 }
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400539 SkIPoint offset;
540 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
541 fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset);
542 if (GrTextureProxy* proxy = pipeline.dstTextureProxy()) {
543 GrFragmentProcessor::TextureSampler sampler(sk_ref_sp(proxy));
544 setTexture(gpu, sampler.samplerState(), sampler.peekTexture(), &bindings, &binding);
545 }
Stephen White7fba36b2019-09-10 13:05:22 -0400546 fDataManager.uploadUniformBuffers(gpu, geom, frag);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400547 dawn::BindGroupDescriptor descriptor;
548 descriptor.layout = fBindGroupLayout;
549 descriptor.bindingCount = bindings.size();
550 descriptor.bindings = bindings.data();
551 return gpu->device().CreateBindGroup(&descriptor);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400552}