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.h"
28 #include "src/gpu/graphite/render/CommonDepthStencilSettings.h"
29
30 namespace skgpu::graphite {
31
AnalyticBlurRenderStep()32 AnalyticBlurRenderStep::AnalyticBlurRenderStep()
33 : RenderStep(RenderStepID::kAnalyticBlur,
34 Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage,
35 /*uniforms=*/
36 {{"localToDevice", SkSLType::kFloat4x4},
37 {"deviceToScaledShape", SkSLType::kFloat3x3},
38 {"shapeData", SkSLType::kFloat4},
39 {"blurData", SkSLType::kHalf2},
40 {"shapeType", SkSLType::kInt},
41 {"depth", SkSLType::kFloat}},
42 PrimitiveType::kTriangles,
43 kDirectDepthGreaterPass,
44 /*vertexAttrs=*/
45 {{"position", VertexAttribType::kFloat2, SkSLType::kFloat2},
46 {"ssboIndices", VertexAttribType::kUInt2, SkSLType::kUInt2}},
47 /*instanceAttrs=*/{},
48 /*varyings=*/
49 // scaledShapeCoords are the fragment coordinates in local shape space, where
50 // the shape has been scaled to device space but not translated or rotated.
51 {{"scaledShapeCoords", SkSLType::kFloat2}}) {}
52
vertexSkSL() const53 std::string AnalyticBlurRenderStep::vertexSkSL() const {
54 return
55 "float4 devPosition = localToDevice * float4(position, depth, 1.0);\n"
56 "stepLocalCoords = position;\n"
57 "scaledShapeCoords = (deviceToScaledShape * devPosition.xy1).xy;\n";
58 }
59
texturesAndSamplersSkSL(const ResourceBindingRequirements & bindingReqs,int * nextBindingIndex) const60 std::string AnalyticBlurRenderStep::texturesAndSamplersSkSL(
61 const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const {
62 return EmitSamplerLayout(bindingReqs, nextBindingIndex) + " sampler2D s;";
63 }
64
fragmentCoverageSkSL() const65 const char* AnalyticBlurRenderStep::fragmentCoverageSkSL() const {
66 return "outputCoverage = blur_coverage_fn(scaledShapeCoords, "
67 "shapeData, "
68 "blurData, "
69 "shapeType, "
70 "s);";
71 }
72
writeVertices(DrawWriter * writer,const DrawParams & params,skvx::uint2 ssboIndices) const73 void AnalyticBlurRenderStep::writeVertices(DrawWriter* writer,
74 const DrawParams& params,
75 skvx::uint2 ssboIndices) const {
76 const Rect& r = params.geometry().analyticBlurMask().drawBounds();
77 DrawWriter::Vertices verts{*writer};
78 verts.append(6) << skvx::float2(r.left(), r.top()) << ssboIndices
79 << skvx::float2(r.right(), r.top()) << ssboIndices
80 << skvx::float2(r.left(), r.bot()) << ssboIndices
81 << skvx::float2(r.right(), r.top()) << ssboIndices
82 << skvx::float2(r.right(), r.bot()) << ssboIndices
83 << skvx::float2(r.left(), r.bot()) << ssboIndices;
84 }
85
writeUniformsAndTextures(const DrawParams & params,PipelineDataGatherer * gatherer) const86 void AnalyticBlurRenderStep::writeUniformsAndTextures(const DrawParams& params,
87 PipelineDataGatherer* gatherer) const {
88 SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
89
90 gatherer->write(params.transform().matrix());
91
92 const AnalyticBlurMask& blur = params.geometry().analyticBlurMask();
93 gatherer->write(blur.deviceToScaledShape().asM33());
94 gatherer->write(blur.shapeData().asSkRect());
95 gatherer->writeHalf(blur.blurData());
96 gatherer->write(static_cast<int>(blur.shapeType()));
97 gatherer->write(params.order().depthAsFloat());
98
99 SkSamplingOptions samplingOptions = blur.shapeType() == AnalyticBlurMask::ShapeType::kRect
100 ? SkFilterMode::kLinear
101 : SkFilterMode::kNearest;
102 gatherer->add(blur.refProxy(), {samplingOptions, SkTileMode::kClamp});
103 }
104
105 } // namespace skgpu::graphite
106