| 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 | |
| 8 | #include "src/gpu/ccpr/GrCCCoverageProcessor.h" |
| 9 | |
| 10 | #include "src/gpu/GrOpFlushState.h" |
| 11 | #include "src/gpu/GrOpsRenderPass.h" |
| 12 | #include "src/gpu/GrProgramInfo.h" |
| 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" |
| 19 | |
| 20 | class GrCCCoverageProcessor::TriangleShader : public GrCCCoverageProcessor::Shader { |
| 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 { |
| 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 | |
| 36 | void emitFragmentCoverageCode( |
| 37 | GrGLSLFPFragmentBuilder* f, const char* outputCoverage) const override { |
| 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 | |
| 46 | void emitSampleMaskCode(GrGLSLFPFragmentBuilder*) const override { return; } |
| 47 | |
| 48 | GrGLSLVarying fCoverages; |
| 49 | }; |
| 50 | |
| 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);" ); |
| 73 | s->codeAppendf("%s = (abs(area_x2 * 1024) > basewidth) ? sign(half(area_x2)) : 0;" , |
| 74 | outputWind); |
| 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. |
| 78 | s->codeAppendf("%s = sign(half(area_x2));" , outputWind); |
| 79 | } |
| 80 | } |
| 81 | |
| 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. |
| 114 | s->codeAppendf("%s = half(abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;" , |
| 115 | outputCoverage); |
| 116 | } |
| 117 | |
| 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) {" ); |
| 130 | s->codeAppendf( "%s[i] = half(abs(t[i]) != nwidth ? t[i] / nwidth : sign(t[i])) * -.5 - .5;" , |
| 131 | outputCoverages); |
| 132 | s->codeAppendf("}" ); |
| 133 | } |
| 134 | |
| 135 | void GrCCCoverageProcessor::Shader::CalcCornerAttenuation(GrGLSLVertexGeoBuilder* s, |
| 136 | const char* leftDir, const char* rightDir, |
| 137 | const char* outputAttenuation) { |
| 138 | // obtuseness = cos(corner_angle) if corner_angle > 90 degrees |
| 139 | // 0 if corner_angle <= 90 degrees |
| 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. |
| 143 | s->codeAppendf("half obtuseness = max(half(dot(%s, %s)), 0);" , leftDir, rightDir); |
| 144 | |
| 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 |
| 149 | s->codeAppendf("half2 abs_bisect_maybe_transpose = abs((0 == obtuseness) ? half2(%s - %s) : " |
| 150 | "half2(%s + %s));" , |
| 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);" ); |
| 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 | |
| 178 | GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 179 | std::unique_ptr<Shader> shader; |
| 180 | switch (fPrimitiveType) { |
| 181 | case PrimitiveType::kTriangles: |
| 182 | case PrimitiveType::kWeightedTriangles: |
| 183 | shader = std::make_unique<TriangleShader>(); |
| 184 | break; |
| 185 | case PrimitiveType::kQuadratics: |
| 186 | shader = std::make_unique<GrCCQuadraticShader>(); |
| 187 | break; |
| 188 | case PrimitiveType::kCubics: |
| 189 | shader = std::make_unique<GrCCCubicShader>(); |
| 190 | break; |
| 191 | case PrimitiveType::kConics: |
| 192 | shader = std::make_unique<GrCCConicShader>(); |
| 193 | break; |
| 194 | } |
| 195 | return this->onCreateGLSLInstance(std::move(shader)); |
| 196 | } |
| 197 | |
| 198 | void GrCCCoverageProcessor::bindPipeline(GrOpFlushState* flushState, const GrPipeline& pipeline, |
| 199 | const SkRect& drawBounds) const { |
| 200 | GrProgramInfo programInfo(flushState->proxy()->numSamples(), |
| 201 | flushState->proxy()->numStencilSamples(), |
| 202 | flushState->proxy()->backendFormat(), |
| 203 | flushState->writeView()->origin(), &pipeline, this, |
| 204 | this->primType()); |
| 205 | flushState->bindPipeline(programInfo, drawBounds); |
| 206 | } |
| 207 | |