blob: d58c1b5118e35aebb742365e31f9742294943807 [file] [log] [blame]
Chris Dalton90e8fb12017-12-22 02:24:53 -07001/*
2 * Copyright 2017 Google Inc.
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ccpr/GrVSCoverageProcessor.h"
Chris Dalton90e8fb12017-12-22 02:24:53 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrMesh.h"
11#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton90e8fb12017-12-22 02:24:53 -070012
Chris Dalton8dfc70f2018-03-26 19:15:22 -060013// This class implements the coverage processor with vertex shaders.
Chris Dalton2c5e0112019-03-29 13:14:18 -050014class GrVSCoverageProcessor::Impl : public GrGLSLGeometryProcessor {
Chris Dalton8dfc70f2018-03-26 19:15:22 -060015public:
Chris Dalton2c5e0112019-03-29 13:14:18 -050016 Impl(std::unique_ptr<Shader> shader, int numSides)
Chris Dalton8dfc70f2018-03-26 19:15:22 -060017 : fShader(std::move(shader)), fNumSides(numSides) {}
Chris Daltonfe462ef2018-03-08 15:54:01 +000018
Chris Dalton8dfc70f2018-03-26 19:15:22 -060019private:
Chris Daltonfe462ef2018-03-08 15:54:01 +000020 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
21 FPCoordTransformIter&& transformIter) final {
22 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
23 }
24
Chris Dalton8dfc70f2018-03-26 19:15:22 -060025 void onEmitCode(EmitArgs&, GrGPArgs*) override;
Chris Daltonfe462ef2018-03-08 15:54:01 +000026
27 const std::unique_ptr<Shader> fShader;
Chris Dalton8dfc70f2018-03-26 19:15:22 -060028 const int fNumSides;
Chris Daltonfe462ef2018-03-08 15:54:01 +000029};
30
Brian Salomon92be2f72018-06-19 14:33:47 -040031static constexpr int kInstanceAttribIdx_X = 0; // Transposed X values of all input points.
32static constexpr int kInstanceAttribIdx_Y = 1; // Transposed Y values of all input points.
Chris Dalton8dfc70f2018-03-26 19:15:22 -060033
34// Vertex data tells the shader how to offset vertices for conservative raster, as well as how to
35// calculate coverage values for corners and edges.
Chris Dalton8738cf42018-03-09 11:57:40 -070036static constexpr int kVertexData_LeftNeighborIdShift = 10;
37static constexpr int kVertexData_RightNeighborIdShift = 8;
38static constexpr int kVertexData_BloatIdxShift = 6;
39static constexpr int kVertexData_InvertNegativeCoverageBit = 1 << 5;
40static constexpr int kVertexData_IsCornerBit = 1 << 4;
Chris Dalton0a793812018-03-07 11:18:30 -070041static constexpr int kVertexData_IsEdgeBit = 1 << 3;
42static constexpr int kVertexData_IsHullBit = 1 << 2;
43
Chris Dalton0a793812018-03-07 11:18:30 -070044static constexpr int32_t pack_vertex_data(int32_t leftNeighborID, int32_t rightNeighborID,
45 int32_t bloatIdx, int32_t cornerID,
46 int32_t extraData = 0) {
47 return (leftNeighborID << kVertexData_LeftNeighborIdShift) |
48 (rightNeighborID << kVertexData_RightNeighborIdShift) |
49 (bloatIdx << kVertexData_BloatIdxShift) |
50 cornerID | extraData;
Chris Dalton90e8fb12017-12-22 02:24:53 -070051}
52
Chris Dalton0a793812018-03-07 11:18:30 -070053static constexpr int32_t hull_vertex_data(int32_t cornerID, int32_t bloatIdx, int n) {
54 return pack_vertex_data((cornerID + n - 1) % n, (cornerID + 1) % n, bloatIdx, cornerID,
55 kVertexData_IsHullBit);
Chris Dalton90e8fb12017-12-22 02:24:53 -070056}
57
Chris Daltonbaf3e782018-03-08 15:55:58 +000058static constexpr int32_t edge_vertex_data(int32_t edgeID, int32_t endptIdx, int32_t bloatIdx,
59 int n) {
60 return pack_vertex_data(0 == endptIdx ? (edgeID + 1) % n : edgeID,
61 0 == endptIdx ? (edgeID + 1) % n : edgeID,
62 bloatIdx, 0 == endptIdx ? edgeID : (edgeID + 1) % n,
63 kVertexData_IsEdgeBit |
Chris Dalton8738cf42018-03-09 11:57:40 -070064 (!endptIdx ? kVertexData_InvertNegativeCoverageBit : 0));
Chris Dalton90e8fb12017-12-22 02:24:53 -070065}
66
Chris Dalton21ba5512018-03-21 17:20:21 -060067static constexpr int32_t corner_vertex_data(int32_t leftID, int32_t cornerID, int32_t rightID,
68 int32_t bloatIdx) {
69 return pack_vertex_data(leftID, rightID, bloatIdx, cornerID, kVertexData_IsCornerBit);
Chris Dalton8738cf42018-03-09 11:57:40 -070070}
71
72static constexpr int32_t kTriangleVertices[] = {
Chris Dalton90e8fb12017-12-22 02:24:53 -070073 hull_vertex_data(0, 0, 3),
74 hull_vertex_data(0, 1, 3),
75 hull_vertex_data(0, 2, 3),
76 hull_vertex_data(1, 0, 3),
77 hull_vertex_data(1, 1, 3),
78 hull_vertex_data(1, 2, 3),
79 hull_vertex_data(2, 0, 3),
80 hull_vertex_data(2, 1, 3),
81 hull_vertex_data(2, 2, 3),
82
Chris Daltonbaf3e782018-03-08 15:55:58 +000083 edge_vertex_data(0, 0, 0, 3),
84 edge_vertex_data(0, 0, 1, 3),
85 edge_vertex_data(0, 0, 2, 3),
86 edge_vertex_data(0, 1, 0, 3),
87 edge_vertex_data(0, 1, 1, 3),
88 edge_vertex_data(0, 1, 2, 3),
Chris Dalton90e8fb12017-12-22 02:24:53 -070089
Chris Daltonbaf3e782018-03-08 15:55:58 +000090 edge_vertex_data(1, 0, 0, 3),
91 edge_vertex_data(1, 0, 1, 3),
92 edge_vertex_data(1, 0, 2, 3),
93 edge_vertex_data(1, 1, 0, 3),
94 edge_vertex_data(1, 1, 1, 3),
95 edge_vertex_data(1, 1, 2, 3),
Chris Dalton90e8fb12017-12-22 02:24:53 -070096
Chris Daltonbaf3e782018-03-08 15:55:58 +000097 edge_vertex_data(2, 0, 0, 3),
98 edge_vertex_data(2, 0, 1, 3),
99 edge_vertex_data(2, 0, 2, 3),
100 edge_vertex_data(2, 1, 0, 3),
101 edge_vertex_data(2, 1, 1, 3),
102 edge_vertex_data(2, 1, 2, 3),
Chris Dalton8738cf42018-03-09 11:57:40 -0700103
Chris Dalton21ba5512018-03-21 17:20:21 -0600104 corner_vertex_data(2, 0, 1, 0),
105 corner_vertex_data(2, 0, 1, 1),
106 corner_vertex_data(2, 0, 1, 2),
107 corner_vertex_data(2, 0, 1, 3),
Chris Dalton8738cf42018-03-09 11:57:40 -0700108
Chris Dalton21ba5512018-03-21 17:20:21 -0600109 corner_vertex_data(0, 1, 2, 0),
110 corner_vertex_data(0, 1, 2, 1),
111 corner_vertex_data(0, 1, 2, 2),
112 corner_vertex_data(0, 1, 2, 3),
Chris Dalton8738cf42018-03-09 11:57:40 -0700113
Chris Dalton21ba5512018-03-21 17:20:21 -0600114 corner_vertex_data(1, 2, 0, 0),
115 corner_vertex_data(1, 2, 0, 1),
116 corner_vertex_data(1, 2, 0, 2),
117 corner_vertex_data(1, 2, 0, 3),
Chris Dalton90e8fb12017-12-22 02:24:53 -0700118};
119
Chris Dalton8738cf42018-03-09 11:57:40 -0700120GR_DECLARE_STATIC_UNIQUE_KEY(gTriangleVertexBufferKey);
Chris Dalton90e8fb12017-12-22 02:24:53 -0700121
Chris Dalton27059d32018-01-23 14:06:50 -0700122static constexpr uint16_t kRestartStrip = 0xffff;
123
Chris Dalton8738cf42018-03-09 11:57:40 -0700124static constexpr uint16_t kTriangleIndicesAsStrips[] = {
Chris Dalton27059d32018-01-23 14:06:50 -0700125 1, 2, 0, 3, 8, kRestartStrip, // First corner and main body of the hull.
126 4, 5, 3, 6, 8, 7, kRestartStrip, // Opposite side and corners of the hull.
127 10, 9, 11, 14, 12, 13, kRestartStrip, // First edge.
128 16, 15, 17, 20, 18, 19, kRestartStrip, // Second edge.
Chris Dalton8738cf42018-03-09 11:57:40 -0700129 22, 21, 23, 26, 24, 25, kRestartStrip, // Third edge.
Chris Dalton04a1de52018-03-14 02:04:09 -0600130 28, 27, 29, 30, kRestartStrip, // First corner.
131 32, 31, 33, 34, kRestartStrip, // Second corner.
132 36, 35, 37, 38 // Third corner.
Chris Dalton27059d32018-01-23 14:06:50 -0700133};
134
Chris Dalton8738cf42018-03-09 11:57:40 -0700135static constexpr uint16_t kTriangleIndicesAsTris[] = {
Chris Dalton90e8fb12017-12-22 02:24:53 -0700136 // First corner and main body of the hull.
137 1, 2, 0,
138 2, 3, 0,
139 0, 3, 8, // Main body.
140
141 // Opposite side and corners of the hull.
142 4, 5, 3,
143 5, 6, 3,
144 3, 6, 8,
145 6, 7, 8,
146
147 // First edge.
148 10, 9, 11,
149 9, 14, 11,
150 11, 14, 12,
151 14, 13, 12,
152
153 // Second edge.
154 16, 15, 17,
155 15, 20, 17,
156 17, 20, 18,
157 20, 19, 18,
158
159 // Third edge.
160 22, 21, 23,
161 21, 26, 23,
162 23, 26, 24,
163 26, 25, 24,
Chris Dalton8738cf42018-03-09 11:57:40 -0700164
165 // First corner.
Chris Dalton04a1de52018-03-14 02:04:09 -0600166 28, 27, 29,
167 27, 30, 29,
Chris Dalton8738cf42018-03-09 11:57:40 -0700168
169 // Second corner.
Chris Dalton04a1de52018-03-14 02:04:09 -0600170 32, 31, 33,
171 31, 34, 33,
Chris Dalton8738cf42018-03-09 11:57:40 -0700172
173 // Third corner.
Chris Dalton04a1de52018-03-14 02:04:09 -0600174 36, 35, 37,
175 35, 38, 37,
Chris Dalton90e8fb12017-12-22 02:24:53 -0700176};
177
Chris Dalton8738cf42018-03-09 11:57:40 -0700178GR_DECLARE_STATIC_UNIQUE_KEY(gTriangleIndexBufferKey);
Chris Dalton90e8fb12017-12-22 02:24:53 -0700179
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600180// Curves, including quadratics, are drawn with a four-sided hull.
181static constexpr int32_t kCurveVertices[] = {
Chris Dalton90e8fb12017-12-22 02:24:53 -0700182 hull_vertex_data(0, 0, 4),
183 hull_vertex_data(0, 1, 4),
184 hull_vertex_data(0, 2, 4),
185 hull_vertex_data(1, 0, 4),
186 hull_vertex_data(1, 1, 4),
187 hull_vertex_data(1, 2, 4),
188 hull_vertex_data(2, 0, 4),
189 hull_vertex_data(2, 1, 4),
190 hull_vertex_data(2, 2, 4),
191 hull_vertex_data(3, 0, 4),
192 hull_vertex_data(3, 1, 4),
193 hull_vertex_data(3, 2, 4),
194
Chris Dalton21ba5512018-03-21 17:20:21 -0600195 corner_vertex_data(3, 0, 1, 0),
196 corner_vertex_data(3, 0, 1, 1),
197 corner_vertex_data(3, 0, 1, 2),
198 corner_vertex_data(3, 0, 1, 3),
199
200 corner_vertex_data(2, 3, 0, 0),
201 corner_vertex_data(2, 3, 0, 1),
202 corner_vertex_data(2, 3, 0, 2),
203 corner_vertex_data(2, 3, 0, 3),
Chris Dalton90e8fb12017-12-22 02:24:53 -0700204};
205
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600206GR_DECLARE_STATIC_UNIQUE_KEY(gCurveVertexBufferKey);
Chris Dalton90e8fb12017-12-22 02:24:53 -0700207
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600208static constexpr uint16_t kCurveIndicesAsStrips[] = {
Chris Dalton27059d32018-01-23 14:06:50 -0700209 1, 0, 2, 11, 3, 5, 4, kRestartStrip, // First half of the hull (split diagonally).
Chris Dalton21ba5512018-03-21 17:20:21 -0600210 7, 6, 8, 5, 9, 11, 10, kRestartStrip, // Second half of the hull.
211 13, 12, 14, 15, kRestartStrip, // First corner.
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600212 17, 16, 18, 19 // Final corner.
Chris Dalton27059d32018-01-23 14:06:50 -0700213};
214
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600215static constexpr uint16_t kCurveIndicesAsTris[] = {
Chris Dalton90e8fb12017-12-22 02:24:53 -0700216 // First half of the hull (split diagonally).
217 1, 0, 2,
218 0, 11, 2,
219 2, 11, 3,
220 11, 5, 3,
221 3, 5, 4,
222
223 // Second half of the hull.
224 7, 6, 8,
225 6, 5, 8,
226 8, 5, 9,
227 5, 11, 9,
228 9, 11, 10,
Chris Dalton21ba5512018-03-21 17:20:21 -0600229
230 // First corner.
231 13, 12, 14,
232 12, 15, 14,
233
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600234 // Final corner.
Chris Dalton21ba5512018-03-21 17:20:21 -0600235 17, 16, 18,
236 16, 19, 18,
Chris Dalton90e8fb12017-12-22 02:24:53 -0700237};
238
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600239GR_DECLARE_STATIC_UNIQUE_KEY(gCurveIndexBufferKey);
Chris Dalton90e8fb12017-12-22 02:24:53 -0700240
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600241// Generates a conservative raster hull around a triangle or curve. For triangles we generate
242// additional conservative rasters with coverage ramps around the edges and corners.
243//
244// Triangles are drawn in three steps: (1) Draw a conservative raster of the entire triangle, with a
245// coverage of +1. (2) Draw conservative rasters around each edge, with a coverage ramp from -1 to
246// 0. These edge coverage values convert jagged conservative raster edges into smooth, antialiased
247// ones. (3) Draw conservative rasters (aka pixel-size boxes) around each corner, replacing the
248// previous coverage values with ones that ramp to zero in the bloat vertices that fall outside the
249// triangle.
250//
Chris Dalton2c5e0112019-03-29 13:14:18 -0500251// Curve shaders handle the opposite edge and corners on their own. For curves we just generate a
252// conservative raster here and the shader does the rest.
253void GrVSCoverageProcessor::Impl::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
254 const GrVSCoverageProcessor& proc = args.fGP.cast<GrVSCoverageProcessor>();
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600255 GrGLSLVertexBuilder* v = args.fVertBuilder;
256 int numInputPoints = proc.numInputPoints();
Chris Dalton8738cf42018-03-09 11:57:40 -0700257
Chris Dalton9f2dab02018-04-18 14:07:03 -0600258 int inputWidth = (4 == numInputPoints || proc.hasInputWeight()) ? 4 : 3;
259 const char* swizzle = (4 == inputWidth) ? "xyzw" : "xyz";
Brian Salomon70132d02018-05-29 15:33:06 -0400260 v->codeAppendf("float%ix2 pts = transpose(float2x%i(%s.%s, %s.%s));", inputWidth, inputWidth,
Chris Dalton2c5e0112019-03-29 13:14:18 -0500261 proc.fInputXAndYValues[kInstanceAttribIdx_X].name(), swizzle,
262 proc.fInputXAndYValues[kInstanceAttribIdx_Y].name(), swizzle);
Chris Dalton90e8fb12017-12-22 02:24:53 -0700263
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600264 v->codeAppend ("half wind;");
265 Shader::CalcWind(proc, v, "pts", "wind");
266 if (PrimitiveType::kWeightedTriangles == proc.fPrimitiveType) {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600267 SkASSERT(3 == numInputPoints);
Brian Salomon92be2f72018-06-19 14:33:47 -0400268 SkASSERT(kFloat4_GrVertexAttribType ==
Chris Dalton2c5e0112019-03-29 13:14:18 -0500269 proc.fInputXAndYValues[kInstanceAttribIdx_X].cpuType());
Ethan Nicholase1f55022019-02-05 17:17:40 -0500270 v->codeAppendf("wind *= half(%s.w);",
Chris Dalton2c5e0112019-03-29 13:14:18 -0500271 proc.fInputXAndYValues[kInstanceAttribIdx_X].name());
Chris Dalton622650a2018-03-07 17:30:10 -0700272 }
273
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600274 float bloat = kAABloatRadius;
275#ifdef SK_DEBUG
276 if (proc.debugBloatEnabled()) {
277 bloat *= proc.debugBloat();
278 }
279#endif
280 v->defineConstant("bloat", bloat);
281
282 const char* hullPts = "pts";
Chris Dalton84d36cd2019-04-17 14:47:17 -0600283 fShader->emitSetupCode(v, "pts", (4 == fNumSides) ? &hullPts : nullptr);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600284
285 // Reverse all indices if the wind is counter-clockwise: [0, 1, 2] -> [2, 1, 0].
286 v->codeAppendf("int clockwise_indices = wind > 0 ? %s : 0x%x - %s;",
Chris Dalton2c5e0112019-03-29 13:14:18 -0500287 proc.fPerVertexData.name(),
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600288 ((fNumSides - 1) << kVertexData_LeftNeighborIdShift) |
289 ((fNumSides - 1) << kVertexData_RightNeighborIdShift) |
290 (((1 << kVertexData_RightNeighborIdShift) - 1) ^ 3) |
291 (fNumSides - 1),
Chris Dalton2c5e0112019-03-29 13:14:18 -0500292 proc.fPerVertexData.name());
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600293
294 // Here we generate conservative raster geometry for the input polygon. It is the convex
295 // hull of N pixel-size boxes, one centered on each the input points. Each corner has three
296 // vertices, where one or two may cause degenerate triangles. The vertex data tells us how
297 // to offset each vertex. Triangle edges and corners are also handled here using the same
298 // concept. For more details on conservative raster, see:
299 // https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter42.html
300 v->codeAppendf("float2 corner = %s[clockwise_indices & 3];", hullPts);
301 v->codeAppendf("float2 left = %s[clockwise_indices >> %i];",
302 hullPts, kVertexData_LeftNeighborIdShift);
303 v->codeAppendf("float2 right = %s[(clockwise_indices >> %i) & 3];",
304 hullPts, kVertexData_RightNeighborIdShift);
305
306 v->codeAppend ("float2 leftbloat = sign(corner - left);");
307 v->codeAppend ("leftbloat = float2(0 != leftbloat.y ? leftbloat.y : leftbloat.x, "
308 "0 != leftbloat.x ? -leftbloat.x : -leftbloat.y);");
309
310 v->codeAppend ("float2 rightbloat = sign(right - corner);");
311 v->codeAppend ("rightbloat = float2(0 != rightbloat.y ? rightbloat.y : rightbloat.x, "
312 "0 != rightbloat.x ? -rightbloat.x : -rightbloat.y);");
313
314 v->codeAppend ("bool2 left_right_notequal = notEqual(leftbloat, rightbloat);");
315
316 v->codeAppend ("float2 bloatdir = leftbloat;");
317
318 v->codeAppend ("float2 leftdir = corner - left;");
319 v->codeAppend ("leftdir = (float2(0) != leftdir) ? normalize(leftdir) : float2(1, 0);");
320
321 v->codeAppend ("float2 rightdir = right - corner;");
322 v->codeAppend ("rightdir = (float2(0) != rightdir) ? normalize(rightdir) : float2(1, 0);");
323
Brian Salomon70132d02018-05-29 15:33:06 -0400324 v->codeAppendf("if (0 != (%s & %i)) {", // Are we a corner?
Chris Dalton2c5e0112019-03-29 13:14:18 -0500325 proc.fPerVertexData.name(), kVertexData_IsCornerBit);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600326
327 // In corner boxes, all 4 coverage values will not map linearly.
328 // Therefore it is important to align the box so its diagonal shared
329 // edge points out of the triangle, in the direction that ramps to 0.
330 v->codeAppend ( "bloatdir = float2(leftdir.x > rightdir.x ? +1 : -1, "
331 "leftdir.y > rightdir.y ? +1 : -1);");
332
333 // For corner boxes, we hack left_right_notequal to always true. This
334 // in turn causes the upcoming code to always rotate, generating all
335 // 4 vertices of the corner box.
336 v->codeAppendf( "left_right_notequal = bool2(true);");
337 v->codeAppend ("}");
338
339 // At each corner of the polygon, our hull will have either 1, 2, or 3 vertices (or 4 if
340 // it's a corner box). We begin with this corner's first raster vertex (leftbloat), then
341 // continue rotating 90 degrees clockwise until we reach the desired raster vertex for this
342 // invocation. Corners with less than 3 corresponding raster vertices will result in
343 // redundant vertices and degenerate triangles.
Chris Dalton2c5e0112019-03-29 13:14:18 -0500344 v->codeAppendf("int bloatidx = (%s >> %i) & 3;", proc.fPerVertexData.name(),
Brian Salomon70132d02018-05-29 15:33:06 -0400345 kVertexData_BloatIdxShift);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600346 v->codeAppend ("switch (bloatidx) {");
347 v->codeAppend ( "case 3:");
348 // Only corners will have bloatidx=3, and corners always rotate.
349 v->codeAppend ( "bloatdir = float2(-bloatdir.y, +bloatdir.x);"); // 90 deg CW.
350 // fallthru.
351 v->codeAppend ( "case 2:");
352 v->codeAppendf( "if (all(left_right_notequal)) {");
353 v->codeAppend ( "bloatdir = float2(-bloatdir.y, +bloatdir.x);"); // 90 deg CW.
354 v->codeAppend ( "}");
355 // fallthru.
356 v->codeAppend ( "case 1:");
357 v->codeAppendf( "if (any(left_right_notequal)) {");
358 v->codeAppend ( "bloatdir = float2(-bloatdir.y, +bloatdir.x);"); // 90 deg CW.
359 v->codeAppend ( "}");
360 // fallthru.
361 v->codeAppend ("}");
362
Chris Dalton84d36cd2019-04-17 14:47:17 -0600363 v->codeAppend ("float2 vertexpos = fma(bloatdir, float2(bloat), corner);");
364 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos");
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600365
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600366 // Hulls have a coverage of +1 all around.
367 v->codeAppend ("half coverage = +1;");
368
369 if (3 == fNumSides) {
Chris Dalton4c239342018-04-05 18:43:40 -0600370 v->codeAppend ("half left_coverage; {");
371 Shader::CalcEdgeCoverageAtBloatVertex(v, "left", "corner", "bloatdir", "left_coverage");
372 v->codeAppend ("}");
373
374 v->codeAppend ("half right_coverage; {");
375 Shader::CalcEdgeCoverageAtBloatVertex(v, "corner", "right", "bloatdir", "right_coverage");
376 v->codeAppend ("}");
377
Brian Salomon70132d02018-05-29 15:33:06 -0400378 v->codeAppendf("if (0 != (%s & %i)) {", // Are we an edge?
Chris Dalton2c5e0112019-03-29 13:14:18 -0500379 proc.fPerVertexData.name(), kVertexData_IsEdgeBit);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600380 v->codeAppend ( "coverage = left_coverage;");
381 v->codeAppend ("}");
382
Brian Salomon70132d02018-05-29 15:33:06 -0400383 v->codeAppendf("if (0 != (%s & %i)) {", // Invert coverage?
Chris Dalton2c5e0112019-03-29 13:14:18 -0500384 proc.fPerVertexData.name(),
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600385 kVertexData_InvertNegativeCoverageBit);
386 v->codeAppend ( "coverage = -1 - coverage;");
387 v->codeAppend ("}");
Chris Dalton84d36cd2019-04-17 14:47:17 -0600388 } else if (!fShader->calculatesOwnEdgeCoverage()) {
389 // Determine the amount of coverage to subtract out for the flat edge of the curve.
390 v->codeAppendf("float2 p0 = pts[0], p1 = pts[%i];", numInputPoints - 1);
391 v->codeAppendf("float2 n = float2(p0.y - p1.y, p1.x - p0.x);");
392 v->codeAppend ("float nwidth = bloat*2 * (abs(n.x) + abs(n.y));");
393 // When nwidth=0, wind must also be 0 (and coverage * wind = 0). So it doesn't matter
394 // what we come up with here as long as it isn't NaN or Inf.
395 v->codeAppend ("float d = dot(p0 - vertexpos, n);");
396 v->codeAppend ("d /= (0 != nwidth) ? nwidth : 1;");
397 v->codeAppend ("coverage = half(d) - .5*sign(wind);");
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600398 }
399
Chris Dalton4c239342018-04-05 18:43:40 -0600400 // Non-corner geometry should have zero effect from corner coverage.
401 v->codeAppend ("half2 corner_coverage = half2(0);");
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600402
Brian Salomon70132d02018-05-29 15:33:06 -0400403 v->codeAppendf("if (0 != (%s & %i)) {", // Are we a corner?
Chris Dalton2c5e0112019-03-29 13:14:18 -0500404 proc.fPerVertexData.name(), kVertexData_IsCornerBit);
Chris Dalton84d36cd2019-04-17 14:47:17 -0600405 // Erase what the previous geometry wrote.
406 v->codeAppend ( "wind = -wind;");
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600407 if (3 == fNumSides) {
Chris Dalton84d36cd2019-04-17 14:47:17 -0600408 v->codeAppend ("coverage = 1 + left_coverage + right_coverage;");
409 } else if (!fShader->calculatesOwnEdgeCoverage()) {
410 v->codeAppend ("coverage = -coverage;");
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600411 }
Chris Dalton4c239342018-04-05 18:43:40 -0600412
413 // Corner boxes require attenuated coverage.
414 v->codeAppend ( "half attenuation; {");
415 Shader::CalcCornerAttenuation(v, "leftdir", "rightdir", "attenuation");
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600416 v->codeAppend ( "}");
Chris Dalton4c239342018-04-05 18:43:40 -0600417
418 // Attenuate corner coverage towards the outermost vertex (where bloatidx=0).
419 // This is all that curves need: At each vertex of the corner box, the curve
420 // Shader will calculate the curve's local coverage value, interpolate it
421 // alongside our attenuation parameter, and multiply the two together for a
422 // final coverage value.
Chris Dalton84d36cd2019-04-17 14:47:17 -0600423 v->codeAppend ( "corner_coverage = (0 == bloatidx) ? half2(0, attenuation) : half2(-1,+1);");
Chris Dalton4c239342018-04-05 18:43:40 -0600424
425 if (3 == fNumSides) {
426 // For triangles we also provide the actual coverage values at each vertex of
427 // the corner box.
428 v->codeAppend ("if (1 == bloatidx || 2 == bloatidx) {");
Chris Dalton84d36cd2019-04-17 14:47:17 -0600429 v->codeAppend ( "corner_coverage.x -= right_coverage;");
Chris Dalton4c239342018-04-05 18:43:40 -0600430 v->codeAppend ("}");
431 v->codeAppend ("if (bloatidx >= 2) {");
Chris Dalton84d36cd2019-04-17 14:47:17 -0600432 v->codeAppend ( "corner_coverage.x -= left_coverage;");
Chris Dalton4c239342018-04-05 18:43:40 -0600433 v->codeAppend ("}");
434 }
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600435 v->codeAppend ("}");
436
437 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600438 v->codeAppend ("coverage *= wind;");
Chris Dalton4c239342018-04-05 18:43:40 -0600439 v->codeAppend ("corner_coverage.x *= wind;");
Chris Dalton2c5e0112019-03-29 13:14:18 -0500440 fShader->emitVaryings(varyingHandler, GrGLSLVarying::Scope::kVertToFrag, &AccessCodeString(v),
Chris Dalton84d36cd2019-04-17 14:47:17 -0600441 "vertexpos", "coverage", "corner_coverage", "wind");
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600442
443 varyingHandler->emitAttributes(proc);
444 SkASSERT(!args.fFPCoordTransformHandler->nextCoordTransform());
445
446 // Fragment shader.
Chris Daltonc3318f02019-07-19 14:20:53 -0600447 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
448 f->codeAppendf("half coverage;");
449 fShader->emitFragmentCoverageCode(f, "coverage");
450 f->codeAppendf("%s = half4(coverage);", args.fOutputColor);
451 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600452}
Chris Dalton622650a2018-03-07 17:30:10 -0700453
Chris Dalton2c5e0112019-03-29 13:14:18 -0500454void GrVSCoverageProcessor::reset(PrimitiveType primitiveType, GrResourceProvider* rp) {
Chris Dalton84403d72018-02-13 21:46:17 -0500455 const GrCaps& caps = *rp->caps();
Chris Dalton90e8fb12017-12-22 02:24:53 -0700456
Chris Dalton2c5e0112019-03-29 13:14:18 -0500457 fPrimitiveType = primitiveType;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600458 switch (fPrimitiveType) {
Chris Dalton703b4762018-04-06 16:11:48 -0600459 case PrimitiveType::kTriangles:
460 case PrimitiveType::kWeightedTriangles: {
Chris Dalton8738cf42018-03-09 11:57:40 -0700461 GR_DEFINE_STATIC_UNIQUE_KEY(gTriangleVertexBufferKey);
Chris Dalton2c5e0112019-03-29 13:14:18 -0500462 fVertexBuffer = rp->findOrMakeStaticBuffer(
463 GrGpuBufferType::kVertex, sizeof(kTriangleVertices), kTriangleVertices,
464 gTriangleVertexBufferKey);
Chris Dalton8738cf42018-03-09 11:57:40 -0700465 GR_DEFINE_STATIC_UNIQUE_KEY(gTriangleIndexBufferKey);
Chris Dalton27059d32018-01-23 14:06:50 -0700466 if (caps.usePrimitiveRestart()) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500467 fIndexBuffer = rp->findOrMakeStaticBuffer(
468 GrGpuBufferType::kIndex, sizeof(kTriangleIndicesAsStrips),
469 kTriangleIndicesAsStrips, gTriangleIndexBufferKey);
470 fNumIndicesPerInstance = SK_ARRAY_COUNT(kTriangleIndicesAsStrips);
Chris Dalton27059d32018-01-23 14:06:50 -0700471 } else {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500472 fIndexBuffer = rp->findOrMakeStaticBuffer(
473 GrGpuBufferType::kIndex, sizeof(kTriangleIndicesAsTris),
474 kTriangleIndicesAsTris, gTriangleIndexBufferKey);
475 fNumIndicesPerInstance = SK_ARRAY_COUNT(kTriangleIndicesAsTris);
Chris Daltonfe462ef2018-03-08 15:54:01 +0000476 }
477 break;
478 }
479
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600480 case PrimitiveType::kQuadratics:
Chris Dalton9f2dab02018-04-18 14:07:03 -0600481 case PrimitiveType::kCubics:
482 case PrimitiveType::kConics: {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600483 GR_DEFINE_STATIC_UNIQUE_KEY(gCurveVertexBufferKey);
Chris Dalton2c5e0112019-03-29 13:14:18 -0500484 fVertexBuffer = rp->findOrMakeStaticBuffer(
485 GrGpuBufferType::kVertex, sizeof(kCurveVertices), kCurveVertices,
486 gCurveVertexBufferKey);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600487 GR_DEFINE_STATIC_UNIQUE_KEY(gCurveIndexBufferKey);
Chris Daltonbaf3e782018-03-08 15:55:58 +0000488 if (caps.usePrimitiveRestart()) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500489 fIndexBuffer = rp->findOrMakeStaticBuffer(
490 GrGpuBufferType::kIndex, sizeof(kCurveIndicesAsStrips),
491 kCurveIndicesAsStrips, gCurveIndexBufferKey);
492 fNumIndicesPerInstance = SK_ARRAY_COUNT(kCurveIndicesAsStrips);
Chris Daltonbaf3e782018-03-08 15:55:58 +0000493 } else {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500494 fIndexBuffer = rp->findOrMakeStaticBuffer(
495 GrGpuBufferType::kIndex, sizeof(kCurveIndicesAsTris), kCurveIndicesAsTris,
496 gCurveIndexBufferKey);
497 fNumIndicesPerInstance = SK_ARRAY_COUNT(kCurveIndicesAsTris);
Chris Daltonbaf3e782018-03-08 15:55:58 +0000498 }
499 break;
500 }
Chris Dalton90e8fb12017-12-22 02:24:53 -0700501 }
502
Brian Salomon92be2f72018-06-19 14:33:47 -0400503 GrVertexAttribType xyAttribType;
Brian Osmand4c29702018-09-14 16:16:55 -0400504 GrSLType xySLType;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600505 if (4 == this->numInputPoints() || this->hasInputWeight()) {
Brian Salomon92be2f72018-06-19 14:33:47 -0400506 GR_STATIC_ASSERT(offsetof(QuadPointInstance, fX) == 0);
507 GR_STATIC_ASSERT(sizeof(QuadPointInstance::fX) ==
508 GrVertexAttribTypeSize(kFloat4_GrVertexAttribType));
509 GR_STATIC_ASSERT(sizeof(QuadPointInstance::fY) ==
510 GrVertexAttribTypeSize(kFloat4_GrVertexAttribType));
511 xyAttribType = kFloat4_GrVertexAttribType;
Brian Osmand4c29702018-09-14 16:16:55 -0400512 xySLType = kFloat4_GrSLType;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700513 } else {
Chris Daltonc3318f02019-07-19 14:20:53 -0600514 GR_STATIC_ASSERT(sizeof(TriPointInstance) ==
515 2 * GrVertexAttribTypeSize(kFloat3_GrVertexAttribType));
Brian Salomon92be2f72018-06-19 14:33:47 -0400516 xyAttribType = kFloat3_GrVertexAttribType;
Brian Osmand4c29702018-09-14 16:16:55 -0400517 xySLType = kFloat3_GrSLType;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700518 }
Chris Dalton2c5e0112019-03-29 13:14:18 -0500519 fInputXAndYValues[kInstanceAttribIdx_X] = {"X", xyAttribType, xySLType};
520 fInputXAndYValues[kInstanceAttribIdx_Y] = {"Y", xyAttribType, xySLType};
521 this->setInstanceAttributes(fInputXAndYValues, 2);
522 fPerVertexData = {"vertexdata", kInt_GrVertexAttribType, kInt_GrSLType};
523 this->setVertexAttributes(&fPerVertexData, 1);
Chris Dalton90e8fb12017-12-22 02:24:53 -0700524
Chris Dalton27059d32018-01-23 14:06:50 -0700525 if (caps.usePrimitiveRestart()) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500526 fTriangleType = GrPrimitiveType::kTriangleStrip;
Chris Dalton27059d32018-01-23 14:06:50 -0700527 } else {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500528 fTriangleType = GrPrimitiveType::kTriangles;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700529 }
Chris Dalton90e8fb12017-12-22 02:24:53 -0700530}
531
Chris Dalton2c5e0112019-03-29 13:14:18 -0500532void GrVSCoverageProcessor::appendMesh(sk_sp<const GrGpuBuffer> instanceBuffer, int instanceCount,
533 int baseInstance, SkTArray<GrMesh>* out) const {
534 GrMesh& mesh = out->emplace_back(fTriangleType);
535 auto primitiveRestart = GrPrimitiveRestart(GrPrimitiveType::kTriangleStrip == fTriangleType);
536 mesh.setIndexedInstanced(fIndexBuffer, fNumIndicesPerInstance, std::move(instanceBuffer),
Brian Salomon802cb312018-06-08 18:05:20 -0400537 instanceCount, baseInstance, primitiveRestart);
Chris Dalton2c5e0112019-03-29 13:14:18 -0500538 mesh.setVertexData(fVertexBuffer, 0);
Chris Dalton90e8fb12017-12-22 02:24:53 -0700539}
540
Chris Dalton2c5e0112019-03-29 13:14:18 -0500541GrGLSLPrimitiveProcessor* GrVSCoverageProcessor::onCreateGLSLInstance(
542 std::unique_ptr<Shader> shader) const {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600543 switch (fPrimitiveType) {
544 case PrimitiveType::kTriangles:
Chris Dalton703b4762018-04-06 16:11:48 -0600545 case PrimitiveType::kWeightedTriangles:
Chris Dalton2c5e0112019-03-29 13:14:18 -0500546 return new Impl(std::move(shader), 3);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600547 case PrimitiveType::kQuadratics:
548 case PrimitiveType::kCubics:
Chris Dalton9f2dab02018-04-18 14:07:03 -0600549 case PrimitiveType::kConics:
Chris Dalton2c5e0112019-03-29 13:14:18 -0500550 return new Impl(std::move(shader), 4);
Chris Daltonfe462ef2018-03-08 15:54:01 +0000551 }
Chris Dalton2c5e0112019-03-29 13:14:18 -0500552 SK_ABORT("Invalid PrimitiveType");
Chris Daltonfe462ef2018-03-08 15:54:01 +0000553 return nullptr;
Chris Dalton90e8fb12017-12-22 02:24:53 -0700554}