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) { |
| 43 | case kZero_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 44 | return wgpu::BlendFactor::Zero; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 45 | case kOne_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 46 | return wgpu::BlendFactor::One; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 47 | case kSC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 48 | return wgpu::BlendFactor::SrcColor; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 49 | case kISC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 50 | return wgpu::BlendFactor::OneMinusSrcColor; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 51 | case kDC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 52 | return wgpu::BlendFactor::DstColor; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 53 | case kIDC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 54 | return wgpu::BlendFactor::OneMinusDstColor; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 55 | case kSA_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 56 | return wgpu::BlendFactor::SrcAlpha; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 57 | case kISA_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 58 | return wgpu::BlendFactor::OneMinusSrcAlpha; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 59 | case kDA_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 60 | return wgpu::BlendFactor::DstAlpha; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 61 | case kIDA_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 62 | return wgpu::BlendFactor::OneMinusDstAlpha; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 63 | case kConstC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 64 | return wgpu::BlendFactor::BlendColor; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 65 | case kIConstC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 66 | return wgpu::BlendFactor::OneMinusBlendColor; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 67 | 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 White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 75 | return wgpu::BlendFactor::One; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 79 | static wgpu::BlendFactor to_dawn_blend_factor_for_alpha(GrBlendCoeff coeff) { |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 80 | switch (coeff) { |
| 81 | // Force all srcColor used in alpha slot to alpha version. |
| 82 | case kSC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 83 | return wgpu::BlendFactor::SrcAlpha; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 84 | case kISC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 85 | return wgpu::BlendFactor::OneMinusSrcAlpha; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 86 | case kDC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 87 | return wgpu::BlendFactor::DstAlpha; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 88 | case kIDC_GrBlendCoeff: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 89 | return wgpu::BlendFactor::OneMinusDstAlpha; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 90 | default: |
| 91 | return to_dawn_blend_factor(coeff); |
| 92 | } |
| 93 | } |
| 94 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 95 | static wgpu::BlendOperation to_dawn_blend_operation(GrBlendEquation equation) { |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 96 | switch (equation) { |
| 97 | case kAdd_GrBlendEquation: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 98 | return wgpu::BlendOperation::Add; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 99 | case kSubtract_GrBlendEquation: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 100 | return wgpu::BlendOperation::Subtract; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 101 | case kReverseSubtract_GrBlendEquation: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 102 | return wgpu::BlendOperation::ReverseSubtract; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 103 | default: |
| 104 | SkASSERT(!"unsupported blend equation"); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 105 | return wgpu::BlendOperation::Add; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 109 | static wgpu::CompareFunction to_dawn_compare_function(GrStencilTest test) { |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 110 | switch (test) { |
| 111 | case GrStencilTest::kAlways: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 112 | return wgpu::CompareFunction::Always; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 113 | case GrStencilTest::kNever: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 114 | return wgpu::CompareFunction::Never; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 115 | case GrStencilTest::kGreater: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 116 | return wgpu::CompareFunction::Greater; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 117 | case GrStencilTest::kGEqual: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 118 | return wgpu::CompareFunction::GreaterEqual; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 119 | case GrStencilTest::kLess: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 120 | return wgpu::CompareFunction::Less; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 121 | case GrStencilTest::kLEqual: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 122 | return wgpu::CompareFunction::LessEqual; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 123 | case GrStencilTest::kEqual: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 124 | return wgpu::CompareFunction::Equal; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 125 | case GrStencilTest::kNotEqual: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 126 | return wgpu::CompareFunction::NotEqual; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 127 | default: |
| 128 | SkASSERT(!"unsupported stencil test"); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 129 | return wgpu::CompareFunction::Always; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 133 | static wgpu::StencilOperation to_dawn_stencil_operation(GrStencilOp op) { |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 134 | switch (op) { |
| 135 | case GrStencilOp::kKeep: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 136 | return wgpu::StencilOperation::Keep; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 137 | case GrStencilOp::kZero: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 138 | return wgpu::StencilOperation::Zero; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 139 | case GrStencilOp::kReplace: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 140 | return wgpu::StencilOperation::Replace; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 141 | case GrStencilOp::kInvert: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 142 | return wgpu::StencilOperation::Invert; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 143 | case GrStencilOp::kIncClamp: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 144 | return wgpu::StencilOperation::IncrementClamp; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 145 | case GrStencilOp::kDecClamp: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 146 | return wgpu::StencilOperation::DecrementClamp; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 147 | case GrStencilOp::kIncWrap: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 148 | return wgpu::StencilOperation::IncrementWrap; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 149 | case GrStencilOp::kDecWrap: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 150 | return wgpu::StencilOperation::DecrementWrap; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 151 | default: |
| 152 | SkASSERT(!"unsupported stencil function"); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 153 | return wgpu::StencilOperation::Keep; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 157 | static wgpu::PrimitiveTopology to_dawn_primitive_topology(GrPrimitiveType primitiveType) { |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 158 | switch (primitiveType) { |
| 159 | case GrPrimitiveType::kTriangles: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 160 | return wgpu::PrimitiveTopology::TriangleList; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 161 | case GrPrimitiveType::kTriangleStrip: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 162 | return wgpu::PrimitiveTopology::TriangleStrip; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 163 | case GrPrimitiveType::kPoints: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 164 | return wgpu::PrimitiveTopology::PointList; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 165 | case GrPrimitiveType::kLines: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 166 | return wgpu::PrimitiveTopology::LineList; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 167 | case GrPrimitiveType::kLineStrip: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 168 | return wgpu::PrimitiveTopology::LineStrip; |
Robert Phillips | 571177f | 2019-10-04 14:41:49 -0400 | [diff] [blame] | 169 | case GrPrimitiveType::kPath: |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 170 | default: |
| 171 | SkASSERT(!"unsupported primitive topology"); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 172 | return wgpu::PrimitiveTopology::TriangleList; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 176 | static wgpu::VertexFormat to_dawn_vertex_format(GrVertexAttribType type) { |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 177 | switch (type) { |
| 178 | case kFloat_GrVertexAttribType: |
| 179 | case kHalf_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 180 | return wgpu::VertexFormat::Float; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 181 | case kFloat2_GrVertexAttribType: |
| 182 | case kHalf2_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 183 | return wgpu::VertexFormat::Float2; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 184 | case kFloat3_GrVertexAttribType: |
| 185 | case kHalf3_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 186 | return wgpu::VertexFormat::Float3; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 187 | case kFloat4_GrVertexAttribType: |
| 188 | case kHalf4_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 189 | return wgpu::VertexFormat::Float4; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 190 | case kUShort2_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 191 | return wgpu::VertexFormat::UShort2; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 192 | case kInt_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 193 | return wgpu::VertexFormat::Int; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 194 | case kUByte4_norm_GrVertexAttribType: |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 195 | return wgpu::VertexFormat::UChar4Norm; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 196 | default: |
| 197 | SkASSERT(!"unsupported vertex format"); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 198 | return wgpu::VertexFormat::Float4; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 202 | static wgpu::ColorStateDescriptor create_color_state(const GrDawnGpu* gpu, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 203 | const GrPipeline& pipeline, |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 204 | wgpu::TextureFormat colorFormat) { |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 205 | GrXferProcessor::BlendInfo blendInfo = pipeline.getXferProcessor().getBlendInfo(); |
| 206 | GrBlendEquation equation = blendInfo.fEquation; |
| 207 | GrBlendCoeff srcCoeff = blendInfo.fSrcBlend; |
| 208 | GrBlendCoeff dstCoeff = blendInfo.fDstBlend; |
| 209 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 210 | 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 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::BlendDescriptor colorDesc = {operation, srcFactor, dstFactor}; |
| 218 | wgpu::BlendDescriptor alphaDesc = {operation, srcFactorAlpha, dstFactorAlpha}; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 219 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 220 | wgpu::ColorStateDescriptor descriptor; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 221 | 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 White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 230 | static wgpu::StencilStateFaceDescriptor to_stencil_state_face(const GrStencilSettings::Face& face) { |
| 231 | wgpu::StencilStateFaceDescriptor desc; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 232 | 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 White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 238 | static wgpu::DepthStencilStateDescriptor create_depth_stencil_state( |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 239 | const GrStencilSettings& stencilSettings, |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 240 | wgpu::TextureFormat depthStencilFormat, |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 241 | GrSurfaceOrigin origin) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 242 | wgpu::DepthStencilStateDescriptor state; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 243 | state.format = depthStencilFormat; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 244 | if (!stencilSettings.isDisabled()) { |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 245 | if (stencilSettings.isTwoSided()) { |
Stephen White | f4b3d6b | 2019-10-18 07:54:25 -0400 | [diff] [blame] | 246 | 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 White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 252 | } else { |
Stephen White | f4b3d6b | 2019-10-18 07:54:25 -0400 | [diff] [blame] | 253 | 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 White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | return state; |
| 260 | } |
| 261 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 262 | 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] | 263 | uint32_t offset, uint32_t size, const |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 264 | wgpu::Sampler& sampler, |
| 265 | const wgpu::TextureView& textureView) { |
| 266 | wgpu::BindGroupBinding result; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 267 | 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 White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 276 | 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] | 277 | uint32_t offset, uint32_t size) { |
| 278 | return make_bind_group_binding(binding, buffer, offset, size, nullptr, nullptr); |
| 279 | } |
| 280 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 281 | static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, |
| 282 | const wgpu::Sampler& sampler) { |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 283 | return make_bind_group_binding(binding, nullptr, 0, 0, sampler, nullptr); |
| 284 | } |
| 285 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 286 | static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, |
| 287 | const wgpu::TextureView& textureView) { |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 288 | return make_bind_group_binding(binding, nullptr, 0, 0, nullptr, textureView); |
| 289 | } |
| 290 | |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 291 | sk_sp<GrDawnProgram> GrDawnProgramBuilder::Build(GrDawnGpu* gpu, |
| 292 | GrRenderTarget* renderTarget, |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 293 | const GrProgramInfo& programInfo, |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 294 | GrPrimitiveType primitiveType, |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 295 | wgpu::TextureFormat colorFormat, |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 296 | bool hasDepthStencil, |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 297 | wgpu::TextureFormat depthStencilFormat, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 298 | GrProgramDesc* desc) { |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 299 | GrDawnProgramBuilder builder(gpu, renderTarget, programInfo, desc); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 300 | 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 White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 312 | bool flipY = programInfo.origin() != kTopLeft_GrSurfaceOrigin; |
| 313 | auto vsModule = builder.createShaderModule(builder.fVS, SkSL::Program::kVertex_Kind, flipY, |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 314 | &vertInputs); |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 315 | auto fsModule = builder.createShaderModule(builder.fFS, SkSL::Program::kFragment_Kind, flipY, |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 316 | &fragInputs); |
Stephen White | 40c47e1 | 2019-11-01 13:13:03 -0400 | [diff] [blame^] | 317 | GrDawnUniformHandler::UniformInfoArray& uniforms = builder.fUniformHandler.fUniforms; |
| 318 | uint32_t uniformBufferSize = builder.fUniformHandler.fCurrentUBOOffset; |
| 319 | sk_sp<GrDawnProgram> result(new GrDawnProgram(uniforms, uniformBufferSize)); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 320 | 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 White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 324 | std::vector<wgpu::BindGroupLayoutBinding> layoutBindings; |
Stephen White | dd78efd | 2019-10-23 15:00:20 -0400 | [diff] [blame] | 325 | if (0 != uniformBufferSize) { |
| 326 | layoutBindings.push_back({ GrDawnUniformHandler::kUniformBinding, |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 327 | wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment, |
| 328 | wgpu::BindingType::UniformBuffer}); |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 329 | } |
| 330 | uint32_t binding = GrDawnUniformHandler::kSamplerBindingBase; |
| 331 | for (int i = 0; i < builder.fUniformHandler.fSamplers.count(); ++i) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 332 | layoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment, |
| 333 | wgpu::BindingType::Sampler}); |
| 334 | layoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment, |
| 335 | wgpu::BindingType::SampledTexture}); |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 336 | } |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 337 | wgpu::BindGroupLayoutDescriptor bindGroupLayoutDesc; |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 338 | bindGroupLayoutDesc.bindingCount = layoutBindings.size(); |
| 339 | bindGroupLayoutDesc.bindings = layoutBindings.data(); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 340 | result->fBindGroupLayout = gpu->device().CreateBindGroupLayout(&bindGroupLayoutDesc); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 341 | wgpu::PipelineLayoutDescriptor pipelineLayoutDesc; |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 342 | pipelineLayoutDesc.bindGroupLayoutCount = 1; |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 343 | pipelineLayoutDesc.bindGroupLayouts = &result->fBindGroupLayout; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 344 | auto pipelineLayout = gpu->device().CreatePipelineLayout(&pipelineLayoutDesc); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 345 | result->fBuiltinUniformHandles = builder.fUniformHandles; |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 346 | const GrPipeline& pipeline = programInfo.pipeline(); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 347 | auto colorState = create_color_state(gpu, pipeline, colorFormat); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 348 | wgpu::DepthStencilStateDescriptor depthStencilState; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 349 | GrStencilSettings stencil; |
| 350 | if (pipeline.isStencilEnabled()) { |
| 351 | int numStencilBits = renderTarget->renderTargetPriv().numStencilBits(); |
| 352 | stencil.reset(*pipeline.getUserStencil(), pipeline.hasStencilClip(), numStencilBits); |
| 353 | } |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 354 | depthStencilState = create_depth_stencil_state(stencil, depthStencilFormat, |
| 355 | programInfo.origin()); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 356 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 357 | std::vector<wgpu::VertexBufferDescriptor> inputs; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 358 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 359 | std::vector<wgpu::VertexAttributeDescriptor> vertexAttributes; |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 360 | const GrPrimitiveProcessor& primProc = programInfo.primProc(); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 361 | if (primProc.numVertexAttributes() > 0) { |
| 362 | size_t offset = 0; |
| 363 | int i = 0; |
| 364 | for (const auto& attrib : primProc.vertexAttributes()) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 365 | wgpu::VertexAttributeDescriptor attribute; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 366 | 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 White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 373 | wgpu::VertexBufferDescriptor input; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 374 | input.stride = offset; |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 375 | input.stepMode = wgpu::InputStepMode::Vertex; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 376 | input.attributeCount = vertexAttributes.size(); |
| 377 | input.attributes = &vertexAttributes.front(); |
| 378 | inputs.push_back(input); |
| 379 | } |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 380 | std::vector<wgpu::VertexAttributeDescriptor> instanceAttributes; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 381 | if (primProc.numInstanceAttributes() > 0) { |
| 382 | size_t offset = 0; |
| 383 | int i = 0; |
| 384 | for (const auto& attrib : primProc.instanceAttributes()) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 385 | wgpu::VertexAttributeDescriptor attribute; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 386 | 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 White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 393 | wgpu::VertexBufferDescriptor input; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 394 | input.stride = offset; |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 395 | input.stepMode = wgpu::InputStepMode::Instance; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 396 | input.attributeCount = instanceAttributes.size(); |
| 397 | input.attributes = &instanceAttributes.front(); |
| 398 | inputs.push_back(input); |
| 399 | } |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 400 | wgpu::VertexInputDescriptor vertexInput; |
| 401 | vertexInput.indexFormat = wgpu::IndexFormat::Uint16; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 402 | vertexInput.bufferCount = inputs.size(); |
| 403 | vertexInput.buffers = &inputs.front(); |
| 404 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 405 | wgpu::ProgrammableStageDescriptor vsDesc; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 406 | vsDesc.module = vsModule; |
| 407 | vsDesc.entryPoint = "main"; |
| 408 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 409 | wgpu::ProgrammableStageDescriptor fsDesc; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 410 | fsDesc.module = fsModule; |
| 411 | fsDesc.entryPoint = "main"; |
| 412 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 413 | wgpu::RenderPipelineDescriptor rpDesc; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 414 | rpDesc.layout = pipelineLayout; |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 415 | rpDesc.vertexStage = vsDesc; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 416 | 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 White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 423 | rpDesc.colorStates = &colorState; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 424 | result->fRenderPipeline = gpu->device().CreateRenderPipeline(&rpDesc); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 425 | return result; |
| 426 | } |
| 427 | |
| 428 | GrDawnProgramBuilder::GrDawnProgramBuilder(GrDawnGpu* gpu, |
| 429 | GrRenderTarget* renderTarget, |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 430 | const GrProgramInfo& programInfo, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 431 | GrProgramDesc* desc) |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 432 | : INHERITED(renderTarget, programInfo, desc) |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 433 | , fGpu(gpu) |
| 434 | , fVaryingHandler(this) |
| 435 | , fUniformHandler(this) { |
| 436 | } |
| 437 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 438 | wgpu::ShaderModule GrDawnProgramBuilder::createShaderModule(const GrGLSLShaderBuilder& builder, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 439 | SkSL::Program::Kind kind, |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 440 | bool flipY, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 441 | SkSL::Program::Inputs* inputs) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 442 | wgpu::Device device = fGpu->device(); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 443 | 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 White | 40c47e1 | 2019-11-01 13:13:03 -0400 | [diff] [blame^] | 450 | 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 White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 455 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 456 | wgpu::ShaderModuleDescriptor desc; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 457 | desc.codeSize = spirvSource.size() / 4; |
| 458 | desc.code = reinterpret_cast<const uint32_t*>(spirvSource.c_str()); |
| 459 | |
| 460 | return device.CreateShaderModule(&desc); |
| 461 | }; |
| 462 | |
| 463 | const GrCaps* GrDawnProgramBuilder::caps() const { |
| 464 | return fGpu->caps(); |
| 465 | } |
| 466 | |
| 467 | void 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 Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 475 | SkISize dimensions = rt->dimensions(); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 476 | SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid()); |
| 477 | if (fRenderTargetState.fRenderTargetOrigin != origin || |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 478 | fRenderTargetState.fRenderTargetSize != dimensions) { |
| 479 | fRenderTargetState.fRenderTargetSize = dimensions; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 480 | fRenderTargetState.fRenderTargetOrigin = origin; |
| 481 | |
| 482 | float rtAdjustmentVec[4]; |
| 483 | fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec); |
| 484 | fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec); |
| 485 | } |
| 486 | } |
| 487 | |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 488 | static void setTexture(GrDawnGpu* gpu, const GrSamplerState& state, GrTexture* texture, |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 489 | std::vector<wgpu::BindGroupBinding> *bindings, int* binding) { |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 490 | // FIXME: could probably cache samplers in GrDawnProgram |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 491 | wgpu::Sampler sampler = gpu->getOrCreateSampler(state); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 492 | bindings->push_back(make_bind_group_binding((*binding)++, sampler)); |
| 493 | GrDawnTexture* tex = static_cast<GrDawnTexture*>(texture); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 494 | wgpu::TextureView textureView = tex->textureView(); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 495 | bindings->push_back(make_bind_group_binding((*binding)++, textureView)); |
| 496 | } |
| 497 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 498 | wgpu::BindGroup GrDawnProgram::setData(GrDawnGpu* gpu, const GrRenderTarget* renderTarget, |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 499 | const GrProgramInfo& programInfo) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 500 | std::vector<wgpu::BindGroupBinding> bindings; |
Stephen White | dd78efd | 2019-10-23 15:00:20 -0400 | [diff] [blame] | 501 | 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 White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 508 | } |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 509 | this->setRenderTargetState(renderTarget, programInfo.origin()); |
| 510 | const GrPipeline& pipeline = programInfo.pipeline(); |
| 511 | const GrPrimitiveProcessor& primProc = programInfo.primProc(); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 512 | fGeometryProcessor->setData(fDataManager, primProc, |
| 513 | GrFragmentProcessor::CoordTransformIter(pipeline)); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 514 | int binding = GrDawnUniformHandler::kSamplerBindingBase; |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 515 | auto primProcTextures = programInfo.hasFixedPrimProcTextures() ? |
| 516 | programInfo.fixedPrimProcTextures() : nullptr; |
| 517 | |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 518 | 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 White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 523 | 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 White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 529 | 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 White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 533 | fp = iter.next(); |
| 534 | glslFP = glslIter.next(); |
| 535 | } |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 536 | SkIPoint offset; |
| 537 | GrTexture* dstTexture = pipeline.peekDstTexture(&offset); |
| 538 | fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset); |
| 539 | if (GrTextureProxy* proxy = pipeline.dstTextureProxy()) { |
| 540 | GrFragmentProcessor::TextureSampler sampler(sk_ref_sp(proxy)); |
| 541 | setTexture(gpu, sampler.samplerState(), sampler.peekTexture(), &bindings, &binding); |
| 542 | } |
Stephen White | dd78efd | 2019-10-23 15:00:20 -0400 | [diff] [blame] | 543 | fDataManager.uploadUniformBuffers(gpu, slice); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 544 | wgpu::BindGroupDescriptor descriptor; |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 545 | descriptor.layout = fBindGroupLayout; |
| 546 | descriptor.bindingCount = bindings.size(); |
| 547 | descriptor.bindings = bindings.data(); |
| 548 | return gpu->device().CreateBindGroup(&descriptor); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 549 | } |