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