• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
24 #include "src/gpu/graphite/render/CommonDepthStencilSettings.h"
25 #include "src/gpu/tessellate/MiddleOutPolygonTriangulator.h"
26 
27 #include <algorithm>
28 
29 namespace skgpu::graphite {
30 
MiddleOutFanRenderStep(bool evenOdd)31 MiddleOutFanRenderStep::MiddleOutFanRenderStep(bool evenOdd)
32         : RenderStep(evenOdd ? RenderStepID::kMiddleOutFan_EvenOdd
33                              : RenderStepID::kMiddleOutFan_Winding,
34                      Flags::kRequiresMSAA,
35                      /*uniforms=*/{{"localToDevice", SkSLType::kFloat4x4}},
36                      PrimitiveType::kTriangles,
37                      evenOdd ? kEvenOddStencilPass : kWindingStencilPass,
38                      /*vertexAttrs=*/
39                             {{"position", VertexAttribType::kFloat2, SkSLType::kFloat2},
40                              {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
41                              {"ssboIndices", VertexAttribType::kUInt2, SkSLType::kUInt2}},
42                      /*instanceAttrs=*/{}) {}
43 
~MiddleOutFanRenderStep()44 MiddleOutFanRenderStep::~MiddleOutFanRenderStep() {}
45 
vertexSkSL() const46 std::string MiddleOutFanRenderStep::vertexSkSL() const {
47     return
48         "float4 devPosition = localToDevice * float4(position, 0.0, 1.0);\n"
49         "devPosition.z = depth;\n"
50         "stepLocalCoords = position;\n";
51 }
52 
writeVertices(DrawWriter * writer,const DrawParams & params,skvx::uint2 ssboIndices) const53 void MiddleOutFanRenderStep::writeVertices(DrawWriter* writer,
54                                            const DrawParams& params,
55                                            skvx::uint2 ssboIndices) const {
56     // TODO: Have Shape provide a path-like iterator so we don't actually have to convert non
57     // paths to SkPath just to iterate their pts/verbs
58     SkPath path = params.geometry().shape().asPath();
59 
60     const int maxTrianglesInFans = std::max(path.countVerbs() - 2, 0);
61 
62     float depth = params.order().depthAsFloat();
63 
64     DrawWriter::Vertices verts{*writer};
65     verts.reserve(maxTrianglesInFans * 3);
66     for (tess::PathMiddleOutFanIter it(path); !it.done();) {
67         for (auto [p0, p1, p2] : it.nextStack()) {
68             verts.append(3) << p0 << depth << ssboIndices
69                             << p1 << depth << ssboIndices
70                             << p2 << depth << ssboIndices;
71         }
72     }
73 }
74 
writeUniformsAndTextures(const DrawParams & params,PipelineDataGatherer * gatherer) const75 void MiddleOutFanRenderStep::writeUniformsAndTextures(const DrawParams& params,
76                                                       PipelineDataGatherer* gatherer) const {
77     SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
78 
79     gatherer->write(params.transform().matrix());
80 }
81 
82 }  // namespace skgpu::graphite
83