Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCCConicShader.h" |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 11 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 12 | |
Chris Dalton | 84d36cd | 2019-04-17 14:47:17 -0600 | [diff] [blame] | 13 | void GrCCConicShader::emitSetupCode( |
| 14 | GrGLSLVertexGeoBuilder* s, const char* pts, const char** outHull4) const { |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 15 | // K is distance from the line P2 -> P0. L is distance from the line P0 -> P1, scaled by 2w. |
| 16 | // M is distance from the line P1 -> P2, scaled by 2w. We do this in a space where P1=0. |
| 17 | s->declareGlobal(fKLMMatrix); |
| 18 | s->codeAppendf("float x0 = %s[0].x - %s[1].x, x2 = %s[2].x - %s[1].x;", pts, pts, pts, pts); |
| 19 | s->codeAppendf("float y0 = %s[0].y - %s[1].y, y2 = %s[2].y - %s[1].y;", pts, pts, pts, pts); |
| 20 | s->codeAppendf("float w = %s[3].x;", pts); |
| 21 | s->codeAppendf("%s = float3x3(y2 - y0, x0 - x2, x2*y0 - x0*y2, " |
| 22 | "2*w * float2(+y0, -x0), 0, " |
| 23 | "2*w * float2(-y2, +x2), 0);", fKLMMatrix.c_str()); |
| 24 | |
| 25 | s->declareGlobal(fControlPoint); |
| 26 | s->codeAppendf("%s = %s[1];", fControlPoint.c_str(), pts); |
| 27 | |
Chris Dalton | 84d36cd | 2019-04-17 14:47:17 -0600 | [diff] [blame] | 28 | // Scale KLM by the inverse Manhattan width of K, and make sure K is positive. This allows K to |
| 29 | // double as the flat opposite edge AA. kwidth will not be 0 because we cull degenerate conics |
| 30 | // on the CPU. |
| 31 | s->codeAppendf("float kwidth = 2*bloat * (abs(%s[0].x) + abs(%s[0].y)) * sign(%s[0].z);", |
| 32 | fKLMMatrix.c_str(), fKLMMatrix.c_str(), fKLMMatrix.c_str()); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 33 | s->codeAppendf("%s *= 1/kwidth;", fKLMMatrix.c_str()); |
| 34 | |
| 35 | if (outHull4) { |
| 36 | // Clip the conic triangle by the tangent line at maximum height. Conics have the nice |
| 37 | // property that maximum height always occurs at T=.5. This is a simple application for |
| 38 | // De Casteljau's algorithm. |
| 39 | s->codeAppendf("float2 p1w = %s[1]*w;", pts); |
| 40 | s->codeAppend ("float r = 1 / (1 + w);"); |
Chris Dalton | 5cd6700 | 2018-04-30 11:04:40 -0600 | [diff] [blame] | 41 | s->codeAppend ("float2 conic_hull[4];"); |
| 42 | s->codeAppendf("conic_hull[0] = %s[0];", pts); |
| 43 | s->codeAppendf("conic_hull[1] = (%s[0] + p1w) * r;", pts); |
| 44 | s->codeAppendf("conic_hull[2] = (p1w + %s[2]) * r;", pts); |
| 45 | s->codeAppendf("conic_hull[3] = %s[2];", pts); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 46 | *outHull4 = "conic_hull"; |
| 47 | } |
| 48 | } |
| 49 | |
Chris Dalton | 84d36cd | 2019-04-17 14:47:17 -0600 | [diff] [blame] | 50 | void GrCCConicShader::onEmitVaryings( |
| 51 | GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code, |
| 52 | const char* position, const char* coverage, const char* cornerCoverage, const char* wind) { |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 53 | code->appendf("float3 klm = float3(%s - %s, 1) * %s;", |
| 54 | position, fControlPoint.c_str(), fKLMMatrix.c_str()); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 55 | if (coverage) { |
| 56 | fKLM_fWind.reset(kFloat4_GrSLType, scope); |
| 57 | varyingHandler->addVarying("klm_and_wind", &fKLM_fWind); |
| 58 | code->appendf("%s.w = %s;", OutName(fKLM_fWind), wind); |
| 59 | } else { |
| 60 | fKLM_fWind.reset(kFloat3_GrSLType, scope); |
| 61 | varyingHandler->addVarying("klm", &fKLM_fWind); |
| 62 | } |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 63 | code->appendf("%s.xyz = klm;", OutName(fKLM_fWind)); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 64 | |
| 65 | fGrad_fCorner.reset(cornerCoverage ? kFloat4_GrSLType : kFloat2_GrSLType, scope); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 66 | varyingHandler->addVarying((cornerCoverage) ? "grad_and_corner" : "grad", &fGrad_fCorner); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 67 | code->appendf("%s.xy = 2*bloat * (float3x2(%s) * float3(2*klm[0], -klm[2], -klm[1]));", |
| 68 | OutName(fGrad_fCorner), fKLMMatrix.c_str()); |
| 69 | |
| 70 | if (cornerCoverage) { |
Chris Dalton | 84d36cd | 2019-04-17 14:47:17 -0600 | [diff] [blame] | 71 | SkASSERT(coverage); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 72 | code->appendf("half hull_coverage;"); |
| 73 | this->calcHullCoverage(code, "klm", OutName(fGrad_fCorner), "hull_coverage"); |
| 74 | code->appendf("%s.zw = half2(hull_coverage, 1) * %s;", |
| 75 | OutName(fGrad_fCorner), cornerCoverage); |
| 76 | } |
| 77 | } |
| 78 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 79 | void GrCCConicShader::emitFragmentCoverageCode( |
| 80 | GrGLSLFPFragmentBuilder* f, const char* outputCoverage) const { |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 81 | this->calcHullCoverage(&AccessCodeString(f), fKLM_fWind.fsIn(), fGrad_fCorner.fsIn(), |
| 82 | outputCoverage); |
Chris Dalton | 84d36cd | 2019-04-17 14:47:17 -0600 | [diff] [blame] | 83 | f->codeAppendf("%s *= half(%s.w);", outputCoverage, fKLM_fWind.fsIn()); // Wind. |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 84 | |
| 85 | if (kFloat4_GrSLType == fGrad_fCorner.type()) { |
Chris Dalton | 84d36cd | 2019-04-17 14:47:17 -0600 | [diff] [blame] | 86 | f->codeAppendf("%s = fma(half(%s.z), half(%s.w), %s);", // Attenuated corner coverage. |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 87 | outputCoverage, fGrad_fCorner.fsIn(), fGrad_fCorner.fsIn(), |
| 88 | outputCoverage); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void GrCCConicShader::calcHullCoverage(SkString* code, const char* klm, const char* grad, |
| 93 | const char* outputCoverage) const { |
| 94 | code->appendf("float k = %s.x, l = %s.y, m = %s.z;", klm, klm, klm); |
| 95 | code->append ("float f = k*k - l*m;"); |
| 96 | code->appendf("float fwidth = abs(%s.x) + abs(%s.y);", grad, grad); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 97 | code->appendf("float curve_coverage = min(0.5 - f/fwidth, 1);"); |
| 98 | // K doubles as the flat opposite edge's AA. |
| 99 | code->append ("float edge_coverage = min(k - 0.5, 0);"); |
| 100 | // Total hull coverage. |
| 101 | code->appendf("%s = max(half(curve_coverage + edge_coverage), 0);", outputCoverage); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 102 | } |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 103 | |
| 104 | void GrCCConicShader::emitSampleMaskCode(GrGLSLFPFragmentBuilder* f) const { |
| 105 | f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z;", |
| 106 | fKLM_fWind.fsIn(), fKLM_fWind.fsIn(), fKLM_fWind.fsIn()); |
| 107 | f->codeAppendf("float f = k*k - l*m;"); |
| 108 | f->codeAppendf("float2 grad = %s;", fGrad_fCorner.fsIn()); |
| 109 | f->applyFnToMultisampleMask("f", "grad", GrGLSLFPFragmentBuilder::ScopeFlags::kTopLevel); |
| 110 | } |