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