blob: 68333420fa9f0f8367ee7c4a006d209eabe24429 [file] [log] [blame]
Michael Ludwig460eb5e2018-10-29 11:09:29 -04001/*
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
Michael Ludwigfd4f4df2019-05-29 09:51:09 -04008#include "src/gpu/ops/GrQuadPerEdgeAA.h"
9
Michael Ludwigfb7ba522019-10-29 15:33:34 -040010#include "include/private/SkVx.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/SkGr.h"
Michael Ludwigfb7ba522019-10-29 15:33:34 -040012#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
14#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
16#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
17#include "src/gpu/glsl/GrGLSLVarying.h"
18#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Michael Ludwig460eb5e2018-10-29 11:09:29 -040019
Brian Osman01e6d172020-03-30 15:57:14 -040020static_assert((int)GrQuadAAFlags::kLeft == SkCanvas::kLeft_QuadAAFlag);
21static_assert((int)GrQuadAAFlags::kTop == SkCanvas::kTop_QuadAAFlag);
22static_assert((int)GrQuadAAFlags::kRight == SkCanvas::kRight_QuadAAFlag);
23static_assert((int)GrQuadAAFlags::kBottom == SkCanvas::kBottom_QuadAAFlag);
24static_assert((int)GrQuadAAFlags::kNone == SkCanvas::kNone_QuadAAFlags);
25static_assert((int)GrQuadAAFlags::kAll == SkCanvas::kAll_QuadAAFlags);
Michael Ludwigf995c052018-11-26 15:24:29 -050026
Michael Ludwig460eb5e2018-10-29 11:09:29 -040027namespace {
28
Michael Ludwig73dbea62019-11-19 14:55:36 -050029// Generic WriteQuadProc that can handle any VertexSpec. It writes the 4 vertices in triangle strip
30// order, although the data per-vertex is dependent on the VertexSpec.
31static void write_quad_generic(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -050032 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -050033 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -040034 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050035 static constexpr auto If = GrVertexWriter::If<float>;
Michael Ludwig704d5402019-11-25 09:43:37 -050036
37 SkASSERT(!spec.hasLocalCoords() || localQuad);
38
Michael Ludwig73dbea62019-11-19 14:55:36 -050039 GrQuadPerEdgeAA::CoverageMode mode = spec.coverageMode();
Michael Ludwig553e9a92018-11-29 12:38:35 -050040 for (int i = 0; i < 4; ++i) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050041 // save position, this is a float2 or float3 or float4 depending on the combination of
42 // perspective and coverage mode.
Michael Ludwig704d5402019-11-25 09:43:37 -050043 vb->write(deviceQuad->x(i), deviceQuad->y(i),
44 If(spec.deviceQuadType() == GrQuad::Type::kPerspective, deviceQuad->w(i)),
Robert Phillips29f38542019-10-16 09:20:25 -040045 If(mode == GrQuadPerEdgeAA::CoverageMode::kWithPosition, coverage[i]));
Michael Ludwig4921dc32018-12-03 14:57:29 +000046
Michael Ludwig93aeba02018-12-21 09:50:31 -050047 // save color
48 if (spec.hasVertexColors()) {
Brian Osman2715bf52019-12-06 14:38:47 -050049 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwige6266a22019-03-07 11:24:32 -050050 vb->write(GrVertexColor(
Michael Ludwig73dbea62019-11-19 14:55:36 -050051 color * (mode == GrQuadPerEdgeAA::CoverageMode::kWithColor ? coverage[i] : 1.f),
Robert Phillips29f38542019-10-16 09:20:25 -040052 wide));
Michael Ludwig93aeba02018-12-21 09:50:31 -050053 }
54
55 // save local position
56 if (spec.hasLocalCoords()) {
Michael Ludwig704d5402019-11-25 09:43:37 -050057 vb->write(localQuad->x(i), localQuad->y(i),
58 If(spec.localQuadType() == GrQuad::Type::kPerspective, localQuad->w(i)));
Michael Ludwig93aeba02018-12-21 09:50:31 -050059 }
60
Brian Salomon2432d062020-04-16 20:48:09 -040061 // save the geometry subset
62 if (spec.requiresGeometrySubset()) {
63 vb->write(geomSubset);
Michael Ludwigdcfbe322019-04-01 14:55:54 -040064 }
65
Brian Salomon2432d062020-04-16 20:48:09 -040066 // save the texture subset
67 if (spec.hasSubset()) {
68 vb->write(texSubset);
Michael Ludwig93aeba02018-12-21 09:50:31 -050069 }
70 }
71}
72
Michael Ludwig73dbea62019-11-19 14:55:36 -050073// Specialized WriteQuadProcs for particular VertexSpecs that show up frequently (determined
74// experimentally through recorded GMs, SKPs, and SVGs, as well as SkiaRenderer's usage patterns):
75
Brian Salomon2432d062020-04-16 20:48:09 -040076// 2D (XY), no explicit coverage, vertex color, no locals, no geometry subset, no texture subsetn
Michael Ludwig73dbea62019-11-19 14:55:36 -050077// This represents simple, solid color or shader, non-AA (or AA with cov. as alpha) rects.
78static void write_2d_color(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -050079 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -050080 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -040081 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -050082 // Assert assumptions about VertexSpec
83 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
84 SkASSERT(!spec.hasLocalCoords());
85 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
86 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
87 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -040088 SkASSERT(!spec.requiresGeometrySubset());
89 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -050090 // We don't assert that localQuad == nullptr, since it is possible for GrFillRectOp to
91 // accumulate local coords conservatively (paint not trivial), and then after analysis realize
92 // the processors don't need local coordinates.
Michael Ludwig73dbea62019-11-19 14:55:36 -050093
Brian Osman2715bf52019-12-06 14:38:47 -050094 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -050095 for (int i = 0; i < 4; ++i) {
96 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
97 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
98 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -050099 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500100 }
101}
102
Brian Salomon2432d062020-04-16 20:48:09 -0400103// 2D (XY), no explicit coverage, UV locals, no color, no geometry subset, no texture subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500104// This represents opaque, non AA, textured rects
105static void write_2d_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500106 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500107 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400108 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500109 // Assert assumptions about VertexSpec
110 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
111 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
112 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone);
113 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400114 SkASSERT(!spec.requiresGeometrySubset());
115 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500116 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500117
118 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500119 vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500120 }
121}
122
Brian Salomon2432d062020-04-16 20:48:09 -0400123// 2D (XY), no explicit coverage, UV locals, vertex color, no geometry or texture subsets
Michael Ludwig73dbea62019-11-19 14:55:36 -0500124// This represents transparent, non AA (or AA with cov. as alpha), textured rects
125static void write_2d_color_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500126 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500127 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400128 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500129 // Assert assumptions about VertexSpec
130 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
131 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
132 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
133 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
134 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400135 SkASSERT(!spec.requiresGeometrySubset());
136 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500137 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500138
Brian Osman2715bf52019-12-06 14:38:47 -0500139 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500140 for (int i = 0; i < 4; ++i) {
141 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
142 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
143 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500144 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide),
145 localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500146 }
147}
148
Brian Salomon2432d062020-04-16 20:48:09 -0400149// 2D (XY), explicit coverage, UV locals, no color, no geometry subset, no texture subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500150// This represents opaque, AA, textured rects
151static void write_2d_cov_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500152 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500153 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400154 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500155 // Assert assumptions about VertexSpec
156 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
157 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
158 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition);
159 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400160 SkASSERT(!spec.requiresGeometrySubset());
161 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500162 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500163
164 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500165 vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i],
166 localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500167 }
168}
169
170// NOTE: The three _strict specializations below match the non-strict uv functions above, except
Brian Salomon2432d062020-04-16 20:48:09 -0400171// that they also write the UV subset. These are included to benefit SkiaRenderer, which must make
172// use of both fast and strict constrained subsets. When testing _strict was not that common across
Michael Ludwig73dbea62019-11-19 14:55:36 -0500173// GMS, SKPs, and SVGs but we have little visibility into actual SkiaRenderer statistics. If
Brian Salomon2432d062020-04-16 20:48:09 -0400174// SkiaRenderer can avoid subsets more, these 3 functions should probably be removed for simplicity.
Michael Ludwig73dbea62019-11-19 14:55:36 -0500175
Brian Salomon2432d062020-04-16 20:48:09 -0400176// 2D (XY), no explicit coverage, UV locals, no color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500177// This represents opaque, non AA, textured rects with strict uv sampling
178static void write_2d_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500179 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500180 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400181 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500182 // Assert assumptions about VertexSpec
183 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
184 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
185 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone);
186 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400187 SkASSERT(!spec.requiresGeometrySubset());
188 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500189 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500190
191 for (int i = 0; i < 4; ++i) {
Brian Salomon2432d062020-04-16 20:48:09 -0400192 vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500193 }
194}
195
Brian Salomon2432d062020-04-16 20:48:09 -0400196// 2D (XY), no explicit coverage, UV locals, vertex color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500197// This represents transparent, non AA (or AA with cov. as alpha), textured rects with strict sample
198static void write_2d_color_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500199 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500200 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400201 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500202 // Assert assumptions about VertexSpec
203 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
204 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
205 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
206 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
207 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400208 SkASSERT(!spec.requiresGeometrySubset());
209 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500210 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500211
Brian Osman2715bf52019-12-06 14:38:47 -0500212 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500213 for (int i = 0; i < 4; ++i) {
214 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
215 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
216 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500217 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide),
Brian Salomon2432d062020-04-16 20:48:09 -0400218 localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500219 }
220}
221
Brian Salomon2432d062020-04-16 20:48:09 -0400222// 2D (XY), explicit coverage, UV locals, no color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500223// This represents opaque, AA, textured rects with strict uv sampling
224static void write_2d_cov_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500225 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500226 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400227 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500228 // Assert assumptions about VertexSpec
229 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
230 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
231 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition);
232 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400233 SkASSERT(!spec.requiresGeometrySubset());
234 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500235 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500236
237 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500238 vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i],
Brian Salomon2432d062020-04-16 20:48:09 -0400239 localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500240 }
241}
242
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400243} // anonymous namespace
244
Michael Ludwigc182b942018-11-16 10:27:51 -0500245namespace GrQuadPerEdgeAA {
246
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400247IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads) {
248 if (aa == GrAAType::kCoverage) {
249 return IndexBufferOption::kPictureFramed;
250 } else if (numQuads > 1) {
251 return IndexBufferOption::kIndexedRects;
252 } else {
253 return IndexBufferOption::kTriStrips;
254 }
255}
256
Brian Osman2715bf52019-12-06 14:38:47 -0500257// This is a more elaborate version of fitsInBytes() that allows "no color" for white
258ColorType MinColorType(SkPMColor4f color) {
Brian Salomon1d835422019-03-13 16:11:44 -0400259 if (color == SK_PMColor4fWHITE) {
260 return ColorType::kNone;
Brian Salomon1d835422019-03-13 16:11:44 -0400261 } else {
Brian Osman2715bf52019-12-06 14:38:47 -0500262 return color.fitsInBytes() ? ColorType::kByte : ColorType::kFloat;
Brian Salomon1d835422019-03-13 16:11:44 -0400263 }
264}
265
Michael Ludwig73dbea62019-11-19 14:55:36 -0500266////////////////// Tessellator Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500267
Michael Ludwig73dbea62019-11-19 14:55:36 -0500268Tessellator::WriteQuadProc Tessellator::GetWriteQuadProc(const VertexSpec& spec) {
Brian Salomon2432d062020-04-16 20:48:09 -0400269 // All specialized writing functions requires 2D geometry and no geometry subset. This is not
Michael Ludwig73dbea62019-11-19 14:55:36 -0500270 // the same as just checking device type vs. kRectilinear since non-AA general 2D quads do not
Brian Salomon2432d062020-04-16 20:48:09 -0400271 // require a geometry subset and could then go through a fast path.
272 if (spec.deviceQuadType() != GrQuad::Type::kPerspective && !spec.requiresGeometrySubset()) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500273 CoverageMode mode = spec.coverageMode();
274 if (spec.hasVertexColors()) {
275 if (mode != CoverageMode::kWithPosition) {
276 // Vertex colors, but no explicit coverage
277 if (!spec.hasLocalCoords()) {
278 // Non-UV with vertex colors (possibly with coverage folded into alpha)
279 return write_2d_color;
280 } else if (spec.localQuadType() != GrQuad::Type::kPerspective) {
281 // UV locals with vertex colors (possibly with coverage-as-alpha)
Brian Salomon2432d062020-04-16 20:48:09 -0400282 return spec.hasSubset() ? write_2d_color_uv_strict : write_2d_color_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500283 }
284 }
285 // Else fall through; this is a spec that requires vertex colors and explicit coverage,
286 // which means it's anti-aliased and the FPs don't support coverage as alpha, or
287 // it uses 3D local coordinates.
288 } else if (spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective) {
289 if (mode == CoverageMode::kWithPosition) {
290 // UV locals with explicit coverage
Brian Salomon2432d062020-04-16 20:48:09 -0400291 return spec.hasSubset() ? write_2d_cov_uv_strict : write_2d_cov_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500292 } else {
293 SkASSERT(mode == CoverageMode::kNone);
Brian Salomon2432d062020-04-16 20:48:09 -0400294 return spec.hasSubset() ? write_2d_uv_strict : write_2d_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500295 }
296 }
297 // Else fall through to generic vertex function; this is a spec that has no vertex colors
298 // and [no|uvr] local coords, which doesn't happen often enough to warrant specialization.
299 }
Michael Ludwig41f395d2019-05-23 13:59:45 -0400300
Michael Ludwig73dbea62019-11-19 14:55:36 -0500301 // Arbitrary spec hits the slow path
302 return write_quad_generic;
303}
304
Michael Ludwig189c9802019-11-21 11:21:12 -0500305Tessellator::Tessellator(const VertexSpec& spec, char* vertices)
Michael Ludwig73dbea62019-11-19 14:55:36 -0500306 : fVertexSpec(spec)
Michael Ludwig189c9802019-11-21 11:21:12 -0500307 , fVertexWriter{vertices}
Michael Ludwig73dbea62019-11-19 14:55:36 -0500308 , fWriteProc(Tessellator::GetWriteQuadProc(spec)) {}
309
Michael Ludwig704d5402019-11-25 09:43:37 -0500310void Tessellator::append(GrQuad* deviceQuad, GrQuad* localQuad,
Brian Salomon2432d062020-04-16 20:48:09 -0400311 const SkPMColor4f& color, const SkRect& uvSubset, GrQuadAAFlags aaFlags) {
Michael Ludwig189c9802019-11-21 11:21:12 -0500312 // We allow Tessellator to be created with a null vertices pointer for convenience, but it is
313 // assumed it will never actually be used in those cases.
314 SkASSERT(fVertexWriter.fPtr);
Michael Ludwig704d5402019-11-25 09:43:37 -0500315 SkASSERT(deviceQuad->quadType() <= fVertexSpec.deviceQuadType());
316 SkASSERT(localQuad || !fVertexSpec.hasLocalCoords());
317 SkASSERT(!fVertexSpec.hasLocalCoords() || localQuad->quadType() <= fVertexSpec.localQuadType());
Michael Ludwig73dbea62019-11-19 14:55:36 -0500318
319 static const float kFullCoverage[4] = {1.f, 1.f, 1.f, 1.f};
320 static const float kZeroCoverage[4] = {0.f, 0.f, 0.f, 0.f};
Brian Salomon2432d062020-04-16 20:48:09 -0400321 static const SkRect kIgnoredSubset = SkRect::MakeEmpty();
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400322
Michael Ludwig73dbea62019-11-19 14:55:36 -0500323 if (fVertexSpec.usesCoverageAA()) {
324 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kWithColor ||
325 fVertexSpec.coverageMode() == CoverageMode::kWithPosition);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400326 // Must calculate inner and outer quadrilaterals for the vertex coverage ramps, and possibly
Brian Salomon2432d062020-04-16 20:48:09 -0400327 // a geometry subset if corners are not right angles
328 SkRect geomSubset;
329 if (fVertexSpec.requiresGeometrySubset()) {
330 geomSubset = deviceQuad->bounds();
331 geomSubset.outset(0.5f, 0.5f); // account for AA expansion
Michael Ludwige6266a22019-03-07 11:24:32 -0500332 }
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400333
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500334 if (aaFlags == GrQuadAAFlags::kNone) {
335 // Have to write the coverage AA vertex structure, but there's no math to be done for a
336 // non-aa quad batched into a coverage AA op.
Michael Ludwig189c9802019-11-21 11:21:12 -0500337 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400338 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500339 // Since we pass the same corners in, the outer vertex structure will have 0 area and
340 // the coverage interpolation from 1 to 0 will not be visible.
Michael Ludwig189c9802019-11-21 11:21:12 -0500341 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400342 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500343 } else {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500344 // Reset the tessellation helper to match the current geometry
Michael Ludwig704d5402019-11-25 09:43:37 -0500345 fAAHelper.reset(*deviceQuad, localQuad);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400346
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500347 // Edge inset/outset distance ordered LBTR, set to 0.5 for a half pixel if the AA flag
348 // is turned on, or 0.0 if the edge is not anti-aliased.
349 skvx::Vec<4, float> edgeDistances;
350 if (aaFlags == GrQuadAAFlags::kAll) {
351 edgeDistances = 0.5f;
352 } else {
353 edgeDistances = { (aaFlags & GrQuadAAFlags::kLeft) ? 0.5f : 0.f,
354 (aaFlags & GrQuadAAFlags::kBottom) ? 0.5f : 0.f,
355 (aaFlags & GrQuadAAFlags::kTop) ? 0.5f : 0.f,
356 (aaFlags & GrQuadAAFlags::kRight) ? 0.5f : 0.f };
357 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400358
Michael Ludwig73dbea62019-11-19 14:55:36 -0500359 // Write inner vertices first
360 float coverage[4];
Michael Ludwig704d5402019-11-25 09:43:37 -0500361 fAAHelper.inset(edgeDistances, deviceQuad, localQuad).store(coverage);
362 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, coverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400363 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500364
Michael Ludwig290d6df2020-10-27 09:28:59 -0400365 // Then outer vertices, which use 0.f for their coverage. If the inset was degenerate
366 // to a line (had all coverages < 1), tweak the outset distance so the outer frame's
367 // narrow axis reaches out to 2px, which gives better animation under translation.
368 if (coverage[0] < 1.f && coverage[1] < 1.f && coverage[2] < 1.f && coverage[3] < 1.f) {
369 skvx::Vec<4, float> len = fAAHelper.getEdgeLengths();
370 // Using max guards us against trying to scale a degenerate triangle edge of 0 len
371 // up to 2px. The shuffles are so that edge 0's adjustment is based on the lengths
372 // of its connecting edges (1 and 2), and so forth.
373 skvx::Vec<4, float> maxWH = max(skvx::shuffle<1, 0, 3, 2>(len),
374 skvx::shuffle<2, 3, 0, 1>(len));
375 // wh + 2e' = 2, so e' = (2 - wh) / 2 => e' = e * (2 - wh). But if w or h > 1, then
376 // 2 - wh < 1 and represents the non-narrow axis so clamp to 1.
377 edgeDistances *= max(1.f, 2.f - maxWH);
378 }
Michael Ludwig704d5402019-11-25 09:43:37 -0500379 fAAHelper.outset(edgeDistances, deviceQuad, localQuad);
380 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400381 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500382 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500383 } else {
384 // No outsetting needed, just write a single quad with full coverage
Michael Ludwig73dbea62019-11-19 14:55:36 -0500385 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kNone &&
Brian Salomon2432d062020-04-16 20:48:09 -0400386 !fVertexSpec.requiresGeometrySubset());
Michael Ludwig189c9802019-11-21 11:21:12 -0500387 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400388 kIgnoredSubset, uvSubset);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400389 }
390}
Michael Ludwig20e909e2018-10-30 10:43:57 -0400391
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400392sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawOp::Target* target,
393 IndexBufferOption indexBufferOption) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400394 auto resourceProvider = target->resourceProvider();
395
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400396 switch (indexBufferOption) {
397 case IndexBufferOption::kPictureFramed: return resourceProvider->refAAQuadIndexBuffer();
398 case IndexBufferOption::kIndexedRects: return resourceProvider->refNonAAQuadIndexBuffer();
399 case IndexBufferOption::kTriStrips: // fall through
400 default: return nullptr;
401 }
402}
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400403
Robert Phillips438d9862019-11-14 12:46:05 -0500404int QuadLimit(IndexBufferOption option) {
405 switch (option) {
406 case IndexBufferOption::kPictureFramed: return GrResourceProvider::MaxNumAAQuads();
407 case IndexBufferOption::kIndexedRects: return GrResourceProvider::MaxNumNonAAQuads();
408 case IndexBufferOption::kTriStrips: return SK_MaxS32; // not limited by an indexBuffer
409 }
410
411 SkUNREACHABLE;
412}
Michael Ludwig93aeba02018-12-21 09:50:31 -0500413
Chris Daltondbb833b2020-03-17 12:15:46 -0600414void IssueDraw(const GrCaps& caps, GrOpsRenderPass* renderPass, const VertexSpec& spec,
415 int runningQuadCount, int quadsInDraw, int maxVerts, int absVertBufferOffset) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400416 if (spec.indexBufferOption() == IndexBufferOption::kTriStrips) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400417 int offset = absVertBufferOffset +
418 runningQuadCount * GrResourceProvider::NumVertsPerNonAAQuad();
Chris Daltondbb833b2020-03-17 12:15:46 -0600419 renderPass->draw(4, offset);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400420 return;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500421 }
422
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400423 SkASSERT(spec.indexBufferOption() == IndexBufferOption::kPictureFramed ||
424 spec.indexBufferOption() == IndexBufferOption::kIndexedRects);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400425
Robert Phillips2f05a482019-11-25 09:54:43 -0500426 int maxNumQuads, numIndicesPerQuad, numVertsPerQuad;
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400427
428 if (spec.indexBufferOption() == IndexBufferOption::kPictureFramed) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400429 // AA uses 8 vertices and 30 indices per quad, basically nested rectangles
Robert Phillips2f05a482019-11-25 09:54:43 -0500430 maxNumQuads = GrResourceProvider::MaxNumAAQuads();
431 numIndicesPerQuad = GrResourceProvider::NumIndicesPerAAQuad();
432 numVertsPerQuad = GrResourceProvider::NumVertsPerAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400433 } else {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400434 // Non-AA uses 4 vertices and 6 indices per quad
Robert Phillips2f05a482019-11-25 09:54:43 -0500435 maxNumQuads = GrResourceProvider::MaxNumNonAAQuads();
436 numIndicesPerQuad = GrResourceProvider::NumIndicesPerNonAAQuad();
437 numVertsPerQuad = GrResourceProvider::NumVertsPerNonAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400438 }
439
Robert Phillips2f05a482019-11-25 09:54:43 -0500440 SkASSERT(runningQuadCount + quadsInDraw <= maxNumQuads);
441
442 if (caps.avoidLargeIndexBufferDraws()) {
443 // When we need to avoid large index buffer draws we modify the base vertex of the draw
444 // which, in GL, requires rebinding all vertex attrib arrays, so a base index is generally
445 // preferred.
446 int offset = absVertBufferOffset + runningQuadCount * numVertsPerQuad;
447
Chris Daltondbb833b2020-03-17 12:15:46 -0600448 renderPass->drawIndexPattern(numIndicesPerQuad, quadsInDraw, maxNumQuads, numVertsPerQuad,
449 offset);
Robert Phillips2f05a482019-11-25 09:54:43 -0500450 } else {
451 int baseIndex = runningQuadCount * numIndicesPerQuad;
452 int numIndicesToDraw = quadsInDraw * numIndicesPerQuad;
453
454 int minVertex = runningQuadCount * numVertsPerQuad;
455 int maxVertex = (runningQuadCount + quadsInDraw) * numVertsPerQuad;
456
Chris Daltondbb833b2020-03-17 12:15:46 -0600457 renderPass->drawIndexed(numIndicesToDraw, baseIndex, minVertex, maxVertex,
458 absVertBufferOffset);
Robert Phillips2f05a482019-11-25 09:54:43 -0500459 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500460}
461
Michael Ludwigc182b942018-11-16 10:27:51 -0500462////////////////// VertexSpec Implementation
Michael Ludwig20e909e2018-10-30 10:43:57 -0400463
Michael Ludwigc182b942018-11-16 10:27:51 -0500464int VertexSpec::deviceDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400465 return this->deviceQuadType() == GrQuad::Type::kPerspective ? 3 : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -0500466}
467
468int VertexSpec::localDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400469 return fHasLocalCoords ? (this->localQuadType() == GrQuad::Type::kPerspective ? 3 : 2) : 0;
Michael Ludwigc182b942018-11-16 10:27:51 -0500470}
471
Robert Phillips29f38542019-10-16 09:20:25 -0400472CoverageMode VertexSpec::coverageMode() const {
473 if (this->usesCoverageAA()) {
474 if (this->compatibleWithCoverageAsAlpha() && this->hasVertexColors() &&
Brian Salomon2432d062020-04-16 20:48:09 -0400475 !this->requiresGeometrySubset()) {
476 // Using a geometric subset acts as a second source of coverage and folding
Robert Phillips29f38542019-10-16 09:20:25 -0400477 // the original coverage into color makes it impossible to apply the color's
Brian Salomon2432d062020-04-16 20:48:09 -0400478 // alpha to the geometric subset's coverage when the original shape is clipped.
Robert Phillips29f38542019-10-16 09:20:25 -0400479 return CoverageMode::kWithColor;
480 } else {
481 return CoverageMode::kWithPosition;
482 }
483 } else {
484 return CoverageMode::kNone;
485 }
486}
487
488// This needs to stay in sync w/ QuadPerEdgeAAGeometryProcessor::initializeAttrs
489size_t VertexSpec::vertexSize() const {
490 bool needsPerspective = (this->deviceDimensionality() == 3);
491 CoverageMode coverageMode = this->coverageMode();
492
493 size_t count = 0;
494
495 if (coverageMode == CoverageMode::kWithPosition) {
496 if (needsPerspective) {
497 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
498 } else {
499 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType) +
500 GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
501 }
502 } else {
503 if (needsPerspective) {
504 count += GrVertexAttribTypeSize(kFloat3_GrVertexAttribType);
505 } else {
506 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType);
507 }
508 }
509
Brian Salomon2432d062020-04-16 20:48:09 -0400510 if (this->requiresGeometrySubset()) {
Robert Phillips29f38542019-10-16 09:20:25 -0400511 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
512 }
513
514 count += this->localDimensionality() * GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
515
516 if (ColorType::kByte == this->colorType()) {
517 count += GrVertexAttribTypeSize(kUByte4_norm_GrVertexAttribType);
Brian Osman2715bf52019-12-06 14:38:47 -0500518 } else if (ColorType::kFloat == this->colorType()) {
519 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
Robert Phillips29f38542019-10-16 09:20:25 -0400520 }
521
Brian Salomon2432d062020-04-16 20:48:09 -0400522 if (this->hasSubset()) {
Robert Phillips29f38542019-10-16 09:20:25 -0400523 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
524 }
525
526 return count;
527}
528
Michael Ludwig467994d2018-12-03 14:58:31 +0000529////////////////// Geometry Processor Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500530
Michael Ludwig467994d2018-12-03 14:58:31 +0000531class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor {
532public:
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400533 using Saturate = GrTextureOp::Saturate;
Michael Ludwig20e909e2018-10-30 10:43:57 -0400534
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500535 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const VertexSpec& spec) {
536 return arena->make<QuadPerEdgeAAGeometryProcessor>(spec);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400537 }
538
Brian Salomonccb61422020-01-09 10:46:36 -0500539 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
540 const VertexSpec& vertexSpec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500541 const GrShaderCaps& caps,
542 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500543 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500544 const GrSwizzle& swizzle,
545 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
546 Saturate saturate) {
547 return arena->make<QuadPerEdgeAAGeometryProcessor>(
Robert Phillips323471e2019-11-11 11:33:37 -0500548 vertexSpec, caps, backendFormat, samplerState, swizzle,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500549 std::move(textureColorSpaceXform), saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400550 }
551
Michael Ludwig467994d2018-12-03 14:58:31 +0000552 const char* name() const override { return "QuadPerEdgeAAGeometryProcessor"; }
Michael Ludwig024e2622018-11-30 13:25:55 -0500553
Michael Ludwig467994d2018-12-03 14:58:31 +0000554 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400555 // texturing, device-dimensions are single bit flags
Brian Salomon2432d062020-04-16 20:48:09 -0400556 uint32_t x = (fTexSubset.isInitialized() ? 0 : 0x1)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400557 | (fSampler.isInitialized() ? 0 : 0x2)
558 | (fNeedsPerspective ? 0 : 0x4)
559 | (fSaturate == Saturate::kNo ? 0 : 0x8);
Michael Ludwig467994d2018-12-03 14:58:31 +0000560 // local coords require 2 bits (3 choices), 00 for none, 01 for 2d, 10 for 3d
561 if (fLocalCoord.isInitialized()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400562 x |= kFloat3_GrVertexAttribType == fLocalCoord.cpuType() ? 0x10 : 0x20;
Brian Osman78dc72c2018-12-03 13:20:43 +0000563 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000564 // similar for colors, 00 for none, 01 for bytes, 10 for half-floats
Michael Ludwig93aeba02018-12-21 09:50:31 -0500565 if (fColor.isInitialized()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400566 x |= kUByte4_norm_GrVertexAttribType == fColor.cpuType() ? 0x40 : 0x80;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500567 }
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400568 // and coverage mode, 00 for none, 01 for withposition, 10 for withcolor, 11 for
Brian Salomon2432d062020-04-16 20:48:09 -0400569 // position+geomsubset
570 SkASSERT(!fGeomSubset.isInitialized() || fCoverageMode == CoverageMode::kWithPosition);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500571 if (fCoverageMode != CoverageMode::kNone) {
Brian Salomon2432d062020-04-16 20:48:09 -0400572 x |= fGeomSubset.isInitialized()
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400573 ? 0x300
574 : (CoverageMode::kWithPosition == fCoverageMode ? 0x100 : 0x200);
Michael Ludwig467994d2018-12-03 14:58:31 +0000575 }
576
577 b->add32(GrColorSpaceXform::XformKey(fTextureColorSpaceXform.get()));
578 b->add32(x);
Brian Osman78dc72c2018-12-03 13:20:43 +0000579 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000580
581 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
582 class GLSLProcessor : public GrGLSLGeometryProcessor {
583 public:
Brian Osman609f1592020-07-01 15:14:39 -0400584 void setData(const GrGLSLProgramDataManager& pdman,
585 const GrPrimitiveProcessor& proc) override {
Michael Ludwig467994d2018-12-03 14:58:31 +0000586 const auto& gp = proc.cast<QuadPerEdgeAAGeometryProcessor>();
Michael Ludwig467994d2018-12-03 14:58:31 +0000587 fTextureColorSpaceXformHelper.setData(pdman, gp.fTextureColorSpaceXform.get());
588 }
589
590 private:
591 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
592 using Interpolation = GrGLSLVaryingHandler::Interpolation;
593
594 const auto& gp = args.fGP.cast<QuadPerEdgeAAGeometryProcessor>();
595 fTextureColorSpaceXformHelper.emitCode(args.fUniformHandler,
596 gp.fTextureColorSpaceXform.get());
597
598 args.fVaryingHandler->emitAttributes(gp);
599
Michael Ludwig93aeba02018-12-21 09:50:31 -0500600 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
601 // Strip last channel from the vertex attribute to remove coverage and get the
602 // actual position
603 if (gp.fNeedsPerspective) {
604 args.fVertBuilder->codeAppendf("float3 position = %s.xyz;",
605 gp.fPosition.name());
606 } else {
607 args.fVertBuilder->codeAppendf("float2 position = %s.xy;",
608 gp.fPosition.name());
609 }
610 gpArgs->fPositionVar = {"position",
611 gp.fNeedsPerspective ? kFloat3_GrSLType
612 : kFloat2_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400613 GrShaderVar::TypeModifier::None};
Michael Ludwig467994d2018-12-03 14:58:31 +0000614 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500615 // No coverage to eliminate
616 gpArgs->fPositionVar = gp.fPosition.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000617 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000618
Michael Ludwig553db622020-06-19 10:47:30 -0400619 // This attribute will be uninitialized if earlier FP analysis determined no
620 // local coordinates are needed (and this will not include the inline texture
621 // fetch this GP does before invoking FPs).
622 gpArgs->fLocalCoordVar = gp.fLocalCoord.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000623
624 // Solid color before any texturing gets modulated in
625 if (gp.fColor.isInitialized()) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400626 SkASSERT(gp.fCoverageMode != CoverageMode::kWithColor || !gp.fNeedsPerspective);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500627 // The color cannot be flat if the varying coverage has been modulated into it
Michael Ludwig467994d2018-12-03 14:58:31 +0000628 args.fVaryingHandler->addPassThroughAttribute(gp.fColor, args.fOutputColor,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500629 gp.fCoverageMode == CoverageMode::kWithColor ?
630 Interpolation::kInterpolated : Interpolation::kCanBeFlat);
631 } else {
632 // Output color must be initialized to something
633 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
Michael Ludwig467994d2018-12-03 14:58:31 +0000634 }
635
636 // If there is a texture, must also handle texture coordinates and reading from
637 // the texture in the fragment shader before continuing to fragment processors.
638 if (gp.fSampler.isInitialized()) {
Brian Salomon2432d062020-04-16 20:48:09 -0400639 // Texture coordinates clamped by the subset on the fragment shader; if the GP
Michael Ludwig467994d2018-12-03 14:58:31 +0000640 // has a texture, it's guaranteed to have local coordinates
641 args.fFragBuilder->codeAppend("float2 texCoord;");
642 if (gp.fLocalCoord.cpuType() == kFloat3_GrVertexAttribType) {
643 // Can't do a pass through since we need to perform perspective division
644 GrGLSLVarying v(gp.fLocalCoord.gpuType());
645 args.fVaryingHandler->addVarying(gp.fLocalCoord.name(), &v);
646 args.fVertBuilder->codeAppendf("%s = %s;",
647 v.vsOut(), gp.fLocalCoord.name());
648 args.fFragBuilder->codeAppendf("texCoord = %s.xy / %s.z;",
649 v.fsIn(), v.fsIn());
650 } else {
651 args.fVaryingHandler->addPassThroughAttribute(gp.fLocalCoord, "texCoord");
652 }
653
Brian Salomon2432d062020-04-16 20:48:09 -0400654 // Clamp the now 2D localCoordName variable by the subset if it is provided
655 if (gp.fTexSubset.isInitialized()) {
656 args.fFragBuilder->codeAppend("float4 subset;");
657 args.fVaryingHandler->addPassThroughAttribute(gp.fTexSubset, "subset",
Michael Ludwig467994d2018-12-03 14:58:31 +0000658 Interpolation::kCanBeFlat);
659 args.fFragBuilder->codeAppend(
Brian Salomon2432d062020-04-16 20:48:09 -0400660 "texCoord = clamp(texCoord, subset.xy, subset.zw);");
Michael Ludwig467994d2018-12-03 14:58:31 +0000661 }
662
663 // Now modulate the starting output color by the texture lookup
664 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
Brian Salomon87e9ddb2019-12-19 14:50:22 -0500665 args.fFragBuilder->appendTextureLookupAndBlend(
666 args.fOutputColor, SkBlendMode::kModulate, args.fTexSamplers[0],
Brian Salomond19cd762020-01-06 13:16:31 -0500667 "texCoord", &fTextureColorSpaceXformHelper);
Michael Ludwig467994d2018-12-03 14:58:31 +0000668 args.fFragBuilder->codeAppend(";");
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400669 if (gp.fSaturate == Saturate::kYes) {
670 args.fFragBuilder->codeAppendf("%s = saturate(%s);",
671 args.fOutputColor, args.fOutputColor);
672 }
673 } else {
674 // Saturate is only intended for use with a proxy to account for the fact
675 // that GrTextureOp skips SkPaint conversion, which normally handles this.
676 SkASSERT(gp.fSaturate == Saturate::kNo);
Michael Ludwig467994d2018-12-03 14:58:31 +0000677 }
678
679 // And lastly, output the coverage calculation code
Michael Ludwig93aeba02018-12-21 09:50:31 -0500680 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
681 GrGLSLVarying coverage(kFloat_GrSLType);
682 args.fVaryingHandler->addVarying("coverage", &coverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000683 if (gp.fNeedsPerspective) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400684 // Multiply by "W" in the vertex shader, then by 1/w (sk_FragCoord.w) in
685 // the fragment shader to get screen-space linear coverage.
686 args.fVertBuilder->codeAppendf("%s = %s.w * %s.z;",
687 coverage.vsOut(), gp.fPosition.name(),
688 gp.fPosition.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400689 args.fFragBuilder->codeAppendf("float coverage = %s * sk_FragCoord.w;",
690 coverage.fsIn());
Michael Ludwig93aeba02018-12-21 09:50:31 -0500691 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400692 args.fVertBuilder->codeAppendf("%s = %s;",
693 coverage.vsOut(), gp.fCoverage.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400694 args.fFragBuilder->codeAppendf("float coverage = %s;", coverage.fsIn());
Michael Ludwig467994d2018-12-03 14:58:31 +0000695 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500696
Brian Salomon2432d062020-04-16 20:48:09 -0400697 if (gp.fGeomSubset.isInitialized()) {
698 // Calculate distance from sk_FragCoord to the 4 edges of the subset
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400699 // and clamp them to (0, 1). Use the minimum of these and the original
700 // coverage. This only has to be done in the exterior triangles, the
Brian Salomon2432d062020-04-16 20:48:09 -0400701 // interior of the quad geometry can never be clipped by the subset box.
702 args.fFragBuilder->codeAppend("float4 geoSubset;");
703 args.fVaryingHandler->addPassThroughAttribute(gp.fGeomSubset, "geoSubset",
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400704 Interpolation::kCanBeFlat);
705 args.fFragBuilder->codeAppend(
706 "if (coverage < 0.5) {"
707 " float4 dists4 = clamp(float4(1, 1, -1, -1) * "
Brian Salomon2432d062020-04-16 20:48:09 -0400708 "(sk_FragCoord.xyxy - geoSubset), 0, 1);"
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400709 " float2 dists2 = dists4.xy * dists4.zw;"
710 " coverage = min(coverage, dists2.x * dists2.y);"
711 "}");
712 }
713
714 args.fFragBuilder->codeAppendf("%s = half4(half(coverage));",
715 args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000716 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500717 // Set coverage to 1, since it's either non-AA or the coverage was already
718 // folded into the output color
Brian Salomon2432d062020-04-16 20:48:09 -0400719 SkASSERT(!gp.fGeomSubset.isInitialized());
Ethan Nicholase1f55022019-02-05 17:17:40 -0500720 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000721 }
722 }
723 GrGLSLColorSpaceXformHelper fTextureColorSpaceXformHelper;
724 };
725 return new GLSLProcessor;
726 }
727
728private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500729 friend class ::SkArenaAlloc; // for access to ctor
730
Michael Ludwig467994d2018-12-03 14:58:31 +0000731 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec)
732 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
733 , fTextureColorSpaceXform(nullptr) {
Brian Salomon2432d062020-04-16 20:48:09 -0400734 SkASSERT(!spec.hasSubset());
Michael Ludwig467994d2018-12-03 14:58:31 +0000735 this->initializeAttrs(spec);
736 this->setTextureSamplerCnt(0);
737 }
738
Brian Salomon67529b22019-08-13 15:31:04 -0400739 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec,
740 const GrShaderCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400741 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500742 GrSamplerState samplerState,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400743 const GrSwizzle& swizzle,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400744 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
745 Saturate saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000746 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400747 , fSaturate(saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000748 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Robert Phillips323471e2019-11-11 11:33:37 -0500749 , fSampler(samplerState, backendFormat, swizzle) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500750 SkASSERT(spec.hasLocalCoords());
Michael Ludwig467994d2018-12-03 14:58:31 +0000751 this->initializeAttrs(spec);
752 this->setTextureSamplerCnt(1);
753 }
754
Robert Phillips29f38542019-10-16 09:20:25 -0400755 // This needs to stay in sync w/ VertexSpec::vertexSize
Michael Ludwig467994d2018-12-03 14:58:31 +0000756 void initializeAttrs(const VertexSpec& spec) {
757 fNeedsPerspective = spec.deviceDimensionality() == 3;
Robert Phillips29f38542019-10-16 09:20:25 -0400758 fCoverageMode = spec.coverageMode();
Michael Ludwig93aeba02018-12-21 09:50:31 -0500759
760 if (fCoverageMode == CoverageMode::kWithPosition) {
761 if (fNeedsPerspective) {
762 fPosition = {"positionWithCoverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
763 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400764 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
765 fCoverage = {"coverage", kFloat_GrVertexAttribType, kFloat_GrSLType};
Michael Ludwig93aeba02018-12-21 09:50:31 -0500766 }
767 } else {
768 if (fNeedsPerspective) {
769 fPosition = {"position", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
770 } else {
771 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
772 }
773 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000774
Brian Salomon2432d062020-04-16 20:48:09 -0400775 // Need a geometry subset when the quads are AA and not rectilinear, since their AA
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400776 // outsetting can go beyond a half pixel.
Brian Salomon2432d062020-04-16 20:48:09 -0400777 if (spec.requiresGeometrySubset()) {
778 fGeomSubset = {"geomSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400779 }
780
Michael Ludwig467994d2018-12-03 14:58:31 +0000781 int localDim = spec.localDimensionality();
782 if (localDim == 3) {
783 fLocalCoord = {"localCoord", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
784 } else if (localDim == 2) {
785 fLocalCoord = {"localCoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
786 } // else localDim == 0 and attribute remains uninitialized
787
Brian Osman2715bf52019-12-06 14:38:47 -0500788 if (spec.hasVertexColors()) {
789 fColor = MakeColorAttribute("color", ColorType::kFloat == spec.colorType());
Michael Ludwig467994d2018-12-03 14:58:31 +0000790 }
791
Brian Salomon2432d062020-04-16 20:48:09 -0400792 if (spec.hasSubset()) {
793 fTexSubset = {"texSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwig467994d2018-12-03 14:58:31 +0000794 }
795
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400796 this->setVertexAttributes(&fPosition, 6);
Michael Ludwig467994d2018-12-03 14:58:31 +0000797 }
798
799 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
800
Michael Ludwig93aeba02018-12-21 09:50:31 -0500801 Attribute fPosition; // May contain coverage as last channel
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400802 Attribute fCoverage; // Used for non-perspective position to avoid Intel Metal issues
Michael Ludwig93aeba02018-12-21 09:50:31 -0500803 Attribute fColor; // May have coverage modulated in if the FPs support it
Michael Ludwig467994d2018-12-03 14:58:31 +0000804 Attribute fLocalCoord;
Brian Salomon2432d062020-04-16 20:48:09 -0400805 Attribute fGeomSubset; // Screen-space bounding box on geometry+aa outset
806 Attribute fTexSubset; // Texture-space bounding box on local coords
Michael Ludwig467994d2018-12-03 14:58:31 +0000807
Michael Ludwig93aeba02018-12-21 09:50:31 -0500808 // The positions attribute may have coverage built into it, so float3 is an ambiguous type
809 // and may mean 2d with coverage, or 3d with no coverage
Michael Ludwig467994d2018-12-03 14:58:31 +0000810 bool fNeedsPerspective;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400811 // Should saturate() be called on the color? Only relevant when created with a texture.
812 Saturate fSaturate = Saturate::kNo;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500813 CoverageMode fCoverageMode;
Michael Ludwig467994d2018-12-03 14:58:31 +0000814
815 // Color space will be null and fSampler.isInitialized() returns false when the GP is configured
816 // to skip texturing.
817 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
818 TextureSampler fSampler;
819
John Stiles7571f9e2020-09-02 22:42:33 -0400820 using INHERITED = GrGeometryProcessor;
Michael Ludwig467994d2018-12-03 14:58:31 +0000821};
822
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500823GrGeometryProcessor* MakeProcessor(SkArenaAlloc* arena, const VertexSpec& spec) {
824 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec);
Michael Ludwig467994d2018-12-03 14:58:31 +0000825}
826
Brian Salomonccb61422020-01-09 10:46:36 -0500827GrGeometryProcessor* MakeTexturedProcessor(SkArenaAlloc* arena,
828 const VertexSpec& spec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500829 const GrShaderCaps& caps,
830 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500831 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500832 const GrSwizzle& swizzle,
833 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
Brian Salomonccb61422020-01-09 10:46:36 -0500834 Saturate saturate) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500835 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec, caps, backendFormat, samplerState,
836 swizzle, std::move(textureColorSpaceXform),
837 saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400838}
Michael Ludwigc182b942018-11-16 10:27:51 -0500839
840} // namespace GrQuadPerEdgeAA