Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 1 | /* |
| 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 Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 10 | #include "src/gpu/GrRenderTarget.h" |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrShaderUtils.h" |
| 12 | #include "src/gpu/GrStencilSettings.h" |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 13 | #include "src/gpu/dawn/GrDawnGpu.h" |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 14 | #include "src/gpu/dawn/GrDawnTexture.h" |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 15 | #include "src/sksl/SkSLCompiler.h" |
| 16 | |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 17 | static SkSL::String sksl_to_spirv(const GrDawnGpu* gpu, const char* shaderString, |
Stephen White | 40c47e1 | 2019-11-01 13:13:03 -0400 | [diff] [blame] | 18 | SkSL::Program::Kind kind, bool flipY, uint32_t rtHeightOffset, |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 19 | SkSL::Program::Inputs* inputs) { |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 20 | SkSL::Program::Settings settings; |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 21 | settings.fCaps = gpu->caps()->shaderCaps(); |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 22 | settings.fFlipY = flipY; |
Stephen White | 40c47e1 | 2019-11-01 13:13:03 -0400 | [diff] [blame] | 23 | settings.fRTHeightOffset = rtHeightOffset; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 24 | 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 White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 41 | static wgpu::BlendFactor to_dawn_blend_factor(GrBlendCoeff coeff) { |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 42 | switch (coeff) { |
Greg Daniel | 6e2af5c | 2020-03-30 15:07:05 -0400 | [diff] [blame] | 43 | case kZero_GrBlendCoeff: |
| 44 | return wgpu::BlendFactor::Zero; |
| 45 | case kOne_GrBlendCoeff: |
| 46 | return wgpu::BlendFactor::One; |
| 47 | case kSC_GrBlendCoeff: |
| 48 | return wgpu::BlendFactor::SrcColor; |
| 49 | case kISC_GrBlendCoeff: |
| 50 | return wgpu::BlendFactor::OneMinusSrcColor; |
| 51 | case kDC_GrBlendCoeff: |
| 52 | return wgpu::BlendFactor::DstColor; |
| 53 | case kIDC_GrBlendCoeff: |
| 54 | return wgpu::BlendFactor::OneMinusDstColor; |
| 55 | case kSA_GrBlendCoeff: |
| 56 | return wgpu::BlendFactor::SrcAlpha; |
| 57 | case kISA_GrBlendCoeff: |
| 58 | return wgpu::BlendFactor::OneMinusSrcAlpha; |
| 59 | case kDA_GrBlendCoeff: |
| 60 | return wgpu::BlendFactor::DstAlpha; |
| 61 | case kIDA_GrBlendCoeff: |
| 62 | return wgpu::BlendFactor::OneMinusDstAlpha; |
| 63 | case kConstC_GrBlendCoeff: |
| 64 | return wgpu::BlendFactor::BlendColor; |
| 65 | case kIConstC_GrBlendCoeff: |
| 66 | return wgpu::BlendFactor::OneMinusBlendColor; |
| 67 | case kS2C_GrBlendCoeff: |
| 68 | case kIS2C_GrBlendCoeff: |
| 69 | case kS2A_GrBlendCoeff: |
| 70 | case kIS2A_GrBlendCoeff: |
| 71 | default: |
| 72 | SkASSERT(!"unsupported blend coefficient"); |
| 73 | return wgpu::BlendFactor::One; |
| 74 | } |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 77 | static wgpu::BlendFactor to_dawn_blend_factor_for_alpha(GrBlendCoeff coeff) { |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 78 | switch (coeff) { |
| 79 | // Force all srcColor used in alpha slot to alpha version. |
| 80 | case kSC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 81 | return wgpu::BlendFactor::SrcAlpha; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 82 | case kISC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 83 | return wgpu::BlendFactor::OneMinusSrcAlpha; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 84 | case kDC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 85 | return wgpu::BlendFactor::DstAlpha; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 86 | case kIDC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 87 | return wgpu::BlendFactor::OneMinusDstAlpha; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 88 | default: |
| 89 | return to_dawn_blend_factor(coeff); |
| 90 | } |
| 91 | } |
| 92 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 93 | static wgpu::BlendOperation to_dawn_blend_operation(GrBlendEquation equation) { |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 94 | switch (equation) { |
| 95 | case kAdd_GrBlendEquation: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 96 | return wgpu::BlendOperation::Add; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 97 | case kSubtract_GrBlendEquation: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 98 | return wgpu::BlendOperation::Subtract; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 99 | case kReverseSubtract_GrBlendEquation: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 100 | return wgpu::BlendOperation::ReverseSubtract; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 101 | default: |
| 102 | SkASSERT(!"unsupported blend equation"); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 103 | return wgpu::BlendOperation::Add; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 107 | static wgpu::CompareFunction to_dawn_compare_function(GrStencilTest test) { |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 108 | switch (test) { |
| 109 | case GrStencilTest::kAlways: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 110 | return wgpu::CompareFunction::Always; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 111 | case GrStencilTest::kNever: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 112 | return wgpu::CompareFunction::Never; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 113 | case GrStencilTest::kGreater: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 114 | return wgpu::CompareFunction::Greater; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 115 | case GrStencilTest::kGEqual: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 116 | return wgpu::CompareFunction::GreaterEqual; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 117 | case GrStencilTest::kLess: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 118 | return wgpu::CompareFunction::Less; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 119 | case GrStencilTest::kLEqual: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 120 | return wgpu::CompareFunction::LessEqual; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 121 | case GrStencilTest::kEqual: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 122 | return wgpu::CompareFunction::Equal; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 123 | case GrStencilTest::kNotEqual: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 124 | return wgpu::CompareFunction::NotEqual; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 125 | default: |
| 126 | SkASSERT(!"unsupported stencil test"); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 127 | return wgpu::CompareFunction::Always; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 131 | static wgpu::StencilOperation to_dawn_stencil_operation(GrStencilOp op) { |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 132 | switch (op) { |
| 133 | case GrStencilOp::kKeep: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 134 | return wgpu::StencilOperation::Keep; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 135 | case GrStencilOp::kZero: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 136 | return wgpu::StencilOperation::Zero; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 137 | case GrStencilOp::kReplace: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 138 | return wgpu::StencilOperation::Replace; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 139 | case GrStencilOp::kInvert: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 140 | return wgpu::StencilOperation::Invert; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 141 | case GrStencilOp::kIncClamp: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 142 | return wgpu::StencilOperation::IncrementClamp; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 143 | case GrStencilOp::kDecClamp: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 144 | return wgpu::StencilOperation::DecrementClamp; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 145 | case GrStencilOp::kIncWrap: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 146 | return wgpu::StencilOperation::IncrementWrap; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 147 | case GrStencilOp::kDecWrap: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 148 | return wgpu::StencilOperation::DecrementWrap; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 149 | default: |
| 150 | SkASSERT(!"unsupported stencil function"); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 151 | return wgpu::StencilOperation::Keep; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 155 | static wgpu::PrimitiveTopology to_dawn_primitive_topology(GrPrimitiveType primitiveType) { |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 156 | switch (primitiveType) { |
| 157 | case GrPrimitiveType::kTriangles: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 158 | return wgpu::PrimitiveTopology::TriangleList; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 159 | case GrPrimitiveType::kTriangleStrip: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 160 | return wgpu::PrimitiveTopology::TriangleStrip; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 161 | case GrPrimitiveType::kPoints: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 162 | return wgpu::PrimitiveTopology::PointList; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 163 | case GrPrimitiveType::kLines: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 164 | return wgpu::PrimitiveTopology::LineList; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 165 | case GrPrimitiveType::kLineStrip: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 166 | return wgpu::PrimitiveTopology::LineStrip; |
Robert Phillips | 571177f | 2019-10-04 14:41:49 -0400 | [diff] [blame] | 167 | case GrPrimitiveType::kPath: |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 168 | default: |
| 169 | SkASSERT(!"unsupported primitive topology"); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 170 | return wgpu::PrimitiveTopology::TriangleList; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 174 | static wgpu::VertexFormat to_dawn_vertex_format(GrVertexAttribType type) { |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 175 | switch (type) { |
| 176 | case kFloat_GrVertexAttribType: |
| 177 | case kHalf_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 178 | return wgpu::VertexFormat::Float; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 179 | case kFloat2_GrVertexAttribType: |
| 180 | case kHalf2_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 181 | return wgpu::VertexFormat::Float2; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 182 | case kFloat3_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 183 | return wgpu::VertexFormat::Float3; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 184 | case kFloat4_GrVertexAttribType: |
| 185 | case kHalf4_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 186 | return wgpu::VertexFormat::Float4; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 187 | case kUShort2_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 188 | return wgpu::VertexFormat::UShort2; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 189 | case kInt_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 190 | return wgpu::VertexFormat::Int; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 191 | case kUByte4_norm_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 192 | return wgpu::VertexFormat::UChar4Norm; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 193 | default: |
| 194 | SkASSERT(!"unsupported vertex format"); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 195 | return wgpu::VertexFormat::Float4; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 199 | static wgpu::ColorStateDescriptor create_color_state(const GrDawnGpu* gpu, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 200 | const GrPipeline& pipeline, |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 201 | wgpu::TextureFormat colorFormat) { |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 202 | GrXferProcessor::BlendInfo blendInfo = pipeline.getXferProcessor().getBlendInfo(); |
| 203 | GrBlendEquation equation = blendInfo.fEquation; |
| 204 | GrBlendCoeff srcCoeff = blendInfo.fSrcBlend; |
| 205 | GrBlendCoeff dstCoeff = blendInfo.fDstBlend; |
| 206 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 207 | wgpu::BlendFactor srcFactor = to_dawn_blend_factor(srcCoeff); |
| 208 | wgpu::BlendFactor dstFactor = to_dawn_blend_factor(dstCoeff); |
| 209 | wgpu::BlendFactor srcFactorAlpha = to_dawn_blend_factor_for_alpha(srcCoeff); |
| 210 | wgpu::BlendFactor dstFactorAlpha = to_dawn_blend_factor_for_alpha(dstCoeff); |
| 211 | wgpu::BlendOperation operation = to_dawn_blend_operation(equation); |
| 212 | auto mask = blendInfo.fWriteColor ? wgpu::ColorWriteMask::All : wgpu::ColorWriteMask::None; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 213 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 214 | wgpu::BlendDescriptor colorDesc = {operation, srcFactor, dstFactor}; |
| 215 | wgpu::BlendDescriptor alphaDesc = {operation, srcFactorAlpha, dstFactorAlpha}; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 216 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 217 | wgpu::ColorStateDescriptor descriptor; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 218 | descriptor.format = colorFormat; |
| 219 | descriptor.alphaBlend = alphaDesc; |
| 220 | descriptor.colorBlend = colorDesc; |
| 221 | descriptor.nextInChain = nullptr; |
| 222 | descriptor.writeMask = mask; |
| 223 | |
| 224 | return descriptor; |
| 225 | } |
| 226 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 227 | static wgpu::StencilStateFaceDescriptor to_stencil_state_face(const GrStencilSettings::Face& face) { |
| 228 | wgpu::StencilStateFaceDescriptor desc; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 229 | desc.compare = to_dawn_compare_function(face.fTest); |
| 230 | desc.failOp = desc.depthFailOp = to_dawn_stencil_operation(face.fFailOp); |
| 231 | desc.passOp = to_dawn_stencil_operation(face.fPassOp); |
| 232 | return desc; |
| 233 | } |
| 234 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 235 | static wgpu::DepthStencilStateDescriptor create_depth_stencil_state( |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 236 | const GrProgramInfo& programInfo, |
| 237 | wgpu::TextureFormat depthStencilFormat) { |
| 238 | GrStencilSettings stencilSettings = programInfo.nonGLStencilSettings(); |
| 239 | GrSurfaceOrigin origin = programInfo.origin(); |
| 240 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 241 | wgpu::DepthStencilStateDescriptor state; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 242 | state.format = depthStencilFormat; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 243 | if (!stencilSettings.isDisabled()) { |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 244 | if (stencilSettings.isTwoSided()) { |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 245 | auto front = stencilSettings.postOriginCCWFace(origin); |
Stephen White | 3a62ed4 | 2019-11-11 16:56:02 -0500 | [diff] [blame] | 246 | auto back = stencilSettings.postOriginCWFace(origin); |
Stephen White | f4b3d6b | 2019-10-18 07:54:25 -0400 | [diff] [blame] | 247 | state.stencilFront = to_stencil_state_face(front); |
| 248 | state.stencilBack = to_stencil_state_face(back); |
| 249 | state.stencilReadMask = front.fTestMask; |
| 250 | state.stencilWriteMask = front.fWriteMask; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 251 | } else { |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 252 | auto frontAndBack = stencilSettings.singleSidedFace(); |
Stephen White | f4b3d6b | 2019-10-18 07:54:25 -0400 | [diff] [blame] | 253 | state.stencilBack = state.stencilFront = to_stencil_state_face(frontAndBack); |
| 254 | state.stencilReadMask = frontAndBack.fTestMask; |
| 255 | state.stencilWriteMask = frontAndBack.fWriteMask; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | return state; |
| 259 | } |
| 260 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 261 | static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, const wgpu::Buffer& buffer, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 262 | uint32_t offset, uint32_t size, const |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 263 | wgpu::Sampler& sampler, |
| 264 | const wgpu::TextureView& textureView) { |
| 265 | wgpu::BindGroupBinding result; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 266 | result.binding = binding; |
| 267 | result.buffer = buffer; |
| 268 | result.offset = offset; |
| 269 | result.size = size; |
| 270 | result.sampler = sampler; |
| 271 | result.textureView = textureView; |
| 272 | return result; |
| 273 | } |
| 274 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 275 | static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, const wgpu::Buffer& buffer, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 276 | uint32_t offset, uint32_t size) { |
| 277 | return make_bind_group_binding(binding, buffer, offset, size, nullptr, nullptr); |
| 278 | } |
| 279 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 280 | static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, |
| 281 | const wgpu::Sampler& sampler) { |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 282 | return make_bind_group_binding(binding, nullptr, 0, 0, sampler, nullptr); |
| 283 | } |
| 284 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 285 | static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, |
| 286 | const wgpu::TextureView& textureView) { |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 287 | return make_bind_group_binding(binding, nullptr, 0, 0, nullptr, textureView); |
| 288 | } |
| 289 | |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 290 | sk_sp<GrDawnProgram> GrDawnProgramBuilder::Build(GrDawnGpu* gpu, |
| 291 | GrRenderTarget* renderTarget, |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 292 | const GrProgramInfo& programInfo, |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 293 | wgpu::TextureFormat colorFormat, |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 294 | bool hasDepthStencil, |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 295 | wgpu::TextureFormat depthStencilFormat, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 296 | GrProgramDesc* desc) { |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 297 | GrDawnProgramBuilder builder(gpu, renderTarget, programInfo, desc); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 298 | if (!builder.emitAndInstallProcs()) { |
| 299 | return nullptr; |
| 300 | } |
| 301 | |
| 302 | builder.fVS.extensions().appendf("#extension GL_ARB_separate_shader_objects : enable\n"); |
| 303 | builder.fFS.extensions().appendf("#extension GL_ARB_separate_shader_objects : enable\n"); |
| 304 | builder.fVS.extensions().appendf("#extension GL_ARB_shading_language_420pack : enable\n"); |
| 305 | builder.fFS.extensions().appendf("#extension GL_ARB_shading_language_420pack : enable\n"); |
| 306 | |
| 307 | builder.finalizeShaders(); |
| 308 | |
| 309 | SkSL::Program::Inputs vertInputs, fragInputs; |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 310 | bool flipY = programInfo.origin() != kTopLeft_GrSurfaceOrigin; |
| 311 | auto vsModule = builder.createShaderModule(builder.fVS, SkSL::Program::kVertex_Kind, flipY, |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 312 | &vertInputs); |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 313 | auto fsModule = builder.createShaderModule(builder.fFS, SkSL::Program::kFragment_Kind, flipY, |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 314 | &fragInputs); |
Stephen White | 40c47e1 | 2019-11-01 13:13:03 -0400 | [diff] [blame] | 315 | GrDawnUniformHandler::UniformInfoArray& uniforms = builder.fUniformHandler.fUniforms; |
| 316 | uint32_t uniformBufferSize = builder.fUniformHandler.fCurrentUBOOffset; |
| 317 | sk_sp<GrDawnProgram> result(new GrDawnProgram(uniforms, uniformBufferSize)); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 318 | result->fGeometryProcessor = std::move(builder.fGeometryProcessor); |
| 319 | result->fXferProcessor = std::move(builder.fXferProcessor); |
| 320 | result->fFragmentProcessors = std::move(builder.fFragmentProcessors); |
| 321 | result->fFragmentProcessorCnt = builder.fFragmentProcessorCnt; |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 322 | std::vector<wgpu::BindGroupLayoutBinding> uniformLayoutBindings; |
Stephen White | dd78efd | 2019-10-23 15:00:20 -0400 | [diff] [blame] | 323 | if (0 != uniformBufferSize) { |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 324 | uniformLayoutBindings.push_back({ GrDawnUniformHandler::kUniformBinding, |
| 325 | wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment, |
| 326 | wgpu::BindingType::UniformBuffer}); |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 327 | } |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 328 | wgpu::BindGroupLayoutDescriptor uniformBindGroupLayoutDesc; |
| 329 | uniformBindGroupLayoutDesc.bindingCount = uniformLayoutBindings.size(); |
| 330 | uniformBindGroupLayoutDesc.bindings = uniformLayoutBindings.data(); |
| 331 | result->fBindGroupLayouts[0] = |
| 332 | gpu->device().CreateBindGroupLayout(&uniformBindGroupLayoutDesc); |
| 333 | uint32_t binding = 0; |
| 334 | std::vector<wgpu::BindGroupLayoutBinding> textureLayoutBindings; |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 335 | for (int i = 0; i < builder.fUniformHandler.fSamplers.count(); ++i) { |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 336 | textureLayoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment, |
| 337 | wgpu::BindingType::Sampler}); |
| 338 | textureLayoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment, |
| 339 | wgpu::BindingType::SampledTexture}); |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 340 | } |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 341 | wgpu::BindGroupLayoutDescriptor textureBindGroupLayoutDesc; |
| 342 | textureBindGroupLayoutDesc.bindingCount = textureLayoutBindings.size(); |
| 343 | textureBindGroupLayoutDesc.bindings = textureLayoutBindings.data(); |
| 344 | result->fBindGroupLayouts[1] = |
| 345 | gpu->device().CreateBindGroupLayout(&textureBindGroupLayoutDesc); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 346 | wgpu::PipelineLayoutDescriptor pipelineLayoutDesc; |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 347 | pipelineLayoutDesc.bindGroupLayoutCount = 2; |
| 348 | pipelineLayoutDesc.bindGroupLayouts = &result->fBindGroupLayouts[0]; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 349 | auto pipelineLayout = gpu->device().CreatePipelineLayout(&pipelineLayoutDesc); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 350 | result->fBuiltinUniformHandles = builder.fUniformHandles; |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 351 | const GrPipeline& pipeline = programInfo.pipeline(); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 352 | auto colorState = create_color_state(gpu, pipeline, colorFormat); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 353 | wgpu::DepthStencilStateDescriptor depthStencilState; |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 354 | |
| 355 | #ifdef SK_DEBUG |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 356 | if (pipeline.isStencilEnabled()) { |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 357 | SkASSERT(renderTarget->renderTargetPriv().numStencilBits() == 8); |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 358 | } |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 359 | #endif |
| 360 | depthStencilState = create_depth_stencil_state(programInfo, depthStencilFormat); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 361 | |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 362 | std::vector<wgpu::VertexBufferLayoutDescriptor> inputs; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 363 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 364 | std::vector<wgpu::VertexAttributeDescriptor> vertexAttributes; |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 365 | const GrPrimitiveProcessor& primProc = programInfo.primProc(); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 366 | if (primProc.numVertexAttributes() > 0) { |
| 367 | size_t offset = 0; |
| 368 | int i = 0; |
| 369 | for (const auto& attrib : primProc.vertexAttributes()) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 370 | wgpu::VertexAttributeDescriptor attribute; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 371 | attribute.shaderLocation = i; |
| 372 | attribute.offset = offset; |
| 373 | attribute.format = to_dawn_vertex_format(attrib.cpuType()); |
| 374 | vertexAttributes.push_back(attribute); |
| 375 | offset += attrib.sizeAlign4(); |
| 376 | i++; |
| 377 | } |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 378 | wgpu::VertexBufferLayoutDescriptor input; |
| 379 | input.arrayStride = offset; |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 380 | input.stepMode = wgpu::InputStepMode::Vertex; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 381 | input.attributeCount = vertexAttributes.size(); |
| 382 | input.attributes = &vertexAttributes.front(); |
| 383 | inputs.push_back(input); |
| 384 | } |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 385 | std::vector<wgpu::VertexAttributeDescriptor> instanceAttributes; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 386 | if (primProc.numInstanceAttributes() > 0) { |
| 387 | size_t offset = 0; |
| 388 | int i = 0; |
| 389 | for (const auto& attrib : primProc.instanceAttributes()) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 390 | wgpu::VertexAttributeDescriptor attribute; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 391 | attribute.shaderLocation = i; |
| 392 | attribute.offset = offset; |
| 393 | attribute.format = to_dawn_vertex_format(attrib.cpuType()); |
| 394 | instanceAttributes.push_back(attribute); |
| 395 | offset += attrib.sizeAlign4(); |
| 396 | i++; |
| 397 | } |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 398 | wgpu::VertexBufferLayoutDescriptor input; |
| 399 | input.arrayStride = offset; |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 400 | input.stepMode = wgpu::InputStepMode::Instance; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 401 | input.attributeCount = instanceAttributes.size(); |
| 402 | input.attributes = &instanceAttributes.front(); |
| 403 | inputs.push_back(input); |
| 404 | } |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 405 | wgpu::VertexStateDescriptor vertexState; |
| 406 | vertexState.indexFormat = wgpu::IndexFormat::Uint16; |
| 407 | vertexState.vertexBufferCount = inputs.size(); |
| 408 | vertexState.vertexBuffers = &inputs.front(); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 409 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 410 | wgpu::ProgrammableStageDescriptor vsDesc; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 411 | vsDesc.module = vsModule; |
| 412 | vsDesc.entryPoint = "main"; |
| 413 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 414 | wgpu::ProgrammableStageDescriptor fsDesc; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 415 | fsDesc.module = fsModule; |
| 416 | fsDesc.entryPoint = "main"; |
| 417 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 418 | wgpu::RenderPipelineDescriptor rpDesc; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 419 | rpDesc.layout = pipelineLayout; |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 420 | rpDesc.vertexStage = vsDesc; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 421 | rpDesc.fragmentStage = &fsDesc; |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 422 | rpDesc.vertexState = &vertexState; |
Robert Phillips | fcaae48 | 2019-11-07 10:17:03 -0500 | [diff] [blame] | 423 | rpDesc.primitiveTopology = to_dawn_primitive_topology(programInfo.primitiveType()); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 424 | if (hasDepthStencil) { |
| 425 | rpDesc.depthStencilState = &depthStencilState; |
| 426 | } |
| 427 | rpDesc.colorStateCount = 1; |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 428 | rpDesc.colorStates = &colorState; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 429 | result->fRenderPipeline = gpu->device().CreateRenderPipeline(&rpDesc); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 430 | return result; |
| 431 | } |
| 432 | |
| 433 | GrDawnProgramBuilder::GrDawnProgramBuilder(GrDawnGpu* gpu, |
| 434 | GrRenderTarget* renderTarget, |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 435 | const GrProgramInfo& programInfo, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 436 | GrProgramDesc* desc) |
Stephen White | 511af2e | 2020-02-07 11:04:58 -0500 | [diff] [blame] | 437 | : INHERITED(renderTarget, *desc, programInfo) |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 438 | , fGpu(gpu) |
| 439 | , fVaryingHandler(this) |
| 440 | , fUniformHandler(this) { |
| 441 | } |
| 442 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 443 | wgpu::ShaderModule GrDawnProgramBuilder::createShaderModule(const GrGLSLShaderBuilder& builder, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 444 | SkSL::Program::Kind kind, |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 445 | bool flipY, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 446 | SkSL::Program::Inputs* inputs) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 447 | wgpu::Device device = fGpu->device(); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 448 | SkString source(builder.fCompilerString.c_str()); |
| 449 | |
| 450 | #if 0 |
| 451 | SkSL::String sksl = GrShaderUtils::PrettyPrint(builder.fCompilerString); |
| 452 | printf("converting program:\n%s\n", sksl.c_str()); |
| 453 | #endif |
| 454 | |
Stephen White | 40c47e1 | 2019-11-01 13:13:03 -0400 | [diff] [blame] | 455 | SkSL::String spirvSource = sksl_to_spirv(fGpu, source.c_str(), kind, flipY, |
| 456 | fUniformHandler.getRTHeightOffset(), inputs); |
| 457 | if (inputs->fRTHeight) { |
| 458 | this->addRTHeightUniform(SKSL_RTHEIGHT_NAME); |
| 459 | } |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 460 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 461 | wgpu::ShaderModuleDescriptor desc; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 462 | desc.codeSize = spirvSource.size() / 4; |
| 463 | desc.code = reinterpret_cast<const uint32_t*>(spirvSource.c_str()); |
| 464 | |
| 465 | return device.CreateShaderModule(&desc); |
| 466 | }; |
| 467 | |
| 468 | const GrCaps* GrDawnProgramBuilder::caps() const { |
| 469 | return fGpu->caps(); |
| 470 | } |
| 471 | |
| 472 | void GrDawnProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) { |
| 473 | // Load the RT height uniform if it is needed to y-flip gl_FragCoord. |
| 474 | if (fBuiltinUniformHandles.fRTHeightUni.isValid() && |
| 475 | fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) { |
| 476 | fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height())); |
| 477 | } |
| 478 | |
| 479 | // set RT adjustment |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 480 | SkISize dimensions = rt->dimensions(); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 481 | SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid()); |
| 482 | if (fRenderTargetState.fRenderTargetOrigin != origin || |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 483 | fRenderTargetState.fRenderTargetSize != dimensions) { |
| 484 | fRenderTargetState.fRenderTargetSize = dimensions; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 485 | fRenderTargetState.fRenderTargetOrigin = origin; |
| 486 | |
| 487 | float rtAdjustmentVec[4]; |
| 488 | fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec); |
| 489 | fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec); |
| 490 | } |
| 491 | } |
| 492 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 493 | static void set_texture(GrDawnGpu* gpu, GrSamplerState state, GrTexture* texture, |
| 494 | std::vector<wgpu::BindGroupBinding>* bindings, int* binding) { |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 495 | // FIXME: could probably cache samplers in GrDawnProgram |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 496 | wgpu::Sampler sampler = gpu->getOrCreateSampler(state); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 497 | bindings->push_back(make_bind_group_binding((*binding)++, sampler)); |
| 498 | GrDawnTexture* tex = static_cast<GrDawnTexture*>(texture); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 499 | wgpu::TextureView textureView = tex->textureView(); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 500 | bindings->push_back(make_bind_group_binding((*binding)++, textureView)); |
| 501 | } |
| 502 | |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 503 | wgpu::BindGroup GrDawnProgram::setUniformData(GrDawnGpu* gpu, const GrRenderTarget* renderTarget, |
| 504 | const GrProgramInfo& programInfo) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 505 | std::vector<wgpu::BindGroupBinding> bindings; |
Stephen White | dd78efd | 2019-10-23 15:00:20 -0400 | [diff] [blame] | 506 | GrDawnRingBuffer::Slice slice; |
| 507 | uint32_t uniformBufferSize = fDataManager.uniformBufferSize(); |
| 508 | if (0 != uniformBufferSize) { |
| 509 | slice = gpu->allocateUniformRingBufferSlice(uniformBufferSize); |
| 510 | bindings.push_back(make_bind_group_binding(GrDawnUniformHandler::kUniformBinding, |
| 511 | slice.fBuffer, slice.fOffset, |
| 512 | uniformBufferSize)); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 513 | } |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 514 | this->setRenderTargetState(renderTarget, programInfo.origin()); |
| 515 | const GrPipeline& pipeline = programInfo.pipeline(); |
| 516 | const GrPrimitiveProcessor& primProc = programInfo.primProc(); |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 517 | GrFragmentProcessor::PipelineCoordTransformRange transformRange(pipeline); |
| 518 | fGeometryProcessor->setData(fDataManager, primProc, transformRange); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 519 | GrFragmentProcessor::CIter fpIter(pipeline); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 520 | GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt); |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 521 | for (; fpIter && glslIter; ++fpIter, ++glslIter) { |
| 522 | glslIter->setData(fDataManager, *fpIter); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 523 | } |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 524 | SkIPoint offset; |
| 525 | GrTexture* dstTexture = pipeline.peekDstTexture(&offset); |
| 526 | fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset); |
Stephen White | 8eda499 | 2020-03-17 11:44:48 -0400 | [diff] [blame] | 527 | if (0 != uniformBufferSize) { |
| 528 | fDataManager.uploadUniformBuffers(slice.fData); |
| 529 | } |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 530 | wgpu::BindGroupDescriptor descriptor; |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 531 | descriptor.layout = fBindGroupLayouts[0]; |
| 532 | descriptor.bindingCount = bindings.size(); |
| 533 | descriptor.bindings = bindings.data(); |
| 534 | return gpu->device().CreateBindGroup(&descriptor); |
| 535 | } |
| 536 | |
| 537 | wgpu::BindGroup GrDawnProgram::setTextures(GrDawnGpu* gpu, |
Stephen White | 6e8ceee | 2020-02-25 16:25:43 -0500 | [diff] [blame] | 538 | const GrPrimitiveProcessor& primProc, |
| 539 | const GrPipeline& pipeline, |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 540 | const GrSurfaceProxy* const primProcTextures[]) { |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 541 | std::vector<wgpu::BindGroupBinding> bindings; |
| 542 | int binding = 0; |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 543 | if (primProcTextures) { |
| 544 | for (int i = 0; i < primProc.numTextureSamplers(); ++i) { |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 545 | SkASSERT(primProcTextures[i]->asTextureProxy()); |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 546 | auto& sampler = primProc.textureSampler(i); |
| 547 | set_texture(gpu, sampler.samplerState(), primProcTextures[i]->peekTexture(), &bindings, |
| 548 | &binding); |
| 549 | } |
| 550 | } |
Stephen White | a521c96 | 2019-12-04 09:57:48 -0500 | [diff] [blame] | 551 | GrFragmentProcessor::CIter fpIter(pipeline); |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 552 | GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt); |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 553 | for (; fpIter && glslIter; ++fpIter, ++glslIter) { |
| 554 | for (int i = 0; i < fpIter->numTextureSamplers(); ++i) { |
| 555 | auto& s = fpIter->textureSampler(i); |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 556 | set_texture(gpu, s.samplerState(), s.peekTexture(), &bindings, &binding); |
| 557 | } |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 558 | } |
| 559 | SkIPoint offset; |
| 560 | if (GrTexture* dstTexture = pipeline.peekDstTexture(&offset)) { |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 561 | set_texture(gpu, GrSamplerState::Filter::kNearest, dstTexture, &bindings, &binding); |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 562 | } |
| 563 | wgpu::BindGroupDescriptor descriptor; |
| 564 | descriptor.layout = fBindGroupLayouts[1]; |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 565 | descriptor.bindingCount = bindings.size(); |
| 566 | descriptor.bindings = bindings.data(); |
| 567 | return gpu->device().CreateBindGroup(&descriptor); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 568 | } |