blob: feaac1a1439c3727e50bb3c0e7450206a412f458 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ccpr/GrGSCoverageProcessor.h"
Chris Dalton6a3dbee2017-10-16 10:44:41 -06009
Chris Dalton39ca9732020-03-10 10:34:17 -060010#include "src/gpu/GrOpsRenderPass.h"
Robert Phillips03e4c952019-11-26 16:20:22 -050011#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton6a3dbee2017-10-16 10:44:41 -060013
Chris Dalton1fbdb612017-12-12 12:48:47 -070014using InputType = GrGLSLGeometryBuilder::InputType;
15using OutputType = GrGLSLGeometryBuilder::OutputType;
Chris Dalton6a3dbee2017-10-16 10:44:41 -060016
17/**
18 * This class and its subclasses implement the coverage processor with geometry shaders.
19 */
Chris Dalton2c5e0112019-03-29 13:14:18 -050020class GrGSCoverageProcessor::Impl : public GrGLSLGeometryProcessor {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060021protected:
Chris Dalton2c5e0112019-03-29 13:14:18 -050022 Impl(std::unique_ptr<Shader> shader) : fShader(std::move(shader)) {}
Chris Dalton6a3dbee2017-10-16 10:44:41 -060023
Chris Dalton84d36cd2019-04-17 14:47:17 -060024 virtual bool hasCoverage(const GrGSCoverageProcessor& proc) const { return false; }
Chris Dalton21ba5512018-03-21 17:20:21 -060025
Chris Dalton6a3dbee2017-10-16 10:44:41 -060026 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
Brian Salomonc241b582019-11-27 08:57:17 -050027 const CoordTransformRange& transformRange) final {
28 this->setTransformDataHelper(SkMatrix::I(), pdman, transformRange);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060029 }
30
31 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
Chris Dalton2c5e0112019-03-29 13:14:18 -050032 const GrGSCoverageProcessor& proc = args.fGP.cast<GrGSCoverageProcessor>();
Chris Dalton6a3dbee2017-10-16 10:44:41 -060033
Chris Dalton23261772017-12-10 16:41:45 -070034 // The vertex shader simply forwards transposed x or y values to the geometry shader.
Brian Salomon92be2f72018-06-19 14:33:47 -040035 SkASSERT(1 == proc.numVertexAttributes());
Chris Dalton2c5e0112019-03-29 13:14:18 -050036 gpArgs->fPositionVar = proc.fInputXOrYValues.asShaderVar();
Chris Dalton6a3dbee2017-10-16 10:44:41 -060037
38 // Geometry shader.
39 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
40 this->emitGeometryShader(proc, varyingHandler, args.fGeomBuilder, args.fRTAdjustName);
41 varyingHandler->emitAttributes(proc);
Chris Dalton23261772017-12-10 16:41:45 -070042 varyingHandler->setNoPerspective();
Brian Salomon7d8b3972019-11-26 22:34:44 -050043 SkASSERT(!*args.fFPCoordTransformHandler);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060044
45 // Fragment shader.
Chris Daltonc3318f02019-07-19 14:20:53 -060046 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
47 f->codeAppendf("half coverage;");
48 fShader->emitFragmentCoverageCode(f, "coverage");
49 f->codeAppendf("%s = half4(coverage);", args.fOutputColor);
50 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060051 }
52
Chris Dalton2c5e0112019-03-29 13:14:18 -050053 void emitGeometryShader(
54 const GrGSCoverageProcessor& proc, GrGLSLVaryingHandler* varyingHandler,
55 GrGLSLGeometryBuilder* g, const char* rtAdjust) const {
Chris Dalton43646532017-12-07 12:47:02 -070056 int numInputPoints = proc.numInputPoints();
57 SkASSERT(3 == numInputPoints || 4 == numInputPoints);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060058
Chris Dalton9f2dab02018-04-18 14:07:03 -060059 int inputWidth = (4 == numInputPoints || proc.hasInputWeight()) ? 4 : 3;
60 const char* posValues = (4 == inputWidth) ? "sk_Position" : "sk_Position.xyz";
Chris Dalton23261772017-12-10 16:41:45 -070061 g->codeAppendf("float%ix2 pts = transpose(float2x%i(sk_in[0].%s, sk_in[1].%s));",
Chris Dalton9f2dab02018-04-18 14:07:03 -060062 inputWidth, inputWidth, posValues, posValues);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060063
64 GrShaderVar wind("wind", kHalf_GrSLType);
65 g->declareGlobal(wind);
Chris Dalton6f5e77a2018-04-23 21:14:42 -060066 Shader::CalcWind(proc, g, "pts", wind.c_str());
Chris Dalton2c5e0112019-03-29 13:14:18 -050067 if (PrimitiveType::kWeightedTriangles == proc.primitiveType()) {
Chris Dalton84403d72018-02-13 21:46:17 -050068 SkASSERT(3 == numInputPoints);
Chris Dalton2c5e0112019-03-29 13:14:18 -050069 SkASSERT(kFloat4_GrVertexAttribType == proc.fInputXOrYValues.cpuType());
Ethan Nicholase1f55022019-02-05 17:17:40 -050070 g->codeAppendf("%s *= half(sk_in[0].sk_Position.w);", wind.c_str());
Chris Dalton43646532017-12-07 12:47:02 -070071 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -060072
73 SkString emitVertexFn;
Chris Daltonc3318f02019-07-19 14:20:53 -060074 SkSTArray<3, GrShaderVar> emitArgs;
Chris Dalton74231952019-01-18 15:53:20 -070075 const char* corner = emitArgs.emplace_back("corner", kFloat2_GrSLType).c_str();
76 const char* bloatdir = emitArgs.emplace_back("bloatdir", kFloat2_GrSLType).c_str();
Chris Dalton84d36cd2019-04-17 14:47:17 -060077 const char* inputCoverage = nullptr;
78 if (this->hasCoverage(proc)) {
79 inputCoverage = emitArgs.emplace_back("coverage", kHalf_GrSLType).c_str();
Chris Daltonfe462ef2018-03-08 15:54:01 +000080 }
Chris Dalton4c239342018-04-05 18:43:40 -060081 const char* cornerCoverage = nullptr;
Chris Dalton2c5e0112019-03-29 13:14:18 -050082 if (Subpass::kCorners == proc.fSubpass) {
Chris Dalton4c239342018-04-05 18:43:40 -060083 cornerCoverage = emitArgs.emplace_back("corner_coverage", kHalf2_GrSLType).c_str();
Chris Dalton04a1de52018-03-14 02:04:09 -060084 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -060085 g->emitFunction(kVoid_GrSLType, "emitVertex", emitArgs.count(), emitArgs.begin(), [&]() {
86 SkString fnBody;
Chris Dalton84d36cd2019-04-17 14:47:17 -060087 fnBody.appendf("float2 vertexpos = fma(%s, float2(bloat), %s);", bloatdir, corner);
88 const char* coverage = inputCoverage;
89 if (!coverage) {
90 if (!fShader->calculatesOwnEdgeCoverage()) {
91 // Flat edge opposite the curve. Coverages need full precision since distance
92 // to the opposite edge can be large.
93 fnBody.appendf("float coverage = dot(float3(vertexpos, 1), %s);",
94 fEdgeDistanceEquation.c_str());
95 } else {
96 // The "coverage" param should hold only the signed winding value.
97 fnBody.appendf("float coverage = 1;");
98 }
99 coverage = "coverage";
Chris Dalton21ba5512018-03-21 17:20:21 -0600100 }
Chris Dalton84d36cd2019-04-17 14:47:17 -0600101 fnBody.appendf("%s *= %s;", coverage, wind.c_str());
Chris Dalton4c239342018-04-05 18:43:40 -0600102 if (cornerCoverage) {
103 fnBody.appendf("%s.x *= %s;", cornerCoverage, wind.c_str());
Chris Dalton21ba5512018-03-21 17:20:21 -0600104 }
Chris Dalton90e8fb12017-12-22 02:24:53 -0700105 fShader->emitVaryings(varyingHandler, GrGLSLVarying::Scope::kGeoToFrag, &fnBody,
Chris Dalton84d36cd2019-04-17 14:47:17 -0600106 "vertexpos", coverage, cornerCoverage, wind.c_str());
Chris Dalton74231952019-01-18 15:53:20 -0700107 g->emitVertex(&fnBody, "vertexpos", rtAdjust);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600108 return fnBody;
109 }().c_str(), &emitVertexFn);
110
Chris Daltonc17bf322017-10-24 10:59:03 -0600111 float bloat = kAABloatRadius;
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600112#ifdef SK_DEBUG
Chris Dalton8d38a7f2018-03-19 14:16:44 -0600113 if (proc.debugBloatEnabled()) {
Chris Daltonc17bf322017-10-24 10:59:03 -0600114 bloat *= proc.debugBloat();
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600115 }
116#endif
Chris Daltonc17bf322017-10-24 10:59:03 -0600117 g->defineConstant("bloat", bloat);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600118
Chris Dalton84d36cd2019-04-17 14:47:17 -0600119 if (!this->hasCoverage(proc) && !fShader->calculatesOwnEdgeCoverage()) {
120 // Determine the amount of coverage to subtract out for the flat edge of the curve.
121 g->declareGlobal(fEdgeDistanceEquation);
122 g->codeAppendf("float2 p0 = pts[0], p1 = pts[%i];", numInputPoints - 1);
123 g->codeAppendf("float2 n = float2(p0.y - p1.y, p1.x - p0.x);");
124 g->codeAppend ("float nwidth = bloat*2 * (abs(n.x) + abs(n.y));");
125 // When nwidth=0, wind must also be 0 (and coverage * wind = 0). So it doesn't matter
126 // what we come up with here as long as it isn't NaN or Inf.
127 g->codeAppend ("n /= (0 != nwidth) ? nwidth : 1;");
128 g->codeAppendf("%s = float3(-n, dot(n, p0) - .5*sign(%s));",
129 fEdgeDistanceEquation.c_str(), wind.c_str());
130 }
131
Chris Dalton21ba5512018-03-21 17:20:21 -0600132 this->onEmitGeometryShader(proc, g, wind, emitVertexFn.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600133 }
134
Chris Dalton2c5e0112019-03-29 13:14:18 -0500135 virtual void onEmitGeometryShader(const GrGSCoverageProcessor&, GrGLSLGeometryBuilder*,
Chris Dalton21ba5512018-03-21 17:20:21 -0600136 const GrShaderVar& wind, const char* emitVertexFn) const = 0;
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600137
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600138 const std::unique_ptr<Shader> fShader;
Chris Dalton84d36cd2019-04-17 14:47:17 -0600139 const GrShaderVar fEdgeDistanceEquation{"edge_distance_equation", kFloat3_GrSLType};
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600140
141 typedef GrGLSLGeometryProcessor INHERITED;
142};
143
Chris Dalton1fbdb612017-12-12 12:48:47 -0700144/**
Chris Dalton5183e642018-03-07 12:53:01 -0700145 * Generates conservative rasters around a triangle and its edges, and calculates coverage ramps.
146 *
147 * Triangle rough outlines are drawn in two steps: (1) draw a conservative raster of the entire
148 * triangle, with a coverage of +1, and (2) draw conservative rasters around each edge, with a
149 * coverage ramp from -1 to 0. These edge coverage values convert jagged conservative raster edges
150 * into smooth, antialiased ones.
151 *
Chris Dalton2c5e0112019-03-29 13:14:18 -0500152 * The final corners get touched up in a later step by TriangleCornerImpl.
Chris Dalton1fbdb612017-12-12 12:48:47 -0700153 */
Chris Dalton2c5e0112019-03-29 13:14:18 -0500154class GrGSCoverageProcessor::TriangleHullImpl : public GrGSCoverageProcessor::Impl {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600155public:
Chris Dalton2c5e0112019-03-29 13:14:18 -0500156 TriangleHullImpl(std::unique_ptr<Shader> shader) : Impl(std::move(shader)) {}
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600157
Chris Dalton84d36cd2019-04-17 14:47:17 -0600158 bool hasCoverage(const GrGSCoverageProcessor& proc) const override { return true; }
Chris Dalton21ba5512018-03-21 17:20:21 -0600159
Chris Dalton2c5e0112019-03-29 13:14:18 -0500160 void onEmitGeometryShader(const GrGSCoverageProcessor&, GrGLSLGeometryBuilder* g,
Chris Dalton21ba5512018-03-21 17:20:21 -0600161 const GrShaderVar& wind, const char* emitVertexFn) const override {
Chris Dalton84d36cd2019-04-17 14:47:17 -0600162 fShader->emitSetupCode(g, "pts");
Chris Dalton8738cf42018-03-09 11:57:40 -0700163
Chris Dalton1fbdb612017-12-12 12:48:47 -0700164 // Visualize the input triangle as upright and equilateral, with a flat base. Paying special
165 // attention to wind, we can identify the points as top, bottom-left, and bottom-right.
166 //
Chris Dalton5183e642018-03-07 12:53:01 -0700167 // NOTE: We generate the rasters in 5 independent invocations, so each invocation designates
Chris Dalton1fbdb612017-12-12 12:48:47 -0700168 // the corner it will begin with as the top.
Chris Dalton5183e642018-03-07 12:53:01 -0700169 g->codeAppendf("int i = (%s > 0 ? sk_InvocationID : 4 - sk_InvocationID) %% 3;",
170 wind.c_str());
171 g->codeAppend ("float2 top = pts[i];");
172 g->codeAppendf("float2 right = pts[(i + (%s > 0 ? 1 : 2)) %% 3];", wind.c_str());
173 g->codeAppendf("float2 left = pts[(i + (%s > 0 ? 2 : 1)) %% 3];", wind.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600174
Chris Dalton5183e642018-03-07 12:53:01 -0700175 // Determine which direction to outset the conservative raster from each of the three edges.
176 g->codeAppend ("float2 leftbloat = sign(top - left);");
177 g->codeAppend ("leftbloat = float2(0 != leftbloat.y ? leftbloat.y : leftbloat.x, "
178 "0 != leftbloat.x ? -leftbloat.x : -leftbloat.y);");
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600179
Chris Dalton5183e642018-03-07 12:53:01 -0700180 g->codeAppend ("float2 rightbloat = sign(right - top);");
181 g->codeAppend ("rightbloat = float2(0 != rightbloat.y ? rightbloat.y : rightbloat.x, "
182 "0 != rightbloat.x ? -rightbloat.x : -rightbloat.y);");
183
184 g->codeAppend ("float2 downbloat = sign(left - right);");
185 g->codeAppend ("downbloat = float2(0 != downbloat.y ? downbloat.y : downbloat.x, "
186 "0 != downbloat.x ? -downbloat.x : -downbloat.y);");
187
188 // The triangle's conservative raster has a coverage of +1 all around.
189 g->codeAppend ("half4 coverages = half4(+1);");
190
191 // Edges have coverage ramps.
192 g->codeAppend ("if (sk_InvocationID >= 2) {"); // Are we an edge?
193 Shader::CalcEdgeCoverageAtBloatVertex(g, "top", "right",
194 "float2(+rightbloat.y, -rightbloat.x)",
195 "coverages[0]");
196 g->codeAppend ( "coverages.yzw = half3(-1, 0, -1 - coverages[0]);");
197 // Reassign bloats to characterize a conservative raster around a single edge, rather than
198 // the entire triangle.
199 g->codeAppend ( "leftbloat = downbloat = -rightbloat;");
200 g->codeAppend ("}");
201
Chris Dalton5183e642018-03-07 12:53:01 -0700202 // Here we generate the conservative raster geometry. The triangle's conservative raster is
203 // the convex hull of 3 pixel-size boxes centered on the input points. This translates to a
204 // convex polygon with either one, two, or three vertices at each input point (depending on
205 // how sharp the corner is) that we split between two invocations. Edge conservative rasters
206 // are convex hulls of 2 pixel-size boxes, one at each endpoint. For more details on
207 // conservative raster, see:
Chris Dalton1fbdb612017-12-12 12:48:47 -0700208 // https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter42.html
209 g->codeAppendf("bool2 left_right_notequal = notEqual(leftbloat, rightbloat);");
210 g->codeAppend ("if (all(left_right_notequal)) {");
211 // The top corner will have three conservative raster vertices. Emit the
212 // middle one first to the triangle strip.
Chris Dalton74231952019-01-18 15:53:20 -0700213 g->codeAppendf( "%s(top, float2(-leftbloat.y, +leftbloat.x), coverages[0]);",
Chris Dalton5183e642018-03-07 12:53:01 -0700214 emitVertexFn);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600215 g->codeAppend ("}");
Chris Dalton1fbdb612017-12-12 12:48:47 -0700216 g->codeAppend ("if (any(left_right_notequal)) {");
217 // Second conservative raster vertex for the top corner.
Chris Dalton74231952019-01-18 15:53:20 -0700218 g->codeAppendf( "%s(top, rightbloat, coverages[1]);", emitVertexFn);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600219 g->codeAppend ("}");
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600220
Chris Dalton5183e642018-03-07 12:53:01 -0700221 // Main interior body.
Chris Dalton74231952019-01-18 15:53:20 -0700222 g->codeAppendf("%s(top, leftbloat, coverages[2]);", emitVertexFn);
223 g->codeAppendf("%s(right, rightbloat, coverages[1]);", emitVertexFn);
Chris Dalton1fbdb612017-12-12 12:48:47 -0700224
Chris Dalton5183e642018-03-07 12:53:01 -0700225 // Here the invocations diverge slightly. We can't symmetrically divide three triangle
226 // points between two invocations, so each does the following:
Chris Dalton1fbdb612017-12-12 12:48:47 -0700227 //
Chris Dalton5183e642018-03-07 12:53:01 -0700228 // sk_InvocationID=0: Finishes the main interior body of the triangle hull.
229 // sk_InvocationID=1: Remaining two conservative raster vertices for the third hull corner.
230 // sk_InvocationID=2..4: Finish the opposite endpoint of their corresponding edge.
Chris Dalton1fbdb612017-12-12 12:48:47 -0700231 g->codeAppendf("bool2 right_down_notequal = notEqual(rightbloat, downbloat);");
232 g->codeAppend ("if (any(right_down_notequal) || 0 == sk_InvocationID) {");
Chris Dalton74231952019-01-18 15:53:20 -0700233 g->codeAppendf( "%s((0 == sk_InvocationID) ? left : right, "
234 "(0 == sk_InvocationID) ? leftbloat : downbloat, "
Chris Dalton5183e642018-03-07 12:53:01 -0700235 "coverages[2]);", emitVertexFn);
Chris Dalton1fbdb612017-12-12 12:48:47 -0700236 g->codeAppend ("}");
237 g->codeAppend ("if (all(right_down_notequal) && 0 != sk_InvocationID) {");
Chris Dalton74231952019-01-18 15:53:20 -0700238 g->codeAppendf( "%s(right, float2(-rightbloat.y, +rightbloat.x), coverages[3]);",
Chris Dalton5183e642018-03-07 12:53:01 -0700239 emitVertexFn);
Chris Dalton1fbdb612017-12-12 12:48:47 -0700240 g->codeAppend ("}");
241
Chris Dalton5183e642018-03-07 12:53:01 -0700242 // 5 invocations: 2 triangle hull invocations and 3 edges.
243 g->configure(InputType::kLines, OutputType::kTriangleStrip, 6, 5);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600244 }
245};
246
Chris Dalton1fbdb612017-12-12 12:48:47 -0700247/**
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600248 * Generates a conservative raster around a convex quadrilateral that encloses a cubic or quadratic.
249 */
Chris Dalton2c5e0112019-03-29 13:14:18 -0500250class GrGSCoverageProcessor::CurveHullImpl : public GrGSCoverageProcessor::Impl {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600251public:
Chris Dalton2c5e0112019-03-29 13:14:18 -0500252 CurveHullImpl(std::unique_ptr<Shader> shader) : Impl(std::move(shader)) {}
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600253
Chris Dalton2c5e0112019-03-29 13:14:18 -0500254 void onEmitGeometryShader(const GrGSCoverageProcessor&, GrGLSLGeometryBuilder* g,
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600255 const GrShaderVar& wind, const char* emitVertexFn) const override {
256 const char* hullPts = "pts";
Chris Dalton84d36cd2019-04-17 14:47:17 -0600257 fShader->emitSetupCode(g, "pts", &hullPts);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600258
259 // Visualize the input (convex) quadrilateral as a square. Paying special attention to wind,
260 // we can identify the points by their corresponding corner.
261 //
262 // NOTE: We split the square down the diagonal from top-right to bottom-left, and generate
263 // the hull in two independent invocations. Each invocation designates the corner it will
264 // begin with as top-left.
265 g->codeAppend ("int i = sk_InvocationID * 2;");
266 g->codeAppendf("float2 topleft = %s[i];", hullPts);
267 g->codeAppendf("float2 topright = %s[%s > 0 ? i + 1 : 3 - i];", hullPts, wind.c_str());
268 g->codeAppendf("float2 bottomleft = %s[%s > 0 ? 3 - i : i + 1];", hullPts, wind.c_str());
269 g->codeAppendf("float2 bottomright = %s[2 - i];", hullPts);
270
271 // Determine how much to outset the conservative raster hull from the relevant edges.
Chris Dalton74231952019-01-18 15:53:20 -0700272 g->codeAppend ("float2 leftbloat = float2(topleft.y > bottomleft.y ? +1 : -1, "
273 "topleft.x > bottomleft.x ? -1 : +1);");
274 g->codeAppend ("float2 upbloat = float2(topright.y > topleft.y ? +1 : -1, "
275 "topright.x > topleft.x ? -1 : +1);");
276 g->codeAppend ("float2 rightbloat = float2(bottomright.y > topright.y ? +1 : -1, "
277 "bottomright.x > topright.x ? -1 : +1);");
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600278
279 // Here we generate the conservative raster geometry. It is the convex hull of 4 pixel-size
280 // boxes centered on the input points, split evenly between two invocations. This translates
281 // to a polygon with either one, two, or three vertices at each input point, depending on
282 // how sharp the corner is. For more details on conservative raster, see:
283 // https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter42.html
284 g->codeAppendf("bool2 left_up_notequal = notEqual(leftbloat, upbloat);");
285 g->codeAppend ("if (all(left_up_notequal)) {");
286 // The top-left corner will have three conservative raster vertices.
287 // Emit the middle one first to the triangle strip.
Chris Dalton74231952019-01-18 15:53:20 -0700288 g->codeAppendf( "%s(topleft, float2(-leftbloat.y, leftbloat.x));", emitVertexFn);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600289 g->codeAppend ("}");
290 g->codeAppend ("if (any(left_up_notequal)) {");
291 // Second conservative raster vertex for the top-left corner.
Chris Dalton74231952019-01-18 15:53:20 -0700292 g->codeAppendf( "%s(topleft, leftbloat);", emitVertexFn);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600293 g->codeAppend ("}");
294
295 // Main interior body of this invocation's half of the hull.
Chris Dalton74231952019-01-18 15:53:20 -0700296 g->codeAppendf("%s(topleft, upbloat);", emitVertexFn);
297 g->codeAppendf("%s(bottomleft, leftbloat);", emitVertexFn);
298 g->codeAppendf("%s(topright, upbloat);", emitVertexFn);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600299
300 // Remaining two conservative raster vertices for the top-right corner.
301 g->codeAppendf("bool2 up_right_notequal = notEqual(upbloat, rightbloat);");
302 g->codeAppend ("if (any(up_right_notequal)) {");
Chris Dalton74231952019-01-18 15:53:20 -0700303 g->codeAppendf( "%s(topright, rightbloat);", emitVertexFn);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600304 g->codeAppend ("}");
305 g->codeAppend ("if (all(up_right_notequal)) {");
Chris Dalton74231952019-01-18 15:53:20 -0700306 g->codeAppendf( "%s(topright, float2(-upbloat.y, upbloat.x));", emitVertexFn);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600307 g->codeAppend ("}");
308
309 g->configure(InputType::kLines, OutputType::kTriangleStrip, 7, 2);
310 }
311};
312
313/**
Chris Dalton21ba5512018-03-21 17:20:21 -0600314 * Generates conservative rasters around corners (aka pixel-size boxes) and calculates
315 * coverage and attenuation ramps to fix up the coverage values written by the hulls.
Chris Dalton8738cf42018-03-09 11:57:40 -0700316 */
Chris Dalton2c5e0112019-03-29 13:14:18 -0500317class GrGSCoverageProcessor::CornerImpl : public GrGSCoverageProcessor::Impl {
Chris Dalton8738cf42018-03-09 11:57:40 -0700318public:
Chris Dalton2c5e0112019-03-29 13:14:18 -0500319 CornerImpl(std::unique_ptr<Shader> shader) : Impl(std::move(shader)) {}
Chris Dalton8738cf42018-03-09 11:57:40 -0700320
Chris Dalton84d36cd2019-04-17 14:47:17 -0600321 bool hasCoverage(const GrGSCoverageProcessor& proc) const override {
322 return proc.isTriangles();
323 }
Chris Dalton8738cf42018-03-09 11:57:40 -0700324
Chris Dalton2c5e0112019-03-29 13:14:18 -0500325 void onEmitGeometryShader(const GrGSCoverageProcessor& proc, GrGLSLGeometryBuilder* g,
Chris Dalton21ba5512018-03-21 17:20:21 -0600326 const GrShaderVar& wind, const char* emitVertexFn) const override {
Chris Dalton84d36cd2019-04-17 14:47:17 -0600327 fShader->emitSetupCode(g, "pts");
Chris Dalton21ba5512018-03-21 17:20:21 -0600328
Chris Dalton21ba5512018-03-21 17:20:21 -0600329 g->codeAppendf("int corneridx = sk_InvocationID;");
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600330 if (!proc.isTriangles()) {
Chris Dalton21ba5512018-03-21 17:20:21 -0600331 g->codeAppendf("corneridx *= %i;", proc.numInputPoints() - 1);
332 }
333
334 g->codeAppendf("float2 corner = pts[corneridx];");
335 g->codeAppendf("float2 left = pts[(corneridx + (%s > 0 ? %i : 1)) %% %i];",
336 wind.c_str(), proc.numInputPoints() - 1, proc.numInputPoints());
337 g->codeAppendf("float2 right = pts[(corneridx + (%s > 0 ? 1 : %i)) %% %i];",
338 wind.c_str(), proc.numInputPoints() - 1, proc.numInputPoints());
Chris Dalton8738cf42018-03-09 11:57:40 -0700339
Chris Dalton04a1de52018-03-14 02:04:09 -0600340 g->codeAppend ("float2 leftdir = corner - left;");
341 g->codeAppend ("leftdir = (float2(0) != leftdir) ? normalize(leftdir) : float2(1, 0);");
342
343 g->codeAppend ("float2 rightdir = right - corner;");
344 g->codeAppend ("rightdir = (float2(0) != rightdir) ? normalize(rightdir) : float2(1, 0);");
345
Chris Dalton8738cf42018-03-09 11:57:40 -0700346 // Find "outbloat" and "crossbloat" at our corner. The outbloat points diagonally out of the
Chris Dalton04a1de52018-03-14 02:04:09 -0600347 // triangle, in the direction that should ramp to zero coverage with attenuation. The
348 // crossbloat runs perpindicular to outbloat.
Chris Dalton8738cf42018-03-09 11:57:40 -0700349 g->codeAppend ("float2 outbloat = float2(leftdir.x > rightdir.x ? +1 : -1, "
350 "leftdir.y > rightdir.y ? +1 : -1);");
351 g->codeAppend ("float2 crossbloat = float2(-outbloat.y, +outbloat.x);");
352
Chris Dalton04a1de52018-03-14 02:04:09 -0600353 g->codeAppend ("half attenuation; {");
Chris Dalton4c239342018-04-05 18:43:40 -0600354 Shader::CalcCornerAttenuation(g, "leftdir", "rightdir", "attenuation");
Chris Dalton04a1de52018-03-14 02:04:09 -0600355 g->codeAppend ("}");
356
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600357 if (proc.isTriangles()) {
Chris Dalton4c239342018-04-05 18:43:40 -0600358 g->codeAppend ("half2 left_coverages; {");
359 Shader::CalcEdgeCoveragesAtBloatVertices(g, "left", "corner", "-outbloat",
360 "-crossbloat", "left_coverages");
361 g->codeAppend ("}");
Chris Dalton04a1de52018-03-14 02:04:09 -0600362
Chris Dalton4c239342018-04-05 18:43:40 -0600363 g->codeAppend ("half2 right_coverages; {");
364 Shader::CalcEdgeCoveragesAtBloatVertices(g, "corner", "right", "-outbloat",
365 "crossbloat", "right_coverages");
366 g->codeAppend ("}");
Chris Dalton04a1de52018-03-14 02:04:09 -0600367
Chris Dalton4c239342018-04-05 18:43:40 -0600368 // Emit a corner box. The first coverage argument erases the values that were written
369 // previously by the hull and edge geometry. The second pair are multiplied together by
370 // the fragment shader. They ramp to 0 with attenuation in the direction of outbloat,
371 // and linearly from left-edge coverage to right-edge coverage in the direction of
372 // crossbloat.
373 //
374 // NOTE: Since this is not a linear mapping, it is important that the box's diagonal
375 // shared edge points in the direction of outbloat.
Chris Dalton74231952019-01-18 15:53:20 -0700376 g->codeAppendf("%s(corner, -crossbloat, right_coverages[1] - left_coverages[1],"
Chris Dalton4c239342018-04-05 18:43:40 -0600377 "half2(1 + left_coverages[1], 1));",
378 emitVertexFn);
Chris Dalton04a1de52018-03-14 02:04:09 -0600379
Chris Dalton74231952019-01-18 15:53:20 -0700380 g->codeAppendf("%s(corner, outbloat, 1 + left_coverages[0] + right_coverages[0], "
381 "half2(0, attenuation));",
Chris Dalton4c239342018-04-05 18:43:40 -0600382 emitVertexFn);
383
Chris Dalton74231952019-01-18 15:53:20 -0700384 g->codeAppendf("%s(corner, -outbloat, -1 - left_coverages[0] - right_coverages[0], "
Chris Dalton4c239342018-04-05 18:43:40 -0600385 "half2(1 + left_coverages[0] + right_coverages[0], 1));",
386 emitVertexFn);
387
Chris Dalton74231952019-01-18 15:53:20 -0700388 g->codeAppendf("%s(corner, crossbloat, left_coverages[1] - right_coverages[1],"
Chris Dalton4c239342018-04-05 18:43:40 -0600389 "half2(1 + right_coverages[1], 1));",
390 emitVertexFn);
391 } else {
Chris Dalton84d36cd2019-04-17 14:47:17 -0600392 // Curves are simpler. Setting "wind = -wind" causes the Shader to erase what it had
393 // written in the previous pass hull. Then, at each vertex of the corner box, the Shader
394 // will calculate the curve's local coverage value, interpolate it alongside our
395 // attenuation parameter, and multiply the two together for a final coverage value.
396 g->codeAppendf("%s = -%s;", wind.c_str(), wind.c_str());
397 if (!fShader->calculatesOwnEdgeCoverage()) {
398 g->codeAppendf("%s = -%s;",
399 fEdgeDistanceEquation.c_str(), fEdgeDistanceEquation.c_str());
400 }
401 g->codeAppendf("%s(corner, -crossbloat, half2(-1, 1));", emitVertexFn);
402 g->codeAppendf("%s(corner, outbloat, half2(0, attenuation));",
Chris Dalton4c239342018-04-05 18:43:40 -0600403 emitVertexFn);
Chris Dalton84d36cd2019-04-17 14:47:17 -0600404 g->codeAppendf("%s(corner, -outbloat, half2(-1, 1));", emitVertexFn);
405 g->codeAppendf("%s(corner, crossbloat, half2(-1, 1));", emitVertexFn);
Chris Dalton4c239342018-04-05 18:43:40 -0600406 }
Chris Dalton8738cf42018-03-09 11:57:40 -0700407
Chris Dalton6f5e77a2018-04-23 21:14:42 -0600408 g->configure(InputType::kLines, OutputType::kTriangleStrip, 4, proc.isTriangles() ? 3 : 2);
Chris Dalton8738cf42018-03-09 11:57:40 -0700409 }
410};
411
Chris Dalton39ca9732020-03-10 10:34:17 -0600412void GrGSCoverageProcessor::reset(PrimitiveType primitiveType, int subpassIdx,
413 GrResourceProvider*) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500414 fPrimitiveType = primitiveType; // This will affect the return values for numInputPoints, etc.
415
Chris Dalton9f2dab02018-04-18 14:07:03 -0600416 if (4 == this->numInputPoints() || this->hasInputWeight()) {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500417 fInputXOrYValues =
Brian Osmand4c29702018-09-14 16:16:55 -0400418 {"x_or_y_values", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Salomon4dea72a2019-12-18 10:43:10 -0500419 static_assert(sizeof(QuadPointInstance) ==
420 2 * GrVertexAttribTypeSize(kFloat4_GrVertexAttribType));
421 static_assert(offsetof(QuadPointInstance, fY) ==
422 GrVertexAttribTypeSize(kFloat4_GrVertexAttribType));
Chris Dalton23261772017-12-10 16:41:45 -0700423 } else {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500424 fInputXOrYValues =
Brian Osmand4c29702018-09-14 16:16:55 -0400425 {"x_or_y_values", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
Brian Salomon4dea72a2019-12-18 10:43:10 -0500426 static_assert(sizeof(TriPointInstance) ==
427 2 * GrVertexAttribTypeSize(kFloat3_GrVertexAttribType));
Chris Dalton23261772017-12-10 16:41:45 -0700428 }
Chris Dalton2c5e0112019-03-29 13:14:18 -0500429
430 this->setVertexAttributes(&fInputXOrYValues, 1);
Chris Dalton39ca9732020-03-10 10:34:17 -0600431
432 SkASSERT(subpassIdx == 0 || subpassIdx == 1);
433 fSubpass = (Subpass)subpassIdx;
Chris Dalton23261772017-12-10 16:41:45 -0700434}
435
Chris Dalton39ca9732020-03-10 10:34:17 -0600436void GrGSCoverageProcessor::bindBuffers(GrOpsRenderPass* renderPass,
437 const GrBuffer* instanceBuffer) const {
438 renderPass->bindBuffers(nullptr, nullptr, instanceBuffer);
439}
440
441void GrGSCoverageProcessor::drawInstances(GrOpsRenderPass* renderPass, int instanceCount,
442 int baseInstance) const {
Chris Dalton2c5e0112019-03-29 13:14:18 -0500443 // We don't actually make instanced draw calls. Instead, we feed transposed x,y point values to
444 // the GPU in a regular vertex array and draw kLines (see initGS). Then, each vertex invocation
445 // receives either the shape's x or y values as inputs, which it forwards to the geometry
446 // shader.
Chris Dalton39ca9732020-03-10 10:34:17 -0600447 renderPass->draw(instanceCount * 2, baseInstance * 2);
Chris Dalton2c5e0112019-03-29 13:14:18 -0500448}
449
450GrGLSLPrimitiveProcessor* GrGSCoverageProcessor::onCreateGLSLInstance(
451 std::unique_ptr<Shader> shader) const {
452 if (Subpass::kHulls == fSubpass) {
453 return this->isTriangles()
454 ? (Impl*) new TriangleHullImpl(std::move(shader))
455 : (Impl*) new CurveHullImpl(std::move(shader));
456 }
457 SkASSERT(Subpass::kCorners == fSubpass);
458 return new CornerImpl(std::move(shader));
Chris Dalton1fbdb612017-12-12 12:48:47 -0700459}