blob: e63cd9a967af4ebf279d16c2a251d641863941fd [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 Whitee2641312019-08-29 15:10:50 -0400294 GrPrimitiveType primitiveType,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400295 wgpu::TextureFormat colorFormat,
Stephen Whitef813ef72019-08-09 12:28:37 -0400296 bool hasDepthStencil,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400297 wgpu::TextureFormat depthStencilFormat,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400298 GrProgramDesc* desc) {
Stephen White729c78d2019-10-14 12:42:59 -0400299 GrDawnProgramBuilder builder(gpu, renderTarget, programInfo, desc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400300 if (!builder.emitAndInstallProcs()) {
301 return nullptr;
302 }
303
304 builder.fVS.extensions().appendf("#extension GL_ARB_separate_shader_objects : enable\n");
305 builder.fFS.extensions().appendf("#extension GL_ARB_separate_shader_objects : enable\n");
306 builder.fVS.extensions().appendf("#extension GL_ARB_shading_language_420pack : enable\n");
307 builder.fFS.extensions().appendf("#extension GL_ARB_shading_language_420pack : enable\n");
308
309 builder.finalizeShaders();
310
311 SkSL::Program::Inputs vertInputs, fragInputs;
Stephen White20c626a2019-10-15 13:35:37 -0400312 bool flipY = programInfo.origin() != kTopLeft_GrSurfaceOrigin;
313 auto vsModule = builder.createShaderModule(builder.fVS, SkSL::Program::kVertex_Kind, flipY,
Stephen Whitee2641312019-08-29 15:10:50 -0400314 &vertInputs);
Stephen White20c626a2019-10-15 13:35:37 -0400315 auto fsModule = builder.createShaderModule(builder.fFS, SkSL::Program::kFragment_Kind, flipY,
Stephen Whitee2641312019-08-29 15:10:50 -0400316 &fragInputs);
Stephen White40c47e12019-11-01 13:13:03 -0400317 GrDawnUniformHandler::UniformInfoArray& uniforms = builder.fUniformHandler.fUniforms;
318 uint32_t uniformBufferSize = builder.fUniformHandler.fCurrentUBOOffset;
319 sk_sp<GrDawnProgram> result(new GrDawnProgram(uniforms, uniformBufferSize));
Stephen Whitebb6bed12019-08-02 09:57:55 -0400320 result->fGeometryProcessor = std::move(builder.fGeometryProcessor);
321 result->fXferProcessor = std::move(builder.fXferProcessor);
322 result->fFragmentProcessors = std::move(builder.fFragmentProcessors);
323 result->fFragmentProcessorCnt = builder.fFragmentProcessorCnt;
Stephen White3cc8d4f2019-10-30 09:56:23 -0400324 std::vector<wgpu::BindGroupLayoutBinding> layoutBindings;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400325 if (0 != uniformBufferSize) {
326 layoutBindings.push_back({ GrDawnUniformHandler::kUniformBinding,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400327 wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment,
328 wgpu::BindingType::UniformBuffer});
Stephen White170d9902019-08-15 16:48:24 -0400329 }
330 uint32_t binding = GrDawnUniformHandler::kSamplerBindingBase;
331 for (int i = 0; i < builder.fUniformHandler.fSamplers.count(); ++i) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400332 layoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment,
333 wgpu::BindingType::Sampler});
334 layoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment,
335 wgpu::BindingType::SampledTexture});
Stephen White170d9902019-08-15 16:48:24 -0400336 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400337 wgpu::BindGroupLayoutDescriptor bindGroupLayoutDesc;
Stephen White170d9902019-08-15 16:48:24 -0400338 bindGroupLayoutDesc.bindingCount = layoutBindings.size();
339 bindGroupLayoutDesc.bindings = layoutBindings.data();
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400340 result->fBindGroupLayout = gpu->device().CreateBindGroupLayout(&bindGroupLayoutDesc);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400341 wgpu::PipelineLayoutDescriptor pipelineLayoutDesc;
Stephen White170d9902019-08-15 16:48:24 -0400342 pipelineLayoutDesc.bindGroupLayoutCount = 1;
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400343 pipelineLayoutDesc.bindGroupLayouts = &result->fBindGroupLayout;
Stephen Whitee2641312019-08-29 15:10:50 -0400344 auto pipelineLayout = gpu->device().CreatePipelineLayout(&pipelineLayoutDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400345 result->fBuiltinUniformHandles = builder.fUniformHandles;
Stephen White729c78d2019-10-14 12:42:59 -0400346 const GrPipeline& pipeline = programInfo.pipeline();
Stephen Whitee2641312019-08-29 15:10:50 -0400347 auto colorState = create_color_state(gpu, pipeline, colorFormat);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400348 wgpu::DepthStencilStateDescriptor depthStencilState;
Stephen Whitef813ef72019-08-09 12:28:37 -0400349 GrStencilSettings stencil;
350 if (pipeline.isStencilEnabled()) {
351 int numStencilBits = renderTarget->renderTargetPriv().numStencilBits();
352 stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(), numStencilBits);
353 }
Stephen White729c78d2019-10-14 12:42:59 -0400354 depthStencilState = create_depth_stencil_state(stencil, depthStencilFormat,
355 programInfo.origin());
Stephen Whitee2641312019-08-29 15:10:50 -0400356
Stephen White3cc8d4f2019-10-30 09:56:23 -0400357 std::vector<wgpu::VertexBufferDescriptor> inputs;
Stephen Whitee2641312019-08-29 15:10:50 -0400358
Stephen White3cc8d4f2019-10-30 09:56:23 -0400359 std::vector<wgpu::VertexAttributeDescriptor> vertexAttributes;
Stephen White729c78d2019-10-14 12:42:59 -0400360 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Stephen Whitee2641312019-08-29 15:10:50 -0400361 if (primProc.numVertexAttributes() > 0) {
362 size_t offset = 0;
363 int i = 0;
364 for (const auto& attrib : primProc.vertexAttributes()) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400365 wgpu::VertexAttributeDescriptor attribute;
Stephen Whitee2641312019-08-29 15:10:50 -0400366 attribute.shaderLocation = i;
367 attribute.offset = offset;
368 attribute.format = to_dawn_vertex_format(attrib.cpuType());
369 vertexAttributes.push_back(attribute);
370 offset += attrib.sizeAlign4();
371 i++;
372 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400373 wgpu::VertexBufferDescriptor input;
Stephen Whitee2641312019-08-29 15:10:50 -0400374 input.stride = offset;
Stephen White3cc8d4f2019-10-30 09:56:23 -0400375 input.stepMode = wgpu::InputStepMode::Vertex;
Stephen Whitee2641312019-08-29 15:10:50 -0400376 input.attributeCount = vertexAttributes.size();
377 input.attributes = &vertexAttributes.front();
378 inputs.push_back(input);
379 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400380 std::vector<wgpu::VertexAttributeDescriptor> instanceAttributes;
Stephen Whitee2641312019-08-29 15:10:50 -0400381 if (primProc.numInstanceAttributes() > 0) {
382 size_t offset = 0;
383 int i = 0;
384 for (const auto& attrib : primProc.instanceAttributes()) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400385 wgpu::VertexAttributeDescriptor attribute;
Stephen Whitee2641312019-08-29 15:10:50 -0400386 attribute.shaderLocation = i;
387 attribute.offset = offset;
388 attribute.format = to_dawn_vertex_format(attrib.cpuType());
389 instanceAttributes.push_back(attribute);
390 offset += attrib.sizeAlign4();
391 i++;
392 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400393 wgpu::VertexBufferDescriptor input;
Stephen Whitee2641312019-08-29 15:10:50 -0400394 input.stride = offset;
Stephen White3cc8d4f2019-10-30 09:56:23 -0400395 input.stepMode = wgpu::InputStepMode::Instance;
Stephen Whitee2641312019-08-29 15:10:50 -0400396 input.attributeCount = instanceAttributes.size();
397 input.attributes = &instanceAttributes.front();
398 inputs.push_back(input);
399 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400400 wgpu::VertexInputDescriptor vertexInput;
401 vertexInput.indexFormat = wgpu::IndexFormat::Uint16;
Stephen Whitee2641312019-08-29 15:10:50 -0400402 vertexInput.bufferCount = inputs.size();
403 vertexInput.buffers = &inputs.front();
404
Stephen White3cc8d4f2019-10-30 09:56:23 -0400405 wgpu::ProgrammableStageDescriptor vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400406 vsDesc.module = vsModule;
407 vsDesc.entryPoint = "main";
408
Stephen White3cc8d4f2019-10-30 09:56:23 -0400409 wgpu::ProgrammableStageDescriptor fsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400410 fsDesc.module = fsModule;
411 fsDesc.entryPoint = "main";
412
Stephen White3cc8d4f2019-10-30 09:56:23 -0400413 wgpu::RenderPipelineDescriptor rpDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400414 rpDesc.layout = pipelineLayout;
Stephen White20c626a2019-10-15 13:35:37 -0400415 rpDesc.vertexStage = vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400416 rpDesc.fragmentStage = &fsDesc;
417 rpDesc.vertexInput = &vertexInput;
418 rpDesc.primitiveTopology = to_dawn_primitive_topology(primitiveType);
419 if (hasDepthStencil) {
420 rpDesc.depthStencilState = &depthStencilState;
421 }
422 rpDesc.colorStateCount = 1;
Stephen White20c626a2019-10-15 13:35:37 -0400423 rpDesc.colorStates = &colorState;
Stephen Whitee2641312019-08-29 15:10:50 -0400424 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
Stephen White3cc8d4f2019-10-30 09:56:23 -0400438wgpu::ShaderModule GrDawnProgramBuilder::createShaderModule(const GrGLSLShaderBuilder& builder,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400439 SkSL::Program::Kind kind,
Stephen White20c626a2019-10-15 13:35:37 -0400440 bool flipY,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400441 SkSL::Program::Inputs* inputs) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400442 wgpu::Device device = fGpu->device();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400443 SkString source(builder.fCompilerString.c_str());
444
445#if 0
446 SkSL::String sksl = GrShaderUtils::PrettyPrint(builder.fCompilerString);
447 printf("converting program:\n%s\n", sksl.c_str());
448#endif
449
Stephen White40c47e12019-11-01 13:13:03 -0400450 SkSL::String spirvSource = sksl_to_spirv(fGpu, source.c_str(), kind, flipY,
451 fUniformHandler.getRTHeightOffset(), inputs);
452 if (inputs->fRTHeight) {
453 this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
454 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400455
Stephen White3cc8d4f2019-10-30 09:56:23 -0400456 wgpu::ShaderModuleDescriptor desc;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400457 desc.codeSize = spirvSource.size() / 4;
458 desc.code = reinterpret_cast<const uint32_t*>(spirvSource.c_str());
459
460 return device.CreateShaderModule(&desc);
461};
462
463const GrCaps* GrDawnProgramBuilder::caps() const {
464 return fGpu->caps();
465}
466
467void GrDawnProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) {
468 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
469 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
470 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
471 fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
472 }
473
474 // set RT adjustment
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400475 SkISize dimensions = rt->dimensions();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400476 SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid());
477 if (fRenderTargetState.fRenderTargetOrigin != origin ||
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400478 fRenderTargetState.fRenderTargetSize != dimensions) {
479 fRenderTargetState.fRenderTargetSize = dimensions;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400480 fRenderTargetState.fRenderTargetOrigin = origin;
481
482 float rtAdjustmentVec[4];
483 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
484 fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
485 }
486}
487
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400488static void setTexture(GrDawnGpu* gpu, const GrSamplerState& state, GrTexture* texture,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400489 std::vector<wgpu::BindGroupBinding> *bindings, int* binding) {
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400490 // FIXME: could probably cache samplers in GrDawnProgram
Stephen White3cc8d4f2019-10-30 09:56:23 -0400491 wgpu::Sampler sampler = gpu->getOrCreateSampler(state);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400492 bindings->push_back(make_bind_group_binding((*binding)++, sampler));
493 GrDawnTexture* tex = static_cast<GrDawnTexture*>(texture);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400494 wgpu::TextureView textureView = tex->textureView();
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400495 bindings->push_back(make_bind_group_binding((*binding)++, textureView));
496}
497
Stephen White3cc8d4f2019-10-30 09:56:23 -0400498wgpu::BindGroup GrDawnProgram::setData(GrDawnGpu* gpu, const GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400499 const GrProgramInfo& programInfo) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400500 std::vector<wgpu::BindGroupBinding> bindings;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400501 GrDawnRingBuffer::Slice slice;
502 uint32_t uniformBufferSize = fDataManager.uniformBufferSize();
503 if (0 != uniformBufferSize) {
504 slice = gpu->allocateUniformRingBufferSlice(uniformBufferSize);
505 bindings.push_back(make_bind_group_binding(GrDawnUniformHandler::kUniformBinding,
506 slice.fBuffer, slice.fOffset,
507 uniformBufferSize));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400508 }
Stephen White729c78d2019-10-14 12:42:59 -0400509 this->setRenderTargetState(renderTarget, programInfo.origin());
510 const GrPipeline& pipeline = programInfo.pipeline();
511 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400512 fGeometryProcessor->setData(fDataManager, primProc,
513 GrFragmentProcessor::CoordTransformIter(pipeline));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400514 int binding = GrDawnUniformHandler::kSamplerBindingBase;
Stephen White729c78d2019-10-14 12:42:59 -0400515 auto primProcTextures = programInfo.hasFixedPrimProcTextures() ?
516 programInfo.fixedPrimProcTextures() : nullptr;
517
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400518 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
519 auto& sampler = primProc.textureSampler(i);
520 setTexture(gpu, sampler.samplerState(), primProcTextures[i]->peekTexture(), &bindings,
521 &binding);
522 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400523 GrFragmentProcessor::Iter iter(pipeline);
524 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
525 const GrFragmentProcessor* fp = iter.next();
526 GrGLSLFragmentProcessor* glslFP = glslIter.next();
527 while (fp && glslFP) {
528 glslFP->setData(fDataManager, *fp);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400529 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
530 auto& s = fp->textureSampler(i);
531 setTexture(gpu, s.samplerState(), s.peekTexture(), &bindings, &binding);
532 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400533 fp = iter.next();
534 glslFP = glslIter.next();
535 }
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400536 SkIPoint offset;
537 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
538 fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset);
Stephen White183fc992019-11-01 13:53:46 -0400539 if (dstTexture) {
540 setTexture(gpu, GrSamplerState::ClampNearest(), dstTexture, &bindings, &binding);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400541 }
Stephen Whitedd78efd2019-10-23 15:00:20 -0400542 fDataManager.uploadUniformBuffers(gpu, slice);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400543 wgpu::BindGroupDescriptor descriptor;
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400544 descriptor.layout = fBindGroupLayout;
545 descriptor.bindingCount = bindings.size();
546 descriptor.bindings = bindings.data();
547 return gpu->device().CreateBindGroup(&descriptor);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400548}