blob: 8133422228bdc130cc201d6b36b80ad2d7165f87 [file] [log] [blame]
Michael Ludwig460eb5e2018-10-29 11:09:29 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Michael Ludwigfd4f4df2019-05-29 09:51:09 -04008#include "src/gpu/ops/GrQuadPerEdgeAA.h"
9
Michael Ludwigfb7ba522019-10-29 15:33:34 -040010#include "include/private/SkVx.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/SkGr.h"
Michael Ludwigfb7ba522019-10-29 15:33:34 -040012#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
14#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/glsl/GrGLSLVarying.h"
17#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Michael Ludwig460eb5e2018-10-29 11:09:29 -040018
Brian Osman01e6d172020-03-30 15:57:14 -040019static_assert((int)GrQuadAAFlags::kLeft == SkCanvas::kLeft_QuadAAFlag);
20static_assert((int)GrQuadAAFlags::kTop == SkCanvas::kTop_QuadAAFlag);
21static_assert((int)GrQuadAAFlags::kRight == SkCanvas::kRight_QuadAAFlag);
22static_assert((int)GrQuadAAFlags::kBottom == SkCanvas::kBottom_QuadAAFlag);
23static_assert((int)GrQuadAAFlags::kNone == SkCanvas::kNone_QuadAAFlags);
24static_assert((int)GrQuadAAFlags::kAll == SkCanvas::kAll_QuadAAFlags);
Michael Ludwigf995c052018-11-26 15:24:29 -050025
Michael Ludwig460eb5e2018-10-29 11:09:29 -040026namespace {
27
Michael Ludwig73dbea62019-11-19 14:55:36 -050028// Generic WriteQuadProc that can handle any VertexSpec. It writes the 4 vertices in triangle strip
29// order, although the data per-vertex is dependent on the VertexSpec.
30static void write_quad_generic(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -050031 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -050032 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -040033 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050034 static constexpr auto If = GrVertexWriter::If<float>;
Michael Ludwig704d5402019-11-25 09:43:37 -050035
36 SkASSERT(!spec.hasLocalCoords() || localQuad);
37
Michael Ludwig73dbea62019-11-19 14:55:36 -050038 GrQuadPerEdgeAA::CoverageMode mode = spec.coverageMode();
Michael Ludwig553e9a92018-11-29 12:38:35 -050039 for (int i = 0; i < 4; ++i) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050040 // save position, this is a float2 or float3 or float4 depending on the combination of
41 // perspective and coverage mode.
Michael Ludwig704d5402019-11-25 09:43:37 -050042 vb->write(deviceQuad->x(i), deviceQuad->y(i),
43 If(spec.deviceQuadType() == GrQuad::Type::kPerspective, deviceQuad->w(i)),
Robert Phillips29f38542019-10-16 09:20:25 -040044 If(mode == GrQuadPerEdgeAA::CoverageMode::kWithPosition, coverage[i]));
Michael Ludwig4921dc32018-12-03 14:57:29 +000045
Michael Ludwig93aeba02018-12-21 09:50:31 -050046 // save color
47 if (spec.hasVertexColors()) {
Brian Osman2715bf52019-12-06 14:38:47 -050048 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwige6266a22019-03-07 11:24:32 -050049 vb->write(GrVertexColor(
Michael Ludwig73dbea62019-11-19 14:55:36 -050050 color * (mode == GrQuadPerEdgeAA::CoverageMode::kWithColor ? coverage[i] : 1.f),
Robert Phillips29f38542019-10-16 09:20:25 -040051 wide));
Michael Ludwig93aeba02018-12-21 09:50:31 -050052 }
53
54 // save local position
55 if (spec.hasLocalCoords()) {
Michael Ludwig704d5402019-11-25 09:43:37 -050056 vb->write(localQuad->x(i), localQuad->y(i),
57 If(spec.localQuadType() == GrQuad::Type::kPerspective, localQuad->w(i)));
Michael Ludwig93aeba02018-12-21 09:50:31 -050058 }
59
Brian Salomon2432d062020-04-16 20:48:09 -040060 // save the geometry subset
61 if (spec.requiresGeometrySubset()) {
62 vb->write(geomSubset);
Michael Ludwigdcfbe322019-04-01 14:55:54 -040063 }
64
Brian Salomon2432d062020-04-16 20:48:09 -040065 // save the texture subset
66 if (spec.hasSubset()) {
67 vb->write(texSubset);
Michael Ludwig93aeba02018-12-21 09:50:31 -050068 }
69 }
70}
71
Michael Ludwig73dbea62019-11-19 14:55:36 -050072// Specialized WriteQuadProcs for particular VertexSpecs that show up frequently (determined
73// experimentally through recorded GMs, SKPs, and SVGs, as well as SkiaRenderer's usage patterns):
74
Brian Salomon2432d062020-04-16 20:48:09 -040075// 2D (XY), no explicit coverage, vertex color, no locals, no geometry subset, no texture subsetn
Michael Ludwig73dbea62019-11-19 14:55:36 -050076// This represents simple, solid color or shader, non-AA (or AA with cov. as alpha) rects.
77static void write_2d_color(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -050078 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -050079 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -040080 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -050081 // Assert assumptions about VertexSpec
82 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
83 SkASSERT(!spec.hasLocalCoords());
84 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
85 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
86 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -040087 SkASSERT(!spec.requiresGeometrySubset());
88 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -050089 // We don't assert that localQuad == nullptr, since it is possible for GrFillRectOp to
90 // accumulate local coords conservatively (paint not trivial), and then after analysis realize
91 // the processors don't need local coordinates.
Michael Ludwig73dbea62019-11-19 14:55:36 -050092
Brian Osman2715bf52019-12-06 14:38:47 -050093 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -050094 for (int i = 0; i < 4; ++i) {
95 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
96 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
97 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -050098 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide));
Michael Ludwig73dbea62019-11-19 14:55:36 -050099 }
100}
101
Brian Salomon2432d062020-04-16 20:48:09 -0400102// 2D (XY), no explicit coverage, UV locals, no color, no geometry subset, no texture subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500103// This represents opaque, non AA, textured rects
104static void write_2d_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500105 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500106 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400107 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500108 // Assert assumptions about VertexSpec
109 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
110 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
111 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone);
112 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400113 SkASSERT(!spec.requiresGeometrySubset());
114 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500115 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500116
117 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500118 vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500119 }
120}
121
Brian Salomon2432d062020-04-16 20:48:09 -0400122// 2D (XY), no explicit coverage, UV locals, vertex color, no geometry or texture subsets
Michael Ludwig73dbea62019-11-19 14:55:36 -0500123// This represents transparent, non AA (or AA with cov. as alpha), textured rects
124static void write_2d_color_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500125 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500126 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400127 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500128 // Assert assumptions about VertexSpec
129 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
130 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
131 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
132 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
133 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400134 SkASSERT(!spec.requiresGeometrySubset());
135 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500136 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500137
Brian Osman2715bf52019-12-06 14:38:47 -0500138 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500139 for (int i = 0; i < 4; ++i) {
140 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
141 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
142 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500143 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide),
144 localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500145 }
146}
147
Brian Salomon2432d062020-04-16 20:48:09 -0400148// 2D (XY), explicit coverage, UV locals, no color, no geometry subset, no texture subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500149// This represents opaque, AA, textured rects
150static void write_2d_cov_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500151 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500152 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400153 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500154 // Assert assumptions about VertexSpec
155 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
156 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
157 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition);
158 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400159 SkASSERT(!spec.requiresGeometrySubset());
160 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500161 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500162
163 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500164 vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i],
165 localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500166 }
167}
168
169// NOTE: The three _strict specializations below match the non-strict uv functions above, except
Brian Salomon2432d062020-04-16 20:48:09 -0400170// that they also write the UV subset. These are included to benefit SkiaRenderer, which must make
171// use of both fast and strict constrained subsets. When testing _strict was not that common across
Michael Ludwig73dbea62019-11-19 14:55:36 -0500172// GMS, SKPs, and SVGs but we have little visibility into actual SkiaRenderer statistics. If
Brian Salomon2432d062020-04-16 20:48:09 -0400173// SkiaRenderer can avoid subsets more, these 3 functions should probably be removed for simplicity.
Michael Ludwig73dbea62019-11-19 14:55:36 -0500174
Brian Salomon2432d062020-04-16 20:48:09 -0400175// 2D (XY), no explicit coverage, UV locals, no color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500176// This represents opaque, non AA, textured rects with strict uv sampling
177static void write_2d_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500178 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500179 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400180 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500181 // Assert assumptions about VertexSpec
182 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
183 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
184 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone);
185 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400186 SkASSERT(!spec.requiresGeometrySubset());
187 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500188 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500189
190 for (int i = 0; i < 4; ++i) {
Brian Salomon2432d062020-04-16 20:48:09 -0400191 vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500192 }
193}
194
Brian Salomon2432d062020-04-16 20:48:09 -0400195// 2D (XY), no explicit coverage, UV locals, vertex color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500196// This represents transparent, non AA (or AA with cov. as alpha), textured rects with strict sample
197static void write_2d_color_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500198 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500199 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400200 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500201 // Assert assumptions about VertexSpec
202 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
203 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
204 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
205 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
206 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400207 SkASSERT(!spec.requiresGeometrySubset());
208 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500209 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500210
Brian Osman2715bf52019-12-06 14:38:47 -0500211 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500212 for (int i = 0; i < 4; ++i) {
213 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
214 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
215 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500216 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide),
Brian Salomon2432d062020-04-16 20:48:09 -0400217 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), explicit coverage, UV locals, no color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500222// This represents opaque, AA, textured rects with strict uv sampling
223static void write_2d_cov_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500224 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500225 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400226 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500227 // Assert assumptions about VertexSpec
228 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
229 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
230 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition);
231 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400232 SkASSERT(!spec.requiresGeometrySubset());
233 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500234 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500235
236 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500237 vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i],
Brian Salomon2432d062020-04-16 20:48:09 -0400238 localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500239 }
240}
241
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400242} // anonymous namespace
243
Michael Ludwigc182b942018-11-16 10:27:51 -0500244namespace GrQuadPerEdgeAA {
245
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400246IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads) {
247 if (aa == GrAAType::kCoverage) {
248 return IndexBufferOption::kPictureFramed;
249 } else if (numQuads > 1) {
250 return IndexBufferOption::kIndexedRects;
251 } else {
252 return IndexBufferOption::kTriStrips;
253 }
254}
255
Brian Osman2715bf52019-12-06 14:38:47 -0500256// This is a more elaborate version of fitsInBytes() that allows "no color" for white
257ColorType MinColorType(SkPMColor4f color) {
Brian Salomon1d835422019-03-13 16:11:44 -0400258 if (color == SK_PMColor4fWHITE) {
259 return ColorType::kNone;
Brian Salomon1d835422019-03-13 16:11:44 -0400260 } else {
Brian Osman2715bf52019-12-06 14:38:47 -0500261 return color.fitsInBytes() ? ColorType::kByte : ColorType::kFloat;
Brian Salomon1d835422019-03-13 16:11:44 -0400262 }
263}
264
Michael Ludwig73dbea62019-11-19 14:55:36 -0500265////////////////// Tessellator Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500266
Michael Ludwig73dbea62019-11-19 14:55:36 -0500267Tessellator::WriteQuadProc Tessellator::GetWriteQuadProc(const VertexSpec& spec) {
Brian Salomon2432d062020-04-16 20:48:09 -0400268 // All specialized writing functions requires 2D geometry and no geometry subset. This is not
Michael Ludwig73dbea62019-11-19 14:55:36 -0500269 // the same as just checking device type vs. kRectilinear since non-AA general 2D quads do not
Brian Salomon2432d062020-04-16 20:48:09 -0400270 // require a geometry subset and could then go through a fast path.
271 if (spec.deviceQuadType() != GrQuad::Type::kPerspective && !spec.requiresGeometrySubset()) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500272 CoverageMode mode = spec.coverageMode();
273 if (spec.hasVertexColors()) {
274 if (mode != CoverageMode::kWithPosition) {
275 // Vertex colors, but no explicit coverage
276 if (!spec.hasLocalCoords()) {
277 // Non-UV with vertex colors (possibly with coverage folded into alpha)
278 return write_2d_color;
279 } else if (spec.localQuadType() != GrQuad::Type::kPerspective) {
280 // UV locals with vertex colors (possibly with coverage-as-alpha)
Brian Salomon2432d062020-04-16 20:48:09 -0400281 return spec.hasSubset() ? write_2d_color_uv_strict : write_2d_color_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500282 }
283 }
284 // Else fall through; this is a spec that requires vertex colors and explicit coverage,
285 // which means it's anti-aliased and the FPs don't support coverage as alpha, or
286 // it uses 3D local coordinates.
287 } else if (spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective) {
288 if (mode == CoverageMode::kWithPosition) {
289 // UV locals with explicit coverage
Brian Salomon2432d062020-04-16 20:48:09 -0400290 return spec.hasSubset() ? write_2d_cov_uv_strict : write_2d_cov_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500291 } else {
292 SkASSERT(mode == CoverageMode::kNone);
Brian Salomon2432d062020-04-16 20:48:09 -0400293 return spec.hasSubset() ? write_2d_uv_strict : write_2d_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500294 }
295 }
296 // Else fall through to generic vertex function; this is a spec that has no vertex colors
297 // and [no|uvr] local coords, which doesn't happen often enough to warrant specialization.
298 }
Michael Ludwig41f395d2019-05-23 13:59:45 -0400299
Michael Ludwig73dbea62019-11-19 14:55:36 -0500300 // Arbitrary spec hits the slow path
301 return write_quad_generic;
302}
303
Michael Ludwig189c9802019-11-21 11:21:12 -0500304Tessellator::Tessellator(const VertexSpec& spec, char* vertices)
Michael Ludwig73dbea62019-11-19 14:55:36 -0500305 : fVertexSpec(spec)
Michael Ludwig189c9802019-11-21 11:21:12 -0500306 , fVertexWriter{vertices}
Michael Ludwig73dbea62019-11-19 14:55:36 -0500307 , fWriteProc(Tessellator::GetWriteQuadProc(spec)) {}
308
Michael Ludwig704d5402019-11-25 09:43:37 -0500309void Tessellator::append(GrQuad* deviceQuad, GrQuad* localQuad,
Brian Salomon2432d062020-04-16 20:48:09 -0400310 const SkPMColor4f& color, const SkRect& uvSubset, GrQuadAAFlags aaFlags) {
Michael Ludwig189c9802019-11-21 11:21:12 -0500311 // We allow Tessellator to be created with a null vertices pointer for convenience, but it is
312 // assumed it will never actually be used in those cases.
313 SkASSERT(fVertexWriter.fPtr);
Michael Ludwig704d5402019-11-25 09:43:37 -0500314 SkASSERT(deviceQuad->quadType() <= fVertexSpec.deviceQuadType());
315 SkASSERT(localQuad || !fVertexSpec.hasLocalCoords());
316 SkASSERT(!fVertexSpec.hasLocalCoords() || localQuad->quadType() <= fVertexSpec.localQuadType());
Michael Ludwig73dbea62019-11-19 14:55:36 -0500317
318 static const float kFullCoverage[4] = {1.f, 1.f, 1.f, 1.f};
319 static const float kZeroCoverage[4] = {0.f, 0.f, 0.f, 0.f};
Brian Salomon2432d062020-04-16 20:48:09 -0400320 static const SkRect kIgnoredSubset = SkRect::MakeEmpty();
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400321
Michael Ludwig73dbea62019-11-19 14:55:36 -0500322 if (fVertexSpec.usesCoverageAA()) {
323 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kWithColor ||
324 fVertexSpec.coverageMode() == CoverageMode::kWithPosition);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400325 // Must calculate inner and outer quadrilaterals for the vertex coverage ramps, and possibly
Brian Salomon2432d062020-04-16 20:48:09 -0400326 // a geometry subset if corners are not right angles
327 SkRect geomSubset;
328 if (fVertexSpec.requiresGeometrySubset()) {
Brian Salomon659e71f2021-02-24 10:09:02 -0500329#ifdef SK_USE_LEGACY_AA_QUAD_SUBSET
Brian Salomon2432d062020-04-16 20:48:09 -0400330 geomSubset = deviceQuad->bounds();
331 geomSubset.outset(0.5f, 0.5f); // account for AA expansion
Brian Salomon659e71f2021-02-24 10:09:02 -0500332#else
333 // Our GP code expects a 0.5 outset rect (coverage is computed as 0 at the values of
334 // the uniform). However, if we have quad edges that aren't supposed to be antialiased
335 // they may lie close to the bounds. So in that case we outset by an additional 0.5.
336 // This is a sort of backup clipping mechanism for cases where quad outsetting of nearly
337 // parallel edges produces long thin extrusions from the original geometry.
338 float outset = aaFlags == GrQuadAAFlags::kAll ? 0.5f : 1.f;
339 geomSubset = deviceQuad->bounds().makeOutset(outset, outset);
340#endif
Michael Ludwige6266a22019-03-07 11:24:32 -0500341 }
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400342
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500343 if (aaFlags == GrQuadAAFlags::kNone) {
344 // Have to write the coverage AA vertex structure, but there's no math to be done for a
345 // non-aa quad batched into a coverage AA op.
Michael Ludwig189c9802019-11-21 11:21:12 -0500346 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400347 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500348 // Since we pass the same corners in, the outer vertex structure will have 0 area and
349 // the coverage interpolation from 1 to 0 will not be visible.
Michael Ludwig189c9802019-11-21 11:21:12 -0500350 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400351 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500352 } else {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500353 // Reset the tessellation helper to match the current geometry
Michael Ludwig704d5402019-11-25 09:43:37 -0500354 fAAHelper.reset(*deviceQuad, localQuad);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400355
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500356 // Edge inset/outset distance ordered LBTR, set to 0.5 for a half pixel if the AA flag
357 // is turned on, or 0.0 if the edge is not anti-aliased.
358 skvx::Vec<4, float> edgeDistances;
359 if (aaFlags == GrQuadAAFlags::kAll) {
360 edgeDistances = 0.5f;
361 } else {
362 edgeDistances = { (aaFlags & GrQuadAAFlags::kLeft) ? 0.5f : 0.f,
363 (aaFlags & GrQuadAAFlags::kBottom) ? 0.5f : 0.f,
364 (aaFlags & GrQuadAAFlags::kTop) ? 0.5f : 0.f,
365 (aaFlags & GrQuadAAFlags::kRight) ? 0.5f : 0.f };
366 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400367
Michael Ludwig73dbea62019-11-19 14:55:36 -0500368 // Write inner vertices first
369 float coverage[4];
Michael Ludwig704d5402019-11-25 09:43:37 -0500370 fAAHelper.inset(edgeDistances, deviceQuad, localQuad).store(coverage);
371 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, coverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400372 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500373
Michael Ludwig290d6df2020-10-27 09:28:59 -0400374 // Then outer vertices, which use 0.f for their coverage. If the inset was degenerate
375 // to a line (had all coverages < 1), tweak the outset distance so the outer frame's
376 // narrow axis reaches out to 2px, which gives better animation under translation.
377 if (coverage[0] < 1.f && coverage[1] < 1.f && coverage[2] < 1.f && coverage[3] < 1.f) {
378 skvx::Vec<4, float> len = fAAHelper.getEdgeLengths();
379 // Using max guards us against trying to scale a degenerate triangle edge of 0 len
380 // up to 2px. The shuffles are so that edge 0's adjustment is based on the lengths
381 // of its connecting edges (1 and 2), and so forth.
382 skvx::Vec<4, float> maxWH = max(skvx::shuffle<1, 0, 3, 2>(len),
383 skvx::shuffle<2, 3, 0, 1>(len));
384 // wh + 2e' = 2, so e' = (2 - wh) / 2 => e' = e * (2 - wh). But if w or h > 1, then
385 // 2 - wh < 1 and represents the non-narrow axis so clamp to 1.
386 edgeDistances *= max(1.f, 2.f - maxWH);
387 }
Michael Ludwig704d5402019-11-25 09:43:37 -0500388 fAAHelper.outset(edgeDistances, deviceQuad, localQuad);
389 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400390 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500391 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500392 } else {
393 // No outsetting needed, just write a single quad with full coverage
Michael Ludwig73dbea62019-11-19 14:55:36 -0500394 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kNone &&
Brian Salomon2432d062020-04-16 20:48:09 -0400395 !fVertexSpec.requiresGeometrySubset());
Michael Ludwig189c9802019-11-21 11:21:12 -0500396 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400397 kIgnoredSubset, uvSubset);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400398 }
399}
Michael Ludwig20e909e2018-10-30 10:43:57 -0400400
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400401sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawOp::Target* target,
402 IndexBufferOption indexBufferOption) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400403 auto resourceProvider = target->resourceProvider();
404
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400405 switch (indexBufferOption) {
406 case IndexBufferOption::kPictureFramed: return resourceProvider->refAAQuadIndexBuffer();
407 case IndexBufferOption::kIndexedRects: return resourceProvider->refNonAAQuadIndexBuffer();
408 case IndexBufferOption::kTriStrips: // fall through
409 default: return nullptr;
410 }
411}
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400412
Robert Phillips438d9862019-11-14 12:46:05 -0500413int QuadLimit(IndexBufferOption option) {
414 switch (option) {
415 case IndexBufferOption::kPictureFramed: return GrResourceProvider::MaxNumAAQuads();
416 case IndexBufferOption::kIndexedRects: return GrResourceProvider::MaxNumNonAAQuads();
417 case IndexBufferOption::kTriStrips: return SK_MaxS32; // not limited by an indexBuffer
418 }
419
420 SkUNREACHABLE;
421}
Michael Ludwig93aeba02018-12-21 09:50:31 -0500422
Chris Daltondbb833b2020-03-17 12:15:46 -0600423void IssueDraw(const GrCaps& caps, GrOpsRenderPass* renderPass, const VertexSpec& spec,
424 int runningQuadCount, int quadsInDraw, int maxVerts, int absVertBufferOffset) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400425 if (spec.indexBufferOption() == IndexBufferOption::kTriStrips) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400426 int offset = absVertBufferOffset +
427 runningQuadCount * GrResourceProvider::NumVertsPerNonAAQuad();
Chris Daltondbb833b2020-03-17 12:15:46 -0600428 renderPass->draw(4, offset);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400429 return;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500430 }
431
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400432 SkASSERT(spec.indexBufferOption() == IndexBufferOption::kPictureFramed ||
433 spec.indexBufferOption() == IndexBufferOption::kIndexedRects);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400434
Robert Phillips2f05a482019-11-25 09:54:43 -0500435 int maxNumQuads, numIndicesPerQuad, numVertsPerQuad;
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400436
437 if (spec.indexBufferOption() == IndexBufferOption::kPictureFramed) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400438 // AA uses 8 vertices and 30 indices per quad, basically nested rectangles
Robert Phillips2f05a482019-11-25 09:54:43 -0500439 maxNumQuads = GrResourceProvider::MaxNumAAQuads();
440 numIndicesPerQuad = GrResourceProvider::NumIndicesPerAAQuad();
441 numVertsPerQuad = GrResourceProvider::NumVertsPerAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400442 } else {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400443 // Non-AA uses 4 vertices and 6 indices per quad
Robert Phillips2f05a482019-11-25 09:54:43 -0500444 maxNumQuads = GrResourceProvider::MaxNumNonAAQuads();
445 numIndicesPerQuad = GrResourceProvider::NumIndicesPerNonAAQuad();
446 numVertsPerQuad = GrResourceProvider::NumVertsPerNonAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400447 }
448
Robert Phillips2f05a482019-11-25 09:54:43 -0500449 SkASSERT(runningQuadCount + quadsInDraw <= maxNumQuads);
450
451 if (caps.avoidLargeIndexBufferDraws()) {
452 // When we need to avoid large index buffer draws we modify the base vertex of the draw
453 // which, in GL, requires rebinding all vertex attrib arrays, so a base index is generally
454 // preferred.
455 int offset = absVertBufferOffset + runningQuadCount * numVertsPerQuad;
456
Chris Daltondbb833b2020-03-17 12:15:46 -0600457 renderPass->drawIndexPattern(numIndicesPerQuad, quadsInDraw, maxNumQuads, numVertsPerQuad,
458 offset);
Robert Phillips2f05a482019-11-25 09:54:43 -0500459 } else {
460 int baseIndex = runningQuadCount * numIndicesPerQuad;
461 int numIndicesToDraw = quadsInDraw * numIndicesPerQuad;
462
463 int minVertex = runningQuadCount * numVertsPerQuad;
Michael Ludwigfa1cb402021-06-02 10:53:16 -0400464 int maxVertex = (runningQuadCount + quadsInDraw) * numVertsPerQuad - 1; // inclusive
Robert Phillips2f05a482019-11-25 09:54:43 -0500465
Chris Daltondbb833b2020-03-17 12:15:46 -0600466 renderPass->drawIndexed(numIndicesToDraw, baseIndex, minVertex, maxVertex,
467 absVertBufferOffset);
Robert Phillips2f05a482019-11-25 09:54:43 -0500468 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500469}
470
Michael Ludwigc182b942018-11-16 10:27:51 -0500471////////////////// VertexSpec Implementation
Michael Ludwig20e909e2018-10-30 10:43:57 -0400472
Michael Ludwigc182b942018-11-16 10:27:51 -0500473int VertexSpec::deviceDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400474 return this->deviceQuadType() == GrQuad::Type::kPerspective ? 3 : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -0500475}
476
477int VertexSpec::localDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400478 return fHasLocalCoords ? (this->localQuadType() == GrQuad::Type::kPerspective ? 3 : 2) : 0;
Michael Ludwigc182b942018-11-16 10:27:51 -0500479}
480
Robert Phillips29f38542019-10-16 09:20:25 -0400481CoverageMode VertexSpec::coverageMode() const {
482 if (this->usesCoverageAA()) {
483 if (this->compatibleWithCoverageAsAlpha() && this->hasVertexColors() &&
Brian Salomon2432d062020-04-16 20:48:09 -0400484 !this->requiresGeometrySubset()) {
485 // Using a geometric subset acts as a second source of coverage and folding
Robert Phillips29f38542019-10-16 09:20:25 -0400486 // the original coverage into color makes it impossible to apply the color's
Brian Salomon2432d062020-04-16 20:48:09 -0400487 // alpha to the geometric subset's coverage when the original shape is clipped.
Robert Phillips29f38542019-10-16 09:20:25 -0400488 return CoverageMode::kWithColor;
489 } else {
490 return CoverageMode::kWithPosition;
491 }
492 } else {
493 return CoverageMode::kNone;
494 }
495}
496
497// This needs to stay in sync w/ QuadPerEdgeAAGeometryProcessor::initializeAttrs
498size_t VertexSpec::vertexSize() const {
499 bool needsPerspective = (this->deviceDimensionality() == 3);
500 CoverageMode coverageMode = this->coverageMode();
501
502 size_t count = 0;
503
504 if (coverageMode == CoverageMode::kWithPosition) {
505 if (needsPerspective) {
506 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
507 } else {
508 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType) +
509 GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
510 }
511 } else {
512 if (needsPerspective) {
513 count += GrVertexAttribTypeSize(kFloat3_GrVertexAttribType);
514 } else {
515 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType);
516 }
517 }
518
Brian Salomon2432d062020-04-16 20:48:09 -0400519 if (this->requiresGeometrySubset()) {
Robert Phillips29f38542019-10-16 09:20:25 -0400520 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
521 }
522
523 count += this->localDimensionality() * GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
524
525 if (ColorType::kByte == this->colorType()) {
526 count += GrVertexAttribTypeSize(kUByte4_norm_GrVertexAttribType);
Brian Osman2715bf52019-12-06 14:38:47 -0500527 } else if (ColorType::kFloat == this->colorType()) {
528 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
Robert Phillips29f38542019-10-16 09:20:25 -0400529 }
530
Brian Salomon2432d062020-04-16 20:48:09 -0400531 if (this->hasSubset()) {
Robert Phillips29f38542019-10-16 09:20:25 -0400532 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
533 }
534
535 return count;
536}
537
Michael Ludwig467994d2018-12-03 14:58:31 +0000538////////////////// Geometry Processor Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500539
Michael Ludwig467994d2018-12-03 14:58:31 +0000540class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor {
541public:
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400542 using Saturate = GrTextureOp::Saturate;
Michael Ludwig20e909e2018-10-30 10:43:57 -0400543
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500544 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const VertexSpec& spec) {
Mike Kleinf1241082020-12-14 15:59:09 -0600545 return arena->make([&](void* ptr) {
546 return new (ptr) QuadPerEdgeAAGeometryProcessor(spec);
547 });
Michael Ludwig20e909e2018-10-30 10:43:57 -0400548 }
549
Brian Salomonccb61422020-01-09 10:46:36 -0500550 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
551 const VertexSpec& vertexSpec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500552 const GrShaderCaps& caps,
553 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500554 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500555 const GrSwizzle& swizzle,
556 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
557 Saturate saturate) {
Mike Kleinf1241082020-12-14 15:59:09 -0600558 return arena->make([&](void* ptr) {
559 return new (ptr) QuadPerEdgeAAGeometryProcessor(
560 vertexSpec, caps, backendFormat, samplerState, swizzle,
561 std::move(textureColorSpaceXform), saturate);
562 });
Michael Ludwig20e909e2018-10-30 10:43:57 -0400563 }
564
Michael Ludwig467994d2018-12-03 14:58:31 +0000565 const char* name() const override { return "QuadPerEdgeAAGeometryProcessor"; }
Michael Ludwig024e2622018-11-30 13:25:55 -0500566
Michael Ludwig467994d2018-12-03 14:58:31 +0000567 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400568 // texturing, device-dimensions are single bit flags
Brian Osman48d7f7c2021-03-03 13:38:08 -0500569 b->addBool(fTexSubset.isInitialized(), "subset");
570 b->addBool(fSampler.isInitialized(), "textured");
571 b->addBool(fNeedsPerspective, "perspective");
572 b->addBool((fSaturate == Saturate::kYes), "saturate");
573
574 b->addBool(fLocalCoord.isInitialized(), "hasLocalCoords");
Michael Ludwig467994d2018-12-03 14:58:31 +0000575 if (fLocalCoord.isInitialized()) {
Brian Osman48d7f7c2021-03-03 13:38:08 -0500576 // 2D (0) or 3D (1)
577 b->addBits(1, (kFloat3_GrVertexAttribType == fLocalCoord.cpuType()), "localCoordsType");
Brian Osman78dc72c2018-12-03 13:20:43 +0000578 }
Brian Osman48d7f7c2021-03-03 13:38:08 -0500579 b->addBool(fColor.isInitialized(), "hasColor");
Michael Ludwig93aeba02018-12-21 09:50:31 -0500580 if (fColor.isInitialized()) {
Brian Osman48d7f7c2021-03-03 13:38:08 -0500581 // bytes (0) or floats (1)
582 b->addBits(1, (kFloat4_GrVertexAttribType == fColor.cpuType()), "colorType");
Michael Ludwig93aeba02018-12-21 09:50:31 -0500583 }
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400584 // and coverage mode, 00 for none, 01 for withposition, 10 for withcolor, 11 for
Brian Salomon2432d062020-04-16 20:48:09 -0400585 // position+geomsubset
Brian Osman48d7f7c2021-03-03 13:38:08 -0500586 uint32_t coverageKey = 0;
Brian Salomon2432d062020-04-16 20:48:09 -0400587 SkASSERT(!fGeomSubset.isInitialized() || fCoverageMode == CoverageMode::kWithPosition);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500588 if (fCoverageMode != CoverageMode::kNone) {
Brian Osman48d7f7c2021-03-03 13:38:08 -0500589 coverageKey = fGeomSubset.isInitialized()
590 ? 0x3
591 : (CoverageMode::kWithPosition == fCoverageMode ? 0x1 : 0x2);
Michael Ludwig467994d2018-12-03 14:58:31 +0000592 }
Brian Osman48d7f7c2021-03-03 13:38:08 -0500593 b->addBits(2, coverageKey, "coverageMode");
Michael Ludwig467994d2018-12-03 14:58:31 +0000594
Brian Osman48d7f7c2021-03-03 13:38:08 -0500595 b->add32(GrColorSpaceXform::XformKey(fTextureColorSpaceXform.get()), "colorSpaceXform");
Brian Osman78dc72c2018-12-03 13:20:43 +0000596 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000597
Robert Phillipsf10535f2021-03-23 09:30:45 -0400598 GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
Michael Ludwig467994d2018-12-03 14:58:31 +0000599 class GLSLProcessor : public GrGLSLGeometryProcessor {
600 public:
Brian Osman609f1592020-07-01 15:14:39 -0400601 void setData(const GrGLSLProgramDataManager& pdman,
Brian Salomon5a328282021-04-14 10:32:25 -0400602 const GrShaderCaps&,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400603 const GrGeometryProcessor& geomProc) override {
604 const auto& gp = geomProc.cast<QuadPerEdgeAAGeometryProcessor>();
Michael Ludwig467994d2018-12-03 14:58:31 +0000605 fTextureColorSpaceXformHelper.setData(pdman, gp.fTextureColorSpaceXform.get());
606 }
607
608 private:
609 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
610 using Interpolation = GrGLSLVaryingHandler::Interpolation;
611
Robert Phillips787fd9d2021-03-22 14:48:09 -0400612 const auto& gp = args.fGeomProc.cast<QuadPerEdgeAAGeometryProcessor>();
Michael Ludwig467994d2018-12-03 14:58:31 +0000613 fTextureColorSpaceXformHelper.emitCode(args.fUniformHandler,
614 gp.fTextureColorSpaceXform.get());
615
616 args.fVaryingHandler->emitAttributes(gp);
617
Michael Ludwig93aeba02018-12-21 09:50:31 -0500618 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
619 // Strip last channel from the vertex attribute to remove coverage and get the
620 // actual position
621 if (gp.fNeedsPerspective) {
622 args.fVertBuilder->codeAppendf("float3 position = %s.xyz;",
623 gp.fPosition.name());
624 } else {
625 args.fVertBuilder->codeAppendf("float2 position = %s.xy;",
626 gp.fPosition.name());
627 }
628 gpArgs->fPositionVar = {"position",
629 gp.fNeedsPerspective ? kFloat3_GrSLType
630 : kFloat2_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400631 GrShaderVar::TypeModifier::None};
Michael Ludwig467994d2018-12-03 14:58:31 +0000632 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500633 // No coverage to eliminate
634 gpArgs->fPositionVar = gp.fPosition.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000635 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000636
Michael Ludwig553db622020-06-19 10:47:30 -0400637 // This attribute will be uninitialized if earlier FP analysis determined no
638 // local coordinates are needed (and this will not include the inline texture
639 // fetch this GP does before invoking FPs).
640 gpArgs->fLocalCoordVar = gp.fLocalCoord.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000641
642 // Solid color before any texturing gets modulated in
John Stiles4d7ac492021-03-09 20:16:43 -0500643 const char* blendDst;
Michael Ludwig467994d2018-12-03 14:58:31 +0000644 if (gp.fColor.isInitialized()) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400645 SkASSERT(gp.fCoverageMode != CoverageMode::kWithColor || !gp.fNeedsPerspective);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500646 // The color cannot be flat if the varying coverage has been modulated into it
John Stiles4d7ac492021-03-09 20:16:43 -0500647 args.fFragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
Michael Ludwig467994d2018-12-03 14:58:31 +0000648 args.fVaryingHandler->addPassThroughAttribute(gp.fColor, args.fOutputColor,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500649 gp.fCoverageMode == CoverageMode::kWithColor ?
650 Interpolation::kInterpolated : Interpolation::kCanBeFlat);
John Stiles4d7ac492021-03-09 20:16:43 -0500651 blendDst = args.fOutputColor;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500652 } else {
653 // Output color must be initialized to something
John Stiles4d7ac492021-03-09 20:16:43 -0500654 args.fFragBuilder->codeAppendf("half4 %s = half4(1);", args.fOutputColor);
655 blendDst = nullptr;
Michael Ludwig467994d2018-12-03 14:58:31 +0000656 }
657
658 // If there is a texture, must also handle texture coordinates and reading from
659 // the texture in the fragment shader before continuing to fragment processors.
660 if (gp.fSampler.isInitialized()) {
Brian Salomon2432d062020-04-16 20:48:09 -0400661 // Texture coordinates clamped by the subset on the fragment shader; if the GP
Michael Ludwig467994d2018-12-03 14:58:31 +0000662 // has a texture, it's guaranteed to have local coordinates
663 args.fFragBuilder->codeAppend("float2 texCoord;");
664 if (gp.fLocalCoord.cpuType() == kFloat3_GrVertexAttribType) {
665 // Can't do a pass through since we need to perform perspective division
666 GrGLSLVarying v(gp.fLocalCoord.gpuType());
667 args.fVaryingHandler->addVarying(gp.fLocalCoord.name(), &v);
668 args.fVertBuilder->codeAppendf("%s = %s;",
669 v.vsOut(), gp.fLocalCoord.name());
670 args.fFragBuilder->codeAppendf("texCoord = %s.xy / %s.z;",
671 v.fsIn(), v.fsIn());
672 } else {
673 args.fVaryingHandler->addPassThroughAttribute(gp.fLocalCoord, "texCoord");
674 }
675
Brian Salomon2432d062020-04-16 20:48:09 -0400676 // Clamp the now 2D localCoordName variable by the subset if it is provided
677 if (gp.fTexSubset.isInitialized()) {
678 args.fFragBuilder->codeAppend("float4 subset;");
679 args.fVaryingHandler->addPassThroughAttribute(gp.fTexSubset, "subset",
Michael Ludwig467994d2018-12-03 14:58:31 +0000680 Interpolation::kCanBeFlat);
681 args.fFragBuilder->codeAppend(
John Stiles4d7ac492021-03-09 20:16:43 -0500682 "texCoord = clamp(texCoord, subset.LT, subset.RB);");
Michael Ludwig467994d2018-12-03 14:58:31 +0000683 }
684
685 // Now modulate the starting output color by the texture lookup
John Stiles4d7ac492021-03-09 20:16:43 -0500686 args.fFragBuilder->codeAppendf(
687 "%s = %s(",
688 args.fOutputColor,
689 (gp.fSaturate == Saturate::kYes) ? "saturate" : "");
Brian Salomon87e9ddb2019-12-19 14:50:22 -0500690 args.fFragBuilder->appendTextureLookupAndBlend(
John Stiles4d7ac492021-03-09 20:16:43 -0500691 blendDst, SkBlendMode::kModulate, args.fTexSamplers[0],
Brian Salomond19cd762020-01-06 13:16:31 -0500692 "texCoord", &fTextureColorSpaceXformHelper);
John Stiles4d7ac492021-03-09 20:16:43 -0500693 args.fFragBuilder->codeAppend(");");
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400694 } else {
695 // Saturate is only intended for use with a proxy to account for the fact
696 // that GrTextureOp skips SkPaint conversion, which normally handles this.
697 SkASSERT(gp.fSaturate == Saturate::kNo);
Michael Ludwig467994d2018-12-03 14:58:31 +0000698 }
699
700 // And lastly, output the coverage calculation code
Michael Ludwig93aeba02018-12-21 09:50:31 -0500701 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
702 GrGLSLVarying coverage(kFloat_GrSLType);
703 args.fVaryingHandler->addVarying("coverage", &coverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000704 if (gp.fNeedsPerspective) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400705 // Multiply by "W" in the vertex shader, then by 1/w (sk_FragCoord.w) in
706 // the fragment shader to get screen-space linear coverage.
707 args.fVertBuilder->codeAppendf("%s = %s.w * %s.z;",
708 coverage.vsOut(), gp.fPosition.name(),
709 gp.fPosition.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400710 args.fFragBuilder->codeAppendf("float coverage = %s * sk_FragCoord.w;",
711 coverage.fsIn());
Michael Ludwig93aeba02018-12-21 09:50:31 -0500712 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400713 args.fVertBuilder->codeAppendf("%s = %s;",
714 coverage.vsOut(), gp.fCoverage.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400715 args.fFragBuilder->codeAppendf("float coverage = %s;", coverage.fsIn());
Michael Ludwig467994d2018-12-03 14:58:31 +0000716 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500717
Brian Salomon2432d062020-04-16 20:48:09 -0400718 if (gp.fGeomSubset.isInitialized()) {
719 // Calculate distance from sk_FragCoord to the 4 edges of the subset
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400720 // and clamp them to (0, 1). Use the minimum of these and the original
721 // coverage. This only has to be done in the exterior triangles, the
Brian Salomon2432d062020-04-16 20:48:09 -0400722 // interior of the quad geometry can never be clipped by the subset box.
723 args.fFragBuilder->codeAppend("float4 geoSubset;");
724 args.fVaryingHandler->addPassThroughAttribute(gp.fGeomSubset, "geoSubset",
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400725 Interpolation::kCanBeFlat);
Brian Salomon659e71f2021-02-24 10:09:02 -0500726#ifdef SK_USE_LEGACY_AA_QUAD_SUBSET
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400727 args.fFragBuilder->codeAppend(
728 "if (coverage < 0.5) {"
729 " float4 dists4 = clamp(float4(1, 1, -1, -1) * "
Brian Salomon2432d062020-04-16 20:48:09 -0400730 "(sk_FragCoord.xyxy - geoSubset), 0, 1);"
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400731 " float2 dists2 = dists4.xy * dists4.zw;"
732 " coverage = min(coverage, dists2.x * dists2.y);"
733 "}");
Brian Salomon659e71f2021-02-24 10:09:02 -0500734#else
735 args.fFragBuilder->codeAppend(
736 // This is lifted from GrAARectEffect. It'd be nice if we could
737 // invoke a FP from a GP rather than duplicate this code.
738 "half4 dists4 = clamp(half4(1, 1, -1, -1) * "
739 "half4(sk_FragCoord.xyxy - geoSubset), 0, 1);\n"
740 "half2 dists2 = dists4.xy + dists4.zw - 1;\n"
741 "half subsetCoverage = dists2.x * dists2.y;\n"
742 "coverage = min(coverage, subsetCoverage);");
743#endif
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400744 }
745
John Stiles4d7ac492021-03-09 20:16:43 -0500746 args.fFragBuilder->codeAppendf("half4 %s = half4(half(coverage));",
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400747 args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000748 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500749 // Set coverage to 1, since it's either non-AA or the coverage was already
750 // folded into the output color
Brian Salomon2432d062020-04-16 20:48:09 -0400751 SkASSERT(!gp.fGeomSubset.isInitialized());
John Stiles4d7ac492021-03-09 20:16:43 -0500752 args.fFragBuilder->codeAppendf("const half4 %s = half4(1);",
753 args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000754 }
755 }
756 GrGLSLColorSpaceXformHelper fTextureColorSpaceXformHelper;
757 };
758 return new GLSLProcessor;
759 }
760
761private:
762 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec)
763 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
764 , fTextureColorSpaceXform(nullptr) {
Brian Salomon2432d062020-04-16 20:48:09 -0400765 SkASSERT(!spec.hasSubset());
Michael Ludwig467994d2018-12-03 14:58:31 +0000766 this->initializeAttrs(spec);
767 this->setTextureSamplerCnt(0);
768 }
769
Brian Salomon67529b22019-08-13 15:31:04 -0400770 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec,
771 const GrShaderCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400772 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500773 GrSamplerState samplerState,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400774 const GrSwizzle& swizzle,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400775 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
776 Saturate saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000777 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400778 , fSaturate(saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000779 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Robert Phillips323471e2019-11-11 11:33:37 -0500780 , fSampler(samplerState, backendFormat, swizzle) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500781 SkASSERT(spec.hasLocalCoords());
Michael Ludwig467994d2018-12-03 14:58:31 +0000782 this->initializeAttrs(spec);
783 this->setTextureSamplerCnt(1);
784 }
785
Robert Phillips29f38542019-10-16 09:20:25 -0400786 // This needs to stay in sync w/ VertexSpec::vertexSize
Michael Ludwig467994d2018-12-03 14:58:31 +0000787 void initializeAttrs(const VertexSpec& spec) {
788 fNeedsPerspective = spec.deviceDimensionality() == 3;
Robert Phillips29f38542019-10-16 09:20:25 -0400789 fCoverageMode = spec.coverageMode();
Michael Ludwig93aeba02018-12-21 09:50:31 -0500790
791 if (fCoverageMode == CoverageMode::kWithPosition) {
792 if (fNeedsPerspective) {
793 fPosition = {"positionWithCoverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
794 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400795 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
796 fCoverage = {"coverage", kFloat_GrVertexAttribType, kFloat_GrSLType};
Michael Ludwig93aeba02018-12-21 09:50:31 -0500797 }
798 } else {
799 if (fNeedsPerspective) {
800 fPosition = {"position", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
801 } else {
802 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
803 }
804 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000805
Brian Salomon2432d062020-04-16 20:48:09 -0400806 // Need a geometry subset when the quads are AA and not rectilinear, since their AA
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400807 // outsetting can go beyond a half pixel.
Brian Salomon2432d062020-04-16 20:48:09 -0400808 if (spec.requiresGeometrySubset()) {
809 fGeomSubset = {"geomSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400810 }
811
Michael Ludwig467994d2018-12-03 14:58:31 +0000812 int localDim = spec.localDimensionality();
813 if (localDim == 3) {
814 fLocalCoord = {"localCoord", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
815 } else if (localDim == 2) {
816 fLocalCoord = {"localCoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
817 } // else localDim == 0 and attribute remains uninitialized
818
Brian Osman2715bf52019-12-06 14:38:47 -0500819 if (spec.hasVertexColors()) {
820 fColor = MakeColorAttribute("color", ColorType::kFloat == spec.colorType());
Michael Ludwig467994d2018-12-03 14:58:31 +0000821 }
822
Brian Salomon2432d062020-04-16 20:48:09 -0400823 if (spec.hasSubset()) {
824 fTexSubset = {"texSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwig467994d2018-12-03 14:58:31 +0000825 }
826
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400827 this->setVertexAttributes(&fPosition, 6);
Michael Ludwig467994d2018-12-03 14:58:31 +0000828 }
829
830 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
831
Michael Ludwig93aeba02018-12-21 09:50:31 -0500832 Attribute fPosition; // May contain coverage as last channel
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400833 Attribute fCoverage; // Used for non-perspective position to avoid Intel Metal issues
Michael Ludwig93aeba02018-12-21 09:50:31 -0500834 Attribute fColor; // May have coverage modulated in if the FPs support it
Michael Ludwig467994d2018-12-03 14:58:31 +0000835 Attribute fLocalCoord;
Brian Salomon2432d062020-04-16 20:48:09 -0400836 Attribute fGeomSubset; // Screen-space bounding box on geometry+aa outset
837 Attribute fTexSubset; // Texture-space bounding box on local coords
Michael Ludwig467994d2018-12-03 14:58:31 +0000838
Michael Ludwig93aeba02018-12-21 09:50:31 -0500839 // The positions attribute may have coverage built into it, so float3 is an ambiguous type
840 // and may mean 2d with coverage, or 3d with no coverage
Michael Ludwig467994d2018-12-03 14:58:31 +0000841 bool fNeedsPerspective;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400842 // Should saturate() be called on the color? Only relevant when created with a texture.
843 Saturate fSaturate = Saturate::kNo;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500844 CoverageMode fCoverageMode;
Michael Ludwig467994d2018-12-03 14:58:31 +0000845
846 // Color space will be null and fSampler.isInitialized() returns false when the GP is configured
847 // to skip texturing.
848 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
849 TextureSampler fSampler;
850
John Stiles7571f9e2020-09-02 22:42:33 -0400851 using INHERITED = GrGeometryProcessor;
Michael Ludwig467994d2018-12-03 14:58:31 +0000852};
853
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500854GrGeometryProcessor* MakeProcessor(SkArenaAlloc* arena, const VertexSpec& spec) {
855 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec);
Michael Ludwig467994d2018-12-03 14:58:31 +0000856}
857
Brian Salomonccb61422020-01-09 10:46:36 -0500858GrGeometryProcessor* MakeTexturedProcessor(SkArenaAlloc* arena,
859 const VertexSpec& spec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500860 const GrShaderCaps& caps,
861 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500862 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500863 const GrSwizzle& swizzle,
864 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
Brian Salomonccb61422020-01-09 10:46:36 -0500865 Saturate saturate) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500866 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec, caps, backendFormat, samplerState,
867 swizzle, std::move(textureColorSpaceXform),
868 saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400869}
Michael Ludwigc182b942018-11-16 10:27:51 -0500870
871} // namespace GrQuadPerEdgeAA