blob: 1c29862bea876f0d200a750ac5731177a5f27aa5 [file] [log] [blame]
Chris Dalton6a3dbee2017-10-16 10:44:41 -06001/*
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
Chris Dalton6a3dbee2017-10-16 10:44:41 -060012void GrCCPRCubicShader::emitSetupCode(GrGLSLShaderBuilder* s, const char* pts,
Chris Daltonc17bf322017-10-24 10:59:03 -060013 const char* segmentId, const char* wind,
14 GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060015 // Evaluate the cubic at T=.5 for an mid-ish point.
16 s->codeAppendf("float2 midpoint = %s * float4(.125, .375, .375, .125);", pts);
17
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);
31 s->codeAppend ("float4 K, L, M;");
32 s->codeAppend ("float2 l, m;");
33 s->codeAppend ("float discr = 3*D2*D2 - 4*D1*D3;");
34 if (CubicType::kSerpentine == fCubicType) {
35 // This math also works out for the "cusp" and "cusp at infinity" cases.
36 s->codeAppend ("float q = sqrt(max(3*discr, 0));");
37 s->codeAppend ("q = 3*D2 + (D2 >= 0 ? q : -q);");
38 s->codeAppend ("l.ts = normalize(float2(q, 6*D1));");
39 s->codeAppend ("m.ts = discr <= 0 ? l.ts : normalize(float2(2*D3, q));");
40 s->codeAppend ("K = float4(0, l.s * m.s, -l.t * m.s - m.t * l.s, l.t * m.t);");
41 s->codeAppend ("L = float4(-1,3,-3,1) * l.ssst * l.sstt * l.sttt;");
42 s->codeAppend ("M = float4(-1,3,-3,1) * m.ssst * m.sstt * m.sttt;");
43 } else {
44 s->codeAppend ("float q = sqrt(max(-discr, 0));");
45 s->codeAppend ("q = D2 + (D2 >= 0 ? q : -q);");
46 s->codeAppend ("l.ts = normalize(float2(q, 2*D1));");
47 s->codeAppend ("m.ts = discr >= 0 ? l.ts : normalize(float2(2 * (D2*D2 - D3*D1), D1*q));");
48 s->codeAppend ("float4 lxm = float4(l.s * m.s, l.s * m.t, l.t * m.s, l.t * m.t);");
49 s->codeAppend ("K = float4(0, lxm.x, -lxm.y - lxm.z, lxm.w);");
50 s->codeAppend ("L = float4(-1,1,-1,1) * l.sstt * (lxm.xyzw + float4(0, 2*lxm.zy, 0));");
51 s->codeAppend ("M = float4(-1,1,-1,1) * m.sstt * (lxm.xzyw + float4(0, 2*lxm.yz, 0));");
52 }
53 s->codeAppend ("short middlerow = abs(D2) > abs(D1) ? 2 : 1;");
54 s->codeAppend ("float3x3 CI = inverse(float3x3(C[0][0], C[0][middlerow], C[0][3], "
55 "C[1][0], C[1][middlerow], C[1][3], "
56 " 0, 0, 1));");
57 s->codeAppendf("%s = CI * float3x3(K[0], K[middlerow], K[3], "
58 "L[0], L[middlerow], L[3], "
59 "M[0], M[middlerow], M[3]);", fKLMMatrix.c_str());
60
61 // Orient the KLM matrix so we fill the correct side of the curve.
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, "
65 "0, orientation[0], 0, "
66 "0, 0, orientation[1]);", fKLMMatrix.c_str());
67
Chris Dalton6a3dbee2017-10-16 10:44:41 -060068 // Determine the amount of additional coverage to subtract out for the flat edge (P3 -> P0).
69 s->declareGlobal(fEdgeDistanceEquation);
70 s->codeAppendf("short edgeidx0 = %s > 0 ? 3 : 0;", wind);
71 s->codeAppendf("float2 edgept0 = %s[edgeidx0];", pts);
72 s->codeAppendf("float2 edgept1 = %s[3 - edgeidx0];", pts);
73 Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str());
74
Chris Daltonc17bf322017-10-24 10:59:03 -060075 this->onEmitSetupCode(s, pts, segmentId, vars);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060076}
77
78GrCCPRCubicShader::WindHandling
79GrCCPRCubicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code,
80 const char* position, const char* /*coverage*/,
81 const char* /*wind*/) {
Chris Daltonfdde34e2017-10-16 14:15:26 -060082 varyingHandler->addVarying("klmd", &fKLMD);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060083 code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str());
84 code->appendf("float d = dot(float3(%s, 1), %s);", position, fEdgeDistanceEquation.c_str());
85 code->appendf("%s = float4(klm, d);", fKLMD.gsOut());
86
87 this->onEmitVaryings(varyingHandler, code);
88 return WindHandling::kNotHandled;
89}
90
91void GrCCPRCubicHullShader::onEmitSetupCode(GrGLSLShaderBuilder* s, const char* /*pts*/,
Chris Daltonc17bf322017-10-24 10:59:03 -060092 const char* /*wedgeId*/, GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060093 // "midpoint" was just defined by the base class.
94 vars->fHullVars.fAlternateMidpoint = "midpoint";
95}
96
97void GrCCPRCubicHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code) {
98 // "klm" was just defined by the base class.
Chris Daltonfdde34e2017-10-16 14:15:26 -060099 varyingHandler->addVarying("grad_matrix", &fGradMatrix);
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600100 code->appendf("%s[0] = 3 * klm[0] * %s[0].xy;", fGradMatrix.gsOut(), fKLMMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600101 code->appendf("%s[1] = -klm[1] * %s[2].xy - klm[2] * %s[1].xy;",
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600102 fGradMatrix.gsOut(), fKLMMatrix.c_str(), fKLMMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600103}
104
105void GrCCPRCubicHullShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
106 const char* outputCoverage) const {
107 f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;",
108 fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn());
109 f->codeAppend ("float f = k*k*k - l*m;");
110 f->codeAppendf("float2 grad_f = %s * float2(k, 1);", fGradMatrix.fsIn());
111 f->codeAppendf("%s = clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);", outputCoverage);
112 f->codeAppendf("%s += min(d, 0);", outputCoverage); // Flat closing edge.
113}
114
115void GrCCPRCubicCornerShader::onEmitSetupCode(GrGLSLShaderBuilder* s, const char* pts,
Chris Daltonc17bf322017-10-24 10:59:03 -0600116 const char* cornerId, GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600117 s->codeAppendf("float2 corner = %s[%s * 3];", pts, cornerId);
118 vars->fCornerVars.fPoint = "corner";
119}
120
121void GrCCPRCubicCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code) {
Chris Daltonfdde34e2017-10-16 14:15:26 -0600122 varyingHandler->addFlatVarying("dklmddx", &fdKLMDdx);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600123 code->appendf("%s = float4(%s[0].x, %s[1].x, %s[2].x, %s.x);",
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600124 fdKLMDdx.gsOut(), fKLMMatrix.c_str(), fKLMMatrix.c_str(),
125 fKLMMatrix.c_str(), fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600126
Chris Daltonfdde34e2017-10-16 14:15:26 -0600127 varyingHandler->addFlatVarying("dklmddy", &fdKLMDdy);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600128 code->appendf("%s = float4(%s[0].y, %s[1].y, %s[2].y, %s.y);",
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600129 fdKLMDdy.gsOut(), fKLMMatrix.c_str(), fKLMMatrix.c_str(),
130 fKLMMatrix.c_str(), fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600131}
132
133void GrCCPRCubicCornerShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
134 const char* outputCoverage) const {
135 f->codeAppendf("float2x4 grad_klmd = float2x4(%s, %s);", fdKLMDdx.fsIn(), fdKLMDdy.fsIn());
136
137 // Erase what the previous hull shader wrote. We don't worry about the two corners falling on
138 // the same pixel because those cases should have been weeded out by this point.
139 f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;",
140 fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn());
141 f->codeAppend ("float f = k*k*k - l*m;");
142 f->codeAppend ("float2 grad_f = float3(3*k*k, -m, -l) * float2x3(grad_klmd);");
143 f->codeAppendf("%s = -clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);",
144 outputCoverage);
145 f->codeAppendf("%s -= d;", outputCoverage);
146
147 // Use software msaa to estimate actual coverage at the corner pixels.
148 const int sampleCount = Shader::DefineSoftSampleLocations(f, "samples");
149 f->codeAppendf("float4 klmd_center = float4(%s.xyz, %s.w + 0.5);",
150 fKLMD.fsIn(), fKLMD.fsIn());
151 f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount);
152 f->codeAppend ( "float4 klmd = grad_klmd * samples[i] + klmd_center;");
153 f->codeAppend ( "half f = klmd.y * klmd.z - klmd.x * klmd.x * klmd.x;");
154 f->codeAppendf( "%s += all(greaterThan(half4(f, klmd.y, klmd.z, klmd.w), "
155 "half4(0))) ? %f : 0;",
156 outputCoverage, 1.0 / sampleCount);
157 f->codeAppend ("}");
158}