blob: d08026ad192e4048508ee839c2dfa36955232d76 [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 Dalton383a2ef2018-01-08 17:21:41 -050036Shader::WindHandling GrCCQuadraticShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
37 GrGLSLVarying::Scope scope,
38 SkString* code, const char* position,
39 const char* coverage,
40 const char* /*wind*/) {
Chris Daltonde5a8142017-12-18 10:05:15 -070041 SkASSERT(!coverage);
42
Chris Dalton90e8fb12017-12-22 02:24:53 -070043 fXYD.reset(kFloat3_GrSLType, scope);
Chris Daltonfdde34e2017-10-16 14:15:26 -060044 varyingHandler->addVarying("xyd", &fXYD);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060045 code->appendf("%s.xy = (%s * float3(%s, 1)).xy;",
Chris Dalton90e8fb12017-12-22 02:24:53 -070046 OutName(fXYD), fCanonicalMatrix.c_str(), position);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060047 code->appendf("%s.z = dot(%s.xy, %s) + %s.z;",
Chris Dalton90e8fb12017-12-22 02:24:53 -070048 OutName(fXYD), fEdgeDistanceEquation.c_str(), position,
Chris Dalton6a3dbee2017-10-16 10:44:41 -060049 fEdgeDistanceEquation.c_str());
50
Chris Dalton90e8fb12017-12-22 02:24:53 -070051 this->onEmitVaryings(varyingHandler, scope, code);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060052 return WindHandling::kNotHandled;
53}
54
Chris Dalton383a2ef2018-01-08 17:21:41 -050055void GrCCQuadraticHullShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
56 const char* /*repetitionID*/,
57 GeometryVars* vars) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060058 // Find the T value whose tangent is halfway between the tangents at the endpionts.
59 s->codeAppendf("float2 tan0 = %s[1] - %s[0];", pts, pts);
60 s->codeAppendf("float2 tan1 = %s[2] - %s[1];", pts, pts);
61 s->codeAppend ("float2 midnorm = normalize(tan0) - normalize(tan1);");
62 s->codeAppend ("float2 T = midnorm * float2x2(tan0 - tan1, tan0);");
63 s->codeAppend ("float t = clamp(T.t / T.s, 0, 1);"); // T.s != 0; we cull flat curves on CPU.
64
65 // Clip the bezier triangle by the tangent at our new t value. This is a simple application for
66 // De Casteljau's algorithm.
67 s->codeAppendf("float4x2 quadratic_hull = float4x2(%s[0], "
68 "%s[0] + tan0 * t, "
69 "%s[1] + tan1 * t, "
70 "%s[2]);", pts, pts, pts, pts);
71 vars->fHullVars.fAlternatePoints = "quadratic_hull";
72}
73
Chris Dalton383a2ef2018-01-08 17:21:41 -050074void GrCCQuadraticHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
75 GrGLSLVarying::Scope scope, SkString* code) {
Chris Dalton90e8fb12017-12-22 02:24:53 -070076 fGrad.reset(kFloat2_GrSLType, scope);
Chris Daltonfdde34e2017-10-16 14:15:26 -060077 varyingHandler->addVarying("grad", &fGrad);
Chris Daltoncc0ab7e2017-10-24 14:16:52 -060078 code->appendf("%s = float2(2 * %s.x, -1) * float2x2(%s);",
Chris Dalton90e8fb12017-12-22 02:24:53 -070079 OutName(fGrad), OutName(fXYD), fCanonicalMatrix.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -060080}
81
Robert Phillips7f861922018-01-30 13:13:42 +000082void GrCCQuadraticHullShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
Chris Dalton383a2ef2018-01-08 17:21:41 -050083 const char* outputCoverage) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060084 f->codeAppendf("float d = (%s.x * %s.x - %s.y) * inversesqrt(dot(%s, %s));",
85 fXYD.fsIn(), fXYD.fsIn(), fXYD.fsIn(), fGrad.fsIn(), fGrad.fsIn());
86 f->codeAppendf("%s = clamp(0.5 - d, 0, 1);", outputCoverage);
87 f->codeAppendf("%s += min(%s.z, 0);", outputCoverage, fXYD.fsIn()); // Flat closing edge.
88}
89
Chris Dalton383a2ef2018-01-08 17:21:41 -050090void GrCCQuadraticCornerShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
91 const char* repetitionID,
92 GeometryVars* vars) const {
Chris Dalton1fbdb612017-12-12 12:48:47 -070093 s->codeAppendf("float2 corner = %s[%s * 2];", pts, repetitionID);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060094 vars->fCornerVars.fPoint = "corner";
95}
96
Chris Dalton383a2ef2018-01-08 17:21:41 -050097void GrCCQuadraticCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
Chris Dalton90e8fb12017-12-22 02:24:53 -070098 GrGLSLVarying::Scope scope, SkString* code) {
99 fdXYDdx.reset(kFloat3_GrSLType, scope);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600100 varyingHandler->addFlatVarying("dXYDdx", &fdXYDdx);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600101 code->appendf("%s = float3(%s[0].x, %s[0].y, %s.x);",
Chris Dalton90e8fb12017-12-22 02:24:53 -0700102 OutName(fdXYDdx), fCanonicalMatrix.c_str(), fCanonicalMatrix.c_str(),
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600103 fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600104
Chris Dalton90e8fb12017-12-22 02:24:53 -0700105 fdXYDdy.reset(kFloat3_GrSLType, scope);
Chris Daltonfdde34e2017-10-16 14:15:26 -0600106 varyingHandler->addFlatVarying("dXYDdy", &fdXYDdy);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600107 code->appendf("%s = float3(%s[1].x, %s[1].y, %s.y);",
Chris Dalton90e8fb12017-12-22 02:24:53 -0700108 OutName(fdXYDdy), fCanonicalMatrix.c_str(), fCanonicalMatrix.c_str(),
Chris Daltoncc0ab7e2017-10-24 14:16:52 -0600109 fEdgeDistanceEquation.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600110}
111
Robert Phillips7f861922018-01-30 13:13:42 +0000112void GrCCQuadraticCornerShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
Chris Dalton383a2ef2018-01-08 17:21:41 -0500113 const char* outputCoverage) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600114 f->codeAppendf("float x = %s.x, y = %s.y, d = %s.z;",
115 fXYD.fsIn(), fXYD.fsIn(), fXYD.fsIn());
116 f->codeAppendf("float2x3 grad_xyd = float2x3(%s, %s);", fdXYDdx.fsIn(), fdXYDdy.fsIn());
117
118 // Erase what the previous hull shader wrote. We don't worry about the two corners falling on
119 // the same pixel because those cases should have been weeded out by this point.
120 f->codeAppend ("float f = x*x - y;");
121 f->codeAppend ("float2 grad_f = float2(2*x, -1) * float2x2(grad_xyd);");
122 f->codeAppendf("%s = -(0.5 - f * inversesqrt(dot(grad_f, grad_f)));", outputCoverage);
123 f->codeAppendf("%s -= d;", outputCoverage);
124
125 // Use software msaa to approximate coverage at the corner pixels.
126 int sampleCount = Shader::DefineSoftSampleLocations(f, "samples");
127 f->codeAppendf("float3 xyd_center = float3(%s.xy, %s.z + 0.5);", fXYD.fsIn(), fXYD.fsIn());
128 f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount);
129 f->codeAppend ( "float3 xyd = grad_xyd * samples[i] + xyd_center;");
130 f->codeAppend ( "half f = xyd.y - xyd.x * xyd.x;"); // f > 0 -> inside curve.
131 f->codeAppendf( "%s += all(greaterThan(float2(f,xyd.z), float2(0))) ? %f : 0;",
132 outputCoverage, 1.0 / sampleCount);
133 f->codeAppendf("}");
134}