blob: bccba21d0caae49bd246721ea20a4130940b3f0a [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 "GrCCPRQuadraticShader.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 GrCCPRQuadraticShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
14 const char* repetitionID, const char* wind,
Chris Dalton6a3dbee2017-10-16 10:44:41 -060015 GeometryVars* vars) const {
16 s->declareGlobal(fCanonicalMatrix);
17 s->codeAppendf("%s = float3x3(0.0, 0, 1, "
18 "0.5, 0, 1, "
19 "1.0, 1, 1) * "
20 "inverse(float3x3(%s[0], 1, "
21 "%s[1], 1, "
22 "%s[2], 1));",
23 fCanonicalMatrix.c_str(), pts, pts, pts);
24
Chris Dalton6a3dbee2017-10-16 10:44:41 -060025 s->declareGlobal(fEdgeDistanceEquation);
26 s->codeAppendf("float2 edgept0 = %s[%s > 0 ? 2 : 0];", pts, wind);
27 s->codeAppendf("float2 edgept1 = %s[%s > 0 ? 0 : 2];", pts, wind);
28 Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str());
29
Chris Dalton1fbdb612017-12-12 12:48:47 -070030 this->onEmitSetupCode(s, pts, repetitionID, vars);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060031}
32
33GrCCPRQuadraticShader::WindHandling
34GrCCPRQuadraticShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code,
35 const char* position, const char* /*coverage*/,
36 const char* /*wind*/) {
Chris Daltonfdde34e2017-10-16 14:15:26 -060037 varyingHandler->addVarying("xyd", &fXYD);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060038 code->appendf("%s.xy = (%s * float3(%s, 1)).xy;",
39 fXYD.gsOut(), fCanonicalMatrix.c_str(), position);
40 code->appendf("%s.z = dot(%s.xy, %s) + %s.z;",
41 fXYD.gsOut(), fEdgeDistanceEquation.c_str(), position,
42 fEdgeDistanceEquation.c_str());
43
44 this->onEmitVaryings(varyingHandler, code);
45 return WindHandling::kNotHandled;
46}
47
Chris Dalton1fbdb612017-12-12 12:48:47 -070048void GrCCPRQuadraticHullShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
49 const char* /*repetitionID*/,
50 GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060051 // Find the T value whose tangent is halfway between the tangents at the endpionts.
52 s->codeAppendf("float2 tan0 = %s[1] - %s[0];", pts, pts);
53 s->codeAppendf("float2 tan1 = %s[2] - %s[1];", pts, pts);
54 s->codeAppend ("float2 midnorm = normalize(tan0) - normalize(tan1);");
55 s->codeAppend ("float2 T = midnorm * float2x2(tan0 - tan1, tan0);");
56 s->codeAppend ("float t = clamp(T.t / T.s, 0, 1);"); // T.s != 0; we cull flat curves on CPU.
57
58 // Clip the bezier triangle by the tangent at our new t value. This is a simple application for
59 // De Casteljau's algorithm.
60 s->codeAppendf("float4x2 quadratic_hull = float4x2(%s[0], "
61 "%s[0] + tan0 * t, "
62 "%s[1] + tan1 * t, "
63 "%s[2]);", pts, pts, pts, pts);
64 vars->fHullVars.fAlternatePoints = "quadratic_hull";
65}
66
67void GrCCPRQuadraticHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
68 SkString* code) {
Chris Daltonfdde34e2017-10-16 14:15:26 -060069 varyingHandler->addVarying("grad", &fGrad);
Chris Daltoncc0ab7e2017-10-24 14:16:52 -060070 code->appendf("%s = float2(2 * %s.x, -1) * float2x2(%s);",
71 fGrad.gsOut(), fXYD.gsOut(), fCanonicalMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060072}
73
74void GrCCPRQuadraticHullShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
75 const char* outputCoverage) const {
76 f->codeAppendf("float d = (%s.x * %s.x - %s.y) * inversesqrt(dot(%s, %s));",
77 fXYD.fsIn(), fXYD.fsIn(), fXYD.fsIn(), fGrad.fsIn(), fGrad.fsIn());
78 f->codeAppendf("%s = clamp(0.5 - d, 0, 1);", outputCoverage);
79 f->codeAppendf("%s += min(%s.z, 0);", outputCoverage, fXYD.fsIn()); // Flat closing edge.
80}
81
Chris Dalton1fbdb612017-12-12 12:48:47 -070082void GrCCPRQuadraticCornerShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
83 const char* repetitionID,
84 GeometryVars* vars) const {
85 s->codeAppendf("float2 corner = %s[%s * 2];", pts, repetitionID);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060086 vars->fCornerVars.fPoint = "corner";
87}
88
89void GrCCPRQuadraticCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
90 SkString* code) {
Chris Daltonfdde34e2017-10-16 14:15:26 -060091 varyingHandler->addFlatVarying("dXYDdx", &fdXYDdx);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060092 code->appendf("%s = float3(%s[0].x, %s[0].y, %s.x);",
Chris Daltoncc0ab7e2017-10-24 14:16:52 -060093 fdXYDdx.gsOut(), fCanonicalMatrix.c_str(), fCanonicalMatrix.c_str(),
94 fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060095
Chris Daltonfdde34e2017-10-16 14:15:26 -060096 varyingHandler->addFlatVarying("dXYDdy", &fdXYDdy);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060097 code->appendf("%s = float3(%s[1].x, %s[1].y, %s.y);",
Chris Daltoncc0ab7e2017-10-24 14:16:52 -060098 fdXYDdy.gsOut(), fCanonicalMatrix.c_str(), fCanonicalMatrix.c_str(),
99 fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600100}
101
102void GrCCPRQuadraticCornerShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
103 const char* outputCoverage) const {
104 f->codeAppendf("float x = %s.x, y = %s.y, d = %s.z;",
105 fXYD.fsIn(), fXYD.fsIn(), fXYD.fsIn());
106 f->codeAppendf("float2x3 grad_xyd = float2x3(%s, %s);", fdXYDdx.fsIn(), fdXYDdy.fsIn());
107
108 // Erase what the previous hull shader wrote. We don't worry about the two corners falling on
109 // the same pixel because those cases should have been weeded out by this point.
110 f->codeAppend ("float f = x*x - y;");
111 f->codeAppend ("float2 grad_f = float2(2*x, -1) * float2x2(grad_xyd);");
112 f->codeAppendf("%s = -(0.5 - f * inversesqrt(dot(grad_f, grad_f)));", outputCoverage);
113 f->codeAppendf("%s -= d;", outputCoverage);
114
115 // Use software msaa to approximate coverage at the corner pixels.
116 int sampleCount = Shader::DefineSoftSampleLocations(f, "samples");
117 f->codeAppendf("float3 xyd_center = float3(%s.xy, %s.z + 0.5);", fXYD.fsIn(), fXYD.fsIn());
118 f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount);
119 f->codeAppend ( "float3 xyd = grad_xyd * samples[i] + xyd_center;");
120 f->codeAppend ( "half f = xyd.y - xyd.x * xyd.x;"); // f > 0 -> inside curve.
121 f->codeAppendf( "%s += all(greaterThan(float2(f,xyd.z), float2(0))) ? %f : 0;",
122 outputCoverage, 1.0 / sampleCount);
123 f->codeAppendf("}");
124}