blob: f860406b17c21c7c6c6eaeba678d5dc4a883e693 [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"
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"
17#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/glsl/GrGLSLVarying.h"
19#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Michael Ludwig460eb5e2018-10-29 11:09:29 -040020
Brian Osman01e6d172020-03-30 15:57:14 -040021static_assert((int)GrQuadAAFlags::kLeft == SkCanvas::kLeft_QuadAAFlag);
22static_assert((int)GrQuadAAFlags::kTop == SkCanvas::kTop_QuadAAFlag);
23static_assert((int)GrQuadAAFlags::kRight == SkCanvas::kRight_QuadAAFlag);
24static_assert((int)GrQuadAAFlags::kBottom == SkCanvas::kBottom_QuadAAFlag);
25static_assert((int)GrQuadAAFlags::kNone == SkCanvas::kNone_QuadAAFlags);
26static_assert((int)GrQuadAAFlags::kAll == SkCanvas::kAll_QuadAAFlags);
Michael Ludwigf995c052018-11-26 15:24:29 -050027
Michael Ludwig460eb5e2018-10-29 11:09:29 -040028namespace {
29
Michael Ludwig73dbea62019-11-19 14:55:36 -050030// Generic WriteQuadProc that can handle any VertexSpec. It writes the 4 vertices in triangle strip
31// order, although the data per-vertex is dependent on the VertexSpec.
32static void write_quad_generic(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -050033 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -050034 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -040035 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050036 static constexpr auto If = GrVertexWriter::If<float>;
Michael Ludwig704d5402019-11-25 09:43:37 -050037
38 SkASSERT(!spec.hasLocalCoords() || localQuad);
39
Michael Ludwig73dbea62019-11-19 14:55:36 -050040 GrQuadPerEdgeAA::CoverageMode mode = spec.coverageMode();
Michael Ludwig553e9a92018-11-29 12:38:35 -050041 for (int i = 0; i < 4; ++i) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050042 // save position, this is a float2 or float3 or float4 depending on the combination of
43 // perspective and coverage mode.
Michael Ludwig704d5402019-11-25 09:43:37 -050044 vb->write(deviceQuad->x(i), deviceQuad->y(i),
45 If(spec.deviceQuadType() == GrQuad::Type::kPerspective, deviceQuad->w(i)),
Robert Phillips29f38542019-10-16 09:20:25 -040046 If(mode == GrQuadPerEdgeAA::CoverageMode::kWithPosition, coverage[i]));
Michael Ludwig4921dc32018-12-03 14:57:29 +000047
Michael Ludwig93aeba02018-12-21 09:50:31 -050048 // save color
49 if (spec.hasVertexColors()) {
Brian Osman2715bf52019-12-06 14:38:47 -050050 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwige6266a22019-03-07 11:24:32 -050051 vb->write(GrVertexColor(
Michael Ludwig73dbea62019-11-19 14:55:36 -050052 color * (mode == GrQuadPerEdgeAA::CoverageMode::kWithColor ? coverage[i] : 1.f),
Robert Phillips29f38542019-10-16 09:20:25 -040053 wide));
Michael Ludwig93aeba02018-12-21 09:50:31 -050054 }
55
56 // save local position
57 if (spec.hasLocalCoords()) {
Michael Ludwig704d5402019-11-25 09:43:37 -050058 vb->write(localQuad->x(i), localQuad->y(i),
59 If(spec.localQuadType() == GrQuad::Type::kPerspective, localQuad->w(i)));
Michael Ludwig93aeba02018-12-21 09:50:31 -050060 }
61
Brian Salomon2432d062020-04-16 20:48:09 -040062 // save the geometry subset
63 if (spec.requiresGeometrySubset()) {
64 vb->write(geomSubset);
Michael Ludwigdcfbe322019-04-01 14:55:54 -040065 }
66
Brian Salomon2432d062020-04-16 20:48:09 -040067 // save the texture subset
68 if (spec.hasSubset()) {
69 vb->write(texSubset);
Michael Ludwig93aeba02018-12-21 09:50:31 -050070 }
71 }
72}
73
Michael Ludwig73dbea62019-11-19 14:55:36 -050074// Specialized WriteQuadProcs for particular VertexSpecs that show up frequently (determined
75// experimentally through recorded GMs, SKPs, and SVGs, as well as SkiaRenderer's usage patterns):
76
Brian Salomon2432d062020-04-16 20:48:09 -040077// 2D (XY), no explicit coverage, vertex color, no locals, no geometry subset, no texture subsetn
Michael Ludwig73dbea62019-11-19 14:55:36 -050078// This represents simple, solid color or shader, non-AA (or AA with cov. as alpha) rects.
79static void write_2d_color(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -050080 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -050081 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -040082 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -050083 // Assert assumptions about VertexSpec
84 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
85 SkASSERT(!spec.hasLocalCoords());
86 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
87 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
88 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -040089 SkASSERT(!spec.requiresGeometrySubset());
90 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -050091 // We don't assert that localQuad == nullptr, since it is possible for GrFillRectOp to
92 // accumulate local coords conservatively (paint not trivial), and then after analysis realize
93 // the processors don't need local coordinates.
Michael Ludwig73dbea62019-11-19 14:55:36 -050094
Brian Osman2715bf52019-12-06 14:38:47 -050095 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -050096 for (int i = 0; i < 4; ++i) {
97 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
98 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
99 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500100 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500101 }
102}
103
Brian Salomon2432d062020-04-16 20:48:09 -0400104// 2D (XY), no explicit coverage, UV locals, no color, no geometry subset, no texture subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500105// This represents opaque, non AA, textured rects
106static void write_2d_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500107 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500108 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400109 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500110 // Assert assumptions about VertexSpec
111 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
112 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
113 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone);
114 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400115 SkASSERT(!spec.requiresGeometrySubset());
116 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500117 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500118
119 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500120 vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500121 }
122}
123
Brian Salomon2432d062020-04-16 20:48:09 -0400124// 2D (XY), no explicit coverage, UV locals, vertex color, no geometry or texture subsets
Michael Ludwig73dbea62019-11-19 14:55:36 -0500125// This represents transparent, non AA (or AA with cov. as alpha), textured rects
126static void write_2d_color_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500127 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500128 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400129 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500130 // Assert assumptions about VertexSpec
131 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
132 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
133 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
134 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
135 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400136 SkASSERT(!spec.requiresGeometrySubset());
137 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500138 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500139
Brian Osman2715bf52019-12-06 14:38:47 -0500140 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500141 for (int i = 0; i < 4; ++i) {
142 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
143 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
144 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500145 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide),
146 localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500147 }
148}
149
Brian Salomon2432d062020-04-16 20:48:09 -0400150// 2D (XY), explicit coverage, UV locals, no color, no geometry subset, no texture subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500151// This represents opaque, AA, textured rects
152static void write_2d_cov_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500153 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500154 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400155 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500156 // Assert assumptions about VertexSpec
157 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
158 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
159 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition);
160 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400161 SkASSERT(!spec.requiresGeometrySubset());
162 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500163 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500164
165 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500166 vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i],
167 localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500168 }
169}
170
171// NOTE: The three _strict specializations below match the non-strict uv functions above, except
Brian Salomon2432d062020-04-16 20:48:09 -0400172// that they also write the UV subset. These are included to benefit SkiaRenderer, which must make
173// use of both fast and strict constrained subsets. When testing _strict was not that common across
Michael Ludwig73dbea62019-11-19 14:55:36 -0500174// GMS, SKPs, and SVGs but we have little visibility into actual SkiaRenderer statistics. If
Brian Salomon2432d062020-04-16 20:48:09 -0400175// SkiaRenderer can avoid subsets more, these 3 functions should probably be removed for simplicity.
Michael Ludwig73dbea62019-11-19 14:55:36 -0500176
Brian Salomon2432d062020-04-16 20:48:09 -0400177// 2D (XY), no explicit coverage, UV locals, no color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500178// This represents opaque, non AA, textured rects with strict uv sampling
179static void write_2d_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500180 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500181 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400182 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500183 // Assert assumptions about VertexSpec
184 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
185 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
186 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone);
187 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400188 SkASSERT(!spec.requiresGeometrySubset());
189 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500190 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500191
192 for (int i = 0; i < 4; ++i) {
Brian Salomon2432d062020-04-16 20:48:09 -0400193 vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500194 }
195}
196
Brian Salomon2432d062020-04-16 20:48:09 -0400197// 2D (XY), no explicit coverage, UV locals, vertex color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500198// This represents transparent, non AA (or AA with cov. as alpha), textured rects with strict sample
199static void write_2d_color_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500200 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500201 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400202 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500203 // Assert assumptions about VertexSpec
204 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
205 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
206 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
207 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
208 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400209 SkASSERT(!spec.requiresGeometrySubset());
210 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500211 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500212
Brian Osman2715bf52019-12-06 14:38:47 -0500213 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500214 for (int i = 0; i < 4; ++i) {
215 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
216 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
217 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500218 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide),
Brian Salomon2432d062020-04-16 20:48:09 -0400219 localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500220 }
221}
222
Brian Salomon2432d062020-04-16 20:48:09 -0400223// 2D (XY), explicit coverage, UV locals, no color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500224// This represents opaque, AA, textured rects with strict uv sampling
225static void write_2d_cov_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500226 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500227 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400228 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500229 // Assert assumptions about VertexSpec
230 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
231 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
232 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition);
233 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400234 SkASSERT(!spec.requiresGeometrySubset());
235 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500236 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500237
238 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500239 vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i],
Brian Salomon2432d062020-04-16 20:48:09 -0400240 localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500241 }
242}
243
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400244} // anonymous namespace
245
Michael Ludwigc182b942018-11-16 10:27:51 -0500246namespace GrQuadPerEdgeAA {
247
Brian Salomonb8c4add2021-06-28 09:20:44 -0400248IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads) {
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400249 if (aa == GrAAType::kCoverage) {
250 return IndexBufferOption::kPictureFramed;
Brian Salomonb8c4add2021-06-28 09:20:44 -0400251 } else if (numQuads > 1) {
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400252 return IndexBufferOption::kIndexedRects;
253 } else {
254 return IndexBufferOption::kTriStrips;
255 }
256}
257
Brian Osman2715bf52019-12-06 14:38:47 -0500258// This is a more elaborate version of fitsInBytes() that allows "no color" for white
259ColorType MinColorType(SkPMColor4f color) {
Brian Salomon1d835422019-03-13 16:11:44 -0400260 if (color == SK_PMColor4fWHITE) {
261 return ColorType::kNone;
Brian Salomon1d835422019-03-13 16:11:44 -0400262 } else {
Brian Osman2715bf52019-12-06 14:38:47 -0500263 return color.fitsInBytes() ? ColorType::kByte : ColorType::kFloat;
Brian Salomon1d835422019-03-13 16:11:44 -0400264 }
265}
266
Michael Ludwig73dbea62019-11-19 14:55:36 -0500267////////////////// Tessellator Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500268
Michael Ludwig73dbea62019-11-19 14:55:36 -0500269Tessellator::WriteQuadProc Tessellator::GetWriteQuadProc(const VertexSpec& spec) {
Brian Salomon2432d062020-04-16 20:48:09 -0400270 // All specialized writing functions requires 2D geometry and no geometry subset. This is not
Michael Ludwig73dbea62019-11-19 14:55:36 -0500271 // the same as just checking device type vs. kRectilinear since non-AA general 2D quads do not
Brian Salomon2432d062020-04-16 20:48:09 -0400272 // require a geometry subset and could then go through a fast path.
273 if (spec.deviceQuadType() != GrQuad::Type::kPerspective && !spec.requiresGeometrySubset()) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500274 CoverageMode mode = spec.coverageMode();
275 if (spec.hasVertexColors()) {
276 if (mode != CoverageMode::kWithPosition) {
277 // Vertex colors, but no explicit coverage
278 if (!spec.hasLocalCoords()) {
279 // Non-UV with vertex colors (possibly with coverage folded into alpha)
280 return write_2d_color;
281 } else if (spec.localQuadType() != GrQuad::Type::kPerspective) {
282 // UV locals with vertex colors (possibly with coverage-as-alpha)
Brian Salomon2432d062020-04-16 20:48:09 -0400283 return spec.hasSubset() ? write_2d_color_uv_strict : write_2d_color_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500284 }
285 }
286 // Else fall through; this is a spec that requires vertex colors and explicit coverage,
287 // which means it's anti-aliased and the FPs don't support coverage as alpha, or
288 // it uses 3D local coordinates.
289 } else if (spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective) {
290 if (mode == CoverageMode::kWithPosition) {
291 // UV locals with explicit coverage
Brian Salomon2432d062020-04-16 20:48:09 -0400292 return spec.hasSubset() ? write_2d_cov_uv_strict : write_2d_cov_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500293 } else {
294 SkASSERT(mode == CoverageMode::kNone);
Brian Salomon2432d062020-04-16 20:48:09 -0400295 return spec.hasSubset() ? write_2d_uv_strict : write_2d_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500296 }
297 }
298 // Else fall through to generic vertex function; this is a spec that has no vertex colors
299 // and [no|uvr] local coords, which doesn't happen often enough to warrant specialization.
300 }
Michael Ludwig41f395d2019-05-23 13:59:45 -0400301
Michael Ludwig73dbea62019-11-19 14:55:36 -0500302 // Arbitrary spec hits the slow path
303 return write_quad_generic;
304}
305
Michael Ludwig189c9802019-11-21 11:21:12 -0500306Tessellator::Tessellator(const VertexSpec& spec, char* vertices)
Michael Ludwig73dbea62019-11-19 14:55:36 -0500307 : fVertexSpec(spec)
Michael Ludwig189c9802019-11-21 11:21:12 -0500308 , fVertexWriter{vertices}
Michael Ludwig73dbea62019-11-19 14:55:36 -0500309 , fWriteProc(Tessellator::GetWriteQuadProc(spec)) {}
310
Michael Ludwig704d5402019-11-25 09:43:37 -0500311void Tessellator::append(GrQuad* deviceQuad, GrQuad* localQuad,
Brian Salomon2432d062020-04-16 20:48:09 -0400312 const SkPMColor4f& color, const SkRect& uvSubset, GrQuadAAFlags aaFlags) {
Michael Ludwig189c9802019-11-21 11:21:12 -0500313 // We allow Tessellator to be created with a null vertices pointer for convenience, but it is
314 // assumed it will never actually be used in those cases.
315 SkASSERT(fVertexWriter.fPtr);
Michael Ludwig704d5402019-11-25 09:43:37 -0500316 SkASSERT(deviceQuad->quadType() <= fVertexSpec.deviceQuadType());
317 SkASSERT(localQuad || !fVertexSpec.hasLocalCoords());
318 SkASSERT(!fVertexSpec.hasLocalCoords() || localQuad->quadType() <= fVertexSpec.localQuadType());
Michael Ludwig73dbea62019-11-19 14:55:36 -0500319
320 static const float kFullCoverage[4] = {1.f, 1.f, 1.f, 1.f};
321 static const float kZeroCoverage[4] = {0.f, 0.f, 0.f, 0.f};
Brian Salomon2432d062020-04-16 20:48:09 -0400322 static const SkRect kIgnoredSubset = SkRect::MakeEmpty();
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400323
Michael Ludwig73dbea62019-11-19 14:55:36 -0500324 if (fVertexSpec.usesCoverageAA()) {
325 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kWithColor ||
326 fVertexSpec.coverageMode() == CoverageMode::kWithPosition);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400327 // Must calculate inner and outer quadrilaterals for the vertex coverage ramps, and possibly
Brian Salomon2432d062020-04-16 20:48:09 -0400328 // a geometry subset if corners are not right angles
329 SkRect geomSubset;
330 if (fVertexSpec.requiresGeometrySubset()) {
Brian Salomon659e71f2021-02-24 10:09:02 -0500331#ifdef SK_USE_LEGACY_AA_QUAD_SUBSET
Brian Salomon2432d062020-04-16 20:48:09 -0400332 geomSubset = deviceQuad->bounds();
333 geomSubset.outset(0.5f, 0.5f); // account for AA expansion
Brian Salomon659e71f2021-02-24 10:09:02 -0500334#else
335 // Our GP code expects a 0.5 outset rect (coverage is computed as 0 at the values of
336 // the uniform). However, if we have quad edges that aren't supposed to be antialiased
337 // they may lie close to the bounds. So in that case we outset by an additional 0.5.
338 // This is a sort of backup clipping mechanism for cases where quad outsetting of nearly
339 // parallel edges produces long thin extrusions from the original geometry.
340 float outset = aaFlags == GrQuadAAFlags::kAll ? 0.5f : 1.f;
341 geomSubset = deviceQuad->bounds().makeOutset(outset, outset);
342#endif
Michael Ludwige6266a22019-03-07 11:24:32 -0500343 }
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400344
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500345 if (aaFlags == GrQuadAAFlags::kNone) {
346 // Have to write the coverage AA vertex structure, but there's no math to be done for a
347 // non-aa quad batched into a coverage AA op.
Michael Ludwig189c9802019-11-21 11:21:12 -0500348 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400349 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500350 // Since we pass the same corners in, the outer vertex structure will have 0 area and
351 // the coverage interpolation from 1 to 0 will not be visible.
Michael Ludwig189c9802019-11-21 11:21:12 -0500352 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400353 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500354 } else {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500355 // Reset the tessellation helper to match the current geometry
Michael Ludwig704d5402019-11-25 09:43:37 -0500356 fAAHelper.reset(*deviceQuad, localQuad);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400357
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500358 // Edge inset/outset distance ordered LBTR, set to 0.5 for a half pixel if the AA flag
359 // is turned on, or 0.0 if the edge is not anti-aliased.
360 skvx::Vec<4, float> edgeDistances;
361 if (aaFlags == GrQuadAAFlags::kAll) {
362 edgeDistances = 0.5f;
363 } else {
364 edgeDistances = { (aaFlags & GrQuadAAFlags::kLeft) ? 0.5f : 0.f,
365 (aaFlags & GrQuadAAFlags::kBottom) ? 0.5f : 0.f,
366 (aaFlags & GrQuadAAFlags::kTop) ? 0.5f : 0.f,
367 (aaFlags & GrQuadAAFlags::kRight) ? 0.5f : 0.f };
368 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400369
Michael Ludwig73dbea62019-11-19 14:55:36 -0500370 // Write inner vertices first
371 float coverage[4];
Michael Ludwig704d5402019-11-25 09:43:37 -0500372 fAAHelper.inset(edgeDistances, deviceQuad, localQuad).store(coverage);
373 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, coverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400374 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500375
Michael Ludwig290d6df2020-10-27 09:28:59 -0400376 // Then outer vertices, which use 0.f for their coverage. If the inset was degenerate
377 // to a line (had all coverages < 1), tweak the outset distance so the outer frame's
378 // narrow axis reaches out to 2px, which gives better animation under translation.
379 if (coverage[0] < 1.f && coverage[1] < 1.f && coverage[2] < 1.f && coverage[3] < 1.f) {
380 skvx::Vec<4, float> len = fAAHelper.getEdgeLengths();
381 // Using max guards us against trying to scale a degenerate triangle edge of 0 len
382 // up to 2px. The shuffles are so that edge 0's adjustment is based on the lengths
383 // of its connecting edges (1 and 2), and so forth.
384 skvx::Vec<4, float> maxWH = max(skvx::shuffle<1, 0, 3, 2>(len),
385 skvx::shuffle<2, 3, 0, 1>(len));
386 // wh + 2e' = 2, so e' = (2 - wh) / 2 => e' = e * (2 - wh). But if w or h > 1, then
387 // 2 - wh < 1 and represents the non-narrow axis so clamp to 1.
388 edgeDistances *= max(1.f, 2.f - maxWH);
389 }
Michael Ludwig704d5402019-11-25 09:43:37 -0500390 fAAHelper.outset(edgeDistances, deviceQuad, localQuad);
391 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400392 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500393 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500394 } else {
395 // No outsetting needed, just write a single quad with full coverage
Michael Ludwig73dbea62019-11-19 14:55:36 -0500396 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kNone &&
Brian Salomon2432d062020-04-16 20:48:09 -0400397 !fVertexSpec.requiresGeometrySubset());
Michael Ludwig189c9802019-11-21 11:21:12 -0500398 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400399 kIgnoredSubset, uvSubset);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400400 }
401}
Michael Ludwig20e909e2018-10-30 10:43:57 -0400402
Robert Phillips71143952021-06-17 14:55:07 -0400403sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawTarget* target,
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400404 IndexBufferOption indexBufferOption) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400405 auto resourceProvider = target->resourceProvider();
406
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400407 switch (indexBufferOption) {
408 case IndexBufferOption::kPictureFramed: return resourceProvider->refAAQuadIndexBuffer();
409 case IndexBufferOption::kIndexedRects: return resourceProvider->refNonAAQuadIndexBuffer();
410 case IndexBufferOption::kTriStrips: // fall through
411 default: return nullptr;
412 }
413}
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400414
Robert Phillips438d9862019-11-14 12:46:05 -0500415int QuadLimit(IndexBufferOption option) {
416 switch (option) {
417 case IndexBufferOption::kPictureFramed: return GrResourceProvider::MaxNumAAQuads();
418 case IndexBufferOption::kIndexedRects: return GrResourceProvider::MaxNumNonAAQuads();
419 case IndexBufferOption::kTriStrips: return SK_MaxS32; // not limited by an indexBuffer
420 }
421
422 SkUNREACHABLE;
423}
Michael Ludwig93aeba02018-12-21 09:50:31 -0500424
Chris Daltondbb833b2020-03-17 12:15:46 -0600425void IssueDraw(const GrCaps& caps, GrOpsRenderPass* renderPass, const VertexSpec& spec,
426 int runningQuadCount, int quadsInDraw, int maxVerts, int absVertBufferOffset) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400427 if (spec.indexBufferOption() == IndexBufferOption::kTriStrips) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400428 int offset = absVertBufferOffset +
429 runningQuadCount * GrResourceProvider::NumVertsPerNonAAQuad();
Chris Daltondbb833b2020-03-17 12:15:46 -0600430 renderPass->draw(4, offset);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400431 return;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500432 }
433
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400434 SkASSERT(spec.indexBufferOption() == IndexBufferOption::kPictureFramed ||
435 spec.indexBufferOption() == IndexBufferOption::kIndexedRects);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400436
Robert Phillips2f05a482019-11-25 09:54:43 -0500437 int maxNumQuads, numIndicesPerQuad, numVertsPerQuad;
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400438
439 if (spec.indexBufferOption() == IndexBufferOption::kPictureFramed) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400440 // AA uses 8 vertices and 30 indices per quad, basically nested rectangles
Robert Phillips2f05a482019-11-25 09:54:43 -0500441 maxNumQuads = GrResourceProvider::MaxNumAAQuads();
442 numIndicesPerQuad = GrResourceProvider::NumIndicesPerAAQuad();
443 numVertsPerQuad = GrResourceProvider::NumVertsPerAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400444 } else {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400445 // Non-AA uses 4 vertices and 6 indices per quad
Robert Phillips2f05a482019-11-25 09:54:43 -0500446 maxNumQuads = GrResourceProvider::MaxNumNonAAQuads();
447 numIndicesPerQuad = GrResourceProvider::NumIndicesPerNonAAQuad();
448 numVertsPerQuad = GrResourceProvider::NumVertsPerNonAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400449 }
450
Robert Phillips2f05a482019-11-25 09:54:43 -0500451 SkASSERT(runningQuadCount + quadsInDraw <= maxNumQuads);
452
453 if (caps.avoidLargeIndexBufferDraws()) {
454 // When we need to avoid large index buffer draws we modify the base vertex of the draw
455 // which, in GL, requires rebinding all vertex attrib arrays, so a base index is generally
456 // preferred.
457 int offset = absVertBufferOffset + runningQuadCount * numVertsPerQuad;
458
Chris Daltondbb833b2020-03-17 12:15:46 -0600459 renderPass->drawIndexPattern(numIndicesPerQuad, quadsInDraw, maxNumQuads, numVertsPerQuad,
460 offset);
Robert Phillips2f05a482019-11-25 09:54:43 -0500461 } else {
462 int baseIndex = runningQuadCount * numIndicesPerQuad;
463 int numIndicesToDraw = quadsInDraw * numIndicesPerQuad;
464
465 int minVertex = runningQuadCount * numVertsPerQuad;
Michael Ludwigfa1cb402021-06-02 10:53:16 -0400466 int maxVertex = (runningQuadCount + quadsInDraw) * numVertsPerQuad - 1; // inclusive
Robert Phillips2f05a482019-11-25 09:54:43 -0500467
Chris Daltondbb833b2020-03-17 12:15:46 -0600468 renderPass->drawIndexed(numIndicesToDraw, baseIndex, minVertex, maxVertex,
469 absVertBufferOffset);
Robert Phillips2f05a482019-11-25 09:54:43 -0500470 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500471}
472
Michael Ludwigc182b942018-11-16 10:27:51 -0500473////////////////// VertexSpec Implementation
Michael Ludwig20e909e2018-10-30 10:43:57 -0400474
Michael Ludwigc182b942018-11-16 10:27:51 -0500475int VertexSpec::deviceDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400476 return this->deviceQuadType() == GrQuad::Type::kPerspective ? 3 : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -0500477}
478
479int VertexSpec::localDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400480 return fHasLocalCoords ? (this->localQuadType() == GrQuad::Type::kPerspective ? 3 : 2) : 0;
Michael Ludwigc182b942018-11-16 10:27:51 -0500481}
482
Robert Phillips29f38542019-10-16 09:20:25 -0400483CoverageMode VertexSpec::coverageMode() const {
484 if (this->usesCoverageAA()) {
485 if (this->compatibleWithCoverageAsAlpha() && this->hasVertexColors() &&
Brian Salomon2432d062020-04-16 20:48:09 -0400486 !this->requiresGeometrySubset()) {
487 // Using a geometric subset acts as a second source of coverage and folding
Robert Phillips29f38542019-10-16 09:20:25 -0400488 // the original coverage into color makes it impossible to apply the color's
Brian Salomon2432d062020-04-16 20:48:09 -0400489 // alpha to the geometric subset's coverage when the original shape is clipped.
Robert Phillips29f38542019-10-16 09:20:25 -0400490 return CoverageMode::kWithColor;
491 } else {
492 return CoverageMode::kWithPosition;
493 }
494 } else {
495 return CoverageMode::kNone;
496 }
497}
498
499// This needs to stay in sync w/ QuadPerEdgeAAGeometryProcessor::initializeAttrs
500size_t VertexSpec::vertexSize() const {
501 bool needsPerspective = (this->deviceDimensionality() == 3);
502 CoverageMode coverageMode = this->coverageMode();
503
504 size_t count = 0;
505
506 if (coverageMode == CoverageMode::kWithPosition) {
507 if (needsPerspective) {
508 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
509 } else {
510 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType) +
511 GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
512 }
513 } else {
514 if (needsPerspective) {
515 count += GrVertexAttribTypeSize(kFloat3_GrVertexAttribType);
516 } else {
517 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType);
518 }
519 }
520
Brian Salomon2432d062020-04-16 20:48:09 -0400521 if (this->requiresGeometrySubset()) {
Robert Phillips29f38542019-10-16 09:20:25 -0400522 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
523 }
524
525 count += this->localDimensionality() * GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
526
527 if (ColorType::kByte == this->colorType()) {
528 count += GrVertexAttribTypeSize(kUByte4_norm_GrVertexAttribType);
Brian Osman2715bf52019-12-06 14:38:47 -0500529 } else if (ColorType::kFloat == this->colorType()) {
530 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
Robert Phillips29f38542019-10-16 09:20:25 -0400531 }
532
Brian Salomon2432d062020-04-16 20:48:09 -0400533 if (this->hasSubset()) {
Robert Phillips29f38542019-10-16 09:20:25 -0400534 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
535 }
536
537 return count;
538}
539
Michael Ludwig467994d2018-12-03 14:58:31 +0000540////////////////// Geometry Processor Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500541
Michael Ludwig467994d2018-12-03 14:58:31 +0000542class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor {
543public:
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400544 using Saturate = GrTextureOp::Saturate;
Michael Ludwig20e909e2018-10-30 10:43:57 -0400545
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500546 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const VertexSpec& spec) {
Mike Kleinf1241082020-12-14 15:59:09 -0600547 return arena->make([&](void* ptr) {
548 return new (ptr) QuadPerEdgeAAGeometryProcessor(spec);
549 });
Michael Ludwig20e909e2018-10-30 10:43:57 -0400550 }
551
Brian Salomonccb61422020-01-09 10:46:36 -0500552 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
553 const VertexSpec& vertexSpec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500554 const GrShaderCaps& caps,
555 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500556 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500557 const GrSwizzle& swizzle,
558 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
559 Saturate saturate) {
Mike Kleinf1241082020-12-14 15:59:09 -0600560 return arena->make([&](void* ptr) {
561 return new (ptr) QuadPerEdgeAAGeometryProcessor(
562 vertexSpec, caps, backendFormat, samplerState, swizzle,
563 std::move(textureColorSpaceXform), saturate);
564 });
Michael Ludwig20e909e2018-10-30 10:43:57 -0400565 }
566
Michael Ludwig467994d2018-12-03 14:58:31 +0000567 const char* name() const override { return "QuadPerEdgeAAGeometryProcessor"; }
Michael Ludwig024e2622018-11-30 13:25:55 -0500568
Michael Ludwig467994d2018-12-03 14:58:31 +0000569 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400570 // texturing, device-dimensions are single bit flags
Brian Osman48d7f7c2021-03-03 13:38:08 -0500571 b->addBool(fTexSubset.isInitialized(), "subset");
572 b->addBool(fSampler.isInitialized(), "textured");
573 b->addBool(fNeedsPerspective, "perspective");
574 b->addBool((fSaturate == Saturate::kYes), "saturate");
575
576 b->addBool(fLocalCoord.isInitialized(), "hasLocalCoords");
Michael Ludwig467994d2018-12-03 14:58:31 +0000577 if (fLocalCoord.isInitialized()) {
Brian Osman48d7f7c2021-03-03 13:38:08 -0500578 // 2D (0) or 3D (1)
579 b->addBits(1, (kFloat3_GrVertexAttribType == fLocalCoord.cpuType()), "localCoordsType");
Brian Osman78dc72c2018-12-03 13:20:43 +0000580 }
Brian Osman48d7f7c2021-03-03 13:38:08 -0500581 b->addBool(fColor.isInitialized(), "hasColor");
Michael Ludwig93aeba02018-12-21 09:50:31 -0500582 if (fColor.isInitialized()) {
Brian Osman48d7f7c2021-03-03 13:38:08 -0500583 // bytes (0) or floats (1)
584 b->addBits(1, (kFloat4_GrVertexAttribType == fColor.cpuType()), "colorType");
Michael Ludwig93aeba02018-12-21 09:50:31 -0500585 }
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400586 // and coverage mode, 00 for none, 01 for withposition, 10 for withcolor, 11 for
Brian Salomon2432d062020-04-16 20:48:09 -0400587 // position+geomsubset
Brian Osman48d7f7c2021-03-03 13:38:08 -0500588 uint32_t coverageKey = 0;
Brian Salomon2432d062020-04-16 20:48:09 -0400589 SkASSERT(!fGeomSubset.isInitialized() || fCoverageMode == CoverageMode::kWithPosition);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500590 if (fCoverageMode != CoverageMode::kNone) {
Brian Osman48d7f7c2021-03-03 13:38:08 -0500591 coverageKey = fGeomSubset.isInitialized()
592 ? 0x3
593 : (CoverageMode::kWithPosition == fCoverageMode ? 0x1 : 0x2);
Michael Ludwig467994d2018-12-03 14:58:31 +0000594 }
Brian Osman48d7f7c2021-03-03 13:38:08 -0500595 b->addBits(2, coverageKey, "coverageMode");
Michael Ludwig467994d2018-12-03 14:58:31 +0000596
Brian Osman48d7f7c2021-03-03 13:38:08 -0500597 b->add32(GrColorSpaceXform::XformKey(fTextureColorSpaceXform.get()), "colorSpaceXform");
Brian Osman78dc72c2018-12-03 13:20:43 +0000598 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000599
Robert Phillipsf10535f2021-03-23 09:30:45 -0400600 GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
Michael Ludwig467994d2018-12-03 14:58:31 +0000601 class GLSLProcessor : public GrGLSLGeometryProcessor {
602 public:
Brian Osman609f1592020-07-01 15:14:39 -0400603 void setData(const GrGLSLProgramDataManager& pdman,
Brian Salomon5a328282021-04-14 10:32:25 -0400604 const GrShaderCaps&,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400605 const GrGeometryProcessor& geomProc) override {
606 const auto& gp = geomProc.cast<QuadPerEdgeAAGeometryProcessor>();
Michael Ludwig467994d2018-12-03 14:58:31 +0000607 fTextureColorSpaceXformHelper.setData(pdman, gp.fTextureColorSpaceXform.get());
608 }
609
610 private:
611 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
612 using Interpolation = GrGLSLVaryingHandler::Interpolation;
613
Robert Phillips787fd9d2021-03-22 14:48:09 -0400614 const auto& gp = args.fGeomProc.cast<QuadPerEdgeAAGeometryProcessor>();
Michael Ludwig467994d2018-12-03 14:58:31 +0000615 fTextureColorSpaceXformHelper.emitCode(args.fUniformHandler,
616 gp.fTextureColorSpaceXform.get());
617
618 args.fVaryingHandler->emitAttributes(gp);
619
Michael Ludwig93aeba02018-12-21 09:50:31 -0500620 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
621 // Strip last channel from the vertex attribute to remove coverage and get the
622 // actual position
623 if (gp.fNeedsPerspective) {
624 args.fVertBuilder->codeAppendf("float3 position = %s.xyz;",
625 gp.fPosition.name());
626 } else {
627 args.fVertBuilder->codeAppendf("float2 position = %s.xy;",
628 gp.fPosition.name());
629 }
630 gpArgs->fPositionVar = {"position",
631 gp.fNeedsPerspective ? kFloat3_GrSLType
632 : kFloat2_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400633 GrShaderVar::TypeModifier::None};
Michael Ludwig467994d2018-12-03 14:58:31 +0000634 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500635 // No coverage to eliminate
636 gpArgs->fPositionVar = gp.fPosition.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000637 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000638
Michael Ludwig553db622020-06-19 10:47:30 -0400639 // This attribute will be uninitialized if earlier FP analysis determined no
640 // local coordinates are needed (and this will not include the inline texture
641 // fetch this GP does before invoking FPs).
642 gpArgs->fLocalCoordVar = gp.fLocalCoord.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000643
644 // Solid color before any texturing gets modulated in
John Stiles4d7ac492021-03-09 20:16:43 -0500645 const char* blendDst;
Michael Ludwig467994d2018-12-03 14:58:31 +0000646 if (gp.fColor.isInitialized()) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400647 SkASSERT(gp.fCoverageMode != CoverageMode::kWithColor || !gp.fNeedsPerspective);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500648 // The color cannot be flat if the varying coverage has been modulated into it
John Stiles4d7ac492021-03-09 20:16:43 -0500649 args.fFragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
Michael Ludwig467994d2018-12-03 14:58:31 +0000650 args.fVaryingHandler->addPassThroughAttribute(gp.fColor, args.fOutputColor,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500651 gp.fCoverageMode == CoverageMode::kWithColor ?
652 Interpolation::kInterpolated : Interpolation::kCanBeFlat);
John Stiles4d7ac492021-03-09 20:16:43 -0500653 blendDst = args.fOutputColor;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500654 } else {
655 // Output color must be initialized to something
John Stiles4d7ac492021-03-09 20:16:43 -0500656 args.fFragBuilder->codeAppendf("half4 %s = half4(1);", args.fOutputColor);
657 blendDst = nullptr;
Michael Ludwig467994d2018-12-03 14:58:31 +0000658 }
659
660 // If there is a texture, must also handle texture coordinates and reading from
661 // the texture in the fragment shader before continuing to fragment processors.
662 if (gp.fSampler.isInitialized()) {
Brian Salomon2432d062020-04-16 20:48:09 -0400663 // Texture coordinates clamped by the subset on the fragment shader; if the GP
Michael Ludwig467994d2018-12-03 14:58:31 +0000664 // has a texture, it's guaranteed to have local coordinates
665 args.fFragBuilder->codeAppend("float2 texCoord;");
666 if (gp.fLocalCoord.cpuType() == kFloat3_GrVertexAttribType) {
667 // Can't do a pass through since we need to perform perspective division
668 GrGLSLVarying v(gp.fLocalCoord.gpuType());
669 args.fVaryingHandler->addVarying(gp.fLocalCoord.name(), &v);
670 args.fVertBuilder->codeAppendf("%s = %s;",
671 v.vsOut(), gp.fLocalCoord.name());
672 args.fFragBuilder->codeAppendf("texCoord = %s.xy / %s.z;",
673 v.fsIn(), v.fsIn());
674 } else {
675 args.fVaryingHandler->addPassThroughAttribute(gp.fLocalCoord, "texCoord");
676 }
677
Brian Salomon2432d062020-04-16 20:48:09 -0400678 // Clamp the now 2D localCoordName variable by the subset if it is provided
679 if (gp.fTexSubset.isInitialized()) {
680 args.fFragBuilder->codeAppend("float4 subset;");
681 args.fVaryingHandler->addPassThroughAttribute(gp.fTexSubset, "subset",
Michael Ludwig467994d2018-12-03 14:58:31 +0000682 Interpolation::kCanBeFlat);
683 args.fFragBuilder->codeAppend(
John Stiles4d7ac492021-03-09 20:16:43 -0500684 "texCoord = clamp(texCoord, subset.LT, subset.RB);");
Michael Ludwig467994d2018-12-03 14:58:31 +0000685 }
686
687 // Now modulate the starting output color by the texture lookup
John Stiles4d7ac492021-03-09 20:16:43 -0500688 args.fFragBuilder->codeAppendf(
689 "%s = %s(",
690 args.fOutputColor,
691 (gp.fSaturate == Saturate::kYes) ? "saturate" : "");
Brian Salomon87e9ddb2019-12-19 14:50:22 -0500692 args.fFragBuilder->appendTextureLookupAndBlend(
John Stiles4d7ac492021-03-09 20:16:43 -0500693 blendDst, SkBlendMode::kModulate, args.fTexSamplers[0],
Brian Salomond19cd762020-01-06 13:16:31 -0500694 "texCoord", &fTextureColorSpaceXformHelper);
John Stiles4d7ac492021-03-09 20:16:43 -0500695 args.fFragBuilder->codeAppend(");");
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400696 } else {
697 // Saturate is only intended for use with a proxy to account for the fact
698 // that GrTextureOp skips SkPaint conversion, which normally handles this.
699 SkASSERT(gp.fSaturate == Saturate::kNo);
Michael Ludwig467994d2018-12-03 14:58:31 +0000700 }
701
702 // And lastly, output the coverage calculation code
Michael Ludwig93aeba02018-12-21 09:50:31 -0500703 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
704 GrGLSLVarying coverage(kFloat_GrSLType);
705 args.fVaryingHandler->addVarying("coverage", &coverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000706 if (gp.fNeedsPerspective) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400707 // Multiply by "W" in the vertex shader, then by 1/w (sk_FragCoord.w) in
708 // the fragment shader to get screen-space linear coverage.
709 args.fVertBuilder->codeAppendf("%s = %s.w * %s.z;",
710 coverage.vsOut(), gp.fPosition.name(),
711 gp.fPosition.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400712 args.fFragBuilder->codeAppendf("float coverage = %s * sk_FragCoord.w;",
713 coverage.fsIn());
Michael Ludwig93aeba02018-12-21 09:50:31 -0500714 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400715 args.fVertBuilder->codeAppendf("%s = %s;",
716 coverage.vsOut(), gp.fCoverage.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400717 args.fFragBuilder->codeAppendf("float coverage = %s;", coverage.fsIn());
Michael Ludwig467994d2018-12-03 14:58:31 +0000718 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500719
Brian Salomon2432d062020-04-16 20:48:09 -0400720 if (gp.fGeomSubset.isInitialized()) {
721 // Calculate distance from sk_FragCoord to the 4 edges of the subset
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400722 // and clamp them to (0, 1). Use the minimum of these and the original
723 // coverage. This only has to be done in the exterior triangles, the
Brian Salomon2432d062020-04-16 20:48:09 -0400724 // interior of the quad geometry can never be clipped by the subset box.
725 args.fFragBuilder->codeAppend("float4 geoSubset;");
726 args.fVaryingHandler->addPassThroughAttribute(gp.fGeomSubset, "geoSubset",
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400727 Interpolation::kCanBeFlat);
Brian Salomon659e71f2021-02-24 10:09:02 -0500728#ifdef SK_USE_LEGACY_AA_QUAD_SUBSET
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400729 args.fFragBuilder->codeAppend(
730 "if (coverage < 0.5) {"
731 " float4 dists4 = clamp(float4(1, 1, -1, -1) * "
Brian Salomon2432d062020-04-16 20:48:09 -0400732 "(sk_FragCoord.xyxy - geoSubset), 0, 1);"
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400733 " float2 dists2 = dists4.xy * dists4.zw;"
734 " coverage = min(coverage, dists2.x * dists2.y);"
735 "}");
Brian Salomon659e71f2021-02-24 10:09:02 -0500736#else
737 args.fFragBuilder->codeAppend(
738 // This is lifted from GrAARectEffect. It'd be nice if we could
739 // invoke a FP from a GP rather than duplicate this code.
740 "half4 dists4 = clamp(half4(1, 1, -1, -1) * "
741 "half4(sk_FragCoord.xyxy - geoSubset), 0, 1);\n"
742 "half2 dists2 = dists4.xy + dists4.zw - 1;\n"
743 "half subsetCoverage = dists2.x * dists2.y;\n"
744 "coverage = min(coverage, subsetCoverage);");
745#endif
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400746 }
747
John Stiles4d7ac492021-03-09 20:16:43 -0500748 args.fFragBuilder->codeAppendf("half4 %s = half4(half(coverage));",
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400749 args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000750 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500751 // Set coverage to 1, since it's either non-AA or the coverage was already
752 // folded into the output color
Brian Salomon2432d062020-04-16 20:48:09 -0400753 SkASSERT(!gp.fGeomSubset.isInitialized());
John Stiles4d7ac492021-03-09 20:16:43 -0500754 args.fFragBuilder->codeAppendf("const half4 %s = half4(1);",
755 args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000756 }
757 }
758 GrGLSLColorSpaceXformHelper fTextureColorSpaceXformHelper;
759 };
760 return new GLSLProcessor;
761 }
762
763private:
764 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec)
765 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
766 , fTextureColorSpaceXform(nullptr) {
Brian Salomon2432d062020-04-16 20:48:09 -0400767 SkASSERT(!spec.hasSubset());
Michael Ludwig467994d2018-12-03 14:58:31 +0000768 this->initializeAttrs(spec);
769 this->setTextureSamplerCnt(0);
770 }
771
Brian Salomon67529b22019-08-13 15:31:04 -0400772 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec,
773 const GrShaderCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400774 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500775 GrSamplerState samplerState,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400776 const GrSwizzle& swizzle,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400777 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
778 Saturate saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000779 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400780 , fSaturate(saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000781 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Robert Phillips323471e2019-11-11 11:33:37 -0500782 , fSampler(samplerState, backendFormat, swizzle) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500783 SkASSERT(spec.hasLocalCoords());
Michael Ludwig467994d2018-12-03 14:58:31 +0000784 this->initializeAttrs(spec);
785 this->setTextureSamplerCnt(1);
786 }
787
Robert Phillips29f38542019-10-16 09:20:25 -0400788 // This needs to stay in sync w/ VertexSpec::vertexSize
Michael Ludwig467994d2018-12-03 14:58:31 +0000789 void initializeAttrs(const VertexSpec& spec) {
790 fNeedsPerspective = spec.deviceDimensionality() == 3;
Robert Phillips29f38542019-10-16 09:20:25 -0400791 fCoverageMode = spec.coverageMode();
Michael Ludwig93aeba02018-12-21 09:50:31 -0500792
793 if (fCoverageMode == CoverageMode::kWithPosition) {
794 if (fNeedsPerspective) {
795 fPosition = {"positionWithCoverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
796 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400797 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
798 fCoverage = {"coverage", kFloat_GrVertexAttribType, kFloat_GrSLType};
Michael Ludwig93aeba02018-12-21 09:50:31 -0500799 }
800 } else {
801 if (fNeedsPerspective) {
802 fPosition = {"position", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
803 } else {
804 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
805 }
806 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000807
Brian Salomon2432d062020-04-16 20:48:09 -0400808 // Need a geometry subset when the quads are AA and not rectilinear, since their AA
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400809 // outsetting can go beyond a half pixel.
Brian Salomon2432d062020-04-16 20:48:09 -0400810 if (spec.requiresGeometrySubset()) {
811 fGeomSubset = {"geomSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400812 }
813
Michael Ludwig467994d2018-12-03 14:58:31 +0000814 int localDim = spec.localDimensionality();
815 if (localDim == 3) {
816 fLocalCoord = {"localCoord", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
817 } else if (localDim == 2) {
818 fLocalCoord = {"localCoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
819 } // else localDim == 0 and attribute remains uninitialized
820
Brian Osman2715bf52019-12-06 14:38:47 -0500821 if (spec.hasVertexColors()) {
822 fColor = MakeColorAttribute("color", ColorType::kFloat == spec.colorType());
Michael Ludwig467994d2018-12-03 14:58:31 +0000823 }
824
Brian Salomon2432d062020-04-16 20:48:09 -0400825 if (spec.hasSubset()) {
826 fTexSubset = {"texSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwig467994d2018-12-03 14:58:31 +0000827 }
828
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400829 this->setVertexAttributes(&fPosition, 6);
Michael Ludwig467994d2018-12-03 14:58:31 +0000830 }
831
832 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
833
Michael Ludwig93aeba02018-12-21 09:50:31 -0500834 Attribute fPosition; // May contain coverage as last channel
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400835 Attribute fCoverage; // Used for non-perspective position to avoid Intel Metal issues
Michael Ludwig93aeba02018-12-21 09:50:31 -0500836 Attribute fColor; // May have coverage modulated in if the FPs support it
Michael Ludwig467994d2018-12-03 14:58:31 +0000837 Attribute fLocalCoord;
Brian Salomon2432d062020-04-16 20:48:09 -0400838 Attribute fGeomSubset; // Screen-space bounding box on geometry+aa outset
839 Attribute fTexSubset; // Texture-space bounding box on local coords
Michael Ludwig467994d2018-12-03 14:58:31 +0000840
Michael Ludwig93aeba02018-12-21 09:50:31 -0500841 // The positions attribute may have coverage built into it, so float3 is an ambiguous type
842 // and may mean 2d with coverage, or 3d with no coverage
Michael Ludwig467994d2018-12-03 14:58:31 +0000843 bool fNeedsPerspective;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400844 // Should saturate() be called on the color? Only relevant when created with a texture.
845 Saturate fSaturate = Saturate::kNo;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500846 CoverageMode fCoverageMode;
Michael Ludwig467994d2018-12-03 14:58:31 +0000847
848 // Color space will be null and fSampler.isInitialized() returns false when the GP is configured
849 // to skip texturing.
850 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
851 TextureSampler fSampler;
852
John Stiles7571f9e2020-09-02 22:42:33 -0400853 using INHERITED = GrGeometryProcessor;
Michael Ludwig467994d2018-12-03 14:58:31 +0000854};
855
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500856GrGeometryProcessor* MakeProcessor(SkArenaAlloc* arena, const VertexSpec& spec) {
857 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec);
Michael Ludwig467994d2018-12-03 14:58:31 +0000858}
859
Brian Salomonccb61422020-01-09 10:46:36 -0500860GrGeometryProcessor* MakeTexturedProcessor(SkArenaAlloc* arena,
861 const VertexSpec& spec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500862 const GrShaderCaps& caps,
863 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500864 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500865 const GrSwizzle& swizzle,
866 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
Brian Salomonccb61422020-01-09 10:46:36 -0500867 Saturate saturate) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500868 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec, caps, backendFormat, samplerState,
869 swizzle, std::move(textureColorSpaceXform),
870 saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400871}
Michael Ludwigc182b942018-11-16 10:27:51 -0500872
873} // namespace GrQuadPerEdgeAA