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