blob: 16fd174d471e5d4a341b5a661cca4f69aaed95b0 [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 "GrCCQuadraticShader.h"
Chris Dalton6a3dbee2017-10-16 10:44:41 -06009
Chris Dalton90e8fb12017-12-22 02:24:53 -070010#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton6a3dbee2017-10-16 10:44:41 -060011#include "glsl/GrGLSLFragmentShaderBuilder.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 GrCCQuadraticShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
Chris Daltonbaf3e782018-03-08 15:55:58 +000017 const char* repetitionID, const char* wind,
Chris Daltonfe462ef2018-03-08 15:54:01 +000018 GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060019 s->declareGlobal(fCanonicalMatrix);
20 s->codeAppendf("%s = float3x3(0.0, 0, 1, "
21 "0.5, 0, 1, "
22 "1.0, 1, 1) * "
23 "inverse(float3x3(%s[0], 1, "
24 "%s[1], 1, "
25 "%s[2], 1));",
26 fCanonicalMatrix.c_str(), pts, pts, pts);
27
Chris Daltonbaf3e782018-03-08 15:55:58 +000028 s->declareGlobal(fEdgeDistanceEquation);
29 s->codeAppendf("float2 edgept0 = %s[%s > 0 ? 2 : 0];", pts, wind);
30 s->codeAppendf("float2 edgept1 = %s[%s > 0 ? 0 : 2];", pts, wind);
31 Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str());
32
33 this->onEmitSetupCode(s, pts, repetitionID, vars);
34}
35
36void GrCCQuadraticShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
37 GrGLSLVarying::Scope scope, SkString* code,
Chris Dalton04a1de52018-03-14 02:04:09 -060038 const char* position, const char* coverage,
39 const char* attenuatedCoverage, const char* wind) {
40 SkASSERT(!coverage);
41 SkASSERT(!attenuatedCoverage);
Chris Daltonbaf3e782018-03-08 15:55:58 +000042
43 fXYDW.reset(kFloat4_GrSLType, scope);
44 varyingHandler->addVarying("xydw", &fXYDW);
45 code->appendf("%s.xy = (%s * float3(%s, 1)).xy;",
46 OutName(fXYDW), fCanonicalMatrix.c_str(), position);
47 code->appendf("%s.z = dot(%s.xy, %s) + %s.z;",
48 OutName(fXYDW), fEdgeDistanceEquation.c_str(), position,
49 fEdgeDistanceEquation.c_str());
50 code->appendf("%s.w = %s;", OutName(fXYDW), wind);
51
52 this->onEmitVaryings(varyingHandler, scope, code);
53}
54
55void GrCCQuadraticShader::onEmitFragmentCode(GrGLSLFPFragmentBuilder* f,
56 const char* outputCoverage) const {
57 this->emitCoverage(f, outputCoverage);
58 f->codeAppendf("%s *= %s.w;", outputCoverage, fXYDW.fsIn()); // Sign by wind.
59}
60
61void GrCCQuadraticHullShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
62 const char* /*repetitionID*/,
63 GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060064 // Find the T value whose tangent is halfway between the tangents at the endpionts.
65 s->codeAppendf("float2 tan0 = %s[1] - %s[0];", pts, pts);
66 s->codeAppendf("float2 tan1 = %s[2] - %s[1];", pts, pts);
67 s->codeAppend ("float2 midnorm = normalize(tan0) - normalize(tan1);");
68 s->codeAppend ("float2 T = midnorm * float2x2(tan0 - tan1, tan0);");
69 s->codeAppend ("float t = clamp(T.t / T.s, 0, 1);"); // T.s != 0; we cull flat curves on CPU.
70
71 // Clip the bezier triangle by the tangent at our new t value. This is a simple application for
72 // De Casteljau's algorithm.
73 s->codeAppendf("float4x2 quadratic_hull = float4x2(%s[0], "
74 "%s[0] + tan0 * t, "
75 "%s[1] + tan1 * t, "
76 "%s[2]);", pts, pts, pts, pts);
Chris Daltonfe462ef2018-03-08 15:54:01 +000077 vars->fHullVars.fAlternatePoints = "quadratic_hull";
Chris Dalton6a3dbee2017-10-16 10:44:41 -060078}
79
Chris Daltonbaf3e782018-03-08 15:55:58 +000080void GrCCQuadraticHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
81 GrGLSLVarying::Scope scope, SkString* code) {
82 fGrad.reset(kFloat2_GrSLType, scope);
83 varyingHandler->addVarying("grad", &fGrad);
84 code->appendf("%s = float2(2 * %s.x, -1) * float2x2(%s);",
85 OutName(fGrad), OutName(fXYDW), fCanonicalMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060086}
87
Chris Daltonbaf3e782018-03-08 15:55:58 +000088void GrCCQuadraticHullShader::emitCoverage(GrGLSLFPFragmentBuilder* f,
89 const char* outputCoverage) const {
90 f->codeAppendf("float d = (%s.x * %s.x - %s.y) * inversesqrt(dot(%s, %s));",
91 fXYDW.fsIn(), fXYDW.fsIn(), fXYDW.fsIn(), fGrad.fsIn(), fGrad.fsIn());
92 f->codeAppendf("%s = clamp(0.5 - d, 0, 1);", outputCoverage);
93 f->codeAppendf("%s += min(%s.z, 0);", outputCoverage, fXYDW.fsIn()); // Flat closing edge.
94}
95
96void GrCCQuadraticCornerShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
97 const char* repetitionID,
98 GeometryVars* vars) const {
99 s->codeAppendf("float2 corner = %s[%s * 2];", pts, repetitionID);
100 vars->fCornerVars.fPoint = "corner";
101}
102
103void GrCCQuadraticCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
104 GrGLSLVarying::Scope scope, SkString* code) {
105 using Interpolation = GrGLSLVaryingHandler::Interpolation;
106
107 fdXYDdx.reset(kFloat3_GrSLType, scope);
108 varyingHandler->addVarying("dXYDdx", &fdXYDdx, Interpolation::kCanBeFlat);
109 code->appendf("%s = float3(%s[0].x, %s[0].y, %s.x);",
110 OutName(fdXYDdx), fCanonicalMatrix.c_str(), fCanonicalMatrix.c_str(),
111 fEdgeDistanceEquation.c_str());
112
113 fdXYDdy.reset(kFloat3_GrSLType, scope);
114 varyingHandler->addVarying("dXYDdy", &fdXYDdy, Interpolation::kCanBeFlat);
115 code->appendf("%s = float3(%s[1].x, %s[1].y, %s.y);",
116 OutName(fdXYDdy), fCanonicalMatrix.c_str(), fCanonicalMatrix.c_str(),
117 fEdgeDistanceEquation.c_str());
118}
119
120void GrCCQuadraticCornerShader::emitCoverage(GrGLSLFPFragmentBuilder* f,
Chris Daltonf510e262018-01-30 16:42:37 -0700121 const char* outputCoverage) const {
Chris Daltonbaf3e782018-03-08 15:55:58 +0000122 f->codeAppendf("float x = %s.x, y = %s.y, d = %s.z;",
123 fXYDW.fsIn(), fXYDW.fsIn(), fXYDW.fsIn());
124 f->codeAppendf("float2x3 grad_xyd = float2x3(%s, %s);", fdXYDdx.fsIn(), fdXYDdy.fsIn());
125
126 // Erase what the previous hull shader wrote. We don't worry about the two corners falling on
127 // the same pixel because those cases should have been weeded out by this point.
128 f->codeAppend ("float f = x*x - y;");
129 f->codeAppend ("float2 grad_f = float2(2*x, -1) * float2x2(grad_xyd);");
130 f->codeAppendf("%s = -(0.5 - f * inversesqrt(dot(grad_f, grad_f)));", outputCoverage);
131 f->codeAppendf("%s -= d;", outputCoverage);
132
133 // Use software msaa to approximate coverage at the corner pixels.
134 int sampleCount = Shader::DefineSoftSampleLocations(f, "samples");
135 f->codeAppendf("float3 xyd_center = float3(%s.xy, %s.z + 0.5);", fXYDW.fsIn(), fXYDW.fsIn());
136 f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount);
137 f->codeAppend ( "float3 xyd = grad_xyd * samples[i] + xyd_center;");
138 f->codeAppend ( "half f = xyd.y - xyd.x * xyd.x;"); // f > 0 -> inside curve.
139 f->codeAppendf( "%s += all(greaterThan(float2(f,xyd.z), float2(0))) ? %f : 0;",
140 outputCoverage, 1.0 / sampleCount);
141 f->codeAppendf("}");
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600142}