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