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 "src/gpu/graphite/DrawParams.h"
11 #include "src/gpu/graphite/DrawWriter.h"
12 #include "src/gpu/graphite/PipelineData.h"
13 #include "src/gpu/graphite/render/CommonDepthStencilSettings.h"
14
15 #include "src/gpu/tessellate/FixedCountBufferUtils.h"
16 #include "src/gpu/tessellate/MiddleOutPolygonTriangulator.h"
17
18 namespace skgpu::graphite {
19
MiddleOutFanRenderStep(bool evenOdd)20 MiddleOutFanRenderStep::MiddleOutFanRenderStep(bool evenOdd)
21 : RenderStep("MiddleOutFanRenderStep",
22 evenOdd ? "even-odd" : "winding",
23 Flags::kRequiresMSAA,
24 /*uniforms=*/{{"localToDevice", SkSLType::kFloat4x4}},
25 PrimitiveType::kTriangles,
26 evenOdd ? kEvenOddStencilPass : kWindingStencilPass,
27 /*vertexAttrs=*/
28 {{"position", VertexAttribType::kFloat2, SkSLType::kFloat2},
29 {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
30 {"ssboIndices", VertexAttribType::kUShort2, SkSLType::kUShort2}},
31 /*instanceAttrs=*/{}) {}
32
~MiddleOutFanRenderStep()33 MiddleOutFanRenderStep::~MiddleOutFanRenderStep() {}
34
vertexSkSL() const35 std::string MiddleOutFanRenderStep::vertexSkSL() const {
36 return R"(
37 float4 devPosition = localToDevice * float4(position, 0.0, 1.0);
38 devPosition.z = depth;
39 stepLocalCoords = position;
40 )";
41 }
42
writeVertices(DrawWriter * writer,const DrawParams & params,skvx::ushort2 ssboIndices) const43 void MiddleOutFanRenderStep::writeVertices(DrawWriter* writer,
44 const DrawParams& params,
45 skvx::ushort2 ssboIndices) const {
46 // TODO: Have Shape provide a path-like iterator so we don't actually have to convert non
47 // paths to SkPath just to iterate their pts/verbs
48 SkPath path = params.geometry().shape().asPath();
49
50 const int maxTrianglesInFans = std::max(path.countVerbs() - 2, 0);
51
52 float depth = params.order().depthAsFloat();
53
54 DrawWriter::Vertices verts{*writer};
55 verts.reserve(maxTrianglesInFans * 3);
56 for (tess::PathMiddleOutFanIter it(path); !it.done();) {
57 for (auto [p0, p1, p2] : it.nextStack()) {
58 verts.append(3) << p0 << depth << ssboIndices
59 << p1 << depth << ssboIndices
60 << p2 << depth << ssboIndices;
61 }
62 }
63 }
64
writeUniformsAndTextures(const DrawParams & params,PipelineDataGatherer * gatherer) const65 void MiddleOutFanRenderStep::writeUniformsAndTextures(const DrawParams& params,
66 PipelineDataGatherer* gatherer) const {
67 SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
68
69 gatherer->write(params.transform().matrix());
70 }
71
72 } // namespace skgpu::graphite
73