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 | fe462ef | 2018-03-08 15:54:01 +0000 | [diff] [blame^] | 15 | void GrCCCubicShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts, |
| 16 | const char* /*repetitionID*/, const char* /*wind*/, |
| 17 | GeometryVars*) 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 | df04ce2 | 2018-03-07 17:28:07 -0700 | [diff] [blame] | 61 | // 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] | 62 | s->codeAppendf("float2 orientation = sign(float3(midpoint, 1) * float2x3(%s[1], %s[2]));", |
| 63 | fKLMMatrix.c_str(), fKLMMatrix.c_str()); |
| 64 | s->codeAppendf("%s *= float3x3(orientation[0] * orientation[1], 0, 0, " |
Chris Dalton | df04ce2 | 2018-03-07 17:28:07 -0700 | [diff] [blame] | 65 | "0, orientation[0], 0, " |
| 66 | "0, 0, orientation[1]);", fKLMMatrix.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 67 | } |
| 68 | |
Chris Dalton | fe462ef | 2018-03-08 15:54:01 +0000 | [diff] [blame^] | 69 | void GrCCCubicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, |
| 70 | GrGLSLVarying::Scope scope, SkString* code, |
| 71 | const char* position, const char* inputCoverage, |
| 72 | const char* wind) { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 73 | code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 74 | |
Chris Dalton | df04ce2 | 2018-03-07 17:28:07 -0700 | [diff] [blame] | 75 | fKLMW.reset(kFloat4_GrSLType, scope); |
| 76 | varyingHandler->addVarying("klmw", &fKLMW); |
| 77 | code->appendf("%s.xyz = klm;", OutName(fKLMW)); |
Chris Dalton | fe462ef | 2018-03-08 15:54:01 +0000 | [diff] [blame^] | 78 | code->appendf("%s.w = %s * %s;", OutName(fKLMW), inputCoverage, wind); |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 79 | |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 80 | fGradMatrix.reset(kFloat2x2_GrSLType, scope); |
Chris Dalton | d23c7c4 | 2017-12-22 19:05:15 +0000 | [diff] [blame] | 81 | varyingHandler->addVarying("grad_matrix", &fGradMatrix); |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 82 | 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] | 83 | 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] | 84 | OutName(fGradMatrix), fKLMMatrix.c_str(), fKLMMatrix.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 85 | } |
| 86 | |
Chris Dalton | df04ce2 | 2018-03-07 17:28:07 -0700 | [diff] [blame] | 87 | void GrCCCubicShader::onEmitFragmentCode(const GrCCCoverageProcessor& proc, |
| 88 | GrGLSLFPFragmentBuilder* f, |
| 89 | const char* outputCoverage) const { |
| 90 | f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z;", |
| 91 | fKLMW.fsIn(), fKLMW.fsIn(), fKLMW.fsIn()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 92 | f->codeAppend ("float f = k*k*k - l*m;"); |
| 93 | f->codeAppendf("float2 grad_f = %s * float2(k, 1);", fGradMatrix.fsIn()); |
Chris Dalton | df04ce2 | 2018-03-07 17:28:07 -0700 | [diff] [blame] | 94 | f->codeAppend ("float d = f * inversesqrt(dot(grad_f, grad_f));"); |
| 95 | #ifdef SK_DEBUG |
| 96 | if (proc.debugVisualizationsEnabled()) { |
| 97 | f->codeAppendf("d /= %f;", proc.debugBloat()); |
| 98 | } |
| 99 | #endif |
| 100 | f->codeAppendf("%s = clamp(0.5 - d, 0, 1) * %s.w;", outputCoverage, fKLMW.fsIn()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 101 | } |