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