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