blob: 6f9b7dd20d35850fc3a78621dc9228f5f96c82d6 [file] [log] [blame]
Chris Dalton6a3dbee2017-10-16 10:44:41 -06001/*
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
Chris Dalton2c5e0112019-03-29 13:14:18 -05008#include "GrGSCoverageProcessor.h"
Chris Dalton6a3dbee2017-10-16 10:44:41 -06009
Chris Dalton23261772017-12-10 16:41:45 -070010#include "GrMesh.h"
Chris Daltonc17bf322017-10-24 10:59:03 -060011#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton6a3dbee2017-10-16 10:44:41 -060012
Chris Dalton1fbdb612017-12-12 12:48:47 -070013using InputType = GrGLSLGeometryBuilder::InputType;
14using OutputType = GrGLSLGeometryBuilder::OutputType;
Chris Dalton6a3dbee2017-10-16 10:44:41 -060015
16/**
17 * This class and its subclasses implement the coverage processor with geometry shaders.
18 */
Chris Dalton2c5e0112019-03-29 13:14:18 -050019class GrGSCoverageProcessor::Impl : public GrGLSLGeometryProcessor {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060020protected:
Chris Dalton2c5e0112019-03-29 13:14:18 -050021 Impl(std::unique_ptr<Shader> shader) : fShader(std::move(shader)) {}
Chris Dalton6a3dbee2017-10-16 10:44:41 -060022
Chris Dalton84d36cd2019-04-17 14:47:17 -060023 virtual bool hasCoverage(const GrGSCoverageProcessor& proc) const { return false; }
Chris Dalton21ba5512018-03-21 17:20:21 -060024
Chris Dalton6a3dbee2017-10-16 10:44:41 -060025 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
26 FPCoordTransformIter&& transformIter) final {
27 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
28 }
29
30 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
Chris Dalton2c5e0112019-03-29 13:14:18 -050031 const GrGSCoverageProcessor& proc = args.fGP.cast<GrGSCoverageProcessor>();
Chris Dalton6a3dbee2017-10-16 10:44:41 -060032
Chris Dalton23261772017-12-10 16:41:45 -070033 // The vertex shader simply forwards transposed x or y values to the geometry shader.
Brian Salomon92be2f72018-06-19 14:33:47 -040034 SkASSERT(1 == proc.numVertexAttributes());
Chris Dalton2c5e0112019-03-29 13:14:18 -050035 gpArgs->fPositionVar = proc.fInputXOrYValues.asShaderVar();
Chris Dalton6a3dbee2017-10-16 10:44:41 -060036
37 // Geometry shader.
38 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
39 this->emitGeometryShader(proc, varyingHandler, args.fGeomBuilder, args.fRTAdjustName);
40 varyingHandler->emitAttributes(proc);
Chris Dalton23261772017-12-10 16:41:45 -070041 varyingHandler->setNoPerspective();
Chris Dalton6a3dbee2017-10-16 10:44:41 -060042 SkASSERT(!args.fFPCoordTransformHandler->nextCoordTransform());
43
44 // Fragment shader.
45 fShader->emitFragmentCode(proc, args.fFragBuilder, args.fOutputColor, args.fOutputCoverage);
46 }
47
Chris Dalton2c5e0112019-03-29 13:14:18 -050048 void emitGeometryShader(
49 const GrGSCoverageProcessor& proc, GrGLSLVaryingHandler* varyingHandler,
50 GrGLSLGeometryBuilder* g, const char* rtAdjust) const {
Chris Dalton43646532017-12-07 12:47:02 -070051 int numInputPoints = proc.numInputPoints();
52 SkASSERT(3 == numInputPoints || 4 == numInputPoints);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060053
Chris Dalton9f2dab02018-04-18 14:07:03 -060054 int inputWidth = (4 == numInputPoints || proc.hasInputWeight()) ? 4 : 3;
55 const char* posValues = (4 == inputWidth) ? "sk_Position" : "sk_Position.xyz";
Chris Dalton23261772017-12-10 16:41:45 -070056 g->codeAppendf("float%ix2 pts = transpose(float2x%i(sk_in[0].%s, sk_in[1].%s));",
Chris Dalton9f2dab02018-04-18 14:07:03 -060057 inputWidth, inputWidth, posValues, posValues);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060058
59 GrShaderVar wind("wind", kHalf_GrSLType);
60 g->declareGlobal(wind);
Chris Dalton6f5e77a2018-04-23 21:14:42 -060061 Shader::CalcWind(proc, g, "pts", wind.c_str());
Chris Dalton2c5e0112019-03-29 13:14:18 -050062 if (PrimitiveType::kWeightedTriangles == proc.primitiveType()) {
Chris Dalton84403d72018-02-13 21:46:17 -050063 SkASSERT(3 == numInputPoints);
Chris Dalton2c5e0112019-03-29 13:14:18 -050064 SkASSERT(kFloat4_GrVertexAttribType == proc.fInputXOrYValues.cpuType());
Ethan Nicholase1f55022019-02-05 17:17:40 -050065 g->codeAppendf("%s *= half(sk_in[0].sk_Position.w);", wind.c_str());
Chris Dalton43646532017-12-07 12:47:02 -070066 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -060067
68 SkString emitVertexFn;
69 SkSTArray<2, GrShaderVar> emitArgs;
Chris Dalton74231952019-01-18 15:53:20 -070070 const char* corner = emitArgs.emplace_back("corner", kFloat2_GrSLType).c_str();
71 const char* bloatdir = emitArgs.emplace_back("bloatdir", kFloat2_GrSLType).c_str();
Chris Dalton84d36cd2019-04-17 14:47:17 -060072 const char* inputCoverage = nullptr;
73 if (this->hasCoverage(proc)) {
74 inputCoverage = emitArgs.emplace_back("coverage", kHalf_GrSLType).c_str();
Chris Daltonfe462ef2018-03-08 15:54:01 +000075 }
Chris Dalton4c239342018-04-05 18:43:40 -060076 const char* cornerCoverage = nullptr;
Chris Dalton2c5e0112019-03-29 13:14:18 -050077 if (Subpass::kCorners == proc.fSubpass) {
Chris Dalton4c239342018-04-05 18:43:40 -060078 cornerCoverage = emitArgs.emplace_back("corner_coverage", kHalf2_GrSLType).c_str();
Chris Dalton04a1de52018-03-14 02:04:09 -060079 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -060080 g->emitFunction(kVoid_GrSLType, "emitVertex", emitArgs.count(), emitArgs.begin(), [&]() {
81 SkString fnBody;
Chris Dalton84d36cd2019-04-17 14:47:17 -060082 fnBody.appendf("float2 vertexpos = fma(%s, float2(bloat), %s);", bloatdir, corner);
83 const char* coverage = inputCoverage;
84 if (!coverage) {
85 if (!fShader->calculatesOwnEdgeCoverage()) {
86 // Flat edge opposite the curve. Coverages need full precision since distance
87 // to the opposite edge can be large.
88 fnBody.appendf("float coverage = dot(float3(vertexpos, 1), %s);",
89 fEdgeDistanceEquation.c_str());
90 } else {
91 // The "coverage" param should hold only the signed winding value.
92 fnBody.appendf("float coverage = 1;");
93 }
94 coverage = "coverage";
Chris Dalton21ba5512018-03-21 17:20:21 -060095 }
Chris Dalton84d36cd2019-04-17 14:47:17 -060096 fnBody.appendf("%s *= %s;", coverage, wind.c_str());
Chris Dalton4c239342018-04-05 18:43:40 -060097 if (cornerCoverage) {
98 fnBody.appendf("%s.x *= %s;", cornerCoverage, wind.c_str());
Chris Dalton21ba5512018-03-21 17:20:21 -060099 }
Chris Dalton90e8fb12017-12-22 02:24:53 -0700100 fShader->emitVaryings(varyingHandler, GrGLSLVarying::Scope::kGeoToFrag, &fnBody,
Chris Dalton84d36cd2019-04-17 14:47:17 -0600101 "vertexpos", coverage, cornerCoverage, wind.c_str());
Chris Dalton74231952019-01-18 15:53:20 -0700102 g->emitVertex(&fnBody, "vertexpos", rtAdjust);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600103 return fnBody;
104 }().c_str(), &emitVertexFn);
105
Chris Daltonc17bf322017-10-24 10:59:03 -0600106 float bloat = kAABloatRadius;
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600107#ifdef SK_DEBUG
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600108 if (proc.debugBloatEnabled()) {
Chris Daltonc17bf322017-10-24 10:59:03 -0600109 bloat *= proc.debugBloat();
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600110 }
111#endif
Chris Daltonc17bf322017-10-24 10:59:03 -0600112 g->defineConstant("bloat", bloat);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600113
Chris Dalton84d36cd2019-04-17 14:47:17 -0600114 if (!this->hasCoverage(proc) && !fShader->calculatesOwnEdgeCoverage()) {
115 // Determine the amount of coverage to subtract out for the flat edge of the curve.
116 g->declareGlobal(fEdgeDistanceEquation);
117 g->codeAppendf("float2 p0 = pts[0], p1 = pts[%i];", numInputPoints - 1);
118 g->codeAppendf("float2 n = float2(p0.y - p1.y, p1.x - p0.x);");
119 g->codeAppend ("float nwidth = bloat*2 * (abs(n.x) + abs(n.y));");
120 // When nwidth=0, wind must also be 0 (and coverage * wind = 0). So it doesn't matter
121 // what we come up with here as long as it isn't NaN or Inf.
122 g->codeAppend ("n /= (0 != nwidth) ? nwidth : 1;");
123 g->codeAppendf("%s = float3(-n, dot(n, p0) - .5*sign(%s));",
124 fEdgeDistanceEquation.c_str(), wind.c_str());
125 }
126
Chris Dalton21ba5512018-03-21 17:20:21 -0600127 this->onEmitGeometryShader(proc, g, wind, emitVertexFn.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600128 }
129
Chris Dalton2c5e0112019-03-29 13:14:18 -0500130 virtual void onEmitGeometryShader(const GrGSCoverageProcessor&, GrGLSLGeometryBuilder*,
Chris Dalton21ba5512018-03-21 17:20:21 -0600131 const GrShaderVar& wind, const char* emitVertexFn) const = 0;
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600132
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600133 const std::unique_ptr<Shader> fShader;
Chris Dalton84d36cd2019-04-17 14:47:17 -0600134 const GrShaderVar fEdgeDistanceEquation{"edge_distance_equation", kFloat3_GrSLType};
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600135
136 typedef GrGLSLGeometryProcessor INHERITED;
137};
138
Chris Dalton1fbdb612017-12-12 12:48:47 -0700139/**
Chris Dalton5183e642018-03-07 12:53:01 -0700140 * Generates conservative rasters around a triangle and its edges, and calculates coverage ramps.
141 *
142 * Triangle rough outlines are drawn in two steps: (1) draw a conservative raster of the entire
143 * triangle, with a coverage of +1, and (2) draw conservative rasters around each edge, with a
144 * coverage ramp from -1 to 0. These edge coverage values convert jagged conservative raster edges
145 * into smooth, antialiased ones.
146 *
Chris Dalton2c5e0112019-03-29 13:14:18 -0500147 * The final corners get touched up in a later step by TriangleCornerImpl.
Chris Dalton1fbdb612017-12-12 12:48:47 -0700148 */
Chris Dalton2c5e0112019-03-29 13:14:18 -0500149class GrGSCoverageProcessor::TriangleHullImpl : public GrGSCoverageProcessor::Impl {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600150public:
Chris Dalton2c5e0112019-03-29 13:14:18 -0500151 TriangleHullImpl(std::unique_ptr<Shader> shader) : Impl(std::move(shader)) {}
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600152
Chris Dalton84d36cd2019-04-17 14:47:17 -0600153 bool hasCoverage(const GrGSCoverageProcessor& proc) const override { return true; }
Chris Dalton21ba5512018-03-21 17:20:21 -0600154
Chris Dalton2c5e0112019-03-29 13:14:18 -0500155 void onEmitGeometryShader(const GrGSCoverageProcessor&, GrGLSLGeometryBuilder* g,
Chris Dalton21ba5512018-03-21 17:20:21 -0600156 const GrShaderVar& wind, const char* emitVertexFn) const override {
Chris Dalton84d36cd2019-04-17 14:47:17 -0600157 fShader->emitSetupCode(g, "pts");
Chris Dalton8738cf42018-03-09 11:57:40 -0700158
Chris Dalton1fbdb612017-12-12 12:48:47 -0700159 // Visualize the input triangle as upright and equilateral, with a flat base. Paying special
160 // attention to wind, we can identify the points as top, bottom-left, and bottom-right.
161 //
Chris Dalton5183e642018-03-07 12:53:01 -0700162 // NOTE: We generate the rasters in 5 independent invocations, so each invocation designates
Chris Dalton1fbdb612017-12-12 12:48:47 -0700163 // the corner it will begin with as the top.
Chris Dalton5183e642018-03-07 12:53:01 -0700164 g->codeAppendf("int i = (%s > 0 ? sk_InvocationID : 4 - sk_InvocationID) %% 3;",
165 wind.c_str());
166 g->codeAppend ("float2 top = pts[i];");
167 g->codeAppendf("float2 right = pts[(i + (%s > 0 ? 1 : 2)) %% 3];", wind.c_str());
168 g->codeAppendf("float2 left = pts[(i + (%s > 0 ? 2 : 1)) %% 3];", wind.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600169
Chris Dalton5183e642018-03-07 12:53:01 -0700170 // Determine which direction to outset the conservative raster from each of the three edges.
171 g->codeAppend ("float2 leftbloat = sign(top - left);");
172 g->codeAppend ("leftbloat = float2(0 != leftbloat.y ? leftbloat.y : leftbloat.x, "
173 "0 != leftbloat.x ? -leftbloat.x : -leftbloat.y);");
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600174
Chris Dalton5183e642018-03-07 12:53:01 -0700175 g->codeAppend ("float2 rightbloat = sign(right - top);");
176 g->codeAppend ("rightbloat = float2(0 != rightbloat.y ? rightbloat.y : rightbloat.x, "
177 "0 != rightbloat.x ? -rightbloat.x : -rightbloat.y);");
178
179 g->codeAppend ("float2 downbloat = sign(left - right);");
180 g->codeAppend ("downbloat = float2(0 != downbloat.y ? downbloat.y : downbloat.x, "
181 "0 != downbloat.x ? -downbloat.x : -downbloat.y);");
182
183 // The triangle's conservative raster has a coverage of +1 all around.
184 g->codeAppend ("half4 coverages = half4(+1);");
185
186 // Edges have coverage ramps.
187 g->codeAppend ("if (sk_InvocationID >= 2) {"); // Are we an edge?
188 Shader::CalcEdgeCoverageAtBloatVertex(g, "top", "right",
189 "float2(+rightbloat.y, -rightbloat.x)",
190 "coverages[0]");
191 g->codeAppend ( "coverages.yzw = half3(-1, 0, -1 - coverages[0]);");
192 // Reassign bloats to characterize a conservative raster around a single edge, rather than
193 // the entire triangle.
194 g->codeAppend ( "leftbloat = downbloat = -rightbloat;");
195 g->codeAppend ("}");
196
Chris Dalton5183e642018-03-07 12:53:01 -0700197 // Here we generate the conservative raster geometry. The triangle's conservative raster is
198 // the convex hull of 3 pixel-size boxes centered on the input points. This translates to a
199 // convex polygon with either one, two, or three vertices at each input point (depending on
200 // how sharp the corner is) that we split between two invocations. Edge conservative rasters
201 // are convex hulls of 2 pixel-size boxes, one at each endpoint. For more details on
202 // conservative raster, see:
Chris Dalton1fbdb612017-12-12 12:48:47 -0700203 // https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter42.html
204 g->codeAppendf("bool2 left_right_notequal = notEqual(leftbloat, rightbloat);");
205 g->codeAppend ("if (all(left_right_notequal)) {");
206 // The top corner will have three conservative raster vertices. Emit the
207 // middle one first to the triangle strip.
Chris Dalton74231952019-01-18 15:53:20 -0700208 g->codeAppendf( "%s(top, float2(-leftbloat.y, +leftbloat.x), coverages[0]);",
Chris Dalton5183e642018-03-07 12:53:01 -0700209 emitVertexFn);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600210 g->codeAppend ("}");
Chris Dalton1fbdb612017-12-12 12:48:47 -0700211 g->codeAppend ("if (any(left_right_notequal)) {");
212 // Second conservative raster vertex for the top corner.
Chris Dalton74231952019-01-18 15:53:20 -0700213 g->codeAppendf( "%s(top, rightbloat, coverages[1]);", emitVertexFn);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600214 g->codeAppend ("}");
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600215
Chris Dalton5183e642018-03-07 12:53:01 -0700216 // Main interior body.
Chris Dalton74231952019-01-18 15:53:20 -0700217 g->codeAppendf("%s(top, leftbloat, coverages[2]);", emitVertexFn);
218 g->codeAppendf("%s(right, rightbloat, coverages[1]);", emitVertexFn);
Chris Dalton1fbdb612017-12-12 12:48:47 -0700219
Chris Dalton5183e642018-03-07 12:53:01 -0700220 // Here the invocations diverge slightly. We can't symmetrically divide three triangle
221 // points between two invocations, so each does the following:
Chris Dalton1fbdb612017-12-12 12:48:47 -0700222 //
Chris Dalton5183e642018-03-07 12:53:01 -0700223 // sk_InvocationID=0: Finishes the main interior body of the triangle hull.
224 // sk_InvocationID=1: Remaining two conservative raster vertices for the third hull corner.
225 // sk_InvocationID=2..4: Finish the opposite endpoint of their corresponding edge.
Chris Dalton1fbdb612017-12-12 12:48:47 -0700226 g->codeAppendf("bool2 right_down_notequal = notEqual(rightbloat, downbloat);");
227 g->codeAppend ("if (any(right_down_notequal) || 0 == sk_InvocationID) {");
Chris Dalton74231952019-01-18 15:53:20 -0700228 g->codeAppendf( "%s((0 == sk_InvocationID) ? left : right, "
229 "(0 == sk_InvocationID) ? leftbloat : downbloat, "
Chris Dalton5183e642018-03-07 12:53:01 -0700230 "coverages[2]);", emitVertexFn);
Chris Dalton1fbdb612017-12-12 12:48:47 -0700231 g->codeAppend ("}");
232 g->codeAppend ("if (all(right_down_notequal) && 0 != sk_InvocationID) {");
Chris Dalton74231952019-01-18 15:53:20 -0700233 g->codeAppendf( "%s(right, float2(-rightbloat.y, +rightbloat.x), coverages[3]);",
Chris Dalton5183e642018-03-07 12:53:01 -0700234 emitVertexFn);
Chris Dalton1fbdb612017-12-12 12:48:47 -0700235 g->codeAppend ("}");
236
Chris Dalton5183e642018-03-07 12:53:01 -0700237 // 5 invocations: 2 triangle hull invocations and 3 edges.
238 g->configure(InputType::kLines, OutputType::kTriangleStrip, 6, 5);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600239 }
240};
241
Chris Dalton1fbdb612017-12-12 12:48:47 -0700242/**
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600243 * Generates a conservative raster around a convex quadrilateral that encloses a cubic or quadratic.
244 */
Chris Dalton2c5e0112019-03-29 13:14:18 -0500245class GrGSCoverageProcessor::CurveHullImpl : public GrGSCoverageProcessor::Impl {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600246public:
Chris Dalton2c5e0112019-03-29 13:14:18 -0500247 CurveHullImpl(std::unique_ptr<Shader> shader) : Impl(std::move(shader)) {}
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600248
Chris Dalton2c5e0112019-03-29 13:14:18 -0500249 void onEmitGeometryShader(const GrGSCoverageProcessor&, GrGLSLGeometryBuilder* g,
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600250 const GrShaderVar& wind, const char* emitVertexFn) const override {
251 const char* hullPts = "pts";
Chris Dalton84d36cd2019-04-17 14:47:17 -0600252 fShader->emitSetupCode(g, "pts", &hullPts);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600253
254 // Visualize the input (convex) quadrilateral as a square. Paying special attention to wind,
255 // we can identify the points by their corresponding corner.
256 //
257 // NOTE: We split the square down the diagonal from top-right to bottom-left, and generate
258 // the hull in two independent invocations. Each invocation designates the corner it will
259 // begin with as top-left.
260 g->codeAppend ("int i = sk_InvocationID * 2;");
261 g->codeAppendf("float2 topleft = %s[i];", hullPts);
262 g->codeAppendf("float2 topright = %s[%s > 0 ? i + 1 : 3 - i];", hullPts, wind.c_str());
263 g->codeAppendf("float2 bottomleft = %s[%s > 0 ? 3 - i : i + 1];", hullPts, wind.c_str());
264 g->codeAppendf("float2 bottomright = %s[2 - i];", hullPts);
265
266 // Determine how much to outset the conservative raster hull from the relevant edges.
Chris Dalton74231952019-01-18 15:53:20 -0700267 g->codeAppend ("float2 leftbloat = float2(topleft.y > bottomleft.y ? +1 : -1, "
268 "topleft.x > bottomleft.x ? -1 : +1);");
269 g->codeAppend ("float2 upbloat = float2(topright.y > topleft.y ? +1 : -1, "
270 "topright.x > topleft.x ? -1 : +1);");
271 g->codeAppend ("float2 rightbloat = float2(bottomright.y > topright.y ? +1 : -1, "
272 "bottomright.x > topright.x ? -1 : +1);");
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600273
274 // Here we generate the conservative raster geometry. It is the convex hull of 4 pixel-size
275 // boxes centered on the input points, split evenly between two invocations. This translates
276 // to a polygon with either one, two, or three vertices at each input point, depending on
277 // how sharp the corner is. For more details on conservative raster, see:
278 // https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter42.html
279 g->codeAppendf("bool2 left_up_notequal = notEqual(leftbloat, upbloat);");
280 g->codeAppend ("if (all(left_up_notequal)) {");
281 // The top-left corner will have three conservative raster vertices.
282 // Emit the middle one first to the triangle strip.
Chris Dalton74231952019-01-18 15:53:20 -0700283 g->codeAppendf( "%s(topleft, float2(-leftbloat.y, leftbloat.x));", emitVertexFn);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600284 g->codeAppend ("}");
285 g->codeAppend ("if (any(left_up_notequal)) {");
286 // Second conservative raster vertex for the top-left corner.
Chris Dalton74231952019-01-18 15:53:20 -0700287 g->codeAppendf( "%s(topleft, leftbloat);", emitVertexFn);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600288 g->codeAppend ("}");
289
290 // Main interior body of this invocation's half of the hull.
Chris Dalton74231952019-01-18 15:53:20 -0700291 g->codeAppendf("%s(topleft, upbloat);", emitVertexFn);
292 g->codeAppendf("%s(bottomleft, leftbloat);", emitVertexFn);
293 g->codeAppendf("%s(topright, upbloat);", emitVertexFn);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600294
295 // Remaining two conservative raster vertices for the top-right corner.
296 g->codeAppendf("bool2 up_right_notequal = notEqual(upbloat, rightbloat);");
297 g->codeAppend ("if (any(up_right_notequal)) {");
Chris Dalton74231952019-01-18 15:53:20 -0700298 g->codeAppendf( "%s(topright, rightbloat);", emitVertexFn);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600299 g->codeAppend ("}");
300 g->codeAppend ("if (all(up_right_notequal)) {");
Chris Dalton74231952019-01-18 15:53:20 -0700301 g->codeAppendf( "%s(topright, float2(-upbloat.y, upbloat.x));", emitVertexFn);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600302 g->codeAppend ("}");
303
304 g->configure(InputType::kLines, OutputType::kTriangleStrip, 7, 2);
305 }
306};
307
308/**
Chris Dalton21ba5512018-03-21 17:20:21 -0600309 * Generates conservative rasters around corners (aka pixel-size boxes) and calculates
310 * coverage and attenuation ramps to fix up the coverage values written by the hulls.
Chris Dalton8738cf42018-03-09 11:57:40 -0700311 */
Chris Dalton2c5e0112019-03-29 13:14:18 -0500312class GrGSCoverageProcessor::CornerImpl : public GrGSCoverageProcessor::Impl {
Chris Dalton8738cf42018-03-09 11:57:40 -0700313public:
Chris Dalton2c5e0112019-03-29 13:14:18 -0500314 CornerImpl(std::unique_ptr<Shader> shader) : Impl(std::move(shader)) {}
Chris Dalton8738cf42018-03-09 11:57:40 -0700315
Chris Dalton84d36cd2019-04-17 14:47:17 -0600316 bool hasCoverage(const GrGSCoverageProcessor& proc) const override {
317 return proc.isTriangles();
318 }
Chris Dalton8738cf42018-03-09 11:57:40 -0700319
Chris Dalton2c5e0112019-03-29 13:14:18 -0500320 void onEmitGeometryShader(const GrGSCoverageProcessor& proc, GrGLSLGeometryBuilder* g,
Chris Dalton21ba5512018-03-21 17:20:21 -0600321 const GrShaderVar& wind, const char* emitVertexFn) const override {
Chris Dalton84d36cd2019-04-17 14:47:17 -0600322 fShader->emitSetupCode(g, "pts");
Chris Dalton21ba5512018-03-21 17:20:21 -0600323
Chris Dalton21ba5512018-03-21 17:20:21 -0600324 g->codeAppendf("int corneridx = sk_InvocationID;");
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600325 if (!proc.isTriangles()) {
Chris Dalton21ba5512018-03-21 17:20:21 -0600326 g->codeAppendf("corneridx *= %i;", proc.numInputPoints() - 1);
327 }
328
329 g->codeAppendf("float2 corner = pts[corneridx];");
330 g->codeAppendf("float2 left = pts[(corneridx + (%s > 0 ? %i : 1)) %% %i];",
331 wind.c_str(), proc.numInputPoints() - 1, proc.numInputPoints());
332 g->codeAppendf("float2 right = pts[(corneridx + (%s > 0 ? 1 : %i)) %% %i];",
333 wind.c_str(), proc.numInputPoints() - 1, proc.numInputPoints());
Chris Dalton8738cf42018-03-09 11:57:40 -0700334
Chris Dalton04a1de52018-03-14 02:04:09 -0600335 g->codeAppend ("float2 leftdir = corner - left;");
336 g->codeAppend ("leftdir = (float2(0) != leftdir) ? normalize(leftdir) : float2(1, 0);");
337
338 g->codeAppend ("float2 rightdir = right - corner;");
339 g->codeAppend ("rightdir = (float2(0) != rightdir) ? normalize(rightdir) : float2(1, 0);");
340
Chris Dalton8738cf42018-03-09 11:57:40 -0700341 // Find "outbloat" and "crossbloat" at our corner. The outbloat points diagonally out of the
Chris Dalton04a1de52018-03-14 02:04:09 -0600342 // triangle, in the direction that should ramp to zero coverage with attenuation. The
343 // crossbloat runs perpindicular to outbloat.
Chris Dalton8738cf42018-03-09 11:57:40 -0700344 g->codeAppend ("float2 outbloat = float2(leftdir.x > rightdir.x ? +1 : -1, "
345 "leftdir.y > rightdir.y ? +1 : -1);");
346 g->codeAppend ("float2 crossbloat = float2(-outbloat.y, +outbloat.x);");
347
Chris Dalton04a1de52018-03-14 02:04:09 -0600348 g->codeAppend ("half attenuation; {");
Chris Dalton4c239342018-04-05 18:43:40 -0600349 Shader::CalcCornerAttenuation(g, "leftdir", "rightdir", "attenuation");
Chris Dalton04a1de52018-03-14 02:04:09 -0600350 g->codeAppend ("}");
351
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600352 if (proc.isTriangles()) {
Chris Dalton4c239342018-04-05 18:43:40 -0600353 g->codeAppend ("half2 left_coverages; {");
354 Shader::CalcEdgeCoveragesAtBloatVertices(g, "left", "corner", "-outbloat",
355 "-crossbloat", "left_coverages");
356 g->codeAppend ("}");
Chris Dalton04a1de52018-03-14 02:04:09 -0600357
Chris Dalton4c239342018-04-05 18:43:40 -0600358 g->codeAppend ("half2 right_coverages; {");
359 Shader::CalcEdgeCoveragesAtBloatVertices(g, "corner", "right", "-outbloat",
360 "crossbloat", "right_coverages");
361 g->codeAppend ("}");
Chris Dalton04a1de52018-03-14 02:04:09 -0600362
Chris Dalton4c239342018-04-05 18:43:40 -0600363 // Emit a corner box. The first coverage argument erases the values that were written
364 // previously by the hull and edge geometry. The second pair are multiplied together by
365 // the fragment shader. They ramp to 0 with attenuation in the direction of outbloat,
366 // and linearly from left-edge coverage to right-edge coverage in the direction of
367 // crossbloat.
368 //
369 // NOTE: Since this is not a linear mapping, it is important that the box's diagonal
370 // shared edge points in the direction of outbloat.
Chris Dalton74231952019-01-18 15:53:20 -0700371 g->codeAppendf("%s(corner, -crossbloat, right_coverages[1] - left_coverages[1],"
Chris Dalton4c239342018-04-05 18:43:40 -0600372 "half2(1 + left_coverages[1], 1));",
373 emitVertexFn);
Chris Dalton04a1de52018-03-14 02:04:09 -0600374
Chris Dalton74231952019-01-18 15:53:20 -0700375 g->codeAppendf("%s(corner, outbloat, 1 + left_coverages[0] + right_coverages[0], "
376 "half2(0, attenuation));",
Chris Dalton4c239342018-04-05 18:43:40 -0600377 emitVertexFn);
378
Chris Dalton74231952019-01-18 15:53:20 -0700379 g->codeAppendf("%s(corner, -outbloat, -1 - left_coverages[0] - right_coverages[0], "
Chris Dalton4c239342018-04-05 18:43:40 -0600380 "half2(1 + left_coverages[0] + right_coverages[0], 1));",
381 emitVertexFn);
382
Chris Dalton74231952019-01-18 15:53:20 -0700383 g->codeAppendf("%s(corner, crossbloat, left_coverages[1] - right_coverages[1],"
Chris Dalton4c239342018-04-05 18:43:40 -0600384 "half2(1 + right_coverages[1], 1));",
385 emitVertexFn);
386 } else {
Chris Dalton84d36cd2019-04-17 14:47:17 -0600387 // Curves are simpler. Setting "wind = -wind" causes the Shader to erase what it had
388 // written in the previous pass hull. Then, at each vertex of the corner box, the Shader
389 // will calculate the curve's local coverage value, interpolate it alongside our
390 // attenuation parameter, and multiply the two together for a final coverage value.
391 g->codeAppendf("%s = -%s;", wind.c_str(), wind.c_str());
392 if (!fShader->calculatesOwnEdgeCoverage()) {
393 g->codeAppendf("%s = -%s;",
394 fEdgeDistanceEquation.c_str(), fEdgeDistanceEquation.c_str());
395 }
396 g->codeAppendf("%s(corner, -crossbloat, half2(-1, 1));", emitVertexFn);
397 g->codeAppendf("%s(corner, outbloat, half2(0, attenuation));",
Chris Dalton4c239342018-04-05 18:43:40 -0600398 emitVertexFn);
Chris Dalton84d36cd2019-04-17 14:47:17 -0600399 g->codeAppendf("%s(corner, -outbloat, half2(-1, 1));", emitVertexFn);
400 g->codeAppendf("%s(corner, crossbloat, half2(-1, 1));", emitVertexFn);
Chris Dalton4c239342018-04-05 18:43:40 -0600401 }
Chris Dalton8738cf42018-03-09 11:57:40 -0700402
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600403 g->configure(InputType::kLines, OutputType::kTriangleStrip, 4, proc.isTriangles() ? 3 : 2);
Chris Dalton8738cf42018-03-09 11:57:40 -0700404 }
405};
406
Chris Dalton2c5e0112019-03-29 13:14:18 -0500407void GrGSCoverageProcessor::reset(PrimitiveType primitiveType, GrResourceProvider*) {
408 fPrimitiveType = primitiveType; // This will affect the return values for numInputPoints, etc.
409
Chris Dalton9f2dab02018-04-18 14:07:03 -0600410 if (4 == this->numInputPoints() || this->hasInputWeight()) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500411 fInputXOrYValues =
Brian Osmand4c29702018-09-14 16:16:55 -0400412 {"x_or_y_values", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Salomon92be2f72018-06-19 14:33:47 -0400413 GR_STATIC_ASSERT(sizeof(QuadPointInstance) ==
414 2 * GrVertexAttribTypeSize(kFloat4_GrVertexAttribType));
415 GR_STATIC_ASSERT(offsetof(QuadPointInstance, fY) ==
416 GrVertexAttribTypeSize(kFloat4_GrVertexAttribType));
Chris Dalton23261772017-12-10 16:41:45 -0700417 } else {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500418 fInputXOrYValues =
Brian Osmand4c29702018-09-14 16:16:55 -0400419 {"x_or_y_values", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon92be2f72018-06-19 14:33:47 -0400420 GR_STATIC_ASSERT(sizeof(TriPointInstance) ==
421 2 * GrVertexAttribTypeSize(kFloat3_GrVertexAttribType));
422 GR_STATIC_ASSERT(offsetof(TriPointInstance, fY) ==
423 GrVertexAttribTypeSize(kFloat3_GrVertexAttribType));
Chris Dalton23261772017-12-10 16:41:45 -0700424 }
Chris Dalton2c5e0112019-03-29 13:14:18 -0500425
426 this->setVertexAttributes(&fInputXOrYValues, 1);
Chris Dalton23261772017-12-10 16:41:45 -0700427}
428
Chris Dalton2c5e0112019-03-29 13:14:18 -0500429void GrGSCoverageProcessor::appendMesh(sk_sp<const GrGpuBuffer> instanceBuffer, int instanceCount,
430 int baseInstance, SkTArray<GrMesh>* out) const {
431 // We don't actually make instanced draw calls. Instead, we feed transposed x,y point values to
432 // the GPU in a regular vertex array and draw kLines (see initGS). Then, each vertex invocation
433 // receives either the shape's x or y values as inputs, which it forwards to the geometry
434 // shader.
Chris Dalton23261772017-12-10 16:41:45 -0700435 GrMesh& mesh = out->emplace_back(GrPrimitiveType::kLines);
436 mesh.setNonIndexedNonInstanced(instanceCount * 2);
Brian Salomon12d22642019-01-29 14:38:50 -0500437 mesh.setVertexData(std::move(instanceBuffer), baseInstance * 2);
Chris Dalton23261772017-12-10 16:41:45 -0700438}
Chris Dalton1fbdb612017-12-12 12:48:47 -0700439
Chris Dalton2c5e0112019-03-29 13:14:18 -0500440void GrGSCoverageProcessor::draw(
441 GrOpFlushState* flushState, const GrPipeline& pipeline, const SkIRect scissorRects[],
442 const GrMesh meshes[], int meshCount, const SkRect& drawBounds) const {
443 // The geometry shader impl draws primitives in two subpasses: The first pass fills the interior
444 // and does edge AA. The second pass does touch up on corner pixels.
445 for (int i = 0; i < 2; ++i) {
446 fSubpass = (Subpass) i;
447 this->GrCCCoverageProcessor::draw(
448 flushState, pipeline, scissorRects, meshes, meshCount, drawBounds);
Chris Dalton1fbdb612017-12-12 12:48:47 -0700449 }
Chris Dalton2c5e0112019-03-29 13:14:18 -0500450}
451
452GrGLSLPrimitiveProcessor* GrGSCoverageProcessor::onCreateGLSLInstance(
453 std::unique_ptr<Shader> shader) const {
454 if (Subpass::kHulls == fSubpass) {
455 return this->isTriangles()
456 ? (Impl*) new TriangleHullImpl(std::move(shader))
457 : (Impl*) new CurveHullImpl(std::move(shader));
458 }
459 SkASSERT(Subpass::kCorners == fSubpass);
460 return new CornerImpl(std::move(shader));
Chris Dalton1fbdb612017-12-12 12:48:47 -0700461}