blob: 4c9485eba0dfc1a174ff10063fbb4336580943c0 [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 Dalton7c7ff032018-03-28 20:09:58 -060011#include "glsl/GrGLSLProgramBuilder.h"
Chris Dalton1fbdb612017-12-12 12:48:47 -070012#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton6a3dbee2017-10-16 10:44:41 -060013
Chris Dalton383a2ef2018-01-08 17:21:41 -050014using Shader = GrCCCoverageProcessor::Shader;
Chris Daltonde5a8142017-12-18 10:05:15 -070015
Chris Daltonfe462ef2018-03-08 15:54:01 +000016void GrCCCubicShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
Chris Dalton21ba5512018-03-21 17:20:21 -060017 const char* wind, const char** /*tighterHull*/) const {
Chris Dalton7c7ff032018-03-28 20:09:58 -060018 // Define a function that normalizes the homogeneous coordinates T=t/s in order to avoid
19 // exponent overflow.
20 SkString normalizeHomogCoordFn;
21 GrShaderVar coord("coord", kFloat2_GrSLType);
22 s->emitFunction(kFloat2_GrSLType, "normalize_homogeneous_coord", 1, &coord,
23 s->getProgramBuilder()->shaderCaps()->fpManipulationSupport()
24 // Exponent manipulation version: Scale the exponents so the larger
25 // component has a magnitude in 1..2.
26 // (Neither component should be infinity because ccpr crops big paths.)
27 ? "int exp;"
28 "frexp(max(abs(coord.t), abs(coord.s)), exp);"
29 "return coord * ldexp(1, 1 - exp);"
30
31 // Division version: Divide by the component with the larger magnitude.
32 // (Both should not be 0 because ccpr catches degenerate cubics.)
33 : "bool swap = abs(coord.t) > abs(coord.s);"
34 "coord = swap ? coord.ts : coord;"
35 "coord = float2(1, coord.t/coord.s);"
36 "return swap ? coord.ts : coord;",
37
38 &normalizeHomogCoordFn);
39
Chris Dalton6a3dbee2017-10-16 10:44:41 -060040 // Find the cubic's power basis coefficients.
41 s->codeAppendf("float2x4 C = float4x4(-1, 3, -3, 1, "
42 " 3, -6, 3, 0, "
43 "-3, 3, 0, 0, "
44 " 1, 0, 0, 0) * transpose(%s);", pts);
45
46 // Find the cubic's inflection function.
47 s->codeAppend ("float D3 = +determinant(float2x2(C[0].yz, C[1].yz));");
48 s->codeAppend ("float D2 = -determinant(float2x2(C[0].xz, C[1].xz));");
49 s->codeAppend ("float D1 = +determinant(float2x2(C));");
50
51 // Calculate the KLM matrix.
52 s->declareGlobal(fKLMMatrix);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060053 s->codeAppend ("float discr = 3*D2*D2 - 4*D1*D3;");
Chris Daltonbe4ffab2017-12-08 10:59:58 -070054 s->codeAppend ("float x = discr >= 0 ? 3 : 1;");
55 s->codeAppend ("float q = sqrt(x * abs(discr));");
56 s->codeAppend ("q = x*D2 + (D2 >= 0 ? q : -q);");
57
58 s->codeAppend ("float2 l, m;");
Chris Dalton7c7ff032018-03-28 20:09:58 -060059 s->codeAppendf("l.ts = %s(float2(q, 2*x * D1));", normalizeHomogCoordFn.c_str());
60 s->codeAppendf("m.ts = %s(float2(2, q) * (discr >= 0 ? float2(D3, 1) "
61 ": float2(D2*D2 - D3*D1, D1)));",
62 normalizeHomogCoordFn.c_str());
Chris Daltonbe4ffab2017-12-08 10:59:58 -070063
64 s->codeAppend ("float4 K;");
65 s->codeAppend ("float4 lm = l.sstt * m.stst;");
66 s->codeAppend ("K = float4(0, lm.x, -lm.y - lm.z, lm.w);");
67
68 s->codeAppend ("float4 L, M;");
69 s->codeAppend ("lm.yz += 2*lm.zy;");
70 s->codeAppend ("L = float4(-1,x,-x,1) * l.sstt * (discr >= 0 ? l.ssst * l.sttt : lm);");
71 s->codeAppend ("M = float4(-1,x,-x,1) * m.sstt * (discr >= 0 ? m.ssst * m.sttt : lm.xzyw);");
72
Chris Dalton7c7ff032018-03-28 20:09:58 -060073 s->codeAppend ("int middlerow = abs(D2) > abs(D1) ? 2 : 1;");
Chris Dalton6a3dbee2017-10-16 10:44:41 -060074 s->codeAppend ("float3x3 CI = inverse(float3x3(C[0][0], C[0][middlerow], C[0][3], "
75 "C[1][0], C[1][middlerow], C[1][3], "
76 " 0, 0, 1));");
77 s->codeAppendf("%s = CI * float3x3(K[0], K[middlerow], K[3], "
78 "L[0], L[middlerow], L[3], "
79 "M[0], M[middlerow], M[3]);", fKLMMatrix.c_str());
80
Chris Dalton1fbdb612017-12-12 12:48:47 -070081 // Evaluate the cubic at T=.5 for a mid-ish point.
82 s->codeAppendf("float2 midpoint = %s * float4(.125, .375, .375, .125);", pts);
83
Chris Dalton21ba5512018-03-21 17:20:21 -060084 // 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 -060085 s->codeAppendf("float2 orientation = sign(float3(midpoint, 1) * float2x3(%s[1], %s[2]));",
86 fKLMMatrix.c_str(), fKLMMatrix.c_str());
87 s->codeAppendf("%s *= float3x3(orientation[0] * orientation[1], 0, 0, "
Chris Dalton21ba5512018-03-21 17:20:21 -060088 "0, orientation[0], 0, "
89 "0, 0, orientation[1]);", fKLMMatrix.c_str());
Chris Daltonbaf3e782018-03-08 15:55:58 +000090
91 // Determine the amount of additional coverage to subtract out for the flat edge (P3 -> P0).
92 s->declareGlobal(fEdgeDistanceEquation);
Chris Dalton7c7ff032018-03-28 20:09:58 -060093 s->codeAppendf("int edgeidx0 = %s > 0 ? 3 : 0;", wind);
Chris Daltonbaf3e782018-03-08 15:55:58 +000094 s->codeAppendf("float2 edgept0 = %s[edgeidx0];", pts);
95 s->codeAppendf("float2 edgept1 = %s[3 - edgeidx0];", pts);
96 Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060097}
98
Chris Daltonfe462ef2018-03-08 15:54:01 +000099void GrCCCubicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
100 GrGLSLVarying::Scope scope, SkString* code,
Chris Dalton04a1de52018-03-14 02:04:09 -0600101 const char* position, const char* coverage,
Chris Dalton4c239342018-04-05 18:43:40 -0600102 const char* cornerCoverage) {
103 fKLM_fEdge.reset(kFloat4_GrSLType, scope);
104 varyingHandler->addVarying("klm_and_edge", &fKLM_fEdge);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600105 code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str());
Chris Dalton21ba5512018-03-21 17:20:21 -0600106 // We give L & M both the same sign as wind, in order to pass this value to the fragment shader.
107 // (Cubics are pre-chopped such that L & M do not change sign within any individual segment.)
108 code->appendf("%s.xyz = klm * float3(1, %s, %s);",
Chris Dalton4c239342018-04-05 18:43:40 -0600109 OutName(fKLM_fEdge), coverage, coverage); // coverage == wind on curves.
Chris Dalton21ba5512018-03-21 17:20:21 -0600110 code->appendf("%s.w = dot(float3(%s, 1), %s);", // Flat edge opposite the curve.
Chris Dalton4c239342018-04-05 18:43:40 -0600111 OutName(fKLM_fEdge), position, fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600112
Chris Dalton21ba5512018-03-21 17:20:21 -0600113 fGradMatrix.reset(kFloat2x2_GrSLType, scope);
114 varyingHandler->addVarying("grad_matrix", &fGradMatrix);
115 code->appendf("%s[0] = 2*bloat * 3 * klm[0] * %s[0].xy;",
116 OutName(fGradMatrix), fKLMMatrix.c_str());
117 code->appendf("%s[1] = -2*bloat * (klm[1] * %s[2].xy + klm[2] * %s[1].xy);",
118 OutName(fGradMatrix), fKLMMatrix.c_str(), fKLMMatrix.c_str());
119
Chris Dalton4c239342018-04-05 18:43:40 -0600120 if (cornerCoverage) {
121 code->appendf("half hull_coverage; {");
122 this->calcHullCoverage(code, OutName(fKLM_fEdge), OutName(fGradMatrix), "hull_coverage");
123 code->appendf("}");
Chris Dalton21ba5512018-03-21 17:20:21 -0600124 fCornerCoverage.reset(kHalf2_GrSLType, scope);
125 varyingHandler->addVarying("corner_coverage", &fCornerCoverage);
Chris Dalton4c239342018-04-05 18:43:40 -0600126 code->appendf("%s = half2(hull_coverage, 1) * %s;",
127 OutName(fCornerCoverage), cornerCoverage);
Chris Dalton21ba5512018-03-21 17:20:21 -0600128 }
Chris Daltonbaf3e782018-03-08 15:55:58 +0000129}
Chris Daltonf510e262018-01-30 16:42:37 -0700130
Chris Daltonbaf3e782018-03-08 15:55:58 +0000131void GrCCCubicShader::onEmitFragmentCode(GrGLSLFPFragmentBuilder* f,
132 const char* outputCoverage) const {
Chris Dalton4c239342018-04-05 18:43:40 -0600133 this->calcHullCoverage(&AccessCodeString(f), fKLM_fEdge.fsIn(), fGradMatrix.fsIn(),
134 outputCoverage);
Chris Daltonbaf3e782018-03-08 15:55:58 +0000135
Chris Daltonbaf3e782018-03-08 15:55:58 +0000136 // Wind is the sign of both L and/or M. Take the sign of whichever has the larger magnitude.
137 // (In reality, either would be fine because we chop cubics with more than a half pixel of
138 // padding around the L & M lines, so neither should approach zero.)
139 f->codeAppend ("half wind = sign(l + m);");
Chris Dalton4c239342018-04-05 18:43:40 -0600140 f->codeAppendf("%s *= wind;", outputCoverage);
Chris Daltonbaf3e782018-03-08 15:55:58 +0000141
Chris Dalton21ba5512018-03-21 17:20:21 -0600142 if (fCornerCoverage.fsIn()) {
143 f->codeAppendf("%s = %s.x * %s.y + %s;", // Attenuated corner coverage.
144 outputCoverage, fCornerCoverage.fsIn(), fCornerCoverage.fsIn(),
145 outputCoverage);
146 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600147}
Chris Dalton4c239342018-04-05 18:43:40 -0600148
149void GrCCCubicShader::calcHullCoverage(SkString* code, const char* klmAndEdge,
150 const char* gradMatrix, const char* outputCoverage) const {
151 code->appendf("float k = %s.x, l = %s.y, m = %s.z;", klmAndEdge, klmAndEdge, klmAndEdge);
152 code->append ("float f = k*k*k - l*m;");
153 code->appendf("float2 grad = %s * float2(k, 1);", gradMatrix);
154 code->append ("float fwidth = abs(grad.x) + abs(grad.y);");
155 code->appendf("%s = min(0.5 - f/fwidth, 1);", outputCoverage); // Curve coverage.
156 code->appendf("half d = min(%s.w, 0);", klmAndEdge); // Flat edge opposite the curve.
157 code->appendf("%s = max(%s + d, 0);", outputCoverage, outputCoverage); // Total hull coverage.
158}