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 | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 26 | std::unique_ptr<SkSL::Program> program = gpu->shaderCompiler()->convertProgram( |
| 27 | kind, |
| 28 | shaderString, |
| 29 | settings); |
| 30 | if (!program) { |
| 31 | SkDebugf("SkSL error:\n%s\n", gpu->shaderCompiler()->errorText().c_str()); |
| 32 | SkASSERT(false); |
| 33 | return ""; |
| 34 | } |
| 35 | *inputs = program->fInputs; |
| 36 | SkSL::String code; |
| 37 | if (!gpu->shaderCompiler()->toSPIRV(*program, &code)) { |
| 38 | return ""; |
| 39 | } |
| 40 | return code; |
| 41 | } |
| 42 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 43 | static wgpu::BlendFactor to_dawn_blend_factor(GrBlendCoeff coeff) { |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 44 | switch (coeff) { |
Greg Daniel | 6e2af5c | 2020-03-30 15:07:05 -0400 | [diff] [blame] | 45 | case kZero_GrBlendCoeff: |
| 46 | return wgpu::BlendFactor::Zero; |
| 47 | case kOne_GrBlendCoeff: |
| 48 | return wgpu::BlendFactor::One; |
| 49 | case kSC_GrBlendCoeff: |
| 50 | return wgpu::BlendFactor::SrcColor; |
| 51 | case kISC_GrBlendCoeff: |
| 52 | return wgpu::BlendFactor::OneMinusSrcColor; |
| 53 | case kDC_GrBlendCoeff: |
| 54 | return wgpu::BlendFactor::DstColor; |
| 55 | case kIDC_GrBlendCoeff: |
| 56 | return wgpu::BlendFactor::OneMinusDstColor; |
| 57 | case kSA_GrBlendCoeff: |
| 58 | return wgpu::BlendFactor::SrcAlpha; |
| 59 | case kISA_GrBlendCoeff: |
| 60 | return wgpu::BlendFactor::OneMinusSrcAlpha; |
| 61 | case kDA_GrBlendCoeff: |
| 62 | return wgpu::BlendFactor::DstAlpha; |
| 63 | case kIDA_GrBlendCoeff: |
| 64 | return wgpu::BlendFactor::OneMinusDstAlpha; |
| 65 | case kConstC_GrBlendCoeff: |
| 66 | return wgpu::BlendFactor::BlendColor; |
| 67 | case kIConstC_GrBlendCoeff: |
| 68 | return wgpu::BlendFactor::OneMinusBlendColor; |
| 69 | case kS2C_GrBlendCoeff: |
| 70 | case kIS2C_GrBlendCoeff: |
| 71 | case kS2A_GrBlendCoeff: |
| 72 | case kIS2A_GrBlendCoeff: |
| 73 | default: |
| 74 | SkASSERT(!"unsupported blend coefficient"); |
| 75 | return wgpu::BlendFactor::One; |
| 76 | } |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 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: |
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( |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 238 | const GrProgramInfo& programInfo, |
| 239 | wgpu::TextureFormat depthStencilFormat) { |
| 240 | GrStencilSettings stencilSettings = programInfo.nonGLStencilSettings(); |
| 241 | GrSurfaceOrigin origin = programInfo.origin(); |
| 242 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 243 | wgpu::DepthStencilStateDescriptor state; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 244 | state.format = depthStencilFormat; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 245 | if (!stencilSettings.isDisabled()) { |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 246 | if (stencilSettings.isTwoSided()) { |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 247 | auto front = stencilSettings.postOriginCCWFace(origin); |
Stephen White | 3a62ed4 | 2019-11-11 16:56:02 -0500 | [diff] [blame] | 248 | auto back = stencilSettings.postOriginCWFace(origin); |
Stephen White | f4b3d6b | 2019-10-18 07:54:25 -0400 | [diff] [blame] | 249 | state.stencilFront = to_stencil_state_face(front); |
| 250 | state.stencilBack = to_stencil_state_face(back); |
| 251 | state.stencilReadMask = front.fTestMask; |
| 252 | state.stencilWriteMask = front.fWriteMask; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 253 | } else { |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 254 | auto frontAndBack = stencilSettings.singleSidedFace(); |
Stephen White | f4b3d6b | 2019-10-18 07:54:25 -0400 | [diff] [blame] | 255 | state.stencilBack = state.stencilFront = to_stencil_state_face(frontAndBack); |
| 256 | state.stencilReadMask = frontAndBack.fTestMask; |
| 257 | state.stencilWriteMask = frontAndBack.fWriteMask; |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | return state; |
| 261 | } |
| 262 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 263 | 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] | 264 | uint32_t offset, uint32_t size, const |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 265 | wgpu::Sampler& sampler, |
| 266 | const wgpu::TextureView& textureView) { |
| 267 | wgpu::BindGroupBinding result; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 268 | result.binding = binding; |
| 269 | result.buffer = buffer; |
| 270 | result.offset = offset; |
| 271 | result.size = size; |
| 272 | result.sampler = sampler; |
| 273 | result.textureView = textureView; |
| 274 | return result; |
| 275 | } |
| 276 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 277 | 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] | 278 | uint32_t offset, uint32_t size) { |
| 279 | return make_bind_group_binding(binding, buffer, offset, size, nullptr, nullptr); |
| 280 | } |
| 281 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 282 | static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, |
| 283 | const wgpu::Sampler& sampler) { |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 284 | return make_bind_group_binding(binding, nullptr, 0, 0, sampler, nullptr); |
| 285 | } |
| 286 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 287 | static wgpu::BindGroupBinding make_bind_group_binding(uint32_t binding, |
| 288 | const wgpu::TextureView& textureView) { |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 289 | return make_bind_group_binding(binding, nullptr, 0, 0, nullptr, textureView); |
| 290 | } |
| 291 | |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 292 | sk_sp<GrDawnProgram> GrDawnProgramBuilder::Build(GrDawnGpu* gpu, |
| 293 | GrRenderTarget* renderTarget, |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 294 | const GrProgramInfo& programInfo, |
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 | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 324 | std::vector<wgpu::BindGroupLayoutBinding> uniformLayoutBindings; |
Stephen White | dd78efd | 2019-10-23 15:00:20 -0400 | [diff] [blame] | 325 | if (0 != uniformBufferSize) { |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 326 | uniformLayoutBindings.push_back({ GrDawnUniformHandler::kUniformBinding, |
| 327 | wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment, |
| 328 | wgpu::BindingType::UniformBuffer}); |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 329 | } |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 330 | wgpu::BindGroupLayoutDescriptor uniformBindGroupLayoutDesc; |
| 331 | uniformBindGroupLayoutDesc.bindingCount = uniformLayoutBindings.size(); |
| 332 | uniformBindGroupLayoutDesc.bindings = uniformLayoutBindings.data(); |
| 333 | result->fBindGroupLayouts[0] = |
| 334 | gpu->device().CreateBindGroupLayout(&uniformBindGroupLayoutDesc); |
| 335 | uint32_t binding = 0; |
| 336 | std::vector<wgpu::BindGroupLayoutBinding> textureLayoutBindings; |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 337 | for (int i = 0; i < builder.fUniformHandler.fSamplers.count(); ++i) { |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 338 | textureLayoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment, |
| 339 | wgpu::BindingType::Sampler}); |
| 340 | textureLayoutBindings.push_back({ binding++, wgpu::ShaderStage::Fragment, |
| 341 | wgpu::BindingType::SampledTexture}); |
Stephen White | 170d990 | 2019-08-15 16:48:24 -0400 | [diff] [blame] | 342 | } |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 343 | wgpu::BindGroupLayoutDescriptor textureBindGroupLayoutDesc; |
| 344 | textureBindGroupLayoutDesc.bindingCount = textureLayoutBindings.size(); |
| 345 | textureBindGroupLayoutDesc.bindings = textureLayoutBindings.data(); |
| 346 | result->fBindGroupLayouts[1] = |
| 347 | gpu->device().CreateBindGroupLayout(&textureBindGroupLayoutDesc); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 348 | wgpu::PipelineLayoutDescriptor pipelineLayoutDesc; |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 349 | pipelineLayoutDesc.bindGroupLayoutCount = 2; |
| 350 | pipelineLayoutDesc.bindGroupLayouts = &result->fBindGroupLayouts[0]; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 351 | auto pipelineLayout = gpu->device().CreatePipelineLayout(&pipelineLayoutDesc); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 352 | result->fBuiltinUniformHandles = builder.fUniformHandles; |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 353 | const GrPipeline& pipeline = programInfo.pipeline(); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 354 | auto colorState = create_color_state(gpu, pipeline, colorFormat); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 355 | wgpu::DepthStencilStateDescriptor depthStencilState; |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 356 | |
| 357 | #ifdef SK_DEBUG |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 358 | if (pipeline.isStencilEnabled()) { |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 359 | SkASSERT(renderTarget->renderTargetPriv().numStencilBits() == 8); |
Stephen White | f813ef7 | 2019-08-09 12:28:37 -0400 | [diff] [blame] | 360 | } |
Robert Phillips | a87c529 | 2019-11-12 10:12:42 -0500 | [diff] [blame] | 361 | #endif |
| 362 | depthStencilState = create_depth_stencil_state(programInfo, depthStencilFormat); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 363 | |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 364 | std::vector<wgpu::VertexBufferLayoutDescriptor> inputs; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 365 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 366 | std::vector<wgpu::VertexAttributeDescriptor> vertexAttributes; |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 367 | const GrPrimitiveProcessor& primProc = programInfo.primProc(); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 368 | if (primProc.numVertexAttributes() > 0) { |
| 369 | size_t offset = 0; |
| 370 | int i = 0; |
| 371 | for (const auto& attrib : primProc.vertexAttributes()) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 372 | wgpu::VertexAttributeDescriptor attribute; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 373 | attribute.shaderLocation = i; |
| 374 | attribute.offset = offset; |
| 375 | attribute.format = to_dawn_vertex_format(attrib.cpuType()); |
| 376 | vertexAttributes.push_back(attribute); |
| 377 | offset += attrib.sizeAlign4(); |
| 378 | i++; |
| 379 | } |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 380 | wgpu::VertexBufferLayoutDescriptor input; |
| 381 | input.arrayStride = offset; |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 382 | input.stepMode = wgpu::InputStepMode::Vertex; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 383 | input.attributeCount = vertexAttributes.size(); |
| 384 | input.attributes = &vertexAttributes.front(); |
| 385 | inputs.push_back(input); |
| 386 | } |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 387 | std::vector<wgpu::VertexAttributeDescriptor> instanceAttributes; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 388 | if (primProc.numInstanceAttributes() > 0) { |
| 389 | size_t offset = 0; |
| 390 | int i = 0; |
| 391 | for (const auto& attrib : primProc.instanceAttributes()) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 392 | wgpu::VertexAttributeDescriptor attribute; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 393 | attribute.shaderLocation = i; |
| 394 | attribute.offset = offset; |
| 395 | attribute.format = to_dawn_vertex_format(attrib.cpuType()); |
| 396 | instanceAttributes.push_back(attribute); |
| 397 | offset += attrib.sizeAlign4(); |
| 398 | i++; |
| 399 | } |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 400 | wgpu::VertexBufferLayoutDescriptor input; |
| 401 | input.arrayStride = offset; |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 402 | input.stepMode = wgpu::InputStepMode::Instance; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 403 | input.attributeCount = instanceAttributes.size(); |
| 404 | input.attributes = &instanceAttributes.front(); |
| 405 | inputs.push_back(input); |
| 406 | } |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 407 | wgpu::VertexStateDescriptor vertexState; |
| 408 | vertexState.indexFormat = wgpu::IndexFormat::Uint16; |
| 409 | vertexState.vertexBufferCount = inputs.size(); |
| 410 | vertexState.vertexBuffers = &inputs.front(); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 411 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 412 | wgpu::ProgrammableStageDescriptor vsDesc; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 413 | vsDesc.module = vsModule; |
| 414 | vsDesc.entryPoint = "main"; |
| 415 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 416 | wgpu::ProgrammableStageDescriptor fsDesc; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 417 | fsDesc.module = fsModule; |
| 418 | fsDesc.entryPoint = "main"; |
| 419 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 420 | wgpu::RenderPipelineDescriptor rpDesc; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 421 | rpDesc.layout = pipelineLayout; |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 422 | rpDesc.vertexStage = vsDesc; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 423 | rpDesc.fragmentStage = &fsDesc; |
Sean Gilhuly | 6c536a5 | 2019-11-11 11:13:03 -0500 | [diff] [blame] | 424 | rpDesc.vertexState = &vertexState; |
Robert Phillips | fcaae48 | 2019-11-07 10:17:03 -0500 | [diff] [blame] | 425 | rpDesc.primitiveTopology = to_dawn_primitive_topology(programInfo.primitiveType()); |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 426 | if (hasDepthStencil) { |
| 427 | rpDesc.depthStencilState = &depthStencilState; |
| 428 | } |
| 429 | rpDesc.colorStateCount = 1; |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 430 | rpDesc.colorStates = &colorState; |
Stephen White | e264131 | 2019-08-29 15:10:50 -0400 | [diff] [blame] | 431 | result->fRenderPipeline = gpu->device().CreateRenderPipeline(&rpDesc); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 432 | return result; |
| 433 | } |
| 434 | |
| 435 | GrDawnProgramBuilder::GrDawnProgramBuilder(GrDawnGpu* gpu, |
| 436 | GrRenderTarget* renderTarget, |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 437 | const GrProgramInfo& programInfo, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 438 | GrProgramDesc* desc) |
Stephen White | 511af2e | 2020-02-07 11:04:58 -0500 | [diff] [blame] | 439 | : INHERITED(renderTarget, *desc, programInfo) |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 440 | , fGpu(gpu) |
| 441 | , fVaryingHandler(this) |
| 442 | , fUniformHandler(this) { |
| 443 | } |
| 444 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 445 | wgpu::ShaderModule GrDawnProgramBuilder::createShaderModule(const GrGLSLShaderBuilder& builder, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 446 | SkSL::Program::Kind kind, |
Stephen White | 20c626a | 2019-10-15 13:35:37 -0400 | [diff] [blame] | 447 | bool flipY, |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 448 | SkSL::Program::Inputs* inputs) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 449 | wgpu::Device device = fGpu->device(); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 450 | SkString source(builder.fCompilerString.c_str()); |
| 451 | |
| 452 | #if 0 |
| 453 | SkSL::String sksl = GrShaderUtils::PrettyPrint(builder.fCompilerString); |
| 454 | printf("converting program:\n%s\n", sksl.c_str()); |
| 455 | #endif |
| 456 | |
Stephen White | 40c47e1 | 2019-11-01 13:13:03 -0400 | [diff] [blame] | 457 | SkSL::String spirvSource = sksl_to_spirv(fGpu, source.c_str(), kind, flipY, |
| 458 | fUniformHandler.getRTHeightOffset(), inputs); |
| 459 | if (inputs->fRTHeight) { |
| 460 | this->addRTHeightUniform(SKSL_RTHEIGHT_NAME); |
| 461 | } |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 462 | |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 463 | wgpu::ShaderModuleDescriptor desc; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 464 | desc.codeSize = spirvSource.size() / 4; |
| 465 | desc.code = reinterpret_cast<const uint32_t*>(spirvSource.c_str()); |
| 466 | |
| 467 | return device.CreateShaderModule(&desc); |
| 468 | }; |
| 469 | |
| 470 | const GrCaps* GrDawnProgramBuilder::caps() const { |
| 471 | return fGpu->caps(); |
| 472 | } |
| 473 | |
| 474 | void GrDawnProgram::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) { |
| 475 | // Load the RT height uniform if it is needed to y-flip gl_FragCoord. |
| 476 | if (fBuiltinUniformHandles.fRTHeightUni.isValid() && |
| 477 | fRenderTargetState.fRenderTargetSize.fHeight != rt->height()) { |
| 478 | fDataManager.set1f(fBuiltinUniformHandles.fRTHeightUni, SkIntToScalar(rt->height())); |
| 479 | } |
| 480 | |
| 481 | // set RT adjustment |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 482 | SkISize dimensions = rt->dimensions(); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 483 | SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid()); |
| 484 | if (fRenderTargetState.fRenderTargetOrigin != origin || |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 485 | fRenderTargetState.fRenderTargetSize != dimensions) { |
| 486 | fRenderTargetState.fRenderTargetSize = dimensions; |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 487 | fRenderTargetState.fRenderTargetOrigin = origin; |
| 488 | |
| 489 | float rtAdjustmentVec[4]; |
| 490 | fRenderTargetState.getRTAdjustmentVec(rtAdjustmentVec); |
| 491 | fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec); |
| 492 | } |
| 493 | } |
| 494 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 495 | static void set_texture(GrDawnGpu* gpu, GrSamplerState state, GrTexture* texture, |
| 496 | std::vector<wgpu::BindGroupBinding>* bindings, int* binding) { |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 497 | // FIXME: could probably cache samplers in GrDawnProgram |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 498 | wgpu::Sampler sampler = gpu->getOrCreateSampler(state); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 499 | bindings->push_back(make_bind_group_binding((*binding)++, sampler)); |
| 500 | GrDawnTexture* tex = static_cast<GrDawnTexture*>(texture); |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 501 | wgpu::TextureView textureView = tex->textureView(); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 502 | bindings->push_back(make_bind_group_binding((*binding)++, textureView)); |
| 503 | } |
| 504 | |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 505 | wgpu::BindGroup GrDawnProgram::setUniformData(GrDawnGpu* gpu, const GrRenderTarget* renderTarget, |
| 506 | const GrProgramInfo& programInfo) { |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 507 | std::vector<wgpu::BindGroupBinding> bindings; |
Stephen White | dd78efd | 2019-10-23 15:00:20 -0400 | [diff] [blame] | 508 | GrDawnRingBuffer::Slice slice; |
| 509 | uint32_t uniformBufferSize = fDataManager.uniformBufferSize(); |
| 510 | if (0 != uniformBufferSize) { |
| 511 | slice = gpu->allocateUniformRingBufferSlice(uniformBufferSize); |
| 512 | bindings.push_back(make_bind_group_binding(GrDawnUniformHandler::kUniformBinding, |
| 513 | slice.fBuffer, slice.fOffset, |
| 514 | uniformBufferSize)); |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 515 | } |
Stephen White | 729c78d | 2019-10-14 12:42:59 -0400 | [diff] [blame] | 516 | this->setRenderTargetState(renderTarget, programInfo.origin()); |
| 517 | const GrPipeline& pipeline = programInfo.pipeline(); |
| 518 | const GrPrimitiveProcessor& primProc = programInfo.primProc(); |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 519 | GrFragmentProcessor::PipelineCoordTransformRange transformRange(pipeline); |
| 520 | fGeometryProcessor->setData(fDataManager, primProc, transformRange); |
Brian Salomon | 7eabfe8 | 2019-12-02 14:20:20 -0500 | [diff] [blame] | 521 | GrFragmentProcessor::CIter fpIter(pipeline); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 522 | GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt); |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 523 | for (; fpIter && glslIter; ++fpIter, ++glslIter) { |
| 524 | glslIter->setData(fDataManager, *fpIter); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 525 | } |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 526 | SkIPoint offset; |
| 527 | GrTexture* dstTexture = pipeline.peekDstTexture(&offset); |
| 528 | fXferProcessor->setData(fDataManager, pipeline.getXferProcessor(), dstTexture, offset); |
Stephen White | 8eda499 | 2020-03-17 11:44:48 -0400 | [diff] [blame] | 529 | if (0 != uniformBufferSize) { |
| 530 | fDataManager.uploadUniformBuffers(slice.fData); |
| 531 | } |
Stephen White | 3cc8d4f | 2019-10-30 09:56:23 -0400 | [diff] [blame] | 532 | wgpu::BindGroupDescriptor descriptor; |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 533 | descriptor.layout = fBindGroupLayouts[0]; |
| 534 | descriptor.bindingCount = bindings.size(); |
| 535 | descriptor.bindings = bindings.data(); |
| 536 | return gpu->device().CreateBindGroup(&descriptor); |
| 537 | } |
| 538 | |
| 539 | wgpu::BindGroup GrDawnProgram::setTextures(GrDawnGpu* gpu, |
Stephen White | 6e8ceee | 2020-02-25 16:25:43 -0500 | [diff] [blame] | 540 | const GrPrimitiveProcessor& primProc, |
| 541 | const GrPipeline& pipeline, |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 542 | const GrSurfaceProxy* const primProcTextures[]) { |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 543 | std::vector<wgpu::BindGroupBinding> bindings; |
| 544 | int binding = 0; |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 545 | if (primProcTextures) { |
| 546 | for (int i = 0; i < primProc.numTextureSamplers(); ++i) { |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 547 | SkASSERT(primProcTextures[i]->asTextureProxy()); |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 548 | auto& sampler = primProc.textureSampler(i); |
| 549 | set_texture(gpu, sampler.samplerState(), primProcTextures[i]->peekTexture(), &bindings, |
| 550 | &binding); |
| 551 | } |
| 552 | } |
Stephen White | a521c96 | 2019-12-04 09:57:48 -0500 | [diff] [blame] | 553 | GrFragmentProcessor::CIter fpIter(pipeline); |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 554 | GrGLSLFragmentProcessor::Iter glslIter(fFragmentProcessors.get(), fFragmentProcessorCnt); |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 555 | for (; fpIter && glslIter; ++fpIter, ++glslIter) { |
| 556 | for (int i = 0; i < fpIter->numTextureSamplers(); ++i) { |
| 557 | auto& s = fpIter->textureSampler(i); |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 558 | set_texture(gpu, s.samplerState(), s.peekTexture(), &bindings, &binding); |
| 559 | } |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 560 | } |
| 561 | SkIPoint offset; |
| 562 | if (GrTexture* dstTexture = pipeline.peekDstTexture(&offset)) { |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 563 | set_texture(gpu, GrSamplerState::Filter::kNearest, dstTexture, &bindings, &binding); |
Stephen White | 7cb52cb | 2019-11-12 10:40:27 -0500 | [diff] [blame] | 564 | } |
| 565 | wgpu::BindGroupDescriptor descriptor; |
| 566 | descriptor.layout = fBindGroupLayouts[1]; |
Stephen White | b7eaedc | 2019-08-21 09:48:05 -0400 | [diff] [blame] | 567 | descriptor.bindingCount = bindings.size(); |
| 568 | descriptor.bindings = bindings.data(); |
| 569 | return gpu->device().CreateBindGroup(&descriptor); |
Stephen White | bb6bed1 | 2019-08-02 09:57:55 -0400 | [diff] [blame] | 570 | } |