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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCCCoverageProcessor.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/core/SkMakeUnique.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrOpFlushState.h" |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrOpsRenderPass.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/ccpr/GrCCConicShader.h" |
| 14 | #include "src/gpu/ccpr/GrCCCubicShader.h" |
| 15 | #include "src/gpu/ccpr/GrCCQuadraticShader.h" |
| 16 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 17 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
| 18 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 19 | |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 20 | class GrCCCoverageProcessor::TriangleShader : public GrCCCoverageProcessor::Shader { |
Chris Dalton | 84d36cd | 2019-04-17 14:47:17 -0600 | [diff] [blame] | 21 | void onEmitVaryings( |
| 22 | GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code, |
| 23 | const char* position, const char* coverage, const char* cornerCoverage, |
| 24 | const char* /*wind*/) override { |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 25 | if (!cornerCoverage) { |
| 26 | fCoverages.reset(kHalf_GrSLType, scope); |
| 27 | varyingHandler->addVarying("coverage", &fCoverages); |
| 28 | code->appendf("%s = %s;", OutName(fCoverages), coverage); |
| 29 | } else { |
| 30 | fCoverages.reset(kHalf3_GrSLType, scope); |
| 31 | varyingHandler->addVarying("coverages", &fCoverages); |
| 32 | code->appendf("%s = half3(%s, %s);", OutName(fCoverages), coverage, cornerCoverage); |
| 33 | } |
| 34 | } |
| 35 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 36 | void emitFragmentCoverageCode( |
| 37 | GrGLSLFPFragmentBuilder* f, const char* outputCoverage) const override { |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 38 | if (kHalf_GrSLType == fCoverages.type()) { |
| 39 | f->codeAppendf("%s = %s;", outputCoverage, fCoverages.fsIn()); |
| 40 | } else { |
| 41 | f->codeAppendf("%s = %s.z * %s.y + %s.x;", |
| 42 | outputCoverage, fCoverages.fsIn(), fCoverages.fsIn(), fCoverages.fsIn()); |
| 43 | } |
| 44 | } |
| 45 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 46 | void emitSampleMaskCode(GrGLSLFPFragmentBuilder*) const override { return; } |
| 47 | |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 48 | GrGLSLVarying fCoverages; |
| 49 | }; |
Eric Boren | d6365e5 | 2017-10-16 12:31:14 +0000 | [diff] [blame] | 50 | |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 51 | void GrCCCoverageProcessor::Shader::CalcWind(const GrCCCoverageProcessor& proc, |
| 52 | GrGLSLVertexGeoBuilder* s, const char* pts, |
| 53 | const char* outputWind) { |
| 54 | if (3 == proc.numInputPoints()) { |
| 55 | s->codeAppendf("float2 a = %s[0] - %s[1], " |
| 56 | "b = %s[0] - %s[2];", pts, pts, pts, pts); |
| 57 | } else { |
| 58 | // All inputs are convex, so it's sufficient to just average the middle two input points. |
| 59 | SkASSERT(4 == proc.numInputPoints()); |
| 60 | s->codeAppendf("float2 p12 = (%s[1] + %s[2]) * .5;", pts, pts); |
| 61 | s->codeAppendf("float2 a = %s[0] - p12, " |
| 62 | "b = %s[0] - %s[3];", pts, pts, pts); |
| 63 | } |
| 64 | |
| 65 | s->codeAppend ("float area_x2 = determinant(float2x2(a, b));"); |
| 66 | if (proc.isTriangles()) { |
| 67 | // We cull extremely thin triangles by zeroing wind. When a triangle gets too thin it's |
| 68 | // possible for FP round-off error to actually give us the wrong winding direction, causing |
| 69 | // rendering artifacts. The criteria we choose is "height <~ 1/1024". So we drop a triangle |
| 70 | // if the max effect it can have on any single pixel is <~ 1/1024, or 1/4 of a bit in 8888. |
| 71 | s->codeAppend ("float2 bbox_size = max(abs(a), abs(b));"); |
| 72 | s->codeAppend ("float basewidth = max(bbox_size.x + bbox_size.y, 1);"); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 73 | s->codeAppendf("%s = (abs(area_x2 * 1024) > basewidth) ? sign(half(area_x2)) : 0;", |
| 74 | outputWind); |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 75 | } else { |
| 76 | // We already converted nearly-flat curves to lines on the CPU, so no need to worry about |
| 77 | // thin curve hulls at this point. |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 78 | s->codeAppendf("%s = sign(half(area_x2));", outputWind); |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Chris Dalton | 0a79381 | 2018-03-07 11:18:30 -0700 | [diff] [blame] | 82 | void GrCCCoverageProcessor::Shader::CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder* s, |
| 83 | const char* leftPt, |
| 84 | const char* rightPt, |
| 85 | const char* rasterVertexDir, |
| 86 | const char* outputCoverage) { |
| 87 | // Here we find an edge's coverage at one corner of a conservative raster bloat box whose center |
| 88 | // falls on the edge in question. (A bloat box is axis-aligned and the size of one pixel.) We |
| 89 | // always set up coverage so it is -1 at the outermost corner, 0 at the innermost, and -.5 at |
| 90 | // the center. Interpolated, these coverage values convert jagged conservative raster edges into |
| 91 | // smooth antialiased edges. |
| 92 | // |
| 93 | // d1 == (P + sign(n) * bloat) dot n (Distance at the bloat box vertex whose |
| 94 | // == P dot n + (abs(n.x) + abs(n.y)) * bloatSize coverage=-1, where the bloat box is |
| 95 | // centered on P.) |
| 96 | // |
| 97 | // d0 == (P - sign(n) * bloat) dot n (Distance at the bloat box vertex whose |
| 98 | // == P dot n - (abs(n.x) + abs(n.y)) * bloatSize coverage=0, where the bloat box is |
| 99 | // centered on P.) |
| 100 | // |
| 101 | // d == (P + rasterVertexDir * bloatSize) dot n (Distance at the bloat box vertex whose |
| 102 | // == P dot n + (rasterVertexDir dot n) * bloatSize coverage we wish to calculate.) |
| 103 | // |
| 104 | // coverage == -(d - d0) / (d1 - d0) (coverage=-1 at d=d1; coverage=0 at d=d0) |
| 105 | // |
| 106 | // == (rasterVertexDir dot n) / (abs(n.x) + abs(n.y)) * -.5 - .5 |
| 107 | // |
| 108 | s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);", |
| 109 | rightPt, leftPt, leftPt, rightPt); |
| 110 | s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);"); |
| 111 | s->codeAppendf("float t = dot(%s, n);", rasterVertexDir); |
| 112 | // The below conditional guarantees we get exactly 1 on the divide when nwidth=t (in case the |
| 113 | // GPU divides by multiplying by the reciprocal?) It also guards against NaN when nwidth=0. |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 114 | s->codeAppendf("%s = half(abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;", |
| 115 | outputCoverage); |
Chris Dalton | 0a79381 | 2018-03-07 11:18:30 -0700 | [diff] [blame] | 116 | } |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 117 | |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 118 | void GrCCCoverageProcessor::Shader::CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder* s, |
| 119 | const char* leftPt, |
| 120 | const char* rightPt, |
| 121 | const char* bloatDir1, |
| 122 | const char* bloatDir2, |
| 123 | const char* outputCoverages) { |
| 124 | // See comments in CalcEdgeCoverageAtBloatVertex. |
| 125 | s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);", |
| 126 | rightPt, leftPt, leftPt, rightPt); |
| 127 | s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);"); |
| 128 | s->codeAppendf("float2 t = n * float2x2(%s, %s);", bloatDir1, bloatDir2); |
| 129 | s->codeAppendf("for (int i = 0; i < 2; ++i) {"); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 130 | s->codeAppendf( "%s[i] = half(abs(t[i]) != nwidth ? t[i] / nwidth : sign(t[i])) * -.5 - .5;", |
Chris Dalton | 8738cf4 | 2018-03-09 11:57:40 -0700 | [diff] [blame] | 131 | outputCoverages); |
| 132 | s->codeAppendf("}"); |
| 133 | } |
| 134 | |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 135 | void GrCCCoverageProcessor::Shader::CalcCornerAttenuation(GrGLSLVertexGeoBuilder* s, |
| 136 | const char* leftDir, const char* rightDir, |
| 137 | const char* outputAttenuation) { |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 138 | // obtuseness = cos(corner_angle) if corner_angle > 90 degrees |
| 139 | // 0 if corner_angle <= 90 degrees |
Chris Dalton | b7e0371 | 2018-10-04 15:23:13 -0600 | [diff] [blame] | 140 | // |
| 141 | // NOTE: leftDir and rightDir are normalized and point in the same direction the path was |
| 142 | // defined with, i.e., leftDir points into the corner and rightDir points away from the corner. |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 143 | s->codeAppendf("half obtuseness = max(half(dot(%s, %s)), 0);", leftDir, rightDir); |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 144 | |
Chris Dalton | b7e0371 | 2018-10-04 15:23:13 -0600 | [diff] [blame] | 145 | // axis_alignedness = 1 - tan(angle_to_nearest_axis_from_corner_bisector) |
| 146 | // (i.e., 1 when the corner bisector is aligned with the x- or y-axis |
| 147 | // 0 when the corner bisector falls on a 45 degree angle |
| 148 | // 0..1 when the corner bisector falls somewhere in between |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 149 | s->codeAppendf("half2 abs_bisect_maybe_transpose = abs((0 == obtuseness) ? half2(%s - %s) : " |
| 150 | "half2(%s + %s));", |
Chris Dalton | b7e0371 | 2018-10-04 15:23:13 -0600 | [diff] [blame] | 151 | leftDir, rightDir, leftDir, rightDir); |
| 152 | s->codeAppend ("half axis_alignedness = " |
| 153 | "1 - min(abs_bisect_maybe_transpose.y, abs_bisect_maybe_transpose.x) / " |
| 154 | "max(abs_bisect_maybe_transpose.x, abs_bisect_maybe_transpose.y);"); |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 155 | |
| 156 | // ninety_degreesness = sin^2(corner_angle) |
| 157 | // sin^2 just because... it's always positive and the results looked better than plain sine... ? |
| 158 | s->codeAppendf("half ninety_degreesness = determinant(half2x2(%s, %s));", leftDir, rightDir); |
| 159 | s->codeAppend ("ninety_degreesness = ninety_degreesness * ninety_degreesness;"); |
| 160 | |
| 161 | // The below formula is not smart. It was just arrived at by considering the following |
| 162 | // observations: |
| 163 | // |
| 164 | // 1. 90-degree, axis-aligned corners have full attenuation along the bisector. |
| 165 | // (i.e. coverage = 1 - distance_to_corner^2) |
| 166 | // (i.e. outputAttenuation = 0) |
| 167 | // |
| 168 | // 2. 180-degree corners always have zero attenuation. |
| 169 | // (i.e. coverage = 1 - distance_to_corner) |
| 170 | // (i.e. outputAttenuation = 1) |
| 171 | // |
| 172 | // 3. 90-degree corners whose bisector falls on a 45 degree angle also do not attenuate. |
| 173 | // (i.e. outputAttenuation = 1) |
| 174 | s->codeAppendf("%s = max(obtuseness, axis_alignedness * ninety_degreesness);", |
| 175 | outputAttenuation); |
| 176 | } |
| 177 | |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 178 | GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 179 | std::unique_ptr<Shader> shader; |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 180 | switch (fPrimitiveType) { |
| 181 | case PrimitiveType::kTriangles: |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 182 | case PrimitiveType::kWeightedTriangles: |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 183 | shader = skstd::make_unique<TriangleShader>(); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 184 | break; |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 185 | case PrimitiveType::kQuadratics: |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 186 | shader = skstd::make_unique<GrCCQuadraticShader>(); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 187 | break; |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 188 | case PrimitiveType::kCubics: |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 189 | shader = skstd::make_unique<GrCCCubicShader>(); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 190 | break; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 191 | case PrimitiveType::kConics: |
| 192 | shader = skstd::make_unique<GrCCConicShader>(); |
| 193 | break; |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 194 | } |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 195 | return this->onCreateGLSLInstance(std::move(shader)); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 196 | } |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 197 | |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 198 | void GrCCCoverageProcessor::draw( |
| 199 | GrOpFlushState* flushState, const GrPipeline& pipeline, const SkIRect scissorRects[], |
| 200 | const GrMesh meshes[], int meshCount, const SkRect& drawBounds) const { |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 201 | GrPipeline::DynamicStateArrays dynamicStateArrays; |
| 202 | dynamicStateArrays.fScissorRects = scissorRects; |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 203 | GrOpsRenderPass* renderPass = flushState->opsRenderPass(); |
| 204 | renderPass->draw(*this, pipeline, nullptr, &dynamicStateArrays, meshes, meshCount, drawBounds); |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 205 | } |