xref: /aosp_15_r20/external/skia/src/gpu/graphite/render/MiddleOutFanRenderStep.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2022 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/MiddleOutFanRenderStep.h"
9 
10 #include "include/core/SkPath.h"
11 #include "include/private/base/SkDebug.h"
12 #include "src/base/SkEnumBitMask.h"
13 #include "src/core/SkSLTypeShared.h"
14 #include "src/gpu/BufferWriter.h"
15 #include "src/gpu/graphite/Attribute.h"
16 #include "src/gpu/graphite/DrawOrder.h"
17 #include "src/gpu/graphite/DrawParams.h"
18 #include "src/gpu/graphite/DrawTypes.h"
19 #include "src/gpu/graphite/DrawWriter.h"
20 #include "src/gpu/graphite/PipelineData.h"
21 #include "src/gpu/graphite/geom/Geometry.h"
22 #include "src/gpu/graphite/geom/Shape.h"
23 #include "src/gpu/graphite/geom/Transform_graphite.h"
24 #include "src/gpu/graphite/render/CommonDepthStencilSettings.h"
25 #include "src/gpu/tessellate/MiddleOutPolygonTriangulator.h"
26 
27 #include <algorithm>
28 #include <string_view>
29 
30 namespace skgpu::graphite {
31 
MiddleOutFanRenderStep(bool evenOdd)32 MiddleOutFanRenderStep::MiddleOutFanRenderStep(bool evenOdd)
33         : RenderStep("MiddleOutFanRenderStep",
34                      evenOdd ? "even-odd" : "winding",
35                      Flags::kRequiresMSAA,
36                      /*uniforms=*/{{"localToDevice", SkSLType::kFloat4x4}},
37                      PrimitiveType::kTriangles,
38                      evenOdd ? kEvenOddStencilPass : kWindingStencilPass,
39                      /*vertexAttrs=*/
40                             {{"position", VertexAttribType::kFloat2, SkSLType::kFloat2},
41                              {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
42                              {"ssboIndices", VertexAttribType::kUInt2, SkSLType::kUInt2}},
43                      /*instanceAttrs=*/{}) {}
44 
~MiddleOutFanRenderStep()45 MiddleOutFanRenderStep::~MiddleOutFanRenderStep() {}
46 
vertexSkSL() const47 std::string MiddleOutFanRenderStep::vertexSkSL() const {
48     return R"(
49         float4 devPosition = localToDevice * float4(position, 0.0, 1.0);
50         devPosition.z = depth;
51         stepLocalCoords = position;
52     )";
53 }
54 
writeVertices(DrawWriter * writer,const DrawParams & params,skvx::uint2 ssboIndices) const55 void MiddleOutFanRenderStep::writeVertices(DrawWriter* writer,
56                                            const DrawParams& params,
57                                            skvx::uint2 ssboIndices) const {
58     // TODO: Have Shape provide a path-like iterator so we don't actually have to convert non
59     // paths to SkPath just to iterate their pts/verbs
60     SkPath path = params.geometry().shape().asPath();
61 
62     const int maxTrianglesInFans = std::max(path.countVerbs() - 2, 0);
63 
64     float depth = params.order().depthAsFloat();
65 
66     DrawWriter::Vertices verts{*writer};
67     verts.reserve(maxTrianglesInFans * 3);
68     for (tess::PathMiddleOutFanIter it(path); !it.done();) {
69         for (auto [p0, p1, p2] : it.nextStack()) {
70             verts.append(3) << p0 << depth << ssboIndices
71                             << p1 << depth << ssboIndices
72                             << p2 << depth << ssboIndices;
73         }
74     }
75 }
76 
writeUniformsAndTextures(const DrawParams & params,PipelineDataGatherer * gatherer) const77 void MiddleOutFanRenderStep::writeUniformsAndTextures(const DrawParams& params,
78                                                       PipelineDataGatherer* gatherer) const {
79     SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
80 
81     gatherer->write(params.transform().matrix());
82 }
83 
84 }  // namespace skgpu::graphite
85