blob: c9e546a74b675e465e9598a5077112271fc371a3 [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 Daltonde5a8142017-12-18 10:05:15 -070013using Shader = GrCCPRCoverageProcessor::Shader;
14
Chris Dalton1fbdb612017-12-12 12:48:47 -070015void GrCCPRCubicShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
16 const char* repetitionID, const char* wind,
Chris Daltonc17bf322017-10-24 10:59:03 -060017 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 Daltonde5a8142017-12-18 10:05:15 -070078Shader::WindHandling GrCCPRCubicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
79 SkString* code, const char* position,
80 const char* coverage, const char* /*wind*/) {
81 SkASSERT(!coverage);
82
Chris Daltonfdde34e2017-10-16 14:15:26 -060083 varyingHandler->addVarying("klmd", &fKLMD);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060084 code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str());
85 code->appendf("float d = dot(float3(%s, 1), %s);", position, fEdgeDistanceEquation.c_str());
86 code->appendf("%s = float4(klm, d);", fKLMD.gsOut());
87
88 this->onEmitVaryings(varyingHandler, code);
89 return WindHandling::kNotHandled;
90}
91
Chris Dalton6a3dbee2017-10-16 10:44:41 -060092void GrCCPRCubicHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code) {
93 // "klm" was just defined by the base class.
Chris Daltonfdde34e2017-10-16 14:15:26 -060094 varyingHandler->addVarying("grad_matrix", &fGradMatrix);
Chris Daltoncc0ab7e2017-10-24 14:16:52 -060095 code->appendf("%s[0] = 3 * klm[0] * %s[0].xy;", fGradMatrix.gsOut(), fKLMMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060096 code->appendf("%s[1] = -klm[1] * %s[2].xy - klm[2] * %s[1].xy;",
Chris Daltoncc0ab7e2017-10-24 14:16:52 -060097 fGradMatrix.gsOut(), fKLMMatrix.c_str(), fKLMMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060098}
99
100void GrCCPRCubicHullShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
101 const char* outputCoverage) const {
102 f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;",
103 fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn());
104 f->codeAppend ("float f = k*k*k - l*m;");
105 f->codeAppendf("float2 grad_f = %s * float2(k, 1);", fGradMatrix.fsIn());
106 f->codeAppendf("%s = clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);", outputCoverage);
107 f->codeAppendf("%s += min(d, 0);", outputCoverage); // Flat closing edge.
108}
109
Chris Dalton1fbdb612017-12-12 12:48:47 -0700110void GrCCPRCubicCornerShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
111 const char* repetitionID, GeometryVars* vars) const {
112 s->codeAppendf("float2 corner = %s[%s * 3];", pts, repetitionID);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600113 vars->fCornerVars.fPoint = "corner";
114}
115
116void GrCCPRCubicCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code) {
Chris Daltonfdde34e2017-10-16 14:15:26 -0600117 varyingHandler->addFlatVarying("dklmddx", &fdKLMDdx);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600118 code->appendf("%s = float4(%s[0].x, %s[1].x, %s[2].x, %s.x);",
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600119 fdKLMDdx.gsOut(), fKLMMatrix.c_str(), fKLMMatrix.c_str(),
120 fKLMMatrix.c_str(), fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600121
Chris Daltonfdde34e2017-10-16 14:15:26 -0600122 varyingHandler->addFlatVarying("dklmddy", &fdKLMDdy);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600123 code->appendf("%s = float4(%s[0].y, %s[1].y, %s[2].y, %s.y);",
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600124 fdKLMDdy.gsOut(), fKLMMatrix.c_str(), fKLMMatrix.c_str(),
125 fKLMMatrix.c_str(), fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600126}
127
128void GrCCPRCubicCornerShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
129 const char* outputCoverage) const {
130 f->codeAppendf("float2x4 grad_klmd = float2x4(%s, %s);", fdKLMDdx.fsIn(), fdKLMDdy.fsIn());
131
132 // Erase what the previous hull shader wrote. We don't worry about the two corners falling on
133 // the same pixel because those cases should have been weeded out by this point.
134 f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z, d = %s.w;",
135 fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn(), fKLMD.fsIn());
136 f->codeAppend ("float f = k*k*k - l*m;");
137 f->codeAppend ("float2 grad_f = float3(3*k*k, -m, -l) * float2x3(grad_klmd);");
138 f->codeAppendf("%s = -clamp(0.5 - f * inversesqrt(dot(grad_f, grad_f)), 0, 1);",
139 outputCoverage);
140 f->codeAppendf("%s -= d;", outputCoverage);
141
142 // Use software msaa to estimate actual coverage at the corner pixels.
143 const int sampleCount = Shader::DefineSoftSampleLocations(f, "samples");
144 f->codeAppendf("float4 klmd_center = float4(%s.xyz, %s.w + 0.5);",
145 fKLMD.fsIn(), fKLMD.fsIn());
146 f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount);
147 f->codeAppend ( "float4 klmd = grad_klmd * samples[i] + klmd_center;");
148 f->codeAppend ( "half f = klmd.y * klmd.z - klmd.x * klmd.x * klmd.x;");
149 f->codeAppendf( "%s += all(greaterThan(half4(f, klmd.y, klmd.z, klmd.w), "
150 "half4(0))) ? %f : 0;",
151 outputCoverage, 1.0 / sampleCount);
152 f->codeAppend ("}");
153}