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 | |
| 8 | #include "GrCCPRQuadraticShader.h" |
| 9 | |
| 10 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame^] | 11 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 12 | |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame^] | 13 | void GrCCPRQuadraticShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts, |
| 14 | const char* repetitionID, const char* wind, |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 15 | GeometryVars* vars) const { |
| 16 | s->declareGlobal(fCanonicalMatrix); |
| 17 | s->codeAppendf("%s = float3x3(0.0, 0, 1, " |
| 18 | "0.5, 0, 1, " |
| 19 | "1.0, 1, 1) * " |
| 20 | "inverse(float3x3(%s[0], 1, " |
| 21 | "%s[1], 1, " |
| 22 | "%s[2], 1));", |
| 23 | fCanonicalMatrix.c_str(), pts, pts, pts); |
| 24 | |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 25 | s->declareGlobal(fEdgeDistanceEquation); |
| 26 | s->codeAppendf("float2 edgept0 = %s[%s > 0 ? 2 : 0];", pts, wind); |
| 27 | s->codeAppendf("float2 edgept1 = %s[%s > 0 ? 0 : 2];", pts, wind); |
| 28 | Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str()); |
| 29 | |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame^] | 30 | this->onEmitSetupCode(s, pts, repetitionID, vars); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | GrCCPRQuadraticShader::WindHandling |
| 34 | GrCCPRQuadraticShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code, |
| 35 | const char* position, const char* /*coverage*/, |
| 36 | const char* /*wind*/) { |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 37 | varyingHandler->addVarying("xyd", &fXYD); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 38 | code->appendf("%s.xy = (%s * float3(%s, 1)).xy;", |
| 39 | fXYD.gsOut(), fCanonicalMatrix.c_str(), position); |
| 40 | code->appendf("%s.z = dot(%s.xy, %s) + %s.z;", |
| 41 | fXYD.gsOut(), fEdgeDistanceEquation.c_str(), position, |
| 42 | fEdgeDistanceEquation.c_str()); |
| 43 | |
| 44 | this->onEmitVaryings(varyingHandler, code); |
| 45 | return WindHandling::kNotHandled; |
| 46 | } |
| 47 | |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame^] | 48 | void GrCCPRQuadraticHullShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts, |
| 49 | const char* /*repetitionID*/, |
| 50 | GeometryVars* vars) const { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 51 | // Find the T value whose tangent is halfway between the tangents at the endpionts. |
| 52 | s->codeAppendf("float2 tan0 = %s[1] - %s[0];", pts, pts); |
| 53 | s->codeAppendf("float2 tan1 = %s[2] - %s[1];", pts, pts); |
| 54 | s->codeAppend ("float2 midnorm = normalize(tan0) - normalize(tan1);"); |
| 55 | s->codeAppend ("float2 T = midnorm * float2x2(tan0 - tan1, tan0);"); |
| 56 | s->codeAppend ("float t = clamp(T.t / T.s, 0, 1);"); // T.s != 0; we cull flat curves on CPU. |
| 57 | |
| 58 | // Clip the bezier triangle by the tangent at our new t value. This is a simple application for |
| 59 | // De Casteljau's algorithm. |
| 60 | s->codeAppendf("float4x2 quadratic_hull = float4x2(%s[0], " |
| 61 | "%s[0] + tan0 * t, " |
| 62 | "%s[1] + tan1 * t, " |
| 63 | "%s[2]);", pts, pts, pts, pts); |
| 64 | vars->fHullVars.fAlternatePoints = "quadratic_hull"; |
| 65 | } |
| 66 | |
| 67 | void GrCCPRQuadraticHullShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, |
| 68 | SkString* code) { |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 69 | varyingHandler->addVarying("grad", &fGrad); |
Chris Dalton | cc0ab7e | 2017-10-24 14:16:52 -0600 | [diff] [blame] | 70 | code->appendf("%s = float2(2 * %s.x, -1) * float2x2(%s);", |
| 71 | fGrad.gsOut(), fXYD.gsOut(), fCanonicalMatrix.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void GrCCPRQuadraticHullShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f, |
| 75 | const char* outputCoverage) const { |
| 76 | f->codeAppendf("float d = (%s.x * %s.x - %s.y) * inversesqrt(dot(%s, %s));", |
| 77 | fXYD.fsIn(), fXYD.fsIn(), fXYD.fsIn(), fGrad.fsIn(), fGrad.fsIn()); |
| 78 | f->codeAppendf("%s = clamp(0.5 - d, 0, 1);", outputCoverage); |
| 79 | f->codeAppendf("%s += min(%s.z, 0);", outputCoverage, fXYD.fsIn()); // Flat closing edge. |
| 80 | } |
| 81 | |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame^] | 82 | void GrCCPRQuadraticCornerShader::onEmitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts, |
| 83 | const char* repetitionID, |
| 84 | GeometryVars* vars) const { |
| 85 | s->codeAppendf("float2 corner = %s[%s * 2];", pts, repetitionID); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 86 | vars->fCornerVars.fPoint = "corner"; |
| 87 | } |
| 88 | |
| 89 | void GrCCPRQuadraticCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, |
| 90 | SkString* code) { |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 91 | varyingHandler->addFlatVarying("dXYDdx", &fdXYDdx); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 92 | code->appendf("%s = float3(%s[0].x, %s[0].y, %s.x);", |
Chris Dalton | cc0ab7e | 2017-10-24 14:16:52 -0600 | [diff] [blame] | 93 | fdXYDdx.gsOut(), fCanonicalMatrix.c_str(), fCanonicalMatrix.c_str(), |
| 94 | fEdgeDistanceEquation.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 95 | |
Chris Dalton | fdde34e | 2017-10-16 14:15:26 -0600 | [diff] [blame] | 96 | varyingHandler->addFlatVarying("dXYDdy", &fdXYDdy); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 97 | code->appendf("%s = float3(%s[1].x, %s[1].y, %s.y);", |
Chris Dalton | cc0ab7e | 2017-10-24 14:16:52 -0600 | [diff] [blame] | 98 | fdXYDdy.gsOut(), fCanonicalMatrix.c_str(), fCanonicalMatrix.c_str(), |
| 99 | fEdgeDistanceEquation.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void GrCCPRQuadraticCornerShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f, |
| 103 | const char* outputCoverage) const { |
| 104 | f->codeAppendf("float x = %s.x, y = %s.y, d = %s.z;", |
| 105 | fXYD.fsIn(), fXYD.fsIn(), fXYD.fsIn()); |
| 106 | f->codeAppendf("float2x3 grad_xyd = float2x3(%s, %s);", fdXYDdx.fsIn(), fdXYDdy.fsIn()); |
| 107 | |
| 108 | // Erase what the previous hull shader wrote. We don't worry about the two corners falling on |
| 109 | // the same pixel because those cases should have been weeded out by this point. |
| 110 | f->codeAppend ("float f = x*x - y;"); |
| 111 | f->codeAppend ("float2 grad_f = float2(2*x, -1) * float2x2(grad_xyd);"); |
| 112 | f->codeAppendf("%s = -(0.5 - f * inversesqrt(dot(grad_f, grad_f)));", outputCoverage); |
| 113 | f->codeAppendf("%s -= d;", outputCoverage); |
| 114 | |
| 115 | // Use software msaa to approximate coverage at the corner pixels. |
| 116 | int sampleCount = Shader::DefineSoftSampleLocations(f, "samples"); |
| 117 | f->codeAppendf("float3 xyd_center = float3(%s.xy, %s.z + 0.5);", fXYD.fsIn(), fXYD.fsIn()); |
| 118 | f->codeAppendf("for (int i = 0; i < %i; ++i) {", sampleCount); |
| 119 | f->codeAppend ( "float3 xyd = grad_xyd * samples[i] + xyd_center;"); |
| 120 | f->codeAppend ( "half f = xyd.y - xyd.x * xyd.x;"); // f > 0 -> inside curve. |
| 121 | f->codeAppendf( "%s += all(greaterThan(float2(f,xyd.z), float2(0))) ? %f : 0;", |
| 122 | outputCoverage, 1.0 / sampleCount); |
| 123 | f->codeAppendf("}"); |
| 124 | } |