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 | |
| 8 | #include "GrCCPRCubicShader.h" |
| 9 | |
| 10 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 11 | |
| 12 | void GrCCPRCubicShader::appendInputPointFetch(const GrCCPRCoverageProcessor& proc, |
| 13 | GrGLSLShaderBuilder* s, |
| 14 | const TexelBufferHandle& pointsBuffer, |
| 15 | const char* pointId) const { |
| 16 | s->appendTexelFetch(pointsBuffer, |
| 17 | SkStringPrintf("%s.x + %s", proc.instanceAttrib(), pointId).c_str()); |
| 18 | } |
| 19 | |
| 20 | void GrCCPRCubicShader::emitWind(GrGLSLShaderBuilder* s, const char* pts, |
| 21 | const char* rtAdjust, const char* outputWind) const { |
| 22 | |
| 23 | s->codeAppendf("float area_times_2 = determinant(float3x3(1, %s[0], " |
| 24 | "1, %s[2], " |
| 25 | "0, %s[3] - %s[1]));", |
| 26 | pts, pts, pts, pts); |
| 27 | // Drop curves that are nearly flat. The KLM math becomes unstable in this case. |
| 28 | s->codeAppendf("if (2 * abs(area_times_2) < length((%s[3] - %s[0]) * %s.zx)) {", |
| 29 | pts, pts, rtAdjust); |
| 30 | #ifndef SK_BUILD_FOR_MAC |
| 31 | s->codeAppend ( "return;"); |
| 32 | #else |
| 33 | // Returning from this geometry shader makes Mac very unhappy. Instead we make wind 0. |
| 34 | s->codeAppend ( "area_times_2 = 0;"); |
| 35 | #endif |
| 36 | s->codeAppend ("}"); |
| 37 | s->codeAppendf("%s = sign(area_times_2);", outputWind); |
| 38 | } |
| 39 | |
| 40 | void GrCCPRCubicShader::emitSetupCode(GrGLSLShaderBuilder* s, const char* pts, |
| 41 | const char* segmentId, const char* bloat, const char* wind, |
| 42 | const char* rtAdjust, GeometryVars* vars) const { |
| 43 | // Evaluate the cubic at T=.5 for an mid-ish point. |
| 44 | s->codeAppendf("float2 midpoint = %s * float4(.125, .375, .375, .125);", pts); |
| 45 | |
| 46 | // Find the cubic's power basis coefficients. |
| 47 | s->codeAppendf("float2x4 C = float4x4(-1, 3, -3, 1, " |
| 48 | " 3, -6, 3, 0, " |
| 49 | "-3, 3, 0, 0, " |
| 50 | " 1, 0, 0, 0) * transpose(%s);", pts); |
| 51 | |
| 52 | // Find the cubic's inflection function. |
| 53 | s->codeAppend ("float D3 = +determinant(float2x2(C[0].yz, C[1].yz));"); |
| 54 | s->codeAppend ("float D2 = -determinant(float2x2(C[0].xz, C[1].xz));"); |
| 55 | s->codeAppend ("float D1 = +determinant(float2x2(C));"); |
| 56 | |
| 57 | // Calculate the KLM matrix. |
| 58 | s->declareGlobal(fKLMMatrix); |
| 59 | s->codeAppend ("float4 K, L, M;"); |
| 60 | s->codeAppend ("float2 l, m;"); |
| 61 | s->codeAppend ("float discr = 3*D2*D2 - 4*D1*D3;"); |
| 62 | if (CubicType::kSerpentine == fCubicType) { |
| 63 | // This math also works out for the "cusp" and "cusp at infinity" cases. |
| 64 | s->codeAppend ("float q = sqrt(max(3*discr, 0));"); |
| 65 | s->codeAppend ("q = 3*D2 + (D2 >= 0 ? q : -q);"); |
| 66 | s->codeAppend ("l.ts = normalize(float2(q, 6*D1));"); |
| 67 | s->codeAppend ("m.ts = discr <= 0 ? l.ts : normalize(float2(2*D3, q));"); |
| 68 | s->codeAppend ("K = float4(0, l.s * m.s, -l.t * m.s - m.t * l.s, l.t * m.t);"); |
| 69 | s->codeAppend ("L = float4(-1,3,-3,1) * l.ssst * l.sstt * l.sttt;"); |
| 70 | s->codeAppend ("M = float4(-1,3,-3,1) * m.ssst * m.sstt * m.sttt;"); |
| 71 | } else { |
| 72 | s->codeAppend ("float q = sqrt(max(-discr, 0));"); |
| 73 | s->codeAppend ("q = D2 + (D2 >= 0 ? q : -q);"); |
| 74 | s->codeAppend ("l.ts = normalize(float2(q, 2*D1));"); |
| 75 | s->codeAppend ("m.ts = discr >= 0 ? l.ts : normalize(float2(2 * (D2*D2 - D3*D1), D1*q));"); |
| 76 | s->codeAppend ("float4 lxm = float4(l.s * m.s, l.s * m.t, l.t * m.s, l.t * m.t);"); |
| 77 | s->codeAppend ("K = float4(0, lxm.x, -lxm.y - lxm.z, lxm.w);"); |
| 78 | s->codeAppend ("L = float4(-1,1,-1,1) * l.sstt * (lxm.xyzw + float4(0, 2*lxm.zy, 0));"); |
| 79 | s->codeAppend ("M = float4(-1,1,-1,1) * m.sstt * (lxm.xzyw + float4(0, 2*lxm.yz, 0));"); |
| 80 | } |
| 81 | s->codeAppend ("short middlerow = abs(D2) > abs(D1) ? 2 : 1;"); |
| 82 | s->codeAppend ("float3x3 CI = inverse(float3x3(C[0][0], C[0][middlerow], C[0][3], " |
| 83 | "C[1][0], C[1][middlerow], C[1][3], " |
| 84 | " 0, 0, 1));"); |
| 85 | s->codeAppendf("%s = CI * float3x3(K[0], K[middlerow], K[3], " |
| 86 | "L[0], L[middlerow], L[3], " |
| 87 | "M[0], M[middlerow], M[3]);", fKLMMatrix.c_str()); |
| 88 | |
| 89 | // Orient the KLM matrix so we fill the correct side of the curve. |
| 90 | s->codeAppendf("float2 orientation = sign(float3(midpoint, 1) * float2x3(%s[1], %s[2]));", |
| 91 | fKLMMatrix.c_str(), fKLMMatrix.c_str()); |
| 92 | s->codeAppendf("%s *= float3x3(orientation[0] * orientation[1], 0, 0, " |
| 93 | "0, orientation[0], 0, " |
| 94 | "0, 0, orientation[1]);", fKLMMatrix.c_str()); |
| 95 | |
| 96 | s->declareGlobal(fKLMDerivatives); |
| 97 | s->codeAppendf("%s[0] = %s[0].xy * %s.xz;", |
| 98 | fKLMDerivatives.c_str(), fKLMMatrix.c_str(), rtAdjust); |
| 99 | s->codeAppendf("%s[1] = %s[1].xy * %s.xz;", |
| 100 | fKLMDerivatives.c_str(), fKLMMatrix.c_str(), rtAdjust); |
| 101 | s->codeAppendf("%s[2] = %s[2].xy * %s.xz;", |
| 102 | fKLMDerivatives.c_str(), fKLMMatrix.c_str(), rtAdjust); |
| 103 | |
| 104 | // Determine the amount of additional coverage to subtract out for the flat edge (P3 -> P0). |
| 105 | s->declareGlobal(fEdgeDistanceEquation); |
| 106 | s->codeAppendf("short edgeidx0 = %s > 0 ? 3 : 0;", wind); |
| 107 | s->codeAppendf("float2 edgept0 = %s[edgeidx0];", pts); |
| 108 | s->codeAppendf("float2 edgept1 = %s[3 - edgeidx0];", pts); |
| 109 | Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str()); |
| 110 | |
| 111 | this->onEmitSetupCode(s, pts, segmentId, rtAdjust, vars); |
| 112 | } |
| 113 | |
| 114 | GrCCPRCubicShader::WindHandling |
| 115 | GrCCPRCubicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code, |
| 116 | const char* position, const char* /*coverage*/, |
| 117 | const char* /*wind*/) { |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame^] | 118 | varyingHandler->addVarying("klmd", &fKLMD); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 119 | code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str()); |
| 120 | code->appendf("float d = dot(float3(%s, 1), %s);", position, fEdgeDistanceEquation.c_str()); |
| 121 | code->appendf("%s = float4(klm, d);", fKLMD.gsOut()); |
| 122 | |
| 123 | this->onEmitVaryings(varyingHandler, code); |
| 124 | return WindHandling::kNotHandled; |
| 125 | } |
| 126 | |
| 127 | void GrCCPRCubicHullShader::onEmitSetupCode(GrGLSLShaderBuilder* s, const char* /*pts*/, |
| 128 | const char* /*wedgeId*/, const char* /*rtAdjust*/, |
| 129 | GeometryVars* vars) const { |
| 130 | // "midpoint" was just defined by the base class. |
| 131 | vars->fHullVars.fAlternateMidpoint = "midpoint"; |
| 132 | } |
| 133 | |
| 134 | void GrCCPRCubicHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code) { |
| 135 | // "klm" was just defined by the base class. |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame^] | 136 | varyingHandler->addVarying("grad_matrix", &fGradMatrix); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 137 | code->appendf("%s[0] = 3 * klm[0] * %s[0];", fGradMatrix.gsOut(), fKLMDerivatives.c_str()); |
| 138 | code->appendf("%s[1] = -klm[1] * %s[2].xy - klm[2] * %s[1].xy;", |
| 139 | fGradMatrix.gsOut(), fKLMDerivatives.c_str(), fKLMDerivatives.c_str()); |
| 140 | } |
| 141 | |
| 142 | void GrCCPRCubicHullShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f, |
| 143 | const char* outputCoverage) const { |
| 144 | f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;", |
| 145 | fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn()); |
| 146 | f->codeAppend ("float f = k*k*k - l*m;"); |
| 147 | f->codeAppendf("float2 grad_f = %s * float2(k, 1);", fGradMatrix.fsIn()); |
| 148 | f->codeAppendf("%s = clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);", outputCoverage); |
| 149 | f->codeAppendf("%s += min(d, 0);", outputCoverage); // Flat closing edge. |
| 150 | } |
| 151 | |
| 152 | void GrCCPRCubicCornerShader::onEmitSetupCode(GrGLSLShaderBuilder* s, const char* pts, |
| 153 | const char* cornerId, const char* rtAdjust, |
| 154 | GeometryVars* vars) const { |
| 155 | s->declareGlobal(fEdgeDistanceDerivatives); |
| 156 | s->codeAppendf("%s = %s.xy * %s.xz;", |
| 157 | fEdgeDistanceDerivatives.c_str(), fEdgeDistanceEquation.c_str(), rtAdjust); |
| 158 | |
| 159 | s->codeAppendf("float2 corner = %s[%s * 3];", pts, cornerId); |
| 160 | vars->fCornerVars.fPoint = "corner"; |
| 161 | } |
| 162 | |
| 163 | void GrCCPRCubicCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code) { |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame^] | 164 | varyingHandler->addFlatVarying("dklmddx", &fdKLMDdx); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 165 | code->appendf("%s = float4(%s[0].x, %s[1].x, %s[2].x, %s.x);", |
| 166 | fdKLMDdx.gsOut(), fKLMDerivatives.c_str(), fKLMDerivatives.c_str(), |
| 167 | fKLMDerivatives.c_str(), fEdgeDistanceDerivatives.c_str()); |
| 168 | |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame^] | 169 | varyingHandler->addFlatVarying("dklmddy", &fdKLMDdy); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 170 | code->appendf("%s = float4(%s[0].y, %s[1].y, %s[2].y, %s.y);", |
| 171 | fdKLMDdy.gsOut(), fKLMDerivatives.c_str(), fKLMDerivatives.c_str(), |
| 172 | fKLMDerivatives.c_str(), fEdgeDistanceDerivatives.c_str()); |
| 173 | |
| 174 | // Otherwise, fEdgeDistances = fEdgeDistances * sign(wind * rtAdjust.x * rdAdjust.z). |
| 175 | GR_STATIC_ASSERT(kTopLeft_GrSurfaceOrigin == GrCCPRCoverageProcessor::kAtlasOrigin); |
| 176 | } |
| 177 | |
| 178 | void GrCCPRCubicCornerShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f, |
| 179 | const char* outputCoverage) const { |
| 180 | f->codeAppendf("float2x4 grad_klmd = float2x4(%s, %s);", fdKLMDdx.fsIn(), fdKLMDdy.fsIn()); |
| 181 | |
| 182 | // Erase what the previous hull shader wrote. We don't worry about the two corners falling on |
| 183 | // the same pixel because those cases should have been weeded out by this point. |
| 184 | f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;", |
| 185 | fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn()); |
| 186 | f->codeAppend ("float f = k*k*k - l*m;"); |
| 187 | f->codeAppend ("float2 grad_f = float3(3*k*k, -m, -l) * float2x3(grad_klmd);"); |
| 188 | f->codeAppendf("%s = -clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);", |
| 189 | outputCoverage); |
| 190 | f->codeAppendf("%s -= d;", outputCoverage); |
| 191 | |
| 192 | // Use software msaa to estimate actual coverage at the corner pixels. |
| 193 | const int sampleCount = Shader::DefineSoftSampleLocations(f, "samples"); |
| 194 | f->codeAppendf("float4 klmd_center = float4(%s.xyz, %s.w + 0.5);", |
| 195 | fKLMD.fsIn(), fKLMD.fsIn()); |
| 196 | f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount); |
| 197 | f->codeAppend ( "float4 klmd = grad_klmd * samples[i] + klmd_center;"); |
| 198 | f->codeAppend ( "half f = klmd.y * klmd.z - klmd.x * klmd.x * klmd.x;"); |
| 199 | f->codeAppendf( "%s += all(greaterThan(half4(f, klmd.y, klmd.z, klmd.w), " |
| 200 | "half4(0))) ? %f : 0;", |
| 201 | outputCoverage, 1.0 / sampleCount); |
| 202 | f->codeAppend ("}"); |
| 203 | } |