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 | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 11 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 12 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 13 | using Shader = GrCCCoverageProcessor::Shader; |
Chris Dalton | de5a814 | 2017-12-18 10:05:15 -0700 | [diff] [blame] | 14 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 15 | void GrCCCubicShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts, |
| 16 | const char* repetitionID, const char* wind, |
| 17 | GeometryVars* vars) const { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 18 | // Find the cubic's power basis coefficients. |
| 19 | s->codeAppendf("float2x4 C = float4x4(-1, 3, -3, 1, " |
| 20 | " 3, -6, 3, 0, " |
| 21 | "-3, 3, 0, 0, " |
| 22 | " 1, 0, 0, 0) * transpose(%s);", pts); |
| 23 | |
| 24 | // Find the cubic's inflection function. |
| 25 | s->codeAppend ("float D3 = +determinant(float2x2(C[0].yz, C[1].yz));"); |
| 26 | s->codeAppend ("float D2 = -determinant(float2x2(C[0].xz, C[1].xz));"); |
| 27 | s->codeAppend ("float D1 = +determinant(float2x2(C));"); |
| 28 | |
| 29 | // Calculate the KLM matrix. |
| 30 | s->declareGlobal(fKLMMatrix); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 31 | s->codeAppend ("float discr = 3*D2*D2 - 4*D1*D3;"); |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 32 | s->codeAppend ("float x = discr >= 0 ? 3 : 1;"); |
| 33 | s->codeAppend ("float q = sqrt(x * abs(discr));"); |
| 34 | s->codeAppend ("q = x*D2 + (D2 >= 0 ? q : -q);"); |
| 35 | |
| 36 | s->codeAppend ("float2 l, m;"); |
| 37 | s->codeAppend ("l.ts = normalize(float2(q, 2*x * D1));"); |
| 38 | s->codeAppend ("m.ts = normalize(float2(2, q) * (discr >= 0 ? float2(D3, 1) " |
| 39 | ": float2(D2*D2 - D3*D1, D1)));"); |
| 40 | |
| 41 | s->codeAppend ("float4 K;"); |
| 42 | s->codeAppend ("float4 lm = l.sstt * m.stst;"); |
| 43 | s->codeAppend ("K = float4(0, lm.x, -lm.y - lm.z, lm.w);"); |
| 44 | |
| 45 | s->codeAppend ("float4 L, M;"); |
| 46 | s->codeAppend ("lm.yz += 2*lm.zy;"); |
| 47 | s->codeAppend ("L = float4(-1,x,-x,1) * l.sstt * (discr >= 0 ? l.ssst * l.sttt : lm);"); |
| 48 | s->codeAppend ("M = float4(-1,x,-x,1) * m.sstt * (discr >= 0 ? m.ssst * m.sttt : lm.xzyw);"); |
| 49 | |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 50 | s->codeAppend ("short middlerow = abs(D2) > abs(D1) ? 2 : 1;"); |
| 51 | s->codeAppend ("float3x3 CI = inverse(float3x3(C[0][0], C[0][middlerow], C[0][3], " |
| 52 | "C[1][0], C[1][middlerow], C[1][3], " |
| 53 | " 0, 0, 1));"); |
| 54 | s->codeAppendf("%s = CI * float3x3(K[0], K[middlerow], K[3], " |
| 55 | "L[0], L[middlerow], L[3], " |
| 56 | "M[0], M[middlerow], M[3]);", fKLMMatrix.c_str()); |
| 57 | |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 58 | // Evaluate the cubic at T=.5 for a mid-ish point. |
| 59 | s->codeAppendf("float2 midpoint = %s * float4(.125, .375, .375, .125);", pts); |
| 60 | |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 61 | // Orient the KLM matrix so L & M have matching signs on the side of the curve we wish to fill. |
| 62 | // We give L & M both the same sign as wind, in order to pass this value to the fragment shader. |
| 63 | // (Cubics are pre-chopped such that L & M do not change sign within any individual segment). |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 64 | s->codeAppendf("float2 orientation = sign(float3(midpoint, 1) * float2x3(%s[1], %s[2]));", |
| 65 | fKLMMatrix.c_str(), fKLMMatrix.c_str()); |
| 66 | s->codeAppendf("%s *= float3x3(orientation[0] * orientation[1], 0, 0, " |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 67 | "0, orientation[0] * %s, 0, " |
| 68 | "0, 0, orientation[1] * %s);", fKLMMatrix.c_str(), wind, wind); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 69 | |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 70 | // Determine the amount of additional coverage to subtract out for the flat edge (P3 -> P0). |
| 71 | s->declareGlobal(fEdgeDistanceEquation); |
| 72 | s->codeAppendf("short edgeidx0 = %s > 0 ? 3 : 0;", wind); |
| 73 | s->codeAppendf("float2 edgept0 = %s[edgeidx0];", pts); |
| 74 | s->codeAppendf("float2 edgept1 = %s[3 - edgeidx0];", pts); |
| 75 | Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str()); |
| 76 | |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 77 | this->onEmitSetupCode(s, pts, repetitionID, vars); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 78 | } |
| 79 | |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 80 | void GrCCCubicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, |
| 81 | GrGLSLVarying::Scope scope, SkString* code, |
| 82 | const char* position, const char* inputCoverage, |
| 83 | const char* /*wind*/) { |
| 84 | SkASSERT(!inputCoverage); |
Chris Dalton | de5a814 | 2017-12-18 10:05:15 -0700 | [diff] [blame] | 85 | |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 86 | fKLMD.reset(kFloat4_GrSLType, scope); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 87 | varyingHandler->addVarying("klmd", &fKLMD); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 88 | code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str()); |
| 89 | code->appendf("float d = dot(float3(%s, 1), %s);", position, fEdgeDistanceEquation.c_str()); |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 90 | code->appendf("%s = float4(klm, d);", OutName(fKLMD)); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 91 | |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 92 | this->onEmitVaryings(varyingHandler, scope, code); |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void GrCCCubicShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f, |
| 96 | const char* outputCoverage) const { |
| 97 | f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;", |
| 98 | fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn()); |
| 99 | |
| 100 | this->emitCoverage(f, outputCoverage); |
| 101 | |
| 102 | // Wind is the sign of both L and/or M. Take the sign of whichever has the larger magnitude. |
| 103 | // (In reality, either would be fine because we chop cubics with more than a half pixel of |
| 104 | // padding around the L & M lines, so neither should approach zero.) |
| 105 | f->codeAppend ("half wind = sign(l + m);"); |
| 106 | f->codeAppendf("%s *= wind;", outputCoverage); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 107 | } |
| 108 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 109 | void GrCCCubicHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, |
| 110 | GrGLSLVarying::Scope scope, SkString* code) { |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 111 | fGradMatrix.reset(kFloat2x2_GrSLType, scope); |
Chris Dalton | d23c7c4 | 2017-12-22 19:05:15 +0000 | [diff] [blame] | 112 | varyingHandler->addVarying("grad_matrix", &fGradMatrix); |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 113 | // "klm" was just defined by the base class. |
| 114 | code->appendf("%s[0] = 3 * klm[0] * %s[0].xy;", OutName(fGradMatrix), fKLMMatrix.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 115 | code->appendf("%s[1] = -klm[1] * %s[2].xy - klm[2] * %s[1].xy;", |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 116 | OutName(fGradMatrix), fKLMMatrix.c_str(), fKLMMatrix.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 117 | } |
| 118 | |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 119 | void GrCCCubicHullShader::emitCoverage(GrGLSLPPFragmentBuilder* f, |
| 120 | const char* outputCoverage) const { |
| 121 | // k,l,m,d are defined by the base class. |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 122 | f->codeAppend ("float f = k*k*k - l*m;"); |
| 123 | f->codeAppendf("float2 grad_f = %s * float2(k, 1);", fGradMatrix.fsIn()); |
| 124 | f->codeAppendf("%s = clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);", outputCoverage); |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 125 | f->codeAppendf("%s += min(d, 0);", outputCoverage); // Flat edge opposite the curve. |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 126 | } |
| 127 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 128 | void GrCCCubicCornerShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts, |
| 129 | const char* repetitionID, GeometryVars* vars) const { |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 130 | s->codeAppendf("float2 corner = %s[%s * 3];", pts, repetitionID); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 131 | vars->fCornerVars.fPoint = "corner"; |
| 132 | } |
| 133 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 134 | void GrCCCubicCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, |
| 135 | GrGLSLVarying::Scope scope, SkString* code) { |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 136 | using Interpolation = GrGLSLVaryingHandler::Interpolation; |
| 137 | |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 138 | fdKLMDdx.reset(kFloat4_GrSLType, scope); |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 139 | varyingHandler->addVarying("dklmddx", &fdKLMDdx, Interpolation::kCanBeFlat); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 140 | code->appendf("%s = float4(%s[0].x, %s[1].x, %s[2].x, %s.x);", |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 141 | OutName(fdKLMDdx), fKLMMatrix.c_str(), fKLMMatrix.c_str(), |
Chris Dalton | cc0ab7e | 2017-10-24 14:16:52 -0600 | [diff] [blame] | 142 | fKLMMatrix.c_str(), fEdgeDistanceEquation.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 143 | |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 144 | fdKLMDdy.reset(kFloat4_GrSLType, scope); |
Chris Dalton | 7b04631 | 2018-02-02 11:06:30 -0700 | [diff] [blame] | 145 | varyingHandler->addVarying("dklmddy", &fdKLMDdy, Interpolation::kCanBeFlat); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 146 | code->appendf("%s = float4(%s[0].y, %s[1].y, %s[2].y, %s.y);", |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 147 | OutName(fdKLMDdy), fKLMMatrix.c_str(), fKLMMatrix.c_str(), |
Chris Dalton | cc0ab7e | 2017-10-24 14:16:52 -0600 | [diff] [blame] | 148 | fKLMMatrix.c_str(), fEdgeDistanceEquation.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 149 | } |
| 150 | |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 151 | void GrCCCubicCornerShader::emitCoverage(GrGLSLPPFragmentBuilder* f, |
| 152 | const char* outputCoverage) const { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 153 | f->codeAppendf("float2x4 grad_klmd = float2x4(%s, %s);", fdKLMDdx.fsIn(), fdKLMDdy.fsIn()); |
| 154 | |
| 155 | // Erase what the previous hull shader wrote. We don't worry about the two corners falling on |
| 156 | // the same pixel because those cases should have been weeded out by this point. |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 157 | // k,l,m,d are defined by the base class. |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 158 | f->codeAppend ("float f = k*k*k - l*m;"); |
| 159 | f->codeAppend ("float2 grad_f = float3(3*k*k, -m, -l) * float2x3(grad_klmd);"); |
| 160 | f->codeAppendf("%s = -clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);", |
| 161 | outputCoverage); |
| 162 | f->codeAppendf("%s -= d;", outputCoverage); |
| 163 | |
| 164 | // Use software msaa to estimate actual coverage at the corner pixels. |
| 165 | const int sampleCount = Shader::DefineSoftSampleLocations(f, "samples"); |
| 166 | f->codeAppendf("float4 klmd_center = float4(%s.xyz, %s.w + 0.5);", |
| 167 | fKLMD.fsIn(), fKLMD.fsIn()); |
| 168 | f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount); |
| 169 | f->codeAppend ( "float4 klmd = grad_klmd * samples[i] + klmd_center;"); |
| 170 | f->codeAppend ( "half f = klmd.y * klmd.z - klmd.x * klmd.x * klmd.x;"); |
| 171 | f->codeAppendf( "%s += all(greaterThan(half4(f, klmd.y, klmd.z, klmd.w), " |
| 172 | "half4(0))) ? %f : 0;", |
| 173 | outputCoverage, 1.0 / sampleCount); |
| 174 | f->codeAppend ("}"); |
| 175 | } |