xref: /aosp_15_r20/external/skia/src/gpu/graphite/render/AnalyticBlurRenderStep.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkM44.h"
11 #include "include/core/SkSamplingOptions.h"
12 #include "include/core/SkTileMode.h"
13 #include "include/private/base/SkDebug.h"
14 #include "src/base/SkEnumBitMask.h"
15 #include "src/core/SkSLTypeShared.h"
16 #include "src/gpu/BufferWriter.h"
17 #include "src/gpu/graphite/Attribute.h"
18 #include "src/gpu/graphite/ContextUtils.h"
19 #include "src/gpu/graphite/DrawOrder.h"
20 #include "src/gpu/graphite/DrawParams.h"
21 #include "src/gpu/graphite/DrawTypes.h"
22 #include "src/gpu/graphite/DrawWriter.h"
23 #include "src/gpu/graphite/PipelineData.h"
24 #include "src/gpu/graphite/geom/AnalyticBlurMask.h"
25 #include "src/gpu/graphite/geom/Geometry.h"
26 #include "src/gpu/graphite/geom/Rect.h"
27 #include "src/gpu/graphite/geom/Transform_graphite.h"
28 #include "src/gpu/graphite/render/CommonDepthStencilSettings.h"
29 
30 #include <string_view>
31 
32 namespace skgpu::graphite {
33 
AnalyticBlurRenderStep()34 AnalyticBlurRenderStep::AnalyticBlurRenderStep()
35         : RenderStep("AnalyticBlurRenderStep",
36                      "",
37                      Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage,
38                      /*uniforms=*/
39                      {{"localToDevice", SkSLType::kFloat4x4},
40                       {"deviceToScaledShape", SkSLType::kFloat3x3},
41                       {"shapeData", SkSLType::kFloat4},
42                       {"blurData", SkSLType::kHalf2},
43                       {"shapeType", SkSLType::kInt},
44                       {"depth", SkSLType::kFloat}},
45                      PrimitiveType::kTriangles,
46                      kDirectDepthGreaterPass,
47                      /*vertexAttrs=*/
48                      {{"position", VertexAttribType::kFloat2, SkSLType::kFloat2},
49                       {"ssboIndices", VertexAttribType::kUInt2, SkSLType::kUInt2}},
50                      /*instanceAttrs=*/{},
51                      /*varyings=*/
52                      // scaledShapeCoords are the fragment coordinates in local shape space, where
53                      // the shape has been scaled to device space but not translated or rotated.
54                      {{"scaledShapeCoords", SkSLType::kFloat2}}) {}
55 
vertexSkSL() const56 std::string AnalyticBlurRenderStep::vertexSkSL() const {
57     return R"(
58         float4 devPosition = localToDevice * float4(position, depth, 1.0);
59         stepLocalCoords = position;
60         scaledShapeCoords = (deviceToScaledShape * devPosition.xy1).xy;
61     )";
62 }
63 
texturesAndSamplersSkSL(const ResourceBindingRequirements & bindingReqs,int * nextBindingIndex) const64 std::string AnalyticBlurRenderStep::texturesAndSamplersSkSL(
65         const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const {
66     return EmitSamplerLayout(bindingReqs, nextBindingIndex) + " sampler2D s;";
67 }
68 
fragmentCoverageSkSL() const69 const char* AnalyticBlurRenderStep::fragmentCoverageSkSL() const {
70     return "outputCoverage = blur_coverage_fn(scaledShapeCoords, "
71                                              "shapeData, "
72                                              "blurData, "
73                                              "shapeType, "
74                                              "s);";
75 }
76 
writeVertices(DrawWriter * writer,const DrawParams & params,skvx::uint2 ssboIndices) const77 void AnalyticBlurRenderStep::writeVertices(DrawWriter* writer,
78                                            const DrawParams& params,
79                                            skvx::uint2 ssboIndices) const {
80     const Rect& r = params.geometry().analyticBlurMask().drawBounds();
81     DrawWriter::Vertices verts{*writer};
82     verts.append(6) << skvx::float2(r.left(), r.top()) << ssboIndices
83                     << skvx::float2(r.right(), r.top()) << ssboIndices
84                     << skvx::float2(r.left(), r.bot()) << ssboIndices
85                     << skvx::float2(r.right(), r.top()) << ssboIndices
86                     << skvx::float2(r.right(), r.bot()) << ssboIndices
87                     << skvx::float2(r.left(), r.bot()) << ssboIndices;
88 }
89 
writeUniformsAndTextures(const DrawParams & params,PipelineDataGatherer * gatherer) const90 void AnalyticBlurRenderStep::writeUniformsAndTextures(const DrawParams& params,
91                                                       PipelineDataGatherer* gatherer) const {
92     SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
93 
94     gatherer->write(params.transform().matrix());
95 
96     const AnalyticBlurMask& blur = params.geometry().analyticBlurMask();
97     gatherer->write(blur.deviceToScaledShape().asM33());
98     gatherer->write(blur.shapeData().asSkRect());
99     gatherer->writeHalf(blur.blurData());
100     gatherer->write(static_cast<int>(blur.shapeType()));
101     gatherer->write(params.order().depthAsFloat());
102 
103     SkSamplingOptions samplingOptions = blur.shapeType() == AnalyticBlurMask::ShapeType::kRect
104                                                 ? SkFilterMode::kLinear
105                                                 : SkFilterMode::kNearest;
106     gatherer->add(blur.refProxy(), {samplingOptions, SkTileMode::kClamp});
107 }
108 
109 }  // namespace skgpu::graphite
110