blob: 8c3a1077961288902104cfdc879a46d133c15c5a [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;
Stephen Whitebb6bed12019-08-02 09:57:55 -040024 std::unique_ptr<SkSL::Program> program = gpu->shaderCompiler()->convertProgram(
25 kind,
26 shaderString,
27 settings);
28 if (!program) {
29 SkDebugf("SkSL error:\n%s\n", gpu->shaderCompiler()->errorText().c_str());
30 SkASSERT(false);
31 return "";
32 }
33 *inputs = program->fInputs;
34 SkSL::String code;
35 if (!gpu->shaderCompiler()->toSPIRV(*program, &code)) {
36 return "";
37 }
38 return code;
39}
40
Stephen White3cc8d4f2019-10-30 09:56:23 -040041static wgpu::BlendFactor to_dawn_blend_factor(GrBlendCoeff coeff) {
Stephen Whitebb6bed12019-08-02 09:57:55 -040042 switch (coeff) {
43 case kZero_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040044 return wgpu::BlendFactor::Zero;
Stephen Whitebb6bed12019-08-02 09:57:55 -040045 case kOne_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040046 return wgpu::BlendFactor::One;
Stephen Whitebb6bed12019-08-02 09:57:55 -040047 case kSC_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040048 return wgpu::BlendFactor::SrcColor;
Stephen Whitebb6bed12019-08-02 09:57:55 -040049 case kISC_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040050 return wgpu::BlendFactor::OneMinusSrcColor;
Stephen Whitebb6bed12019-08-02 09:57:55 -040051 case kDC_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040052 return wgpu::BlendFactor::DstColor;
Stephen Whitebb6bed12019-08-02 09:57:55 -040053 case kIDC_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040054 return wgpu::BlendFactor::OneMinusDstColor;
Stephen Whitebb6bed12019-08-02 09:57:55 -040055 case kSA_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040056 return wgpu::BlendFactor::SrcAlpha;
Stephen Whitebb6bed12019-08-02 09:57:55 -040057 case kISA_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040058 return wgpu::BlendFactor::OneMinusSrcAlpha;
Stephen Whitebb6bed12019-08-02 09:57:55 -040059 case kDA_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040060 return wgpu::BlendFactor::DstAlpha;
Stephen Whitebb6bed12019-08-02 09:57:55 -040061 case kIDA_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040062 return wgpu::BlendFactor::OneMinusDstAlpha;
Stephen Whitebb6bed12019-08-02 09:57:55 -040063 case kConstC_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040064 return wgpu::BlendFactor::BlendColor;
Stephen Whitebb6bed12019-08-02 09:57:55 -040065 case kIConstC_GrBlendCoeff:
Stephen White3cc8d4f2019-10-30 09:56:23 -040066 return wgpu::BlendFactor::OneMinusBlendColor;
Stephen Whitebb6bed12019-08-02 09:57:55 -040067 case kConstA_GrBlendCoeff:
68 case kIConstA_GrBlendCoeff:
69 case kS2C_GrBlendCoeff:
70 case kIS2C_GrBlendCoeff:
71 case kS2A_GrBlendCoeff:
72 case kIS2A_GrBlendCoeff:
73 default:
74 SkASSERT(!"unsupported blend coefficient");
Stephen White3cc8d4f2019-10-30 09:56:23 -040075 return wgpu::BlendFactor::One;
Stephen Whitebb6bed12019-08-02 09:57:55 -040076 }
77}
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:
185 case kHalf3_GrVertexAttribType:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400186 return wgpu::VertexFormat::Float3;
Stephen Whitee2641312019-08-29 15:10:50 -0400187 case kFloat4_GrVertexAttribType:
188 case kHalf4_GrVertexAttribType:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400189 return wgpu::VertexFormat::Float4;
Stephen Whitee2641312019-08-29 15:10:50 -0400190 case kUShort2_GrVertexAttribType:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400191 return wgpu::VertexFormat::UShort2;
Stephen Whitee2641312019-08-29 15:10:50 -0400192 case kInt_GrVertexAttribType:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400193 return wgpu::VertexFormat::Int;
Stephen Whitee2641312019-08-29 15:10:50 -0400194 case kUByte4_norm_GrVertexAttribType:
Stephen White3cc8d4f2019-10-30 09:56:23 -0400195 return wgpu::VertexFormat::UChar4Norm;
Stephen Whitee2641312019-08-29 15:10:50 -0400196 default:
197 SkASSERT(!"unsupported vertex format");
Stephen White3cc8d4f2019-10-30 09:56:23 -0400198 return wgpu::VertexFormat::Float4;
Stephen Whitee2641312019-08-29 15:10:50 -0400199 }
200}
201
Stephen White3cc8d4f2019-10-30 09:56:23 -0400202static wgpu::ColorStateDescriptor create_color_state(const GrDawnGpu* gpu,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400203 const GrPipeline& pipeline,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400204 wgpu::TextureFormat colorFormat) {
Stephen Whitebb6bed12019-08-02 09:57:55 -0400205 GrXferProcessor::BlendInfo blendInfo = pipeline.getXferProcessor().getBlendInfo();
206 GrBlendEquation equation = blendInfo.fEquation;
207 GrBlendCoeff srcCoeff = blendInfo.fSrcBlend;
208 GrBlendCoeff dstCoeff = blendInfo.fDstBlend;
209
Stephen White3cc8d4f2019-10-30 09:56:23 -0400210 wgpu::BlendFactor srcFactor = to_dawn_blend_factor(srcCoeff);
211 wgpu::BlendFactor dstFactor = to_dawn_blend_factor(dstCoeff);
212 wgpu::BlendFactor srcFactorAlpha = to_dawn_blend_factor_for_alpha(srcCoeff);
213 wgpu::BlendFactor dstFactorAlpha = to_dawn_blend_factor_for_alpha(dstCoeff);
214 wgpu::BlendOperation operation = to_dawn_blend_operation(equation);
215 auto mask = blendInfo.fWriteColor ? wgpu::ColorWriteMask::All : wgpu::ColorWriteMask::None;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400216
Stephen White3cc8d4f2019-10-30 09:56:23 -0400217 wgpu::BlendDescriptor colorDesc = {operation, srcFactor, dstFactor};
218 wgpu::BlendDescriptor alphaDesc = {operation, srcFactorAlpha, dstFactorAlpha};
Stephen Whitebb6bed12019-08-02 09:57:55 -0400219
Stephen White3cc8d4f2019-10-30 09:56:23 -0400220 wgpu::ColorStateDescriptor descriptor;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400221 descriptor.format = colorFormat;
222 descriptor.alphaBlend = alphaDesc;
223 descriptor.colorBlend = colorDesc;
224 descriptor.nextInChain = nullptr;
225 descriptor.writeMask = mask;
226
227 return descriptor;
228}
229
Stephen White3cc8d4f2019-10-30 09:56:23 -0400230static wgpu::StencilStateFaceDescriptor to_stencil_state_face(const GrStencilSettings::Face& face) {
231 wgpu::StencilStateFaceDescriptor desc;
Stephen Whitef813ef72019-08-09 12:28:37 -0400232 desc.compare = to_dawn_compare_function(face.fTest);
233 desc.failOp = desc.depthFailOp = to_dawn_stencil_operation(face.fFailOp);
234 desc.passOp = to_dawn_stencil_operation(face.fPassOp);
235 return desc;
236}
237
Stephen White3cc8d4f2019-10-30 09:56:23 -0400238static wgpu::DepthStencilStateDescriptor create_depth_stencil_state(
Stephen Whitef813ef72019-08-09 12:28:37 -0400239 const GrStencilSettings& stencilSettings,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400240 wgpu::TextureFormat depthStencilFormat,
Stephen Whitef813ef72019-08-09 12:28:37 -0400241 GrSurfaceOrigin origin) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400242 wgpu::DepthStencilStateDescriptor state;
Stephen Whitef813ef72019-08-09 12:28:37 -0400243 state.format = depthStencilFormat;
Stephen Whitee2641312019-08-29 15:10:50 -0400244 if (!stencilSettings.isDisabled()) {
Stephen Whitef813ef72019-08-09 12:28:37 -0400245 if (stencilSettings.isTwoSided()) {
Stephen Whitef4b3d6b2019-10-18 07:54:25 -0400246 auto front = stencilSettings.front(origin);
247 auto back = stencilSettings.front(origin);
248 state.stencilFront = to_stencil_state_face(front);
249 state.stencilBack = to_stencil_state_face(back);
250 state.stencilReadMask = front.fTestMask;
251 state.stencilWriteMask = front.fWriteMask;
Stephen Whitef813ef72019-08-09 12:28:37 -0400252 } else {
Stephen Whitef4b3d6b2019-10-18 07:54:25 -0400253 auto frontAndBack = stencilSettings.frontAndBack();
254 state.stencilBack = state.stencilFront = to_stencil_state_face(frontAndBack);
255 state.stencilReadMask = frontAndBack.fTestMask;
256 state.stencilWriteMask = frontAndBack.fWriteMask;
Stephen Whitef813ef72019-08-09 12:28:37 -0400257 }
258 }
259 return state;
260}
261
Stephen White3cc8d4f2019-10-30 09:56:23 -0400262static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, const wgpu::Buffer& buffer,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400263 uint32_t offset, uint32_t size, const
Stephen White3cc8d4f2019-10-30 09:56:23 -0400264 wgpu::Sampler& sampler,
265 const wgpu::TextureView& textureView) {
266 wgpu::BindGroupBinding result;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400267 result.binding = binding;
268 result.buffer = buffer;
269 result.offset = offset;
270 result.size = size;
271 result.sampler = sampler;
272 result.textureView = textureView;
273 return result;
274}
275
Stephen White3cc8d4f2019-10-30 09:56:23 -0400276static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, const wgpu::Buffer& buffer,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400277 uint32_t offset, uint32_t size) {
278 return make_bind_group_binding(binding, buffer, offset, size, nullptr, nullptr);
279}
280
Stephen White3cc8d4f2019-10-30 09:56:23 -0400281static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding,
282 const wgpu::Sampler& sampler) {
Stephen White170d9902019-08-15 16:48:24 -0400283 return make_bind_group_binding(binding, nullptr, 0, 0, sampler, nullptr);
284}
285
Stephen White3cc8d4f2019-10-30 09:56:23 -0400286static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding,
287 const wgpu::TextureView& textureView) {
Stephen White170d9902019-08-15 16:48:24 -0400288 return make_bind_group_binding(binding, nullptr, 0, 0, nullptr, textureView);
289}
290
Stephen Whitebb6bed12019-08-02 09:57:55 -0400291sk_sp<GrDawnProgram> GrDawnProgramBuilder::Build(GrDawnGpu* gpu,
292 GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400293 const GrProgramInfo& programInfo,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400294 wgpu::TextureFormat colorFormat,
Stephen Whitef813ef72019-08-09 12:28:37 -0400295 bool hasDepthStencil,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400296 wgpu::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;
Stephen White20c626a2019-10-15 13:35:37 -0400311 bool flipY = programInfo.origin() != kTopLeft_GrSurfaceOrigin;
312 auto vsModule = builder.createShaderModule(builder.fVS, SkSL::Program::kVertex_Kind, flipY,
Stephen Whitee2641312019-08-29 15:10:50 -0400313 &vertInputs);
Stephen White20c626a2019-10-15 13:35:37 -0400314 auto fsModule = builder.createShaderModule(builder.fFS, SkSL::Program::kFragment_Kind, flipY,
Stephen Whitee2641312019-08-29 15:10:50 -0400315 &fragInputs);
Stephen White40c47e12019-11-01 13:13:03 -0400316 GrDawnUniformHandler::UniformInfoArray& uniforms = builder.fUniformHandler.fUniforms;
317 uint32_t uniformBufferSize = builder.fUniformHandler.fCurrentUBOOffset;
318 sk_sp<GrDawnProgram> result(new GrDawnProgram(uniforms, uniformBufferSize));
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;
Stephen White3cc8d4f2019-10-30 09:56:23 -0400323 std::vector<wgpu::BindGroupLayoutBinding> layoutBindings;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400324 if (0 != uniformBufferSize) {
325 layoutBindings.push_back({ GrDawnUniformHandler::kUniformBinding,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400326 wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment,
327 wgpu::BindingType::UniformBuffer});
Stephen White170d9902019-08-15 16:48:24 -0400328 }
329 uint32_t binding = GrDawnUniformHandler::kSamplerBindingBase;
330 for (int i = 0; i < builder.fUniformHandler.fSamplers.count(); ++i) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400331 layoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment,
332 wgpu::BindingType::Sampler});
333 layoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment,
334 wgpu::BindingType::SampledTexture});
Stephen White170d9902019-08-15 16:48:24 -0400335 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400336 wgpu::BindGroupLayoutDescriptor bindGroupLayoutDesc;
Stephen White170d9902019-08-15 16:48:24 -0400337 bindGroupLayoutDesc.bindingCount = layoutBindings.size();
338 bindGroupLayoutDesc.bindings = layoutBindings.data();
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400339 result->fBindGroupLayout = gpu->device().CreateBindGroupLayout(&bindGroupLayoutDesc);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400340 wgpu::PipelineLayoutDescriptor pipelineLayoutDesc;
Stephen White170d9902019-08-15 16:48:24 -0400341 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);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400347 wgpu::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
Stephen White3cc8d4f2019-10-30 09:56:23 -0400356 std::vector<wgpu::VertexBufferDescriptor> inputs;
Stephen Whitee2641312019-08-29 15:10:50 -0400357
Stephen White3cc8d4f2019-10-30 09:56:23 -0400358 std::vector<wgpu::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()) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400364 wgpu::VertexAttributeDescriptor attribute;
Stephen Whitee2641312019-08-29 15:10:50 -0400365 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 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400372 wgpu::VertexBufferDescriptor input;
Stephen Whitee2641312019-08-29 15:10:50 -0400373 input.stride = offset;
Stephen White3cc8d4f2019-10-30 09:56:23 -0400374 input.stepMode = wgpu::InputStepMode::Vertex;
Stephen Whitee2641312019-08-29 15:10:50 -0400375 input.attributeCount = vertexAttributes.size();
376 input.attributes = &vertexAttributes.front();
377 inputs.push_back(input);
378 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400379 std::vector<wgpu::VertexAttributeDescriptor> instanceAttributes;
Stephen Whitee2641312019-08-29 15:10:50 -0400380 if (primProc.numInstanceAttributes() > 0) {
381 size_t offset = 0;
382 int i = 0;
383 for (const auto& attrib : primProc.instanceAttributes()) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400384 wgpu::VertexAttributeDescriptor attribute;
Stephen Whitee2641312019-08-29 15:10:50 -0400385 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 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400392 wgpu::VertexBufferDescriptor input;
Stephen Whitee2641312019-08-29 15:10:50 -0400393 input.stride = offset;
Stephen White3cc8d4f2019-10-30 09:56:23 -0400394 input.stepMode = wgpu::InputStepMode::Instance;
Stephen Whitee2641312019-08-29 15:10:50 -0400395 input.attributeCount = instanceAttributes.size();
396 input.attributes = &instanceAttributes.front();
397 inputs.push_back(input);
398 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400399 wgpu::VertexInputDescriptor vertexInput;
400 vertexInput.indexFormat = wgpu::IndexFormat::Uint16;
Stephen Whitee2641312019-08-29 15:10:50 -0400401 vertexInput.bufferCount = inputs.size();
402 vertexInput.buffers = &inputs.front();
403
Stephen White3cc8d4f2019-10-30 09:56:23 -0400404 wgpu::ProgrammableStageDescriptor vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400405 vsDesc.module = vsModule;
406 vsDesc.entryPoint = "main";
407
Stephen White3cc8d4f2019-10-30 09:56:23 -0400408 wgpu::ProgrammableStageDescriptor fsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400409 fsDesc.module = fsModule;
410 fsDesc.entryPoint = "main";
411
Stephen White3cc8d4f2019-10-30 09:56:23 -0400412 wgpu::RenderPipelineDescriptor rpDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400413 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;
Robert Phillipsfcaae482019-11-07 10:17:03 -0500417 rpDesc.primitiveTopology = to_dawn_primitive_topology(programInfo.primitiveType());
Stephen Whitee2641312019-08-29 15:10:50 -0400418 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
Stephen White3cc8d4f2019-10-30 09:56:23 -0400437wgpu::ShaderModule GrDawnProgramBuilder::createShaderModule(const GrGLSLShaderBuilder& builder,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400438 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) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400441 wgpu::Device device = fGpu->device();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400442 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 White40c47e12019-11-01 13:13:03 -0400449 SkSL::String spirvSource = sksl_to_spirv(fGpu, source.c_str(), kind, flipY,
450 fUniformHandler.getRTHeightOffset(), inputs);
451 if (inputs->fRTHeight) {
452 this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
453 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400454
Stephen White3cc8d4f2019-10-30 09:56:23 -0400455 wgpu::ShaderModuleDescriptor desc;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400456 desc.codeSize = spirvSource.size() / 4;
457 desc.code = reinterpret_cast<const uint32_t*>(spirvSource.c_str());
458
459 return device.CreateShaderModule(&desc);
460};
461
462const GrCaps* GrDawnProgramBuilder::caps() const {
463 return fGpu->caps();
464}
465
466void GrDawnProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) {
467 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
468 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
469 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
470 fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
471 }
472
473 // set RT adjustment
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400474 SkISize dimensions = rt->dimensions();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400475 SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid());
476 if (fRenderTargetState.fRenderTargetOrigin != origin ||
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400477 fRenderTargetState.fRenderTargetSize != dimensions) {
478 fRenderTargetState.fRenderTargetSize = dimensions;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400479 fRenderTargetState.fRenderTargetOrigin = origin;
480
481 float rtAdjustmentVec[4];
482 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
483 fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
484 }
485}
486
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400487static void setTexture(GrDawnGpu* gpu, const GrSamplerState& state, GrTexture* texture,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400488 std::vector<wgpu::BindGroupBinding> *bindings, int* binding) {
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400489 // FIXME: could probably cache samplers in GrDawnProgram
Stephen White3cc8d4f2019-10-30 09:56:23 -0400490 wgpu::Sampler sampler = gpu->getOrCreateSampler(state);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400491 bindings->push_back(make_bind_group_binding((*binding)++, sampler));
492 GrDawnTexture* tex = static_cast<GrDawnTexture*>(texture);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400493 wgpu::TextureView textureView = tex->textureView();
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400494 bindings->push_back(make_bind_group_binding((*binding)++, textureView));
495}
496
Stephen White3cc8d4f2019-10-30 09:56:23 -0400497wgpu::BindGroup GrDawnProgram::setData(GrDawnGpu* gpu, const GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400498 const GrProgramInfo& programInfo) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400499 std::vector<wgpu::BindGroupBinding> bindings;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400500 GrDawnRingBuffer::Slice slice;
501 uint32_t uniformBufferSize = fDataManager.uniformBufferSize();
502 if (0 != uniformBufferSize) {
503 slice = gpu->allocateUniformRingBufferSlice(uniformBufferSize);
504 bindings.push_back(make_bind_group_binding(GrDawnUniformHandler::kUniformBinding,
505 slice.fBuffer, slice.fOffset,
506 uniformBufferSize));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400507 }
Stephen White729c78d2019-10-14 12:42:59 -0400508 this->setRenderTargetState(renderTarget, programInfo.origin());
509 const GrPipeline& pipeline = programInfo.pipeline();
510 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400511 fGeometryProcessor->setData(fDataManager, primProc,
512 GrFragmentProcessor::CoordTransformIter(pipeline));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400513 int binding = GrDawnUniformHandler::kSamplerBindingBase;
Stephen White729c78d2019-10-14 12:42:59 -0400514 auto primProcTextures = programInfo.hasFixedPrimProcTextures() ?
515 programInfo.fixedPrimProcTextures() : nullptr;
516
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400517 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
518 auto& sampler = primProc.textureSampler(i);
519 setTexture(gpu, sampler.samplerState(), primProcTextures[i]->peekTexture(), &bindings,
520 &binding);
521 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400522 GrFragmentProcessor::Iter iter(pipeline);
523 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
524 const GrFragmentProcessor* fp = iter.next();
525 GrGLSLFragmentProcessor* glslFP = glslIter.next();
526 while (fp && glslFP) {
527 glslFP->setData(fDataManager, *fp);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400528 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
529 auto& s = fp->textureSampler(i);
530 setTexture(gpu, s.samplerState(), s.peekTexture(), &bindings, &binding);
531 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400532 fp = iter.next();
533 glslFP = glslIter.next();
534 }
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400535 SkIPoint offset;
536 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
537 fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset);
Stephen White183fc992019-11-01 13:53:46 -0400538 if (dstTexture) {
539 setTexture(gpu, GrSamplerState::ClampNearest(), dstTexture, &bindings, &binding);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400540 }
Stephen Whitedd78efd2019-10-23 15:00:20 -0400541 fDataManager.uploadUniformBuffers(gpu, slice);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400542 wgpu::BindGroupDescriptor descriptor;
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400543 descriptor.layout = fBindGroupLayout;
544 descriptor.bindingCount = bindings.size();
545 descriptor.bindings = bindings.data();
546 return gpu->device().CreateBindGroup(&descriptor);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400547}