blob: 295de0ca6c007d3250ff8d02d77f0c4f4a6df428 [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 Dalton383a2ef2018-01-08 17:21:41 -050015void GrCCCubicShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
16 const char* repetitionID, const char* wind,
17 GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060018 // 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);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060031 s->codeAppend ("float discr = 3*D2*D2 - 4*D1*D3;");
Chris Daltonbe4ffab2017-12-08 10:59:58 -070032 s->codeAppend ("float x = discr >= 0 ? 3 : 1;");
33 s->codeAppend ("float q = sqrt(x * abs(discr));");
34 s->codeAppend ("q = x*D2 + (D2 >= 0 ? q : -q);");
35
36 s->codeAppend ("float2 l, m;");
37 s->codeAppend ("l.ts = normalize(float2(q, 2*x * D1));");
38 s->codeAppend ("m.ts = normalize(float2(2, q) * (discr >= 0 ? float2(D3, 1) "
39 ": float2(D2*D2 - D3*D1, D1)));");
40
41 s->codeAppend ("float4 K;");
42 s->codeAppend ("float4 lm = l.sstt * m.stst;");
43 s->codeAppend ("K = float4(0, lm.x, -lm.y - lm.z, lm.w);");
44
45 s->codeAppend ("float4 L, M;");
46 s->codeAppend ("lm.yz += 2*lm.zy;");
47 s->codeAppend ("L = float4(-1,x,-x,1) * l.sstt * (discr >= 0 ? l.ssst * l.sttt : lm);");
48 s->codeAppend ("M = float4(-1,x,-x,1) * m.sstt * (discr >= 0 ? m.ssst * m.sttt : lm.xzyw);");
49
Chris Dalton6a3dbee2017-10-16 10:44:41 -060050 s->codeAppend ("short middlerow = abs(D2) > abs(D1) ? 2 : 1;");
51 s->codeAppend ("float3x3 CI = inverse(float3x3(C[0][0], C[0][middlerow], C[0][3], "
52 "C[1][0], C[1][middlerow], C[1][3], "
53 " 0, 0, 1));");
54 s->codeAppendf("%s = CI * float3x3(K[0], K[middlerow], K[3], "
55 "L[0], L[middlerow], L[3], "
56 "M[0], M[middlerow], M[3]);", fKLMMatrix.c_str());
57
Chris Dalton1fbdb612017-12-12 12:48:47 -070058 // Evaluate the cubic at T=.5 for a mid-ish point.
59 s->codeAppendf("float2 midpoint = %s * float4(.125, .375, .375, .125);", pts);
60
Chris Dalton6a3dbee2017-10-16 10:44:41 -060061 // 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 Dalton1fbdb612017-12-12 12:48:47 -070075 this->onEmitSetupCode(s, pts, repetitionID, vars);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060076}
77
Chris Dalton383a2ef2018-01-08 17:21:41 -050078Shader::WindHandling GrCCCubicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
79 GrGLSLVarying::Scope scope,
80 SkString* code, const char* position,
81 const char* coverage, const char* /*wind*/) {
Chris Daltonde5a8142017-12-18 10:05:15 -070082 SkASSERT(!coverage);
83
Chris Dalton90e8fb12017-12-22 02:24:53 -070084 fKLMD.reset(kFloat4_GrSLType, scope);
Chris Daltonfdde34e2017-10-16 14:15:26 -060085 varyingHandler->addVarying("klmd", &fKLMD);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060086 code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str());
87 code->appendf("float d = dot(float3(%s, 1), %s);", position, fEdgeDistanceEquation.c_str());
Chris Dalton90e8fb12017-12-22 02:24:53 -070088 code->appendf("%s = float4(klm, d);", OutName(fKLMD));
Chris Dalton6a3dbee2017-10-16 10:44:41 -060089
Chris Dalton90e8fb12017-12-22 02:24:53 -070090 this->onEmitVaryings(varyingHandler, scope, code);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060091 return WindHandling::kNotHandled;
92}
93
Chris Dalton383a2ef2018-01-08 17:21:41 -050094void GrCCCubicHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
95 GrGLSLVarying::Scope scope, SkString* code) {
Chris Dalton90e8fb12017-12-22 02:24:53 -070096 fGradMatrix.reset(kFloat2x2_GrSLType, scope);
Chris Daltond23c7c42017-12-22 19:05:15 +000097 varyingHandler->addVarying("grad_matrix", &fGradMatrix);
Chris Dalton90e8fb12017-12-22 02:24:53 -070098 // "klm" was just defined by the base class.
99 code->appendf("%s[0] = 3 * klm[0] * %s[0].xy;", OutName(fGradMatrix), fKLMMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600100 code->appendf("%s[1] = -klm[1] * %s[2].xy - klm[2] * %s[1].xy;",
Chris Dalton90e8fb12017-12-22 02:24:53 -0700101 OutName(fGradMatrix), fKLMMatrix.c_str(), fKLMMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600102}
103
Robert Phillips7f861922018-01-30 13:13:42 +0000104void GrCCCubicHullShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
Chris Dalton383a2ef2018-01-08 17:21:41 -0500105 const char* outputCoverage) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600106 f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;",
107 fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn());
108 f->codeAppend ("float f = k*k*k - l*m;");
109 f->codeAppendf("float2 grad_f = %s * float2(k, 1);", fGradMatrix.fsIn());
110 f->codeAppendf("%s = clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);", outputCoverage);
111 f->codeAppendf("%s += min(d, 0);", outputCoverage); // Flat closing edge.
112}
113
Chris Dalton383a2ef2018-01-08 17:21:41 -0500114void GrCCCubicCornerShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
115 const char* repetitionID, GeometryVars* vars) const {
Chris Dalton1fbdb612017-12-12 12:48:47 -0700116 s->codeAppendf("float2 corner = %s[%s * 3];", pts, repetitionID);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600117 vars->fCornerVars.fPoint = "corner";
118}
119
Chris Dalton383a2ef2018-01-08 17:21:41 -0500120void GrCCCubicCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
121 GrGLSLVarying::Scope scope, SkString* code) {
Chris Dalton90e8fb12017-12-22 02:24:53 -0700122 fdKLMDdx.reset(kFloat4_GrSLType, scope);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600123 varyingHandler->addFlatVarying("dklmddx", &fdKLMDdx);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600124 code->appendf("%s = float4(%s[0].x, %s[1].x, %s[2].x, %s.x);",
Chris Dalton90e8fb12017-12-22 02:24:53 -0700125 OutName(fdKLMDdx), fKLMMatrix.c_str(), fKLMMatrix.c_str(),
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600126 fKLMMatrix.c_str(), fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600127
Chris Dalton90e8fb12017-12-22 02:24:53 -0700128 fdKLMDdy.reset(kFloat4_GrSLType, scope);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600129 varyingHandler->addFlatVarying("dklmddy", &fdKLMDdy);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600130 code->appendf("%s = float4(%s[0].y, %s[1].y, %s[2].y, %s.y);",
Chris Dalton90e8fb12017-12-22 02:24:53 -0700131 OutName(fdKLMDdy), fKLMMatrix.c_str(), fKLMMatrix.c_str(),
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600132 fKLMMatrix.c_str(), fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600133}
134
Robert Phillips7f861922018-01-30 13:13:42 +0000135void GrCCCubicCornerShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
Chris Dalton383a2ef2018-01-08 17:21:41 -0500136 const char* outputCoverage) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600137 f->codeAppendf("float2x4 grad_klmd = float2x4(%s, %s);", fdKLMDdx.fsIn(), fdKLMDdy.fsIn());
138
139 // Erase what the previous hull shader wrote. We don't worry about the two corners falling on
140 // the same pixel because those cases should have been weeded out by this point.
141 f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;",
142 fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn());
143 f->codeAppend ("float f = k*k*k - l*m;");
144 f->codeAppend ("float2 grad_f = float3(3*k*k, -m, -l) * float2x3(grad_klmd);");
145 f->codeAppendf("%s = -clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);",
146 outputCoverage);
147 f->codeAppendf("%s -= d;", outputCoverage);
148
149 // Use software msaa to estimate actual coverage at the corner pixels.
150 const int sampleCount = Shader::DefineSoftSampleLocations(f, "samples");
151 f->codeAppendf("float4 klmd_center = float4(%s.xyz, %s.w + 0.5);",
152 fKLMD.fsIn(), fKLMD.fsIn());
153 f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount);
154 f->codeAppend ( "float4 klmd = grad_klmd * samples[i] + klmd_center;");
155 f->codeAppend ( "half f = klmd.y * klmd.z - klmd.x * klmd.x * klmd.x;");
156 f->codeAppendf( "%s += all(greaterThan(half4(f, klmd.y, klmd.z, klmd.w), "
157 "half4(0))) ? %f : 0;",
158 outputCoverage, 1.0 / sampleCount);
159 f->codeAppend ("}");
160}