Chris Dalton | 1a325d2 | 2017-07-14 15:17: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 | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 9 | |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 10 | #include "GrGpuCommandBuffer.h" |
| 11 | #include "GrOpFlushState.h" |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 12 | #include "SkMakeUnique.h" |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 13 | #include "ccpr/GrCCCubicShader.h" |
| 14 | #include "ccpr/GrCCQuadraticShader.h" |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 15 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 16 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 17 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 18 | |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 19 | class GrCCCoverageProcessor::TriangleShader : public GrCCCoverageProcessor::Shader { |
| 20 | void onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, |
| 21 | SkString* code, const char* position, const char* coverage, |
| 22 | const char* cornerCoverage) override { |
| 23 | if (!cornerCoverage) { |
| 24 | fCoverages.reset(kHalf_GrSLType, scope); |
| 25 | varyingHandler->addVarying("coverage", &fCoverages); |
| 26 | code->appendf("%s = %s;", OutName(fCoverages), coverage); |
| 27 | } else { |
| 28 | fCoverages.reset(kHalf3_GrSLType, scope); |
| 29 | varyingHandler->addVarying("coverages", &fCoverages); |
| 30 | code->appendf("%s = half3(%s, %s);", OutName(fCoverages), coverage, cornerCoverage); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void onEmitFragmentCode(GrGLSLFPFragmentBuilder* f, const char* outputCoverage) const override { |
| 35 | if (kHalf_GrSLType == fCoverages.type()) { |
| 36 | f->codeAppendf("%s = %s;", outputCoverage, fCoverages.fsIn()); |
| 37 | } else { |
| 38 | f->codeAppendf("%s = %s.z * %s.y + %s.x;", |
| 39 | outputCoverage, fCoverages.fsIn(), fCoverages.fsIn(), fCoverages.fsIn()); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | GrGLSLVarying fCoverages; |
| 44 | }; |
Eric Boren | d6365e5 | 2017-10-16 12:31:14 +0000 | [diff] [blame] | 45 | |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 46 | void GrCCCoverageProcessor::Shader::EmitEdgeDistanceEquation(GrGLSLVertexGeoBuilder* s, |
| 47 | const char* leftPt, |
| 48 | const char* rightPt, |
| 49 | const char* outputDistanceEquation) { |
| 50 | s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);", |
| 51 | rightPt, leftPt, leftPt, rightPt); |
| 52 | s->codeAppend ("float nwidth = (abs(n.x) + abs(n.y)) * (bloat * 2);"); |
| 53 | // When nwidth=0, wind must also be 0 (and coverage * wind = 0). So it doesn't matter what we |
| 54 | // come up with here as long as it isn't NaN or Inf. |
| 55 | s->codeAppend ("n /= (0 != nwidth) ? nwidth : 1;"); |
| 56 | s->codeAppendf("%s = float3(-n, dot(n, %s) - .5);", outputDistanceEquation, leftPt); |
| 57 | } |
| 58 | |
Chris Dalton | 0a79381 | 2018-03-07 11:18:30 -0700 | [diff] [blame] | 59 | void GrCCCoverageProcessor::Shader::CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder* s, |
| 60 | const char* leftPt, |
| 61 | const char* rightPt, |
| 62 | const char* rasterVertexDir, |
| 63 | const char* outputCoverage) { |
| 64 | // Here we find an edge's coverage at one corner of a conservative raster bloat box whose center |
| 65 | // falls on the edge in question. (A bloat box is axis-aligned and the size of one pixel.) We |
| 66 | // always set up coverage so it is -1 at the outermost corner, 0 at the innermost, and -.5 at |
| 67 | // the center. Interpolated, these coverage values convert jagged conservative raster edges into |
| 68 | // smooth antialiased edges. |
| 69 | // |
| 70 | // d1 == (P + sign(n) * bloat) dot n (Distance at the bloat box vertex whose |
| 71 | // == P dot n + (abs(n.x) + abs(n.y)) * bloatSize coverage=-1, where the bloat box is |
| 72 | // centered on P.) |
| 73 | // |
| 74 | // d0 == (P - sign(n) * bloat) dot n (Distance at the bloat box vertex whose |
| 75 | // == P dot n - (abs(n.x) + abs(n.y)) * bloatSize coverage=0, where the bloat box is |
| 76 | // centered on P.) |
| 77 | // |
| 78 | // d == (P + rasterVertexDir * bloatSize) dot n (Distance at the bloat box vertex whose |
| 79 | // == P dot n + (rasterVertexDir dot n) * bloatSize coverage we wish to calculate.) |
| 80 | // |
| 81 | // coverage == -(d - d0) / (d1 - d0) (coverage=-1 at d=d1; coverage=0 at d=d0) |
| 82 | // |
| 83 | // == (rasterVertexDir dot n) / (abs(n.x) + abs(n.y)) * -.5 - .5 |
| 84 | // |
| 85 | s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);", |
| 86 | rightPt, leftPt, leftPt, rightPt); |
| 87 | s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);"); |
| 88 | s->codeAppendf("float t = dot(%s, n);", rasterVertexDir); |
| 89 | // The below conditional guarantees we get exactly 1 on the divide when nwidth=t (in case the |
| 90 | // GPU divides by multiplying by the reciprocal?) It also guards against NaN when nwidth=0. |
| 91 | s->codeAppendf("%s = (abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;", outputCoverage); |
| 92 | } |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 93 | |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 94 | void GrCCCoverageProcessor::Shader::CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder* s, |
| 95 | const char* leftPt, |
| 96 | const char* rightPt, |
| 97 | const char* bloatDir1, |
| 98 | const char* bloatDir2, |
| 99 | const char* outputCoverages) { |
| 100 | // See comments in CalcEdgeCoverageAtBloatVertex. |
| 101 | s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);", |
| 102 | rightPt, leftPt, leftPt, rightPt); |
| 103 | s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);"); |
| 104 | s->codeAppendf("float2 t = n * float2x2(%s, %s);", bloatDir1, bloatDir2); |
| 105 | s->codeAppendf("for (int i = 0; i < 2; ++i) {"); |
| 106 | s->codeAppendf( "%s[i] = (abs(t[i]) != nwidth ? t[i] / nwidth : sign(t[i])) * -.5 - .5;", |
| 107 | outputCoverages); |
| 108 | s->codeAppendf("}"); |
| 109 | } |
| 110 | |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 111 | void GrCCCoverageProcessor::Shader::CalcCornerAttenuation(GrGLSLVertexGeoBuilder* s, |
| 112 | const char* leftDir, const char* rightDir, |
| 113 | const char* outputAttenuation) { |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 114 | // obtuseness = cos(corner_angle) if corner_angle > 90 degrees |
| 115 | // 0 if corner_angle <= 90 degrees |
| 116 | s->codeAppendf("half obtuseness = max(dot(%s, %s), 0);", leftDir, rightDir); |
| 117 | |
| 118 | // axis_alignedness = 1 when the leftDir/rightDir bisector is aligned with the x- or y-axis |
| 119 | // 0 when the bisector falls on a 45 degree angle |
| 120 | // (i.e. 1 - tan(angle_to_nearest_axis)) |
| 121 | s->codeAppendf("half2 abs_bisect = abs(%s - %s);", leftDir, rightDir); |
| 122 | s->codeAppend ("half axis_alignedness = 1 - min(abs_bisect.y, abs_bisect.x) / " |
| 123 | "max(abs_bisect.x, abs_bisect.y);"); |
| 124 | |
| 125 | // ninety_degreesness = sin^2(corner_angle) |
| 126 | // sin^2 just because... it's always positive and the results looked better than plain sine... ? |
| 127 | s->codeAppendf("half ninety_degreesness = determinant(half2x2(%s, %s));", leftDir, rightDir); |
| 128 | s->codeAppend ("ninety_degreesness = ninety_degreesness * ninety_degreesness;"); |
| 129 | |
| 130 | // The below formula is not smart. It was just arrived at by considering the following |
| 131 | // observations: |
| 132 | // |
| 133 | // 1. 90-degree, axis-aligned corners have full attenuation along the bisector. |
| 134 | // (i.e. coverage = 1 - distance_to_corner^2) |
| 135 | // (i.e. outputAttenuation = 0) |
| 136 | // |
| 137 | // 2. 180-degree corners always have zero attenuation. |
| 138 | // (i.e. coverage = 1 - distance_to_corner) |
| 139 | // (i.e. outputAttenuation = 1) |
| 140 | // |
| 141 | // 3. 90-degree corners whose bisector falls on a 45 degree angle also do not attenuate. |
| 142 | // (i.e. outputAttenuation = 1) |
| 143 | s->codeAppendf("%s = max(obtuseness, axis_alignedness * ninety_degreesness);", |
| 144 | outputAttenuation); |
| 145 | } |
| 146 | |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 147 | void GrCCCoverageProcessor::getGLSLProcessorKey(const GrShaderCaps&, |
| 148 | GrProcessorKeyBuilder* b) const { |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame^] | 149 | int key = (int)fPrimitiveType << 2; |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 150 | if (GSSubpass::kCorners == fGSSubpass) { |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 151 | key |= 2; |
| 152 | } |
| 153 | if (Impl::kVertexShader == fImpl) { |
| 154 | key |= 1; |
| 155 | } |
| 156 | #ifdef SK_DEBUG |
| 157 | uint32_t bloatBits; |
| 158 | memcpy(&bloatBits, &fDebugBloat, 4); |
| 159 | b->add32(bloatBits); |
| 160 | #endif |
| 161 | b->add32(key); |
| 162 | } |
| 163 | |
| 164 | GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 165 | std::unique_ptr<Shader> shader; |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 166 | switch (fPrimitiveType) { |
| 167 | case PrimitiveType::kTriangles: |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame^] | 168 | case PrimitiveType::kWeightedTriangles: |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 169 | shader = skstd::make_unique<TriangleShader>(); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 170 | break; |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 171 | case PrimitiveType::kQuadratics: |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 172 | shader = skstd::make_unique<GrCCQuadraticShader>(); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 173 | break; |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 174 | case PrimitiveType::kCubics: |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 175 | shader = skstd::make_unique<GrCCCubicShader>(); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 176 | break; |
| 177 | } |
| 178 | return Impl::kGeometryShader == fImpl ? this->createGSImpl(std::move(shader)) |
| 179 | : this->createVSImpl(std::move(shader)); |
| 180 | } |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 181 | |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 182 | void GrCCCoverageProcessor::Shader::emitFragmentCode(const GrCCCoverageProcessor& proc, |
| 183 | GrGLSLFPFragmentBuilder* f, |
| 184 | const char* skOutputColor, |
| 185 | const char* skOutputCoverage) const { |
| 186 | f->codeAppendf("half coverage = 0;"); |
| 187 | this->onEmitFragmentCode(f, "coverage"); |
| 188 | f->codeAppendf("%s.a = coverage;", skOutputColor); |
| 189 | f->codeAppendf("%s = half4(1);", skOutputCoverage); |
| 190 | } |
| 191 | |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 192 | void GrCCCoverageProcessor::draw(GrOpFlushState* flushState, const GrPipeline& pipeline, |
| 193 | const GrMesh meshes[], |
| 194 | const GrPipeline::DynamicState dynamicStates[], int meshCount, |
| 195 | const SkRect& drawBounds) const { |
| 196 | GrGpuRTCommandBuffer* cmdBuff = flushState->rtCommandBuffer(); |
| 197 | cmdBuff->draw(pipeline, *this, meshes, dynamicStates, meshCount, drawBounds); |
| 198 | |
| 199 | // Geometry shader backend draws primitives in two subpasses. |
| 200 | if (Impl::kGeometryShader == fImpl) { |
| 201 | SkASSERT(GSSubpass::kHulls == fGSSubpass); |
| 202 | GrCCCoverageProcessor cornerProc(*this, GSSubpass::kCorners); |
| 203 | cmdBuff->draw(pipeline, cornerProc, meshes, dynamicStates, meshCount, drawBounds); |
| 204 | } |
| 205 | } |