blob: 1fc36844c8758859cb6e9cfc500b9782a41a359c [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
Robert Phillipsef80d7b2021-09-14 16:10:56 -04008#include "src/gpu/ops/QuadPerEdgeAA.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -04009
Michael Ludwigfb7ba522019-10-29 15:33:34 -040010#include "include/private/SkVx.h"
Robert Phillips71143952021-06-17 14:55:07 -040011#include "src/gpu/GrMeshDrawTarget.h"
Robert Phillips1a82a4e2021-07-01 10:27:44 -040012#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/SkGr.h"
Michael Ludwigfb7ba522019-10-29 15:33:34 -040014#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
16#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#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
Robert Phillipsef80d7b2021-09-14 16:10:56 -040029using VertexSpec = skgpu::v1::QuadPerEdgeAA::VertexSpec;
30using CoverageMode = skgpu::v1::QuadPerEdgeAA::CoverageMode;
31using ColorType = skgpu::v1::QuadPerEdgeAA::ColorType;
32
Michael Ludwig73dbea62019-11-19 14:55:36 -050033// Generic WriteQuadProc that can handle any VertexSpec. It writes the 4 vertices in triangle strip
34// order, although the data per-vertex is dependent on the VertexSpec.
Robert Phillipsef80d7b2021-09-14 16:10:56 -040035void write_quad_generic(GrVertexWriter* vb,
36 const VertexSpec& spec,
37 const GrQuad* deviceQuad,
38 const GrQuad* localQuad,
39 const float coverage[4],
40 const SkPMColor4f& color,
41 const SkRect& geomSubset,
42 const SkRect& texSubset) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050043 static constexpr auto If = GrVertexWriter::If<float>;
Michael Ludwig704d5402019-11-25 09:43:37 -050044
45 SkASSERT(!spec.hasLocalCoords() || localQuad);
46
Robert Phillipsef80d7b2021-09-14 16:10:56 -040047 CoverageMode mode = spec.coverageMode();
Michael Ludwig553e9a92018-11-29 12:38:35 -050048 for (int i = 0; i < 4; ++i) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050049 // save position, this is a float2 or float3 or float4 depending on the combination of
50 // perspective and coverage mode.
Michael Ludwig704d5402019-11-25 09:43:37 -050051 vb->write(deviceQuad->x(i), deviceQuad->y(i),
52 If(spec.deviceQuadType() == GrQuad::Type::kPerspective, deviceQuad->w(i)),
Robert Phillipsef80d7b2021-09-14 16:10:56 -040053 If(mode == CoverageMode::kWithPosition, coverage[i]));
Michael Ludwig4921dc32018-12-03 14:57:29 +000054
Michael Ludwig93aeba02018-12-21 09:50:31 -050055 // save color
56 if (spec.hasVertexColors()) {
Robert Phillipsef80d7b2021-09-14 16:10:56 -040057 bool wide = spec.colorType() == ColorType::kFloat;
58 vb->write(GrVertexColor(color * (mode == CoverageMode::kWithColor ? coverage[i] : 1.f),
59 wide));
Michael Ludwig93aeba02018-12-21 09:50:31 -050060 }
61
62 // save local position
63 if (spec.hasLocalCoords()) {
Michael Ludwig704d5402019-11-25 09:43:37 -050064 vb->write(localQuad->x(i), localQuad->y(i),
65 If(spec.localQuadType() == GrQuad::Type::kPerspective, localQuad->w(i)));
Michael Ludwig93aeba02018-12-21 09:50:31 -050066 }
67
Brian Salomon2432d062020-04-16 20:48:09 -040068 // save the geometry subset
69 if (spec.requiresGeometrySubset()) {
70 vb->write(geomSubset);
Michael Ludwigdcfbe322019-04-01 14:55:54 -040071 }
72
Brian Salomon2432d062020-04-16 20:48:09 -040073 // save the texture subset
74 if (spec.hasSubset()) {
75 vb->write(texSubset);
Michael Ludwig93aeba02018-12-21 09:50:31 -050076 }
77 }
78}
79
Michael Ludwig73dbea62019-11-19 14:55:36 -050080// Specialized WriteQuadProcs for particular VertexSpecs that show up frequently (determined
81// experimentally through recorded GMs, SKPs, and SVGs, as well as SkiaRenderer's usage patterns):
82
Brian Salomon2432d062020-04-16 20:48:09 -040083// 2D (XY), no explicit coverage, vertex color, no locals, no geometry subset, no texture subsetn
Michael Ludwig73dbea62019-11-19 14:55:36 -050084// This represents simple, solid color or shader, non-AA (or AA with cov. as alpha) rects.
Robert Phillipsef80d7b2021-09-14 16:10:56 -040085void write_2d_color(GrVertexWriter* vb,
86 const VertexSpec& spec,
87 const GrQuad* deviceQuad,
88 const GrQuad* localQuad,
89 const float coverage[4],
90 const SkPMColor4f& color,
91 const SkRect& geomSubset,
92 const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -050093 // Assert assumptions about VertexSpec
94 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
95 SkASSERT(!spec.hasLocalCoords());
Robert Phillipsef80d7b2021-09-14 16:10:56 -040096 SkASSERT(spec.coverageMode() == CoverageMode::kNone ||
97 spec.coverageMode() == CoverageMode::kWithColor);
Michael Ludwig73dbea62019-11-19 14:55:36 -050098 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -040099 SkASSERT(!spec.requiresGeometrySubset());
100 SkASSERT(!spec.hasSubset());
Robert Phillipsfbf02142021-09-01 16:31:34 -0400101 // We don't assert that localQuad == nullptr, since it is possible for FillRectOp to
Michael Ludwig704d5402019-11-25 09:43:37 -0500102 // accumulate local coords conservatively (paint not trivial), and then after analysis realize
103 // the processors don't need local coordinates.
Michael Ludwig73dbea62019-11-19 14:55:36 -0500104
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400105 bool wide = spec.colorType() == ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500106 for (int i = 0; i < 4; ++i) {
107 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400108 SkASSERT(spec.coverageMode() == CoverageMode::kWithColor || coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500109 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500110 }
111}
112
Brian Salomon2432d062020-04-16 20:48:09 -0400113// 2D (XY), no explicit coverage, UV locals, no color, no geometry subset, no texture subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500114// This represents opaque, non AA, textured rects
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400115void write_2d_uv(GrVertexWriter* vb,
116 const VertexSpec& spec,
117 const GrQuad* deviceQuad,
118 const GrQuad* localQuad,
119 const float coverage[4],
120 const SkPMColor4f& color,
121 const SkRect& geomSubset,
122 const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500123 // Assert assumptions about VertexSpec
124 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
125 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400126 SkASSERT(spec.coverageMode() == CoverageMode::kNone);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500127 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400128 SkASSERT(!spec.requiresGeometrySubset());
129 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500130 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500131
132 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500133 vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500134 }
135}
136
Brian Salomon2432d062020-04-16 20:48:09 -0400137// 2D (XY), no explicit coverage, UV locals, vertex color, no geometry or texture subsets
Michael Ludwig73dbea62019-11-19 14:55:36 -0500138// This represents transparent, non AA (or AA with cov. as alpha), textured rects
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400139void write_2d_color_uv(GrVertexWriter* vb,
140 const VertexSpec& spec,
141 const GrQuad* deviceQuad,
142 const GrQuad* localQuad,
143 const float coverage[4],
144 const SkPMColor4f& color,
145 const SkRect& geomSubset,
146 const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500147 // Assert assumptions about VertexSpec
148 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
149 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400150 SkASSERT(spec.coverageMode() == CoverageMode::kNone ||
151 spec.coverageMode() == CoverageMode::kWithColor);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500152 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400153 SkASSERT(!spec.requiresGeometrySubset());
154 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500155 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500156
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400157 bool wide = spec.colorType() == ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500158 for (int i = 0; i < 4; ++i) {
159 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400160 SkASSERT(spec.coverageMode() == CoverageMode::kWithColor || coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500161 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide),
162 localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500163 }
164}
165
Brian Salomon2432d062020-04-16 20:48:09 -0400166// 2D (XY), explicit coverage, UV locals, no color, no geometry subset, no texture subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500167// This represents opaque, AA, textured rects
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400168void write_2d_cov_uv(GrVertexWriter* vb,
169 const VertexSpec& spec,
170 const GrQuad* deviceQuad,
171 const GrQuad* localQuad,
172 const float coverage[4],
173 const SkPMColor4f& color,
174 const SkRect& geomSubset,
175 const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500176 // Assert assumptions about VertexSpec
177 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
178 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400179 SkASSERT(spec.coverageMode() == CoverageMode::kWithPosition);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500180 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400181 SkASSERT(!spec.requiresGeometrySubset());
182 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500183 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500184
185 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500186 vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i],
187 localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500188 }
189}
190
191// NOTE: The three _strict specializations below match the non-strict uv functions above, except
Brian Salomon2432d062020-04-16 20:48:09 -0400192// that they also write the UV subset. These are included to benefit SkiaRenderer, which must make
193// use of both fast and strict constrained subsets. When testing _strict was not that common across
Michael Ludwig73dbea62019-11-19 14:55:36 -0500194// GMS, SKPs, and SVGs but we have little visibility into actual SkiaRenderer statistics. If
Brian Salomon2432d062020-04-16 20:48:09 -0400195// SkiaRenderer can avoid subsets more, these 3 functions should probably be removed for simplicity.
Michael Ludwig73dbea62019-11-19 14:55:36 -0500196
Brian Salomon2432d062020-04-16 20:48:09 -0400197// 2D (XY), no explicit coverage, UV locals, no color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500198// This represents opaque, non AA, textured rects with strict uv sampling
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400199void write_2d_uv_strict(GrVertexWriter* vb,
200 const VertexSpec& spec,
201 const GrQuad* deviceQuad,
202 const GrQuad* localQuad,
203 const float coverage[4],
204 const SkPMColor4f& color,
205 const SkRect& geomSubset,
206 const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500207 // Assert assumptions about VertexSpec
208 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
209 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400210 SkASSERT(spec.coverageMode() == CoverageMode::kNone);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500211 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400212 SkASSERT(!spec.requiresGeometrySubset());
213 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500214 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500215
216 for (int i = 0; i < 4; ++i) {
Brian Salomon2432d062020-04-16 20:48:09 -0400217 vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500218 }
219}
220
Brian Salomon2432d062020-04-16 20:48:09 -0400221// 2D (XY), no explicit coverage, UV locals, vertex color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500222// This represents transparent, non AA (or AA with cov. as alpha), textured rects with strict sample
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400223void write_2d_color_uv_strict(GrVertexWriter* vb,
224 const VertexSpec& spec,
225 const GrQuad* deviceQuad,
226 const GrQuad* localQuad,
227 const float coverage[4],
228 const SkPMColor4f& color,
229 const SkRect& geomSubset,
230 const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500231 // Assert assumptions about VertexSpec
232 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
233 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400234 SkASSERT(spec.coverageMode() == CoverageMode::kNone ||
235 spec.coverageMode() == CoverageMode::kWithColor);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500236 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400237 SkASSERT(!spec.requiresGeometrySubset());
238 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500239 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500240
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400241 bool wide = spec.colorType() == ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500242 for (int i = 0; i < 4; ++i) {
243 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400244 SkASSERT(spec.coverageMode() == CoverageMode::kWithColor || coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500245 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide),
Brian Salomon2432d062020-04-16 20:48:09 -0400246 localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500247 }
248}
249
Brian Salomon2432d062020-04-16 20:48:09 -0400250// 2D (XY), explicit coverage, UV locals, no color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500251// This represents opaque, AA, textured rects with strict uv sampling
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400252void write_2d_cov_uv_strict(GrVertexWriter* vb,
253 const VertexSpec& spec,
254 const GrQuad* deviceQuad,
255 const GrQuad* localQuad,
256 const float coverage[4],
257 const SkPMColor4f& color,
258 const SkRect& geomSubset,
259 const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500260 // Assert assumptions about VertexSpec
261 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
262 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400263 SkASSERT(spec.coverageMode() == CoverageMode::kWithPosition);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500264 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400265 SkASSERT(!spec.requiresGeometrySubset());
266 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500267 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500268
269 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500270 vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i],
Brian Salomon2432d062020-04-16 20:48:09 -0400271 localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500272 }
273}
274
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400275} // anonymous namespace
276
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400277namespace skgpu::v1::QuadPerEdgeAA {
Michael Ludwigc182b942018-11-16 10:27:51 -0500278
Brian Salomonb8c4add2021-06-28 09:20:44 -0400279IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads) {
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400280 if (aa == GrAAType::kCoverage) {
281 return IndexBufferOption::kPictureFramed;
Brian Salomonb8c4add2021-06-28 09:20:44 -0400282 } else if (numQuads > 1) {
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400283 return IndexBufferOption::kIndexedRects;
284 } else {
285 return IndexBufferOption::kTriStrips;
286 }
287}
288
Brian Osman2715bf52019-12-06 14:38:47 -0500289// This is a more elaborate version of fitsInBytes() that allows "no color" for white
290ColorType MinColorType(SkPMColor4f color) {
Brian Salomon1d835422019-03-13 16:11:44 -0400291 if (color == SK_PMColor4fWHITE) {
292 return ColorType::kNone;
Brian Salomon1d835422019-03-13 16:11:44 -0400293 } else {
Brian Osman2715bf52019-12-06 14:38:47 -0500294 return color.fitsInBytes() ? ColorType::kByte : ColorType::kFloat;
Brian Salomon1d835422019-03-13 16:11:44 -0400295 }
296}
297
Michael Ludwig73dbea62019-11-19 14:55:36 -0500298////////////////// Tessellator Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500299
Michael Ludwig73dbea62019-11-19 14:55:36 -0500300Tessellator::WriteQuadProc Tessellator::GetWriteQuadProc(const VertexSpec& spec) {
Brian Salomon2432d062020-04-16 20:48:09 -0400301 // All specialized writing functions requires 2D geometry and no geometry subset. This is not
Michael Ludwig73dbea62019-11-19 14:55:36 -0500302 // the same as just checking device type vs. kRectilinear since non-AA general 2D quads do not
Brian Salomon2432d062020-04-16 20:48:09 -0400303 // require a geometry subset and could then go through a fast path.
304 if (spec.deviceQuadType() != GrQuad::Type::kPerspective && !spec.requiresGeometrySubset()) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500305 CoverageMode mode = spec.coverageMode();
306 if (spec.hasVertexColors()) {
307 if (mode != CoverageMode::kWithPosition) {
308 // Vertex colors, but no explicit coverage
309 if (!spec.hasLocalCoords()) {
310 // Non-UV with vertex colors (possibly with coverage folded into alpha)
311 return write_2d_color;
312 } else if (spec.localQuadType() != GrQuad::Type::kPerspective) {
313 // UV locals with vertex colors (possibly with coverage-as-alpha)
Brian Salomon2432d062020-04-16 20:48:09 -0400314 return spec.hasSubset() ? write_2d_color_uv_strict : write_2d_color_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500315 }
316 }
317 // Else fall through; this is a spec that requires vertex colors and explicit coverage,
318 // which means it's anti-aliased and the FPs don't support coverage as alpha, or
319 // it uses 3D local coordinates.
320 } else if (spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective) {
321 if (mode == CoverageMode::kWithPosition) {
322 // UV locals with explicit coverage
Brian Salomon2432d062020-04-16 20:48:09 -0400323 return spec.hasSubset() ? write_2d_cov_uv_strict : write_2d_cov_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500324 } else {
325 SkASSERT(mode == CoverageMode::kNone);
Brian Salomon2432d062020-04-16 20:48:09 -0400326 return spec.hasSubset() ? write_2d_uv_strict : write_2d_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500327 }
328 }
329 // Else fall through to generic vertex function; this is a spec that has no vertex colors
330 // and [no|uvr] local coords, which doesn't happen often enough to warrant specialization.
331 }
Michael Ludwig41f395d2019-05-23 13:59:45 -0400332
Michael Ludwig73dbea62019-11-19 14:55:36 -0500333 // Arbitrary spec hits the slow path
334 return write_quad_generic;
335}
336
Michael Ludwig189c9802019-11-21 11:21:12 -0500337Tessellator::Tessellator(const VertexSpec& spec, char* vertices)
Michael Ludwig73dbea62019-11-19 14:55:36 -0500338 : fVertexSpec(spec)
Michael Ludwig189c9802019-11-21 11:21:12 -0500339 , fVertexWriter{vertices}
Michael Ludwig73dbea62019-11-19 14:55:36 -0500340 , fWriteProc(Tessellator::GetWriteQuadProc(spec)) {}
341
Michael Ludwig704d5402019-11-25 09:43:37 -0500342void Tessellator::append(GrQuad* deviceQuad, GrQuad* localQuad,
Brian Salomon2432d062020-04-16 20:48:09 -0400343 const SkPMColor4f& color, const SkRect& uvSubset, GrQuadAAFlags aaFlags) {
Michael Ludwig189c9802019-11-21 11:21:12 -0500344 // We allow Tessellator to be created with a null vertices pointer for convenience, but it is
345 // assumed it will never actually be used in those cases.
346 SkASSERT(fVertexWriter.fPtr);
Michael Ludwig704d5402019-11-25 09:43:37 -0500347 SkASSERT(deviceQuad->quadType() <= fVertexSpec.deviceQuadType());
348 SkASSERT(localQuad || !fVertexSpec.hasLocalCoords());
349 SkASSERT(!fVertexSpec.hasLocalCoords() || localQuad->quadType() <= fVertexSpec.localQuadType());
Michael Ludwig73dbea62019-11-19 14:55:36 -0500350
351 static const float kFullCoverage[4] = {1.f, 1.f, 1.f, 1.f};
352 static const float kZeroCoverage[4] = {0.f, 0.f, 0.f, 0.f};
Brian Salomon2432d062020-04-16 20:48:09 -0400353 static const SkRect kIgnoredSubset = SkRect::MakeEmpty();
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400354
Michael Ludwig73dbea62019-11-19 14:55:36 -0500355 if (fVertexSpec.usesCoverageAA()) {
356 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kWithColor ||
357 fVertexSpec.coverageMode() == CoverageMode::kWithPosition);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400358 // Must calculate inner and outer quadrilaterals for the vertex coverage ramps, and possibly
Brian Salomon2432d062020-04-16 20:48:09 -0400359 // a geometry subset if corners are not right angles
360 SkRect geomSubset;
361 if (fVertexSpec.requiresGeometrySubset()) {
Brian Salomon659e71f2021-02-24 10:09:02 -0500362#ifdef SK_USE_LEGACY_AA_QUAD_SUBSET
Brian Salomon2432d062020-04-16 20:48:09 -0400363 geomSubset = deviceQuad->bounds();
364 geomSubset.outset(0.5f, 0.5f); // account for AA expansion
Brian Salomon659e71f2021-02-24 10:09:02 -0500365#else
366 // Our GP code expects a 0.5 outset rect (coverage is computed as 0 at the values of
367 // the uniform). However, if we have quad edges that aren't supposed to be antialiased
368 // they may lie close to the bounds. So in that case we outset by an additional 0.5.
369 // This is a sort of backup clipping mechanism for cases where quad outsetting of nearly
370 // parallel edges produces long thin extrusions from the original geometry.
371 float outset = aaFlags == GrQuadAAFlags::kAll ? 0.5f : 1.f;
372 geomSubset = deviceQuad->bounds().makeOutset(outset, outset);
373#endif
Michael Ludwige6266a22019-03-07 11:24:32 -0500374 }
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400375
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500376 if (aaFlags == GrQuadAAFlags::kNone) {
377 // Have to write the coverage AA vertex structure, but there's no math to be done for a
378 // non-aa quad batched into a coverage AA op.
Michael Ludwig189c9802019-11-21 11:21:12 -0500379 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400380 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500381 // Since we pass the same corners in, the outer vertex structure will have 0 area and
382 // the coverage interpolation from 1 to 0 will not be visible.
Michael Ludwig189c9802019-11-21 11:21:12 -0500383 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400384 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500385 } else {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500386 // Reset the tessellation helper to match the current geometry
Michael Ludwig704d5402019-11-25 09:43:37 -0500387 fAAHelper.reset(*deviceQuad, localQuad);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400388
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500389 // Edge inset/outset distance ordered LBTR, set to 0.5 for a half pixel if the AA flag
390 // is turned on, or 0.0 if the edge is not anti-aliased.
391 skvx::Vec<4, float> edgeDistances;
392 if (aaFlags == GrQuadAAFlags::kAll) {
393 edgeDistances = 0.5f;
394 } else {
395 edgeDistances = { (aaFlags & GrQuadAAFlags::kLeft) ? 0.5f : 0.f,
396 (aaFlags & GrQuadAAFlags::kBottom) ? 0.5f : 0.f,
397 (aaFlags & GrQuadAAFlags::kTop) ? 0.5f : 0.f,
398 (aaFlags & GrQuadAAFlags::kRight) ? 0.5f : 0.f };
399 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400400
Michael Ludwig73dbea62019-11-19 14:55:36 -0500401 // Write inner vertices first
402 float coverage[4];
Michael Ludwig704d5402019-11-25 09:43:37 -0500403 fAAHelper.inset(edgeDistances, deviceQuad, localQuad).store(coverage);
404 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, coverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400405 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500406
Michael Ludwig290d6df2020-10-27 09:28:59 -0400407 // Then outer vertices, which use 0.f for their coverage. If the inset was degenerate
408 // to a line (had all coverages < 1), tweak the outset distance so the outer frame's
409 // narrow axis reaches out to 2px, which gives better animation under translation.
Michael Ludwig575c9212021-07-13 11:09:52 -0400410 const bool hairline = aaFlags == GrQuadAAFlags::kAll &&
411 coverage[0] < 1.f &&
412 coverage[1] < 1.f &&
413 coverage[2] < 1.f &&
414 coverage[3] < 1.f;
415 if (hairline) {
Michael Ludwig290d6df2020-10-27 09:28:59 -0400416 skvx::Vec<4, float> len = fAAHelper.getEdgeLengths();
417 // Using max guards us against trying to scale a degenerate triangle edge of 0 len
418 // up to 2px. The shuffles are so that edge 0's adjustment is based on the lengths
419 // of its connecting edges (1 and 2), and so forth.
420 skvx::Vec<4, float> maxWH = max(skvx::shuffle<1, 0, 3, 2>(len),
421 skvx::shuffle<2, 3, 0, 1>(len));
422 // wh + 2e' = 2, so e' = (2 - wh) / 2 => e' = e * (2 - wh). But if w or h > 1, then
423 // 2 - wh < 1 and represents the non-narrow axis so clamp to 1.
424 edgeDistances *= max(1.f, 2.f - maxWH);
425 }
Michael Ludwig704d5402019-11-25 09:43:37 -0500426 fAAHelper.outset(edgeDistances, deviceQuad, localQuad);
427 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400428 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500429 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500430 } else {
431 // No outsetting needed, just write a single quad with full coverage
Michael Ludwig73dbea62019-11-19 14:55:36 -0500432 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kNone &&
Brian Salomon2432d062020-04-16 20:48:09 -0400433 !fVertexSpec.requiresGeometrySubset());
Michael Ludwig189c9802019-11-21 11:21:12 -0500434 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400435 kIgnoredSubset, uvSubset);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400436 }
437}
Michael Ludwig20e909e2018-10-30 10:43:57 -0400438
Robert Phillips71143952021-06-17 14:55:07 -0400439sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawTarget* target,
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400440 IndexBufferOption indexBufferOption) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400441 auto resourceProvider = target->resourceProvider();
442
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400443 switch (indexBufferOption) {
444 case IndexBufferOption::kPictureFramed: return resourceProvider->refAAQuadIndexBuffer();
445 case IndexBufferOption::kIndexedRects: return resourceProvider->refNonAAQuadIndexBuffer();
446 case IndexBufferOption::kTriStrips: // fall through
447 default: return nullptr;
448 }
449}
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400450
Robert Phillips438d9862019-11-14 12:46:05 -0500451int QuadLimit(IndexBufferOption option) {
452 switch (option) {
453 case IndexBufferOption::kPictureFramed: return GrResourceProvider::MaxNumAAQuads();
454 case IndexBufferOption::kIndexedRects: return GrResourceProvider::MaxNumNonAAQuads();
455 case IndexBufferOption::kTriStrips: return SK_MaxS32; // not limited by an indexBuffer
456 }
457
458 SkUNREACHABLE;
459}
Michael Ludwig93aeba02018-12-21 09:50:31 -0500460
Chris Daltondbb833b2020-03-17 12:15:46 -0600461void IssueDraw(const GrCaps& caps, GrOpsRenderPass* renderPass, const VertexSpec& spec,
462 int runningQuadCount, int quadsInDraw, int maxVerts, int absVertBufferOffset) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400463 if (spec.indexBufferOption() == IndexBufferOption::kTriStrips) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400464 int offset = absVertBufferOffset +
465 runningQuadCount * GrResourceProvider::NumVertsPerNonAAQuad();
Chris Daltondbb833b2020-03-17 12:15:46 -0600466 renderPass->draw(4, offset);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400467 return;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500468 }
469
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400470 SkASSERT(spec.indexBufferOption() == IndexBufferOption::kPictureFramed ||
471 spec.indexBufferOption() == IndexBufferOption::kIndexedRects);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400472
Robert Phillips2f05a482019-11-25 09:54:43 -0500473 int maxNumQuads, numIndicesPerQuad, numVertsPerQuad;
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400474
475 if (spec.indexBufferOption() == IndexBufferOption::kPictureFramed) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400476 // AA uses 8 vertices and 30 indices per quad, basically nested rectangles
Robert Phillips2f05a482019-11-25 09:54:43 -0500477 maxNumQuads = GrResourceProvider::MaxNumAAQuads();
478 numIndicesPerQuad = GrResourceProvider::NumIndicesPerAAQuad();
479 numVertsPerQuad = GrResourceProvider::NumVertsPerAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400480 } else {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400481 // Non-AA uses 4 vertices and 6 indices per quad
Robert Phillips2f05a482019-11-25 09:54:43 -0500482 maxNumQuads = GrResourceProvider::MaxNumNonAAQuads();
483 numIndicesPerQuad = GrResourceProvider::NumIndicesPerNonAAQuad();
484 numVertsPerQuad = GrResourceProvider::NumVertsPerNonAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400485 }
486
Robert Phillips2f05a482019-11-25 09:54:43 -0500487 SkASSERT(runningQuadCount + quadsInDraw <= maxNumQuads);
488
489 if (caps.avoidLargeIndexBufferDraws()) {
490 // When we need to avoid large index buffer draws we modify the base vertex of the draw
491 // which, in GL, requires rebinding all vertex attrib arrays, so a base index is generally
492 // preferred.
493 int offset = absVertBufferOffset + runningQuadCount * numVertsPerQuad;
494
Chris Daltondbb833b2020-03-17 12:15:46 -0600495 renderPass->drawIndexPattern(numIndicesPerQuad, quadsInDraw, maxNumQuads, numVertsPerQuad,
496 offset);
Robert Phillips2f05a482019-11-25 09:54:43 -0500497 } else {
498 int baseIndex = runningQuadCount * numIndicesPerQuad;
499 int numIndicesToDraw = quadsInDraw * numIndicesPerQuad;
500
501 int minVertex = runningQuadCount * numVertsPerQuad;
Michael Ludwigfa1cb402021-06-02 10:53:16 -0400502 int maxVertex = (runningQuadCount + quadsInDraw) * numVertsPerQuad - 1; // inclusive
Robert Phillips2f05a482019-11-25 09:54:43 -0500503
Chris Daltondbb833b2020-03-17 12:15:46 -0600504 renderPass->drawIndexed(numIndicesToDraw, baseIndex, minVertex, maxVertex,
505 absVertBufferOffset);
Robert Phillips2f05a482019-11-25 09:54:43 -0500506 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500507}
508
Michael Ludwigc182b942018-11-16 10:27:51 -0500509////////////////// VertexSpec Implementation
Michael Ludwig20e909e2018-10-30 10:43:57 -0400510
Michael Ludwigc182b942018-11-16 10:27:51 -0500511int VertexSpec::deviceDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400512 return this->deviceQuadType() == GrQuad::Type::kPerspective ? 3 : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -0500513}
514
515int VertexSpec::localDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400516 return fHasLocalCoords ? (this->localQuadType() == GrQuad::Type::kPerspective ? 3 : 2) : 0;
Michael Ludwigc182b942018-11-16 10:27:51 -0500517}
518
Robert Phillips29f38542019-10-16 09:20:25 -0400519CoverageMode VertexSpec::coverageMode() const {
520 if (this->usesCoverageAA()) {
521 if (this->compatibleWithCoverageAsAlpha() && this->hasVertexColors() &&
Brian Salomon2432d062020-04-16 20:48:09 -0400522 !this->requiresGeometrySubset()) {
523 // Using a geometric subset acts as a second source of coverage and folding
Robert Phillips29f38542019-10-16 09:20:25 -0400524 // the original coverage into color makes it impossible to apply the color's
Brian Salomon2432d062020-04-16 20:48:09 -0400525 // alpha to the geometric subset's coverage when the original shape is clipped.
Robert Phillips29f38542019-10-16 09:20:25 -0400526 return CoverageMode::kWithColor;
527 } else {
528 return CoverageMode::kWithPosition;
529 }
530 } else {
531 return CoverageMode::kNone;
532 }
533}
534
535// This needs to stay in sync w/ QuadPerEdgeAAGeometryProcessor::initializeAttrs
536size_t VertexSpec::vertexSize() const {
537 bool needsPerspective = (this->deviceDimensionality() == 3);
538 CoverageMode coverageMode = this->coverageMode();
539
540 size_t count = 0;
541
542 if (coverageMode == CoverageMode::kWithPosition) {
543 if (needsPerspective) {
544 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
545 } else {
546 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType) +
547 GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
548 }
549 } else {
550 if (needsPerspective) {
551 count += GrVertexAttribTypeSize(kFloat3_GrVertexAttribType);
552 } else {
553 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType);
554 }
555 }
556
Brian Salomon2432d062020-04-16 20:48:09 -0400557 if (this->requiresGeometrySubset()) {
Robert Phillips29f38542019-10-16 09:20:25 -0400558 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
559 }
560
561 count += this->localDimensionality() * GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
562
563 if (ColorType::kByte == this->colorType()) {
564 count += GrVertexAttribTypeSize(kUByte4_norm_GrVertexAttribType);
Brian Osman2715bf52019-12-06 14:38:47 -0500565 } else if (ColorType::kFloat == this->colorType()) {
566 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
Robert Phillips29f38542019-10-16 09:20:25 -0400567 }
568
Brian Salomon2432d062020-04-16 20:48:09 -0400569 if (this->hasSubset()) {
Robert Phillips29f38542019-10-16 09:20:25 -0400570 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
571 }
572
573 return count;
574}
575
Michael Ludwig467994d2018-12-03 14:58:31 +0000576////////////////// Geometry Processor Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500577
Michael Ludwig467994d2018-12-03 14:58:31 +0000578class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor {
579public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500580 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const VertexSpec& spec) {
Mike Kleinf1241082020-12-14 15:59:09 -0600581 return arena->make([&](void* ptr) {
582 return new (ptr) QuadPerEdgeAAGeometryProcessor(spec);
583 });
Michael Ludwig20e909e2018-10-30 10:43:57 -0400584 }
585
Brian Salomonccb61422020-01-09 10:46:36 -0500586 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
587 const VertexSpec& vertexSpec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500588 const GrShaderCaps& caps,
589 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500590 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500591 const GrSwizzle& swizzle,
592 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
593 Saturate saturate) {
Mike Kleinf1241082020-12-14 15:59:09 -0600594 return arena->make([&](void* ptr) {
595 return new (ptr) QuadPerEdgeAAGeometryProcessor(
596 vertexSpec, caps, backendFormat, samplerState, swizzle,
597 std::move(textureColorSpaceXform), saturate);
598 });
Michael Ludwig20e909e2018-10-30 10:43:57 -0400599 }
600
Michael Ludwig467994d2018-12-03 14:58:31 +0000601 const char* name() const override { return "QuadPerEdgeAAGeometryProcessor"; }
Michael Ludwig024e2622018-11-30 13:25:55 -0500602
Brian Salomon13b28732021-08-06 15:33:58 -0400603 void addToKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400604 // texturing, device-dimensions are single bit flags
Brian Osman48d7f7c2021-03-03 13:38:08 -0500605 b->addBool(fTexSubset.isInitialized(), "subset");
606 b->addBool(fSampler.isInitialized(), "textured");
607 b->addBool(fNeedsPerspective, "perspective");
608 b->addBool((fSaturate == Saturate::kYes), "saturate");
609
610 b->addBool(fLocalCoord.isInitialized(), "hasLocalCoords");
Michael Ludwig467994d2018-12-03 14:58:31 +0000611 if (fLocalCoord.isInitialized()) {
Brian Osman48d7f7c2021-03-03 13:38:08 -0500612 // 2D (0) or 3D (1)
613 b->addBits(1, (kFloat3_GrVertexAttribType == fLocalCoord.cpuType()), "localCoordsType");
Brian Osman78dc72c2018-12-03 13:20:43 +0000614 }
Brian Osman48d7f7c2021-03-03 13:38:08 -0500615 b->addBool(fColor.isInitialized(), "hasColor");
Michael Ludwig93aeba02018-12-21 09:50:31 -0500616 if (fColor.isInitialized()) {
Brian Osman48d7f7c2021-03-03 13:38:08 -0500617 // bytes (0) or floats (1)
618 b->addBits(1, (kFloat4_GrVertexAttribType == fColor.cpuType()), "colorType");
Michael Ludwig93aeba02018-12-21 09:50:31 -0500619 }
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400620 // and coverage mode, 00 for none, 01 for withposition, 10 for withcolor, 11 for
Brian Salomon2432d062020-04-16 20:48:09 -0400621 // position+geomsubset
Brian Osman48d7f7c2021-03-03 13:38:08 -0500622 uint32_t coverageKey = 0;
Brian Salomon2432d062020-04-16 20:48:09 -0400623 SkASSERT(!fGeomSubset.isInitialized() || fCoverageMode == CoverageMode::kWithPosition);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500624 if (fCoverageMode != CoverageMode::kNone) {
Brian Osman48d7f7c2021-03-03 13:38:08 -0500625 coverageKey = fGeomSubset.isInitialized()
626 ? 0x3
627 : (CoverageMode::kWithPosition == fCoverageMode ? 0x1 : 0x2);
Michael Ludwig467994d2018-12-03 14:58:31 +0000628 }
Brian Osman48d7f7c2021-03-03 13:38:08 -0500629 b->addBits(2, coverageKey, "coverageMode");
Michael Ludwig467994d2018-12-03 14:58:31 +0000630
Brian Osman48d7f7c2021-03-03 13:38:08 -0500631 b->add32(GrColorSpaceXform::XformKey(fTextureColorSpaceXform.get()), "colorSpaceXform");
Brian Osman78dc72c2018-12-03 13:20:43 +0000632 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000633
Brian Salomonf95940b2021-08-09 15:56:24 -0400634 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override {
Brian Salomonbab2d112021-08-11 09:59:56 -0400635 class Impl : public ProgramImpl {
Michael Ludwig467994d2018-12-03 14:58:31 +0000636 public:
Brian Osman609f1592020-07-01 15:14:39 -0400637 void setData(const GrGLSLProgramDataManager& pdman,
Brian Salomon5a328282021-04-14 10:32:25 -0400638 const GrShaderCaps&,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400639 const GrGeometryProcessor& geomProc) override {
640 const auto& gp = geomProc.cast<QuadPerEdgeAAGeometryProcessor>();
Michael Ludwig467994d2018-12-03 14:58:31 +0000641 fTextureColorSpaceXformHelper.setData(pdman, gp.fTextureColorSpaceXform.get());
642 }
643
644 private:
645 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
646 using Interpolation = GrGLSLVaryingHandler::Interpolation;
647
Robert Phillips787fd9d2021-03-22 14:48:09 -0400648 const auto& gp = args.fGeomProc.cast<QuadPerEdgeAAGeometryProcessor>();
Michael Ludwig467994d2018-12-03 14:58:31 +0000649 fTextureColorSpaceXformHelper.emitCode(args.fUniformHandler,
650 gp.fTextureColorSpaceXform.get());
651
652 args.fVaryingHandler->emitAttributes(gp);
653
Michael Ludwig93aeba02018-12-21 09:50:31 -0500654 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
655 // Strip last channel from the vertex attribute to remove coverage and get the
656 // actual position
657 if (gp.fNeedsPerspective) {
658 args.fVertBuilder->codeAppendf("float3 position = %s.xyz;",
659 gp.fPosition.name());
660 } else {
661 args.fVertBuilder->codeAppendf("float2 position = %s.xy;",
662 gp.fPosition.name());
663 }
664 gpArgs->fPositionVar = {"position",
665 gp.fNeedsPerspective ? kFloat3_GrSLType
666 : kFloat2_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400667 GrShaderVar::TypeModifier::None};
Michael Ludwig467994d2018-12-03 14:58:31 +0000668 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500669 // No coverage to eliminate
670 gpArgs->fPositionVar = gp.fPosition.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000671 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000672
Michael Ludwig553db622020-06-19 10:47:30 -0400673 // This attribute will be uninitialized if earlier FP analysis determined no
674 // local coordinates are needed (and this will not include the inline texture
675 // fetch this GP does before invoking FPs).
676 gpArgs->fLocalCoordVar = gp.fLocalCoord.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000677
678 // Solid color before any texturing gets modulated in
John Stiles4d7ac492021-03-09 20:16:43 -0500679 const char* blendDst;
Michael Ludwig467994d2018-12-03 14:58:31 +0000680 if (gp.fColor.isInitialized()) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400681 SkASSERT(gp.fCoverageMode != CoverageMode::kWithColor || !gp.fNeedsPerspective);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500682 // The color cannot be flat if the varying coverage has been modulated into it
John Stiles4d7ac492021-03-09 20:16:43 -0500683 args.fFragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
Brian Salomon48959462021-08-11 13:01:06 -0400684 args.fVaryingHandler->addPassThroughAttribute(
685 gp.fColor.asShaderVar(),
686 args.fOutputColor,
687 gp.fCoverageMode == CoverageMode::kWithColor
688 ? Interpolation::kInterpolated
689 : Interpolation::kCanBeFlat);
John Stiles4d7ac492021-03-09 20:16:43 -0500690 blendDst = args.fOutputColor;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500691 } else {
692 // Output color must be initialized to something
John Stiles4d7ac492021-03-09 20:16:43 -0500693 args.fFragBuilder->codeAppendf("half4 %s = half4(1);", args.fOutputColor);
694 blendDst = nullptr;
Michael Ludwig467994d2018-12-03 14:58:31 +0000695 }
696
697 // If there is a texture, must also handle texture coordinates and reading from
698 // the texture in the fragment shader before continuing to fragment processors.
699 if (gp.fSampler.isInitialized()) {
Brian Salomon2432d062020-04-16 20:48:09 -0400700 // Texture coordinates clamped by the subset on the fragment shader; if the GP
Michael Ludwig467994d2018-12-03 14:58:31 +0000701 // has a texture, it's guaranteed to have local coordinates
702 args.fFragBuilder->codeAppend("float2 texCoord;");
703 if (gp.fLocalCoord.cpuType() == kFloat3_GrVertexAttribType) {
704 // Can't do a pass through since we need to perform perspective division
705 GrGLSLVarying v(gp.fLocalCoord.gpuType());
706 args.fVaryingHandler->addVarying(gp.fLocalCoord.name(), &v);
707 args.fVertBuilder->codeAppendf("%s = %s;",
708 v.vsOut(), gp.fLocalCoord.name());
709 args.fFragBuilder->codeAppendf("texCoord = %s.xy / %s.z;",
710 v.fsIn(), v.fsIn());
711 } else {
Brian Salomon48959462021-08-11 13:01:06 -0400712 args.fVaryingHandler->addPassThroughAttribute(gp.fLocalCoord.asShaderVar(),
713 "texCoord");
Michael Ludwig467994d2018-12-03 14:58:31 +0000714 }
715
Brian Salomon2432d062020-04-16 20:48:09 -0400716 // Clamp the now 2D localCoordName variable by the subset if it is provided
717 if (gp.fTexSubset.isInitialized()) {
718 args.fFragBuilder->codeAppend("float4 subset;");
Brian Salomon48959462021-08-11 13:01:06 -0400719 args.fVaryingHandler->addPassThroughAttribute(gp.fTexSubset.asShaderVar(),
720 "subset",
Michael Ludwig467994d2018-12-03 14:58:31 +0000721 Interpolation::kCanBeFlat);
722 args.fFragBuilder->codeAppend(
John Stiles4d7ac492021-03-09 20:16:43 -0500723 "texCoord = clamp(texCoord, subset.LT, subset.RB);");
Michael Ludwig467994d2018-12-03 14:58:31 +0000724 }
725
726 // Now modulate the starting output color by the texture lookup
John Stiles4d7ac492021-03-09 20:16:43 -0500727 args.fFragBuilder->codeAppendf(
728 "%s = %s(",
729 args.fOutputColor,
730 (gp.fSaturate == Saturate::kYes) ? "saturate" : "");
Brian Salomon87e9ddb2019-12-19 14:50:22 -0500731 args.fFragBuilder->appendTextureLookupAndBlend(
John Stiles4d7ac492021-03-09 20:16:43 -0500732 blendDst, SkBlendMode::kModulate, args.fTexSamplers[0],
Brian Salomond19cd762020-01-06 13:16:31 -0500733 "texCoord", &fTextureColorSpaceXformHelper);
John Stiles4d7ac492021-03-09 20:16:43 -0500734 args.fFragBuilder->codeAppend(");");
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400735 } else {
736 // Saturate is only intended for use with a proxy to account for the fact
Robert Phillipsfbf02142021-09-01 16:31:34 -0400737 // that TextureOp skips SkPaint conversion, which normally handles this.
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400738 SkASSERT(gp.fSaturate == Saturate::kNo);
Michael Ludwig467994d2018-12-03 14:58:31 +0000739 }
740
741 // And lastly, output the coverage calculation code
Michael Ludwig93aeba02018-12-21 09:50:31 -0500742 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
743 GrGLSLVarying coverage(kFloat_GrSLType);
744 args.fVaryingHandler->addVarying("coverage", &coverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000745 if (gp.fNeedsPerspective) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400746 // Multiply by "W" in the vertex shader, then by 1/w (sk_FragCoord.w) in
747 // the fragment shader to get screen-space linear coverage.
748 args.fVertBuilder->codeAppendf("%s = %s.w * %s.z;",
749 coverage.vsOut(), gp.fPosition.name(),
750 gp.fPosition.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400751 args.fFragBuilder->codeAppendf("float coverage = %s * sk_FragCoord.w;",
752 coverage.fsIn());
Michael Ludwig93aeba02018-12-21 09:50:31 -0500753 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400754 args.fVertBuilder->codeAppendf("%s = %s;",
755 coverage.vsOut(), gp.fCoverage.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400756 args.fFragBuilder->codeAppendf("float coverage = %s;", coverage.fsIn());
Michael Ludwig467994d2018-12-03 14:58:31 +0000757 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500758
Brian Salomon2432d062020-04-16 20:48:09 -0400759 if (gp.fGeomSubset.isInitialized()) {
760 // Calculate distance from sk_FragCoord to the 4 edges of the subset
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400761 // and clamp them to (0, 1). Use the minimum of these and the original
762 // coverage. This only has to be done in the exterior triangles, the
Brian Salomon2432d062020-04-16 20:48:09 -0400763 // interior of the quad geometry can never be clipped by the subset box.
764 args.fFragBuilder->codeAppend("float4 geoSubset;");
Brian Salomon48959462021-08-11 13:01:06 -0400765 args.fVaryingHandler->addPassThroughAttribute(gp.fGeomSubset.asShaderVar(),
766 "geoSubset",
767 Interpolation::kCanBeFlat);
Brian Salomon659e71f2021-02-24 10:09:02 -0500768#ifdef SK_USE_LEGACY_AA_QUAD_SUBSET
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400769 args.fFragBuilder->codeAppend(
770 "if (coverage < 0.5) {"
771 " float4 dists4 = clamp(float4(1, 1, -1, -1) * "
Brian Salomon2432d062020-04-16 20:48:09 -0400772 "(sk_FragCoord.xyxy - geoSubset), 0, 1);"
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400773 " float2 dists2 = dists4.xy * dists4.zw;"
774 " coverage = min(coverage, dists2.x * dists2.y);"
775 "}");
Brian Salomon659e71f2021-02-24 10:09:02 -0500776#else
777 args.fFragBuilder->codeAppend(
778 // This is lifted from GrAARectEffect. It'd be nice if we could
779 // invoke a FP from a GP rather than duplicate this code.
780 "half4 dists4 = clamp(half4(1, 1, -1, -1) * "
781 "half4(sk_FragCoord.xyxy - geoSubset), 0, 1);\n"
782 "half2 dists2 = dists4.xy + dists4.zw - 1;\n"
783 "half subsetCoverage = dists2.x * dists2.y;\n"
784 "coverage = min(coverage, subsetCoverage);");
785#endif
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400786 }
787
John Stiles4d7ac492021-03-09 20:16:43 -0500788 args.fFragBuilder->codeAppendf("half4 %s = half4(half(coverage));",
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400789 args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000790 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500791 // Set coverage to 1, since it's either non-AA or the coverage was already
792 // folded into the output color
Brian Salomon2432d062020-04-16 20:48:09 -0400793 SkASSERT(!gp.fGeomSubset.isInitialized());
John Stiles4d7ac492021-03-09 20:16:43 -0500794 args.fFragBuilder->codeAppendf("const half4 %s = half4(1);",
795 args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000796 }
797 }
Brian Salomonbab2d112021-08-11 09:59:56 -0400798
Michael Ludwig467994d2018-12-03 14:58:31 +0000799 GrGLSLColorSpaceXformHelper fTextureColorSpaceXformHelper;
800 };
Brian Salomonbab2d112021-08-11 09:59:56 -0400801
802 return std::make_unique<Impl>();
Michael Ludwig467994d2018-12-03 14:58:31 +0000803 }
804
805private:
Robert Phillips21453902021-08-27 16:05:04 -0400806 using Saturate = skgpu::v1::TextureOp::Saturate;
Brian Salomonbab2d112021-08-11 09:59:56 -0400807
Michael Ludwig467994d2018-12-03 14:58:31 +0000808 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec)
809 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
810 , fTextureColorSpaceXform(nullptr) {
Brian Salomon2432d062020-04-16 20:48:09 -0400811 SkASSERT(!spec.hasSubset());
Michael Ludwig467994d2018-12-03 14:58:31 +0000812 this->initializeAttrs(spec);
813 this->setTextureSamplerCnt(0);
814 }
815
Brian Salomon67529b22019-08-13 15:31:04 -0400816 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec,
817 const GrShaderCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400818 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500819 GrSamplerState samplerState,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400820 const GrSwizzle& swizzle,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400821 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
822 Saturate saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000823 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400824 , fSaturate(saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000825 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Robert Phillips323471e2019-11-11 11:33:37 -0500826 , fSampler(samplerState, backendFormat, swizzle) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500827 SkASSERT(spec.hasLocalCoords());
Michael Ludwig467994d2018-12-03 14:58:31 +0000828 this->initializeAttrs(spec);
829 this->setTextureSamplerCnt(1);
830 }
831
Robert Phillips29f38542019-10-16 09:20:25 -0400832 // This needs to stay in sync w/ VertexSpec::vertexSize
Michael Ludwig467994d2018-12-03 14:58:31 +0000833 void initializeAttrs(const VertexSpec& spec) {
834 fNeedsPerspective = spec.deviceDimensionality() == 3;
Robert Phillips29f38542019-10-16 09:20:25 -0400835 fCoverageMode = spec.coverageMode();
Michael Ludwig93aeba02018-12-21 09:50:31 -0500836
837 if (fCoverageMode == CoverageMode::kWithPosition) {
838 if (fNeedsPerspective) {
839 fPosition = {"positionWithCoverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
840 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400841 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
842 fCoverage = {"coverage", kFloat_GrVertexAttribType, kFloat_GrSLType};
Michael Ludwig93aeba02018-12-21 09:50:31 -0500843 }
844 } else {
845 if (fNeedsPerspective) {
846 fPosition = {"position", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
847 } else {
848 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
849 }
850 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000851
Brian Salomon2432d062020-04-16 20:48:09 -0400852 // Need a geometry subset when the quads are AA and not rectilinear, since their AA
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400853 // outsetting can go beyond a half pixel.
Brian Salomon2432d062020-04-16 20:48:09 -0400854 if (spec.requiresGeometrySubset()) {
855 fGeomSubset = {"geomSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400856 }
857
Michael Ludwig467994d2018-12-03 14:58:31 +0000858 int localDim = spec.localDimensionality();
859 if (localDim == 3) {
860 fLocalCoord = {"localCoord", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
861 } else if (localDim == 2) {
862 fLocalCoord = {"localCoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
863 } // else localDim == 0 and attribute remains uninitialized
864
Brian Osman2715bf52019-12-06 14:38:47 -0500865 if (spec.hasVertexColors()) {
866 fColor = MakeColorAttribute("color", ColorType::kFloat == spec.colorType());
Michael Ludwig467994d2018-12-03 14:58:31 +0000867 }
868
Brian Salomon2432d062020-04-16 20:48:09 -0400869 if (spec.hasSubset()) {
870 fTexSubset = {"texSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwig467994d2018-12-03 14:58:31 +0000871 }
872
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400873 this->setVertexAttributes(&fPosition, 6);
Michael Ludwig467994d2018-12-03 14:58:31 +0000874 }
875
876 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
877
Michael Ludwig93aeba02018-12-21 09:50:31 -0500878 Attribute fPosition; // May contain coverage as last channel
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400879 Attribute fCoverage; // Used for non-perspective position to avoid Intel Metal issues
Michael Ludwig93aeba02018-12-21 09:50:31 -0500880 Attribute fColor; // May have coverage modulated in if the FPs support it
Michael Ludwig467994d2018-12-03 14:58:31 +0000881 Attribute fLocalCoord;
Brian Salomon2432d062020-04-16 20:48:09 -0400882 Attribute fGeomSubset; // Screen-space bounding box on geometry+aa outset
883 Attribute fTexSubset; // Texture-space bounding box on local coords
Michael Ludwig467994d2018-12-03 14:58:31 +0000884
Michael Ludwig93aeba02018-12-21 09:50:31 -0500885 // The positions attribute may have coverage built into it, so float3 is an ambiguous type
886 // and may mean 2d with coverage, or 3d with no coverage
Michael Ludwig467994d2018-12-03 14:58:31 +0000887 bool fNeedsPerspective;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400888 // Should saturate() be called on the color? Only relevant when created with a texture.
889 Saturate fSaturate = Saturate::kNo;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500890 CoverageMode fCoverageMode;
Michael Ludwig467994d2018-12-03 14:58:31 +0000891
892 // Color space will be null and fSampler.isInitialized() returns false when the GP is configured
893 // to skip texturing.
894 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
895 TextureSampler fSampler;
896
John Stiles7571f9e2020-09-02 22:42:33 -0400897 using INHERITED = GrGeometryProcessor;
Michael Ludwig467994d2018-12-03 14:58:31 +0000898};
899
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500900GrGeometryProcessor* MakeProcessor(SkArenaAlloc* arena, const VertexSpec& spec) {
901 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec);
Michael Ludwig467994d2018-12-03 14:58:31 +0000902}
903
Brian Salomonccb61422020-01-09 10:46:36 -0500904GrGeometryProcessor* MakeTexturedProcessor(SkArenaAlloc* arena,
905 const VertexSpec& spec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500906 const GrShaderCaps& caps,
907 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500908 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500909 const GrSwizzle& swizzle,
910 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
Brian Salomonccb61422020-01-09 10:46:36 -0500911 Saturate saturate) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500912 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec, caps, backendFormat, samplerState,
913 swizzle, std::move(textureColorSpaceXform),
914 saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400915}
Michael Ludwigc182b942018-11-16 10:27:51 -0500916
Robert Phillipsef80d7b2021-09-14 16:10:56 -0400917} // namespace skgpu::v1::QuadPerEdgeAA