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 | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 8 | #include "GrCCCubicShader.h" |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 9 | |
| 10 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Chris Dalton | 7c7ff03 | 2018-03-28 20:09:58 -0600 | [diff] [blame^] | 11 | #include "glsl/GrGLSLProgramBuilder.h" |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 13 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 14 | using Shader = GrCCCoverageProcessor::Shader; |
Chris Dalton | de5a814 | 2017-12-18 10:05:15 -0700 | [diff] [blame] | 15 | |
Chris Dalton | fe462ef | 2018-03-08 15:54:01 +0000 | [diff] [blame] | 16 | void GrCCCubicShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts, |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 17 | const char* wind, const char** /*tighterHull*/) const { |
Chris Dalton | 7c7ff03 | 2018-03-28 20:09:58 -0600 | [diff] [blame^] | 18 | // Define a function that normalizes the homogeneous coordinates T=t/s in order to avoid |
| 19 | // exponent overflow. |
| 20 | SkString normalizeHomogCoordFn; |
| 21 | GrShaderVar coord("coord", kFloat2_GrSLType); |
| 22 | s->emitFunction(kFloat2_GrSLType, "normalize_homogeneous_coord", 1, &coord, |
| 23 | s->getProgramBuilder()->shaderCaps()->fpManipulationSupport() |
| 24 | // Exponent manipulation version: Scale the exponents so the larger |
| 25 | // component has a magnitude in 1..2. |
| 26 | // (Neither component should be infinity because ccpr crops big paths.) |
| 27 | ? "int exp;" |
| 28 | "frexp(max(abs(coord.t), abs(coord.s)), exp);" |
| 29 | "return coord * ldexp(1, 1 - exp);" |
| 30 | |
| 31 | // Division version: Divide by the component with the larger magnitude. |
| 32 | // (Both should not be 0 because ccpr catches degenerate cubics.) |
| 33 | : "bool swap = abs(coord.t) > abs(coord.s);" |
| 34 | "coord = swap ? coord.ts : coord;" |
| 35 | "coord = float2(1, coord.t/coord.s);" |
| 36 | "return swap ? coord.ts : coord;", |
| 37 | |
| 38 | &normalizeHomogCoordFn); |
| 39 | |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 40 | // Find the cubic's power basis coefficients. |
| 41 | s->codeAppendf("float2x4 C = float4x4(-1, 3, -3, 1, " |
| 42 | " 3, -6, 3, 0, " |
| 43 | "-3, 3, 0, 0, " |
| 44 | " 1, 0, 0, 0) * transpose(%s);", pts); |
| 45 | |
| 46 | // Find the cubic's inflection function. |
| 47 | s->codeAppend ("float D3 = +determinant(float2x2(C[0].yz, C[1].yz));"); |
| 48 | s->codeAppend ("float D2 = -determinant(float2x2(C[0].xz, C[1].xz));"); |
| 49 | s->codeAppend ("float D1 = +determinant(float2x2(C));"); |
| 50 | |
| 51 | // Calculate the KLM matrix. |
| 52 | s->declareGlobal(fKLMMatrix); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 53 | s->codeAppend ("float discr = 3*D2*D2 - 4*D1*D3;"); |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 54 | s->codeAppend ("float x = discr >= 0 ? 3 : 1;"); |
| 55 | s->codeAppend ("float q = sqrt(x * abs(discr));"); |
| 56 | s->codeAppend ("q = x*D2 + (D2 >= 0 ? q : -q);"); |
| 57 | |
| 58 | s->codeAppend ("float2 l, m;"); |
Chris Dalton | 7c7ff03 | 2018-03-28 20:09:58 -0600 | [diff] [blame^] | 59 | s->codeAppendf("l.ts = %s(float2(q, 2*x * D1));", normalizeHomogCoordFn.c_str()); |
| 60 | s->codeAppendf("m.ts = %s(float2(2, q) * (discr >= 0 ? float2(D3, 1) " |
| 61 | ": float2(D2*D2 - D3*D1, D1)));", |
| 62 | normalizeHomogCoordFn.c_str()); |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 63 | |
| 64 | s->codeAppend ("float4 K;"); |
| 65 | s->codeAppend ("float4 lm = l.sstt * m.stst;"); |
| 66 | s->codeAppend ("K = float4(0, lm.x, -lm.y - lm.z, lm.w);"); |
| 67 | |
| 68 | s->codeAppend ("float4 L, M;"); |
| 69 | s->codeAppend ("lm.yz += 2*lm.zy;"); |
| 70 | s->codeAppend ("L = float4(-1,x,-x,1) * l.sstt * (discr >= 0 ? l.ssst * l.sttt : lm);"); |
| 71 | s->codeAppend ("M = float4(-1,x,-x,1) * m.sstt * (discr >= 0 ? m.ssst * m.sttt : lm.xzyw);"); |
| 72 | |
Chris Dalton | 7c7ff03 | 2018-03-28 20:09:58 -0600 | [diff] [blame^] | 73 | s->codeAppend ("int middlerow = abs(D2) > abs(D1) ? 2 : 1;"); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 74 | s->codeAppend ("float3x3 CI = inverse(float3x3(C[0][0], C[0][middlerow], C[0][3], " |
| 75 | "C[1][0], C[1][middlerow], C[1][3], " |
| 76 | " 0, 0, 1));"); |
| 77 | s->codeAppendf("%s = CI * float3x3(K[0], K[middlerow], K[3], " |
| 78 | "L[0], L[middlerow], L[3], " |
| 79 | "M[0], M[middlerow], M[3]);", fKLMMatrix.c_str()); |
| 80 | |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 81 | // Evaluate the cubic at T=.5 for a mid-ish point. |
| 82 | s->codeAppendf("float2 midpoint = %s * float4(.125, .375, .375, .125);", pts); |
| 83 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 84 | // Orient the KLM matrix so L & M are both positive on the side of the curve we wish to fill. |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 85 | s->codeAppendf("float2 orientation = sign(float3(midpoint, 1) * float2x3(%s[1], %s[2]));", |
| 86 | fKLMMatrix.c_str(), fKLMMatrix.c_str()); |
| 87 | s->codeAppendf("%s *= float3x3(orientation[0] * orientation[1], 0, 0, " |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 88 | "0, orientation[0], 0, " |
| 89 | "0, 0, orientation[1]);", fKLMMatrix.c_str()); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 90 | |
| 91 | // Determine the amount of additional coverage to subtract out for the flat edge (P3 -> P0). |
| 92 | s->declareGlobal(fEdgeDistanceEquation); |
Chris Dalton | 7c7ff03 | 2018-03-28 20:09:58 -0600 | [diff] [blame^] | 93 | s->codeAppendf("int edgeidx0 = %s > 0 ? 3 : 0;", wind); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 94 | s->codeAppendf("float2 edgept0 = %s[edgeidx0];", pts); |
| 95 | s->codeAppendf("float2 edgept1 = %s[3 - edgeidx0];", pts); |
| 96 | Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 97 | } |
| 98 | |
Chris Dalton | fe462ef | 2018-03-08 15:54:01 +0000 | [diff] [blame] | 99 | void GrCCCubicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, |
| 100 | GrGLSLVarying::Scope scope, SkString* code, |
Chris Dalton | 04a1de5 | 2018-03-14 02:04:09 -0600 | [diff] [blame] | 101 | const char* position, const char* coverage, |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 102 | const char* attenuatedCoverage) { |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 103 | fKLMD.reset(kFloat4_GrSLType, scope); |
| 104 | varyingHandler->addVarying("klmd", &fKLMD); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 105 | code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str()); |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 106 | // We give L & M both the same sign as wind, in order to pass this value to the fragment shader. |
| 107 | // (Cubics are pre-chopped such that L & M do not change sign within any individual segment.) |
| 108 | code->appendf("%s.xyz = klm * float3(1, %s, %s);", |
| 109 | OutName(fKLMD), coverage, coverage); // coverage == wind on curves. |
| 110 | code->appendf("%s.w = dot(float3(%s, 1), %s);", // Flat edge opposite the curve. |
| 111 | OutName(fKLMD), position, fEdgeDistanceEquation.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 112 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 113 | fGradMatrix.reset(kFloat2x2_GrSLType, scope); |
| 114 | varyingHandler->addVarying("grad_matrix", &fGradMatrix); |
| 115 | code->appendf("%s[0] = 2*bloat * 3 * klm[0] * %s[0].xy;", |
| 116 | OutName(fGradMatrix), fKLMMatrix.c_str()); |
| 117 | code->appendf("%s[1] = -2*bloat * (klm[1] * %s[2].xy + klm[2] * %s[1].xy);", |
| 118 | OutName(fGradMatrix), fKLMMatrix.c_str(), fKLMMatrix.c_str()); |
| 119 | |
| 120 | if (attenuatedCoverage) { |
| 121 | fCornerCoverage.reset(kHalf2_GrSLType, scope); |
| 122 | varyingHandler->addVarying("corner_coverage", &fCornerCoverage); |
| 123 | code->appendf("%s = %s;", // Attenuated corner coverage. |
| 124 | OutName(fCornerCoverage), attenuatedCoverage); |
| 125 | } |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 126 | } |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 127 | |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 128 | void GrCCCubicShader::onEmitFragmentCode(GrGLSLFPFragmentBuilder* f, |
| 129 | const char* outputCoverage) const { |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 130 | f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z;", fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn()); |
| 131 | f->codeAppend ("float f = k*k*k - l*m;"); |
Chris Dalton | b709f27 | 2018-03-21 23:13:45 -0600 | [diff] [blame] | 132 | f->codeAppendf("float2 grad = %s * float2(k, 1);", fGradMatrix.fsIn()); |
| 133 | f->codeAppend ("float fwidth = abs(grad.x) + abs(grad.y);"); |
| 134 | f->codeAppendf("%s = clamp(0.5 - f/fwidth, 0, 1);", outputCoverage); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 135 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 136 | f->codeAppendf("half d = min(%s.w, 0);", fKLMD.fsIn()); // Flat edge opposite the curve. |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 137 | // Wind is the sign of both L and/or M. Take the sign of whichever has the larger magnitude. |
| 138 | // (In reality, either would be fine because we chop cubics with more than a half pixel of |
| 139 | // padding around the L & M lines, so neither should approach zero.) |
| 140 | f->codeAppend ("half wind = sign(l + m);"); |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 141 | f->codeAppendf("%s = (%s + d) * wind;", outputCoverage, outputCoverage); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 142 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 143 | if (fCornerCoverage.fsIn()) { |
| 144 | f->codeAppendf("%s = %s.x * %s.y + %s;", // Attenuated corner coverage. |
| 145 | outputCoverage, fCornerCoverage.fsIn(), fCornerCoverage.fsIn(), |
| 146 | outputCoverage); |
| 147 | } |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 148 | } |