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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCCCubicShader.h" |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -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/GrGLSLProgramBuilder.h" |
| 12 | #include "src/gpu/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 | 84d36cd | 2019-04-17 14:47:17 -0600 | [diff] [blame] | 16 | void GrCCCubicShader::emitSetupCode( |
| 17 | GrGLSLVertexGeoBuilder* s, const char* pts, const char** /*outHull4*/) const { |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 18 | // Find the cubic's power basis coefficients. |
| 19 | s->codeAppendf("float2x4 C = float4x4(-1, 3, -3, 1, " |
| 20 | " 3, -6, 3, 0, " |
| 21 | "-3, 3, 0, 0, " |
| 22 | " 1, 0, 0, 0) * transpose(%s);", pts); |
| 23 | |
| 24 | // Find the cubic's inflection function. |
| 25 | s->codeAppend ("float D3 = +determinant(float2x2(C[0].yz, C[1].yz));"); |
| 26 | s->codeAppend ("float D2 = -determinant(float2x2(C[0].xz, C[1].xz));"); |
| 27 | s->codeAppend ("float D1 = +determinant(float2x2(C));"); |
| 28 | |
Chris Dalton | 6fdbf61 | 2018-04-15 21:58:19 -0600 | [diff] [blame] | 29 | // Shift the exponents in D so the largest magnitude falls somewhere in 1..2. This protects us |
| 30 | // from overflow while solving for roots and KLM functionals. |
| 31 | s->codeAppend ("float Dmax = max(max(abs(D1), abs(D2)), abs(D3));"); |
| 32 | s->codeAppend ("float norm;"); |
| 33 | if (s->getProgramBuilder()->shaderCaps()->fpManipulationSupport()) { |
| 34 | s->codeAppend ("int exp;"); |
| 35 | s->codeAppend ("frexp(Dmax, exp);"); |
| 36 | s->codeAppend ("norm = ldexp(1, 1 - exp);"); |
| 37 | } else { |
| 38 | s->codeAppend ("norm = 1/Dmax;"); // Dmax will not be 0 because we cull line cubics on CPU. |
| 39 | } |
| 40 | s->codeAppend ("D3 *= norm;"); |
| 41 | s->codeAppend ("D2 *= norm;"); |
| 42 | s->codeAppend ("D1 *= norm;"); |
| 43 | |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 44 | // Calculate the KLM matrix. |
| 45 | s->declareGlobal(fKLMMatrix); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 46 | s->codeAppend ("float discr = 3*D2*D2 - 4*D1*D3;"); |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 47 | s->codeAppend ("float x = discr >= 0 ? 3 : 1;"); |
| 48 | s->codeAppend ("float q = sqrt(x * abs(discr));"); |
| 49 | s->codeAppend ("q = x*D2 + (D2 >= 0 ? q : -q);"); |
| 50 | |
| 51 | s->codeAppend ("float2 l, m;"); |
Chris Dalton | 6fdbf61 | 2018-04-15 21:58:19 -0600 | [diff] [blame] | 52 | s->codeAppend ("l.ts = float2(q, 2*x * D1);"); |
| 53 | s->codeAppend ("m.ts = float2(2, q) * (discr >= 0 ? float2(D3, 1) " |
| 54 | ": float2(D2*D2 - D3*D1, D1));"); |
Chris Dalton | be4ffab | 2017-12-08 10:59:58 -0700 | [diff] [blame] | 55 | |
| 56 | s->codeAppend ("float4 K;"); |
| 57 | s->codeAppend ("float4 lm = l.sstt * m.stst;"); |
| 58 | s->codeAppend ("K = float4(0, lm.x, -lm.y - lm.z, lm.w);"); |
| 59 | |
| 60 | s->codeAppend ("float4 L, M;"); |
| 61 | s->codeAppend ("lm.yz += 2*lm.zy;"); |
| 62 | s->codeAppend ("L = float4(-1,x,-x,1) * l.sstt * (discr >= 0 ? l.ssst * l.sttt : lm);"); |
| 63 | s->codeAppend ("M = float4(-1,x,-x,1) * m.sstt * (discr >= 0 ? m.ssst * m.sttt : lm.xzyw);"); |
| 64 | |
Chris Dalton | 7c7ff03 | 2018-03-28 20:09:58 -0600 | [diff] [blame] | 65 | s->codeAppend ("int middlerow = abs(D2) > abs(D1) ? 2 : 1;"); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 66 | s->codeAppend ("float3x3 CI = inverse(float3x3(C[0][0], C[0][middlerow], C[0][3], " |
| 67 | "C[1][0], C[1][middlerow], C[1][3], " |
| 68 | " 0, 0, 1));"); |
| 69 | s->codeAppendf("%s = CI * float3x3(K[0], K[middlerow], K[3], " |
| 70 | "L[0], L[middlerow], L[3], " |
| 71 | "M[0], M[middlerow], M[3]);", fKLMMatrix.c_str()); |
| 72 | |
Chris Dalton | 1fbdb61 | 2017-12-12 12:48:47 -0700 | [diff] [blame] | 73 | // Evaluate the cubic at T=.5 for a mid-ish point. |
| 74 | s->codeAppendf("float2 midpoint = %s * float4(.125, .375, .375, .125);", pts); |
| 75 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 76 | // Orient the KLM matrix so L & M are both positive on the side of the curve we wish to fill. |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 77 | s->codeAppendf("float2 orientation = sign(float3(midpoint, 1) * float2x3(%s[1], %s[2]));", |
| 78 | fKLMMatrix.c_str(), fKLMMatrix.c_str()); |
| 79 | s->codeAppendf("%s *= float3x3(orientation[0] * orientation[1], 0, 0, " |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 80 | "0, orientation[0], 0, " |
| 81 | "0, 0, orientation[1]);", fKLMMatrix.c_str()); |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 82 | } |
| 83 | |
Chris Dalton | 84d36cd | 2019-04-17 14:47:17 -0600 | [diff] [blame] | 84 | void GrCCCubicShader::onEmitVaryings( |
| 85 | GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code, |
| 86 | const char* position, const char* coverage, const char* cornerCoverage, const char* wind) { |
| 87 | code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str()); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 88 | if (coverage) { |
| 89 | fKLM_fEdge.reset(kFloat4_GrSLType, scope); |
| 90 | varyingHandler->addVarying("klm_and_edge", &fKLM_fEdge); |
| 91 | // Give L&M both the same sign as wind, in order to pass this value to the fragment shader. |
| 92 | // (Cubics are pre-chopped such that L&M do not change sign within any individual segment.) |
| 93 | code->appendf("%s.xyz = klm * float3(1, %s, %s);", OutName(fKLM_fEdge), wind, wind); |
| 94 | // Flat edge opposite the curve. |
| 95 | code->appendf("%s.w = %s;", OutName(fKLM_fEdge), coverage); |
| 96 | } else { |
| 97 | fKLM_fEdge.reset(kFloat3_GrSLType, scope); |
| 98 | varyingHandler->addVarying("klm", &fKLM_fEdge); |
| 99 | code->appendf("%s = klm;", OutName(fKLM_fEdge)); |
| 100 | } |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 101 | |
Chris Dalton | ba37e5b | 2018-04-26 09:19:52 -0600 | [diff] [blame] | 102 | fGradMatrix.reset(kFloat4_GrSLType, scope); |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 103 | varyingHandler->addVarying("grad_matrix", &fGradMatrix); |
Chris Dalton | ba37e5b | 2018-04-26 09:19:52 -0600 | [diff] [blame] | 104 | code->appendf("%s.xy = 2*bloat * 3 * klm[0] * %s[0].xy;", |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 105 | OutName(fGradMatrix), fKLMMatrix.c_str()); |
Chris Dalton | ba37e5b | 2018-04-26 09:19:52 -0600 | [diff] [blame] | 106 | code->appendf("%s.zw = -2*bloat * (klm[1] * %s[2].xy + klm[2] * %s[1].xy);", |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 107 | OutName(fGradMatrix), fKLMMatrix.c_str(), fKLMMatrix.c_str()); |
| 108 | |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 109 | if (cornerCoverage) { |
Chris Dalton | 84d36cd | 2019-04-17 14:47:17 -0600 | [diff] [blame] | 110 | SkASSERT(coverage); |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 111 | code->appendf("half hull_coverage; {"); |
| 112 | this->calcHullCoverage(code, OutName(fKLM_fEdge), OutName(fGradMatrix), "hull_coverage"); |
| 113 | code->appendf("}"); |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 114 | fCornerCoverage.reset(kHalf2_GrSLType, scope); |
| 115 | varyingHandler->addVarying("corner_coverage", &fCornerCoverage); |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 116 | code->appendf("%s = half2(hull_coverage, 1) * %s;", |
| 117 | OutName(fCornerCoverage), cornerCoverage); |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 118 | } |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 119 | } |
Chris Dalton | f510e26 | 2018-01-30 16:42:37 -0700 | [diff] [blame] | 120 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 121 | void GrCCCubicShader::emitFragmentCoverageCode( |
Chris Dalton | 84d36cd | 2019-04-17 14:47:17 -0600 | [diff] [blame] | 122 | GrGLSLFPFragmentBuilder* f, const char* outputCoverage) const { |
| 123 | this->calcHullCoverage( |
| 124 | &AccessCodeString(f), fKLM_fEdge.fsIn(), fGradMatrix.fsIn(), outputCoverage); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 125 | |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 126 | // Wind is the sign of both L and/or M. Take the sign of whichever has the larger magnitude. |
| 127 | // (In reality, either would be fine because we chop cubics with more than a half pixel of |
| 128 | // padding around the L & M lines, so neither should approach zero.) |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 129 | f->codeAppend ("half wind = sign(half(l + m));"); |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 130 | f->codeAppendf("%s *= wind;", outputCoverage); |
Chris Dalton | baf3e78 | 2018-03-08 15:55:58 +0000 | [diff] [blame] | 131 | |
Chris Dalton | 21ba551 | 2018-03-21 17:20:21 -0600 | [diff] [blame] | 132 | if (fCornerCoverage.fsIn()) { |
| 133 | f->codeAppendf("%s = %s.x * %s.y + %s;", // Attenuated corner coverage. |
| 134 | outputCoverage, fCornerCoverage.fsIn(), fCornerCoverage.fsIn(), |
| 135 | outputCoverage); |
| 136 | } |
Chris Dalton | 6a3dbee | 2017-10-16 10:44:41 -0600 | [diff] [blame] | 137 | } |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 138 | |
| 139 | void GrCCCubicShader::calcHullCoverage(SkString* code, const char* klmAndEdge, |
| 140 | const char* gradMatrix, const char* outputCoverage) const { |
| 141 | code->appendf("float k = %s.x, l = %s.y, m = %s.z;", klmAndEdge, klmAndEdge, klmAndEdge); |
| 142 | code->append ("float f = k*k*k - l*m;"); |
Chris Dalton | ba37e5b | 2018-04-26 09:19:52 -0600 | [diff] [blame] | 143 | code->appendf("float2 grad = %s.xy * k + %s.zw;", gradMatrix, gradMatrix); |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 144 | code->append ("float fwidth = abs(grad.x) + abs(grad.y);"); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 145 | code->appendf("float curve_coverage = min(0.5 - f/fwidth, 1);"); |
| 146 | // Flat edge opposite the curve. |
| 147 | code->appendf("float edge_coverage = min(%s.w, 0);", klmAndEdge); |
| 148 | // Total hull coverage. |
| 149 | code->appendf("%s = max(half(curve_coverage + edge_coverage), 0);", outputCoverage); |
Chris Dalton | 4c23934 | 2018-04-05 18:43:40 -0600 | [diff] [blame] | 150 | } |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 151 | |
| 152 | void GrCCCubicShader::emitSampleMaskCode(GrGLSLFPFragmentBuilder* f) const { |
| 153 | f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z;", |
| 154 | fKLM_fEdge.fsIn(), fKLM_fEdge.fsIn(), fKLM_fEdge.fsIn()); |
| 155 | f->codeAppendf("float f = k*k*k - l*m;"); |
| 156 | f->codeAppendf("float2x2 grad_matrix = float2x2(%s);", fGradMatrix.fsIn()); |
| 157 | f->codeAppendf("float2 grad = grad_matrix * float2(k, 1);"); |
| 158 | f->applyFnToMultisampleMask("f", "grad", GrGLSLFPFragmentBuilder::ScopeFlags::kTopLevel); |
| 159 | } |