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
8#include "src/gpu/ccpr/GrCCConicShader.h"
9
10#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
11#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
12
13void GrCCConicShader::emitSetupCode(
14 GrGLSLVertexGeoBuilder* s, const char* pts, const char** outHull4) const {
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
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());
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);");
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);
46 *outHull4 = "conic_hull";
47 }
48}
49
50void GrCCConicShader::onEmitVaryings(
51 GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code,
52 const char* position, const char* coverage, const char* cornerCoverage, const char* wind) {
53 code->appendf("float3 klm = float3(%s - %s, 1) * %s;",
54 position, fControlPoint.c_str(), fKLMMatrix.c_str());
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 }
63 code->appendf("%s.xyz = klm;", OutName(fKLM_fWind));
64
65 fGrad_fCorner.reset(cornerCoverage ? kFloat4_GrSLType : kFloat2_GrSLType, scope);
66 varyingHandler->addVarying((cornerCoverage) ? "grad_and_corner" : "grad", &fGrad_fCorner);
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) {
71 SkASSERT(coverage);
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
79void GrCCConicShader::emitFragmentCoverageCode(
80 GrGLSLFPFragmentBuilder* f, const char* outputCoverage) const {
81 this->calcHullCoverage(&AccessCodeString(f), fKLM_fWind.fsIn(), fGrad_fCorner.fsIn(),
82 outputCoverage);
83 f->codeAppendf("%s *= half(%s.w);", outputCoverage, fKLM_fWind.fsIn()); // Wind.
84
85 if (kFloat4_GrSLType == fGrad_fCorner.type()) {
86 f->codeAppendf("%s = fma(half(%s.z), half(%s.w), %s);", // Attenuated corner coverage.
87 outputCoverage, fGrad_fCorner.fsIn(), fGrad_fCorner.fsIn(),
88 outputCoverage);
89 }
90}
91
92void 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);
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);
102}
103
104void 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}
111