Home | History | Annotate | Download | only in ccpr
      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 "GrCCCubicShader.h"
      9 
     10 #include "glsl/GrGLSLFragmentShaderBuilder.h"
     11 #include "glsl/GrGLSLProgramBuilder.h"
     12 #include "glsl/GrGLSLVertexGeoBuilder.h"
     13 
     14 using Shader = GrCCCoverageProcessor::Shader;
     15 
     16 void GrCCCubicShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
     17                                     const char* wind, const char** /*outHull4*/) const {
     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 
     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 
     44     // Calculate the KLM matrix.
     45     s->declareGlobal(fKLMMatrix);
     46     s->codeAppend ("float discr = 3*D2*D2 - 4*D1*D3;");
     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;");
     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));");
     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 
     65     s->codeAppend ("int middlerow = abs(D2) > abs(D1) ? 2 : 1;");
     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 
     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 
     76     // Orient the KLM matrix so L & M are both positive on the side of the curve we wish to fill.
     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, "
     80                                   "0, orientation[0], 0, "
     81                                   "0, 0, orientation[1]);", fKLMMatrix.c_str());
     82 
     83     // Determine the amount of additional coverage to subtract out for the flat edge (P3 -> P0).
     84     s->declareGlobal(fEdgeDistanceEquation);
     85     s->codeAppendf("int edgeidx0 = %s > 0 ? 3 : 0;", wind);
     86     s->codeAppendf("float2 edgept0 = %s[edgeidx0];", pts);
     87     s->codeAppendf("float2 edgept1 = %s[3 - edgeidx0];", pts);
     88     Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str());
     89 }
     90 
     91 void GrCCCubicShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
     92                                      GrGLSLVarying::Scope scope, SkString* code,
     93                                      const char* position, const char* coverage,
     94                                      const char* cornerCoverage) {
     95     fKLM_fEdge.reset(kFloat4_GrSLType, scope);
     96     varyingHandler->addVarying("klm_and_edge", &fKLM_fEdge);
     97     code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str());
     98     // We give L & M both the same sign as wind, in order to pass this value to the fragment shader.
     99     // (Cubics are pre-chopped such that L & M do not change sign within any individual segment.)
    100     code->appendf("%s.xyz = klm * float3(1, %s, %s);",
    101                   OutName(fKLM_fEdge), coverage, coverage); // coverage == wind on curves.
    102     code->appendf("%s.w = dot(float3(%s, 1), %s);", // Flat edge opposite the curve.
    103                   OutName(fKLM_fEdge), position, fEdgeDistanceEquation.c_str());
    104 
    105     fGradMatrix.reset(kFloat4_GrSLType, scope);
    106     varyingHandler->addVarying("grad_matrix", &fGradMatrix);
    107     code->appendf("%s.xy = 2*bloat * 3 * klm[0] * %s[0].xy;",
    108                   OutName(fGradMatrix), fKLMMatrix.c_str());
    109     code->appendf("%s.zw = -2*bloat * (klm[1] * %s[2].xy + klm[2] * %s[1].xy);",
    110                     OutName(fGradMatrix), fKLMMatrix.c_str(), fKLMMatrix.c_str());
    111 
    112     if (cornerCoverage) {
    113         code->appendf("half hull_coverage; {");
    114         this->calcHullCoverage(code, OutName(fKLM_fEdge), OutName(fGradMatrix), "hull_coverage");
    115         code->appendf("}");
    116         fCornerCoverage.reset(kHalf2_GrSLType, scope);
    117         varyingHandler->addVarying("corner_coverage", &fCornerCoverage);
    118         code->appendf("%s = half2(hull_coverage, 1) * %s;",
    119                       OutName(fCornerCoverage), cornerCoverage);
    120     }
    121 }
    122 
    123 void GrCCCubicShader::onEmitFragmentCode(GrGLSLFPFragmentBuilder* f,
    124                                          const char* outputCoverage) const {
    125     this->calcHullCoverage(&AccessCodeString(f), fKLM_fEdge.fsIn(), fGradMatrix.fsIn(),
    126                            outputCoverage);
    127 
    128     // Wind is the sign of both L and/or M. Take the sign of whichever has the larger magnitude.
    129     // (In reality, either would be fine because we chop cubics with more than a half pixel of
    130     // padding around the L & M lines, so neither should approach zero.)
    131     f->codeAppend ("half wind = sign(l + m);");
    132     f->codeAppendf("%s *= wind;", outputCoverage);
    133 
    134     if (fCornerCoverage.fsIn()) {
    135         f->codeAppendf("%s = %s.x * %s.y + %s;", // Attenuated corner coverage.
    136                        outputCoverage, fCornerCoverage.fsIn(), fCornerCoverage.fsIn(),
    137                        outputCoverage);
    138     }
    139 }
    140 
    141 void GrCCCubicShader::calcHullCoverage(SkString* code, const char* klmAndEdge,
    142                                        const char* gradMatrix, const char* outputCoverage) const {
    143     code->appendf("float k = %s.x, l = %s.y, m = %s.z;", klmAndEdge, klmAndEdge, klmAndEdge);
    144     code->append ("float f = k*k*k - l*m;");
    145     code->appendf("float2 grad = %s.xy * k + %s.zw;", gradMatrix, gradMatrix);
    146     code->append ("float fwidth = abs(grad.x) + abs(grad.y);");
    147     code->appendf("%s = min(0.5 - f/fwidth, 1);", outputCoverage); // Curve coverage.
    148     code->appendf("half d = min(%s.w, 0);", klmAndEdge); // Flat edge opposite the curve.
    149     code->appendf("%s = max(%s + d, 0);", outputCoverage, outputCoverage); // Total hull coverage.
    150 }
    151