Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 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 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 8 | #include "GrCCCoverageProcessor.h" |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 9 | |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 10 | #include "GrMesh.h" |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 11 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 12 | |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 13 | using InputType = GrGLSLGeometryBuilder::InputType; |
| 14 | using OutputType = GrGLSLGeometryBuilder::OutputType; |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * This class and its subclasses implement the coverage processor with geometry shaders. |
| 18 | */ |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 19 | class GrCCCoverageProcessor::GSImpl : public GrGLSLGeometryProcessor { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 20 | protected: |
| 21 | GSImpl(std::unique_ptr<Shader> shader) : fShader(std::move(shader)) {} |
| 22 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 23 | virtual bool hasCoverage() const { return false; } |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 24 | |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 25 | 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 Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 31 | const GrCCCoverageProcessor& proc = args.fGP.cast<GrCCCoverageProcessor>(); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 32 | |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 33 | // The vertex shader simply forwards transposed x or y values to the geometry shader. |
| 34 | SkASSERT(1 == proc.numAttribs()); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 35 | gpArgs->fPositionVar.set(GrVertexAttribTypeToSLType(proc.getAttrib(0).fType), |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 36 | proc.getAttrib(0).fName); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 37 | |
| 38 | // Geometry shader. |
| 39 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 40 | this->emitGeometryShader(proc, varyingHandler, args.fGeomBuilder, args.fRTAdjustName); |
| 41 | varyingHandler->emitAttributes(proc); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 42 | varyingHandler->setNoPerspective(); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 43 | SkASSERT(!args.fFPCoordTransformHandler->nextCoordTransform()); |
| 44 | |
| 45 | // Fragment shader. |
| 46 | fShader->emitFragmentCode(proc, args.fFragBuilder, args.fOutputColor, args.fOutputCoverage); |
| 47 | } |
| 48 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 49 | void emitGeometryShader(const GrCCCoverageProcessor& proc, |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 50 | GrGLSLVaryingHandler* varyingHandler, GrGLSLGeometryBuilder* g, |
| 51 | const char* rtAdjust) const { |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 52 | int numInputPoints = proc.numInputPoints(); |
| 53 | SkASSERT(3 == numInputPoints || 4 == numInputPoints); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 54 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 55 | int inputWidth = (4 == numInputPoints || proc.hasInputWeight()) ? 4 : 3; |
| 56 | const char* posValues = (4 == inputWidth) ? "sk_Position" : "sk_Position.xyz"; |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 57 | g->codeAppendf("float%ix2 pts = transpose(float2x%i(sk_in[0].%s, sk_in[1].%s));", |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 58 | inputWidth, inputWidth, posValues, posValues); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 59 | |
| 60 | GrShaderVar wind("wind", kHalf_GrSLType); |
| 61 | g->declareGlobal(wind); |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 62 | Shader::CalcWind(proc, g, "pts", wind.c_str()); |
| 63 | if (PrimitiveType::kWeightedTriangles == proc.fPrimitiveType) { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 64 | SkASSERT(3 == numInputPoints); |
| 65 | SkASSERT(kFloat4_GrVertexAttribType == proc.getAttrib(0).fType); |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 66 | g->codeAppendf("%s *= sk_in[0].sk_Position.w;", wind.c_str()); |
Chris Dalton | 4364653 | 2017-12-07 12:47:02 -0700 | [diff] [blame] | 67 | } |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 68 | |
| 69 | SkString emitVertexFn; |
| 70 | SkSTArray<2, GrShaderVar> emitArgs; |
| 71 | const char* position = emitArgs.emplace_back("position", kFloat2_GrSLType).c_str(); |
Chris Dalton | fe462ef | 2018-03-08 15:54:01 +0000 | [diff] [blame] | 72 | const char* coverage = nullptr; |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 73 | if (this->hasCoverage()) { |
Chris Dalton | fe462ef | 2018-03-08 15:54:01 +0000 | [diff] [blame] | 74 | coverage = emitArgs.emplace_back("coverage", kHalf_GrSLType).c_str(); |
| 75 | } |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 76 | const char* cornerCoverage = nullptr; |
| 77 | if (GSSubpass::kCorners == proc.fGSSubpass) { |
| 78 | cornerCoverage = emitArgs.emplace_back("corner_coverage", kHalf2_GrSLType).c_str(); |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 79 | } |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 80 | g->emitFunction(kVoid_GrSLType, "emitVertex", emitArgs.count(), emitArgs.begin(), [&]() { |
| 81 | SkString fnBody; |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 82 | if (coverage) { |
| 83 | fnBody.appendf("%s *= %s;", coverage, wind.c_str()); |
| 84 | } |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 85 | if (cornerCoverage) { |
| 86 | fnBody.appendf("%s.x *= %s;", cornerCoverage, wind.c_str()); |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 87 | } |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 88 | fShader->emitVaryings(varyingHandler, GrGLSLVarying::Scope::kGeoToFrag, &fnBody, |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 89 | position, coverage ? coverage : wind.c_str(), cornerCoverage); |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 90 | g->emitVertex(&fnBody, position, rtAdjust); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 91 | return fnBody; |
| 92 | }().c_str(), &emitVertexFn); |
| 93 | |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 94 | float bloat = kAABloatRadius; |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 95 | #ifdef SK_DEBUG |
Chris Dalton | 8d38a7f | 2018-03-19 14:16:44 -0600 | [diff] [blame] | 96 | if (proc.debugBloatEnabled()) { |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 97 | bloat *= proc.debugBloat(); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 98 | } |
| 99 | #endif |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 100 | g->defineConstant("bloat", bloat); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 101 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 102 | this->onEmitGeometryShader(proc, g, wind, emitVertexFn.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 103 | } |
| 104 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 105 | virtual void onEmitGeometryShader(const GrCCCoverageProcessor&, GrGLSLGeometryBuilder*, |
| 106 | const GrShaderVar& wind, const char* emitVertexFn) const = 0; |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 107 | |
| 108 | virtual ~GSImpl() {} |
| 109 | |
| 110 | const std::unique_ptr<Shader> fShader; |
| 111 | |
| 112 | typedef GrGLSLGeometryProcessor INHERITED; |
| 113 | }; |
| 114 | |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 115 | /** |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 116 | * Generates conservative rasters around a triangle and its edges, and calculates coverage ramps. |
| 117 | * |
| 118 | * Triangle rough outlines are drawn in two steps: (1) draw a conservative raster of the entire |
| 119 | * triangle, with a coverage of +1, and (2) draw conservative rasters around each edge, with a |
| 120 | * coverage ramp from -1 to 0. These edge coverage values convert jagged conservative raster edges |
| 121 | * into smooth, antialiased ones. |
| 122 | * |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 123 | * The final corners get touched up in a later step by GSTriangleCornerImpl. |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 124 | */ |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 125 | class GrCCCoverageProcessor::GSTriangleHullImpl : public GrCCCoverageProcessor::GSImpl { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 126 | public: |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 127 | GSTriangleHullImpl(std::unique_ptr<Shader> shader) : GSImpl(std::move(shader)) {} |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 128 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 129 | bool hasCoverage() const override { return true; } |
| 130 | |
| 131 | void onEmitGeometryShader(const GrCCCoverageProcessor&, GrGLSLGeometryBuilder* g, |
| 132 | const GrShaderVar& wind, const char* emitVertexFn) const override { |
| 133 | fShader->emitSetupCode(g, "pts", wind.c_str()); |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 134 | |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 135 | // Visualize the input triangle as upright and equilateral, with a flat base. Paying special |
| 136 | // attention to wind, we can identify the points as top, bottom-left, and bottom-right. |
| 137 | // |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 138 | // NOTE: We generate the rasters in 5 independent invocations, so each invocation designates |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 139 | // the corner it will begin with as the top. |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 140 | g->codeAppendf("int i = (%s > 0 ? sk_InvocationID : 4 - sk_InvocationID) %% 3;", |
| 141 | wind.c_str()); |
| 142 | g->codeAppend ("float2 top = pts[i];"); |
| 143 | g->codeAppendf("float2 right = pts[(i + (%s > 0 ? 1 : 2)) %% 3];", wind.c_str()); |
| 144 | g->codeAppendf("float2 left = pts[(i + (%s > 0 ? 2 : 1)) %% 3];", wind.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 145 | |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 146 | // Determine which direction to outset the conservative raster from each of the three edges. |
| 147 | g->codeAppend ("float2 leftbloat = sign(top - left);"); |
| 148 | g->codeAppend ("leftbloat = float2(0 != leftbloat.y ? leftbloat.y : leftbloat.x, " |
| 149 | "0 != leftbloat.x ? -leftbloat.x : -leftbloat.y);"); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 150 | |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 151 | g->codeAppend ("float2 rightbloat = sign(right - top);"); |
| 152 | g->codeAppend ("rightbloat = float2(0 != rightbloat.y ? rightbloat.y : rightbloat.x, " |
| 153 | "0 != rightbloat.x ? -rightbloat.x : -rightbloat.y);"); |
| 154 | |
| 155 | g->codeAppend ("float2 downbloat = sign(left - right);"); |
| 156 | g->codeAppend ("downbloat = float2(0 != downbloat.y ? downbloat.y : downbloat.x, " |
| 157 | "0 != downbloat.x ? -downbloat.x : -downbloat.y);"); |
| 158 | |
| 159 | // The triangle's conservative raster has a coverage of +1 all around. |
| 160 | g->codeAppend ("half4 coverages = half4(+1);"); |
| 161 | |
| 162 | // Edges have coverage ramps. |
| 163 | g->codeAppend ("if (sk_InvocationID >= 2) {"); // Are we an edge? |
| 164 | Shader::CalcEdgeCoverageAtBloatVertex(g, "top", "right", |
| 165 | "float2(+rightbloat.y, -rightbloat.x)", |
| 166 | "coverages[0]"); |
| 167 | g->codeAppend ( "coverages.yzw = half3(-1, 0, -1 - coverages[0]);"); |
| 168 | // Reassign bloats to characterize a conservative raster around a single edge, rather than |
| 169 | // the entire triangle. |
| 170 | g->codeAppend ( "leftbloat = downbloat = -rightbloat;"); |
| 171 | g->codeAppend ("}"); |
| 172 | |
| 173 | // These can't be scaled until after we calculate coverage. |
| 174 | g->codeAppend ("leftbloat *= bloat;"); |
| 175 | g->codeAppend ("rightbloat *= bloat;"); |
| 176 | g->codeAppend ("downbloat *= bloat;"); |
| 177 | |
| 178 | // Here we generate the conservative raster geometry. The triangle's conservative raster is |
| 179 | // the convex hull of 3 pixel-size boxes centered on the input points. This translates to a |
| 180 | // convex polygon with either one, two, or three vertices at each input point (depending on |
| 181 | // how sharp the corner is) that we split between two invocations. Edge conservative rasters |
| 182 | // are convex hulls of 2 pixel-size boxes, one at each endpoint. For more details on |
| 183 | // conservative raster, see: |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 184 | // https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter42.html |
| 185 | g->codeAppendf("bool2 left_right_notequal = notEqual(leftbloat, rightbloat);"); |
| 186 | g->codeAppend ("if (all(left_right_notequal)) {"); |
| 187 | // The top corner will have three conservative raster vertices. Emit the |
| 188 | // middle one first to the triangle strip. |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 189 | g->codeAppendf( "%s(top + float2(-leftbloat.y, +leftbloat.x), coverages[0]);", |
| 190 | emitVertexFn); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 191 | g->codeAppend ("}"); |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 192 | g->codeAppend ("if (any(left_right_notequal)) {"); |
| 193 | // Second conservative raster vertex for the top corner. |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 194 | g->codeAppendf( "%s(top + rightbloat, coverages[1]);", emitVertexFn); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 195 | g->codeAppend ("}"); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 196 | |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 197 | // Main interior body. |
| 198 | g->codeAppendf("%s(top + leftbloat, coverages[2]);", emitVertexFn); |
| 199 | g->codeAppendf("%s(right + rightbloat, coverages[1]);", emitVertexFn); |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 200 | |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 201 | // Here the invocations diverge slightly. We can't symmetrically divide three triangle |
| 202 | // points between two invocations, so each does the following: |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 203 | // |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 204 | // sk_InvocationID=0: Finishes the main interior body of the triangle hull. |
| 205 | // sk_InvocationID=1: Remaining two conservative raster vertices for the third hull corner. |
| 206 | // sk_InvocationID=2..4: Finish the opposite endpoint of their corresponding edge. |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 207 | g->codeAppendf("bool2 right_down_notequal = notEqual(rightbloat, downbloat);"); |
| 208 | g->codeAppend ("if (any(right_down_notequal) || 0 == sk_InvocationID) {"); |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 209 | g->codeAppendf( "%s(0 == sk_InvocationID ? left + leftbloat : right + downbloat, " |
| 210 | "coverages[2]);", emitVertexFn); |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 211 | g->codeAppend ("}"); |
| 212 | g->codeAppend ("if (all(right_down_notequal) && 0 != sk_InvocationID) {"); |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 213 | g->codeAppendf( "%s(right + float2(-rightbloat.y, +rightbloat.x), coverages[3]);", |
| 214 | emitVertexFn); |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 215 | g->codeAppend ("}"); |
| 216 | |
Chris Dalton | 5183e64 | 2018-03-07 12:53:01 -0700 | [diff] [blame] | 217 | // 5 invocations: 2 triangle hull invocations and 3 edges. |
| 218 | g->configure(InputType::kLines, OutputType::kTriangleStrip, 6, 5); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 219 | } |
| 220 | }; |
| 221 | |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 222 | /** |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 223 | * Generates a conservative raster around a convex quadrilateral that encloses a cubic or quadratic. |
| 224 | */ |
| 225 | class GrCCCoverageProcessor::GSCurveHullImpl : public GrCCCoverageProcessor::GSImpl { |
| 226 | public: |
| 227 | GSCurveHullImpl(std::unique_ptr<Shader> shader) : GSImpl(std::move(shader)) {} |
| 228 | |
| 229 | void onEmitGeometryShader(const GrCCCoverageProcessor&, GrGLSLGeometryBuilder* g, |
| 230 | const GrShaderVar& wind, const char* emitVertexFn) const override { |
| 231 | const char* hullPts = "pts"; |
| 232 | fShader->emitSetupCode(g, "pts", wind.c_str(), &hullPts); |
| 233 | |
| 234 | // Visualize the input (convex) quadrilateral as a square. Paying special attention to wind, |
| 235 | // we can identify the points by their corresponding corner. |
| 236 | // |
| 237 | // NOTE: We split the square down the diagonal from top-right to bottom-left, and generate |
| 238 | // the hull in two independent invocations. Each invocation designates the corner it will |
| 239 | // begin with as top-left. |
| 240 | g->codeAppend ("int i = sk_InvocationID * 2;"); |
| 241 | g->codeAppendf("float2 topleft = %s[i];", hullPts); |
| 242 | g->codeAppendf("float2 topright = %s[%s > 0 ? i + 1 : 3 - i];", hullPts, wind.c_str()); |
| 243 | g->codeAppendf("float2 bottomleft = %s[%s > 0 ? 3 - i : i + 1];", hullPts, wind.c_str()); |
| 244 | g->codeAppendf("float2 bottomright = %s[2 - i];", hullPts); |
| 245 | |
| 246 | // Determine how much to outset the conservative raster hull from the relevant edges. |
| 247 | g->codeAppend ("float2 leftbloat = float2(topleft.y > bottomleft.y ? +bloat : -bloat, " |
| 248 | "topleft.x > bottomleft.x ? -bloat : bloat);"); |
| 249 | g->codeAppend ("float2 upbloat = float2(topright.y > topleft.y ? +bloat : -bloat, " |
| 250 | "topright.x > topleft.x ? -bloat : +bloat);"); |
| 251 | g->codeAppend ("float2 rightbloat = float2(bottomright.y > topright.y ? +bloat : -bloat, " |
| 252 | "bottomright.x > topright.x ? -bloat : +bloat);"); |
| 253 | |
| 254 | // Here we generate the conservative raster geometry. It is the convex hull of 4 pixel-size |
| 255 | // boxes centered on the input points, split evenly between two invocations. This translates |
| 256 | // to a polygon with either one, two, or three vertices at each input point, depending on |
| 257 | // how sharp the corner is. For more details on conservative raster, see: |
| 258 | // https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter42.html |
| 259 | g->codeAppendf("bool2 left_up_notequal = notEqual(leftbloat, upbloat);"); |
| 260 | g->codeAppend ("if (all(left_up_notequal)) {"); |
| 261 | // The top-left corner will have three conservative raster vertices. |
| 262 | // Emit the middle one first to the triangle strip. |
| 263 | g->codeAppendf( "%s(topleft + float2(-leftbloat.y, leftbloat.x));", emitVertexFn); |
| 264 | g->codeAppend ("}"); |
| 265 | g->codeAppend ("if (any(left_up_notequal)) {"); |
| 266 | // Second conservative raster vertex for the top-left corner. |
| 267 | g->codeAppendf( "%s(topleft + leftbloat);", emitVertexFn); |
| 268 | g->codeAppend ("}"); |
| 269 | |
| 270 | // Main interior body of this invocation's half of the hull. |
| 271 | g->codeAppendf("%s(topleft + upbloat);", emitVertexFn); |
| 272 | g->codeAppendf("%s(bottomleft + leftbloat);", emitVertexFn); |
| 273 | g->codeAppendf("%s(topright + upbloat);", emitVertexFn); |
| 274 | |
| 275 | // Remaining two conservative raster vertices for the top-right corner. |
| 276 | g->codeAppendf("bool2 up_right_notequal = notEqual(upbloat, rightbloat);"); |
| 277 | g->codeAppend ("if (any(up_right_notequal)) {"); |
| 278 | g->codeAppendf( "%s(topright + rightbloat);", emitVertexFn); |
| 279 | g->codeAppend ("}"); |
| 280 | g->codeAppend ("if (all(up_right_notequal)) {"); |
| 281 | g->codeAppendf( "%s(topright + float2(-upbloat.y, upbloat.x));", emitVertexFn); |
| 282 | g->codeAppend ("}"); |
| 283 | |
| 284 | g->configure(InputType::kLines, OutputType::kTriangleStrip, 7, 2); |
| 285 | } |
| 286 | }; |
| 287 | |
| 288 | /** |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 289 | * Generates conservative rasters around corners (aka pixel-size boxes) and calculates |
| 290 | * coverage and attenuation ramps to fix up the coverage values written by the hulls. |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 291 | */ |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 292 | class GrCCCoverageProcessor::GSCornerImpl : public GrCCCoverageProcessor::GSImpl { |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 293 | public: |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 294 | GSCornerImpl(std::unique_ptr<Shader> shader) : GSImpl(std::move(shader)) {} |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 295 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 296 | bool hasCoverage() const override { return true; } |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 297 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 298 | void onEmitGeometryShader(const GrCCCoverageProcessor& proc, GrGLSLGeometryBuilder* g, |
| 299 | const GrShaderVar& wind, const char* emitVertexFn) const override { |
| 300 | fShader->emitSetupCode(g, "pts", wind.c_str()); |
| 301 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 302 | g->codeAppendf("int corneridx = sk_InvocationID;"); |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 303 | if (!proc.isTriangles()) { |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 304 | g->codeAppendf("corneridx *= %i;", proc.numInputPoints() - 1); |
| 305 | } |
| 306 | |
| 307 | g->codeAppendf("float2 corner = pts[corneridx];"); |
| 308 | g->codeAppendf("float2 left = pts[(corneridx + (%s > 0 ? %i : 1)) %% %i];", |
| 309 | wind.c_str(), proc.numInputPoints() - 1, proc.numInputPoints()); |
| 310 | g->codeAppendf("float2 right = pts[(corneridx + (%s > 0 ? 1 : %i)) %% %i];", |
| 311 | wind.c_str(), proc.numInputPoints() - 1, proc.numInputPoints()); |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 312 | |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 313 | g->codeAppend ("float2 leftdir = corner - left;"); |
| 314 | g->codeAppend ("leftdir = (float2(0) != leftdir) ? normalize(leftdir) : float2(1, 0);"); |
| 315 | |
| 316 | g->codeAppend ("float2 rightdir = right - corner;"); |
| 317 | g->codeAppend ("rightdir = (float2(0) != rightdir) ? normalize(rightdir) : float2(1, 0);"); |
| 318 | |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 319 | // Find "outbloat" and "crossbloat" at our corner. The outbloat points diagonally out of the |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 320 | // triangle, in the direction that should ramp to zero coverage with attenuation. The |
| 321 | // crossbloat runs perpindicular to outbloat. |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 322 | g->codeAppend ("float2 outbloat = float2(leftdir.x > rightdir.x ? +1 : -1, " |
| 323 | "leftdir.y > rightdir.y ? +1 : -1);"); |
| 324 | g->codeAppend ("float2 crossbloat = float2(-outbloat.y, +outbloat.x);"); |
| 325 | |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 326 | g->codeAppend ("half attenuation; {"); |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 327 | Shader::CalcCornerAttenuation(g, "leftdir", "rightdir", "attenuation"); |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 328 | g->codeAppend ("}"); |
| 329 | |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 330 | if (proc.isTriangles()) { |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 331 | g->codeAppend ("half2 left_coverages; {"); |
| 332 | Shader::CalcEdgeCoveragesAtBloatVertices(g, "left", "corner", "-outbloat", |
| 333 | "-crossbloat", "left_coverages"); |
| 334 | g->codeAppend ("}"); |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 335 | |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 336 | g->codeAppend ("half2 right_coverages; {"); |
| 337 | Shader::CalcEdgeCoveragesAtBloatVertices(g, "corner", "right", "-outbloat", |
| 338 | "crossbloat", "right_coverages"); |
| 339 | g->codeAppend ("}"); |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 340 | |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 341 | // Emit a corner box. The first coverage argument erases the values that were written |
| 342 | // previously by the hull and edge geometry. The second pair are multiplied together by |
| 343 | // the fragment shader. They ramp to 0 with attenuation in the direction of outbloat, |
| 344 | // and linearly from left-edge coverage to right-edge coverage in the direction of |
| 345 | // crossbloat. |
| 346 | // |
| 347 | // NOTE: Since this is not a linear mapping, it is important that the box's diagonal |
| 348 | // shared edge points in the direction of outbloat. |
| 349 | g->codeAppendf("%s(corner - crossbloat * bloat, right_coverages[1] - left_coverages[1]," |
| 350 | "half2(1 + left_coverages[1], 1));", |
| 351 | emitVertexFn); |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 352 | |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 353 | g->codeAppendf("%s(corner + outbloat * bloat, " |
| 354 | "1 + left_coverages[0] + right_coverages[0], half2(0, attenuation));", |
| 355 | emitVertexFn); |
| 356 | |
| 357 | g->codeAppendf("%s(corner - outbloat * bloat, " |
| 358 | "-1 - left_coverages[0] - right_coverages[0], " |
| 359 | "half2(1 + left_coverages[0] + right_coverages[0], 1));", |
| 360 | emitVertexFn); |
| 361 | |
| 362 | g->codeAppendf("%s(corner + crossbloat * bloat, left_coverages[1] - right_coverages[1]," |
| 363 | "half2(1 + right_coverages[1], 1));", |
| 364 | emitVertexFn); |
| 365 | } else { |
| 366 | // Curves are simpler. The first coverage value of -1 means "wind = -wind", and causes |
| 367 | // the Shader to erase what it had written previously for the hull. Then, at each vertex |
| 368 | // of the corner box, the Shader will calculate the curve's local coverage value, |
| 369 | // interpolate it alongside our attenuation parameter, and multiply the two together for |
| 370 | // a final coverage value. |
| 371 | g->codeAppendf("%s(corner - crossbloat * bloat, -1, half2(1));", emitVertexFn); |
| 372 | g->codeAppendf("%s(corner + outbloat * bloat, -1, half2(0, attenuation));", |
| 373 | emitVertexFn); |
| 374 | g->codeAppendf("%s(corner - outbloat * bloat, -1, half2(1));", emitVertexFn); |
| 375 | g->codeAppendf("%s(corner + crossbloat * bloat, -1, half2(1));", emitVertexFn); |
| 376 | } |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 377 | |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 378 | g->configure(InputType::kLines, OutputType::kTriangleStrip, 4, proc.isTriangles() ? 3 : 2); |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 379 | } |
| 380 | }; |
| 381 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 382 | void GrCCCoverageProcessor::initGS() { |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 383 | SkASSERT(Impl::kGeometryShader == fImpl); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 384 | if (4 == this->numInputPoints() || this->hasInputWeight()) { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 385 | this->addVertexAttrib("x_or_y_values", kFloat4_GrVertexAttribType); |
| 386 | SkASSERT(sizeof(QuadPointInstance) == this->getVertexStride() * 2); |
| 387 | SkASSERT(offsetof(QuadPointInstance, fY) == this->getVertexStride()); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 388 | } else { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 389 | this->addVertexAttrib("x_or_y_values", kFloat3_GrVertexAttribType); |
| 390 | SkASSERT(sizeof(TriPointInstance) == this->getVertexStride() * 2); |
| 391 | SkASSERT(offsetof(TriPointInstance, fY) == this->getVertexStride()); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 392 | } |
| 393 | this->setWillUseGeoShader(); |
| 394 | } |
| 395 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 396 | void GrCCCoverageProcessor::appendGSMesh(GrBuffer* instanceBuffer, int instanceCount, |
| 397 | int baseInstance, SkTArray<GrMesh>* out) const { |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 398 | // GSImpl doesn't actually make instanced draw calls. Instead, we feed transposed x,y point |
| 399 | // values to the GPU in a regular vertex array and draw kLines (see initGS). Then, each vertex |
| 400 | // invocation receives either the shape's x or y values as inputs, which it forwards to the |
| 401 | // geometry shader. |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 402 | SkASSERT(Impl::kGeometryShader == fImpl); |
Chris Dalton | 2326177 | 2017-12-10 16:41:45 -0700 | [diff] [blame] | 403 | GrMesh& mesh = out->emplace_back(GrPrimitiveType::kLines); |
| 404 | mesh.setNonIndexedNonInstanced(instanceCount * 2); |
| 405 | mesh.setVertexData(instanceBuffer, baseInstance * 2); |
| 406 | } |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 407 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 408 | GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGSImpl(std::unique_ptr<Shader> shadr) const { |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 409 | if (GSSubpass::kHulls == fGSSubpass) { |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 410 | return this->isTriangles() |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 411 | ? (GSImpl*) new GSTriangleHullImpl(std::move(shadr)) |
| 412 | : (GSImpl*) new GSCurveHullImpl(std::move(shadr)); |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 413 | } |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 414 | SkASSERT(GSSubpass::kCorners == fGSSubpass); |
| 415 | return new GSCornerImpl(std::move(shadr)); |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 416 | } |