blob: 51eaf634cff47e7a1aef8076d018fe070d7b399e [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()) {
Brian Salomon659e71f2021-02-24 10:09:02 -0500330#ifdef SK_USE_LEGACY_AA_QUAD_SUBSET
Brian Salomon2432d062020-04-16 20:48:09 -0400331 geomSubset = deviceQuad->bounds();
332 geomSubset.outset(0.5f, 0.5f); // account for AA expansion
Brian Salomon659e71f2021-02-24 10:09:02 -0500333#else
334 // Our GP code expects a 0.5 outset rect (coverage is computed as 0 at the values of
335 // the uniform). However, if we have quad edges that aren't supposed to be antialiased
336 // they may lie close to the bounds. So in that case we outset by an additional 0.5.
337 // This is a sort of backup clipping mechanism for cases where quad outsetting of nearly
338 // parallel edges produces long thin extrusions from the original geometry.
339 float outset = aaFlags == GrQuadAAFlags::kAll ? 0.5f : 1.f;
340 geomSubset = deviceQuad->bounds().makeOutset(outset, outset);
341#endif
Michael Ludwige6266a22019-03-07 11:24:32 -0500342 }
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400343
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500344 if (aaFlags == GrQuadAAFlags::kNone) {
345 // Have to write the coverage AA vertex structure, but there's no math to be done for a
346 // non-aa quad batched into a coverage AA op.
Michael Ludwig189c9802019-11-21 11:21:12 -0500347 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400348 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500349 // Since we pass the same corners in, the outer vertex structure will have 0 area and
350 // the coverage interpolation from 1 to 0 will not be visible.
Michael Ludwig189c9802019-11-21 11:21:12 -0500351 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400352 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500353 } else {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500354 // Reset the tessellation helper to match the current geometry
Michael Ludwig704d5402019-11-25 09:43:37 -0500355 fAAHelper.reset(*deviceQuad, localQuad);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400356
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500357 // Edge inset/outset distance ordered LBTR, set to 0.5 for a half pixel if the AA flag
358 // is turned on, or 0.0 if the edge is not anti-aliased.
359 skvx::Vec<4, float> edgeDistances;
360 if (aaFlags == GrQuadAAFlags::kAll) {
361 edgeDistances = 0.5f;
362 } else {
363 edgeDistances = { (aaFlags & GrQuadAAFlags::kLeft) ? 0.5f : 0.f,
364 (aaFlags & GrQuadAAFlags::kBottom) ? 0.5f : 0.f,
365 (aaFlags & GrQuadAAFlags::kTop) ? 0.5f : 0.f,
366 (aaFlags & GrQuadAAFlags::kRight) ? 0.5f : 0.f };
367 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400368
Michael Ludwig73dbea62019-11-19 14:55:36 -0500369 // Write inner vertices first
370 float coverage[4];
Michael Ludwig704d5402019-11-25 09:43:37 -0500371 fAAHelper.inset(edgeDistances, deviceQuad, localQuad).store(coverage);
372 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, coverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400373 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500374
Michael Ludwig290d6df2020-10-27 09:28:59 -0400375 // Then outer vertices, which use 0.f for their coverage. If the inset was degenerate
376 // to a line (had all coverages < 1), tweak the outset distance so the outer frame's
377 // narrow axis reaches out to 2px, which gives better animation under translation.
378 if (coverage[0] < 1.f && coverage[1] < 1.f && coverage[2] < 1.f && coverage[3] < 1.f) {
379 skvx::Vec<4, float> len = fAAHelper.getEdgeLengths();
380 // Using max guards us against trying to scale a degenerate triangle edge of 0 len
381 // up to 2px. The shuffles are so that edge 0's adjustment is based on the lengths
382 // of its connecting edges (1 and 2), and so forth.
383 skvx::Vec<4, float> maxWH = max(skvx::shuffle<1, 0, 3, 2>(len),
384 skvx::shuffle<2, 3, 0, 1>(len));
385 // wh + 2e' = 2, so e' = (2 - wh) / 2 => e' = e * (2 - wh). But if w or h > 1, then
386 // 2 - wh < 1 and represents the non-narrow axis so clamp to 1.
387 edgeDistances *= max(1.f, 2.f - maxWH);
388 }
Michael Ludwig704d5402019-11-25 09:43:37 -0500389 fAAHelper.outset(edgeDistances, deviceQuad, localQuad);
390 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400391 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500392 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500393 } else {
394 // No outsetting needed, just write a single quad with full coverage
Michael Ludwig73dbea62019-11-19 14:55:36 -0500395 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kNone &&
Brian Salomon2432d062020-04-16 20:48:09 -0400396 !fVertexSpec.requiresGeometrySubset());
Michael Ludwig189c9802019-11-21 11:21:12 -0500397 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400398 kIgnoredSubset, uvSubset);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400399 }
400}
Michael Ludwig20e909e2018-10-30 10:43:57 -0400401
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400402sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawOp::Target* target,
403 IndexBufferOption indexBufferOption) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400404 auto resourceProvider = target->resourceProvider();
405
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400406 switch (indexBufferOption) {
407 case IndexBufferOption::kPictureFramed: return resourceProvider->refAAQuadIndexBuffer();
408 case IndexBufferOption::kIndexedRects: return resourceProvider->refNonAAQuadIndexBuffer();
409 case IndexBufferOption::kTriStrips: // fall through
410 default: return nullptr;
411 }
412}
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400413
Robert Phillips438d9862019-11-14 12:46:05 -0500414int QuadLimit(IndexBufferOption option) {
415 switch (option) {
416 case IndexBufferOption::kPictureFramed: return GrResourceProvider::MaxNumAAQuads();
417 case IndexBufferOption::kIndexedRects: return GrResourceProvider::MaxNumNonAAQuads();
418 case IndexBufferOption::kTriStrips: return SK_MaxS32; // not limited by an indexBuffer
419 }
420
421 SkUNREACHABLE;
422}
Michael Ludwig93aeba02018-12-21 09:50:31 -0500423
Chris Daltondbb833b2020-03-17 12:15:46 -0600424void IssueDraw(const GrCaps& caps, GrOpsRenderPass* renderPass, const VertexSpec& spec,
425 int runningQuadCount, int quadsInDraw, int maxVerts, int absVertBufferOffset) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400426 if (spec.indexBufferOption() == IndexBufferOption::kTriStrips) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400427 int offset = absVertBufferOffset +
428 runningQuadCount * GrResourceProvider::NumVertsPerNonAAQuad();
Chris Daltondbb833b2020-03-17 12:15:46 -0600429 renderPass->draw(4, offset);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400430 return;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500431 }
432
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400433 SkASSERT(spec.indexBufferOption() == IndexBufferOption::kPictureFramed ||
434 spec.indexBufferOption() == IndexBufferOption::kIndexedRects);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400435
Robert Phillips2f05a482019-11-25 09:54:43 -0500436 int maxNumQuads, numIndicesPerQuad, numVertsPerQuad;
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400437
438 if (spec.indexBufferOption() == IndexBufferOption::kPictureFramed) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400439 // AA uses 8 vertices and 30 indices per quad, basically nested rectangles
Robert Phillips2f05a482019-11-25 09:54:43 -0500440 maxNumQuads = GrResourceProvider::MaxNumAAQuads();
441 numIndicesPerQuad = GrResourceProvider::NumIndicesPerAAQuad();
442 numVertsPerQuad = GrResourceProvider::NumVertsPerAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400443 } else {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400444 // Non-AA uses 4 vertices and 6 indices per quad
Robert Phillips2f05a482019-11-25 09:54:43 -0500445 maxNumQuads = GrResourceProvider::MaxNumNonAAQuads();
446 numIndicesPerQuad = GrResourceProvider::NumIndicesPerNonAAQuad();
447 numVertsPerQuad = GrResourceProvider::NumVertsPerNonAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400448 }
449
Robert Phillips2f05a482019-11-25 09:54:43 -0500450 SkASSERT(runningQuadCount + quadsInDraw <= maxNumQuads);
451
452 if (caps.avoidLargeIndexBufferDraws()) {
453 // When we need to avoid large index buffer draws we modify the base vertex of the draw
454 // which, in GL, requires rebinding all vertex attrib arrays, so a base index is generally
455 // preferred.
456 int offset = absVertBufferOffset + runningQuadCount * numVertsPerQuad;
457
Chris Daltondbb833b2020-03-17 12:15:46 -0600458 renderPass->drawIndexPattern(numIndicesPerQuad, quadsInDraw, maxNumQuads, numVertsPerQuad,
459 offset);
Robert Phillips2f05a482019-11-25 09:54:43 -0500460 } else {
461 int baseIndex = runningQuadCount * numIndicesPerQuad;
462 int numIndicesToDraw = quadsInDraw * numIndicesPerQuad;
463
464 int minVertex = runningQuadCount * numVertsPerQuad;
465 int maxVertex = (runningQuadCount + quadsInDraw) * numVertsPerQuad;
466
Chris Daltondbb833b2020-03-17 12:15:46 -0600467 renderPass->drawIndexed(numIndicesToDraw, baseIndex, minVertex, maxVertex,
468 absVertBufferOffset);
Robert Phillips2f05a482019-11-25 09:54:43 -0500469 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500470}
471
Michael Ludwigc182b942018-11-16 10:27:51 -0500472////////////////// VertexSpec Implementation
Michael Ludwig20e909e2018-10-30 10:43:57 -0400473
Michael Ludwigc182b942018-11-16 10:27:51 -0500474int VertexSpec::deviceDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400475 return this->deviceQuadType() == GrQuad::Type::kPerspective ? 3 : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -0500476}
477
478int VertexSpec::localDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400479 return fHasLocalCoords ? (this->localQuadType() == GrQuad::Type::kPerspective ? 3 : 2) : 0;
Michael Ludwigc182b942018-11-16 10:27:51 -0500480}
481
Robert Phillips29f38542019-10-16 09:20:25 -0400482CoverageMode VertexSpec::coverageMode() const {
483 if (this->usesCoverageAA()) {
484 if (this->compatibleWithCoverageAsAlpha() && this->hasVertexColors() &&
Brian Salomon2432d062020-04-16 20:48:09 -0400485 !this->requiresGeometrySubset()) {
486 // Using a geometric subset acts as a second source of coverage and folding
Robert Phillips29f38542019-10-16 09:20:25 -0400487 // the original coverage into color makes it impossible to apply the color's
Brian Salomon2432d062020-04-16 20:48:09 -0400488 // alpha to the geometric subset's coverage when the original shape is clipped.
Robert Phillips29f38542019-10-16 09:20:25 -0400489 return CoverageMode::kWithColor;
490 } else {
491 return CoverageMode::kWithPosition;
492 }
493 } else {
494 return CoverageMode::kNone;
495 }
496}
497
498// This needs to stay in sync w/ QuadPerEdgeAAGeometryProcessor::initializeAttrs
499size_t VertexSpec::vertexSize() const {
500 bool needsPerspective = (this->deviceDimensionality() == 3);
501 CoverageMode coverageMode = this->coverageMode();
502
503 size_t count = 0;
504
505 if (coverageMode == CoverageMode::kWithPosition) {
506 if (needsPerspective) {
507 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
508 } else {
509 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType) +
510 GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
511 }
512 } else {
513 if (needsPerspective) {
514 count += GrVertexAttribTypeSize(kFloat3_GrVertexAttribType);
515 } else {
516 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType);
517 }
518 }
519
Brian Salomon2432d062020-04-16 20:48:09 -0400520 if (this->requiresGeometrySubset()) {
Robert Phillips29f38542019-10-16 09:20:25 -0400521 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
522 }
523
524 count += this->localDimensionality() * GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
525
526 if (ColorType::kByte == this->colorType()) {
527 count += GrVertexAttribTypeSize(kUByte4_norm_GrVertexAttribType);
Brian Osman2715bf52019-12-06 14:38:47 -0500528 } else if (ColorType::kFloat == this->colorType()) {
529 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
Robert Phillips29f38542019-10-16 09:20:25 -0400530 }
531
Brian Salomon2432d062020-04-16 20:48:09 -0400532 if (this->hasSubset()) {
Robert Phillips29f38542019-10-16 09:20:25 -0400533 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
534 }
535
536 return count;
537}
538
Michael Ludwig467994d2018-12-03 14:58:31 +0000539////////////////// Geometry Processor Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500540
Michael Ludwig467994d2018-12-03 14:58:31 +0000541class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor {
542public:
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400543 using Saturate = GrTextureOp::Saturate;
Michael Ludwig20e909e2018-10-30 10:43:57 -0400544
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500545 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const VertexSpec& spec) {
Mike Kleinf1241082020-12-14 15:59:09 -0600546 return arena->make([&](void* ptr) {
547 return new (ptr) QuadPerEdgeAAGeometryProcessor(spec);
548 });
Michael Ludwig20e909e2018-10-30 10:43:57 -0400549 }
550
Brian Salomonccb61422020-01-09 10:46:36 -0500551 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
552 const VertexSpec& vertexSpec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500553 const GrShaderCaps& caps,
554 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500555 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500556 const GrSwizzle& swizzle,
557 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
558 Saturate saturate) {
Mike Kleinf1241082020-12-14 15:59:09 -0600559 return arena->make([&](void* ptr) {
560 return new (ptr) QuadPerEdgeAAGeometryProcessor(
561 vertexSpec, caps, backendFormat, samplerState, swizzle,
562 std::move(textureColorSpaceXform), saturate);
563 });
Michael Ludwig20e909e2018-10-30 10:43:57 -0400564 }
565
Michael Ludwig467994d2018-12-03 14:58:31 +0000566 const char* name() const override { return "QuadPerEdgeAAGeometryProcessor"; }
Michael Ludwig024e2622018-11-30 13:25:55 -0500567
Michael Ludwig467994d2018-12-03 14:58:31 +0000568 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400569 // texturing, device-dimensions are single bit flags
Brian Osman48d7f7c2021-03-03 13:38:08 -0500570 b->addBool(fTexSubset.isInitialized(), "subset");
571 b->addBool(fSampler.isInitialized(), "textured");
572 b->addBool(fNeedsPerspective, "perspective");
573 b->addBool((fSaturate == Saturate::kYes), "saturate");
574
575 b->addBool(fLocalCoord.isInitialized(), "hasLocalCoords");
Michael Ludwig467994d2018-12-03 14:58:31 +0000576 if (fLocalCoord.isInitialized()) {
Brian Osman48d7f7c2021-03-03 13:38:08 -0500577 // 2D (0) or 3D (1)
578 b->addBits(1, (kFloat3_GrVertexAttribType == fLocalCoord.cpuType()), "localCoordsType");
Brian Osman78dc72c2018-12-03 13:20:43 +0000579 }
Brian Osman48d7f7c2021-03-03 13:38:08 -0500580 b->addBool(fColor.isInitialized(), "hasColor");
Michael Ludwig93aeba02018-12-21 09:50:31 -0500581 if (fColor.isInitialized()) {
Brian Osman48d7f7c2021-03-03 13:38:08 -0500582 // bytes (0) or floats (1)
583 b->addBits(1, (kFloat4_GrVertexAttribType == fColor.cpuType()), "colorType");
Michael Ludwig93aeba02018-12-21 09:50:31 -0500584 }
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400585 // and coverage mode, 00 for none, 01 for withposition, 10 for withcolor, 11 for
Brian Salomon2432d062020-04-16 20:48:09 -0400586 // position+geomsubset
Brian Osman48d7f7c2021-03-03 13:38:08 -0500587 uint32_t coverageKey = 0;
Brian Salomon2432d062020-04-16 20:48:09 -0400588 SkASSERT(!fGeomSubset.isInitialized() || fCoverageMode == CoverageMode::kWithPosition);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500589 if (fCoverageMode != CoverageMode::kNone) {
Brian Osman48d7f7c2021-03-03 13:38:08 -0500590 coverageKey = fGeomSubset.isInitialized()
591 ? 0x3
592 : (CoverageMode::kWithPosition == fCoverageMode ? 0x1 : 0x2);
Michael Ludwig467994d2018-12-03 14:58:31 +0000593 }
Brian Osman48d7f7c2021-03-03 13:38:08 -0500594 b->addBits(2, coverageKey, "coverageMode");
Michael Ludwig467994d2018-12-03 14:58:31 +0000595
Brian Osman48d7f7c2021-03-03 13:38:08 -0500596 b->add32(GrColorSpaceXform::XformKey(fTextureColorSpaceXform.get()), "colorSpaceXform");
Brian Osman78dc72c2018-12-03 13:20:43 +0000597 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000598
599 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
600 class GLSLProcessor : public GrGLSLGeometryProcessor {
601 public:
Brian Osman609f1592020-07-01 15:14:39 -0400602 void setData(const GrGLSLProgramDataManager& pdman,
603 const GrPrimitiveProcessor& proc) override {
Michael Ludwig467994d2018-12-03 14:58:31 +0000604 const auto& gp = proc.cast<QuadPerEdgeAAGeometryProcessor>();
Michael Ludwig467994d2018-12-03 14:58:31 +0000605 fTextureColorSpaceXformHelper.setData(pdman, gp.fTextureColorSpaceXform.get());
606 }
607
608 private:
609 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
610 using Interpolation = GrGLSLVaryingHandler::Interpolation;
611
612 const auto& gp = args.fGP.cast<QuadPerEdgeAAGeometryProcessor>();
613 fTextureColorSpaceXformHelper.emitCode(args.fUniformHandler,
614 gp.fTextureColorSpaceXform.get());
615
616 args.fVaryingHandler->emitAttributes(gp);
617
Michael Ludwig93aeba02018-12-21 09:50:31 -0500618 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
619 // Strip last channel from the vertex attribute to remove coverage and get the
620 // actual position
621 if (gp.fNeedsPerspective) {
622 args.fVertBuilder->codeAppendf("float3 position = %s.xyz;",
623 gp.fPosition.name());
624 } else {
625 args.fVertBuilder->codeAppendf("float2 position = %s.xy;",
626 gp.fPosition.name());
627 }
628 gpArgs->fPositionVar = {"position",
629 gp.fNeedsPerspective ? kFloat3_GrSLType
630 : kFloat2_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400631 GrShaderVar::TypeModifier::None};
Michael Ludwig467994d2018-12-03 14:58:31 +0000632 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500633 // No coverage to eliminate
634 gpArgs->fPositionVar = gp.fPosition.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000635 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000636
Michael Ludwig553db622020-06-19 10:47:30 -0400637 // This attribute will be uninitialized if earlier FP analysis determined no
638 // local coordinates are needed (and this will not include the inline texture
639 // fetch this GP does before invoking FPs).
640 gpArgs->fLocalCoordVar = gp.fLocalCoord.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000641
642 // Solid color before any texturing gets modulated in
643 if (gp.fColor.isInitialized()) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400644 SkASSERT(gp.fCoverageMode != CoverageMode::kWithColor || !gp.fNeedsPerspective);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500645 // The color cannot be flat if the varying coverage has been modulated into it
Michael Ludwig467994d2018-12-03 14:58:31 +0000646 args.fVaryingHandler->addPassThroughAttribute(gp.fColor, args.fOutputColor,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500647 gp.fCoverageMode == CoverageMode::kWithColor ?
648 Interpolation::kInterpolated : Interpolation::kCanBeFlat);
649 } else {
650 // Output color must be initialized to something
651 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
Michael Ludwig467994d2018-12-03 14:58:31 +0000652 }
653
654 // If there is a texture, must also handle texture coordinates and reading from
655 // the texture in the fragment shader before continuing to fragment processors.
656 if (gp.fSampler.isInitialized()) {
Brian Salomon2432d062020-04-16 20:48:09 -0400657 // Texture coordinates clamped by the subset on the fragment shader; if the GP
Michael Ludwig467994d2018-12-03 14:58:31 +0000658 // has a texture, it's guaranteed to have local coordinates
659 args.fFragBuilder->codeAppend("float2 texCoord;");
660 if (gp.fLocalCoord.cpuType() == kFloat3_GrVertexAttribType) {
661 // Can't do a pass through since we need to perform perspective division
662 GrGLSLVarying v(gp.fLocalCoord.gpuType());
663 args.fVaryingHandler->addVarying(gp.fLocalCoord.name(), &v);
664 args.fVertBuilder->codeAppendf("%s = %s;",
665 v.vsOut(), gp.fLocalCoord.name());
666 args.fFragBuilder->codeAppendf("texCoord = %s.xy / %s.z;",
667 v.fsIn(), v.fsIn());
668 } else {
669 args.fVaryingHandler->addPassThroughAttribute(gp.fLocalCoord, "texCoord");
670 }
671
Brian Salomon2432d062020-04-16 20:48:09 -0400672 // Clamp the now 2D localCoordName variable by the subset if it is provided
673 if (gp.fTexSubset.isInitialized()) {
674 args.fFragBuilder->codeAppend("float4 subset;");
675 args.fVaryingHandler->addPassThroughAttribute(gp.fTexSubset, "subset",
Michael Ludwig467994d2018-12-03 14:58:31 +0000676 Interpolation::kCanBeFlat);
677 args.fFragBuilder->codeAppend(
Brian Salomon2432d062020-04-16 20:48:09 -0400678 "texCoord = clamp(texCoord, subset.xy, subset.zw);");
Michael Ludwig467994d2018-12-03 14:58:31 +0000679 }
680
681 // Now modulate the starting output color by the texture lookup
682 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
Brian Salomon87e9ddb2019-12-19 14:50:22 -0500683 args.fFragBuilder->appendTextureLookupAndBlend(
684 args.fOutputColor, SkBlendMode::kModulate, args.fTexSamplers[0],
Brian Salomond19cd762020-01-06 13:16:31 -0500685 "texCoord", &fTextureColorSpaceXformHelper);
Michael Ludwig467994d2018-12-03 14:58:31 +0000686 args.fFragBuilder->codeAppend(";");
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400687 if (gp.fSaturate == Saturate::kYes) {
688 args.fFragBuilder->codeAppendf("%s = saturate(%s);",
689 args.fOutputColor, args.fOutputColor);
690 }
691 } else {
692 // Saturate is only intended for use with a proxy to account for the fact
693 // that GrTextureOp skips SkPaint conversion, which normally handles this.
694 SkASSERT(gp.fSaturate == Saturate::kNo);
Michael Ludwig467994d2018-12-03 14:58:31 +0000695 }
696
697 // And lastly, output the coverage calculation code
Michael Ludwig93aeba02018-12-21 09:50:31 -0500698 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
699 GrGLSLVarying coverage(kFloat_GrSLType);
700 args.fVaryingHandler->addVarying("coverage", &coverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000701 if (gp.fNeedsPerspective) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400702 // Multiply by "W" in the vertex shader, then by 1/w (sk_FragCoord.w) in
703 // the fragment shader to get screen-space linear coverage.
704 args.fVertBuilder->codeAppendf("%s = %s.w * %s.z;",
705 coverage.vsOut(), gp.fPosition.name(),
706 gp.fPosition.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400707 args.fFragBuilder->codeAppendf("float coverage = %s * sk_FragCoord.w;",
708 coverage.fsIn());
Michael Ludwig93aeba02018-12-21 09:50:31 -0500709 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400710 args.fVertBuilder->codeAppendf("%s = %s;",
711 coverage.vsOut(), gp.fCoverage.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400712 args.fFragBuilder->codeAppendf("float coverage = %s;", coverage.fsIn());
Michael Ludwig467994d2018-12-03 14:58:31 +0000713 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500714
Brian Salomon2432d062020-04-16 20:48:09 -0400715 if (gp.fGeomSubset.isInitialized()) {
716 // Calculate distance from sk_FragCoord to the 4 edges of the subset
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400717 // and clamp them to (0, 1). Use the minimum of these and the original
718 // coverage. This only has to be done in the exterior triangles, the
Brian Salomon2432d062020-04-16 20:48:09 -0400719 // interior of the quad geometry can never be clipped by the subset box.
720 args.fFragBuilder->codeAppend("float4 geoSubset;");
721 args.fVaryingHandler->addPassThroughAttribute(gp.fGeomSubset, "geoSubset",
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400722 Interpolation::kCanBeFlat);
Brian Salomon659e71f2021-02-24 10:09:02 -0500723#ifdef SK_USE_LEGACY_AA_QUAD_SUBSET
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400724 args.fFragBuilder->codeAppend(
725 "if (coverage < 0.5) {"
726 " float4 dists4 = clamp(float4(1, 1, -1, -1) * "
Brian Salomon2432d062020-04-16 20:48:09 -0400727 "(sk_FragCoord.xyxy - geoSubset), 0, 1);"
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400728 " float2 dists2 = dists4.xy * dists4.zw;"
729 " coverage = min(coverage, dists2.x * dists2.y);"
730 "}");
Brian Salomon659e71f2021-02-24 10:09:02 -0500731#else
732 args.fFragBuilder->codeAppend(
733 // This is lifted from GrAARectEffect. It'd be nice if we could
734 // invoke a FP from a GP rather than duplicate this code.
735 "half4 dists4 = clamp(half4(1, 1, -1, -1) * "
736 "half4(sk_FragCoord.xyxy - geoSubset), 0, 1);\n"
737 "half2 dists2 = dists4.xy + dists4.zw - 1;\n"
738 "half subsetCoverage = dists2.x * dists2.y;\n"
739 "coverage = min(coverage, subsetCoverage);");
740#endif
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400741 }
742
743 args.fFragBuilder->codeAppendf("%s = half4(half(coverage));",
744 args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000745 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500746 // Set coverage to 1, since it's either non-AA or the coverage was already
747 // folded into the output color
Brian Salomon2432d062020-04-16 20:48:09 -0400748 SkASSERT(!gp.fGeomSubset.isInitialized());
Ethan Nicholase1f55022019-02-05 17:17:40 -0500749 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000750 }
751 }
752 GrGLSLColorSpaceXformHelper fTextureColorSpaceXformHelper;
753 };
754 return new GLSLProcessor;
755 }
756
757private:
758 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec)
759 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
760 , fTextureColorSpaceXform(nullptr) {
Brian Salomon2432d062020-04-16 20:48:09 -0400761 SkASSERT(!spec.hasSubset());
Michael Ludwig467994d2018-12-03 14:58:31 +0000762 this->initializeAttrs(spec);
763 this->setTextureSamplerCnt(0);
764 }
765
Brian Salomon67529b22019-08-13 15:31:04 -0400766 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec,
767 const GrShaderCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400768 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500769 GrSamplerState samplerState,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400770 const GrSwizzle& swizzle,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400771 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
772 Saturate saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000773 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400774 , fSaturate(saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000775 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Robert Phillips323471e2019-11-11 11:33:37 -0500776 , fSampler(samplerState, backendFormat, swizzle) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500777 SkASSERT(spec.hasLocalCoords());
Michael Ludwig467994d2018-12-03 14:58:31 +0000778 this->initializeAttrs(spec);
779 this->setTextureSamplerCnt(1);
780 }
781
Robert Phillips29f38542019-10-16 09:20:25 -0400782 // This needs to stay in sync w/ VertexSpec::vertexSize
Michael Ludwig467994d2018-12-03 14:58:31 +0000783 void initializeAttrs(const VertexSpec& spec) {
784 fNeedsPerspective = spec.deviceDimensionality() == 3;
Robert Phillips29f38542019-10-16 09:20:25 -0400785 fCoverageMode = spec.coverageMode();
Michael Ludwig93aeba02018-12-21 09:50:31 -0500786
787 if (fCoverageMode == CoverageMode::kWithPosition) {
788 if (fNeedsPerspective) {
789 fPosition = {"positionWithCoverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
790 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400791 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
792 fCoverage = {"coverage", kFloat_GrVertexAttribType, kFloat_GrSLType};
Michael Ludwig93aeba02018-12-21 09:50:31 -0500793 }
794 } else {
795 if (fNeedsPerspective) {
796 fPosition = {"position", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
797 } else {
798 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
799 }
800 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000801
Brian Salomon2432d062020-04-16 20:48:09 -0400802 // Need a geometry subset when the quads are AA and not rectilinear, since their AA
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400803 // outsetting can go beyond a half pixel.
Brian Salomon2432d062020-04-16 20:48:09 -0400804 if (spec.requiresGeometrySubset()) {
805 fGeomSubset = {"geomSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400806 }
807
Michael Ludwig467994d2018-12-03 14:58:31 +0000808 int localDim = spec.localDimensionality();
809 if (localDim == 3) {
810 fLocalCoord = {"localCoord", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
811 } else if (localDim == 2) {
812 fLocalCoord = {"localCoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
813 } // else localDim == 0 and attribute remains uninitialized
814
Brian Osman2715bf52019-12-06 14:38:47 -0500815 if (spec.hasVertexColors()) {
816 fColor = MakeColorAttribute("color", ColorType::kFloat == spec.colorType());
Michael Ludwig467994d2018-12-03 14:58:31 +0000817 }
818
Brian Salomon2432d062020-04-16 20:48:09 -0400819 if (spec.hasSubset()) {
820 fTexSubset = {"texSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwig467994d2018-12-03 14:58:31 +0000821 }
822
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400823 this->setVertexAttributes(&fPosition, 6);
Michael Ludwig467994d2018-12-03 14:58:31 +0000824 }
825
826 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
827
Michael Ludwig93aeba02018-12-21 09:50:31 -0500828 Attribute fPosition; // May contain coverage as last channel
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400829 Attribute fCoverage; // Used for non-perspective position to avoid Intel Metal issues
Michael Ludwig93aeba02018-12-21 09:50:31 -0500830 Attribute fColor; // May have coverage modulated in if the FPs support it
Michael Ludwig467994d2018-12-03 14:58:31 +0000831 Attribute fLocalCoord;
Brian Salomon2432d062020-04-16 20:48:09 -0400832 Attribute fGeomSubset; // Screen-space bounding box on geometry+aa outset
833 Attribute fTexSubset; // Texture-space bounding box on local coords
Michael Ludwig467994d2018-12-03 14:58:31 +0000834
Michael Ludwig93aeba02018-12-21 09:50:31 -0500835 // The positions attribute may have coverage built into it, so float3 is an ambiguous type
836 // and may mean 2d with coverage, or 3d with no coverage
Michael Ludwig467994d2018-12-03 14:58:31 +0000837 bool fNeedsPerspective;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400838 // Should saturate() be called on the color? Only relevant when created with a texture.
839 Saturate fSaturate = Saturate::kNo;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500840 CoverageMode fCoverageMode;
Michael Ludwig467994d2018-12-03 14:58:31 +0000841
842 // Color space will be null and fSampler.isInitialized() returns false when the GP is configured
843 // to skip texturing.
844 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
845 TextureSampler fSampler;
846
John Stiles7571f9e2020-09-02 22:42:33 -0400847 using INHERITED = GrGeometryProcessor;
Michael Ludwig467994d2018-12-03 14:58:31 +0000848};
849
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500850GrGeometryProcessor* MakeProcessor(SkArenaAlloc* arena, const VertexSpec& spec) {
851 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec);
Michael Ludwig467994d2018-12-03 14:58:31 +0000852}
853
Brian Salomonccb61422020-01-09 10:46:36 -0500854GrGeometryProcessor* MakeTexturedProcessor(SkArenaAlloc* arena,
855 const VertexSpec& spec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500856 const GrShaderCaps& caps,
857 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500858 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500859 const GrSwizzle& swizzle,
860 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
Brian Salomonccb61422020-01-09 10:46:36 -0500861 Saturate saturate) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500862 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec, caps, backendFormat, samplerState,
863 swizzle, std::move(textureColorSpaceXform),
864 saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400865}
Michael Ludwigc182b942018-11-16 10:27:51 -0500866
867} // namespace GrQuadPerEdgeAA