Debian Codegraph @

src/gpu/graphite/render/TessellateStrokesRenderStep.cpp

raw
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/TessellateStrokesRenderStep.h"
9
10 #include "include/core/SkM44.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkStrokeRec.h"
14 #include "include/private/base/SkAssert.h"
15 #include "include/private/base/SkDebug.h"
16 #include "include/private/base/SkPoint_impl.h"
17 #include "include/private/base/SkSpan_impl.h"
18 #include "src/base/SkEnumBitMask.h"
19 #include "src/base/SkVx.h"
20 #include "src/core/SkGeometry.h"
21 #include "src/core/SkSLTypeShared.h"
22 #include "src/gpu/graphite/Attribute.h"
23 #include "src/gpu/graphite/DrawOrder.h"
24 #include "src/gpu/graphite/DrawParams.h"
25 #include "src/gpu/graphite/DrawTypes.h"
26 #include "src/gpu/graphite/PipelineData.h"
27 #include "src/gpu/graphite/ResourceTypes.h"
28 #include "src/gpu/graphite/geom/Geometry.h"
29 #include "src/gpu/graphite/geom/Shape.h"
30 #include "src/gpu/graphite/geom/Transform.h"
31 #include "src/gpu/graphite/render/CommonDepthStencilSettings.h"
32 #include "src/gpu/graphite/render/DynamicInstancesPatchAllocator.h"
33 #include "src/gpu/tessellate/FixedCountBufferUtils.h"
34 #include "src/gpu/tessellate/PatchWriter.h"
35 #include "src/gpu/tessellate/StrokeIterator.h"
36 #include "src/gpu/tessellate/Tessellation.h"
37 #include "src/gpu/tessellate/WangsFormula.h"
38 #include "src/sksl/SkSLString.h"
39
40 namespace skgpu::graphite {
41
42 namespace {
43
44 using namespace skgpu::tess;
45
46 // Always use dynamic stroke params and join control points, track the join control point in
47 // PatchWriter and replicate line end points (match Ganesh's shader behavior).
48 //
49 // No explicit curve type on platforms that support infinity.
50 // No color or wide color attribs, since it might always be part of the PaintParams
51 // or we'll add a color-only fast path to RenderStep later.
52 static constexpr PatchAttribs kAttribs = PatchAttribs::kJoinControlPoint |
53 PatchAttribs::kStrokeParams |
54 PatchAttribs::kPaintDepth |
55 PatchAttribs::kSsboIndex;
56 static constexpr PatchAttribs kAttribsWithCurveType = kAttribs | PatchAttribs::kExplicitCurveType;
57 using Writer = PatchWriter<DynamicInstancesPatchAllocator<FixedCountStrokes>,
58 Required<PatchAttribs::kJoinControlPoint>,
59 Required<PatchAttribs::kStrokeParams>,
60 Required<PatchAttribs::kPaintDepth>,
61 Required<PatchAttribs::kSsboIndex>,
62 Optional<PatchAttribs::kExplicitCurveType>,
63 ReplicateLineEndPoints,
64 TrackJoinControlPoints>;
65
66 // The order of the attribute declarations must match the order used by
67 // PatchWriter::emitPatchAttribs, i.e.:
68 // join << fanPoint << stroke << color << depth << curveType << ssboIndex
69 static constexpr Attribute kBaseAttributes[] = {
70 {"p01", VertexAttribType::kFloat4, SkSLType::kFloat4},
71 {"p23", VertexAttribType::kFloat4, SkSLType::kFloat4},
72 {"prevPoint", VertexAttribType::kFloat2, SkSLType::kFloat2},
73 {"stroke", VertexAttribType::kFloat2, SkSLType::kFloat2},
74 {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
75 {"ssboIndex", VertexAttribType::kUInt, SkSLType::kUInt}};
76
77 static constexpr Attribute kAttributesWithCurveType[] = {
78 {"p01", VertexAttribType::kFloat4, SkSLType::kFloat4},
79 {"p23", VertexAttribType::kFloat4, SkSLType::kFloat4},
80 {"prevPoint", VertexAttribType::kFloat2, SkSLType::kFloat2},
81 {"stroke", VertexAttribType::kFloat2, SkSLType::kFloat2},
82 {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
83 {"curveType", VertexAttribType::kFloat, SkSLType::kFloat},
84 {"ssboIndex", VertexAttribType::kUInt, SkSLType::kUInt}};
85
86 static constexpr SkSpan<const Attribute> kAttributes[2] = {kAttributesWithCurveType,
87 kBaseAttributes};
88
89 } // namespace
90
91 TessellateStrokesRenderStep::TessellateStrokesRenderStep(Layout layout, bool infinitySupport)
92 : RenderStep(layout,
93 RenderStepID::kTessellateStrokes,
94 Flags::kRequiresMSAA | Flags::kPerformsShading |
95 Flags::kAppendDynamicInstances,
96 /*uniforms=*/{{"affineMatrix", SkSLType::kFloat4},
97 {"translate", SkSLType::kFloat2},
98 {"maxScale", SkSLType::kFloat}},
99 PrimitiveType::kTriangleStrip,
100 kDirectDepthLessPass,
101 /*staticAttrs=*/ {},
102 /*appendAttrs=*/kAttributes[infinitySupport])
103 , fInfinitySupport(infinitySupport) {}
104
105 TessellateStrokesRenderStep::~TessellateStrokesRenderStep() {}
106
107 std::string TessellateStrokesRenderStep::vertexSkSL() const {
108 // TODO: Assumes vertex ID support for now, max edges must equal
109 // skgpu::tess::FixedCountStrokes::kMaxEdges -> (2^14 - 1) -> 16383
110 return SkSL::String::printf(
111 "float edgeID = float(sk_VertexID >> 1);\n"
112 "if ((sk_VertexID & 1) != 0) {"
113 "edgeID = -edgeID;"
114 "}\n"
115 "float2x2 affine = float2x2(affineMatrix.xy, affineMatrix.zw);\n"
116 "float4 devAndLocalCoords = tessellate_stroked_curve("
117 "edgeID, 16383, affine, translate, maxScale, p01, p23, prevPoint,"
118 "stroke, %s);\n"
119 "float4 devPosition = float4(devAndLocalCoords.xy, depth, 1.0);\n"
120 "stepLocalCoords = devAndLocalCoords.zw;\n",
121 fInfinitySupport ? "curve_type_using_inf_support(p23)" : "curveType");
122 }
123
124 void TessellateStrokesRenderStep::writeVertices(DrawWriter* dw,
125 const DrawParams& params,
126 uint32_t ssboIndex) const {
127 SkPath path = params.geometry().shape().asPath(); // TODO: Iterate the Shape directly
128
129 int patchReserveCount = FixedCountStrokes::PreallocCount(path.countVerbs());
130 // Stroke tessellation does not use fixed indices or vertex data, and only needs the vertex ID
131 static const BindBufferInfo kNullBinding = {};
132 // TODO: All HW that Graphite will run on should support instancing ith sk_VertexID, but when
133 // we support Vulkan+Swiftshader, we will need the vertex buffer ID fallback unless Swiftshader
134 // has figured out how to support vertex IDs before then.
135 Writer writer{fInfinitySupport ? kAttribs : kAttribsWithCurveType,
136 *dw,
137 kNullBinding,
138 kNullBinding,
139 patchReserveCount};
140 writer.updatePaintDepthAttrib(params.order().depthAsFloat());
141 writer.updateSsboIndexAttrib(ssboIndex);
142
143 // The vector xform approximates how the control points are transformed by the shader to
144 // more accurately compute how many *parametric* segments are needed.
145 // getMaxScale() returns -1 if it can't compute a scale factor (e.g. perspective), taking the
146 // absolute value automatically converts that to an identity scale factor for our purposes.
147 writer.setShaderTransform(wangs_formula::VectorXform{params.transform().matrix()},
148 params.transform().maxScaleFactor());
149
150 SkASSERT(params.isStroke());
151 writer.updateStrokeParamsAttrib({params.strokeStyle().halfWidth(),
152 params.strokeStyle().joinLimit()});
153
154 for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) {
155 switch (verb) {
156 case SkPathVerb::kMove:
157 // This automatically joins the last contour with the first contour (deferred) if
158 // the contour is closed. If the contour is not closed, it automatically adds
159 // additional patches for the end cap of the last patch and the beginning cap of the
160 // deferred patch. This does nothing if this is the beginning of the first contour.
161 writer.writeDeferredStrokePatch(pts[0], params.strokeStyle().cap());
162 break;
163
164 case SkPathVerb::kClose:
165 // Draws a line back to the starting point of the contour and writes any deferred
166 // patch with a join (instead of caps). Or if the contour was empty, draws a cap.
167 // Since any deferred patch is consumed, the next moveTo's writeDeferredStrokePatch
168 // will do nothing but record the beginning of the new contour.
169 writer.closeDeferredStrokePatch(params.strokeStyle().cap());
170 break;
171
172 case SkPathVerb::kLine:
173 writer.writeLine(pts[0], pts[1]);
174 break;
175
176 case SkPathVerb::kQuad:
177 if (ConicHasCusp(pts)) {
178 // The cusp is always at the midtangent.
179 SkPoint cusp = SkEvalQuadAt(pts, SkFindQuadMidTangent(pts));
180 writer.writeCircle(cusp);
181 // A quad can only have a cusp if it's flat with a 180-degree turnaround.
182 writer.writeLine(pts[0], cusp);
183 writer.writeLine(cusp, pts[2]);
184 } else {
185 writer.writeQuadratic(pts);
186 }
187 break;
188
189 case SkPathVerb::kConic:
190 if (ConicHasCusp(pts)) {
191 // The cusp is always at the midtangent.
192 SkConic conic(pts, *w);
193 SkPoint cusp = conic.evalAt(conic.findMidTangent());
194 writer.writeCircle(cusp);
195 // A conic can only have a cusp if it's flat with a 180-degree turnaround.
196 writer.writeLine(pts[0], cusp);
197 writer.writeLine(cusp, pts[2]);
198 } else {
199 writer.writeConic(pts, *w);
200 }
201 break;
202
203 case SkPathVerb::kCubic: {
204 SkPoint chops[10];
205 float T[2];
206 bool areCusps;
207 int numChops = FindCubicConvex180Chops(pts, T, &areCusps);
208 if (numChops == 0) {
209 writer.writeCubic(pts);
210 } else if (numChops == 1) {
211 SkChopCubicAt(pts, chops, T[0]);
212 if (areCusps) {
213 writer.writeCircle(chops[3]);
214 // In a perfect world, these 3 points would be be equal after chopping
215 // on a cusp.
216 chops[2] = chops[4] = chops[3];
217 }
218 writer.writeCubic(chops);
219 writer.writeCubic(chops + 3);
220 } else {
221 SkASSERT(numChops == 2);
222 SkChopCubicAt(pts, chops, T[0], T[1]);
223 if (areCusps) {
224 writer.writeCircle(chops[3]);
225 writer.writeCircle(chops[6]);
226 // Two cusps are only possible if it's a flat line with two 180-degree
227 // turnarounds.
228 writer.writeLine(chops[0], chops[3]);
229 writer.writeLine(chops[3], chops[6]);
230 writer.writeLine(chops[6], chops[9]);
231 } else {
232 writer.writeCubic(chops);
233 writer.writeCubic(chops + 3);
234 writer.writeCubic(chops + 6);
235 }
236 }
237 break;
238 }
239 }
240 }
241
242 // Finish the last contour (next moveTo point doesn't matter)
243 writer.writeDeferredStrokePatch({0.f, 0.f}, params.strokeStyle().cap());
244 }
245
246 void TessellateStrokesRenderStep::writeUniformsAndTextures(const DrawParams& params,
247 PipelineDataGatherer* gatherer) const {
248 SkDEBUGCODE(gatherer->checkRewind());
249 // TODO: Implement perspective
250 SkASSERT(params.transform().type() < Transform::Type::kPerspective);
251
252 SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
253
254 // affineMatrix = float4 (2x2 of transform), translate = float2, maxScale = float
255 // Column-major 2x2 of the transform.
256 SkV4 upper = {params.transform().matrix().rc(0, 0), params.transform().matrix().rc(1, 0),
257 params.transform().matrix().rc(0, 1), params.transform().matrix().rc(1, 1)};
258 gatherer->write(upper);
259
260 gatherer->write(SkPoint{params.transform().matrix().rc(0, 3),
261 params.transform().matrix().rc(1, 3)});
262
263 gatherer->write(params.transform().maxScaleFactor());
264 }
265
266 } // namespace skgpu::graphite