blob: ca0c7c0a022305bbe225f621b515850b0d1f0f9e [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
Michael Ludwigf995c052018-11-26 15:24:29 -050020
Michael Ludwig460eb5e2018-10-29 11:09:29 -040021namespace {
22
Michael Ludwig73dbea62019-11-19 14:55:36 -050023// Generic WriteQuadProc that can handle any VertexSpec. It writes the 4 vertices in triangle strip
24// order, although the data per-vertex is dependent on the VertexSpec.
25static void write_quad_generic(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
26 const GrQuad& deviceQuad, const GrQuad& localQuad,
27 const float coverage[4], const SkPMColor4f& color,
28 const SkRect& geomDomain, const SkRect& texDomain) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050029 static constexpr auto If = GrVertexWriter::If<float>;
Michael Ludwig73dbea62019-11-19 14:55:36 -050030 GrQuadPerEdgeAA::CoverageMode mode = spec.coverageMode();
Michael Ludwig553e9a92018-11-29 12:38:35 -050031 for (int i = 0; i < 4; ++i) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050032 // save position, this is a float2 or float3 or float4 depending on the combination of
33 // perspective and coverage mode.
Michael Ludwigfb7ba522019-10-29 15:33:34 -040034 vb->write(deviceQuad.x(i), deviceQuad.y(i),
35 If(spec.deviceQuadType() == GrQuad::Type::kPerspective, deviceQuad.w(i)),
Robert Phillips29f38542019-10-16 09:20:25 -040036 If(mode == GrQuadPerEdgeAA::CoverageMode::kWithPosition, coverage[i]));
Michael Ludwig4921dc32018-12-03 14:57:29 +000037
Michael Ludwig93aeba02018-12-21 09:50:31 -050038 // save color
39 if (spec.hasVertexColors()) {
Brian Salomon1d835422019-03-13 16:11:44 -040040 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kHalf;
Michael Ludwige6266a22019-03-07 11:24:32 -050041 vb->write(GrVertexColor(
Michael Ludwig73dbea62019-11-19 14:55:36 -050042 color * (mode == GrQuadPerEdgeAA::CoverageMode::kWithColor ? coverage[i] : 1.f),
Robert Phillips29f38542019-10-16 09:20:25 -040043 wide));
Michael Ludwig93aeba02018-12-21 09:50:31 -050044 }
45
46 // save local position
47 if (spec.hasLocalCoords()) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -040048 vb->write(localQuad.x(i), localQuad.y(i),
49 If(spec.localQuadType() == GrQuad::Type::kPerspective, localQuad.w(i)));
Michael Ludwig93aeba02018-12-21 09:50:31 -050050 }
51
Michael Ludwigdcfbe322019-04-01 14:55:54 -040052 // save the geometry domain
53 if (spec.requiresGeometryDomain()) {
54 vb->write(geomDomain);
55 }
56
57 // save the texture domain
Michael Ludwig93aeba02018-12-21 09:50:31 -050058 if (spec.hasDomain()) {
Michael Ludwigdcfbe322019-04-01 14:55:54 -040059 vb->write(texDomain);
Michael Ludwig93aeba02018-12-21 09:50:31 -050060 }
61 }
62}
63
Michael Ludwig73dbea62019-11-19 14:55:36 -050064// Specialized WriteQuadProcs for particular VertexSpecs that show up frequently (determined
65// experimentally through recorded GMs, SKPs, and SVGs, as well as SkiaRenderer's usage patterns):
66
67// 2D (XY), no explicit coverage, vertex color, no locals, no geometry domain, no texture domain
68// This represents simple, solid color or shader, non-AA (or AA with cov. as alpha) rects.
69static void write_2d_color(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
70 const GrQuad& deviceQuad, const GrQuad& localQuad,
71 const float coverage[4], const SkPMColor4f& color,
72 const SkRect& geomDomain, const SkRect& texDomain) {
73 // Assert assumptions about VertexSpec
74 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
75 SkASSERT(!spec.hasLocalCoords());
76 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
77 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
78 SkASSERT(spec.hasVertexColors());
79 SkASSERT(!spec.requiresGeometryDomain());
80 SkASSERT(!spec.hasDomain());
81
82 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kHalf;
83 for (int i = 0; i < 4; ++i) {
84 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
85 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
86 coverage[i] == 1.f);
87 vb->write(deviceQuad.x(i), deviceQuad.y(i), GrVertexColor(color * coverage[i], wide));
88 }
89}
90
91// 2D (XY), no explicit coverage, UV locals, no color, no geometry domain, no texture domain
92// This represents opaque, non AA, textured rects
93static void write_2d_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
94 const GrQuad& deviceQuad, const GrQuad& localQuad,
95 const float coverage[4], const SkPMColor4f& color,
96 const SkRect& geomDomain, const SkRect& texDomain) {
97 // Assert assumptions about VertexSpec
98 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
99 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
100 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone);
101 SkASSERT(!spec.hasVertexColors());
102 SkASSERT(!spec.requiresGeometryDomain());
103 SkASSERT(!spec.hasDomain());
104
105 for (int i = 0; i < 4; ++i) {
106 vb->write(deviceQuad.x(i), deviceQuad.y(i), localQuad.x(i), localQuad.y(i));
107 }
108}
109
110// 2D (XY), no explicit coverage, UV locals, vertex color, no geometry or texture domains
111// This represents transparent, non AA (or AA with cov. as alpha), textured rects
112static void write_2d_color_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
113 const GrQuad& deviceQuad, const GrQuad& localQuad,
114 const float coverage[4], const SkPMColor4f& color,
115 const SkRect& geomDomain, const SkRect& texDomain) {
116 // Assert assumptions about VertexSpec
117 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
118 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
119 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
120 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
121 SkASSERT(spec.hasVertexColors());
122 SkASSERT(!spec.requiresGeometryDomain());
123 SkASSERT(!spec.hasDomain());
124
125 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kHalf;
126 for (int i = 0; i < 4; ++i) {
127 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
128 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
129 coverage[i] == 1.f);
130 vb->write(deviceQuad.x(i), deviceQuad.y(i), GrVertexColor(color * coverage[i], wide),
131 localQuad.x(i), localQuad.y(i));
132 }
133}
134
135// 2D (XY), explicit coverage, UV locals, no color, no geometry domain, no texture domain
136// This represents opaque, AA, textured rects
137static void write_2d_cov_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
138 const GrQuad& deviceQuad, const GrQuad& localQuad,
139 const float coverage[4], const SkPMColor4f& color,
140 const SkRect& geomDomain, const SkRect& texDomain) {
141 // Assert assumptions about VertexSpec
142 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
143 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
144 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition);
145 SkASSERT(!spec.hasVertexColors());
146 SkASSERT(!spec.requiresGeometryDomain());
147 SkASSERT(!spec.hasDomain());
148
149 for (int i = 0; i < 4; ++i) {
150 vb->write(deviceQuad.x(i), deviceQuad.y(i), coverage[i], localQuad.x(i), localQuad.y(i));
151 }
152}
153
154// NOTE: The three _strict specializations below match the non-strict uv functions above, except
155// that they also write the UV domain. These are included to benefit SkiaRenderer, which must make
156// use of both fast and strict constrained domains. When testing _strict was not that common across
157// GMS, SKPs, and SVGs but we have little visibility into actual SkiaRenderer statistics. If
158// SkiaRenderer can avoid domains more, these 3 functions should probably be removed for simplicity.
159
160// 2D (XY), no explicit coverage, UV locals, no color, tex domain but no geometry domain
161// This represents opaque, non AA, textured rects with strict uv sampling
162static void write_2d_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
163 const GrQuad& deviceQuad, const GrQuad& localQuad,
164 const float coverage[4], const SkPMColor4f& color,
165 const SkRect& geomDomain, const SkRect& texDomain) {
166 // Assert assumptions about VertexSpec
167 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
168 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
169 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone);
170 SkASSERT(!spec.hasVertexColors());
171 SkASSERT(!spec.requiresGeometryDomain());
172 SkASSERT(spec.hasDomain());
173
174 for (int i = 0; i < 4; ++i) {
175 vb->write(deviceQuad.x(i), deviceQuad.y(i), localQuad.x(i), localQuad.y(i), texDomain);
176 }
177}
178
179// 2D (XY), no explicit coverage, UV locals, vertex color, tex domain but no geometry domain
180// This represents transparent, non AA (or AA with cov. as alpha), textured rects with strict sample
181static void write_2d_color_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
182 const GrQuad& deviceQuad, const GrQuad& localQuad,
183 const float coverage[4], const SkPMColor4f& color,
184 const SkRect& geomDomain, const SkRect& texDomain) {
185 // Assert assumptions about VertexSpec
186 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
187 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
188 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
189 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
190 SkASSERT(spec.hasVertexColors());
191 SkASSERT(!spec.requiresGeometryDomain());
192 SkASSERT(spec.hasDomain());
193
194 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kHalf;
195 for (int i = 0; i < 4; ++i) {
196 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
197 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
198 coverage[i] == 1.f);
199 vb->write(deviceQuad.x(i), deviceQuad.y(i), GrVertexColor(color * coverage[i], wide),
200 localQuad.x(i), localQuad.y(i), texDomain);
201 }
202}
203
204// 2D (XY), explicit coverage, UV locals, no color, tex domain but no geometry domain
205// This represents opaque, AA, textured rects with strict uv sampling
206static void write_2d_cov_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
207 const GrQuad& deviceQuad, const GrQuad& localQuad,
208 const float coverage[4], const SkPMColor4f& color,
209 const SkRect& geomDomain, const SkRect& texDomain) {
210 // Assert assumptions about VertexSpec
211 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
212 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
213 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition);
214 SkASSERT(!spec.hasVertexColors());
215 SkASSERT(!spec.requiresGeometryDomain());
216 SkASSERT(spec.hasDomain());
217
218 for (int i = 0; i < 4; ++i) {
219 vb->write(deviceQuad.x(i), deviceQuad.y(i), coverage[i],
220 localQuad.x(i), localQuad.y(i), texDomain);
221 }
222}
223
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400224} // anonymous namespace
225
Michael Ludwigc182b942018-11-16 10:27:51 -0500226namespace GrQuadPerEdgeAA {
227
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400228IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads) {
229 if (aa == GrAAType::kCoverage) {
230 return IndexBufferOption::kPictureFramed;
231 } else if (numQuads > 1) {
232 return IndexBufferOption::kIndexedRects;
233 } else {
234 return IndexBufferOption::kTriStrips;
235 }
236}
237
Brian Osman8fa7ab42019-03-18 10:22:42 -0400238// This is a more elaborate version of SkPMColor4fNeedsWideColor that allows "no color" for white
239ColorType MinColorType(SkPMColor4f color, GrClampType clampType, const GrCaps& caps) {
Brian Salomon1d835422019-03-13 16:11:44 -0400240 if (color == SK_PMColor4fWHITE) {
241 return ColorType::kNone;
Brian Salomon1d835422019-03-13 16:11:44 -0400242 } else {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400243 return SkPMColor4fNeedsWideColor(color, clampType, caps) ? ColorType::kHalf
244 : ColorType::kByte;
Brian Salomon1d835422019-03-13 16:11:44 -0400245 }
246}
247
Michael Ludwig73dbea62019-11-19 14:55:36 -0500248////////////////// Tessellator Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500249
Michael Ludwig73dbea62019-11-19 14:55:36 -0500250Tessellator::WriteQuadProc Tessellator::GetWriteQuadProc(const VertexSpec& spec) {
251 // All specialized writing functions requires 2D geometry and no geometry domain. This is not
252 // the same as just checking device type vs. kRectilinear since non-AA general 2D quads do not
253 // require a geometry domain and could then go through a fast path.
254 if (spec.deviceQuadType() != GrQuad::Type::kPerspective && !spec.requiresGeometryDomain()) {
255 CoverageMode mode = spec.coverageMode();
256 if (spec.hasVertexColors()) {
257 if (mode != CoverageMode::kWithPosition) {
258 // Vertex colors, but no explicit coverage
259 if (!spec.hasLocalCoords()) {
260 // Non-UV with vertex colors (possibly with coverage folded into alpha)
261 return write_2d_color;
262 } else if (spec.localQuadType() != GrQuad::Type::kPerspective) {
263 // UV locals with vertex colors (possibly with coverage-as-alpha)
264 return spec.hasDomain() ? write_2d_color_uv_strict : write_2d_color_uv;
265 }
266 }
267 // Else fall through; this is a spec that requires vertex colors and explicit coverage,
268 // which means it's anti-aliased and the FPs don't support coverage as alpha, or
269 // it uses 3D local coordinates.
270 } else if (spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective) {
271 if (mode == CoverageMode::kWithPosition) {
272 // UV locals with explicit coverage
273 return spec.hasDomain() ? write_2d_cov_uv_strict : write_2d_cov_uv;
274 } else {
275 SkASSERT(mode == CoverageMode::kNone);
276 return spec.hasDomain() ? write_2d_uv_strict : write_2d_uv;
277 }
278 }
279 // Else fall through to generic vertex function; this is a spec that has no vertex colors
280 // and [no|uvr] local coords, which doesn't happen often enough to warrant specialization.
281 }
Michael Ludwig41f395d2019-05-23 13:59:45 -0400282
Michael Ludwig73dbea62019-11-19 14:55:36 -0500283 // Arbitrary spec hits the slow path
284 return write_quad_generic;
285}
286
Michael Ludwig189c9802019-11-21 11:21:12 -0500287Tessellator::Tessellator(const VertexSpec& spec, char* vertices)
Michael Ludwig73dbea62019-11-19 14:55:36 -0500288 : fVertexSpec(spec)
Michael Ludwig189c9802019-11-21 11:21:12 -0500289 , fVertexWriter{vertices}
Michael Ludwig73dbea62019-11-19 14:55:36 -0500290 , fWriteProc(Tessellator::GetWriteQuadProc(spec)) {}
291
Michael Ludwig189c9802019-11-21 11:21:12 -0500292void Tessellator::append(const GrQuad& deviceQuad, const GrQuad& localQuad,
293 const SkPMColor4f& color, const SkRect& uvDomain, GrQuadAAFlags aaFlags) {
294 // We allow Tessellator to be created with a null vertices pointer for convenience, but it is
295 // assumed it will never actually be used in those cases.
296 SkASSERT(fVertexWriter.fPtr);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500297 SkASSERT(deviceQuad.quadType() <= fVertexSpec.deviceQuadType());
298 SkASSERT(!fVertexSpec.hasLocalCoords() || localQuad.quadType() <= fVertexSpec.localQuadType());
299
300 static const float kFullCoverage[4] = {1.f, 1.f, 1.f, 1.f};
301 static const float kZeroCoverage[4] = {0.f, 0.f, 0.f, 0.f};
302 static const SkRect kIgnoredDomain = SkRect::MakeEmpty();
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400303
Michael Ludwig73dbea62019-11-19 14:55:36 -0500304 if (fVertexSpec.usesCoverageAA()) {
305 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kWithColor ||
306 fVertexSpec.coverageMode() == CoverageMode::kWithPosition);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400307 // Must calculate inner and outer quadrilaterals for the vertex coverage ramps, and possibly
Michael Ludwig73dbea62019-11-19 14:55:36 -0500308 // a geometry domain if corners are not right angles
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400309 SkRect geomDomain;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500310 if (fVertexSpec.requiresGeometryDomain()) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400311 geomDomain = deviceQuad.bounds();
312 geomDomain.outset(0.5f, 0.5f); // account for AA expansion
Michael Ludwige6266a22019-03-07 11:24:32 -0500313 }
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400314
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500315 if (aaFlags == GrQuadAAFlags::kNone) {
316 // Have to write the coverage AA vertex structure, but there's no math to be done for a
317 // non-aa quad batched into a coverage AA op.
Michael Ludwig189c9802019-11-21 11:21:12 -0500318 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500319 geomDomain, uvDomain);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500320 // Since we pass the same corners in, the outer vertex structure will have 0 area and
321 // the coverage interpolation from 1 to 0 will not be visible.
Michael Ludwig189c9802019-11-21 11:21:12 -0500322 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500323 geomDomain, uvDomain);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500324 } else {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500325 // Reset the tessellation helper to match the current geometry
326 fAAHelper.reset(deviceQuad, fVertexSpec.hasLocalCoords() ? &localQuad : nullptr);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400327
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500328 // Edge inset/outset distance ordered LBTR, set to 0.5 for a half pixel if the AA flag
329 // is turned on, or 0.0 if the edge is not anti-aliased.
330 skvx::Vec<4, float> edgeDistances;
331 if (aaFlags == GrQuadAAFlags::kAll) {
332 edgeDistances = 0.5f;
333 } else {
334 edgeDistances = { (aaFlags & GrQuadAAFlags::kLeft) ? 0.5f : 0.f,
335 (aaFlags & GrQuadAAFlags::kBottom) ? 0.5f : 0.f,
336 (aaFlags & GrQuadAAFlags::kTop) ? 0.5f : 0.f,
337 (aaFlags & GrQuadAAFlags::kRight) ? 0.5f : 0.f };
338 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400339
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500340 GrQuad aaDeviceQuad, aaLocalQuad;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500341
342 // Write inner vertices first
343 float coverage[4];
344 fAAHelper.inset(edgeDistances, &aaDeviceQuad, &aaLocalQuad).store(coverage);
Michael Ludwig189c9802019-11-21 11:21:12 -0500345 fWriteProc(&fVertexWriter, fVertexSpec, aaDeviceQuad, aaLocalQuad, coverage, color,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500346 geomDomain, uvDomain);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500347
348 // Then outer vertices, which use 0.f for their coverage
Michael Ludwig73dbea62019-11-19 14:55:36 -0500349 fAAHelper.outset(edgeDistances, &aaDeviceQuad, &aaLocalQuad);
Michael Ludwig189c9802019-11-21 11:21:12 -0500350 fWriteProc(&fVertexWriter, fVertexSpec, aaDeviceQuad, aaLocalQuad, kZeroCoverage, color,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500351 geomDomain, uvDomain);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500352 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500353 } else {
354 // No outsetting needed, just write a single quad with full coverage
Michael Ludwig73dbea62019-11-19 14:55:36 -0500355 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kNone &&
356 !fVertexSpec.requiresGeometryDomain());
Michael Ludwig189c9802019-11-21 11:21:12 -0500357 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500358 kIgnoredDomain, uvDomain);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400359 }
360}
Michael Ludwig20e909e2018-10-30 10:43:57 -0400361
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400362sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawOp::Target* target,
363 IndexBufferOption indexBufferOption) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400364 auto resourceProvider = target->resourceProvider();
365
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400366 switch (indexBufferOption) {
367 case IndexBufferOption::kPictureFramed: return resourceProvider->refAAQuadIndexBuffer();
368 case IndexBufferOption::kIndexedRects: return resourceProvider->refNonAAQuadIndexBuffer();
369 case IndexBufferOption::kTriStrips: // fall through
370 default: return nullptr;
371 }
372}
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400373
Robert Phillips438d9862019-11-14 12:46:05 -0500374int QuadLimit(IndexBufferOption option) {
375 switch (option) {
376 case IndexBufferOption::kPictureFramed: return GrResourceProvider::MaxNumAAQuads();
377 case IndexBufferOption::kIndexedRects: return GrResourceProvider::MaxNumNonAAQuads();
378 case IndexBufferOption::kTriStrips: return SK_MaxS32; // not limited by an indexBuffer
379 }
380
381 SkUNREACHABLE;
382}
Michael Ludwig93aeba02018-12-21 09:50:31 -0500383
Robert Phillips2f05a482019-11-25 09:54:43 -0500384void ConfigureMesh(const GrCaps& caps, GrMesh* mesh, const VertexSpec& spec,
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400385 int runningQuadCount, int quadsInDraw, int maxVerts,
386 sk_sp<const GrBuffer> vertexBuffer,
387 sk_sp<const GrBuffer> indexBuffer, int absVertBufferOffset) {
388 SkASSERT(vertexBuffer);
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400389
Robert Phillipscea290f2019-11-06 11:21:03 -0500390 mesh->setPrimitiveType(spec.primitiveType());
391
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400392 if (spec.indexBufferOption() == IndexBufferOption::kTriStrips) {
393 SkASSERT(!indexBuffer);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500394
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400395 mesh->setNonIndexedNonInstanced(4);
396 int offset = absVertBufferOffset +
397 runningQuadCount * GrResourceProvider::NumVertsPerNonAAQuad();
398 mesh->setVertexData(std::move(vertexBuffer), offset);
399 return;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500400 }
401
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400402 SkASSERT(spec.indexBufferOption() == IndexBufferOption::kPictureFramed ||
403 spec.indexBufferOption() == IndexBufferOption::kIndexedRects);
404 SkASSERT(indexBuffer);
405
Robert Phillips2f05a482019-11-25 09:54:43 -0500406 int maxNumQuads, numIndicesPerQuad, numVertsPerQuad;
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400407
408 if (spec.indexBufferOption() == IndexBufferOption::kPictureFramed) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400409 // AA uses 8 vertices and 30 indices per quad, basically nested rectangles
Robert Phillips2f05a482019-11-25 09:54:43 -0500410 maxNumQuads = GrResourceProvider::MaxNumAAQuads();
411 numIndicesPerQuad = GrResourceProvider::NumIndicesPerAAQuad();
412 numVertsPerQuad = GrResourceProvider::NumVertsPerAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400413 } else {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400414 // Non-AA uses 4 vertices and 6 indices per quad
Robert Phillips2f05a482019-11-25 09:54:43 -0500415 maxNumQuads = GrResourceProvider::MaxNumNonAAQuads();
416 numIndicesPerQuad = GrResourceProvider::NumIndicesPerNonAAQuad();
417 numVertsPerQuad = GrResourceProvider::NumVertsPerNonAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400418 }
419
Robert Phillips2f05a482019-11-25 09:54:43 -0500420 SkASSERT(runningQuadCount + quadsInDraw <= maxNumQuads);
421
422 if (caps.avoidLargeIndexBufferDraws()) {
423 // When we need to avoid large index buffer draws we modify the base vertex of the draw
424 // which, in GL, requires rebinding all vertex attrib arrays, so a base index is generally
425 // preferred.
426 int offset = absVertBufferOffset + runningQuadCount * numVertsPerQuad;
427
428 mesh->setIndexedPatterned(std::move(indexBuffer), numIndicesPerQuad,
429 numVertsPerQuad, quadsInDraw, maxNumQuads);
430 mesh->setVertexData(std::move(vertexBuffer), offset);
431 } else {
432 int baseIndex = runningQuadCount * numIndicesPerQuad;
433 int numIndicesToDraw = quadsInDraw * numIndicesPerQuad;
434
435 int minVertex = runningQuadCount * numVertsPerQuad;
436 int maxVertex = (runningQuadCount + quadsInDraw) * numVertsPerQuad;
437
438 mesh->setIndexed(std::move(indexBuffer), numIndicesToDraw,
439 baseIndex, minVertex, maxVertex, GrPrimitiveRestart::kNo);
440 mesh->setVertexData(std::move(vertexBuffer), absVertBufferOffset);
441 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500442}
443
Michael Ludwigc182b942018-11-16 10:27:51 -0500444////////////////// VertexSpec Implementation
Michael Ludwig20e909e2018-10-30 10:43:57 -0400445
Michael Ludwigc182b942018-11-16 10:27:51 -0500446int VertexSpec::deviceDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400447 return this->deviceQuadType() == GrQuad::Type::kPerspective ? 3 : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -0500448}
449
450int VertexSpec::localDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400451 return fHasLocalCoords ? (this->localQuadType() == GrQuad::Type::kPerspective ? 3 : 2) : 0;
Michael Ludwigc182b942018-11-16 10:27:51 -0500452}
453
Robert Phillips29f38542019-10-16 09:20:25 -0400454CoverageMode VertexSpec::coverageMode() const {
455 if (this->usesCoverageAA()) {
456 if (this->compatibleWithCoverageAsAlpha() && this->hasVertexColors() &&
457 !this->requiresGeometryDomain()) {
458 // Using a geometric domain acts as a second source of coverage and folding
459 // the original coverage into color makes it impossible to apply the color's
460 // alpha to the geometric domain's coverage when the original shape is clipped.
461 return CoverageMode::kWithColor;
462 } else {
463 return CoverageMode::kWithPosition;
464 }
465 } else {
466 return CoverageMode::kNone;
467 }
468}
469
470// This needs to stay in sync w/ QuadPerEdgeAAGeometryProcessor::initializeAttrs
471size_t VertexSpec::vertexSize() const {
472 bool needsPerspective = (this->deviceDimensionality() == 3);
473 CoverageMode coverageMode = this->coverageMode();
474
475 size_t count = 0;
476
477 if (coverageMode == CoverageMode::kWithPosition) {
478 if (needsPerspective) {
479 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
480 } else {
481 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType) +
482 GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
483 }
484 } else {
485 if (needsPerspective) {
486 count += GrVertexAttribTypeSize(kFloat3_GrVertexAttribType);
487 } else {
488 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType);
489 }
490 }
491
492 if (this->requiresGeometryDomain()) {
493 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
494 }
495
496 count += this->localDimensionality() * GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
497
498 if (ColorType::kByte == this->colorType()) {
499 count += GrVertexAttribTypeSize(kUByte4_norm_GrVertexAttribType);
500 } else if (ColorType::kHalf == this->colorType()) {
501 count += GrVertexAttribTypeSize(kHalf4_GrVertexAttribType);
502 }
503
504 if (this->hasDomain()) {
505 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
506 }
507
508 return count;
509}
510
Michael Ludwig467994d2018-12-03 14:58:31 +0000511////////////////// Geometry Processor Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500512
Michael Ludwig467994d2018-12-03 14:58:31 +0000513class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor {
514public:
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400515 using Saturate = GrTextureOp::Saturate;
Michael Ludwig20e909e2018-10-30 10:43:57 -0400516
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500517 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const VertexSpec& spec) {
518 return arena->make<QuadPerEdgeAAGeometryProcessor>(spec);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400519 }
520
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500521 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const VertexSpec& vertexSpec,
522 const GrShaderCaps& caps,
523 const GrBackendFormat& backendFormat,
524 const GrSamplerState& samplerState,
525 const GrSwizzle& swizzle,
526 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
527 Saturate saturate) {
528 return arena->make<QuadPerEdgeAAGeometryProcessor>(
Robert Phillips323471e2019-11-11 11:33:37 -0500529 vertexSpec, caps, backendFormat, samplerState, swizzle,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500530 std::move(textureColorSpaceXform), saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400531 }
532
Michael Ludwig467994d2018-12-03 14:58:31 +0000533 const char* name() const override { return "QuadPerEdgeAAGeometryProcessor"; }
Michael Ludwig024e2622018-11-30 13:25:55 -0500534
Michael Ludwig467994d2018-12-03 14:58:31 +0000535 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400536 // texturing, device-dimensions are single bit flags
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400537 uint32_t x = (fTexDomain.isInitialized() ? 0 : 0x1)
538 | (fSampler.isInitialized() ? 0 : 0x2)
539 | (fNeedsPerspective ? 0 : 0x4)
540 | (fSaturate == Saturate::kNo ? 0 : 0x8);
Michael Ludwig467994d2018-12-03 14:58:31 +0000541 // local coords require 2 bits (3 choices), 00 for none, 01 for 2d, 10 for 3d
542 if (fLocalCoord.isInitialized()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400543 x |= kFloat3_GrVertexAttribType == fLocalCoord.cpuType() ? 0x10 : 0x20;
Brian Osman78dc72c2018-12-03 13:20:43 +0000544 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000545 // similar for colors, 00 for none, 01 for bytes, 10 for half-floats
Michael Ludwig93aeba02018-12-21 09:50:31 -0500546 if (fColor.isInitialized()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400547 x |= kUByte4_norm_GrVertexAttribType == fColor.cpuType() ? 0x40 : 0x80;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500548 }
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400549 // and coverage mode, 00 for none, 01 for withposition, 10 for withcolor, 11 for
550 // position+geomdomain
551 SkASSERT(!fGeomDomain.isInitialized() || fCoverageMode == CoverageMode::kWithPosition);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500552 if (fCoverageMode != CoverageMode::kNone) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400553 x |= fGeomDomain.isInitialized()
554 ? 0x300
555 : (CoverageMode::kWithPosition == fCoverageMode ? 0x100 : 0x200);
Michael Ludwig467994d2018-12-03 14:58:31 +0000556 }
557
558 b->add32(GrColorSpaceXform::XformKey(fTextureColorSpaceXform.get()));
559 b->add32(x);
Brian Osman78dc72c2018-12-03 13:20:43 +0000560 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000561
562 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
563 class GLSLProcessor : public GrGLSLGeometryProcessor {
564 public:
565 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
566 FPCoordTransformIter&& transformIter) override {
567 const auto& gp = proc.cast<QuadPerEdgeAAGeometryProcessor>();
568 if (gp.fLocalCoord.isInitialized()) {
569 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
570 }
571 fTextureColorSpaceXformHelper.setData(pdman, gp.fTextureColorSpaceXform.get());
572 }
573
574 private:
575 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
576 using Interpolation = GrGLSLVaryingHandler::Interpolation;
577
578 const auto& gp = args.fGP.cast<QuadPerEdgeAAGeometryProcessor>();
579 fTextureColorSpaceXformHelper.emitCode(args.fUniformHandler,
580 gp.fTextureColorSpaceXform.get());
581
582 args.fVaryingHandler->emitAttributes(gp);
583
Michael Ludwig93aeba02018-12-21 09:50:31 -0500584 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
585 // Strip last channel from the vertex attribute to remove coverage and get the
586 // actual position
587 if (gp.fNeedsPerspective) {
588 args.fVertBuilder->codeAppendf("float3 position = %s.xyz;",
589 gp.fPosition.name());
590 } else {
591 args.fVertBuilder->codeAppendf("float2 position = %s.xy;",
592 gp.fPosition.name());
593 }
594 gpArgs->fPositionVar = {"position",
595 gp.fNeedsPerspective ? kFloat3_GrSLType
596 : kFloat2_GrSLType,
597 GrShaderVar::kNone_TypeModifier};
Michael Ludwig467994d2018-12-03 14:58:31 +0000598 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500599 // No coverage to eliminate
600 gpArgs->fPositionVar = gp.fPosition.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000601 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000602
603 // Handle local coordinates if they exist
604 if (gp.fLocalCoord.isInitialized()) {
605 // NOTE: If the only usage of local coordinates is for the inline texture fetch
606 // before FPs, then there are no registered FPCoordTransforms and this ends up
607 // emitting nothing, so there isn't a duplication of local coordinates
608 this->emitTransforms(args.fVertBuilder,
609 args.fVaryingHandler,
610 args.fUniformHandler,
611 gp.fLocalCoord.asShaderVar(),
612 args.fFPCoordTransformHandler);
613 }
614
615 // Solid color before any texturing gets modulated in
616 if (gp.fColor.isInitialized()) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400617 SkASSERT(gp.fCoverageMode != CoverageMode::kWithColor || !gp.fNeedsPerspective);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500618 // The color cannot be flat if the varying coverage has been modulated into it
Michael Ludwig467994d2018-12-03 14:58:31 +0000619 args.fVaryingHandler->addPassThroughAttribute(gp.fColor, args.fOutputColor,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500620 gp.fCoverageMode == CoverageMode::kWithColor ?
621 Interpolation::kInterpolated : Interpolation::kCanBeFlat);
622 } else {
623 // Output color must be initialized to something
624 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
Michael Ludwig467994d2018-12-03 14:58:31 +0000625 }
626
627 // If there is a texture, must also handle texture coordinates and reading from
628 // the texture in the fragment shader before continuing to fragment processors.
629 if (gp.fSampler.isInitialized()) {
630 // Texture coordinates clamped by the domain on the fragment shader; if the GP
631 // has a texture, it's guaranteed to have local coordinates
632 args.fFragBuilder->codeAppend("float2 texCoord;");
633 if (gp.fLocalCoord.cpuType() == kFloat3_GrVertexAttribType) {
634 // Can't do a pass through since we need to perform perspective division
635 GrGLSLVarying v(gp.fLocalCoord.gpuType());
636 args.fVaryingHandler->addVarying(gp.fLocalCoord.name(), &v);
637 args.fVertBuilder->codeAppendf("%s = %s;",
638 v.vsOut(), gp.fLocalCoord.name());
639 args.fFragBuilder->codeAppendf("texCoord = %s.xy / %s.z;",
640 v.fsIn(), v.fsIn());
641 } else {
642 args.fVaryingHandler->addPassThroughAttribute(gp.fLocalCoord, "texCoord");
643 }
644
645 // Clamp the now 2D localCoordName variable by the domain if it is provided
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400646 if (gp.fTexDomain.isInitialized()) {
Michael Ludwig467994d2018-12-03 14:58:31 +0000647 args.fFragBuilder->codeAppend("float4 domain;");
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400648 args.fVaryingHandler->addPassThroughAttribute(gp.fTexDomain, "domain",
Michael Ludwig467994d2018-12-03 14:58:31 +0000649 Interpolation::kCanBeFlat);
650 args.fFragBuilder->codeAppend(
651 "texCoord = clamp(texCoord, domain.xy, domain.zw);");
652 }
653
654 // Now modulate the starting output color by the texture lookup
655 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
656 args.fFragBuilder->appendTextureLookupAndModulate(
657 args.fOutputColor, args.fTexSamplers[0], "texCoord", kFloat2_GrSLType,
658 &fTextureColorSpaceXformHelper);
659 args.fFragBuilder->codeAppend(";");
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400660 if (gp.fSaturate == Saturate::kYes) {
661 args.fFragBuilder->codeAppendf("%s = saturate(%s);",
662 args.fOutputColor, args.fOutputColor);
663 }
664 } else {
665 // Saturate is only intended for use with a proxy to account for the fact
666 // that GrTextureOp skips SkPaint conversion, which normally handles this.
667 SkASSERT(gp.fSaturate == Saturate::kNo);
Michael Ludwig467994d2018-12-03 14:58:31 +0000668 }
669
670 // And lastly, output the coverage calculation code
Michael Ludwig93aeba02018-12-21 09:50:31 -0500671 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
672 GrGLSLVarying coverage(kFloat_GrSLType);
673 args.fVaryingHandler->addVarying("coverage", &coverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000674 if (gp.fNeedsPerspective) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400675 // Multiply by "W" in the vertex shader, then by 1/w (sk_FragCoord.w) in
676 // the fragment shader to get screen-space linear coverage.
677 args.fVertBuilder->codeAppendf("%s = %s.w * %s.z;",
678 coverage.vsOut(), gp.fPosition.name(),
679 gp.fPosition.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400680 args.fFragBuilder->codeAppendf("float coverage = %s * sk_FragCoord.w;",
681 coverage.fsIn());
Michael Ludwig93aeba02018-12-21 09:50:31 -0500682 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400683 args.fVertBuilder->codeAppendf("%s = %s;",
684 coverage.vsOut(), gp.fCoverage.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400685 args.fFragBuilder->codeAppendf("float coverage = %s;", coverage.fsIn());
Michael Ludwig467994d2018-12-03 14:58:31 +0000686 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500687
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400688 if (gp.fGeomDomain.isInitialized()) {
689 // Calculate distance from sk_FragCoord to the 4 edges of the domain
690 // and clamp them to (0, 1). Use the minimum of these and the original
691 // coverage. This only has to be done in the exterior triangles, the
692 // interior of the quad geometry can never be clipped by the domain box.
693 args.fFragBuilder->codeAppend("float4 geoDomain;");
694 args.fVaryingHandler->addPassThroughAttribute(gp.fGeomDomain, "geoDomain",
695 Interpolation::kCanBeFlat);
696 args.fFragBuilder->codeAppend(
697 "if (coverage < 0.5) {"
698 " float4 dists4 = clamp(float4(1, 1, -1, -1) * "
699 "(sk_FragCoord.xyxy - geoDomain), 0, 1);"
700 " float2 dists2 = dists4.xy * dists4.zw;"
701 " coverage = min(coverage, dists2.x * dists2.y);"
702 "}");
703 }
704
705 args.fFragBuilder->codeAppendf("%s = half4(half(coverage));",
706 args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000707 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500708 // Set coverage to 1, since it's either non-AA or the coverage was already
709 // folded into the output color
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400710 SkASSERT(!gp.fGeomDomain.isInitialized());
Ethan Nicholase1f55022019-02-05 17:17:40 -0500711 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000712 }
713 }
714 GrGLSLColorSpaceXformHelper fTextureColorSpaceXformHelper;
715 };
716 return new GLSLProcessor;
717 }
718
719private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500720 friend class ::SkArenaAlloc; // for access to ctor
721
Michael Ludwig467994d2018-12-03 14:58:31 +0000722 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec)
723 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
724 , fTextureColorSpaceXform(nullptr) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500725 SkASSERT(!spec.hasDomain());
Michael Ludwig467994d2018-12-03 14:58:31 +0000726 this->initializeAttrs(spec);
727 this->setTextureSamplerCnt(0);
728 }
729
Brian Salomon67529b22019-08-13 15:31:04 -0400730 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec,
731 const GrShaderCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400732 const GrBackendFormat& backendFormat,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500733 const GrSamplerState& samplerState,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400734 const GrSwizzle& swizzle,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400735 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
736 Saturate saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000737 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400738 , fSaturate(saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000739 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Robert Phillips323471e2019-11-11 11:33:37 -0500740 , fSampler(samplerState, backendFormat, swizzle) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500741 SkASSERT(spec.hasLocalCoords());
Michael Ludwig467994d2018-12-03 14:58:31 +0000742 this->initializeAttrs(spec);
743 this->setTextureSamplerCnt(1);
744 }
745
Robert Phillips29f38542019-10-16 09:20:25 -0400746 // This needs to stay in sync w/ VertexSpec::vertexSize
Michael Ludwig467994d2018-12-03 14:58:31 +0000747 void initializeAttrs(const VertexSpec& spec) {
748 fNeedsPerspective = spec.deviceDimensionality() == 3;
Robert Phillips29f38542019-10-16 09:20:25 -0400749 fCoverageMode = spec.coverageMode();
Michael Ludwig93aeba02018-12-21 09:50:31 -0500750
751 if (fCoverageMode == CoverageMode::kWithPosition) {
752 if (fNeedsPerspective) {
753 fPosition = {"positionWithCoverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
754 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400755 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
756 fCoverage = {"coverage", kFloat_GrVertexAttribType, kFloat_GrSLType};
Michael Ludwig93aeba02018-12-21 09:50:31 -0500757 }
758 } else {
759 if (fNeedsPerspective) {
760 fPosition = {"position", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
761 } else {
762 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
763 }
764 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000765
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400766 // Need a geometry domain when the quads are AA and not rectilinear, since their AA
767 // outsetting can go beyond a half pixel.
768 if (spec.requiresGeometryDomain()) {
769 fGeomDomain = {"geomDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
770 }
771
Michael Ludwig467994d2018-12-03 14:58:31 +0000772 int localDim = spec.localDimensionality();
773 if (localDim == 3) {
774 fLocalCoord = {"localCoord", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
775 } else if (localDim == 2) {
776 fLocalCoord = {"localCoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
777 } // else localDim == 0 and attribute remains uninitialized
778
779 if (ColorType::kByte == spec.colorType()) {
780 fColor = {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
781 } else if (ColorType::kHalf == spec.colorType()) {
782 fColor = {"color", kHalf4_GrVertexAttribType, kHalf4_GrSLType};
783 }
784
785 if (spec.hasDomain()) {
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400786 fTexDomain = {"texDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwig467994d2018-12-03 14:58:31 +0000787 }
788
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400789 this->setVertexAttributes(&fPosition, 6);
Michael Ludwig467994d2018-12-03 14:58:31 +0000790 }
791
792 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
793
Michael Ludwig93aeba02018-12-21 09:50:31 -0500794 Attribute fPosition; // May contain coverage as last channel
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400795 Attribute fCoverage; // Used for non-perspective position to avoid Intel Metal issues
Michael Ludwig93aeba02018-12-21 09:50:31 -0500796 Attribute fColor; // May have coverage modulated in if the FPs support it
Michael Ludwig467994d2018-12-03 14:58:31 +0000797 Attribute fLocalCoord;
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400798 Attribute fGeomDomain; // Screen-space bounding box on geometry+aa outset
799 Attribute fTexDomain; // Texture-space bounding box on local coords
Michael Ludwig467994d2018-12-03 14:58:31 +0000800
Michael Ludwig93aeba02018-12-21 09:50:31 -0500801 // The positions attribute may have coverage built into it, so float3 is an ambiguous type
802 // and may mean 2d with coverage, or 3d with no coverage
Michael Ludwig467994d2018-12-03 14:58:31 +0000803 bool fNeedsPerspective;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400804 // Should saturate() be called on the color? Only relevant when created with a texture.
805 Saturate fSaturate = Saturate::kNo;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500806 CoverageMode fCoverageMode;
Michael Ludwig467994d2018-12-03 14:58:31 +0000807
808 // Color space will be null and fSampler.isInitialized() returns false when the GP is configured
809 // to skip texturing.
810 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
811 TextureSampler fSampler;
812
813 typedef GrGeometryProcessor INHERITED;
814};
815
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500816GrGeometryProcessor* MakeProcessor(SkArenaAlloc* arena, const VertexSpec& spec) {
817 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec);
Michael Ludwig467994d2018-12-03 14:58:31 +0000818}
819
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500820GrGeometryProcessor* MakeTexturedProcessor(SkArenaAlloc* arena, const VertexSpec& spec,
821 const GrShaderCaps& caps,
822 const GrBackendFormat& backendFormat,
823 const GrSamplerState& samplerState,
824 const GrSwizzle& swizzle,
825 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
826 Saturate saturate) {
827 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec, caps, backendFormat, samplerState,
828 swizzle, std::move(textureColorSpaceXform),
829 saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400830}
Michael Ludwigc182b942018-11-16 10:27:51 -0500831
832} // namespace GrQuadPerEdgeAA