blob: 81353139656df6f70565cba23a2ed78f96f88300 [file] [log] [blame]
Chris Dalton6a3dbee2017-10-16 10:44:41 -06001/*
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
Chris Dalton383a2ef2018-01-08 17:21:41 -05008#include "GrCCTriangleShader.h"
Chris Dalton6a3dbee2017-10-16 10:44:41 -06009
10#include "glsl/GrGLSLFragmentShaderBuilder.h"
Chris Daltonc17bf322017-10-24 10:59:03 -060011#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton6a3dbee2017-10-16 10:44:41 -060012
Chris Dalton383a2ef2018-01-08 17:21:41 -050013using Shader = GrCCCoverageProcessor::Shader;
Chris Dalton6a3dbee2017-10-16 10:44:41 -060014
Chris Daltonf510e262018-01-30 16:42:37 -070015void GrCCTriangleShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
16 GrGLSLVarying::Scope scope, SkString* code,
17 const char* /*position*/, const char* inputCoverage,
18 const char* wind) {
Chris Dalton5183e642018-03-07 12:53:01 -070019 SkASSERT(inputCoverage);
Chris Dalton90e8fb12017-12-22 02:24:53 -070020 fCoverageTimesWind.reset(kHalf_GrSLType, scope);
Chris Dalton5183e642018-03-07 12:53:01 -070021 varyingHandler->addVarying("coverage_times_wind", &fCoverageTimesWind);
22 code->appendf("%s = %s * %s;", OutName(fCoverageTimesWind), inputCoverage, wind);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060023}
24
Chris Daltondf04ce22018-03-07 17:28:07 -070025void GrCCTriangleShader::onEmitFragmentCode(const GrCCCoverageProcessor&,
26 GrGLSLFPFragmentBuilder* f,
Chris Dalton383a2ef2018-01-08 17:21:41 -050027 const char* outputCoverage) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060028 f->codeAppendf("%s = %s;", outputCoverage, fCoverageTimesWind.fsIn());
29}
30
Chris Dalton383a2ef2018-01-08 17:21:41 -050031void GrCCTriangleCornerShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
32 const char* repetitionID, const char* wind,
33 GeometryVars* vars) const {
Chris Dalton1fbdb612017-12-12 12:48:47 -070034 s->codeAppendf("float2 corner = %s[%s];", pts, repetitionID);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060035 vars->fCornerVars.fPoint = "corner";
36
Chris Dalton1fbdb612017-12-12 12:48:47 -070037 s->codeAppendf("float2x2 vectors = float2x2(corner - %s[0 != %s ? %s - 1 : 2], "
38 "corner - %s[2 != %s ? %s + 1 : 0]);",
39 pts, repetitionID, repetitionID, pts, repetitionID,
40 repetitionID);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060041
42 // Make sure neither vector is 0 to avoid a divide-by-zero. Wind will be zero anyway if this
43 // is the case, so whatever we output won't have any effect as long it isn't NaN or Inf.
44 s->codeAppend ("for (int i = 0; i < 2; ++i) {");
45 s->codeAppend ( "vectors[i] = (vectors[i] != float2(0)) ? vectors[i] : float2(1);");
46 s->codeAppend ("}");
47
48 // Find the vector that bisects the region outside the incoming edges. Each edge is
49 // responsible to subtract the outside region on its own the side of the bisector.
50 s->codeAppendf("float2 leftdir = normalize(vectors[%s > 0 ? 0 : 1]);", wind);
51 s->codeAppendf("float2 rightdir = normalize(vectors[%s > 0 ? 1 : 0]);", wind);
52 s->codeAppend ("float2 bisect = dot(leftdir, rightdir) >= 0 ? "
53 "leftdir + rightdir : "
54 "float2(leftdir.y - rightdir.y, rightdir.x - leftdir.x);");
55
56 // In ccpr we don't calculate exact geometric pixel coverage. What the distance-to-edge
57 // method actually finds is coverage inside a logical "AA box", one that is rotated inline
58 // with the edge, and in our case, up-scaled to circumscribe the actual pixel. Below we set
59 // up transformations into normalized logical AA box space for both incoming edges. These
60 // will tell the fragment shader where the corner is located within each edge's AA box.
61 s->declareGlobal(fAABoxMatrices);
62 s->declareGlobal(fAABoxTranslates);
63 s->declareGlobal(fGeoShaderBisects);
64 s->codeAppendf("for (int i = 0; i < 2; ++i) {");
65 // The X component runs parallel to the edge (i.e. distance to the corner).
66 s->codeAppendf( "float2 n = -vectors[%s > 0 ? i : 1 - i];", wind);
Chris Daltonc17bf322017-10-24 10:59:03 -060067 s->codeAppend ( "float nwidth = (abs(n.x) + abs(n.y)) * (bloat * 2);");
Chris Dalton6a3dbee2017-10-16 10:44:41 -060068 s->codeAppend ( "n /= nwidth;"); // nwidth != 0 because both vectors != 0.
69 s->codeAppendf( "%s[i][0] = n;", fAABoxMatrices.c_str());
70 s->codeAppendf( "%s[i][0] = -dot(n, corner) + .5;", fAABoxTranslates.c_str());
71
72 // The Y component runs perpendicular to the edge (i.e. distance-to-edge).
Chris Dalton6a3dbee2017-10-16 10:44:41 -060073 s->codeAppend ( "n = (i == 0) ? float2(-n.y, n.x) : float2(n.y, -n.x);");
Chris Dalton6a3dbee2017-10-16 10:44:41 -060074 s->codeAppendf( "%s[i][1] = n;", fAABoxMatrices.c_str());
75 s->codeAppendf( "%s[i][1] = -dot(n, corner) + .5;", fAABoxTranslates.c_str());
76
77 // Translate the bisector into logical AA box space.
78 // NOTE: Since the region outside two edges of a convex shape is in [180 deg, 360 deg], the
79 // bisector will therefore be in [90 deg, 180 deg]. Or, x >= 0 and y <= 0 in AA box space.
80 s->codeAppendf( "%s[i] = -bisect * %s[i];",
81 fGeoShaderBisects.c_str(), fAABoxMatrices.c_str());
82 s->codeAppend ("}");
83}
84
Chris Daltonf510e262018-01-30 16:42:37 -070085void GrCCTriangleCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
86 GrGLSLVarying::Scope scope, SkString* code,
87 const char* position, const char* inputCoverage,
88 const char* wind) {
Chris Dalton7b046312018-02-02 11:06:30 -070089 using Interpolation = GrGLSLVaryingHandler::Interpolation;
Chris Daltonf510e262018-01-30 16:42:37 -070090 SkASSERT(!inputCoverage);
Chris Daltonde5a8142017-12-18 10:05:15 -070091
Chris Dalton90e8fb12017-12-22 02:24:53 -070092 fCornerLocationInAABoxes.reset(kFloat2x2_GrSLType, scope);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060093 varyingHandler->addVarying("corner_location_in_aa_boxes", &fCornerLocationInAABoxes);
Chris Dalton90e8fb12017-12-22 02:24:53 -070094
95 fBisectInAABoxes.reset(kFloat2x2_GrSLType, scope);
Chris Dalton7b046312018-02-02 11:06:30 -070096 varyingHandler->addVarying("bisect_in_aa_boxes", &fBisectInAABoxes, Interpolation::kCanBeFlat);
Chris Dalton90e8fb12017-12-22 02:24:53 -070097
Chris Dalton6a3dbee2017-10-16 10:44:41 -060098 code->appendf("for (int i = 0; i < 2; ++i) {");
99 code->appendf( "%s[i] = %s * %s[i] + %s[i];",
Chris Dalton90e8fb12017-12-22 02:24:53 -0700100 OutName(fCornerLocationInAABoxes), position, fAABoxMatrices.c_str(),
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600101 fAABoxTranslates.c_str());
Chris Dalton90e8fb12017-12-22 02:24:53 -0700102 code->appendf( "%s[i] = %s[i];", OutName(fBisectInAABoxes), fGeoShaderBisects.c_str());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600103 code->appendf("}");
104
Chris Daltonf510e262018-01-30 16:42:37 -0700105 fWindTimesHalf.reset(kHalf_GrSLType, scope);
Chris Dalton7b046312018-02-02 11:06:30 -0700106 varyingHandler->addVarying("wind_times_half", &fWindTimesHalf, Interpolation::kCanBeFlat);
Chris Daltonf510e262018-01-30 16:42:37 -0700107 code->appendf("%s = %s * .5;", OutName(fWindTimesHalf), wind);
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600108}
109
Chris Daltondf04ce22018-03-07 17:28:07 -0700110void GrCCTriangleCornerShader::onEmitFragmentCode(const GrCCCoverageProcessor&,
111 GrGLSLFPFragmentBuilder* f,
Chris Dalton383a2ef2018-01-08 17:21:41 -0500112 const char* outputCoverage) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600113 // By the time we reach this shader, the pixel is in the following state:
114 //
115 // 1. The hull shader has emitted a coverage of 1.
116 // 2. Both edges have subtracted the area on their outside.
117 //
118 // This generally works, but it is a problem for corner pixels. There is a region within
119 // corner pixels that is outside both edges at the same time. This means the region has been
120 // double subtracted (once by each edge). The purpose of this shader is to fix these corner
121 // pixels.
122 //
123 // More specifically, each edge redoes its coverage analysis so that it only subtracts the
124 // outside area that falls on its own side of the bisector line.
125 //
126 // NOTE: unless the edges fall on multiples of 90 deg from one another, they will have
127 // different AA boxes. (For an explanation of AA boxes, see comments in
128 // onEmitGeometryShader.) This means the coverage analysis will only be approximate. It
129 // seems acceptable, but if we want exact coverage we will need to switch to a more
130 // expensive model.
131 f->codeAppendf("for (int i = 0; i < 2; ++i) {"); // Loop through both edges.
132 f->codeAppendf( "half2 corner = %s[i];", fCornerLocationInAABoxes.fsIn());
133 f->codeAppendf( "half2 bisect = %s[i];", fBisectInAABoxes.fsIn());
134
135 // Find the point at which the bisector exits the logical AA box.
136 // (The inequality works because bisect.x is known >= 0 and bisect.y is known <= 0.)
137 f->codeAppendf( "half2 d = half2(1 - corner.x, -corner.y);");
138 f->codeAppendf( "half T = d.y * bisect.x >= d.x * bisect.y ? d.y / bisect.y "
139 ": d.x / bisect.x;");
140 f->codeAppendf( "half2 exit = corner + bisect * T;");
141
142 // These lines combined (and the final multiply by .5) accomplish the following:
143 // 1. Add back the area beyond the corner that was subtracted out previously.
144 // 2. Subtract out the area beyond the corner, but under the bisector.
145 // The other edge will take care of the area on its own side of the bisector.
146 f->codeAppendf( "%s += (2 - corner.x - exit.x) * corner.y;", outputCoverage);
147 f->codeAppendf( "%s += (corner.x - 1) * exit.y;", outputCoverage);
148 f->codeAppendf("}");
149
Chris Daltonf510e262018-01-30 16:42:37 -0700150 f->codeAppendf("%s *= %s;", outputCoverage, fWindTimesHalf.fsIn());
Chris Dalton6a3dbee2017-10-16 10:44:41 -0600151}