blob: cabd326c0dfc2fb3b289901294da38d4d8a50db0 [file] [log] [blame]
Michael Ludwig460eb5e2018-10-29 11:09:29 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Michael Ludwigfd4f4df2019-05-29 09:51:09 -04008#include "src/gpu/ops/GrQuadPerEdgeAA.h"
9
Michael Ludwigfb7ba522019-10-29 15:33:34 -040010#include "include/private/SkVx.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/SkGr.h"
Michael Ludwigfb7ba522019-10-29 15:33:34 -040012#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
14#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
15#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
16#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
17#include "src/gpu/glsl/GrGLSLVarying.h"
18#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Michael Ludwig460eb5e2018-10-29 11:09:29 -040019
Brian Osman01e6d172020-03-30 15:57:14 -040020static_assert((int)GrQuadAAFlags::kLeft == SkCanvas::kLeft_QuadAAFlag);
21static_assert((int)GrQuadAAFlags::kTop == SkCanvas::kTop_QuadAAFlag);
22static_assert((int)GrQuadAAFlags::kRight == SkCanvas::kRight_QuadAAFlag);
23static_assert((int)GrQuadAAFlags::kBottom == SkCanvas::kBottom_QuadAAFlag);
24static_assert((int)GrQuadAAFlags::kNone == SkCanvas::kNone_QuadAAFlags);
25static_assert((int)GrQuadAAFlags::kAll == SkCanvas::kAll_QuadAAFlags);
Michael Ludwigf995c052018-11-26 15:24:29 -050026
Michael Ludwig460eb5e2018-10-29 11:09:29 -040027namespace {
28
Michael Ludwig73dbea62019-11-19 14:55:36 -050029// Generic WriteQuadProc that can handle any VertexSpec. It writes the 4 vertices in triangle strip
30// order, although the data per-vertex is dependent on the VertexSpec.
31static void write_quad_generic(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -050032 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -050033 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -040034 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050035 static constexpr auto If = GrVertexWriter::If<float>;
Michael Ludwig704d5402019-11-25 09:43:37 -050036
37 SkASSERT(!spec.hasLocalCoords() || localQuad);
38
Michael Ludwig73dbea62019-11-19 14:55:36 -050039 GrQuadPerEdgeAA::CoverageMode mode = spec.coverageMode();
Michael Ludwig553e9a92018-11-29 12:38:35 -050040 for (int i = 0; i < 4; ++i) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050041 // save position, this is a float2 or float3 or float4 depending on the combination of
42 // perspective and coverage mode.
Michael Ludwig704d5402019-11-25 09:43:37 -050043 vb->write(deviceQuad->x(i), deviceQuad->y(i),
44 If(spec.deviceQuadType() == GrQuad::Type::kPerspective, deviceQuad->w(i)),
Robert Phillips29f38542019-10-16 09:20:25 -040045 If(mode == GrQuadPerEdgeAA::CoverageMode::kWithPosition, coverage[i]));
Michael Ludwig4921dc32018-12-03 14:57:29 +000046
Michael Ludwig93aeba02018-12-21 09:50:31 -050047 // save color
48 if (spec.hasVertexColors()) {
Brian Osman2715bf52019-12-06 14:38:47 -050049 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwige6266a22019-03-07 11:24:32 -050050 vb->write(GrVertexColor(
Michael Ludwig73dbea62019-11-19 14:55:36 -050051 color * (mode == GrQuadPerEdgeAA::CoverageMode::kWithColor ? coverage[i] : 1.f),
Robert Phillips29f38542019-10-16 09:20:25 -040052 wide));
Michael Ludwig93aeba02018-12-21 09:50:31 -050053 }
54
55 // save local position
56 if (spec.hasLocalCoords()) {
Michael Ludwig704d5402019-11-25 09:43:37 -050057 vb->write(localQuad->x(i), localQuad->y(i),
58 If(spec.localQuadType() == GrQuad::Type::kPerspective, localQuad->w(i)));
Michael Ludwig93aeba02018-12-21 09:50:31 -050059 }
60
Brian Salomon2432d062020-04-16 20:48:09 -040061 // save the geometry subset
62 if (spec.requiresGeometrySubset()) {
63 vb->write(geomSubset);
Michael Ludwigdcfbe322019-04-01 14:55:54 -040064 }
65
Brian Salomon2432d062020-04-16 20:48:09 -040066 // save the texture subset
67 if (spec.hasSubset()) {
68 vb->write(texSubset);
Michael Ludwig93aeba02018-12-21 09:50:31 -050069 }
70 }
71}
72
Michael Ludwig73dbea62019-11-19 14:55:36 -050073// Specialized WriteQuadProcs for particular VertexSpecs that show up frequently (determined
74// experimentally through recorded GMs, SKPs, and SVGs, as well as SkiaRenderer's usage patterns):
75
Brian Salomon2432d062020-04-16 20:48:09 -040076// 2D (XY), no explicit coverage, vertex color, no locals, no geometry subset, no texture subsetn
Michael Ludwig73dbea62019-11-19 14:55:36 -050077// This represents simple, solid color or shader, non-AA (or AA with cov. as alpha) rects.
78static void write_2d_color(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -050079 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -050080 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -040081 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -050082 // Assert assumptions about VertexSpec
83 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
84 SkASSERT(!spec.hasLocalCoords());
85 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
86 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
87 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -040088 SkASSERT(!spec.requiresGeometrySubset());
89 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -050090 // We don't assert that localQuad == nullptr, since it is possible for GrFillRectOp to
91 // accumulate local coords conservatively (paint not trivial), and then after analysis realize
92 // the processors don't need local coordinates.
Michael Ludwig73dbea62019-11-19 14:55:36 -050093
Brian Osman2715bf52019-12-06 14:38:47 -050094 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -050095 for (int i = 0; i < 4; ++i) {
96 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
97 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
98 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -050099 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500100 }
101}
102
Brian Salomon2432d062020-04-16 20:48:09 -0400103// 2D (XY), no explicit coverage, UV locals, no color, no geometry subset, no texture subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500104// This represents opaque, non AA, textured rects
105static void write_2d_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500106 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500107 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400108 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500109 // Assert assumptions about VertexSpec
110 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
111 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
112 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone);
113 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400114 SkASSERT(!spec.requiresGeometrySubset());
115 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500116 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500117
118 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500119 vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500120 }
121}
122
Brian Salomon2432d062020-04-16 20:48:09 -0400123// 2D (XY), no explicit coverage, UV locals, vertex color, no geometry or texture subsets
Michael Ludwig73dbea62019-11-19 14:55:36 -0500124// This represents transparent, non AA (or AA with cov. as alpha), textured rects
125static void write_2d_color_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500126 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500127 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400128 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500129 // Assert assumptions about VertexSpec
130 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
131 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
132 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
133 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
134 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400135 SkASSERT(!spec.requiresGeometrySubset());
136 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500137 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500138
Brian Osman2715bf52019-12-06 14:38:47 -0500139 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500140 for (int i = 0; i < 4; ++i) {
141 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
142 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
143 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500144 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide),
145 localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500146 }
147}
148
Brian Salomon2432d062020-04-16 20:48:09 -0400149// 2D (XY), explicit coverage, UV locals, no color, no geometry subset, no texture subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500150// This represents opaque, AA, textured rects
151static void write_2d_cov_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500152 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500153 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400154 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500155 // Assert assumptions about VertexSpec
156 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
157 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
158 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition);
159 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400160 SkASSERT(!spec.requiresGeometrySubset());
161 SkASSERT(!spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500162 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500163
164 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500165 vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i],
166 localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500167 }
168}
169
170// NOTE: The three _strict specializations below match the non-strict uv functions above, except
Brian Salomon2432d062020-04-16 20:48:09 -0400171// that they also write the UV subset. These are included to benefit SkiaRenderer, which must make
172// use of both fast and strict constrained subsets. When testing _strict was not that common across
Michael Ludwig73dbea62019-11-19 14:55:36 -0500173// GMS, SKPs, and SVGs but we have little visibility into actual SkiaRenderer statistics. If
Brian Salomon2432d062020-04-16 20:48:09 -0400174// SkiaRenderer can avoid subsets more, these 3 functions should probably be removed for simplicity.
Michael Ludwig73dbea62019-11-19 14:55:36 -0500175
Brian Salomon2432d062020-04-16 20:48:09 -0400176// 2D (XY), no explicit coverage, UV locals, no color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500177// This represents opaque, non AA, textured rects with strict uv sampling
178static void write_2d_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500179 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500180 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400181 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500182 // Assert assumptions about VertexSpec
183 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
184 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
185 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone);
186 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400187 SkASSERT(!spec.requiresGeometrySubset());
188 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500189 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500190
191 for (int i = 0; i < 4; ++i) {
Brian Salomon2432d062020-04-16 20:48:09 -0400192 vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500193 }
194}
195
Brian Salomon2432d062020-04-16 20:48:09 -0400196// 2D (XY), no explicit coverage, UV locals, vertex color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500197// This represents transparent, non AA (or AA with cov. as alpha), textured rects with strict sample
198static void write_2d_color_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500199 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500200 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400201 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500202 // Assert assumptions about VertexSpec
203 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
204 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
205 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
206 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
207 SkASSERT(spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400208 SkASSERT(!spec.requiresGeometrySubset());
209 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500210 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500211
Brian Osman2715bf52019-12-06 14:38:47 -0500212 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500213 for (int i = 0; i < 4; ++i) {
214 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
215 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
216 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500217 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide),
Brian Salomon2432d062020-04-16 20:48:09 -0400218 localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500219 }
220}
221
Brian Salomon2432d062020-04-16 20:48:09 -0400222// 2D (XY), explicit coverage, UV locals, no color, tex subset but no geometry subset
Michael Ludwig73dbea62019-11-19 14:55:36 -0500223// This represents opaque, AA, textured rects with strict uv sampling
224static void write_2d_cov_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500225 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500226 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400227 const SkRect& geomSubset, const SkRect& texSubset) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500228 // Assert assumptions about VertexSpec
229 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
230 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
231 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition);
232 SkASSERT(!spec.hasVertexColors());
Brian Salomon2432d062020-04-16 20:48:09 -0400233 SkASSERT(!spec.requiresGeometrySubset());
234 SkASSERT(spec.hasSubset());
Michael Ludwig704d5402019-11-25 09:43:37 -0500235 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500236
237 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500238 vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i],
Brian Salomon2432d062020-04-16 20:48:09 -0400239 localQuad->x(i), localQuad->y(i), texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500240 }
241}
242
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400243} // anonymous namespace
244
Michael Ludwigc182b942018-11-16 10:27:51 -0500245namespace GrQuadPerEdgeAA {
246
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400247IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads) {
248 if (aa == GrAAType::kCoverage) {
249 return IndexBufferOption::kPictureFramed;
250 } else if (numQuads > 1) {
251 return IndexBufferOption::kIndexedRects;
252 } else {
253 return IndexBufferOption::kTriStrips;
254 }
255}
256
Brian Osman2715bf52019-12-06 14:38:47 -0500257// This is a more elaborate version of fitsInBytes() that allows "no color" for white
258ColorType MinColorType(SkPMColor4f color) {
Brian Salomon1d835422019-03-13 16:11:44 -0400259 if (color == SK_PMColor4fWHITE) {
260 return ColorType::kNone;
Brian Salomon1d835422019-03-13 16:11:44 -0400261 } else {
Brian Osman2715bf52019-12-06 14:38:47 -0500262 return color.fitsInBytes() ? ColorType::kByte : ColorType::kFloat;
Brian Salomon1d835422019-03-13 16:11:44 -0400263 }
264}
265
Michael Ludwig73dbea62019-11-19 14:55:36 -0500266////////////////// Tessellator Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500267
Michael Ludwig73dbea62019-11-19 14:55:36 -0500268Tessellator::WriteQuadProc Tessellator::GetWriteQuadProc(const VertexSpec& spec) {
Brian Salomon2432d062020-04-16 20:48:09 -0400269 // All specialized writing functions requires 2D geometry and no geometry subset. This is not
Michael Ludwig73dbea62019-11-19 14:55:36 -0500270 // the same as just checking device type vs. kRectilinear since non-AA general 2D quads do not
Brian Salomon2432d062020-04-16 20:48:09 -0400271 // require a geometry subset and could then go through a fast path.
272 if (spec.deviceQuadType() != GrQuad::Type::kPerspective && !spec.requiresGeometrySubset()) {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500273 CoverageMode mode = spec.coverageMode();
274 if (spec.hasVertexColors()) {
275 if (mode != CoverageMode::kWithPosition) {
276 // Vertex colors, but no explicit coverage
277 if (!spec.hasLocalCoords()) {
278 // Non-UV with vertex colors (possibly with coverage folded into alpha)
279 return write_2d_color;
280 } else if (spec.localQuadType() != GrQuad::Type::kPerspective) {
281 // UV locals with vertex colors (possibly with coverage-as-alpha)
Brian Salomon2432d062020-04-16 20:48:09 -0400282 return spec.hasSubset() ? write_2d_color_uv_strict : write_2d_color_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500283 }
284 }
285 // Else fall through; this is a spec that requires vertex colors and explicit coverage,
286 // which means it's anti-aliased and the FPs don't support coverage as alpha, or
287 // it uses 3D local coordinates.
288 } else if (spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective) {
289 if (mode == CoverageMode::kWithPosition) {
290 // UV locals with explicit coverage
Brian Salomon2432d062020-04-16 20:48:09 -0400291 return spec.hasSubset() ? write_2d_cov_uv_strict : write_2d_cov_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500292 } else {
293 SkASSERT(mode == CoverageMode::kNone);
Brian Salomon2432d062020-04-16 20:48:09 -0400294 return spec.hasSubset() ? write_2d_uv_strict : write_2d_uv;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500295 }
296 }
297 // Else fall through to generic vertex function; this is a spec that has no vertex colors
298 // and [no|uvr] local coords, which doesn't happen often enough to warrant specialization.
299 }
Michael Ludwig41f395d2019-05-23 13:59:45 -0400300
Michael Ludwig73dbea62019-11-19 14:55:36 -0500301 // Arbitrary spec hits the slow path
302 return write_quad_generic;
303}
304
Michael Ludwig189c9802019-11-21 11:21:12 -0500305Tessellator::Tessellator(const VertexSpec& spec, char* vertices)
Michael Ludwig73dbea62019-11-19 14:55:36 -0500306 : fVertexSpec(spec)
Michael Ludwig189c9802019-11-21 11:21:12 -0500307 , fVertexWriter{vertices}
Michael Ludwig73dbea62019-11-19 14:55:36 -0500308 , fWriteProc(Tessellator::GetWriteQuadProc(spec)) {}
309
Michael Ludwig704d5402019-11-25 09:43:37 -0500310void Tessellator::append(GrQuad* deviceQuad, GrQuad* localQuad,
Brian Salomon2432d062020-04-16 20:48:09 -0400311 const SkPMColor4f& color, const SkRect& uvSubset, GrQuadAAFlags aaFlags) {
Michael Ludwig189c9802019-11-21 11:21:12 -0500312 // We allow Tessellator to be created with a null vertices pointer for convenience, but it is
313 // assumed it will never actually be used in those cases.
314 SkASSERT(fVertexWriter.fPtr);
Michael Ludwig704d5402019-11-25 09:43:37 -0500315 SkASSERT(deviceQuad->quadType() <= fVertexSpec.deviceQuadType());
316 SkASSERT(localQuad || !fVertexSpec.hasLocalCoords());
317 SkASSERT(!fVertexSpec.hasLocalCoords() || localQuad->quadType() <= fVertexSpec.localQuadType());
Michael Ludwig73dbea62019-11-19 14:55:36 -0500318
319 static const float kFullCoverage[4] = {1.f, 1.f, 1.f, 1.f};
320 static const float kZeroCoverage[4] = {0.f, 0.f, 0.f, 0.f};
Brian Salomon2432d062020-04-16 20:48:09 -0400321 static const SkRect kIgnoredSubset = SkRect::MakeEmpty();
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400322
Michael Ludwig73dbea62019-11-19 14:55:36 -0500323 if (fVertexSpec.usesCoverageAA()) {
324 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kWithColor ||
325 fVertexSpec.coverageMode() == CoverageMode::kWithPosition);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400326 // Must calculate inner and outer quadrilaterals for the vertex coverage ramps, and possibly
Brian Salomon2432d062020-04-16 20:48:09 -0400327 // a geometry subset if corners are not right angles
328 SkRect geomSubset;
329 if (fVertexSpec.requiresGeometrySubset()) {
330 geomSubset = deviceQuad->bounds();
331 geomSubset.outset(0.5f, 0.5f); // account for AA expansion
Michael Ludwige6266a22019-03-07 11:24:32 -0500332 }
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400333
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500334 if (aaFlags == GrQuadAAFlags::kNone) {
335 // Have to write the coverage AA vertex structure, but there's no math to be done for a
336 // non-aa quad batched into a coverage AA op.
Michael Ludwig189c9802019-11-21 11:21:12 -0500337 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400338 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500339 // Since we pass the same corners in, the outer vertex structure will have 0 area and
340 // the coverage interpolation from 1 to 0 will not be visible.
Michael Ludwig189c9802019-11-21 11:21:12 -0500341 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400342 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500343 } else {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500344 // Reset the tessellation helper to match the current geometry
Michael Ludwig704d5402019-11-25 09:43:37 -0500345 fAAHelper.reset(*deviceQuad, localQuad);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400346
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500347 // Edge inset/outset distance ordered LBTR, set to 0.5 for a half pixel if the AA flag
348 // is turned on, or 0.0 if the edge is not anti-aliased.
349 skvx::Vec<4, float> edgeDistances;
350 if (aaFlags == GrQuadAAFlags::kAll) {
351 edgeDistances = 0.5f;
352 } else {
353 edgeDistances = { (aaFlags & GrQuadAAFlags::kLeft) ? 0.5f : 0.f,
354 (aaFlags & GrQuadAAFlags::kBottom) ? 0.5f : 0.f,
355 (aaFlags & GrQuadAAFlags::kTop) ? 0.5f : 0.f,
356 (aaFlags & GrQuadAAFlags::kRight) ? 0.5f : 0.f };
357 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400358
Michael Ludwig73dbea62019-11-19 14:55:36 -0500359 // Write inner vertices first
360 float coverage[4];
Michael Ludwig704d5402019-11-25 09:43:37 -0500361 fAAHelper.inset(edgeDistances, deviceQuad, localQuad).store(coverage);
362 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, coverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400363 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500364
365 // Then outer vertices, which use 0.f for their coverage
Michael Ludwig704d5402019-11-25 09:43:37 -0500366 fAAHelper.outset(edgeDistances, deviceQuad, localQuad);
367 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400368 geomSubset, uvSubset);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500369 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500370 } else {
371 // No outsetting needed, just write a single quad with full coverage
Michael Ludwig73dbea62019-11-19 14:55:36 -0500372 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kNone &&
Brian Salomon2432d062020-04-16 20:48:09 -0400373 !fVertexSpec.requiresGeometrySubset());
Michael Ludwig189c9802019-11-21 11:21:12 -0500374 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Brian Salomon2432d062020-04-16 20:48:09 -0400375 kIgnoredSubset, uvSubset);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400376 }
377}
Michael Ludwig20e909e2018-10-30 10:43:57 -0400378
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400379sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawOp::Target* target,
380 IndexBufferOption indexBufferOption) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400381 auto resourceProvider = target->resourceProvider();
382
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400383 switch (indexBufferOption) {
384 case IndexBufferOption::kPictureFramed: return resourceProvider->refAAQuadIndexBuffer();
385 case IndexBufferOption::kIndexedRects: return resourceProvider->refNonAAQuadIndexBuffer();
386 case IndexBufferOption::kTriStrips: // fall through
387 default: return nullptr;
388 }
389}
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400390
Robert Phillips438d9862019-11-14 12:46:05 -0500391int QuadLimit(IndexBufferOption option) {
392 switch (option) {
393 case IndexBufferOption::kPictureFramed: return GrResourceProvider::MaxNumAAQuads();
394 case IndexBufferOption::kIndexedRects: return GrResourceProvider::MaxNumNonAAQuads();
395 case IndexBufferOption::kTriStrips: return SK_MaxS32; // not limited by an indexBuffer
396 }
397
398 SkUNREACHABLE;
399}
Michael Ludwig93aeba02018-12-21 09:50:31 -0500400
Chris Daltondbb833b2020-03-17 12:15:46 -0600401void IssueDraw(const GrCaps& caps, GrOpsRenderPass* renderPass, const VertexSpec& spec,
402 int runningQuadCount, int quadsInDraw, int maxVerts, int absVertBufferOffset) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400403 if (spec.indexBufferOption() == IndexBufferOption::kTriStrips) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400404 int offset = absVertBufferOffset +
405 runningQuadCount * GrResourceProvider::NumVertsPerNonAAQuad();
Chris Daltondbb833b2020-03-17 12:15:46 -0600406 renderPass->draw(4, offset);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400407 return;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500408 }
409
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400410 SkASSERT(spec.indexBufferOption() == IndexBufferOption::kPictureFramed ||
411 spec.indexBufferOption() == IndexBufferOption::kIndexedRects);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400412
Robert Phillips2f05a482019-11-25 09:54:43 -0500413 int maxNumQuads, numIndicesPerQuad, numVertsPerQuad;
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400414
415 if (spec.indexBufferOption() == IndexBufferOption::kPictureFramed) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400416 // AA uses 8 vertices and 30 indices per quad, basically nested rectangles
Robert Phillips2f05a482019-11-25 09:54:43 -0500417 maxNumQuads = GrResourceProvider::MaxNumAAQuads();
418 numIndicesPerQuad = GrResourceProvider::NumIndicesPerAAQuad();
419 numVertsPerQuad = GrResourceProvider::NumVertsPerAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400420 } else {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400421 // Non-AA uses 4 vertices and 6 indices per quad
Robert Phillips2f05a482019-11-25 09:54:43 -0500422 maxNumQuads = GrResourceProvider::MaxNumNonAAQuads();
423 numIndicesPerQuad = GrResourceProvider::NumIndicesPerNonAAQuad();
424 numVertsPerQuad = GrResourceProvider::NumVertsPerNonAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400425 }
426
Robert Phillips2f05a482019-11-25 09:54:43 -0500427 SkASSERT(runningQuadCount + quadsInDraw <= maxNumQuads);
428
429 if (caps.avoidLargeIndexBufferDraws()) {
430 // When we need to avoid large index buffer draws we modify the base vertex of the draw
431 // which, in GL, requires rebinding all vertex attrib arrays, so a base index is generally
432 // preferred.
433 int offset = absVertBufferOffset + runningQuadCount * numVertsPerQuad;
434
Chris Daltondbb833b2020-03-17 12:15:46 -0600435 renderPass->drawIndexPattern(numIndicesPerQuad, quadsInDraw, maxNumQuads, numVertsPerQuad,
436 offset);
Robert Phillips2f05a482019-11-25 09:54:43 -0500437 } else {
438 int baseIndex = runningQuadCount * numIndicesPerQuad;
439 int numIndicesToDraw = quadsInDraw * numIndicesPerQuad;
440
441 int minVertex = runningQuadCount * numVertsPerQuad;
442 int maxVertex = (runningQuadCount + quadsInDraw) * numVertsPerQuad;
443
Chris Daltondbb833b2020-03-17 12:15:46 -0600444 renderPass->drawIndexed(numIndicesToDraw, baseIndex, minVertex, maxVertex,
445 absVertBufferOffset);
Robert Phillips2f05a482019-11-25 09:54:43 -0500446 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500447}
448
Michael Ludwigc182b942018-11-16 10:27:51 -0500449////////////////// VertexSpec Implementation
Michael Ludwig20e909e2018-10-30 10:43:57 -0400450
Michael Ludwigc182b942018-11-16 10:27:51 -0500451int VertexSpec::deviceDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400452 return this->deviceQuadType() == GrQuad::Type::kPerspective ? 3 : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -0500453}
454
455int VertexSpec::localDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400456 return fHasLocalCoords ? (this->localQuadType() == GrQuad::Type::kPerspective ? 3 : 2) : 0;
Michael Ludwigc182b942018-11-16 10:27:51 -0500457}
458
Robert Phillips29f38542019-10-16 09:20:25 -0400459CoverageMode VertexSpec::coverageMode() const {
460 if (this->usesCoverageAA()) {
461 if (this->compatibleWithCoverageAsAlpha() && this->hasVertexColors() &&
Brian Salomon2432d062020-04-16 20:48:09 -0400462 !this->requiresGeometrySubset()) {
463 // Using a geometric subset acts as a second source of coverage and folding
Robert Phillips29f38542019-10-16 09:20:25 -0400464 // the original coverage into color makes it impossible to apply the color's
Brian Salomon2432d062020-04-16 20:48:09 -0400465 // alpha to the geometric subset's coverage when the original shape is clipped.
Robert Phillips29f38542019-10-16 09:20:25 -0400466 return CoverageMode::kWithColor;
467 } else {
468 return CoverageMode::kWithPosition;
469 }
470 } else {
471 return CoverageMode::kNone;
472 }
473}
474
475// This needs to stay in sync w/ QuadPerEdgeAAGeometryProcessor::initializeAttrs
476size_t VertexSpec::vertexSize() const {
477 bool needsPerspective = (this->deviceDimensionality() == 3);
478 CoverageMode coverageMode = this->coverageMode();
479
480 size_t count = 0;
481
482 if (coverageMode == CoverageMode::kWithPosition) {
483 if (needsPerspective) {
484 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
485 } else {
486 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType) +
487 GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
488 }
489 } else {
490 if (needsPerspective) {
491 count += GrVertexAttribTypeSize(kFloat3_GrVertexAttribType);
492 } else {
493 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType);
494 }
495 }
496
Brian Salomon2432d062020-04-16 20:48:09 -0400497 if (this->requiresGeometrySubset()) {
Robert Phillips29f38542019-10-16 09:20:25 -0400498 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
499 }
500
501 count += this->localDimensionality() * GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
502
503 if (ColorType::kByte == this->colorType()) {
504 count += GrVertexAttribTypeSize(kUByte4_norm_GrVertexAttribType);
Brian Osman2715bf52019-12-06 14:38:47 -0500505 } else if (ColorType::kFloat == this->colorType()) {
506 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
Robert Phillips29f38542019-10-16 09:20:25 -0400507 }
508
Brian Salomon2432d062020-04-16 20:48:09 -0400509 if (this->hasSubset()) {
Robert Phillips29f38542019-10-16 09:20:25 -0400510 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
511 }
512
513 return count;
514}
515
Michael Ludwig467994d2018-12-03 14:58:31 +0000516////////////////// Geometry Processor Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500517
Michael Ludwig467994d2018-12-03 14:58:31 +0000518class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor {
519public:
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400520 using Saturate = GrTextureOp::Saturate;
Michael Ludwig20e909e2018-10-30 10:43:57 -0400521
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500522 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const VertexSpec& spec) {
523 return arena->make<QuadPerEdgeAAGeometryProcessor>(spec);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400524 }
525
Brian Salomonccb61422020-01-09 10:46:36 -0500526 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
527 const VertexSpec& vertexSpec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500528 const GrShaderCaps& caps,
529 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500530 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500531 const GrSwizzle& swizzle,
532 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
533 Saturate saturate) {
534 return arena->make<QuadPerEdgeAAGeometryProcessor>(
Robert Phillips323471e2019-11-11 11:33:37 -0500535 vertexSpec, caps, backendFormat, samplerState, swizzle,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500536 std::move(textureColorSpaceXform), saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400537 }
538
Michael Ludwig467994d2018-12-03 14:58:31 +0000539 const char* name() const override { return "QuadPerEdgeAAGeometryProcessor"; }
Michael Ludwig024e2622018-11-30 13:25:55 -0500540
Michael Ludwig467994d2018-12-03 14:58:31 +0000541 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400542 // texturing, device-dimensions are single bit flags
Brian Salomon2432d062020-04-16 20:48:09 -0400543 uint32_t x = (fTexSubset.isInitialized() ? 0 : 0x1)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400544 | (fSampler.isInitialized() ? 0 : 0x2)
545 | (fNeedsPerspective ? 0 : 0x4)
546 | (fSaturate == Saturate::kNo ? 0 : 0x8);
Michael Ludwig467994d2018-12-03 14:58:31 +0000547 // local coords require 2 bits (3 choices), 00 for none, 01 for 2d, 10 for 3d
548 if (fLocalCoord.isInitialized()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400549 x |= kFloat3_GrVertexAttribType == fLocalCoord.cpuType() ? 0x10 : 0x20;
Brian Osman78dc72c2018-12-03 13:20:43 +0000550 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000551 // similar for colors, 00 for none, 01 for bytes, 10 for half-floats
Michael Ludwig93aeba02018-12-21 09:50:31 -0500552 if (fColor.isInitialized()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400553 x |= kUByte4_norm_GrVertexAttribType == fColor.cpuType() ? 0x40 : 0x80;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500554 }
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400555 // and coverage mode, 00 for none, 01 for withposition, 10 for withcolor, 11 for
Brian Salomon2432d062020-04-16 20:48:09 -0400556 // position+geomsubset
557 SkASSERT(!fGeomSubset.isInitialized() || fCoverageMode == CoverageMode::kWithPosition);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500558 if (fCoverageMode != CoverageMode::kNone) {
Brian Salomon2432d062020-04-16 20:48:09 -0400559 x |= fGeomSubset.isInitialized()
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400560 ? 0x300
561 : (CoverageMode::kWithPosition == fCoverageMode ? 0x100 : 0x200);
Michael Ludwig467994d2018-12-03 14:58:31 +0000562 }
563
564 b->add32(GrColorSpaceXform::XformKey(fTextureColorSpaceXform.get()));
565 b->add32(x);
Brian Osman78dc72c2018-12-03 13:20:43 +0000566 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000567
568 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
569 class GLSLProcessor : public GrGLSLGeometryProcessor {
570 public:
Brian Osman609f1592020-07-01 15:14:39 -0400571 void setData(const GrGLSLProgramDataManager& pdman,
572 const GrPrimitiveProcessor& proc) override {
Michael Ludwig467994d2018-12-03 14:58:31 +0000573 const auto& gp = proc.cast<QuadPerEdgeAAGeometryProcessor>();
Michael Ludwig467994d2018-12-03 14:58:31 +0000574 fTextureColorSpaceXformHelper.setData(pdman, gp.fTextureColorSpaceXform.get());
575 }
576
577 private:
578 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
579 using Interpolation = GrGLSLVaryingHandler::Interpolation;
580
581 const auto& gp = args.fGP.cast<QuadPerEdgeAAGeometryProcessor>();
582 fTextureColorSpaceXformHelper.emitCode(args.fUniformHandler,
583 gp.fTextureColorSpaceXform.get());
584
585 args.fVaryingHandler->emitAttributes(gp);
586
Michael Ludwig93aeba02018-12-21 09:50:31 -0500587 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
588 // Strip last channel from the vertex attribute to remove coverage and get the
589 // actual position
590 if (gp.fNeedsPerspective) {
591 args.fVertBuilder->codeAppendf("float3 position = %s.xyz;",
592 gp.fPosition.name());
593 } else {
594 args.fVertBuilder->codeAppendf("float2 position = %s.xy;",
595 gp.fPosition.name());
596 }
597 gpArgs->fPositionVar = {"position",
598 gp.fNeedsPerspective ? kFloat3_GrSLType
599 : kFloat2_GrSLType,
Ben Wagnerdabd98c2020-03-25 15:17:18 -0400600 GrShaderVar::TypeModifier::None};
Michael Ludwig467994d2018-12-03 14:58:31 +0000601 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500602 // No coverage to eliminate
603 gpArgs->fPositionVar = gp.fPosition.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000604 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000605
Michael Ludwig553db622020-06-19 10:47:30 -0400606 // This attribute will be uninitialized if earlier FP analysis determined no
607 // local coordinates are needed (and this will not include the inline texture
608 // fetch this GP does before invoking FPs).
609 gpArgs->fLocalCoordVar = gp.fLocalCoord.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000610
611 // Solid color before any texturing gets modulated in
612 if (gp.fColor.isInitialized()) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400613 SkASSERT(gp.fCoverageMode != CoverageMode::kWithColor || !gp.fNeedsPerspective);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500614 // The color cannot be flat if the varying coverage has been modulated into it
Michael Ludwig467994d2018-12-03 14:58:31 +0000615 args.fVaryingHandler->addPassThroughAttribute(gp.fColor, args.fOutputColor,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500616 gp.fCoverageMode == CoverageMode::kWithColor ?
617 Interpolation::kInterpolated : Interpolation::kCanBeFlat);
618 } else {
619 // Output color must be initialized to something
620 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
Michael Ludwig467994d2018-12-03 14:58:31 +0000621 }
622
623 // If there is a texture, must also handle texture coordinates and reading from
624 // the texture in the fragment shader before continuing to fragment processors.
625 if (gp.fSampler.isInitialized()) {
Brian Salomon2432d062020-04-16 20:48:09 -0400626 // Texture coordinates clamped by the subset on the fragment shader; if the GP
Michael Ludwig467994d2018-12-03 14:58:31 +0000627 // has a texture, it's guaranteed to have local coordinates
628 args.fFragBuilder->codeAppend("float2 texCoord;");
629 if (gp.fLocalCoord.cpuType() == kFloat3_GrVertexAttribType) {
630 // Can't do a pass through since we need to perform perspective division
631 GrGLSLVarying v(gp.fLocalCoord.gpuType());
632 args.fVaryingHandler->addVarying(gp.fLocalCoord.name(), &v);
633 args.fVertBuilder->codeAppendf("%s = %s;",
634 v.vsOut(), gp.fLocalCoord.name());
635 args.fFragBuilder->codeAppendf("texCoord = %s.xy / %s.z;",
636 v.fsIn(), v.fsIn());
637 } else {
638 args.fVaryingHandler->addPassThroughAttribute(gp.fLocalCoord, "texCoord");
639 }
640
Brian Salomon2432d062020-04-16 20:48:09 -0400641 // Clamp the now 2D localCoordName variable by the subset if it is provided
642 if (gp.fTexSubset.isInitialized()) {
643 args.fFragBuilder->codeAppend("float4 subset;");
644 args.fVaryingHandler->addPassThroughAttribute(gp.fTexSubset, "subset",
Michael Ludwig467994d2018-12-03 14:58:31 +0000645 Interpolation::kCanBeFlat);
646 args.fFragBuilder->codeAppend(
Brian Salomon2432d062020-04-16 20:48:09 -0400647 "texCoord = clamp(texCoord, subset.xy, subset.zw);");
Michael Ludwig467994d2018-12-03 14:58:31 +0000648 }
649
650 // Now modulate the starting output color by the texture lookup
651 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
Brian Salomon87e9ddb2019-12-19 14:50:22 -0500652 args.fFragBuilder->appendTextureLookupAndBlend(
653 args.fOutputColor, SkBlendMode::kModulate, args.fTexSamplers[0],
Brian Salomond19cd762020-01-06 13:16:31 -0500654 "texCoord", &fTextureColorSpaceXformHelper);
Michael Ludwig467994d2018-12-03 14:58:31 +0000655 args.fFragBuilder->codeAppend(";");
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400656 if (gp.fSaturate == Saturate::kYes) {
657 args.fFragBuilder->codeAppendf("%s = saturate(%s);",
658 args.fOutputColor, args.fOutputColor);
659 }
660 } else {
661 // Saturate is only intended for use with a proxy to account for the fact
662 // that GrTextureOp skips SkPaint conversion, which normally handles this.
663 SkASSERT(gp.fSaturate == Saturate::kNo);
Michael Ludwig467994d2018-12-03 14:58:31 +0000664 }
665
666 // And lastly, output the coverage calculation code
Michael Ludwig93aeba02018-12-21 09:50:31 -0500667 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
668 GrGLSLVarying coverage(kFloat_GrSLType);
669 args.fVaryingHandler->addVarying("coverage", &coverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000670 if (gp.fNeedsPerspective) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400671 // Multiply by "W" in the vertex shader, then by 1/w (sk_FragCoord.w) in
672 // the fragment shader to get screen-space linear coverage.
673 args.fVertBuilder->codeAppendf("%s = %s.w * %s.z;",
674 coverage.vsOut(), gp.fPosition.name(),
675 gp.fPosition.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400676 args.fFragBuilder->codeAppendf("float coverage = %s * sk_FragCoord.w;",
677 coverage.fsIn());
Michael Ludwig93aeba02018-12-21 09:50:31 -0500678 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400679 args.fVertBuilder->codeAppendf("%s = %s;",
680 coverage.vsOut(), gp.fCoverage.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400681 args.fFragBuilder->codeAppendf("float coverage = %s;", coverage.fsIn());
Michael Ludwig467994d2018-12-03 14:58:31 +0000682 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500683
Brian Salomon2432d062020-04-16 20:48:09 -0400684 if (gp.fGeomSubset.isInitialized()) {
685 // Calculate distance from sk_FragCoord to the 4 edges of the subset
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400686 // and clamp them to (0, 1). Use the minimum of these and the original
687 // coverage. This only has to be done in the exterior triangles, the
Brian Salomon2432d062020-04-16 20:48:09 -0400688 // interior of the quad geometry can never be clipped by the subset box.
689 args.fFragBuilder->codeAppend("float4 geoSubset;");
690 args.fVaryingHandler->addPassThroughAttribute(gp.fGeomSubset, "geoSubset",
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400691 Interpolation::kCanBeFlat);
692 args.fFragBuilder->codeAppend(
693 "if (coverage < 0.5) {"
694 " float4 dists4 = clamp(float4(1, 1, -1, -1) * "
Brian Salomon2432d062020-04-16 20:48:09 -0400695 "(sk_FragCoord.xyxy - geoSubset), 0, 1);"
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400696 " float2 dists2 = dists4.xy * dists4.zw;"
697 " coverage = min(coverage, dists2.x * dists2.y);"
698 "}");
699 }
700
701 args.fFragBuilder->codeAppendf("%s = half4(half(coverage));",
702 args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000703 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500704 // Set coverage to 1, since it's either non-AA or the coverage was already
705 // folded into the output color
Brian Salomon2432d062020-04-16 20:48:09 -0400706 SkASSERT(!gp.fGeomSubset.isInitialized());
Ethan Nicholase1f55022019-02-05 17:17:40 -0500707 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000708 }
709 }
710 GrGLSLColorSpaceXformHelper fTextureColorSpaceXformHelper;
711 };
712 return new GLSLProcessor;
713 }
714
715private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500716 friend class ::SkArenaAlloc; // for access to ctor
717
Michael Ludwig467994d2018-12-03 14:58:31 +0000718 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec)
719 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
720 , fTextureColorSpaceXform(nullptr) {
Brian Salomon2432d062020-04-16 20:48:09 -0400721 SkASSERT(!spec.hasSubset());
Michael Ludwig467994d2018-12-03 14:58:31 +0000722 this->initializeAttrs(spec);
723 this->setTextureSamplerCnt(0);
724 }
725
Brian Salomon67529b22019-08-13 15:31:04 -0400726 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec,
727 const GrShaderCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400728 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500729 GrSamplerState samplerState,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400730 const GrSwizzle& swizzle,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400731 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
732 Saturate saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000733 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400734 , fSaturate(saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000735 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Robert Phillips323471e2019-11-11 11:33:37 -0500736 , fSampler(samplerState, backendFormat, swizzle) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500737 SkASSERT(spec.hasLocalCoords());
Michael Ludwig467994d2018-12-03 14:58:31 +0000738 this->initializeAttrs(spec);
739 this->setTextureSamplerCnt(1);
740 }
741
Robert Phillips29f38542019-10-16 09:20:25 -0400742 // This needs to stay in sync w/ VertexSpec::vertexSize
Michael Ludwig467994d2018-12-03 14:58:31 +0000743 void initializeAttrs(const VertexSpec& spec) {
744 fNeedsPerspective = spec.deviceDimensionality() == 3;
Robert Phillips29f38542019-10-16 09:20:25 -0400745 fCoverageMode = spec.coverageMode();
Michael Ludwig93aeba02018-12-21 09:50:31 -0500746
747 if (fCoverageMode == CoverageMode::kWithPosition) {
748 if (fNeedsPerspective) {
749 fPosition = {"positionWithCoverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
750 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400751 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
752 fCoverage = {"coverage", kFloat_GrVertexAttribType, kFloat_GrSLType};
Michael Ludwig93aeba02018-12-21 09:50:31 -0500753 }
754 } else {
755 if (fNeedsPerspective) {
756 fPosition = {"position", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
757 } else {
758 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
759 }
760 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000761
Brian Salomon2432d062020-04-16 20:48:09 -0400762 // Need a geometry subset when the quads are AA and not rectilinear, since their AA
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400763 // outsetting can go beyond a half pixel.
Brian Salomon2432d062020-04-16 20:48:09 -0400764 if (spec.requiresGeometrySubset()) {
765 fGeomSubset = {"geomSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400766 }
767
Michael Ludwig467994d2018-12-03 14:58:31 +0000768 int localDim = spec.localDimensionality();
769 if (localDim == 3) {
770 fLocalCoord = {"localCoord", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
771 } else if (localDim == 2) {
772 fLocalCoord = {"localCoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
773 } // else localDim == 0 and attribute remains uninitialized
774
Brian Osman2715bf52019-12-06 14:38:47 -0500775 if (spec.hasVertexColors()) {
776 fColor = MakeColorAttribute("color", ColorType::kFloat == spec.colorType());
Michael Ludwig467994d2018-12-03 14:58:31 +0000777 }
778
Brian Salomon2432d062020-04-16 20:48:09 -0400779 if (spec.hasSubset()) {
780 fTexSubset = {"texSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwig467994d2018-12-03 14:58:31 +0000781 }
782
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400783 this->setVertexAttributes(&fPosition, 6);
Michael Ludwig467994d2018-12-03 14:58:31 +0000784 }
785
786 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
787
Michael Ludwig93aeba02018-12-21 09:50:31 -0500788 Attribute fPosition; // May contain coverage as last channel
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400789 Attribute fCoverage; // Used for non-perspective position to avoid Intel Metal issues
Michael Ludwig93aeba02018-12-21 09:50:31 -0500790 Attribute fColor; // May have coverage modulated in if the FPs support it
Michael Ludwig467994d2018-12-03 14:58:31 +0000791 Attribute fLocalCoord;
Brian Salomon2432d062020-04-16 20:48:09 -0400792 Attribute fGeomSubset; // Screen-space bounding box on geometry+aa outset
793 Attribute fTexSubset; // Texture-space bounding box on local coords
Michael Ludwig467994d2018-12-03 14:58:31 +0000794
Michael Ludwig93aeba02018-12-21 09:50:31 -0500795 // The positions attribute may have coverage built into it, so float3 is an ambiguous type
796 // and may mean 2d with coverage, or 3d with no coverage
Michael Ludwig467994d2018-12-03 14:58:31 +0000797 bool fNeedsPerspective;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400798 // Should saturate() be called on the color? Only relevant when created with a texture.
799 Saturate fSaturate = Saturate::kNo;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500800 CoverageMode fCoverageMode;
Michael Ludwig467994d2018-12-03 14:58:31 +0000801
802 // Color space will be null and fSampler.isInitialized() returns false when the GP is configured
803 // to skip texturing.
804 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
805 TextureSampler fSampler;
806
807 typedef GrGeometryProcessor INHERITED;
808};
809
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500810GrGeometryProcessor* MakeProcessor(SkArenaAlloc* arena, const VertexSpec& spec) {
811 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec);
Michael Ludwig467994d2018-12-03 14:58:31 +0000812}
813
Brian Salomonccb61422020-01-09 10:46:36 -0500814GrGeometryProcessor* MakeTexturedProcessor(SkArenaAlloc* arena,
815 const VertexSpec& spec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500816 const GrShaderCaps& caps,
817 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500818 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500819 const GrSwizzle& swizzle,
820 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
Brian Salomonccb61422020-01-09 10:46:36 -0500821 Saturate saturate) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500822 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec, caps, backendFormat, samplerState,
823 swizzle, std::move(textureColorSpaceXform),
824 saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400825}
Michael Ludwigc182b942018-11-16 10:27:51 -0500826
827} // namespace GrQuadPerEdgeAA