blob: 0d34d3748ae1d9e0e2c8c84bb7f62f9a45e55e38 [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"
Chris Dalton1fbdb612017-12-12 12:48:47 -070011#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton6a3dbee2017-10-16 10:44:41 -060012
Chris Dalton1fbdb612017-12-12 12:48:47 -070013void GrCCPRCubicShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
14 const char* repetitionID, const char* wind,
Chris Daltonc17bf322017-10-24 10:59:03 -060015 GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060016 // Find the cubic's power basis coefficients.
17 s->codeAppendf("float2x4 C = float4x4(-1, 3, -3, 1, "
18 " 3, -6, 3, 0, "
19 "-3, 3, 0, 0, "
20 " 1, 0, 0, 0) * transpose(%s);", pts);
21
22 // Find the cubic's inflection function.
23 s->codeAppend ("float D3 = +determinant(float2x2(C[0].yz, C[1].yz));");
24 s->codeAppend ("float D2 = -determinant(float2x2(C[0].xz, C[1].xz));");
25 s->codeAppend ("float D1 = +determinant(float2x2(C));");
26
27 // Calculate the KLM matrix.
28 s->declareGlobal(fKLMMatrix);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060029 s->codeAppend ("float discr = 3*D2*D2 - 4*D1*D3;");
Chris Daltonbe4ffab2017-12-08 10:59:58 -070030 s->codeAppend ("float x = discr >= 0 ? 3 : 1;");
31 s->codeAppend ("float q = sqrt(x * abs(discr));");
32 s->codeAppend ("q = x*D2 + (D2 >= 0 ? q : -q);");
33
34 s->codeAppend ("float2 l, m;");
35 s->codeAppend ("l.ts = normalize(float2(q, 2*x * D1));");
36 s->codeAppend ("m.ts = normalize(float2(2, q) * (discr >= 0 ? float2(D3, 1) "
37 ": float2(D2*D2 - D3*D1, D1)));");
38
39 s->codeAppend ("float4 K;");
40 s->codeAppend ("float4 lm = l.sstt * m.stst;");
41 s->codeAppend ("K = float4(0, lm.x, -lm.y - lm.z, lm.w);");
42
43 s->codeAppend ("float4 L, M;");
44 s->codeAppend ("lm.yz += 2*lm.zy;");
45 s->codeAppend ("L = float4(-1,x,-x,1) * l.sstt * (discr >= 0 ? l.ssst * l.sttt : lm);");
46 s->codeAppend ("M = float4(-1,x,-x,1) * m.sstt * (discr >= 0 ? m.ssst * m.sttt : lm.xzyw);");
47
Chris Dalton6a3dbee2017-10-16 10:44:41 -060048 s->codeAppend ("short middlerow = abs(D2) > abs(D1) ? 2 : 1;");
49 s->codeAppend ("float3x3 CI = inverse(float3x3(C[0][0], C[0][middlerow], C[0][3], "
50 "C[1][0], C[1][middlerow], C[1][3], "
51 " 0, 0, 1));");
52 s->codeAppendf("%s = CI * float3x3(K[0], K[middlerow], K[3], "
53 "L[0], L[middlerow], L[3], "
54 "M[0], M[middlerow], M[3]);", fKLMMatrix.c_str());
55
Chris Dalton1fbdb612017-12-12 12:48:47 -070056 // Evaluate the cubic at T=.5 for a mid-ish point.
57 s->codeAppendf("float2 midpoint = %s * float4(.125, .375, .375, .125);", pts);
58
Chris Dalton6a3dbee2017-10-16 10:44:41 -060059 // Orient the KLM matrix so we fill the correct side of the curve.
60 s->codeAppendf("float2 orientation = sign(float3(midpoint, 1) * float2x3(%s[1], %s[2]));",
61 fKLMMatrix.c_str(), fKLMMatrix.c_str());
62 s->codeAppendf("%s *= float3x3(orientation[0] * orientation[1], 0, 0, "
63 "0, orientation[0], 0, "
64 "0, 0, orientation[1]);", fKLMMatrix.c_str());
65
Chris Dalton6a3dbee2017-10-16 10:44:41 -060066 // Determine the amount of additional coverage to subtract out for the flat edge (P3 -> P0).
67 s->declareGlobal(fEdgeDistanceEquation);
68 s->codeAppendf("short edgeidx0 = %s > 0 ? 3 : 0;", wind);
69 s->codeAppendf("float2 edgept0 = %s[edgeidx0];", pts);
70 s->codeAppendf("float2 edgept1 = %s[3 - edgeidx0];", pts);
71 Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str());
72
Chris Dalton1fbdb612017-12-12 12:48:47 -070073 this->onEmitSetupCode(s, pts, repetitionID, vars);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060074}
75
76GrCCPRCubicShader::WindHandling
77GrCCPRCubicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code,
78 const char* position, const char* /*coverage*/,
79 const char* /*wind*/) {
Chris Daltonfdde34e2017-10-16 14:15:26 -060080 varyingHandler->addVarying("klmd", &fKLMD);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060081 code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str());
82 code->appendf("float d = dot(float3(%s, 1), %s);", position, fEdgeDistanceEquation.c_str());
83 code->appendf("%s = float4(klm, d);", fKLMD.gsOut());
84
85 this->onEmitVaryings(varyingHandler, code);
86 return WindHandling::kNotHandled;
87}
88
Chris Dalton6a3dbee2017-10-16 10:44:41 -060089void GrCCPRCubicHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code) {
90 // "klm" was just defined by the base class.
Chris Daltonfdde34e2017-10-16 14:15:26 -060091 varyingHandler->addVarying("grad_matrix", &fGradMatrix);
Chris Daltoncc0ab7e2017-10-24 14:16:52 -060092 code->appendf("%s[0] = 3 * klm[0] * %s[0].xy;", fGradMatrix.gsOut(), fKLMMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060093 code->appendf("%s[1] = -klm[1] * %s[2].xy - klm[2] * %s[1].xy;",
Chris Daltoncc0ab7e2017-10-24 14:16:52 -060094 fGradMatrix.gsOut(), fKLMMatrix.c_str(), fKLMMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060095}
96
97void GrCCPRCubicHullShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
98 const char* outputCoverage) const {
99 f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;",
100 fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn());
101 f->codeAppend ("float f = k*k*k - l*m;");
102 f->codeAppendf("float2 grad_f = %s * float2(k, 1);", fGradMatrix.fsIn());
103 f->codeAppendf("%s = clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);", outputCoverage);
104 f->codeAppendf("%s += min(d, 0);", outputCoverage); // Flat closing edge.
105}
106
Chris Dalton1fbdb612017-12-12 12:48:47 -0700107void GrCCPRCubicCornerShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
108 const char* repetitionID, GeometryVars* vars) const {
109 s->codeAppendf("float2 corner = %s[%s * 3];", pts, repetitionID);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600110 vars->fCornerVars.fPoint = "corner";
111}
112
113void GrCCPRCubicCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code) {
Chris Daltonfdde34e2017-10-16 14:15:26 -0600114 varyingHandler->addFlatVarying("dklmddx", &fdKLMDdx);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600115 code->appendf("%s = float4(%s[0].x, %s[1].x, %s[2].x, %s.x);",
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600116 fdKLMDdx.gsOut(), fKLMMatrix.c_str(), fKLMMatrix.c_str(),
117 fKLMMatrix.c_str(), fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600118
Chris Daltonfdde34e2017-10-16 14:15:26 -0600119 varyingHandler->addFlatVarying("dklmddy", &fdKLMDdy);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600120 code->appendf("%s = float4(%s[0].y, %s[1].y, %s[2].y, %s.y);",
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600121 fdKLMDdy.gsOut(), fKLMMatrix.c_str(), fKLMMatrix.c_str(),
122 fKLMMatrix.c_str(), fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600123}
124
125void GrCCPRCubicCornerShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
126 const char* outputCoverage) const {
127 f->codeAppendf("float2x4 grad_klmd = float2x4(%s, %s);", fdKLMDdx.fsIn(), fdKLMDdy.fsIn());
128
129 // Erase what the previous hull shader wrote. We don't worry about the two corners falling on
130 // the same pixel because those cases should have been weeded out by this point.
131 f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;",
132 fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn());
133 f->codeAppend ("float f = k*k*k - l*m;");
134 f->codeAppend ("float2 grad_f = float3(3*k*k, -m, -l) * float2x3(grad_klmd);");
135 f->codeAppendf("%s = -clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);",
136 outputCoverage);
137 f->codeAppendf("%s -= d;", outputCoverage);
138
139 // Use software msaa to estimate actual coverage at the corner pixels.
140 const int sampleCount = Shader::DefineSoftSampleLocations(f, "samples");
141 f->codeAppendf("float4 klmd_center = float4(%s.xyz, %s.w + 0.5);",
142 fKLMD.fsIn(), fKLMD.fsIn());
143 f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount);
144 f->codeAppend ( "float4 klmd = grad_klmd * samples[i] + klmd_center;");
145 f->codeAppend ( "half f = klmd.y * klmd.z - klmd.x * klmd.x * klmd.x;");
146 f->codeAppendf( "%s += all(greaterThan(half4(f, klmd.y, klmd.z, klmd.w), "
147 "half4(0))) ? %f : 0;",
148 outputCoverage, 1.0 / sampleCount);
149 f->codeAppend ("}");
150}