blob: d0228d59adbf9e8876de9974b153d7c890ff8800 [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
Chris Dalton383a2ef2018-01-08 17:21:41 -05008#include "GrCCCubicShader.h"
Chris Dalton6a3dbee2017-10-16 10:44:41 -06009
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 Dalton383a2ef2018-01-08 17:21:41 -050013using Shader = GrCCCoverageProcessor::Shader;
Chris Daltonde5a8142017-12-18 10:05:15 -070014
Chris Daltonfe462ef2018-03-08 15:54:01 +000015void GrCCCubicShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
Chris Dalton21ba5512018-03-21 17:20:21 -060016 const char* wind, const char** /*tighterHull*/) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060017 // Find the cubic's power basis coefficients.
18 s->codeAppendf("float2x4 C = float4x4(-1, 3, -3, 1, "
19 " 3, -6, 3, 0, "
20 "-3, 3, 0, 0, "
21 " 1, 0, 0, 0) * transpose(%s);", pts);
22
23 // Find the cubic's inflection function.
24 s->codeAppend ("float D3 = +determinant(float2x2(C[0].yz, C[1].yz));");
25 s->codeAppend ("float D2 = -determinant(float2x2(C[0].xz, C[1].xz));");
26 s->codeAppend ("float D1 = +determinant(float2x2(C));");
27
28 // Calculate the KLM matrix.
29 s->declareGlobal(fKLMMatrix);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060030 s->codeAppend ("float discr = 3*D2*D2 - 4*D1*D3;");
Chris Daltonbe4ffab2017-12-08 10:59:58 -070031 s->codeAppend ("float x = discr >= 0 ? 3 : 1;");
32 s->codeAppend ("float q = sqrt(x * abs(discr));");
33 s->codeAppend ("q = x*D2 + (D2 >= 0 ? q : -q);");
34
35 s->codeAppend ("float2 l, m;");
36 s->codeAppend ("l.ts = normalize(float2(q, 2*x * D1));");
37 s->codeAppend ("m.ts = normalize(float2(2, q) * (discr >= 0 ? float2(D3, 1) "
38 ": float2(D2*D2 - D3*D1, D1)));");
39
40 s->codeAppend ("float4 K;");
41 s->codeAppend ("float4 lm = l.sstt * m.stst;");
42 s->codeAppend ("K = float4(0, lm.x, -lm.y - lm.z, lm.w);");
43
44 s->codeAppend ("float4 L, M;");
45 s->codeAppend ("lm.yz += 2*lm.zy;");
46 s->codeAppend ("L = float4(-1,x,-x,1) * l.sstt * (discr >= 0 ? l.ssst * l.sttt : lm);");
47 s->codeAppend ("M = float4(-1,x,-x,1) * m.sstt * (discr >= 0 ? m.ssst * m.sttt : lm.xzyw);");
48
Chris Dalton6a3dbee2017-10-16 10:44:41 -060049 s->codeAppend ("short middlerow = abs(D2) > abs(D1) ? 2 : 1;");
50 s->codeAppend ("float3x3 CI = inverse(float3x3(C[0][0], C[0][middlerow], C[0][3], "
51 "C[1][0], C[1][middlerow], C[1][3], "
52 " 0, 0, 1));");
53 s->codeAppendf("%s = CI * float3x3(K[0], K[middlerow], K[3], "
54 "L[0], L[middlerow], L[3], "
55 "M[0], M[middlerow], M[3]);", fKLMMatrix.c_str());
56
Chris Dalton1fbdb612017-12-12 12:48:47 -070057 // Evaluate the cubic at T=.5 for a mid-ish point.
58 s->codeAppendf("float2 midpoint = %s * float4(.125, .375, .375, .125);", pts);
59
Chris Dalton21ba5512018-03-21 17:20:21 -060060 // Orient the KLM matrix so L & M are both positive on the side of the curve we wish to fill.
Chris Dalton6a3dbee2017-10-16 10:44:41 -060061 s->codeAppendf("float2 orientation = sign(float3(midpoint, 1) * float2x3(%s[1], %s[2]));",
62 fKLMMatrix.c_str(), fKLMMatrix.c_str());
63 s->codeAppendf("%s *= float3x3(orientation[0] * orientation[1], 0, 0, "
Chris Dalton21ba5512018-03-21 17:20:21 -060064 "0, orientation[0], 0, "
65 "0, 0, orientation[1]);", fKLMMatrix.c_str());
Chris Daltonbaf3e782018-03-08 15:55:58 +000066
67 // Determine the amount of additional coverage to subtract out for the flat edge (P3 -> P0).
68 s->declareGlobal(fEdgeDistanceEquation);
69 s->codeAppendf("short edgeidx0 = %s > 0 ? 3 : 0;", wind);
70 s->codeAppendf("float2 edgept0 = %s[edgeidx0];", pts);
71 s->codeAppendf("float2 edgept1 = %s[3 - edgeidx0];", pts);
72 Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060073}
74
Chris Daltonfe462ef2018-03-08 15:54:01 +000075void GrCCCubicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
76 GrGLSLVarying::Scope scope, SkString* code,
Chris Dalton04a1de52018-03-14 02:04:09 -060077 const char* position, const char* coverage,
Chris Dalton21ba5512018-03-21 17:20:21 -060078 const char* attenuatedCoverage) {
Chris Daltonbaf3e782018-03-08 15:55:58 +000079 fKLMD.reset(kFloat4_GrSLType, scope);
80 varyingHandler->addVarying("klmd", &fKLMD);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060081 code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str());
Chris Dalton21ba5512018-03-21 17:20:21 -060082 // We give L & M both the same sign as wind, in order to pass this value to the fragment shader.
83 // (Cubics are pre-chopped such that L & M do not change sign within any individual segment.)
84 code->appendf("%s.xyz = klm * float3(1, %s, %s);",
85 OutName(fKLMD), coverage, coverage); // coverage == wind on curves.
86 code->appendf("%s.w = dot(float3(%s, 1), %s);", // Flat edge opposite the curve.
87 OutName(fKLMD), position, fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060088
Chris Dalton21ba5512018-03-21 17:20:21 -060089 fGradMatrix.reset(kFloat2x2_GrSLType, scope);
90 varyingHandler->addVarying("grad_matrix", &fGradMatrix);
91 code->appendf("%s[0] = 2*bloat * 3 * klm[0] * %s[0].xy;",
92 OutName(fGradMatrix), fKLMMatrix.c_str());
93 code->appendf("%s[1] = -2*bloat * (klm[1] * %s[2].xy + klm[2] * %s[1].xy);",
94 OutName(fGradMatrix), fKLMMatrix.c_str(), fKLMMatrix.c_str());
95
96 if (attenuatedCoverage) {
97 fCornerCoverage.reset(kHalf2_GrSLType, scope);
98 varyingHandler->addVarying("corner_coverage", &fCornerCoverage);
99 code->appendf("%s = %s;", // Attenuated corner coverage.
100 OutName(fCornerCoverage), attenuatedCoverage);
101 }
Chris Daltonbaf3e782018-03-08 15:55:58 +0000102}
Chris Daltonf510e262018-01-30 16:42:37 -0700103
Chris Daltonbaf3e782018-03-08 15:55:58 +0000104void GrCCCubicShader::onEmitFragmentCode(GrGLSLFPFragmentBuilder* f,
105 const char* outputCoverage) const {
Chris Dalton21ba5512018-03-21 17:20:21 -0600106 f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z;", fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn());
107 f->codeAppend ("float f = k*k*k - l*m;");
Chris Daltonb709f272018-03-21 23:13:45 -0600108 f->codeAppendf("float2 grad = %s * float2(k, 1);", fGradMatrix.fsIn());
109 f->codeAppend ("float fwidth = abs(grad.x) + abs(grad.y);");
110 f->codeAppendf("%s = clamp(0.5 - f/fwidth, 0, 1);", outputCoverage);
Chris Daltonbaf3e782018-03-08 15:55:58 +0000111
Chris Dalton21ba5512018-03-21 17:20:21 -0600112 f->codeAppendf("half d = min(%s.w, 0);", fKLMD.fsIn()); // Flat edge opposite the curve.
Chris Daltonbaf3e782018-03-08 15:55:58 +0000113 // Wind is the sign of both L and/or M. Take the sign of whichever has the larger magnitude.
114 // (In reality, either would be fine because we chop cubics with more than a half pixel of
115 // padding around the L & M lines, so neither should approach zero.)
116 f->codeAppend ("half wind = sign(l + m);");
Chris Dalton21ba5512018-03-21 17:20:21 -0600117 f->codeAppendf("%s = (%s + d) * wind;", outputCoverage, outputCoverage);
Chris Daltonbaf3e782018-03-08 15:55:58 +0000118
Chris Dalton21ba5512018-03-21 17:20:21 -0600119 if (fCornerCoverage.fsIn()) {
120 f->codeAppendf("%s = %s.x * %s.y + %s;", // Attenuated corner coverage.
121 outputCoverage, fCornerCoverage.fsIn(), fCornerCoverage.fsIn(),
122 outputCoverage);
123 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600124}