1 /*
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
8 #include "src/gpu/ccpr/GrGSCoverageProcessor.h"
9
10 #include "src/gpu/GrMesh.h"
11 #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
12
13 using InputType = GrGLSLGeometryBuilder::InputType;
14 using OutputType = GrGLSLGeometryBuilder::OutputType;
15
16 /**
17 * This class and its subclasses implement the coverage processor with geometry shaders.
18 */
19 class GrGSCoverageProcessor::Impl : public GrGLSLGeometryProcessor {
20 protected:
Impl(std::unique_ptr<Shader> shader)21 Impl(std::unique_ptr<Shader> shader) : fShader(std::move(shader)) {}
22
hasCoverage(const GrGSCoverageProcessor & proc) const23 virtual bool hasCoverage(const GrGSCoverageProcessor& proc) const { return false; }
24
setData(const GrGLSLProgramDataManager & pdman,const GrPrimitiveProcessor &,FPCoordTransformIter && transformIter)25 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
26 FPCoordTransformIter&& transformIter) final {
27 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
28 }
29
onEmitCode(EmitArgs & args,GrGPArgs * gpArgs)30 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
31 const GrGSCoverageProcessor& proc = args.fGP.cast<GrGSCoverageProcessor>();
32
33 // The vertex shader simply forwards transposed x or y values to the geometry shader.
34 SkASSERT(1 == proc.numVertexAttributes());
35 gpArgs->fPositionVar = proc.fInputXOrYValues.asShaderVar();
36
37 // Geometry shader.
38 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
39 this->emitGeometryShader(proc, varyingHandler, args.fGeomBuilder, args.fRTAdjustName);
40 varyingHandler->emitAttributes(proc);
41 varyingHandler->setNoPerspective();
42 SkASSERT(!args.fFPCoordTransformHandler->nextCoordTransform());
43
44 // Fragment shader.
45 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
46 f->codeAppendf("half coverage;");
47 fShader->emitFragmentCoverageCode(f, "coverage");
48 f->codeAppendf("%s = half4(coverage);", args.fOutputColor);
49 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
50 }
51
emitGeometryShader(const GrGSCoverageProcessor & proc,GrGLSLVaryingHandler * varyingHandler,GrGLSLGeometryBuilder * g,const char * rtAdjust) const52 void emitGeometryShader(
53 const GrGSCoverageProcessor& proc, GrGLSLVaryingHandler* varyingHandler,
54 GrGLSLGeometryBuilder* g, const char* rtAdjust) const {
55 int numInputPoints = proc.numInputPoints();
56 SkASSERT(3 == numInputPoints || 4 == numInputPoints);
57
58 int inputWidth = (4 == numInputPoints || proc.hasInputWeight()) ? 4 : 3;
59 const char* posValues = (4 == inputWidth) ? "sk_Position" : "sk_Position.xyz";
60 g->codeAppendf("float%ix2 pts = transpose(float2x%i(sk_in[0].%s, sk_in[1].%s));",
61 inputWidth, inputWidth, posValues, posValues);
62
63 GrShaderVar wind("wind", kHalf_GrSLType);
64 g->declareGlobal(wind);
65 Shader::CalcWind(proc, g, "pts", wind.c_str());
66 if (PrimitiveType::kWeightedTriangles == proc.primitiveType()) {
67 SkASSERT(3 == numInputPoints);
68 SkASSERT(kFloat4_GrVertexAttribType == proc.fInputXOrYValues.cpuType());
69 g->codeAppendf("%s *= half(sk_in[0].sk_Position.w);", wind.c_str());
70 }
71
72 SkString emitVertexFn;
73 SkSTArray<3, GrShaderVar> emitArgs;
74 const char* corner = emitArgs.emplace_back("corner", kFloat2_GrSLType).c_str();
75 const char* bloatdir = emitArgs.emplace_back("bloatdir", kFloat2_GrSLType).c_str();
76 const char* inputCoverage = nullptr;
77 if (this->hasCoverage(proc)) {
78 inputCoverage = emitArgs.emplace_back("coverage", kHalf_GrSLType).c_str();
79 }
80 const char* cornerCoverage = nullptr;
81 if (Subpass::kCorners == proc.fSubpass) {
82 cornerCoverage = emitArgs.emplace_back("corner_coverage", kHalf2_GrSLType).c_str();
83 }
84 g->emitFunction(kVoid_GrSLType, "emitVertex", emitArgs.count(), emitArgs.begin(), [&]() {
85 SkString fnBody;
86 fnBody.appendf("float2 vertexpos = fma(%s, float2(bloat), %s);", bloatdir, corner);
87 const char* coverage = inputCoverage;
88 if (!coverage) {
89 if (!fShader->calculatesOwnEdgeCoverage()) {
90 // Flat edge opposite the curve. Coverages need full precision since distance
91 // to the opposite edge can be large.
92 fnBody.appendf("float coverage = dot(float3(vertexpos, 1), %s);",
93 fEdgeDistanceEquation.c_str());
94 } else {
95 // The "coverage" param should hold only the signed winding value.
96 fnBody.appendf("float coverage = 1;");
97 }
98 coverage = "coverage";
99 }
100 fnBody.appendf("%s *= %s;", coverage, wind.c_str());
101 if (cornerCoverage) {
102 fnBody.appendf("%s.x *= %s;", cornerCoverage, wind.c_str());
103 }
104 fShader->emitVaryings(varyingHandler, GrGLSLVarying::Scope::kGeoToFrag, &fnBody,
105 "vertexpos", coverage, cornerCoverage, wind.c_str());
106 g->emitVertex(&fnBody, "vertexpos", rtAdjust);
107 return fnBody;
108 }().c_str(), &emitVertexFn);
109
110 float bloat = kAABloatRadius;
111 #ifdef SK_DEBUG
112 if (proc.debugBloatEnabled()) {
113 bloat *= proc.debugBloat();
114 }
115 #endif
116 g->defineConstant("bloat", bloat);
117
118 if (!this->hasCoverage(proc) && !fShader->calculatesOwnEdgeCoverage()) {
119 // Determine the amount of coverage to subtract out for the flat edge of the curve.
120 g->declareGlobal(fEdgeDistanceEquation);
121 g->codeAppendf("float2 p0 = pts[0], p1 = pts[%i];", numInputPoints - 1);
122 g->codeAppendf("float2 n = float2(p0.y - p1.y, p1.x - p0.x);");
123 g->codeAppend ("float nwidth = bloat*2 * (abs(n.x) + abs(n.y));");
124 // When nwidth=0, wind must also be 0 (and coverage * wind = 0). So it doesn't matter
125 // what we come up with here as long as it isn't NaN or Inf.
126 g->codeAppend ("n /= (0 != nwidth) ? nwidth : 1;");
127 g->codeAppendf("%s = float3(-n, dot(n, p0) - .5*sign(%s));",
128 fEdgeDistanceEquation.c_str(), wind.c_str());
129 }
130
131 this->onEmitGeometryShader(proc, g, wind, emitVertexFn.c_str());
132 }
133
134 virtual void onEmitGeometryShader(const GrGSCoverageProcessor&, GrGLSLGeometryBuilder*,
135 const GrShaderVar& wind, const char* emitVertexFn) const = 0;
136
137 const std::unique_ptr<Shader> fShader;
138 const GrShaderVar fEdgeDistanceEquation{"edge_distance_equation", kFloat3_GrSLType};
139
140 typedef GrGLSLGeometryProcessor INHERITED;
141 };
142
143 /**
144 * Generates conservative rasters around a triangle and its edges, and calculates coverage ramps.
145 *
146 * Triangle rough outlines are drawn in two steps: (1) draw a conservative raster of the entire
147 * triangle, with a coverage of +1, and (2) draw conservative rasters around each edge, with a
148 * coverage ramp from -1 to 0. These edge coverage values convert jagged conservative raster edges
149 * into smooth, antialiased ones.
150 *
151 * The final corners get touched up in a later step by TriangleCornerImpl.
152 */
153 class GrGSCoverageProcessor::TriangleHullImpl : public GrGSCoverageProcessor::Impl {
154 public:
TriangleHullImpl(std::unique_ptr<Shader> shader)155 TriangleHullImpl(std::unique_ptr<Shader> shader) : Impl(std::move(shader)) {}
156
hasCoverage(const GrGSCoverageProcessor & proc) const157 bool hasCoverage(const GrGSCoverageProcessor& proc) const override { return true; }
158
onEmitGeometryShader(const GrGSCoverageProcessor &,GrGLSLGeometryBuilder * g,const GrShaderVar & wind,const char * emitVertexFn) const159 void onEmitGeometryShader(const GrGSCoverageProcessor&, GrGLSLGeometryBuilder* g,
160 const GrShaderVar& wind, const char* emitVertexFn) const override {
161 fShader->emitSetupCode(g, "pts");
162
163 // Visualize the input triangle as upright and equilateral, with a flat base. Paying special
164 // attention to wind, we can identify the points as top, bottom-left, and bottom-right.
165 //
166 // NOTE: We generate the rasters in 5 independent invocations, so each invocation designates
167 // the corner it will begin with as the top.
168 g->codeAppendf("int i = (%s > 0 ? sk_InvocationID : 4 - sk_InvocationID) %% 3;",
169 wind.c_str());
170 g->codeAppend ("float2 top = pts[i];");
171 g->codeAppendf("float2 right = pts[(i + (%s > 0 ? 1 : 2)) %% 3];", wind.c_str());
172 g->codeAppendf("float2 left = pts[(i + (%s > 0 ? 2 : 1)) %% 3];", wind.c_str());
173
174 // Determine which direction to outset the conservative raster from each of the three edges.
175 g->codeAppend ("float2 leftbloat = sign(top - left);");
176 g->codeAppend ("leftbloat = float2(0 != leftbloat.y ? leftbloat.y : leftbloat.x, "
177 "0 != leftbloat.x ? -leftbloat.x : -leftbloat.y);");
178
179 g->codeAppend ("float2 rightbloat = sign(right - top);");
180 g->codeAppend ("rightbloat = float2(0 != rightbloat.y ? rightbloat.y : rightbloat.x, "
181 "0 != rightbloat.x ? -rightbloat.x : -rightbloat.y);");
182
183 g->codeAppend ("float2 downbloat = sign(left - right);");
184 g->codeAppend ("downbloat = float2(0 != downbloat.y ? downbloat.y : downbloat.x, "
185 "0 != downbloat.x ? -downbloat.x : -downbloat.y);");
186
187 // The triangle's conservative raster has a coverage of +1 all around.
188 g->codeAppend ("half4 coverages = half4(+1);");
189
190 // Edges have coverage ramps.
191 g->codeAppend ("if (sk_InvocationID >= 2) {"); // Are we an edge?
192 Shader::CalcEdgeCoverageAtBloatVertex(g, "top", "right",
193 "float2(+rightbloat.y, -rightbloat.x)",
194 "coverages[0]");
195 g->codeAppend ( "coverages.yzw = half3(-1, 0, -1 - coverages[0]);");
196 // Reassign bloats to characterize a conservative raster around a single edge, rather than
197 // the entire triangle.
198 g->codeAppend ( "leftbloat = downbloat = -rightbloat;");
199 g->codeAppend ("}");
200
201 // Here we generate the conservative raster geometry. The triangle's conservative raster is
202 // the convex hull of 3 pixel-size boxes centered on the input points. This translates to a
203 // convex polygon with either one, two, or three vertices at each input point (depending on
204 // how sharp the corner is) that we split between two invocations. Edge conservative rasters
205 // are convex hulls of 2 pixel-size boxes, one at each endpoint. For more details on
206 // conservative raster, see:
207 // https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter42.html
208 g->codeAppendf("bool2 left_right_notequal = notEqual(leftbloat, rightbloat);");
209 g->codeAppend ("if (all(left_right_notequal)) {");
210 // The top corner will have three conservative raster vertices. Emit the
211 // middle one first to the triangle strip.
212 g->codeAppendf( "%s(top, float2(-leftbloat.y, +leftbloat.x), coverages[0]);",
213 emitVertexFn);
214 g->codeAppend ("}");
215 g->codeAppend ("if (any(left_right_notequal)) {");
216 // Second conservative raster vertex for the top corner.
217 g->codeAppendf( "%s(top, rightbloat, coverages[1]);", emitVertexFn);
218 g->codeAppend ("}");
219
220 // Main interior body.
221 g->codeAppendf("%s(top, leftbloat, coverages[2]);", emitVertexFn);
222 g->codeAppendf("%s(right, rightbloat, coverages[1]);", emitVertexFn);
223
224 // Here the invocations diverge slightly. We can't symmetrically divide three triangle
225 // points between two invocations, so each does the following:
226 //
227 // sk_InvocationID=0: Finishes the main interior body of the triangle hull.
228 // sk_InvocationID=1: Remaining two conservative raster vertices for the third hull corner.
229 // sk_InvocationID=2..4: Finish the opposite endpoint of their corresponding edge.
230 g->codeAppendf("bool2 right_down_notequal = notEqual(rightbloat, downbloat);");
231 g->codeAppend ("if (any(right_down_notequal) || 0 == sk_InvocationID) {");
232 g->codeAppendf( "%s((0 == sk_InvocationID) ? left : right, "
233 "(0 == sk_InvocationID) ? leftbloat : downbloat, "
234 "coverages[2]);", emitVertexFn);
235 g->codeAppend ("}");
236 g->codeAppend ("if (all(right_down_notequal) && 0 != sk_InvocationID) {");
237 g->codeAppendf( "%s(right, float2(-rightbloat.y, +rightbloat.x), coverages[3]);",
238 emitVertexFn);
239 g->codeAppend ("}");
240
241 // 5 invocations: 2 triangle hull invocations and 3 edges.
242 g->configure(InputType::kLines, OutputType::kTriangleStrip, 6, 5);
243 }
244 };
245
246 /**
247 * Generates a conservative raster around a convex quadrilateral that encloses a cubic or quadratic.
248 */
249 class GrGSCoverageProcessor::CurveHullImpl : public GrGSCoverageProcessor::Impl {
250 public:
CurveHullImpl(std::unique_ptr<Shader> shader)251 CurveHullImpl(std::unique_ptr<Shader> shader) : Impl(std::move(shader)) {}
252
onEmitGeometryShader(const GrGSCoverageProcessor &,GrGLSLGeometryBuilder * g,const GrShaderVar & wind,const char * emitVertexFn) const253 void onEmitGeometryShader(const GrGSCoverageProcessor&, GrGLSLGeometryBuilder* g,
254 const GrShaderVar& wind, const char* emitVertexFn) const override {
255 const char* hullPts = "pts";
256 fShader->emitSetupCode(g, "pts", &hullPts);
257
258 // Visualize the input (convex) quadrilateral as a square. Paying special attention to wind,
259 // we can identify the points by their corresponding corner.
260 //
261 // NOTE: We split the square down the diagonal from top-right to bottom-left, and generate
262 // the hull in two independent invocations. Each invocation designates the corner it will
263 // begin with as top-left.
264 g->codeAppend ("int i = sk_InvocationID * 2;");
265 g->codeAppendf("float2 topleft = %s[i];", hullPts);
266 g->codeAppendf("float2 topright = %s[%s > 0 ? i + 1 : 3 - i];", hullPts, wind.c_str());
267 g->codeAppendf("float2 bottomleft = %s[%s > 0 ? 3 - i : i + 1];", hullPts, wind.c_str());
268 g->codeAppendf("float2 bottomright = %s[2 - i];", hullPts);
269
270 // Determine how much to outset the conservative raster hull from the relevant edges.
271 g->codeAppend ("float2 leftbloat = float2(topleft.y > bottomleft.y ? +1 : -1, "
272 "topleft.x > bottomleft.x ? -1 : +1);");
273 g->codeAppend ("float2 upbloat = float2(topright.y > topleft.y ? +1 : -1, "
274 "topright.x > topleft.x ? -1 : +1);");
275 g->codeAppend ("float2 rightbloat = float2(bottomright.y > topright.y ? +1 : -1, "
276 "bottomright.x > topright.x ? -1 : +1);");
277
278 // Here we generate the conservative raster geometry. It is the convex hull of 4 pixel-size
279 // boxes centered on the input points, split evenly between two invocations. This translates
280 // to a polygon with either one, two, or three vertices at each input point, depending on
281 // how sharp the corner is. For more details on conservative raster, see:
282 // https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter42.html
283 g->codeAppendf("bool2 left_up_notequal = notEqual(leftbloat, upbloat);");
284 g->codeAppend ("if (all(left_up_notequal)) {");
285 // The top-left corner will have three conservative raster vertices.
286 // Emit the middle one first to the triangle strip.
287 g->codeAppendf( "%s(topleft, float2(-leftbloat.y, leftbloat.x));", emitVertexFn);
288 g->codeAppend ("}");
289 g->codeAppend ("if (any(left_up_notequal)) {");
290 // Second conservative raster vertex for the top-left corner.
291 g->codeAppendf( "%s(topleft, leftbloat);", emitVertexFn);
292 g->codeAppend ("}");
293
294 // Main interior body of this invocation's half of the hull.
295 g->codeAppendf("%s(topleft, upbloat);", emitVertexFn);
296 g->codeAppendf("%s(bottomleft, leftbloat);", emitVertexFn);
297 g->codeAppendf("%s(topright, upbloat);", emitVertexFn);
298
299 // Remaining two conservative raster vertices for the top-right corner.
300 g->codeAppendf("bool2 up_right_notequal = notEqual(upbloat, rightbloat);");
301 g->codeAppend ("if (any(up_right_notequal)) {");
302 g->codeAppendf( "%s(topright, rightbloat);", emitVertexFn);
303 g->codeAppend ("}");
304 g->codeAppend ("if (all(up_right_notequal)) {");
305 g->codeAppendf( "%s(topright, float2(-upbloat.y, upbloat.x));", emitVertexFn);
306 g->codeAppend ("}");
307
308 g->configure(InputType::kLines, OutputType::kTriangleStrip, 7, 2);
309 }
310 };
311
312 /**
313 * Generates conservative rasters around corners (aka pixel-size boxes) and calculates
314 * coverage and attenuation ramps to fix up the coverage values written by the hulls.
315 */
316 class GrGSCoverageProcessor::CornerImpl : public GrGSCoverageProcessor::Impl {
317 public:
CornerImpl(std::unique_ptr<Shader> shader)318 CornerImpl(std::unique_ptr<Shader> shader) : Impl(std::move(shader)) {}
319
hasCoverage(const GrGSCoverageProcessor & proc) const320 bool hasCoverage(const GrGSCoverageProcessor& proc) const override {
321 return proc.isTriangles();
322 }
323
onEmitGeometryShader(const GrGSCoverageProcessor & proc,GrGLSLGeometryBuilder * g,const GrShaderVar & wind,const char * emitVertexFn) const324 void onEmitGeometryShader(const GrGSCoverageProcessor& proc, GrGLSLGeometryBuilder* g,
325 const GrShaderVar& wind, const char* emitVertexFn) const override {
326 fShader->emitSetupCode(g, "pts");
327
328 g->codeAppendf("int corneridx = sk_InvocationID;");
329 if (!proc.isTriangles()) {
330 g->codeAppendf("corneridx *= %i;", proc.numInputPoints() - 1);
331 }
332
333 g->codeAppendf("float2 corner = pts[corneridx];");
334 g->codeAppendf("float2 left = pts[(corneridx + (%s > 0 ? %i : 1)) %% %i];",
335 wind.c_str(), proc.numInputPoints() - 1, proc.numInputPoints());
336 g->codeAppendf("float2 right = pts[(corneridx + (%s > 0 ? 1 : %i)) %% %i];",
337 wind.c_str(), proc.numInputPoints() - 1, proc.numInputPoints());
338
339 g->codeAppend ("float2 leftdir = corner - left;");
340 g->codeAppend ("leftdir = (float2(0) != leftdir) ? normalize(leftdir) : float2(1, 0);");
341
342 g->codeAppend ("float2 rightdir = right - corner;");
343 g->codeAppend ("rightdir = (float2(0) != rightdir) ? normalize(rightdir) : float2(1, 0);");
344
345 // Find "outbloat" and "crossbloat" at our corner. The outbloat points diagonally out of the
346 // triangle, in the direction that should ramp to zero coverage with attenuation. The
347 // crossbloat runs perpindicular to outbloat.
348 g->codeAppend ("float2 outbloat = float2(leftdir.x > rightdir.x ? +1 : -1, "
349 "leftdir.y > rightdir.y ? +1 : -1);");
350 g->codeAppend ("float2 crossbloat = float2(-outbloat.y, +outbloat.x);");
351
352 g->codeAppend ("half attenuation; {");
353 Shader::CalcCornerAttenuation(g, "leftdir", "rightdir", "attenuation");
354 g->codeAppend ("}");
355
356 if (proc.isTriangles()) {
357 g->codeAppend ("half2 left_coverages; {");
358 Shader::CalcEdgeCoveragesAtBloatVertices(g, "left", "corner", "-outbloat",
359 "-crossbloat", "left_coverages");
360 g->codeAppend ("}");
361
362 g->codeAppend ("half2 right_coverages; {");
363 Shader::CalcEdgeCoveragesAtBloatVertices(g, "corner", "right", "-outbloat",
364 "crossbloat", "right_coverages");
365 g->codeAppend ("}");
366
367 // Emit a corner box. The first coverage argument erases the values that were written
368 // previously by the hull and edge geometry. The second pair are multiplied together by
369 // the fragment shader. They ramp to 0 with attenuation in the direction of outbloat,
370 // and linearly from left-edge coverage to right-edge coverage in the direction of
371 // crossbloat.
372 //
373 // NOTE: Since this is not a linear mapping, it is important that the box's diagonal
374 // shared edge points in the direction of outbloat.
375 g->codeAppendf("%s(corner, -crossbloat, right_coverages[1] - left_coverages[1],"
376 "half2(1 + left_coverages[1], 1));",
377 emitVertexFn);
378
379 g->codeAppendf("%s(corner, outbloat, 1 + left_coverages[0] + right_coverages[0], "
380 "half2(0, attenuation));",
381 emitVertexFn);
382
383 g->codeAppendf("%s(corner, -outbloat, -1 - left_coverages[0] - right_coverages[0], "
384 "half2(1 + left_coverages[0] + right_coverages[0], 1));",
385 emitVertexFn);
386
387 g->codeAppendf("%s(corner, crossbloat, left_coverages[1] - right_coverages[1],"
388 "half2(1 + right_coverages[1], 1));",
389 emitVertexFn);
390 } else {
391 // Curves are simpler. Setting "wind = -wind" causes the Shader to erase what it had
392 // written in the previous pass hull. Then, at each vertex of the corner box, the Shader
393 // will calculate the curve's local coverage value, interpolate it alongside our
394 // attenuation parameter, and multiply the two together for a final coverage value.
395 g->codeAppendf("%s = -%s;", wind.c_str(), wind.c_str());
396 if (!fShader->calculatesOwnEdgeCoverage()) {
397 g->codeAppendf("%s = -%s;",
398 fEdgeDistanceEquation.c_str(), fEdgeDistanceEquation.c_str());
399 }
400 g->codeAppendf("%s(corner, -crossbloat, half2(-1, 1));", emitVertexFn);
401 g->codeAppendf("%s(corner, outbloat, half2(0, attenuation));",
402 emitVertexFn);
403 g->codeAppendf("%s(corner, -outbloat, half2(-1, 1));", emitVertexFn);
404 g->codeAppendf("%s(corner, crossbloat, half2(-1, 1));", emitVertexFn);
405 }
406
407 g->configure(InputType::kLines, OutputType::kTriangleStrip, 4, proc.isTriangles() ? 3 : 2);
408 }
409 };
410
reset(PrimitiveType primitiveType,GrResourceProvider *)411 void GrGSCoverageProcessor::reset(PrimitiveType primitiveType, GrResourceProvider*) {
412 fPrimitiveType = primitiveType; // This will affect the return values for numInputPoints, etc.
413
414 if (4 == this->numInputPoints() || this->hasInputWeight()) {
415 fInputXOrYValues =
416 {"x_or_y_values", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
417 GR_STATIC_ASSERT(sizeof(QuadPointInstance) ==
418 2 * GrVertexAttribTypeSize(kFloat4_GrVertexAttribType));
419 GR_STATIC_ASSERT(offsetof(QuadPointInstance, fY) ==
420 GrVertexAttribTypeSize(kFloat4_GrVertexAttribType));
421 } else {
422 fInputXOrYValues =
423 {"x_or_y_values", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
424 GR_STATIC_ASSERT(sizeof(TriPointInstance) ==
425 2 * GrVertexAttribTypeSize(kFloat3_GrVertexAttribType));
426 }
427
428 this->setVertexAttributes(&fInputXOrYValues, 1);
429 }
430
appendMesh(sk_sp<const GrGpuBuffer> instanceBuffer,int instanceCount,int baseInstance,SkTArray<GrMesh> * out) const431 void GrGSCoverageProcessor::appendMesh(sk_sp<const GrGpuBuffer> instanceBuffer, int instanceCount,
432 int baseInstance, SkTArray<GrMesh>* out) const {
433 // We don't actually make instanced draw calls. Instead, we feed transposed x,y point values to
434 // the GPU in a regular vertex array and draw kLines (see initGS). Then, each vertex invocation
435 // receives either the shape's x or y values as inputs, which it forwards to the geometry
436 // shader.
437 GrMesh& mesh = out->emplace_back(GrPrimitiveType::kLines);
438 mesh.setNonIndexedNonInstanced(instanceCount * 2);
439 mesh.setVertexData(std::move(instanceBuffer), baseInstance * 2);
440 }
441
draw(GrOpFlushState * flushState,const GrPipeline & pipeline,const SkIRect scissorRects[],const GrMesh meshes[],int meshCount,const SkRect & drawBounds) const442 void GrGSCoverageProcessor::draw(
443 GrOpFlushState* flushState, const GrPipeline& pipeline, const SkIRect scissorRects[],
444 const GrMesh meshes[], int meshCount, const SkRect& drawBounds) const {
445 // The geometry shader impl draws primitives in two subpasses: The first pass fills the interior
446 // and does edge AA. The second pass does touch up on corner pixels.
447 for (int i = 0; i < 2; ++i) {
448 fSubpass = (Subpass) i;
449 this->GrCCCoverageProcessor::draw(
450 flushState, pipeline, scissorRects, meshes, meshCount, drawBounds);
451 }
452 }
453
onCreateGLSLInstance(std::unique_ptr<Shader> shader) const454 GrGLSLPrimitiveProcessor* GrGSCoverageProcessor::onCreateGLSLInstance(
455 std::unique_ptr<Shader> shader) const {
456 if (Subpass::kHulls == fSubpass) {
457 return this->isTriangles()
458 ? (Impl*) new TriangleHullImpl(std::move(shader))
459 : (Impl*) new CurveHullImpl(std::move(shader));
460 }
461 SkASSERT(Subpass::kCorners == fSubpass);
462 return new CornerImpl(std::move(shader));
463 }
464