1 /*
2 * Copyright 2024 Google LLC
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/graphite/render/AnalyticBlurRenderStep.h"
9
10 #include "src/gpu/graphite/ContextUtils.h"
11 #include "src/gpu/graphite/DrawParams.h"
12 #include "src/gpu/graphite/DrawWriter.h"
13 #include "src/gpu/graphite/PipelineData.h"
14 #include "src/gpu/graphite/render/CommonDepthStencilSettings.h"
15
16 namespace skgpu::graphite {
17
AnalyticBlurRenderStep()18 AnalyticBlurRenderStep::AnalyticBlurRenderStep()
19 : RenderStep("AnalyticBlurRenderStep",
20 "",
21 Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage,
22 /*uniforms=*/
23 {{"localToDevice", SkSLType::kFloat4x4},
24 {"deviceToScaledShape", SkSLType::kFloat3x3},
25 {"shapeData", SkSLType::kFloat4},
26 {"blurData", SkSLType::kHalf2},
27 {"shapeType", SkSLType::kInt},
28 {"depth", SkSLType::kFloat}},
29 PrimitiveType::kTriangles,
30 kDirectDepthGreaterPass,
31 /*vertexAttrs=*/
32 {{"position", VertexAttribType::kFloat2, SkSLType::kFloat2},
33 {"ssboIndices", VertexAttribType::kUShort2, SkSLType::kUShort2}},
34 /*instanceAttrs=*/{},
35 /*varyings=*/
36 // scaledShapeCoords are the fragment coordinates in local shape space, where
37 // the shape has been scaled to device space but not translated or rotated.
38 {{"scaledShapeCoords", SkSLType::kFloat2}}) {}
39
vertexSkSL() const40 std::string AnalyticBlurRenderStep::vertexSkSL() const {
41 return R"(
42 float4 devPosition = localToDevice * float4(position, depth, 1.0);
43 stepLocalCoords = position;
44 scaledShapeCoords = (deviceToScaledShape * devPosition.xy1).xy;
45 )";
46 }
47
texturesAndSamplersSkSL(const ResourceBindingRequirements & bindingReqs,int * nextBindingIndex) const48 std::string AnalyticBlurRenderStep::texturesAndSamplersSkSL(
49 const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const {
50 return EmitSamplerLayout(bindingReqs, nextBindingIndex) + " sampler2D s;";
51 }
52
fragmentCoverageSkSL() const53 const char* AnalyticBlurRenderStep::fragmentCoverageSkSL() const {
54 return "outputCoverage = blur_coverage_fn(scaledShapeCoords, "
55 "shapeData, "
56 "blurData, "
57 "shapeType, "
58 "s);";
59 }
60
writeVertices(DrawWriter * writer,const DrawParams & params,skvx::ushort2 ssboIndices) const61 void AnalyticBlurRenderStep::writeVertices(DrawWriter* writer,
62 const DrawParams& params,
63 skvx::ushort2 ssboIndices) const {
64 const Rect& r = params.geometry().analyticBlurMask().drawBounds();
65 DrawWriter::Vertices verts{*writer};
66 verts.append(6) << skvx::float2(r.left(), r.top()) << ssboIndices
67 << skvx::float2(r.right(), r.top()) << ssboIndices
68 << skvx::float2(r.left(), r.bot()) << ssboIndices
69 << skvx::float2(r.right(), r.top()) << ssboIndices
70 << skvx::float2(r.right(), r.bot()) << ssboIndices
71 << skvx::float2(r.left(), r.bot()) << ssboIndices;
72 }
73
writeUniformsAndTextures(const DrawParams & params,PipelineDataGatherer * gatherer) const74 void AnalyticBlurRenderStep::writeUniformsAndTextures(const DrawParams& params,
75 PipelineDataGatherer* gatherer) const {
76 SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
77
78 gatherer->write(params.transform().matrix());
79
80 const AnalyticBlurMask& blur = params.geometry().analyticBlurMask();
81 gatherer->write(blur.deviceToScaledShape().asM33());
82 gatherer->write(blur.shapeData().asSkRect());
83 gatherer->writeHalf(blur.blurData());
84 gatherer->write(static_cast<int>(blur.shapeType()));
85 gatherer->write(params.order().depthAsFloat());
86
87 SkSamplingOptions samplingOptions = blur.shapeType() == AnalyticBlurMask::ShapeType::kRect
88 ? SkFilterMode::kLinear
89 : SkFilterMode::kNearest;
90 constexpr SkTileMode kTileModes[2] = {SkTileMode::kClamp, SkTileMode::kClamp};
91 gatherer->add(samplingOptions, kTileModes, blur.refProxy());
92 }
93
94 } // namespace skgpu::graphite
95