blob: 5bee85ef3f2aedb160cf82f5b7eb09882abc5db1 [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 Dalton52076d12018-03-21 12:14:10 -060019 s->declareGlobal(fQCoordMatrix);
20 s->codeAppendf("%s = float2x2(1, 1, .5, 0) * inverse(float2x2(%s[2] - %s[0], %s[1] - %s[0]));",
21 fQCoordMatrix.c_str(), pts, pts, pts, pts);
22
23 s->declareGlobal(fQCoord0);
24 s->codeAppendf("%s = %s[0];", fQCoord0.c_str(), pts);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060025
Chris Daltonbaf3e782018-03-08 15:55:58 +000026 s->declareGlobal(fEdgeDistanceEquation);
27 s->codeAppendf("float2 edgept0 = %s[%s > 0 ? 2 : 0];", pts, wind);
28 s->codeAppendf("float2 edgept1 = %s[%s > 0 ? 0 : 2];", pts, wind);
29 Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str());
30
31 this->onEmitSetupCode(s, pts, repetitionID, vars);
32}
33
34void GrCCQuadraticShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
35 GrGLSLVarying::Scope scope, SkString* code,
Chris Dalton04a1de52018-03-14 02:04:09 -060036 const char* position, const char* coverage,
37 const char* attenuatedCoverage, const char* wind) {
38 SkASSERT(!coverage);
39 SkASSERT(!attenuatedCoverage);
Chris Daltonbaf3e782018-03-08 15:55:58 +000040
41 fXYDW.reset(kFloat4_GrSLType, scope);
42 varyingHandler->addVarying("xydw", &fXYDW);
Chris Dalton52076d12018-03-21 12:14:10 -060043 code->appendf("%s.xy = %s * (%s - %s);",
44 OutName(fXYDW), fQCoordMatrix.c_str(), position, fQCoord0.c_str());
Chris Daltonbaf3e782018-03-08 15:55:58 +000045 code->appendf("%s.z = dot(%s.xy, %s) + %s.z;",
46 OutName(fXYDW), fEdgeDistanceEquation.c_str(), position,
47 fEdgeDistanceEquation.c_str());
48 code->appendf("%s.w = %s;", OutName(fXYDW), wind);
49
50 this->onEmitVaryings(varyingHandler, scope, code);
51}
52
53void GrCCQuadraticShader::onEmitFragmentCode(GrGLSLFPFragmentBuilder* f,
54 const char* outputCoverage) const {
55 this->emitCoverage(f, outputCoverage);
56 f->codeAppendf("%s *= %s.w;", outputCoverage, fXYDW.fsIn()); // Sign by wind.
57}
58
59void GrCCQuadraticHullShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
60 const char* /*repetitionID*/,
61 GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060062 // Find the T value whose tangent is halfway between the tangents at the endpionts.
63 s->codeAppendf("float2 tan0 = %s[1] - %s[0];", pts, pts);
64 s->codeAppendf("float2 tan1 = %s[2] - %s[1];", pts, pts);
65 s->codeAppend ("float2 midnorm = normalize(tan0) - normalize(tan1);");
66 s->codeAppend ("float2 T = midnorm * float2x2(tan0 - tan1, tan0);");
67 s->codeAppend ("float t = clamp(T.t / T.s, 0, 1);"); // T.s != 0; we cull flat curves on CPU.
68
69 // Clip the bezier triangle by the tangent at our new t value. This is a simple application for
70 // De Casteljau's algorithm.
71 s->codeAppendf("float4x2 quadratic_hull = float4x2(%s[0], "
72 "%s[0] + tan0 * t, "
73 "%s[1] + tan1 * t, "
74 "%s[2]);", pts, pts, pts, pts);
Chris Daltonfe462ef2018-03-08 15:54:01 +000075 vars->fHullVars.fAlternatePoints = "quadratic_hull";
Chris Dalton6a3dbee2017-10-16 10:44:41 -060076}
77
Chris Daltonbaf3e782018-03-08 15:55:58 +000078void GrCCQuadraticHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
79 GrGLSLVarying::Scope scope, SkString* code) {
80 fGrad.reset(kFloat2_GrSLType, scope);
81 varyingHandler->addVarying("grad", &fGrad);
Chris Dalton52076d12018-03-21 12:14:10 -060082 code->appendf("%s = 2*bloat * float2(2 * %s.x, -1) * %s;",
83 OutName(fGrad), OutName(fXYDW), fQCoordMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060084}
85
Chris Daltonbaf3e782018-03-08 15:55:58 +000086void GrCCQuadraticHullShader::emitCoverage(GrGLSLFPFragmentBuilder* f,
87 const char* outputCoverage) const {
88 f->codeAppendf("float d = (%s.x * %s.x - %s.y) * inversesqrt(dot(%s, %s));",
89 fXYDW.fsIn(), fXYDW.fsIn(), fXYDW.fsIn(), fGrad.fsIn(), fGrad.fsIn());
90 f->codeAppendf("%s = clamp(0.5 - d, 0, 1);", outputCoverage);
91 f->codeAppendf("%s += min(%s.z, 0);", outputCoverage, fXYDW.fsIn()); // Flat closing edge.
92}
93
94void GrCCQuadraticCornerShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
95 const char* repetitionID,
96 GeometryVars* vars) const {
97 s->codeAppendf("float2 corner = %s[%s * 2];", pts, repetitionID);
98 vars->fCornerVars.fPoint = "corner";
99}
100
101void GrCCQuadraticCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
102 GrGLSLVarying::Scope scope, SkString* code) {
103 using Interpolation = GrGLSLVaryingHandler::Interpolation;
104
105 fdXYDdx.reset(kFloat3_GrSLType, scope);
106 varyingHandler->addVarying("dXYDdx", &fdXYDdx, Interpolation::kCanBeFlat);
Chris Dalton52076d12018-03-21 12:14:10 -0600107 code->appendf("%s = 2*bloat * float3(%s[0].x, %s[0].y, %s.x);",
108 OutName(fdXYDdx), fQCoordMatrix.c_str(), fQCoordMatrix.c_str(),
Chris Daltonbaf3e782018-03-08 15:55:58 +0000109 fEdgeDistanceEquation.c_str());
110
111 fdXYDdy.reset(kFloat3_GrSLType, scope);
112 varyingHandler->addVarying("dXYDdy", &fdXYDdy, Interpolation::kCanBeFlat);
Chris Dalton52076d12018-03-21 12:14:10 -0600113 code->appendf("%s = 2*bloat * float3(%s[1].x, %s[1].y, %s.y);",
114 OutName(fdXYDdy), fQCoordMatrix.c_str(), fQCoordMatrix.c_str(),
Chris Daltonbaf3e782018-03-08 15:55:58 +0000115 fEdgeDistanceEquation.c_str());
116}
117
118void GrCCQuadraticCornerShader::emitCoverage(GrGLSLFPFragmentBuilder* f,
Chris Daltonf510e262018-01-30 16:42:37 -0700119 const char* outputCoverage) const {
Chris Daltonbaf3e782018-03-08 15:55:58 +0000120 f->codeAppendf("float x = %s.x, y = %s.y, d = %s.z;",
121 fXYDW.fsIn(), fXYDW.fsIn(), fXYDW.fsIn());
122 f->codeAppendf("float2x3 grad_xyd = float2x3(%s, %s);", fdXYDdx.fsIn(), fdXYDdy.fsIn());
123
124 // Erase what the previous hull shader wrote. We don't worry about the two corners falling on
125 // the same pixel because those cases should have been weeded out by this point.
126 f->codeAppend ("float f = x*x - y;");
127 f->codeAppend ("float2 grad_f = float2(2*x, -1) * float2x2(grad_xyd);");
128 f->codeAppendf("%s = -(0.5 - f * inversesqrt(dot(grad_f, grad_f)));", outputCoverage);
129 f->codeAppendf("%s -= d;", outputCoverage);
130
131 // Use software msaa to approximate coverage at the corner pixels.
132 int sampleCount = Shader::DefineSoftSampleLocations(f, "samples");
133 f->codeAppendf("float3 xyd_center = float3(%s.xy, %s.z + 0.5);", fXYDW.fsIn(), fXYDW.fsIn());
134 f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount);
135 f->codeAppend ( "float3 xyd = grad_xyd * samples[i] + xyd_center;");
136 f->codeAppend ( "half f = xyd.y - xyd.x * xyd.x;"); // f > 0 -> inside curve.
137 f->codeAppendf( "%s += all(greaterThan(float2(f,xyd.z), float2(0))) ? %f : 0;",
138 outputCoverage, 1.0 / sampleCount);
139 f->codeAppendf("}");
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600140}