blob: 3a820d7a7b5859a5fb06c35d53b4ba35c76760ca [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
12void 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
20void GrCCPRCubicShader::emitWind(GrGLSLShaderBuilder* s, const char* pts,
Chris Daltonc17bf322017-10-24 10:59:03 -060021 const char* outputWind) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060022
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.
Chris Daltonc17bf322017-10-24 10:59:03 -060028 s->codeAppendf("if (2 * abs(area_times_2) < length(%s[3] - %s[0])) {", pts, pts);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060029#ifndef SK_BUILD_FOR_MAC
30 s->codeAppend ( "return;");
31#else
32 // Returning from this geometry shader makes Mac very unhappy. Instead we make wind 0.
33 s->codeAppend ( "area_times_2 = 0;");
34#endif
35 s->codeAppend ("}");
36 s->codeAppendf("%s = sign(area_times_2);", outputWind);
37}
38
39void GrCCPRCubicShader::emitSetupCode(GrGLSLShaderBuilder* s, const char* pts,
Chris Daltonc17bf322017-10-24 10:59:03 -060040 const char* segmentId, const char* wind,
41 GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060042 // Evaluate the cubic at T=.5 for an mid-ish point.
43 s->codeAppendf("float2 midpoint = %s * float4(.125, .375, .375, .125);", pts);
44
45 // Find the cubic's power basis coefficients.
46 s->codeAppendf("float2x4 C = float4x4(-1, 3, -3, 1, "
47 " 3, -6, 3, 0, "
48 "-3, 3, 0, 0, "
49 " 1, 0, 0, 0) * transpose(%s);", pts);
50
51 // Find the cubic's inflection function.
52 s->codeAppend ("float D3 = +determinant(float2x2(C[0].yz, C[1].yz));");
53 s->codeAppend ("float D2 = -determinant(float2x2(C[0].xz, C[1].xz));");
54 s->codeAppend ("float D1 = +determinant(float2x2(C));");
55
56 // Calculate the KLM matrix.
57 s->declareGlobal(fKLMMatrix);
58 s->codeAppend ("float4 K, L, M;");
59 s->codeAppend ("float2 l, m;");
60 s->codeAppend ("float discr = 3*D2*D2 - 4*D1*D3;");
61 if (CubicType::kSerpentine == fCubicType) {
62 // This math also works out for the "cusp" and "cusp at infinity" cases.
63 s->codeAppend ("float q = sqrt(max(3*discr, 0));");
64 s->codeAppend ("q = 3*D2 + (D2 >= 0 ? q : -q);");
65 s->codeAppend ("l.ts = normalize(float2(q, 6*D1));");
66 s->codeAppend ("m.ts = discr <= 0 ? l.ts : normalize(float2(2*D3, q));");
67 s->codeAppend ("K = float4(0, l.s * m.s, -l.t * m.s - m.t * l.s, l.t * m.t);");
68 s->codeAppend ("L = float4(-1,3,-3,1) * l.ssst * l.sstt * l.sttt;");
69 s->codeAppend ("M = float4(-1,3,-3,1) * m.ssst * m.sstt * m.sttt;");
70 } else {
71 s->codeAppend ("float q = sqrt(max(-discr, 0));");
72 s->codeAppend ("q = D2 + (D2 >= 0 ? q : -q);");
73 s->codeAppend ("l.ts = normalize(float2(q, 2*D1));");
74 s->codeAppend ("m.ts = discr >= 0 ? l.ts : normalize(float2(2 * (D2*D2 - D3*D1), D1*q));");
75 s->codeAppend ("float4 lxm = float4(l.s * m.s, l.s * m.t, l.t * m.s, l.t * m.t);");
76 s->codeAppend ("K = float4(0, lxm.x, -lxm.y - lxm.z, lxm.w);");
77 s->codeAppend ("L = float4(-1,1,-1,1) * l.sstt * (lxm.xyzw + float4(0, 2*lxm.zy, 0));");
78 s->codeAppend ("M = float4(-1,1,-1,1) * m.sstt * (lxm.xzyw + float4(0, 2*lxm.yz, 0));");
79 }
80 s->codeAppend ("short middlerow = abs(D2) > abs(D1) ? 2 : 1;");
81 s->codeAppend ("float3x3 CI = inverse(float3x3(C[0][0], C[0][middlerow], C[0][3], "
82 "C[1][0], C[1][middlerow], C[1][3], "
83 " 0, 0, 1));");
84 s->codeAppendf("%s = CI * float3x3(K[0], K[middlerow], K[3], "
85 "L[0], L[middlerow], L[3], "
86 "M[0], M[middlerow], M[3]);", fKLMMatrix.c_str());
87
88 // Orient the KLM matrix so we fill the correct side of the curve.
89 s->codeAppendf("float2 orientation = sign(float3(midpoint, 1) * float2x3(%s[1], %s[2]));",
90 fKLMMatrix.c_str(), fKLMMatrix.c_str());
91 s->codeAppendf("%s *= float3x3(orientation[0] * orientation[1], 0, 0, "
92 "0, orientation[0], 0, "
93 "0, 0, orientation[1]);", fKLMMatrix.c_str());
94
Chris Daltonc17bf322017-10-24 10:59:03 -060095 // TODO: remove in followup CL.
Chris Dalton6a3dbee2017-10-16 10:44:41 -060096 s->declareGlobal(fKLMDerivatives);
Chris Daltonc17bf322017-10-24 10:59:03 -060097 s->codeAppendf("%s[0] = %s[0].xy;",
98 fKLMDerivatives.c_str(), fKLMMatrix.c_str());
99 s->codeAppendf("%s[1] = %s[1].xy;",
100 fKLMDerivatives.c_str(), fKLMMatrix.c_str());
101 s->codeAppendf("%s[2] = %s[2].xy;",
102 fKLMDerivatives.c_str(), fKLMMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600103
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
Chris Daltonc17bf322017-10-24 10:59:03 -0600111 this->onEmitSetupCode(s, pts, segmentId, vars);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600112}
113
114GrCCPRCubicShader::WindHandling
115GrCCPRCubicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code,
116 const char* position, const char* /*coverage*/,
117 const char* /*wind*/) {
Chris Daltonfdde34e2017-10-16 14:15:26 -0600118 varyingHandler->addVarying("klmd", &fKLMD);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600119 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
127void GrCCPRCubicHullShader::onEmitSetupCode(GrGLSLShaderBuilder* s, const char* /*pts*/,
Chris Daltonc17bf322017-10-24 10:59:03 -0600128 const char* /*wedgeId*/, GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600129 // "midpoint" was just defined by the base class.
130 vars->fHullVars.fAlternateMidpoint = "midpoint";
131}
132
133void GrCCPRCubicHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code) {
134 // "klm" was just defined by the base class.
Chris Daltonfdde34e2017-10-16 14:15:26 -0600135 varyingHandler->addVarying("grad_matrix", &fGradMatrix);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600136 code->appendf("%s[0] = 3 * klm[0] * %s[0];", fGradMatrix.gsOut(), fKLMDerivatives.c_str());
137 code->appendf("%s[1] = -klm[1] * %s[2].xy - klm[2] * %s[1].xy;",
138 fGradMatrix.gsOut(), fKLMDerivatives.c_str(), fKLMDerivatives.c_str());
139}
140
141void GrCCPRCubicHullShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
142 const char* outputCoverage) const {
143 f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;",
144 fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn());
145 f->codeAppend ("float f = k*k*k - l*m;");
146 f->codeAppendf("float2 grad_f = %s * float2(k, 1);", fGradMatrix.fsIn());
147 f->codeAppendf("%s = clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);", outputCoverage);
148 f->codeAppendf("%s += min(d, 0);", outputCoverage); // Flat closing edge.
149}
150
151void GrCCPRCubicCornerShader::onEmitSetupCode(GrGLSLShaderBuilder* s, const char* pts,
Chris Daltonc17bf322017-10-24 10:59:03 -0600152 const char* cornerId, GeometryVars* vars) const {
153 // TODO: remove in followup CL.
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600154 s->declareGlobal(fEdgeDistanceDerivatives);
Chris Daltonc17bf322017-10-24 10:59:03 -0600155 s->codeAppendf("%s = %s.xy;",
156 fEdgeDistanceDerivatives.c_str(), fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600157
158 s->codeAppendf("float2 corner = %s[%s * 3];", pts, cornerId);
159 vars->fCornerVars.fPoint = "corner";
160}
161
162void GrCCPRCubicCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code) {
Chris Daltonfdde34e2017-10-16 14:15:26 -0600163 varyingHandler->addFlatVarying("dklmddx", &fdKLMDdx);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600164 code->appendf("%s = float4(%s[0].x, %s[1].x, %s[2].x, %s.x);",
165 fdKLMDdx.gsOut(), fKLMDerivatives.c_str(), fKLMDerivatives.c_str(),
166 fKLMDerivatives.c_str(), fEdgeDistanceDerivatives.c_str());
167
Chris Daltonfdde34e2017-10-16 14:15:26 -0600168 varyingHandler->addFlatVarying("dklmddy", &fdKLMDdy);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600169 code->appendf("%s = float4(%s[0].y, %s[1].y, %s[2].y, %s.y);",
170 fdKLMDdy.gsOut(), fKLMDerivatives.c_str(), fKLMDerivatives.c_str(),
171 fKLMDerivatives.c_str(), fEdgeDistanceDerivatives.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600172}
173
174void GrCCPRCubicCornerShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
175 const char* outputCoverage) const {
176 f->codeAppendf("float2x4 grad_klmd = float2x4(%s, %s);", fdKLMDdx.fsIn(), fdKLMDdy.fsIn());
177
178 // Erase what the previous hull shader wrote. We don't worry about the two corners falling on
179 // the same pixel because those cases should have been weeded out by this point.
180 f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;",
181 fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn());
182 f->codeAppend ("float f = k*k*k - l*m;");
183 f->codeAppend ("float2 grad_f = float3(3*k*k, -m, -l) * float2x3(grad_klmd);");
184 f->codeAppendf("%s = -clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);",
185 outputCoverage);
186 f->codeAppendf("%s -= d;", outputCoverage);
187
188 // Use software msaa to estimate actual coverage at the corner pixels.
189 const int sampleCount = Shader::DefineSoftSampleLocations(f, "samples");
190 f->codeAppendf("float4 klmd_center = float4(%s.xyz, %s.w + 0.5);",
191 fKLMD.fsIn(), fKLMD.fsIn());
192 f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount);
193 f->codeAppend ( "float4 klmd = grad_klmd * samples[i] + klmd_center;");
194 f->codeAppend ( "half f = klmd.y * klmd.z - klmd.x * klmd.x * klmd.x;");
195 f->codeAppendf( "%s += all(greaterThan(half4(f, klmd.y, klmd.z, klmd.w), "
196 "half4(0))) ? %f : 0;",
197 outputCoverage, 1.0 / sampleCount);
198 f->codeAppend ("}");
199}