blob: 8ac8fbd683c4f93a417c0ef2fa91805fccc1c08c [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(
Robert Phillipsa87c5292019-11-12 10:12:42 -0500239 const GrProgramInfo& programInfo,
240 wgpu::TextureFormat depthStencilFormat) {
241 GrStencilSettings stencilSettings = programInfo.nonGLStencilSettings();
242 GrSurfaceOrigin origin = programInfo.origin();
243
Stephen White3cc8d4f2019-10-30 09:56:23 -0400244 wgpu::DepthStencilStateDescriptor state;
Stephen Whitef813ef72019-08-09 12:28:37 -0400245 state.format = depthStencilFormat;
Stephen Whitee2641312019-08-29 15:10:50 -0400246 if (!stencilSettings.isDisabled()) {
Stephen Whitef813ef72019-08-09 12:28:37 -0400247 if (stencilSettings.isTwoSided()) {
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500248 auto front = stencilSettings.postOriginCCWFace(origin);
Stephen White3a62ed42019-11-11 16:56:02 -0500249 auto back = stencilSettings.postOriginCWFace(origin);
Stephen Whitef4b3d6b2019-10-18 07:54:25 -0400250 state.stencilFront = to_stencil_state_face(front);
251 state.stencilBack = to_stencil_state_face(back);
252 state.stencilReadMask = front.fTestMask;
253 state.stencilWriteMask = front.fWriteMask;
Stephen Whitef813ef72019-08-09 12:28:37 -0400254 } else {
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500255 auto frontAndBack = stencilSettings.singleSidedFace();
Stephen Whitef4b3d6b2019-10-18 07:54:25 -0400256 state.stencilBack = state.stencilFront = to_stencil_state_face(frontAndBack);
257 state.stencilReadMask = frontAndBack.fTestMask;
258 state.stencilWriteMask = frontAndBack.fWriteMask;
Stephen Whitef813ef72019-08-09 12:28:37 -0400259 }
260 }
261 return state;
262}
263
Stephen White3cc8d4f2019-10-30 09:56:23 -0400264static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, const wgpu::Buffer& buffer,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400265 uint32_t offset, uint32_t size, const
Stephen White3cc8d4f2019-10-30 09:56:23 -0400266 wgpu::Sampler& sampler,
267 const wgpu::TextureView& textureView) {
268 wgpu::BindGroupBinding result;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400269 result.binding = binding;
270 result.buffer = buffer;
271 result.offset = offset;
272 result.size = size;
273 result.sampler = sampler;
274 result.textureView = textureView;
275 return result;
276}
277
Stephen White3cc8d4f2019-10-30 09:56:23 -0400278static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, const wgpu::Buffer& buffer,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400279 uint32_t offset, uint32_t size) {
280 return make_bind_group_binding(binding, buffer, offset, size, nullptr, nullptr);
281}
282
Stephen White3cc8d4f2019-10-30 09:56:23 -0400283static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding,
284 const wgpu::Sampler& sampler) {
Stephen White170d9902019-08-15 16:48:24 -0400285 return make_bind_group_binding(binding, nullptr, 0, 0, sampler, nullptr);
286}
287
Stephen White3cc8d4f2019-10-30 09:56:23 -0400288static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding,
289 const wgpu::TextureView& textureView) {
Stephen White170d9902019-08-15 16:48:24 -0400290 return make_bind_group_binding(binding, nullptr, 0, 0, nullptr, textureView);
291}
292
Stephen Whitebb6bed12019-08-02 09:57:55 -0400293sk_sp<GrDawnProgram> GrDawnProgramBuilder::Build(GrDawnGpu* gpu,
294 GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400295 const GrProgramInfo& programInfo,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400296 wgpu::TextureFormat colorFormat,
Stephen Whitef813ef72019-08-09 12:28:37 -0400297 bool hasDepthStencil,
Stephen White3cc8d4f2019-10-30 09:56:23 -0400298 wgpu::TextureFormat depthStencilFormat,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400299 GrProgramDesc* desc) {
Stephen White729c78d2019-10-14 12:42:59 -0400300 GrDawnProgramBuilder builder(gpu, renderTarget, programInfo, desc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400301 if (!builder.emitAndInstallProcs()) {
302 return nullptr;
303 }
304
305 builder.fVS.extensions().appendf("#extension GL_ARB_separate_shader_objects : enable\n");
306 builder.fFS.extensions().appendf("#extension GL_ARB_separate_shader_objects : enable\n");
307 builder.fVS.extensions().appendf("#extension GL_ARB_shading_language_420pack : enable\n");
308 builder.fFS.extensions().appendf("#extension GL_ARB_shading_language_420pack : enable\n");
309
310 builder.finalizeShaders();
311
312 SkSL::Program::Inputs vertInputs, fragInputs;
Stephen White20c626a2019-10-15 13:35:37 -0400313 bool flipY = programInfo.origin() != kTopLeft_GrSurfaceOrigin;
314 auto vsModule = builder.createShaderModule(builder.fVS, SkSL::Program::kVertex_Kind, flipY,
Stephen Whitee2641312019-08-29 15:10:50 -0400315 &vertInputs);
Stephen White20c626a2019-10-15 13:35:37 -0400316 auto fsModule = builder.createShaderModule(builder.fFS, SkSL::Program::kFragment_Kind, flipY,
Stephen Whitee2641312019-08-29 15:10:50 -0400317 &fragInputs);
Stephen White40c47e12019-11-01 13:13:03 -0400318 GrDawnUniformHandler::UniformInfoArray& uniforms = builder.fUniformHandler.fUniforms;
319 uint32_t uniformBufferSize = builder.fUniformHandler.fCurrentUBOOffset;
320 sk_sp<GrDawnProgram> result(new GrDawnProgram(uniforms, uniformBufferSize));
Stephen Whitebb6bed12019-08-02 09:57:55 -0400321 result->fGeometryProcessor = std::move(builder.fGeometryProcessor);
322 result->fXferProcessor = std::move(builder.fXferProcessor);
323 result->fFragmentProcessors = std::move(builder.fFragmentProcessors);
324 result->fFragmentProcessorCnt = builder.fFragmentProcessorCnt;
Stephen White7cb52cb2019-11-12 10:40:27 -0500325 std::vector<wgpu::BindGroupLayoutBinding> uniformLayoutBindings;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400326 if (0 != uniformBufferSize) {
Stephen White7cb52cb2019-11-12 10:40:27 -0500327 uniformLayoutBindings.push_back({ GrDawnUniformHandler::kUniformBinding,
328 wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment,
329 wgpu::BindingType::UniformBuffer});
Stephen White170d9902019-08-15 16:48:24 -0400330 }
Stephen White7cb52cb2019-11-12 10:40:27 -0500331 wgpu::BindGroupLayoutDescriptor uniformBindGroupLayoutDesc;
332 uniformBindGroupLayoutDesc.bindingCount = uniformLayoutBindings.size();
333 uniformBindGroupLayoutDesc.bindings = uniformLayoutBindings.data();
334 result->fBindGroupLayouts[0] =
335 gpu->device().CreateBindGroupLayout(&uniformBindGroupLayoutDesc);
336 uint32_t binding = 0;
337 std::vector<wgpu::BindGroupLayoutBinding> textureLayoutBindings;
Stephen White170d9902019-08-15 16:48:24 -0400338 for (int i = 0; i < builder.fUniformHandler.fSamplers.count(); ++i) {
Stephen White7cb52cb2019-11-12 10:40:27 -0500339 textureLayoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment,
340 wgpu::BindingType::Sampler});
341 textureLayoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment,
342 wgpu::BindingType::SampledTexture});
Stephen White170d9902019-08-15 16:48:24 -0400343 }
Stephen White7cb52cb2019-11-12 10:40:27 -0500344 wgpu::BindGroupLayoutDescriptor textureBindGroupLayoutDesc;
345 textureBindGroupLayoutDesc.bindingCount = textureLayoutBindings.size();
346 textureBindGroupLayoutDesc.bindings = textureLayoutBindings.data();
347 result->fBindGroupLayouts[1] =
348 gpu->device().CreateBindGroupLayout(&textureBindGroupLayoutDesc);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400349 wgpu::PipelineLayoutDescriptor pipelineLayoutDesc;
Stephen White7cb52cb2019-11-12 10:40:27 -0500350 pipelineLayoutDesc.bindGroupLayoutCount = 2;
351 pipelineLayoutDesc.bindGroupLayouts = &result->fBindGroupLayouts[0];
Stephen Whitee2641312019-08-29 15:10:50 -0400352 auto pipelineLayout = gpu->device().CreatePipelineLayout(&pipelineLayoutDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400353 result->fBuiltinUniformHandles = builder.fUniformHandles;
Stephen White729c78d2019-10-14 12:42:59 -0400354 const GrPipeline& pipeline = programInfo.pipeline();
Stephen Whitee2641312019-08-29 15:10:50 -0400355 auto colorState = create_color_state(gpu, pipeline, colorFormat);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400356 wgpu::DepthStencilStateDescriptor depthStencilState;
Robert Phillipsa87c5292019-11-12 10:12:42 -0500357
358#ifdef SK_DEBUG
Stephen Whitef813ef72019-08-09 12:28:37 -0400359 if (pipeline.isStencilEnabled()) {
Robert Phillipsa87c5292019-11-12 10:12:42 -0500360 SkASSERT(renderTarget->renderTargetPriv().numStencilBits() == 8);
Stephen Whitef813ef72019-08-09 12:28:37 -0400361 }
Robert Phillipsa87c5292019-11-12 10:12:42 -0500362#endif
363 depthStencilState = create_depth_stencil_state(programInfo, depthStencilFormat);
Stephen Whitee2641312019-08-29 15:10:50 -0400364
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500365 std::vector<wgpu::VertexBufferLayoutDescriptor> inputs;
Stephen Whitee2641312019-08-29 15:10:50 -0400366
Stephen White3cc8d4f2019-10-30 09:56:23 -0400367 std::vector<wgpu::VertexAttributeDescriptor> vertexAttributes;
Stephen White729c78d2019-10-14 12:42:59 -0400368 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Stephen Whitee2641312019-08-29 15:10:50 -0400369 if (primProc.numVertexAttributes() > 0) {
370 size_t offset = 0;
371 int i = 0;
372 for (const auto& attrib : primProc.vertexAttributes()) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400373 wgpu::VertexAttributeDescriptor attribute;
Stephen Whitee2641312019-08-29 15:10:50 -0400374 attribute.shaderLocation = i;
375 attribute.offset = offset;
376 attribute.format = to_dawn_vertex_format(attrib.cpuType());
377 vertexAttributes.push_back(attribute);
378 offset += attrib.sizeAlign4();
379 i++;
380 }
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500381 wgpu::VertexBufferLayoutDescriptor input;
382 input.arrayStride = offset;
Stephen White3cc8d4f2019-10-30 09:56:23 -0400383 input.stepMode = wgpu::InputStepMode::Vertex;
Stephen Whitee2641312019-08-29 15:10:50 -0400384 input.attributeCount = vertexAttributes.size();
385 input.attributes = &vertexAttributes.front();
386 inputs.push_back(input);
387 }
Stephen White3cc8d4f2019-10-30 09:56:23 -0400388 std::vector<wgpu::VertexAttributeDescriptor> instanceAttributes;
Stephen Whitee2641312019-08-29 15:10:50 -0400389 if (primProc.numInstanceAttributes() > 0) {
390 size_t offset = 0;
391 int i = 0;
392 for (const auto& attrib : primProc.instanceAttributes()) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400393 wgpu::VertexAttributeDescriptor attribute;
Stephen Whitee2641312019-08-29 15:10:50 -0400394 attribute.shaderLocation = i;
395 attribute.offset = offset;
396 attribute.format = to_dawn_vertex_format(attrib.cpuType());
397 instanceAttributes.push_back(attribute);
398 offset += attrib.sizeAlign4();
399 i++;
400 }
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500401 wgpu::VertexBufferLayoutDescriptor input;
402 input.arrayStride = offset;
Stephen White3cc8d4f2019-10-30 09:56:23 -0400403 input.stepMode = wgpu::InputStepMode::Instance;
Stephen Whitee2641312019-08-29 15:10:50 -0400404 input.attributeCount = instanceAttributes.size();
405 input.attributes = &instanceAttributes.front();
406 inputs.push_back(input);
407 }
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500408 wgpu::VertexStateDescriptor vertexState;
409 vertexState.indexFormat = wgpu::IndexFormat::Uint16;
410 vertexState.vertexBufferCount = inputs.size();
411 vertexState.vertexBuffers = &inputs.front();
Stephen Whitee2641312019-08-29 15:10:50 -0400412
Stephen White3cc8d4f2019-10-30 09:56:23 -0400413 wgpu::ProgrammableStageDescriptor vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400414 vsDesc.module = vsModule;
415 vsDesc.entryPoint = "main";
416
Stephen White3cc8d4f2019-10-30 09:56:23 -0400417 wgpu::ProgrammableStageDescriptor fsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400418 fsDesc.module = fsModule;
419 fsDesc.entryPoint = "main";
420
Stephen White3cc8d4f2019-10-30 09:56:23 -0400421 wgpu::RenderPipelineDescriptor rpDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400422 rpDesc.layout = pipelineLayout;
Stephen White20c626a2019-10-15 13:35:37 -0400423 rpDesc.vertexStage = vsDesc;
Stephen Whitee2641312019-08-29 15:10:50 -0400424 rpDesc.fragmentStage = &fsDesc;
Sean Gilhuly6c536a52019-11-11 11:13:03 -0500425 rpDesc.vertexState = &vertexState;
Robert Phillipsfcaae482019-11-07 10:17:03 -0500426 rpDesc.primitiveTopology = to_dawn_primitive_topology(programInfo.primitiveType());
Stephen Whitee2641312019-08-29 15:10:50 -0400427 if (hasDepthStencil) {
428 rpDesc.depthStencilState = &depthStencilState;
429 }
430 rpDesc.colorStateCount = 1;
Stephen White20c626a2019-10-15 13:35:37 -0400431 rpDesc.colorStates = &colorState;
Stephen Whitee2641312019-08-29 15:10:50 -0400432 result->fRenderPipeline = gpu->device().CreateRenderPipeline(&rpDesc);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400433 return result;
434}
435
436GrDawnProgramBuilder::GrDawnProgramBuilder(GrDawnGpu* gpu,
437 GrRenderTarget* renderTarget,
Stephen White729c78d2019-10-14 12:42:59 -0400438 const GrProgramInfo& programInfo,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400439 GrProgramDesc* desc)
Stephen White729c78d2019-10-14 12:42:59 -0400440 : INHERITED(renderTarget, programInfo, desc)
Stephen Whitebb6bed12019-08-02 09:57:55 -0400441 , fGpu(gpu)
442 , fVaryingHandler(this)
443 , fUniformHandler(this) {
444}
445
Stephen White3cc8d4f2019-10-30 09:56:23 -0400446wgpu::ShaderModule GrDawnProgramBuilder::createShaderModule(const GrGLSLShaderBuilder& builder,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400447 SkSL::Program::Kind kind,
Stephen White20c626a2019-10-15 13:35:37 -0400448 bool flipY,
Stephen Whitebb6bed12019-08-02 09:57:55 -0400449 SkSL::Program::Inputs* inputs) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400450 wgpu::Device device = fGpu->device();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400451 SkString source(builder.fCompilerString.c_str());
452
453#if 0
454 SkSL::String sksl = GrShaderUtils::PrettyPrint(builder.fCompilerString);
455 printf("converting program:\n%s\n", sksl.c_str());
456#endif
457
Stephen White40c47e12019-11-01 13:13:03 -0400458 SkSL::String spirvSource = sksl_to_spirv(fGpu, source.c_str(), kind, flipY,
459 fUniformHandler.getRTHeightOffset(), inputs);
460 if (inputs->fRTHeight) {
461 this->addRTHeightUniform(SKSL_RTHEIGHT_NAME);
462 }
Stephen Whitebb6bed12019-08-02 09:57:55 -0400463
Stephen White3cc8d4f2019-10-30 09:56:23 -0400464 wgpu::ShaderModuleDescriptor desc;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400465 desc.codeSize = spirvSource.size() / 4;
466 desc.code = reinterpret_cast<const uint32_t*>(spirvSource.c_str());
467
468 return device.CreateShaderModule(&desc);
469};
470
471const GrCaps* GrDawnProgramBuilder::caps() const {
472 return fGpu->caps();
473}
474
475void GrDawnProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) {
476 // Load the RT height uniform if it is needed to y-flip gl_FragCoord.
477 if (fBuiltinUniformHandles.fRTHeightUni.isValid() &&
478 fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) {
479 fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height()));
480 }
481
482 // set RT adjustment
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400483 SkISize dimensions = rt->dimensions();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400484 SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid());
485 if (fRenderTargetState.fRenderTargetOrigin != origin ||
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400486 fRenderTargetState.fRenderTargetSize != dimensions) {
487 fRenderTargetState.fRenderTargetSize = dimensions;
Stephen Whitebb6bed12019-08-02 09:57:55 -0400488 fRenderTargetState.fRenderTargetOrigin = origin;
489
490 float rtAdjustmentVec[4];
491 fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec);
492 fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec);
493 }
494}
495
Stephen White7cb52cb2019-11-12 10:40:27 -0500496static void set_texture(GrDawnGpu* gpu, const GrSamplerState& state, GrTexture* texture,
497 std::vector<wgpu::BindGroupBinding> *bindings, int* binding) {
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400498 // FIXME: could probably cache samplers in GrDawnProgram
Stephen White3cc8d4f2019-10-30 09:56:23 -0400499 wgpu::Sampler sampler = gpu->getOrCreateSampler(state);
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400500 bindings->push_back(make_bind_group_binding((*binding)++, sampler));
501 GrDawnTexture* tex = static_cast<GrDawnTexture*>(texture);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400502 wgpu::TextureView textureView = tex->textureView();
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400503 bindings->push_back(make_bind_group_binding((*binding)++, textureView));
504}
505
Stephen White7cb52cb2019-11-12 10:40:27 -0500506wgpu::BindGroup GrDawnProgram::setUniformData(GrDawnGpu* gpu, const GrRenderTarget* renderTarget,
507 const GrProgramInfo& programInfo) {
Stephen White3cc8d4f2019-10-30 09:56:23 -0400508 std::vector<wgpu::BindGroupBinding> bindings;
Stephen Whitedd78efd2019-10-23 15:00:20 -0400509 GrDawnRingBuffer::Slice slice;
510 uint32_t uniformBufferSize = fDataManager.uniformBufferSize();
511 if (0 != uniformBufferSize) {
512 slice = gpu->allocateUniformRingBufferSlice(uniformBufferSize);
513 bindings.push_back(make_bind_group_binding(GrDawnUniformHandler::kUniformBinding,
514 slice.fBuffer, slice.fOffset,
515 uniformBufferSize));
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400516 }
Stephen White729c78d2019-10-14 12:42:59 -0400517 this->setRenderTargetState(renderTarget, programInfo.origin());
518 const GrPipeline& pipeline = programInfo.pipeline();
519 const GrPrimitiveProcessor& primProc = programInfo.primProc();
Stephen Whitebb6bed12019-08-02 09:57:55 -0400520 fGeometryProcessor->setData(fDataManager, primProc,
521 GrFragmentProcessor::CoordTransformIter(pipeline));
522 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);
528 fp = iter.next();
529 glslFP = glslIter.next();
530 }
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400531 SkIPoint offset;
532 GrTexture* dstTexture = pipeline.peekDstTexture(&offset);
533 fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset);
Stephen Whitedd78efd2019-10-23 15:00:20 -0400534 fDataManager.uploadUniformBuffers(gpu, slice);
Stephen White3cc8d4f2019-10-30 09:56:23 -0400535 wgpu::BindGroupDescriptor descriptor;
Stephen White7cb52cb2019-11-12 10:40:27 -0500536 descriptor.layout = fBindGroupLayouts[0];
537 descriptor.bindingCount = bindings.size();
538 descriptor.bindings = bindings.data();
539 return gpu->device().CreateBindGroup(&descriptor);
540}
541
542wgpu::BindGroup GrDawnProgram::setTextures(GrDawnGpu* gpu,
543 const GrProgramInfo& programInfo,
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500544 const GrSurfaceProxy* const primProcTextures[]) {
Stephen White7cb52cb2019-11-12 10:40:27 -0500545 std::vector<wgpu::BindGroupBinding> bindings;
546 int binding = 0;
547 const GrPipeline& pipeline = programInfo.pipeline();
548 const GrPrimitiveProcessor& primProc = programInfo.primProc();
549 if (primProcTextures) {
550 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
Michael Ludwigfcdd0612019-11-25 08:34:31 -0500551 SkASSERT(primProcTextures[i]->asTextureProxy());
Stephen White7cb52cb2019-11-12 10:40:27 -0500552 auto& sampler = primProc.textureSampler(i);
553 set_texture(gpu, sampler.samplerState(), primProcTextures[i]->peekTexture(), &bindings,
554 &binding);
555 }
556 }
557 GrFragmentProcessor::Iter iter(pipeline);
558 GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt);
559 const GrFragmentProcessor* fp = iter.next();
560 GrGLSLFragmentProcessor* glslFP = glslIter.next();
561 while (fp && glslFP) {
562 for (int i = 0; i < fp->numTextureSamplers(); ++i) {
563 auto& s = fp->textureSampler(i);
564 set_texture(gpu, s.samplerState(), s.peekTexture(), &bindings, &binding);
565 }
566 fp = iter.next();
567 glslFP = glslIter.next();
568 }
569 SkIPoint offset;
570 if (GrTexture* dstTexture = pipeline.peekDstTexture(&offset)) {
571 set_texture(gpu, GrSamplerState::ClampNearest(), dstTexture, &bindings, &binding);
572 }
573 wgpu::BindGroupDescriptor descriptor;
574 descriptor.layout = fBindGroupLayouts[1];
Stephen Whiteb7eaedc2019-08-21 09:48:05 -0400575 descriptor.bindingCount = bindings.size();
576 descriptor.bindings = bindings.data();
577 return gpu->device().CreateBindGroup(&descriptor);
Stephen Whitebb6bed12019-08-02 09:57:55 -0400578}