blob: 002fcfd3409b5ec25bcd07b0062f0041b38404c3 [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 Dalton383a2ef2018-01-08 17:21:41 -050016void GrCCQuadraticShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
17 const char* repetitionID, const char* wind,
18 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 Dalton6a3dbee2017-10-16 10:44:41 -060028 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
Chris Dalton1fbdb612017-12-12 12:48:47 -070033 this->onEmitSetupCode(s, pts, repetitionID, vars);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060034}
35
Chris Daltonf510e262018-01-30 16:42:37 -070036void GrCCQuadraticShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
37 GrGLSLVarying::Scope scope, SkString* code,
38 const char* position, const char* inputCoverage,
39 const char* wind) {
40 SkASSERT(!inputCoverage);
Chris Daltonde5a8142017-12-18 10:05:15 -070041
Chris Daltonf510e262018-01-30 16:42:37 -070042 fXYDW.reset(kFloat4_GrSLType, scope);
43 varyingHandler->addVarying("xydw", &fXYDW);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060044 code->appendf("%s.xy = (%s * float3(%s, 1)).xy;",
Chris Daltonf510e262018-01-30 16:42:37 -070045 OutName(fXYDW), fCanonicalMatrix.c_str(), position);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060046 code->appendf("%s.z = dot(%s.xy, %s) + %s.z;",
Chris Daltonf510e262018-01-30 16:42:37 -070047 OutName(fXYDW), fEdgeDistanceEquation.c_str(), position,
Chris Dalton6a3dbee2017-10-16 10:44:41 -060048 fEdgeDistanceEquation.c_str());
Chris Daltonf510e262018-01-30 16:42:37 -070049 code->appendf("%s.w = %s;", OutName(fXYDW), wind);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060050
Chris Dalton90e8fb12017-12-22 02:24:53 -070051 this->onEmitVaryings(varyingHandler, scope, code);
Chris Daltonf510e262018-01-30 16:42:37 -070052}
53
54void GrCCQuadraticShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
55 const char* outputCoverage) const {
56 this->emitCoverage(f, outputCoverage);
57 f->codeAppendf("%s *= %s.w;", outputCoverage, fXYDW.fsIn()); // Sign by wind.
Chris Dalton6a3dbee2017-10-16 10:44:41 -060058}
59
Chris Dalton383a2ef2018-01-08 17:21:41 -050060void GrCCQuadraticHullShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
61 const char* /*repetitionID*/,
62 GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060063 // Find the T value whose tangent is halfway between the tangents at the endpionts.
64 s->codeAppendf("float2 tan0 = %s[1] - %s[0];", pts, pts);
65 s->codeAppendf("float2 tan1 = %s[2] - %s[1];", pts, pts);
66 s->codeAppend ("float2 midnorm = normalize(tan0) - normalize(tan1);");
67 s->codeAppend ("float2 T = midnorm * float2x2(tan0 - tan1, tan0);");
68 s->codeAppend ("float t = clamp(T.t / T.s, 0, 1);"); // T.s != 0; we cull flat curves on CPU.
69
70 // Clip the bezier triangle by the tangent at our new t value. This is a simple application for
71 // De Casteljau's algorithm.
72 s->codeAppendf("float4x2 quadratic_hull = float4x2(%s[0], "
73 "%s[0] + tan0 * t, "
74 "%s[1] + tan1 * t, "
75 "%s[2]);", pts, pts, pts, pts);
76 vars->fHullVars.fAlternatePoints = "quadratic_hull";
77}
78
Chris Dalton383a2ef2018-01-08 17:21:41 -050079void GrCCQuadraticHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
80 GrGLSLVarying::Scope scope, SkString* code) {
Chris Dalton90e8fb12017-12-22 02:24:53 -070081 fGrad.reset(kFloat2_GrSLType, scope);
Chris Daltonfdde34e2017-10-16 14:15:26 -060082 varyingHandler->addVarying("grad", &fGrad);
Chris Daltoncc0ab7e2017-10-24 14:16:52 -060083 code->appendf("%s = float2(2 * %s.x, -1) * float2x2(%s);",
Chris Daltonf510e262018-01-30 16:42:37 -070084 OutName(fGrad), OutName(fXYDW), fCanonicalMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060085}
86
Chris Daltonf510e262018-01-30 16:42:37 -070087void GrCCQuadraticHullShader::emitCoverage(GrGLSLPPFragmentBuilder* f,
88 const char* outputCoverage) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060089 f->codeAppendf("float d = (%s.x * %s.x - %s.y) * inversesqrt(dot(%s, %s));",
Chris Daltonf510e262018-01-30 16:42:37 -070090 fXYDW.fsIn(), fXYDW.fsIn(), fXYDW.fsIn(), fGrad.fsIn(), fGrad.fsIn());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060091 f->codeAppendf("%s = clamp(0.5 - d, 0, 1);", outputCoverage);
Chris Daltonf510e262018-01-30 16:42:37 -070092 f->codeAppendf("%s += min(%s.z, 0);", outputCoverage, fXYDW.fsIn()); // Flat closing edge.
Chris Dalton6a3dbee2017-10-16 10:44:41 -060093}
94
Chris Dalton383a2ef2018-01-08 17:21:41 -050095void GrCCQuadraticCornerShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
96 const char* repetitionID,
97 GeometryVars* vars) const {
Chris Dalton1fbdb612017-12-12 12:48:47 -070098 s->codeAppendf("float2 corner = %s[%s * 2];", pts, repetitionID);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060099 vars->fCornerVars.fPoint = "corner";
100}
101
Chris Dalton383a2ef2018-01-08 17:21:41 -0500102void GrCCQuadraticCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
Chris Daltonf510e262018-01-30 16:42:37 -0700103 GrGLSLVarying::Scope scope, SkString* code) {
Chris Dalton90e8fb12017-12-22 02:24:53 -0700104 fdXYDdx.reset(kFloat3_GrSLType, scope);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600105 varyingHandler->addFlatVarying("dXYDdx", &fdXYDdx);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600106 code->appendf("%s = float3(%s[0].x, %s[0].y, %s.x);",
Chris Dalton90e8fb12017-12-22 02:24:53 -0700107 OutName(fdXYDdx), fCanonicalMatrix.c_str(), fCanonicalMatrix.c_str(),
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600108 fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600109
Chris Dalton90e8fb12017-12-22 02:24:53 -0700110 fdXYDdy.reset(kFloat3_GrSLType, scope);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600111 varyingHandler->addFlatVarying("dXYDdy", &fdXYDdy);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600112 code->appendf("%s = float3(%s[1].x, %s[1].y, %s.y);",
Chris Dalton90e8fb12017-12-22 02:24:53 -0700113 OutName(fdXYDdy), fCanonicalMatrix.c_str(), fCanonicalMatrix.c_str(),
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600114 fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600115}
116
Chris Daltonf510e262018-01-30 16:42:37 -0700117void GrCCQuadraticCornerShader::emitCoverage(GrGLSLPPFragmentBuilder* f,
118 const char* outputCoverage) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600119 f->codeAppendf("float x = %s.x, y = %s.y, d = %s.z;",
Chris Daltonf510e262018-01-30 16:42:37 -0700120 fXYDW.fsIn(), fXYDW.fsIn(), fXYDW.fsIn());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600121 f->codeAppendf("float2x3 grad_xyd = float2x3(%s, %s);", fdXYDdx.fsIn(), fdXYDdy.fsIn());
122
123 // Erase what the previous hull shader wrote. We don't worry about the two corners falling on
124 // the same pixel because those cases should have been weeded out by this point.
125 f->codeAppend ("float f = x*x - y;");
126 f->codeAppend ("float2 grad_f = float2(2*x, -1) * float2x2(grad_xyd);");
127 f->codeAppendf("%s = -(0.5 - f * inversesqrt(dot(grad_f, grad_f)));", outputCoverage);
128 f->codeAppendf("%s -= d;", outputCoverage);
129
130 // Use software msaa to approximate coverage at the corner pixels.
131 int sampleCount = Shader::DefineSoftSampleLocations(f, "samples");
Chris Daltonf510e262018-01-30 16:42:37 -0700132 f->codeAppendf("float3 xyd_center = float3(%s.xy, %s.z + 0.5);", fXYDW.fsIn(), fXYDW.fsIn());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600133 f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount);
134 f->codeAppend ( "float3 xyd = grad_xyd * samples[i] + xyd_center;");
135 f->codeAppend ( "half f = xyd.y - xyd.x * xyd.x;"); // f > 0 -> inside curve.
136 f->codeAppendf( "%s += all(greaterThan(float2(f,xyd.z), float2(0))) ? %f : 0;",
137 outputCoverage, 1.0 / sampleCount);
138 f->codeAppendf("}");
139}