blob: 66b84a900fbfc3349fe7adcf9d74a0867b8bf931 [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,
Michael Ludwig704d5402019-11-25 09:43:37 -050026 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -050027 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 Ludwig704d5402019-11-25 09:43:37 -050030
31 SkASSERT(!spec.hasLocalCoords() || localQuad);
32
Michael Ludwig73dbea62019-11-19 14:55:36 -050033 GrQuadPerEdgeAA::CoverageMode mode = spec.coverageMode();
Michael Ludwig553e9a92018-11-29 12:38:35 -050034 for (int i = 0; i < 4; ++i) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050035 // save position, this is a float2 or float3 or float4 depending on the combination of
36 // perspective and coverage mode.
Michael Ludwig704d5402019-11-25 09:43:37 -050037 vb->write(deviceQuad->x(i), deviceQuad->y(i),
38 If(spec.deviceQuadType() == GrQuad::Type::kPerspective, deviceQuad->w(i)),
Robert Phillips29f38542019-10-16 09:20:25 -040039 If(mode == GrQuadPerEdgeAA::CoverageMode::kWithPosition, coverage[i]));
Michael Ludwig4921dc32018-12-03 14:57:29 +000040
Michael Ludwig93aeba02018-12-21 09:50:31 -050041 // save color
42 if (spec.hasVertexColors()) {
Brian Osman2715bf52019-12-06 14:38:47 -050043 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwige6266a22019-03-07 11:24:32 -050044 vb->write(GrVertexColor(
Michael Ludwig73dbea62019-11-19 14:55:36 -050045 color * (mode == GrQuadPerEdgeAA::CoverageMode::kWithColor ? coverage[i] : 1.f),
Robert Phillips29f38542019-10-16 09:20:25 -040046 wide));
Michael Ludwig93aeba02018-12-21 09:50:31 -050047 }
48
49 // save local position
50 if (spec.hasLocalCoords()) {
Michael Ludwig704d5402019-11-25 09:43:37 -050051 vb->write(localQuad->x(i), localQuad->y(i),
52 If(spec.localQuadType() == GrQuad::Type::kPerspective, localQuad->w(i)));
Michael Ludwig93aeba02018-12-21 09:50:31 -050053 }
54
Michael Ludwigdcfbe322019-04-01 14:55:54 -040055 // save the geometry domain
56 if (spec.requiresGeometryDomain()) {
57 vb->write(geomDomain);
58 }
59
60 // save the texture domain
Michael Ludwig93aeba02018-12-21 09:50:31 -050061 if (spec.hasDomain()) {
Michael Ludwigdcfbe322019-04-01 14:55:54 -040062 vb->write(texDomain);
Michael Ludwig93aeba02018-12-21 09:50:31 -050063 }
64 }
65}
66
Michael Ludwig73dbea62019-11-19 14:55:36 -050067// Specialized WriteQuadProcs for particular VertexSpecs that show up frequently (determined
68// experimentally through recorded GMs, SKPs, and SVGs, as well as SkiaRenderer's usage patterns):
69
70// 2D (XY), no explicit coverage, vertex color, no locals, no geometry domain, no texture domain
71// This represents simple, solid color or shader, non-AA (or AA with cov. as alpha) rects.
72static void write_2d_color(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -050073 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -050074 const float coverage[4], const SkPMColor4f& color,
75 const SkRect& geomDomain, const SkRect& texDomain) {
76 // Assert assumptions about VertexSpec
77 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
78 SkASSERT(!spec.hasLocalCoords());
79 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
80 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
81 SkASSERT(spec.hasVertexColors());
82 SkASSERT(!spec.requiresGeometryDomain());
83 SkASSERT(!spec.hasDomain());
Michael Ludwig704d5402019-11-25 09:43:37 -050084 // We don't assert that localQuad == nullptr, since it is possible for GrFillRectOp to
85 // accumulate local coords conservatively (paint not trivial), and then after analysis realize
86 // the processors don't need local coordinates.
Michael Ludwig73dbea62019-11-19 14:55:36 -050087
Brian Osman2715bf52019-12-06 14:38:47 -050088 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -050089 for (int i = 0; i < 4; ++i) {
90 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
91 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
92 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -050093 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide));
Michael Ludwig73dbea62019-11-19 14:55:36 -050094 }
95}
96
97// 2D (XY), no explicit coverage, UV locals, no color, no geometry domain, no texture domain
98// This represents opaque, non AA, textured rects
99static void write_2d_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500100 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500101 const float coverage[4], const SkPMColor4f& color,
102 const SkRect& geomDomain, const SkRect& texDomain) {
103 // Assert assumptions about VertexSpec
104 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
105 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
106 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone);
107 SkASSERT(!spec.hasVertexColors());
108 SkASSERT(!spec.requiresGeometryDomain());
109 SkASSERT(!spec.hasDomain());
Michael Ludwig704d5402019-11-25 09:43:37 -0500110 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500111
112 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500113 vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500114 }
115}
116
117// 2D (XY), no explicit coverage, UV locals, vertex color, no geometry or texture domains
118// This represents transparent, non AA (or AA with cov. as alpha), textured rects
119static void write_2d_color_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500120 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500121 const float coverage[4], const SkPMColor4f& color,
122 const SkRect& geomDomain, const SkRect& texDomain) {
123 // Assert assumptions about VertexSpec
124 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
125 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
126 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
127 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
128 SkASSERT(spec.hasVertexColors());
129 SkASSERT(!spec.requiresGeometryDomain());
130 SkASSERT(!spec.hasDomain());
Michael Ludwig704d5402019-11-25 09:43:37 -0500131 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500132
Brian Osman2715bf52019-12-06 14:38:47 -0500133 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500134 for (int i = 0; i < 4; ++i) {
135 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
136 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
137 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500138 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide),
139 localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500140 }
141}
142
143// 2D (XY), explicit coverage, UV locals, no color, no geometry domain, no texture domain
144// This represents opaque, AA, textured rects
145static void write_2d_cov_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500146 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500147 const float coverage[4], const SkPMColor4f& color,
148 const SkRect& geomDomain, const SkRect& texDomain) {
149 // Assert assumptions about VertexSpec
150 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
151 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
152 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition);
153 SkASSERT(!spec.hasVertexColors());
154 SkASSERT(!spec.requiresGeometryDomain());
155 SkASSERT(!spec.hasDomain());
Michael Ludwig704d5402019-11-25 09:43:37 -0500156 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500157
158 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500159 vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i],
160 localQuad->x(i), localQuad->y(i));
Michael Ludwig73dbea62019-11-19 14:55:36 -0500161 }
162}
163
164// NOTE: The three _strict specializations below match the non-strict uv functions above, except
165// that they also write the UV domain. These are included to benefit SkiaRenderer, which must make
166// use of both fast and strict constrained domains. When testing _strict was not that common across
167// GMS, SKPs, and SVGs but we have little visibility into actual SkiaRenderer statistics. If
168// SkiaRenderer can avoid domains more, these 3 functions should probably be removed for simplicity.
169
170// 2D (XY), no explicit coverage, UV locals, no color, tex domain but no geometry domain
171// This represents opaque, non AA, textured rects with strict uv sampling
172static void write_2d_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500173 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500174 const float coverage[4], const SkPMColor4f& color,
175 const SkRect& geomDomain, const SkRect& texDomain) {
176 // Assert assumptions about VertexSpec
177 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
178 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
179 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone);
180 SkASSERT(!spec.hasVertexColors());
181 SkASSERT(!spec.requiresGeometryDomain());
182 SkASSERT(spec.hasDomain());
Michael Ludwig704d5402019-11-25 09:43:37 -0500183 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500184
185 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500186 vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i), texDomain);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500187 }
188}
189
190// 2D (XY), no explicit coverage, UV locals, vertex color, tex domain but no geometry domain
191// This represents transparent, non AA (or AA with cov. as alpha), textured rects with strict sample
192static void write_2d_color_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500193 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500194 const float coverage[4], const SkPMColor4f& color,
195 const SkRect& geomDomain, const SkRect& texDomain) {
196 // Assert assumptions about VertexSpec
197 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
198 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
199 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone ||
200 spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor);
201 SkASSERT(spec.hasVertexColors());
202 SkASSERT(!spec.requiresGeometryDomain());
203 SkASSERT(spec.hasDomain());
Michael Ludwig704d5402019-11-25 09:43:37 -0500204 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500205
Brian Osman2715bf52019-12-06 14:38:47 -0500206 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500207 for (int i = 0; i < 4; ++i) {
208 // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything
209 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor ||
210 coverage[i] == 1.f);
Michael Ludwig704d5402019-11-25 09:43:37 -0500211 vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide),
212 localQuad->x(i), localQuad->y(i), texDomain);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500213 }
214}
215
216// 2D (XY), explicit coverage, UV locals, no color, tex domain but no geometry domain
217// This represents opaque, AA, textured rects with strict uv sampling
218static void write_2d_cov_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwig704d5402019-11-25 09:43:37 -0500219 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500220 const float coverage[4], const SkPMColor4f& color,
221 const SkRect& geomDomain, const SkRect& texDomain) {
222 // Assert assumptions about VertexSpec
223 SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective);
224 SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective);
225 SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition);
226 SkASSERT(!spec.hasVertexColors());
227 SkASSERT(!spec.requiresGeometryDomain());
228 SkASSERT(spec.hasDomain());
Michael Ludwig704d5402019-11-25 09:43:37 -0500229 SkASSERT(localQuad);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500230
231 for (int i = 0; i < 4; ++i) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500232 vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i],
233 localQuad->x(i), localQuad->y(i), texDomain);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500234 }
235}
236
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400237} // anonymous namespace
238
Michael Ludwigc182b942018-11-16 10:27:51 -0500239namespace GrQuadPerEdgeAA {
240
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400241IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads) {
242 if (aa == GrAAType::kCoverage) {
243 return IndexBufferOption::kPictureFramed;
244 } else if (numQuads > 1) {
245 return IndexBufferOption::kIndexedRects;
246 } else {
247 return IndexBufferOption::kTriStrips;
248 }
249}
250
Brian Osman2715bf52019-12-06 14:38:47 -0500251// This is a more elaborate version of fitsInBytes() that allows "no color" for white
252ColorType MinColorType(SkPMColor4f color) {
Brian Salomon1d835422019-03-13 16:11:44 -0400253 if (color == SK_PMColor4fWHITE) {
254 return ColorType::kNone;
Brian Salomon1d835422019-03-13 16:11:44 -0400255 } else {
Brian Osman2715bf52019-12-06 14:38:47 -0500256 return color.fitsInBytes() ? ColorType::kByte : ColorType::kFloat;
Brian Salomon1d835422019-03-13 16:11:44 -0400257 }
258}
259
Michael Ludwig73dbea62019-11-19 14:55:36 -0500260////////////////// Tessellator Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500261
Michael Ludwig73dbea62019-11-19 14:55:36 -0500262Tessellator::WriteQuadProc Tessellator::GetWriteQuadProc(const VertexSpec& spec) {
263 // All specialized writing functions requires 2D geometry and no geometry domain. This is not
264 // the same as just checking device type vs. kRectilinear since non-AA general 2D quads do not
265 // require a geometry domain and could then go through a fast path.
266 if (spec.deviceQuadType() != GrQuad::Type::kPerspective && !spec.requiresGeometryDomain()) {
267 CoverageMode mode = spec.coverageMode();
268 if (spec.hasVertexColors()) {
269 if (mode != CoverageMode::kWithPosition) {
270 // Vertex colors, but no explicit coverage
271 if (!spec.hasLocalCoords()) {
272 // Non-UV with vertex colors (possibly with coverage folded into alpha)
273 return write_2d_color;
274 } else if (spec.localQuadType() != GrQuad::Type::kPerspective) {
275 // UV locals with vertex colors (possibly with coverage-as-alpha)
276 return spec.hasDomain() ? write_2d_color_uv_strict : write_2d_color_uv;
277 }
278 }
279 // Else fall through; this is a spec that requires vertex colors and explicit coverage,
280 // which means it's anti-aliased and the FPs don't support coverage as alpha, or
281 // it uses 3D local coordinates.
282 } else if (spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective) {
283 if (mode == CoverageMode::kWithPosition) {
284 // UV locals with explicit coverage
285 return spec.hasDomain() ? write_2d_cov_uv_strict : write_2d_cov_uv;
286 } else {
287 SkASSERT(mode == CoverageMode::kNone);
288 return spec.hasDomain() ? write_2d_uv_strict : write_2d_uv;
289 }
290 }
291 // Else fall through to generic vertex function; this is a spec that has no vertex colors
292 // and [no|uvr] local coords, which doesn't happen often enough to warrant specialization.
293 }
Michael Ludwig41f395d2019-05-23 13:59:45 -0400294
Michael Ludwig73dbea62019-11-19 14:55:36 -0500295 // Arbitrary spec hits the slow path
296 return write_quad_generic;
297}
298
Michael Ludwig189c9802019-11-21 11:21:12 -0500299Tessellator::Tessellator(const VertexSpec& spec, char* vertices)
Michael Ludwig73dbea62019-11-19 14:55:36 -0500300 : fVertexSpec(spec)
Michael Ludwig189c9802019-11-21 11:21:12 -0500301 , fVertexWriter{vertices}
Michael Ludwig73dbea62019-11-19 14:55:36 -0500302 , fWriteProc(Tessellator::GetWriteQuadProc(spec)) {}
303
Michael Ludwig704d5402019-11-25 09:43:37 -0500304void Tessellator::append(GrQuad* deviceQuad, GrQuad* localQuad,
Michael Ludwig189c9802019-11-21 11:21:12 -0500305 const SkPMColor4f& color, const SkRect& uvDomain, GrQuadAAFlags aaFlags) {
306 // We allow Tessellator to be created with a null vertices pointer for convenience, but it is
307 // assumed it will never actually be used in those cases.
308 SkASSERT(fVertexWriter.fPtr);
Michael Ludwig704d5402019-11-25 09:43:37 -0500309 SkASSERT(deviceQuad->quadType() <= fVertexSpec.deviceQuadType());
310 SkASSERT(localQuad || !fVertexSpec.hasLocalCoords());
311 SkASSERT(!fVertexSpec.hasLocalCoords() || localQuad->quadType() <= fVertexSpec.localQuadType());
Michael Ludwig73dbea62019-11-19 14:55:36 -0500312
313 static const float kFullCoverage[4] = {1.f, 1.f, 1.f, 1.f};
314 static const float kZeroCoverage[4] = {0.f, 0.f, 0.f, 0.f};
315 static const SkRect kIgnoredDomain = SkRect::MakeEmpty();
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400316
Michael Ludwig73dbea62019-11-19 14:55:36 -0500317 if (fVertexSpec.usesCoverageAA()) {
318 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kWithColor ||
319 fVertexSpec.coverageMode() == CoverageMode::kWithPosition);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400320 // Must calculate inner and outer quadrilaterals for the vertex coverage ramps, and possibly
Michael Ludwig73dbea62019-11-19 14:55:36 -0500321 // a geometry domain if corners are not right angles
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400322 SkRect geomDomain;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500323 if (fVertexSpec.requiresGeometryDomain()) {
Michael Ludwig704d5402019-11-25 09:43:37 -0500324 geomDomain = deviceQuad->bounds();
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400325 geomDomain.outset(0.5f, 0.5f); // account for AA expansion
Michael Ludwige6266a22019-03-07 11:24:32 -0500326 }
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400327
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500328 if (aaFlags == GrQuadAAFlags::kNone) {
329 // Have to write the coverage AA vertex structure, but there's no math to be done for a
330 // non-aa quad batched into a coverage AA op.
Michael Ludwig189c9802019-11-21 11:21:12 -0500331 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500332 geomDomain, uvDomain);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500333 // Since we pass the same corners in, the outer vertex structure will have 0 area and
334 // the coverage interpolation from 1 to 0 will not be visible.
Michael Ludwig189c9802019-11-21 11:21:12 -0500335 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500336 geomDomain, uvDomain);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500337 } else {
Michael Ludwig73dbea62019-11-19 14:55:36 -0500338 // Reset the tessellation helper to match the current geometry
Michael Ludwig704d5402019-11-25 09:43:37 -0500339 fAAHelper.reset(*deviceQuad, localQuad);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400340
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500341 // Edge inset/outset distance ordered LBTR, set to 0.5 for a half pixel if the AA flag
342 // is turned on, or 0.0 if the edge is not anti-aliased.
343 skvx::Vec<4, float> edgeDistances;
344 if (aaFlags == GrQuadAAFlags::kAll) {
345 edgeDistances = 0.5f;
346 } else {
347 edgeDistances = { (aaFlags & GrQuadAAFlags::kLeft) ? 0.5f : 0.f,
348 (aaFlags & GrQuadAAFlags::kBottom) ? 0.5f : 0.f,
349 (aaFlags & GrQuadAAFlags::kTop) ? 0.5f : 0.f,
350 (aaFlags & GrQuadAAFlags::kRight) ? 0.5f : 0.f };
351 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400352
Michael Ludwig73dbea62019-11-19 14:55:36 -0500353 // Write inner vertices first
354 float coverage[4];
Michael Ludwig704d5402019-11-25 09:43:37 -0500355 fAAHelper.inset(edgeDistances, deviceQuad, localQuad).store(coverage);
356 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, coverage, color,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500357 geomDomain, uvDomain);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500358
359 // Then outer vertices, which use 0.f for their coverage
Michael Ludwig704d5402019-11-25 09:43:37 -0500360 fAAHelper.outset(edgeDistances, deviceQuad, localQuad);
361 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500362 geomDomain, uvDomain);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500363 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500364 } else {
365 // No outsetting needed, just write a single quad with full coverage
Michael Ludwig73dbea62019-11-19 14:55:36 -0500366 SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kNone &&
367 !fVertexSpec.requiresGeometryDomain());
Michael Ludwig189c9802019-11-21 11:21:12 -0500368 fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500369 kIgnoredDomain, uvDomain);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400370 }
371}
Michael Ludwig20e909e2018-10-30 10:43:57 -0400372
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400373sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawOp::Target* target,
374 IndexBufferOption indexBufferOption) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400375 auto resourceProvider = target->resourceProvider();
376
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400377 switch (indexBufferOption) {
378 case IndexBufferOption::kPictureFramed: return resourceProvider->refAAQuadIndexBuffer();
379 case IndexBufferOption::kIndexedRects: return resourceProvider->refNonAAQuadIndexBuffer();
380 case IndexBufferOption::kTriStrips: // fall through
381 default: return nullptr;
382 }
383}
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400384
Robert Phillips438d9862019-11-14 12:46:05 -0500385int QuadLimit(IndexBufferOption option) {
386 switch (option) {
387 case IndexBufferOption::kPictureFramed: return GrResourceProvider::MaxNumAAQuads();
388 case IndexBufferOption::kIndexedRects: return GrResourceProvider::MaxNumNonAAQuads();
389 case IndexBufferOption::kTriStrips: return SK_MaxS32; // not limited by an indexBuffer
390 }
391
392 SkUNREACHABLE;
393}
Michael Ludwig93aeba02018-12-21 09:50:31 -0500394
Robert Phillips2f05a482019-11-25 09:54:43 -0500395void ConfigureMesh(const GrCaps& caps, GrMesh* mesh, const VertexSpec& spec,
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400396 int runningQuadCount, int quadsInDraw, int maxVerts,
397 sk_sp<const GrBuffer> vertexBuffer,
398 sk_sp<const GrBuffer> indexBuffer, int absVertBufferOffset) {
399 SkASSERT(vertexBuffer);
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400400
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400401 if (spec.indexBufferOption() == IndexBufferOption::kTriStrips) {
402 SkASSERT(!indexBuffer);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500403
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400404 mesh->setNonIndexedNonInstanced(4);
405 int offset = absVertBufferOffset +
406 runningQuadCount * GrResourceProvider::NumVertsPerNonAAQuad();
407 mesh->setVertexData(std::move(vertexBuffer), offset);
408 return;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500409 }
410
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400411 SkASSERT(spec.indexBufferOption() == IndexBufferOption::kPictureFramed ||
412 spec.indexBufferOption() == IndexBufferOption::kIndexedRects);
413 SkASSERT(indexBuffer);
414
Robert Phillips2f05a482019-11-25 09:54:43 -0500415 int maxNumQuads, numIndicesPerQuad, numVertsPerQuad;
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400416
417 if (spec.indexBufferOption() == IndexBufferOption::kPictureFramed) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400418 // AA uses 8 vertices and 30 indices per quad, basically nested rectangles
Robert Phillips2f05a482019-11-25 09:54:43 -0500419 maxNumQuads = GrResourceProvider::MaxNumAAQuads();
420 numIndicesPerQuad = GrResourceProvider::NumIndicesPerAAQuad();
421 numVertsPerQuad = GrResourceProvider::NumVertsPerAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400422 } else {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400423 // Non-AA uses 4 vertices and 6 indices per quad
Robert Phillips2f05a482019-11-25 09:54:43 -0500424 maxNumQuads = GrResourceProvider::MaxNumNonAAQuads();
425 numIndicesPerQuad = GrResourceProvider::NumIndicesPerNonAAQuad();
426 numVertsPerQuad = GrResourceProvider::NumVertsPerNonAAQuad();
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400427 }
428
Robert Phillips2f05a482019-11-25 09:54:43 -0500429 SkASSERT(runningQuadCount + quadsInDraw <= maxNumQuads);
430
431 if (caps.avoidLargeIndexBufferDraws()) {
432 // When we need to avoid large index buffer draws we modify the base vertex of the draw
433 // which, in GL, requires rebinding all vertex attrib arrays, so a base index is generally
434 // preferred.
435 int offset = absVertBufferOffset + runningQuadCount * numVertsPerQuad;
436
437 mesh->setIndexedPatterned(std::move(indexBuffer), numIndicesPerQuad,
438 numVertsPerQuad, quadsInDraw, maxNumQuads);
439 mesh->setVertexData(std::move(vertexBuffer), offset);
440 } else {
441 int baseIndex = runningQuadCount * numIndicesPerQuad;
442 int numIndicesToDraw = quadsInDraw * numIndicesPerQuad;
443
444 int minVertex = runningQuadCount * numVertsPerQuad;
445 int maxVertex = (runningQuadCount + quadsInDraw) * numVertsPerQuad;
446
447 mesh->setIndexed(std::move(indexBuffer), numIndicesToDraw,
448 baseIndex, minVertex, maxVertex, GrPrimitiveRestart::kNo);
449 mesh->setVertexData(std::move(vertexBuffer), absVertBufferOffset);
450 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500451}
452
Michael Ludwigc182b942018-11-16 10:27:51 -0500453////////////////// VertexSpec Implementation
Michael Ludwig20e909e2018-10-30 10:43:57 -0400454
Michael Ludwigc182b942018-11-16 10:27:51 -0500455int VertexSpec::deviceDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400456 return this->deviceQuadType() == GrQuad::Type::kPerspective ? 3 : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -0500457}
458
459int VertexSpec::localDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400460 return fHasLocalCoords ? (this->localQuadType() == GrQuad::Type::kPerspective ? 3 : 2) : 0;
Michael Ludwigc182b942018-11-16 10:27:51 -0500461}
462
Robert Phillips29f38542019-10-16 09:20:25 -0400463CoverageMode VertexSpec::coverageMode() const {
464 if (this->usesCoverageAA()) {
465 if (this->compatibleWithCoverageAsAlpha() && this->hasVertexColors() &&
466 !this->requiresGeometryDomain()) {
467 // Using a geometric domain acts as a second source of coverage and folding
468 // the original coverage into color makes it impossible to apply the color's
469 // alpha to the geometric domain's coverage when the original shape is clipped.
470 return CoverageMode::kWithColor;
471 } else {
472 return CoverageMode::kWithPosition;
473 }
474 } else {
475 return CoverageMode::kNone;
476 }
477}
478
479// This needs to stay in sync w/ QuadPerEdgeAAGeometryProcessor::initializeAttrs
480size_t VertexSpec::vertexSize() const {
481 bool needsPerspective = (this->deviceDimensionality() == 3);
482 CoverageMode coverageMode = this->coverageMode();
483
484 size_t count = 0;
485
486 if (coverageMode == CoverageMode::kWithPosition) {
487 if (needsPerspective) {
488 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
489 } else {
490 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType) +
491 GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
492 }
493 } else {
494 if (needsPerspective) {
495 count += GrVertexAttribTypeSize(kFloat3_GrVertexAttribType);
496 } else {
497 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType);
498 }
499 }
500
501 if (this->requiresGeometryDomain()) {
502 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
503 }
504
505 count += this->localDimensionality() * GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
506
507 if (ColorType::kByte == this->colorType()) {
508 count += GrVertexAttribTypeSize(kUByte4_norm_GrVertexAttribType);
Brian Osman2715bf52019-12-06 14:38:47 -0500509 } else if (ColorType::kFloat == this->colorType()) {
510 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
Robert Phillips29f38542019-10-16 09:20:25 -0400511 }
512
513 if (this->hasDomain()) {
514 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
515 }
516
517 return count;
518}
519
Michael Ludwig467994d2018-12-03 14:58:31 +0000520////////////////// Geometry Processor Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500521
Michael Ludwig467994d2018-12-03 14:58:31 +0000522class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor {
523public:
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400524 using Saturate = GrTextureOp::Saturate;
Michael Ludwig20e909e2018-10-30 10:43:57 -0400525
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500526 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const VertexSpec& spec) {
527 return arena->make<QuadPerEdgeAAGeometryProcessor>(spec);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400528 }
529
Brian Salomonccb61422020-01-09 10:46:36 -0500530 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
531 const VertexSpec& vertexSpec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500532 const GrShaderCaps& caps,
533 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500534 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500535 const GrSwizzle& swizzle,
536 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
537 Saturate saturate) {
538 return arena->make<QuadPerEdgeAAGeometryProcessor>(
Robert Phillips323471e2019-11-11 11:33:37 -0500539 vertexSpec, caps, backendFormat, samplerState, swizzle,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500540 std::move(textureColorSpaceXform), saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400541 }
542
Michael Ludwig467994d2018-12-03 14:58:31 +0000543 const char* name() const override { return "QuadPerEdgeAAGeometryProcessor"; }
Michael Ludwig024e2622018-11-30 13:25:55 -0500544
Michael Ludwig467994d2018-12-03 14:58:31 +0000545 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400546 // texturing, device-dimensions are single bit flags
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400547 uint32_t x = (fTexDomain.isInitialized() ? 0 : 0x1)
548 | (fSampler.isInitialized() ? 0 : 0x2)
549 | (fNeedsPerspective ? 0 : 0x4)
550 | (fSaturate == Saturate::kNo ? 0 : 0x8);
Michael Ludwig467994d2018-12-03 14:58:31 +0000551 // local coords require 2 bits (3 choices), 00 for none, 01 for 2d, 10 for 3d
552 if (fLocalCoord.isInitialized()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400553 x |= kFloat3_GrVertexAttribType == fLocalCoord.cpuType() ? 0x10 : 0x20;
Brian Osman78dc72c2018-12-03 13:20:43 +0000554 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000555 // similar for colors, 00 for none, 01 for bytes, 10 for half-floats
Michael Ludwig93aeba02018-12-21 09:50:31 -0500556 if (fColor.isInitialized()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400557 x |= kUByte4_norm_GrVertexAttribType == fColor.cpuType() ? 0x40 : 0x80;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500558 }
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400559 // and coverage mode, 00 for none, 01 for withposition, 10 for withcolor, 11 for
560 // position+geomdomain
561 SkASSERT(!fGeomDomain.isInitialized() || fCoverageMode == CoverageMode::kWithPosition);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500562 if (fCoverageMode != CoverageMode::kNone) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400563 x |= fGeomDomain.isInitialized()
564 ? 0x300
565 : (CoverageMode::kWithPosition == fCoverageMode ? 0x100 : 0x200);
Michael Ludwig467994d2018-12-03 14:58:31 +0000566 }
567
568 b->add32(GrColorSpaceXform::XformKey(fTextureColorSpaceXform.get()));
569 b->add32(x);
Brian Osman78dc72c2018-12-03 13:20:43 +0000570 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000571
572 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
573 class GLSLProcessor : public GrGLSLGeometryProcessor {
574 public:
575 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
Brian Salomonc241b582019-11-27 08:57:17 -0500576 const CoordTransformRange& transformRange) override {
Michael Ludwig467994d2018-12-03 14:58:31 +0000577 const auto& gp = proc.cast<QuadPerEdgeAAGeometryProcessor>();
578 if (gp.fLocalCoord.isInitialized()) {
Brian Salomonc241b582019-11-27 08:57:17 -0500579 this->setTransformDataHelper(SkMatrix::I(), pdman, transformRange);
Michael Ludwig467994d2018-12-03 14:58:31 +0000580 }
581 fTextureColorSpaceXformHelper.setData(pdman, gp.fTextureColorSpaceXform.get());
582 }
583
584 private:
585 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
586 using Interpolation = GrGLSLVaryingHandler::Interpolation;
587
588 const auto& gp = args.fGP.cast<QuadPerEdgeAAGeometryProcessor>();
589 fTextureColorSpaceXformHelper.emitCode(args.fUniformHandler,
590 gp.fTextureColorSpaceXform.get());
591
592 args.fVaryingHandler->emitAttributes(gp);
593
Michael Ludwig93aeba02018-12-21 09:50:31 -0500594 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
595 // Strip last channel from the vertex attribute to remove coverage and get the
596 // actual position
597 if (gp.fNeedsPerspective) {
598 args.fVertBuilder->codeAppendf("float3 position = %s.xyz;",
599 gp.fPosition.name());
600 } else {
601 args.fVertBuilder->codeAppendf("float2 position = %s.xy;",
602 gp.fPosition.name());
603 }
604 gpArgs->fPositionVar = {"position",
605 gp.fNeedsPerspective ? kFloat3_GrSLType
606 : kFloat2_GrSLType,
607 GrShaderVar::kNone_TypeModifier};
Michael Ludwig467994d2018-12-03 14:58:31 +0000608 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500609 // No coverage to eliminate
610 gpArgs->fPositionVar = gp.fPosition.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000611 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000612
613 // Handle local coordinates if they exist
614 if (gp.fLocalCoord.isInitialized()) {
615 // NOTE: If the only usage of local coordinates is for the inline texture fetch
616 // before FPs, then there are no registered FPCoordTransforms and this ends up
617 // emitting nothing, so there isn't a duplication of local coordinates
618 this->emitTransforms(args.fVertBuilder,
619 args.fVaryingHandler,
620 args.fUniformHandler,
621 gp.fLocalCoord.asShaderVar(),
622 args.fFPCoordTransformHandler);
623 }
624
625 // Solid color before any texturing gets modulated in
626 if (gp.fColor.isInitialized()) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400627 SkASSERT(gp.fCoverageMode != CoverageMode::kWithColor || !gp.fNeedsPerspective);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500628 // The color cannot be flat if the varying coverage has been modulated into it
Michael Ludwig467994d2018-12-03 14:58:31 +0000629 args.fVaryingHandler->addPassThroughAttribute(gp.fColor, args.fOutputColor,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500630 gp.fCoverageMode == CoverageMode::kWithColor ?
631 Interpolation::kInterpolated : Interpolation::kCanBeFlat);
632 } else {
633 // Output color must be initialized to something
634 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
Michael Ludwig467994d2018-12-03 14:58:31 +0000635 }
636
637 // If there is a texture, must also handle texture coordinates and reading from
638 // the texture in the fragment shader before continuing to fragment processors.
639 if (gp.fSampler.isInitialized()) {
640 // Texture coordinates clamped by the domain on the fragment shader; if the GP
641 // has a texture, it's guaranteed to have local coordinates
642 args.fFragBuilder->codeAppend("float2 texCoord;");
643 if (gp.fLocalCoord.cpuType() == kFloat3_GrVertexAttribType) {
644 // Can't do a pass through since we need to perform perspective division
645 GrGLSLVarying v(gp.fLocalCoord.gpuType());
646 args.fVaryingHandler->addVarying(gp.fLocalCoord.name(), &v);
647 args.fVertBuilder->codeAppendf("%s = %s;",
648 v.vsOut(), gp.fLocalCoord.name());
649 args.fFragBuilder->codeAppendf("texCoord = %s.xy / %s.z;",
650 v.fsIn(), v.fsIn());
651 } else {
652 args.fVaryingHandler->addPassThroughAttribute(gp.fLocalCoord, "texCoord");
653 }
654
655 // Clamp the now 2D localCoordName variable by the domain if it is provided
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400656 if (gp.fTexDomain.isInitialized()) {
Michael Ludwig467994d2018-12-03 14:58:31 +0000657 args.fFragBuilder->codeAppend("float4 domain;");
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400658 args.fVaryingHandler->addPassThroughAttribute(gp.fTexDomain, "domain",
Michael Ludwig467994d2018-12-03 14:58:31 +0000659 Interpolation::kCanBeFlat);
660 args.fFragBuilder->codeAppend(
661 "texCoord = clamp(texCoord, domain.xy, domain.zw);");
662 }
663
664 // Now modulate the starting output color by the texture lookup
665 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
Brian Salomon87e9ddb2019-12-19 14:50:22 -0500666 args.fFragBuilder->appendTextureLookupAndBlend(
667 args.fOutputColor, SkBlendMode::kModulate, args.fTexSamplers[0],
Brian Salomond19cd762020-01-06 13:16:31 -0500668 "texCoord", &fTextureColorSpaceXformHelper);
Michael Ludwig467994d2018-12-03 14:58:31 +0000669 args.fFragBuilder->codeAppend(";");
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400670 if (gp.fSaturate == Saturate::kYes) {
671 args.fFragBuilder->codeAppendf("%s = saturate(%s);",
672 args.fOutputColor, args.fOutputColor);
673 }
674 } else {
675 // Saturate is only intended for use with a proxy to account for the fact
676 // that GrTextureOp skips SkPaint conversion, which normally handles this.
677 SkASSERT(gp.fSaturate == Saturate::kNo);
Michael Ludwig467994d2018-12-03 14:58:31 +0000678 }
679
680 // And lastly, output the coverage calculation code
Michael Ludwig93aeba02018-12-21 09:50:31 -0500681 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
682 GrGLSLVarying coverage(kFloat_GrSLType);
683 args.fVaryingHandler->addVarying("coverage", &coverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000684 if (gp.fNeedsPerspective) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400685 // Multiply by "W" in the vertex shader, then by 1/w (sk_FragCoord.w) in
686 // the fragment shader to get screen-space linear coverage.
687 args.fVertBuilder->codeAppendf("%s = %s.w * %s.z;",
688 coverage.vsOut(), gp.fPosition.name(),
689 gp.fPosition.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400690 args.fFragBuilder->codeAppendf("float coverage = %s * sk_FragCoord.w;",
691 coverage.fsIn());
Michael Ludwig93aeba02018-12-21 09:50:31 -0500692 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400693 args.fVertBuilder->codeAppendf("%s = %s;",
694 coverage.vsOut(), gp.fCoverage.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400695 args.fFragBuilder->codeAppendf("float coverage = %s;", coverage.fsIn());
Michael Ludwig467994d2018-12-03 14:58:31 +0000696 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500697
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400698 if (gp.fGeomDomain.isInitialized()) {
699 // Calculate distance from sk_FragCoord to the 4 edges of the domain
700 // and clamp them to (0, 1). Use the minimum of these and the original
701 // coverage. This only has to be done in the exterior triangles, the
702 // interior of the quad geometry can never be clipped by the domain box.
703 args.fFragBuilder->codeAppend("float4 geoDomain;");
704 args.fVaryingHandler->addPassThroughAttribute(gp.fGeomDomain, "geoDomain",
705 Interpolation::kCanBeFlat);
706 args.fFragBuilder->codeAppend(
707 "if (coverage < 0.5) {"
708 " float4 dists4 = clamp(float4(1, 1, -1, -1) * "
709 "(sk_FragCoord.xyxy - geoDomain), 0, 1);"
710 " float2 dists2 = dists4.xy * dists4.zw;"
711 " coverage = min(coverage, dists2.x * dists2.y);"
712 "}");
713 }
714
715 args.fFragBuilder->codeAppendf("%s = half4(half(coverage));",
716 args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000717 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500718 // Set coverage to 1, since it's either non-AA or the coverage was already
719 // folded into the output color
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400720 SkASSERT(!gp.fGeomDomain.isInitialized());
Ethan Nicholase1f55022019-02-05 17:17:40 -0500721 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000722 }
723 }
724 GrGLSLColorSpaceXformHelper fTextureColorSpaceXformHelper;
725 };
726 return new GLSLProcessor;
727 }
728
729private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500730 friend class ::SkArenaAlloc; // for access to ctor
731
Michael Ludwig467994d2018-12-03 14:58:31 +0000732 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec)
733 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
734 , fTextureColorSpaceXform(nullptr) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500735 SkASSERT(!spec.hasDomain());
Michael Ludwig467994d2018-12-03 14:58:31 +0000736 this->initializeAttrs(spec);
737 this->setTextureSamplerCnt(0);
738 }
739
Brian Salomon67529b22019-08-13 15:31:04 -0400740 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec,
741 const GrShaderCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400742 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500743 GrSamplerState samplerState,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400744 const GrSwizzle& swizzle,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400745 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
746 Saturate saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000747 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400748 , fSaturate(saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000749 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Robert Phillips323471e2019-11-11 11:33:37 -0500750 , fSampler(samplerState, backendFormat, swizzle) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500751 SkASSERT(spec.hasLocalCoords());
Michael Ludwig467994d2018-12-03 14:58:31 +0000752 this->initializeAttrs(spec);
753 this->setTextureSamplerCnt(1);
754 }
755
Robert Phillips29f38542019-10-16 09:20:25 -0400756 // This needs to stay in sync w/ VertexSpec::vertexSize
Michael Ludwig467994d2018-12-03 14:58:31 +0000757 void initializeAttrs(const VertexSpec& spec) {
758 fNeedsPerspective = spec.deviceDimensionality() == 3;
Robert Phillips29f38542019-10-16 09:20:25 -0400759 fCoverageMode = spec.coverageMode();
Michael Ludwig93aeba02018-12-21 09:50:31 -0500760
761 if (fCoverageMode == CoverageMode::kWithPosition) {
762 if (fNeedsPerspective) {
763 fPosition = {"positionWithCoverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
764 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400765 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
766 fCoverage = {"coverage", kFloat_GrVertexAttribType, kFloat_GrSLType};
Michael Ludwig93aeba02018-12-21 09:50:31 -0500767 }
768 } else {
769 if (fNeedsPerspective) {
770 fPosition = {"position", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
771 } else {
772 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
773 }
774 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000775
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400776 // Need a geometry domain when the quads are AA and not rectilinear, since their AA
777 // outsetting can go beyond a half pixel.
778 if (spec.requiresGeometryDomain()) {
779 fGeomDomain = {"geomDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
780 }
781
Michael Ludwig467994d2018-12-03 14:58:31 +0000782 int localDim = spec.localDimensionality();
783 if (localDim == 3) {
784 fLocalCoord = {"localCoord", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
785 } else if (localDim == 2) {
786 fLocalCoord = {"localCoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
787 } // else localDim == 0 and attribute remains uninitialized
788
Brian Osman2715bf52019-12-06 14:38:47 -0500789 if (spec.hasVertexColors()) {
790 fColor = MakeColorAttribute("color", ColorType::kFloat == spec.colorType());
Michael Ludwig467994d2018-12-03 14:58:31 +0000791 }
792
793 if (spec.hasDomain()) {
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400794 fTexDomain = {"texDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwig467994d2018-12-03 14:58:31 +0000795 }
796
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400797 this->setVertexAttributes(&fPosition, 6);
Michael Ludwig467994d2018-12-03 14:58:31 +0000798 }
799
800 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
801
Michael Ludwig93aeba02018-12-21 09:50:31 -0500802 Attribute fPosition; // May contain coverage as last channel
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400803 Attribute fCoverage; // Used for non-perspective position to avoid Intel Metal issues
Michael Ludwig93aeba02018-12-21 09:50:31 -0500804 Attribute fColor; // May have coverage modulated in if the FPs support it
Michael Ludwig467994d2018-12-03 14:58:31 +0000805 Attribute fLocalCoord;
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400806 Attribute fGeomDomain; // Screen-space bounding box on geometry+aa outset
807 Attribute fTexDomain; // Texture-space bounding box on local coords
Michael Ludwig467994d2018-12-03 14:58:31 +0000808
Michael Ludwig93aeba02018-12-21 09:50:31 -0500809 // The positions attribute may have coverage built into it, so float3 is an ambiguous type
810 // and may mean 2d with coverage, or 3d with no coverage
Michael Ludwig467994d2018-12-03 14:58:31 +0000811 bool fNeedsPerspective;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400812 // Should saturate() be called on the color? Only relevant when created with a texture.
813 Saturate fSaturate = Saturate::kNo;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500814 CoverageMode fCoverageMode;
Michael Ludwig467994d2018-12-03 14:58:31 +0000815
816 // Color space will be null and fSampler.isInitialized() returns false when the GP is configured
817 // to skip texturing.
818 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
819 TextureSampler fSampler;
820
821 typedef GrGeometryProcessor INHERITED;
822};
823
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500824GrGeometryProcessor* MakeProcessor(SkArenaAlloc* arena, const VertexSpec& spec) {
825 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec);
Michael Ludwig467994d2018-12-03 14:58:31 +0000826}
827
Brian Salomonccb61422020-01-09 10:46:36 -0500828GrGeometryProcessor* MakeTexturedProcessor(SkArenaAlloc* arena,
829 const VertexSpec& spec,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500830 const GrShaderCaps& caps,
831 const GrBackendFormat& backendFormat,
Brian Salomonccb61422020-01-09 10:46:36 -0500832 GrSamplerState samplerState,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500833 const GrSwizzle& swizzle,
834 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
Brian Salomonccb61422020-01-09 10:46:36 -0500835 Saturate saturate) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500836 return QuadPerEdgeAAGeometryProcessor::Make(arena, spec, caps, backendFormat, samplerState,
837 swizzle, std::move(textureColorSpaceXform),
838 saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400839}
Michael Ludwigc182b942018-11-16 10:27:51 -0500840
841} // namespace GrQuadPerEdgeAA