blob: ce05697dbf1f6359c1d347bae531f2c4e8d77334 [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
8#include "GrCCPRTriangleShader.h"
9
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 Daltonde5a8142017-12-18 10:05:15 -070013using Shader = GrCCPRCoverageProcessor::Shader;
Chris Dalton6a3dbee2017-10-16 10:44:41 -060014
Chris Daltonde5a8142017-12-18 10:05:15 -070015Shader::WindHandling GrCCPRTriangleShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler,
16 SkString* code, const char* /*position*/,
17 const char* coverage, const char* wind) {
18 if (!coverage) {
19 varyingHandler->addFlatVarying("wind", &fCoverageTimesWind);
20 code->appendf("%s = %s;", fCoverageTimesWind.gsOut(), wind);
21 } else {
22 varyingHandler->addVarying("coverage_times_wind", &fCoverageTimesWind);
23 code->appendf("%s = %s * %s;", fCoverageTimesWind.gsOut(), coverage, wind);
24 }
Chris Dalton6a3dbee2017-10-16 10:44:41 -060025 return WindHandling::kHandled;
26}
27
Chris Daltonde5a8142017-12-18 10:05:15 -070028void GrCCPRTriangleShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
29 const char* outputCoverage) const {
Chris Dalton6a3dbee2017-10-16 10:44:41 -060030 f->codeAppendf("%s = %s;", outputCoverage, fCoverageTimesWind.fsIn());
31}
32
Chris Dalton1fbdb612017-12-12 12:48:47 -070033void GrCCPRTriangleCornerShader::emitSetupCode(GrGLSLVertexGeoBuilder* s, const char* pts,
34 const char* repetitionID, const char* wind,
Chris Dalton6a3dbee2017-10-16 10:44:41 -060035 GeometryVars* vars) const {
Chris Dalton1fbdb612017-12-12 12:48:47 -070036 s->codeAppendf("float2 corner = %s[%s];", pts, repetitionID);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060037 vars->fCornerVars.fPoint = "corner";
38
Chris Dalton1fbdb612017-12-12 12:48:47 -070039 s->codeAppendf("float2x2 vectors = float2x2(corner - %s[0 != %s ? %s - 1 : 2], "
40 "corner - %s[2 != %s ? %s + 1 : 0]);",
41 pts, repetitionID, repetitionID, pts, repetitionID,
42 repetitionID);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060043
44 // Make sure neither vector is 0 to avoid a divide-by-zero. Wind will be zero anyway if this
45 // is the case, so whatever we output won't have any effect as long it isn't NaN or Inf.
46 s->codeAppend ("for (int i = 0; i < 2; ++i) {");
47 s->codeAppend ( "vectors[i] = (vectors[i] != float2(0)) ? vectors[i] : float2(1);");
48 s->codeAppend ("}");
49
50 // Find the vector that bisects the region outside the incoming edges. Each edge is
51 // responsible to subtract the outside region on its own the side of the bisector.
52 s->codeAppendf("float2 leftdir = normalize(vectors[%s > 0 ? 0 : 1]);", wind);
53 s->codeAppendf("float2 rightdir = normalize(vectors[%s > 0 ? 1 : 0]);", wind);
54 s->codeAppend ("float2 bisect = dot(leftdir, rightdir) >= 0 ? "
55 "leftdir + rightdir : "
56 "float2(leftdir.y - rightdir.y, rightdir.x - leftdir.x);");
57
58 // In ccpr we don't calculate exact geometric pixel coverage. What the distance-to-edge
59 // method actually finds is coverage inside a logical "AA box", one that is rotated inline
60 // with the edge, and in our case, up-scaled to circumscribe the actual pixel. Below we set
61 // up transformations into normalized logical AA box space for both incoming edges. These
62 // will tell the fragment shader where the corner is located within each edge's AA box.
63 s->declareGlobal(fAABoxMatrices);
64 s->declareGlobal(fAABoxTranslates);
65 s->declareGlobal(fGeoShaderBisects);
66 s->codeAppendf("for (int i = 0; i < 2; ++i) {");
67 // The X component runs parallel to the edge (i.e. distance to the corner).
68 s->codeAppendf( "float2 n = -vectors[%s > 0 ? i : 1 - i];", wind);
Chris Daltonc17bf322017-10-24 10:59:03 -060069 s->codeAppend ( "float nwidth = (abs(n.x) + abs(n.y)) * (bloat * 2);");
Chris Dalton6a3dbee2017-10-16 10:44:41 -060070 s->codeAppend ( "n /= nwidth;"); // nwidth != 0 because both vectors != 0.
71 s->codeAppendf( "%s[i][0] = n;", fAABoxMatrices.c_str());
72 s->codeAppendf( "%s[i][0] = -dot(n, corner) + .5;", fAABoxTranslates.c_str());
73
74 // The Y component runs perpendicular to the edge (i.e. distance-to-edge).
Chris Dalton6a3dbee2017-10-16 10:44:41 -060075 s->codeAppend ( "n = (i == 0) ? float2(-n.y, n.x) : float2(n.y, -n.x);");
Chris Dalton6a3dbee2017-10-16 10:44:41 -060076 s->codeAppendf( "%s[i][1] = n;", fAABoxMatrices.c_str());
77 s->codeAppendf( "%s[i][1] = -dot(n, corner) + .5;", fAABoxTranslates.c_str());
78
79 // Translate the bisector into logical AA box space.
80 // NOTE: Since the region outside two edges of a convex shape is in [180 deg, 360 deg], the
81 // bisector will therefore be in [90 deg, 180 deg]. Or, x >= 0 and y <= 0 in AA box space.
82 s->codeAppendf( "%s[i] = -bisect * %s[i];",
83 fGeoShaderBisects.c_str(), fAABoxMatrices.c_str());
84 s->codeAppend ("}");
85}
86
Chris Daltonde5a8142017-12-18 10:05:15 -070087Shader::WindHandling
Chris Dalton6a3dbee2017-10-16 10:44:41 -060088GrCCPRTriangleCornerShader::onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, SkString* code,
Chris Daltonde5a8142017-12-18 10:05:15 -070089 const char* position, const char* coverage,
Chris Dalton6a3dbee2017-10-16 10:44:41 -060090 const char* /*wind*/) {
Chris Daltonde5a8142017-12-18 10:05:15 -070091 SkASSERT(!coverage);
92
Chris Dalton6a3dbee2017-10-16 10:44:41 -060093 varyingHandler->addVarying("corner_location_in_aa_boxes", &fCornerLocationInAABoxes);
94 varyingHandler->addFlatVarying("bisect_in_aa_boxes", &fBisectInAABoxes);
95 code->appendf("for (int i = 0; i < 2; ++i) {");
96 code->appendf( "%s[i] = %s * %s[i] + %s[i];",
97 fCornerLocationInAABoxes.gsOut(), position, fAABoxMatrices.c_str(),
98 fAABoxTranslates.c_str());
99 code->appendf( "%s[i] = %s[i];", fBisectInAABoxes.gsOut(), fGeoShaderBisects.c_str());
100 code->appendf("}");
101
102 return WindHandling::kNotHandled;
103}
104
105void GrCCPRTriangleCornerShader::onEmitFragmentCode(GrGLSLPPFragmentBuilder* f,
106 const char* outputCoverage) const {
107 // By the time we reach this shader, the pixel is in the following state:
108 //
109 // 1. The hull shader has emitted a coverage of 1.
110 // 2. Both edges have subtracted the area on their outside.
111 //
112 // This generally works, but it is a problem for corner pixels. There is a region within
113 // corner pixels that is outside both edges at the same time. This means the region has been
114 // double subtracted (once by each edge). The purpose of this shader is to fix these corner
115 // pixels.
116 //
117 // More specifically, each edge redoes its coverage analysis so that it only subtracts the
118 // outside area that falls on its own side of the bisector line.
119 //
120 // NOTE: unless the edges fall on multiples of 90 deg from one another, they will have
121 // different AA boxes. (For an explanation of AA boxes, see comments in
122 // onEmitGeometryShader.) This means the coverage analysis will only be approximate. It
123 // seems acceptable, but if we want exact coverage we will need to switch to a more
124 // expensive model.
125 f->codeAppendf("for (int i = 0; i < 2; ++i) {"); // Loop through both edges.
126 f->codeAppendf( "half2 corner = %s[i];", fCornerLocationInAABoxes.fsIn());
127 f->codeAppendf( "half2 bisect = %s[i];", fBisectInAABoxes.fsIn());
128
129 // Find the point at which the bisector exits the logical AA box.
130 // (The inequality works because bisect.x is known >= 0 and bisect.y is known <= 0.)
131 f->codeAppendf( "half2 d = half2(1 - corner.x, -corner.y);");
132 f->codeAppendf( "half T = d.y * bisect.x >= d.x * bisect.y ? d.y / bisect.y "
133 ": d.x / bisect.x;");
134 f->codeAppendf( "half2 exit = corner + bisect * T;");
135
136 // These lines combined (and the final multiply by .5) accomplish the following:
137 // 1. Add back the area beyond the corner that was subtracted out previously.
138 // 2. Subtract out the area beyond the corner, but under the bisector.
139 // The other edge will take care of the area on its own side of the bisector.
140 f->codeAppendf( "%s += (2 - corner.x - exit.x) * corner.y;", outputCoverage);
141 f->codeAppendf( "%s += (corner.x - 1) * exit.y;", outputCoverage);
142 f->codeAppendf("}");
143
144 f->codeAppendf("%s *= .5;", outputCoverage);
145}