Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 1 | /* |
| 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 Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 8 | #include "GrCCQuadraticShader.h" |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 9 | |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 10 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 11 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 13 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 14 | using Shader = GrCCCoverageProcessor::Shader; |
Chris Dalton | de5a814 | 2017-12-18 10:05:15 -0700 | [diff] [blame] | 15 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 16 | void GrCCQuadraticShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts, |
| 17 | const char* repetitionID, const char* wind, |
| 18 | GeometryVars* vars) const { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 19 | 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 Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 28 | 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 Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 33 | this->onEmitSetupCode(s, pts, repetitionID, vars); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 34 | } |
| 35 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 36 | Shader::WindHandling GrCCQuadraticShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, |
| 37 | GrGLSLVarying::Scope scope, |
| 38 | SkString* code, const char* position, |
| 39 | const char* coverage, |
| 40 | const char* /*wind*/) { |
Chris Dalton | de5a814 | 2017-12-18 10:05:15 -0700 | [diff] [blame] | 41 | SkASSERT(!coverage); |
| 42 | |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 43 | fXYD.reset(kFloat3_GrSLType, scope); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 44 | varyingHandler->addVarying("xyd", &fXYD); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 45 | code->appendf("%s.xy = (%s * float3(%s, 1)).xy;", |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 46 | OutName(fXYD), fCanonicalMatrix.c_str(), position); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 47 | code->appendf("%s.z = dot(%s.xy, %s) + %s.z;", |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 48 | OutName(fXYD), fEdgeDistanceEquation.c_str(), position, |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 49 | fEdgeDistanceEquation.c_str()); |
| 50 | |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 51 | this->onEmitVaryings(varyingHandler, scope, code); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 52 | return WindHandling::kNotHandled; |
| 53 | } |
| 54 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 55 | void GrCCQuadraticHullShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts, |
| 56 | const char* /*repetitionID*/, |
| 57 | GeometryVars* vars) const { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 58 | // 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 Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 74 | void GrCCQuadraticHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, |
| 75 | GrGLSLVarying::Scope scope, SkString* code) { |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 76 | fGrad.reset(kFloat2_GrSLType, scope); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 77 | varyingHandler->addVarying("grad", &fGrad); |
Chris Dalton | cc0ab7e | 2017-10-24 14:16:52 -0600 | [diff] [blame] | 78 | code->appendf("%s = float2(2 * %s.x, -1) * float2x2(%s);", |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 79 | OutName(fGrad), OutName(fXYD), fCanonicalMatrix.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 80 | } |
| 81 | |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 82 | void GrCCQuadraticHullShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f, |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 83 | const char* outputCoverage) const { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 84 | 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 Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 90 | void GrCCQuadraticCornerShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts, |
| 91 | const char* repetitionID, |
| 92 | GeometryVars* vars) const { |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 93 | s->codeAppendf("float2 corner = %s[%s * 2];", pts, repetitionID); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 94 | vars->fCornerVars.fPoint = "corner"; |
| 95 | } |
| 96 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 97 | void GrCCQuadraticCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 98 | GrGLSLVarying::Scope scope, SkString* code) { |
| 99 | fdXYDdx.reset(kFloat3_GrSLType, scope); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 100 | varyingHandler->addFlatVarying("dXYDdx", &fdXYDdx); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 101 | code->appendf("%s = float3(%s[0].x, %s[0].y, %s.x);", |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 102 | OutName(fdXYDdx), fCanonicalMatrix.c_str(), fCanonicalMatrix.c_str(), |
Chris Dalton | cc0ab7e | 2017-10-24 14:16:52 -0600 | [diff] [blame] | 103 | fEdgeDistanceEquation.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 104 | |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 105 | fdXYDdy.reset(kFloat3_GrSLType, scope); |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 106 | varyingHandler->addFlatVarying("dXYDdy", &fdXYDdy); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 107 | code->appendf("%s = float3(%s[1].x, %s[1].y, %s.y);", |
Chris Dalton | 90e8fb1 | 2017-12-22 02:24:53 -0700 | [diff] [blame] | 108 | OutName(fdXYDdy), fCanonicalMatrix.c_str(), fCanonicalMatrix.c_str(), |
Chris Dalton | cc0ab7e | 2017-10-24 14:16:52 -0600 | [diff] [blame] | 109 | fEdgeDistanceEquation.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 110 | } |
| 111 | |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 112 | void GrCCQuadraticCornerShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f, |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 113 | const char* outputCoverage) const { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 114 | 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 | } |