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 "GrCCQuadraticShader.h"
9 
10 #include "glsl/GrGLSLFragmentShaderBuilder.h"
11 #include "glsl/GrGLSLVertexGeoBuilder.h"
12 
emitSetupCode(GrGLSLVertexGeoBuilder * s,const char * pts,const char * wind,const char ** outHull4) const13 void GrCCQuadraticShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
14                                         const char* wind, const char** outHull4) const {
15     s->declareGlobal(fQCoordMatrix);
16     s->codeAppendf("%s = float2x2(1, 1, .5, 0) * inverse(float2x2(%s[2] - %s[0], %s[1] - %s[0]));",
17                    fQCoordMatrix.c_str(), pts, pts, pts, pts);
18 
19     s->declareGlobal(fQCoord0);
20     s->codeAppendf("%s = %s[0];", fQCoord0.c_str(), pts);
21 
22     s->declareGlobal(fEdgeDistanceEquation);
23     s->codeAppendf("float2 edgept0 = %s[%s > 0 ? 2 : 0];", pts, wind);
24     s->codeAppendf("float2 edgept1 = %s[%s > 0 ? 0 : 2];", pts, wind);
25     Shader::EmitEdgeDistanceEquation(s, "edgept0", "edgept1", fEdgeDistanceEquation.c_str());
26 
27     if (outHull4) {
28         // Clip the bezier triangle by the tangent line at maximum height. Quadratics have the nice
29         // property that maximum height always occurs at T=.5. This is a simple application for
30         // De Casteljau's algorithm.
31         s->codeAppend ("float2 quadratic_hull[4];");
32         s->codeAppendf("quadratic_hull[0] = %s[0];", pts);
33         s->codeAppendf("quadratic_hull[1] = (%s[0] + %s[1]) * .5;", pts, pts);
34         s->codeAppendf("quadratic_hull[2] = (%s[1] + %s[2]) * .5;", pts, pts);
35         s->codeAppendf("quadratic_hull[3] = %s[2];", pts);
36         *outHull4 = "quadratic_hull";
37     }
38 }
39 
onEmitVaryings(GrGLSLVaryingHandler * varyingHandler,GrGLSLVarying::Scope scope,SkString * code,const char * position,const char * coverage,const char * cornerCoverage)40 void GrCCQuadraticShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
41                                          GrGLSLVarying::Scope scope, SkString* code,
42                                          const char* position, const char* coverage,
43                                          const char* cornerCoverage) {
44     fCoord_fGrad.reset(kFloat4_GrSLType, scope);
45     varyingHandler->addVarying("coord_and_grad", &fCoord_fGrad);
46     code->appendf("%s.xy = %s * (%s - %s);", // Quadratic coords.
47                   OutName(fCoord_fGrad), fQCoordMatrix.c_str(), position, fQCoord0.c_str());
48     code->appendf("%s.zw = 2*bloat * float2(2 * %s.x, -1) * %s;", // Gradient.
49                   OutName(fCoord_fGrad), OutName(fCoord_fGrad), fQCoordMatrix.c_str());
50 
51     // Coverages need full precision since distance to the opposite edge can be large.
52     fEdge_fWind_fCorner.reset(cornerCoverage ? kFloat4_GrSLType : kFloat2_GrSLType, scope);
53     varyingHandler->addVarying("edge_and_wind_and_corner", &fEdge_fWind_fCorner);
54     code->appendf("float edge = dot(%s, float3(%s, 1));", // Distance to flat opposite edge.
55                   fEdgeDistanceEquation.c_str(), position);
56     code->appendf("%s.x = edge;", OutName(fEdge_fWind_fCorner));
57     code->appendf("%s.y = %s;", OutName(fEdge_fWind_fCorner), coverage); // coverage == wind.
58 
59     if (cornerCoverage) {
60         code->appendf("half hull_coverage;");
61         this->calcHullCoverage(code, OutName(fCoord_fGrad), "edge", "hull_coverage");
62         code->appendf("%s.zw = half2(hull_coverage, 1) * %s;",
63                       OutName(fEdge_fWind_fCorner), cornerCoverage);
64     }
65 }
66 
onEmitFragmentCode(GrGLSLFPFragmentBuilder * f,const char * outputCoverage) const67 void GrCCQuadraticShader::onEmitFragmentCode(GrGLSLFPFragmentBuilder* f,
68                                              const char* outputCoverage) const {
69     this->calcHullCoverage(&AccessCodeString(f), fCoord_fGrad.fsIn(),
70                            SkStringPrintf("%s.x", fEdge_fWind_fCorner.fsIn()).c_str(),
71                            outputCoverage);
72     f->codeAppendf("%s *= %s.y;", outputCoverage, fEdge_fWind_fCorner.fsIn()); // Wind.
73 
74     if (kFloat4_GrSLType == fEdge_fWind_fCorner.type()) {
75         f->codeAppendf("%s = %s.z * %s.w + %s;",// Attenuated corner coverage.
76                        outputCoverage, fEdge_fWind_fCorner.fsIn(), fEdge_fWind_fCorner.fsIn(),
77                        outputCoverage);
78     }
79 }
80 
calcHullCoverage(SkString * code,const char * coordAndGrad,const char * edge,const char * outputCoverage) const81 void GrCCQuadraticShader::calcHullCoverage(SkString* code, const char* coordAndGrad,
82                                            const char* edge, const char* outputCoverage) const {
83     code->appendf("float x = %s.x, y = %s.y;", coordAndGrad, coordAndGrad);
84     code->appendf("float2 grad = %s.zw;", coordAndGrad);
85     code->append ("float f = x*x - y;");
86     code->append ("float fwidth = abs(grad.x) + abs(grad.y);");
87     code->appendf("%s = min(0.5 - f/fwidth, 1);", outputCoverage); // Curve coverage.
88     code->appendf("half d = min(%s, 0);", edge); // Flat edge opposite the curve.
89     code->appendf("%s = max(%s + d, 0);", outputCoverage, outputCoverage); // Total hull coverage.
90 }
91