blob: b9b2d65d7a53066448e875658e42bd11fa44fd77 [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/GrVertexWriter.h"
12#include "src/gpu/SkGr.h"
Michael Ludwigfb7ba522019-10-29 15:33:34 -040013#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
15#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
16#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
17#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
18#include "src/gpu/glsl/GrGLSLVarying.h"
19#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Michael Ludwig460eb5e2018-10-29 11:09:29 -040020
Michael Ludwigf995c052018-11-26 15:24:29 -050021
Michael Ludwig460eb5e2018-10-29 11:09:29 -040022namespace {
23
Michael Ludwig93aeba02018-12-21 09:50:31 -050024// Writes four vertices in triangle strip order, including the additional data for local
Michael Ludwigdcfbe322019-04-01 14:55:54 -040025// coordinates, geometry + texture domains, color, and coverage as needed to satisfy the vertex spec
Michael Ludwig93aeba02018-12-21 09:50:31 -050026static void write_quad(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec,
Michael Ludwigfb7ba522019-10-29 15:33:34 -040027 GrQuadPerEdgeAA::CoverageMode mode, const skvx::Vec<4, float>& coverage,
28 SkPMColor4f color4f, const SkRect& geomDomain, const SkRect& texDomain,
29 const GrQuad& deviceQuad, const GrQuad& localQuad) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050030 static constexpr auto If = GrVertexWriter::If<float>;
31
Michael Ludwig553e9a92018-11-29 12:38:35 -050032 for (int i = 0; i < 4; ++i) {
Michael Ludwig93aeba02018-12-21 09:50:31 -050033 // save position, this is a float2 or float3 or float4 depending on the combination of
34 // perspective and coverage mode.
Michael Ludwigfb7ba522019-10-29 15:33:34 -040035 vb->write(deviceQuad.x(i), deviceQuad.y(i),
36 If(spec.deviceQuadType() == GrQuad::Type::kPerspective, deviceQuad.w(i)),
Robert Phillips29f38542019-10-16 09:20:25 -040037 If(mode == GrQuadPerEdgeAA::CoverageMode::kWithPosition, coverage[i]));
Michael Ludwig4921dc32018-12-03 14:57:29 +000038
Michael Ludwig93aeba02018-12-21 09:50:31 -050039 // save color
40 if (spec.hasVertexColors()) {
Brian Salomon1d835422019-03-13 16:11:44 -040041 bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kHalf;
Michael Ludwige6266a22019-03-07 11:24:32 -050042 vb->write(GrVertexColor(
Robert Phillips29f38542019-10-16 09:20:25 -040043 color4f * (mode == GrQuadPerEdgeAA::CoverageMode::kWithColor ? coverage[i] : 1.f),
44 wide));
Michael Ludwig93aeba02018-12-21 09:50:31 -050045 }
46
47 // save local position
48 if (spec.hasLocalCoords()) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -040049 vb->write(localQuad.x(i), localQuad.y(i),
50 If(spec.localQuadType() == GrQuad::Type::kPerspective, localQuad.w(i)));
Michael Ludwig93aeba02018-12-21 09:50:31 -050051 }
52
Michael Ludwigdcfbe322019-04-01 14:55:54 -040053 // save the geometry domain
54 if (spec.requiresGeometryDomain()) {
55 vb->write(geomDomain);
56 }
57
58 // save the texture domain
Michael Ludwig93aeba02018-12-21 09:50:31 -050059 if (spec.hasDomain()) {
Michael Ludwigdcfbe322019-04-01 14:55:54 -040060 vb->write(texDomain);
Michael Ludwig93aeba02018-12-21 09:50:31 -050061 }
62 }
63}
64
Michael Ludwig460eb5e2018-10-29 11:09:29 -040065} // anonymous namespace
66
Michael Ludwigc182b942018-11-16 10:27:51 -050067namespace GrQuadPerEdgeAA {
68
Robert Phillipsc554dcf2019-10-28 11:43:55 -040069IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads) {
70 if (aa == GrAAType::kCoverage) {
71 return IndexBufferOption::kPictureFramed;
72 } else if (numQuads > 1) {
73 return IndexBufferOption::kIndexedRects;
74 } else {
75 return IndexBufferOption::kTriStrips;
76 }
77}
78
Brian Osman8fa7ab42019-03-18 10:22:42 -040079// This is a more elaborate version of SkPMColor4fNeedsWideColor that allows "no color" for white
80ColorType MinColorType(SkPMColor4f color, GrClampType clampType, const GrCaps& caps) {
Brian Salomon1d835422019-03-13 16:11:44 -040081 if (color == SK_PMColor4fWHITE) {
82 return ColorType::kNone;
Brian Salomon1d835422019-03-13 16:11:44 -040083 } else {
Brian Osman8fa7ab42019-03-18 10:22:42 -040084 return SkPMColor4fNeedsWideColor(color, clampType, caps) ? ColorType::kHalf
85 : ColorType::kByte;
Brian Salomon1d835422019-03-13 16:11:44 -040086 }
87}
88
Michael Ludwigc182b942018-11-16 10:27:51 -050089////////////////// Tessellate Implementation
90
Michael Ludwigde4c58c2019-06-04 09:12:59 -040091void* Tessellate(void* vertices, const VertexSpec& spec, const GrQuad& deviceQuad,
92 const SkPMColor4f& color4f, const GrQuad& localQuad, const SkRect& domain,
Michael Ludwigc182b942018-11-16 10:27:51 -050093 GrQuadAAFlags aaFlags) {
Michael Ludwig41f395d2019-05-23 13:59:45 -040094 SkASSERT(deviceQuad.quadType() <= spec.deviceQuadType());
95 SkASSERT(!spec.hasLocalCoords() || localQuad.quadType() <= spec.localQuadType());
96
Robert Phillips29f38542019-10-16 09:20:25 -040097 GrQuadPerEdgeAA::CoverageMode mode = spec.coverageMode();
Michael Ludwig460eb5e2018-10-29 11:09:29 -040098
Michael Ludwigc182b942018-11-16 10:27:51 -050099 GrVertexWriter vb{vertices};
Michael Ludwig93aeba02018-12-21 09:50:31 -0500100 if (spec.usesCoverageAA()) {
101 SkASSERT(mode == CoverageMode::kWithPosition || mode == CoverageMode::kWithColor);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400102 // Must calculate inner and outer quadrilaterals for the vertex coverage ramps, and possibly
103 // a geometry domain
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400104 SkRect geomDomain;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400105 if (spec.requiresGeometryDomain()) {
106 geomDomain = deviceQuad.bounds();
107 geomDomain.outset(0.5f, 0.5f); // account for AA expansion
Michael Ludwige6266a22019-03-07 11:24:32 -0500108 }
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400109
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400110 // TODO(michaelludwig) - Update TessellateHelper to select processing functions based on the
111 // vertexspec once per op, and then burn through all quads with the selected function ptr.
112 GrQuadUtils::TessellationHelper helper(deviceQuad,
113 spec.hasLocalCoords() ? &localQuad : nullptr);
114
115 // Write inner vertices first
116 GrQuad aaDeviceQuad, aaLocalQuad;
117 helper.inset(aaFlags, &aaDeviceQuad, &aaLocalQuad);
118 write_quad(&vb, spec, mode, helper.pixelCoverage(), color4f, geomDomain, domain,
119 aaDeviceQuad, aaLocalQuad);
120
121 // Then outer vertices, which use 0.f for their coverage
122 helper.outset(aaFlags, &aaDeviceQuad, &aaLocalQuad);
123 write_quad(&vb, spec, mode, 0.f, color4f, geomDomain, domain, aaDeviceQuad, aaLocalQuad);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500124 } else {
125 // No outsetting needed, just write a single quad with full coverage
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400126 SkASSERT(mode == CoverageMode::kNone && !spec.requiresGeometryDomain());
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400127 write_quad(&vb, spec, mode, 1.f, color4f, SkRect::MakeEmpty(), domain,
128 deviceQuad, localQuad);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400129 }
Michael Ludwigc182b942018-11-16 10:27:51 -0500130
131 return vb.fPtr;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400132}
Michael Ludwig20e909e2018-10-30 10:43:57 -0400133
Michael Ludwig93aeba02018-12-21 09:50:31 -0500134bool ConfigureMeshIndices(GrMeshDrawOp::Target* target, GrMesh* mesh, const VertexSpec& spec,
135 int quadCount) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400136 auto resourceProvider = target->resourceProvider();
137
Michael Ludwig93aeba02018-12-21 09:50:31 -0500138 if (spec.usesCoverageAA()) {
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400139 SkASSERT(spec.indexBufferOption() == IndexBufferOption::kPictureFramed);
140
Michael Ludwig93aeba02018-12-21 09:50:31 -0500141 // AA quads use 8 vertices, basically nested rectangles
Robert Phillipsee08d522019-10-28 16:34:44 -0400142 sk_sp<const GrGpuBuffer> ibuffer = resourceProvider->refAAQuadIndexBuffer();
Michael Ludwig93aeba02018-12-21 09:50:31 -0500143 if (!ibuffer) {
144 return false;
145 }
146
147 mesh->setPrimitiveType(GrPrimitiveType::kTriangles);
Robert Phillipsee08d522019-10-28 16:34:44 -0400148 mesh->setIndexedPatterned(std::move(ibuffer),
149 GrResourceProvider::NumIndicesPerAAQuad(),
150 GrResourceProvider::NumVertsPerAAQuad(),
151 quadCount,
152 GrResourceProvider::MaxNumAAQuads());
Michael Ludwig93aeba02018-12-21 09:50:31 -0500153 } else {
154 // Non-AA quads use 4 vertices, and regular triangle strip layout
155 if (quadCount > 1) {
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400156 SkASSERT(spec.indexBufferOption() == IndexBufferOption::kIndexedRects);
157
Robert Phillipsee08d522019-10-28 16:34:44 -0400158 sk_sp<const GrGpuBuffer> ibuffer = resourceProvider->refNonAAQuadIndexBuffer();
Michael Ludwig93aeba02018-12-21 09:50:31 -0500159 if (!ibuffer) {
160 return false;
161 }
162
163 mesh->setPrimitiveType(GrPrimitiveType::kTriangles);
Robert Phillipsee08d522019-10-28 16:34:44 -0400164 mesh->setIndexedPatterned(std::move(ibuffer),
165 GrResourceProvider::NumIndicesPerNonAAQuad(),
166 GrResourceProvider::NumVertsPerNonAAQuad(),
167 quadCount,
168 GrResourceProvider::MaxNumNonAAQuads());
Michael Ludwig93aeba02018-12-21 09:50:31 -0500169 } else {
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400170 SkASSERT(spec.indexBufferOption() != IndexBufferOption::kPictureFramed);
171
Michael Ludwig93aeba02018-12-21 09:50:31 -0500172 mesh->setPrimitiveType(GrPrimitiveType::kTriangleStrip);
173 mesh->setNonIndexedNonInstanced(4);
174 }
175 }
176
177 return true;
178}
179
Michael Ludwigc182b942018-11-16 10:27:51 -0500180////////////////// VertexSpec Implementation
Michael Ludwig20e909e2018-10-30 10:43:57 -0400181
Michael Ludwigc182b942018-11-16 10:27:51 -0500182int VertexSpec::deviceDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400183 return this->deviceQuadType() == GrQuad::Type::kPerspective ? 3 : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -0500184}
185
186int VertexSpec::localDimensionality() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400187 return fHasLocalCoords ? (this->localQuadType() == GrQuad::Type::kPerspective ? 3 : 2) : 0;
Michael Ludwigc182b942018-11-16 10:27:51 -0500188}
189
Robert Phillips29f38542019-10-16 09:20:25 -0400190CoverageMode VertexSpec::coverageMode() const {
191 if (this->usesCoverageAA()) {
192 if (this->compatibleWithCoverageAsAlpha() && this->hasVertexColors() &&
193 !this->requiresGeometryDomain()) {
194 // Using a geometric domain acts as a second source of coverage and folding
195 // the original coverage into color makes it impossible to apply the color's
196 // alpha to the geometric domain's coverage when the original shape is clipped.
197 return CoverageMode::kWithColor;
198 } else {
199 return CoverageMode::kWithPosition;
200 }
201 } else {
202 return CoverageMode::kNone;
203 }
204}
205
206// This needs to stay in sync w/ QuadPerEdgeAAGeometryProcessor::initializeAttrs
207size_t VertexSpec::vertexSize() const {
208 bool needsPerspective = (this->deviceDimensionality() == 3);
209 CoverageMode coverageMode = this->coverageMode();
210
211 size_t count = 0;
212
213 if (coverageMode == CoverageMode::kWithPosition) {
214 if (needsPerspective) {
215 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
216 } else {
217 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType) +
218 GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
219 }
220 } else {
221 if (needsPerspective) {
222 count += GrVertexAttribTypeSize(kFloat3_GrVertexAttribType);
223 } else {
224 count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType);
225 }
226 }
227
228 if (this->requiresGeometryDomain()) {
229 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
230 }
231
232 count += this->localDimensionality() * GrVertexAttribTypeSize(kFloat_GrVertexAttribType);
233
234 if (ColorType::kByte == this->colorType()) {
235 count += GrVertexAttribTypeSize(kUByte4_norm_GrVertexAttribType);
236 } else if (ColorType::kHalf == this->colorType()) {
237 count += GrVertexAttribTypeSize(kHalf4_GrVertexAttribType);
238 }
239
240 if (this->hasDomain()) {
241 count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType);
242 }
243
244 return count;
245}
246
Michael Ludwig467994d2018-12-03 14:58:31 +0000247////////////////// Geometry Processor Implementation
Michael Ludwigc182b942018-11-16 10:27:51 -0500248
Michael Ludwig467994d2018-12-03 14:58:31 +0000249class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor {
250public:
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400251 using Saturate = GrTextureOp::Saturate;
Michael Ludwig20e909e2018-10-30 10:43:57 -0400252
Michael Ludwig467994d2018-12-03 14:58:31 +0000253 static sk_sp<GrGeometryProcessor> Make(const VertexSpec& spec) {
254 return sk_sp<QuadPerEdgeAAGeometryProcessor>(new QuadPerEdgeAAGeometryProcessor(spec));
Michael Ludwig20e909e2018-10-30 10:43:57 -0400255 }
256
Michael Ludwig467994d2018-12-03 14:58:31 +0000257 static sk_sp<GrGeometryProcessor> Make(const VertexSpec& vertexSpec, const GrShaderCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400258 const GrBackendFormat& backendFormat,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500259 const GrSamplerState& samplerState,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400260 const GrSwizzle& swizzle, uint32_t extraSamplerKey,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400261 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
262 Saturate saturate) {
Michael Ludwig467994d2018-12-03 14:58:31 +0000263 return sk_sp<QuadPerEdgeAAGeometryProcessor>(new QuadPerEdgeAAGeometryProcessor(
Robert Phillipsf272bea2019-10-17 08:56:16 -0400264 vertexSpec, caps, backendFormat, samplerState, swizzle, extraSamplerKey,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400265 std::move(textureColorSpaceXform), saturate));
Michael Ludwig20e909e2018-10-30 10:43:57 -0400266 }
267
Michael Ludwig467994d2018-12-03 14:58:31 +0000268 const char* name() const override { return "QuadPerEdgeAAGeometryProcessor"; }
Michael Ludwig024e2622018-11-30 13:25:55 -0500269
Michael Ludwig467994d2018-12-03 14:58:31 +0000270 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400271 // texturing, device-dimensions are single bit flags
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400272 uint32_t x = (fTexDomain.isInitialized() ? 0 : 0x1)
273 | (fSampler.isInitialized() ? 0 : 0x2)
274 | (fNeedsPerspective ? 0 : 0x4)
275 | (fSaturate == Saturate::kNo ? 0 : 0x8);
Michael Ludwig467994d2018-12-03 14:58:31 +0000276 // local coords require 2 bits (3 choices), 00 for none, 01 for 2d, 10 for 3d
277 if (fLocalCoord.isInitialized()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400278 x |= kFloat3_GrVertexAttribType == fLocalCoord.cpuType() ? 0x10 : 0x20;
Brian Osman78dc72c2018-12-03 13:20:43 +0000279 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000280 // similar for colors, 00 for none, 01 for bytes, 10 for half-floats
Michael Ludwig93aeba02018-12-21 09:50:31 -0500281 if (fColor.isInitialized()) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400282 x |= kUByte4_norm_GrVertexAttribType == fColor.cpuType() ? 0x40 : 0x80;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500283 }
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400284 // and coverage mode, 00 for none, 01 for withposition, 10 for withcolor, 11 for
285 // position+geomdomain
286 SkASSERT(!fGeomDomain.isInitialized() || fCoverageMode == CoverageMode::kWithPosition);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500287 if (fCoverageMode != CoverageMode::kNone) {
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400288 x |= fGeomDomain.isInitialized()
289 ? 0x300
290 : (CoverageMode::kWithPosition == fCoverageMode ? 0x100 : 0x200);
Michael Ludwig467994d2018-12-03 14:58:31 +0000291 }
292
293 b->add32(GrColorSpaceXform::XformKey(fTextureColorSpaceXform.get()));
294 b->add32(x);
Brian Osman78dc72c2018-12-03 13:20:43 +0000295 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000296
297 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
298 class GLSLProcessor : public GrGLSLGeometryProcessor {
299 public:
300 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
301 FPCoordTransformIter&& transformIter) override {
302 const auto& gp = proc.cast<QuadPerEdgeAAGeometryProcessor>();
303 if (gp.fLocalCoord.isInitialized()) {
304 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
305 }
306 fTextureColorSpaceXformHelper.setData(pdman, gp.fTextureColorSpaceXform.get());
307 }
308
309 private:
310 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
311 using Interpolation = GrGLSLVaryingHandler::Interpolation;
312
313 const auto& gp = args.fGP.cast<QuadPerEdgeAAGeometryProcessor>();
314 fTextureColorSpaceXformHelper.emitCode(args.fUniformHandler,
315 gp.fTextureColorSpaceXform.get());
316
317 args.fVaryingHandler->emitAttributes(gp);
318
Michael Ludwig93aeba02018-12-21 09:50:31 -0500319 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
320 // Strip last channel from the vertex attribute to remove coverage and get the
321 // actual position
322 if (gp.fNeedsPerspective) {
323 args.fVertBuilder->codeAppendf("float3 position = %s.xyz;",
324 gp.fPosition.name());
325 } else {
326 args.fVertBuilder->codeAppendf("float2 position = %s.xy;",
327 gp.fPosition.name());
328 }
329 gpArgs->fPositionVar = {"position",
330 gp.fNeedsPerspective ? kFloat3_GrSLType
331 : kFloat2_GrSLType,
332 GrShaderVar::kNone_TypeModifier};
Michael Ludwig467994d2018-12-03 14:58:31 +0000333 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500334 // No coverage to eliminate
335 gpArgs->fPositionVar = gp.fPosition.asShaderVar();
Michael Ludwig467994d2018-12-03 14:58:31 +0000336 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000337
338 // Handle local coordinates if they exist
339 if (gp.fLocalCoord.isInitialized()) {
340 // NOTE: If the only usage of local coordinates is for the inline texture fetch
341 // before FPs, then there are no registered FPCoordTransforms and this ends up
342 // emitting nothing, so there isn't a duplication of local coordinates
343 this->emitTransforms(args.fVertBuilder,
344 args.fVaryingHandler,
345 args.fUniformHandler,
346 gp.fLocalCoord.asShaderVar(),
347 args.fFPCoordTransformHandler);
348 }
349
350 // Solid color before any texturing gets modulated in
351 if (gp.fColor.isInitialized()) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400352 SkASSERT(gp.fCoverageMode != CoverageMode::kWithColor || !gp.fNeedsPerspective);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500353 // The color cannot be flat if the varying coverage has been modulated into it
Michael Ludwig467994d2018-12-03 14:58:31 +0000354 args.fVaryingHandler->addPassThroughAttribute(gp.fColor, args.fOutputColor,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500355 gp.fCoverageMode == CoverageMode::kWithColor ?
356 Interpolation::kInterpolated : Interpolation::kCanBeFlat);
357 } else {
358 // Output color must be initialized to something
359 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor);
Michael Ludwig467994d2018-12-03 14:58:31 +0000360 }
361
362 // If there is a texture, must also handle texture coordinates and reading from
363 // the texture in the fragment shader before continuing to fragment processors.
364 if (gp.fSampler.isInitialized()) {
365 // Texture coordinates clamped by the domain on the fragment shader; if the GP
366 // has a texture, it's guaranteed to have local coordinates
367 args.fFragBuilder->codeAppend("float2 texCoord;");
368 if (gp.fLocalCoord.cpuType() == kFloat3_GrVertexAttribType) {
369 // Can't do a pass through since we need to perform perspective division
370 GrGLSLVarying v(gp.fLocalCoord.gpuType());
371 args.fVaryingHandler->addVarying(gp.fLocalCoord.name(), &v);
372 args.fVertBuilder->codeAppendf("%s = %s;",
373 v.vsOut(), gp.fLocalCoord.name());
374 args.fFragBuilder->codeAppendf("texCoord = %s.xy / %s.z;",
375 v.fsIn(), v.fsIn());
376 } else {
377 args.fVaryingHandler->addPassThroughAttribute(gp.fLocalCoord, "texCoord");
378 }
379
380 // Clamp the now 2D localCoordName variable by the domain if it is provided
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400381 if (gp.fTexDomain.isInitialized()) {
Michael Ludwig467994d2018-12-03 14:58:31 +0000382 args.fFragBuilder->codeAppend("float4 domain;");
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400383 args.fVaryingHandler->addPassThroughAttribute(gp.fTexDomain, "domain",
Michael Ludwig467994d2018-12-03 14:58:31 +0000384 Interpolation::kCanBeFlat);
385 args.fFragBuilder->codeAppend(
386 "texCoord = clamp(texCoord, domain.xy, domain.zw);");
387 }
388
389 // Now modulate the starting output color by the texture lookup
390 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
391 args.fFragBuilder->appendTextureLookupAndModulate(
392 args.fOutputColor, args.fTexSamplers[0], "texCoord", kFloat2_GrSLType,
393 &fTextureColorSpaceXformHelper);
394 args.fFragBuilder->codeAppend(";");
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400395 if (gp.fSaturate == Saturate::kYes) {
396 args.fFragBuilder->codeAppendf("%s = saturate(%s);",
397 args.fOutputColor, args.fOutputColor);
398 }
399 } else {
400 // Saturate is only intended for use with a proxy to account for the fact
401 // that GrTextureOp skips SkPaint conversion, which normally handles this.
402 SkASSERT(gp.fSaturate == Saturate::kNo);
Michael Ludwig467994d2018-12-03 14:58:31 +0000403 }
404
405 // And lastly, output the coverage calculation code
Michael Ludwig93aeba02018-12-21 09:50:31 -0500406 if (gp.fCoverageMode == CoverageMode::kWithPosition) {
407 GrGLSLVarying coverage(kFloat_GrSLType);
408 args.fVaryingHandler->addVarying("coverage", &coverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000409 if (gp.fNeedsPerspective) {
Michael Ludwig3d2753e2019-03-29 14:36:32 -0400410 // Multiply by "W" in the vertex shader, then by 1/w (sk_FragCoord.w) in
411 // the fragment shader to get screen-space linear coverage.
412 args.fVertBuilder->codeAppendf("%s = %s.w * %s.z;",
413 coverage.vsOut(), gp.fPosition.name(),
414 gp.fPosition.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400415 args.fFragBuilder->codeAppendf("float coverage = %s * sk_FragCoord.w;",
416 coverage.fsIn());
Michael Ludwig93aeba02018-12-21 09:50:31 -0500417 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400418 args.fVertBuilder->codeAppendf("%s = %s;",
419 coverage.vsOut(), gp.fCoverage.name());
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400420 args.fFragBuilder->codeAppendf("float coverage = %s;", coverage.fsIn());
Michael Ludwig467994d2018-12-03 14:58:31 +0000421 }
Michael Ludwig93aeba02018-12-21 09:50:31 -0500422
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400423 if (gp.fGeomDomain.isInitialized()) {
424 // Calculate distance from sk_FragCoord to the 4 edges of the domain
425 // and clamp them to (0, 1). Use the minimum of these and the original
426 // coverage. This only has to be done in the exterior triangles, the
427 // interior of the quad geometry can never be clipped by the domain box.
428 args.fFragBuilder->codeAppend("float4 geoDomain;");
429 args.fVaryingHandler->addPassThroughAttribute(gp.fGeomDomain, "geoDomain",
430 Interpolation::kCanBeFlat);
431 args.fFragBuilder->codeAppend(
432 "if (coverage < 0.5) {"
433 " float4 dists4 = clamp(float4(1, 1, -1, -1) * "
434 "(sk_FragCoord.xyxy - geoDomain), 0, 1);"
435 " float2 dists2 = dists4.xy * dists4.zw;"
436 " coverage = min(coverage, dists2.x * dists2.y);"
437 "}");
438 }
439
440 args.fFragBuilder->codeAppendf("%s = half4(half(coverage));",
441 args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000442 } else {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500443 // Set coverage to 1, since it's either non-AA or the coverage was already
444 // folded into the output color
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400445 SkASSERT(!gp.fGeomDomain.isInitialized());
Ethan Nicholase1f55022019-02-05 17:17:40 -0500446 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Michael Ludwig467994d2018-12-03 14:58:31 +0000447 }
448 }
449 GrGLSLColorSpaceXformHelper fTextureColorSpaceXformHelper;
450 };
451 return new GLSLProcessor;
452 }
453
454private:
455 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec)
456 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
457 , fTextureColorSpaceXform(nullptr) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500458 SkASSERT(!spec.hasDomain());
Michael Ludwig467994d2018-12-03 14:58:31 +0000459 this->initializeAttrs(spec);
460 this->setTextureSamplerCnt(0);
461 }
462
Brian Salomon67529b22019-08-13 15:31:04 -0400463 QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec,
464 const GrShaderCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400465 const GrBackendFormat& backendFormat,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500466 const GrSamplerState& samplerState,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400467 const GrSwizzle& swizzle,
Greg Daniel7a82edf2018-12-04 10:54:34 -0500468 uint32_t extraSamplerKey,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400469 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
470 Saturate saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000471 : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID)
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400472 , fSaturate(saturate)
Michael Ludwig467994d2018-12-03 14:58:31 +0000473 , fTextureColorSpaceXform(std::move(textureColorSpaceXform))
Robert Phillipsf272bea2019-10-17 08:56:16 -0400474 , fSampler(samplerState, backendFormat, swizzle, extraSamplerKey) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500475 SkASSERT(spec.hasLocalCoords());
Michael Ludwig467994d2018-12-03 14:58:31 +0000476 this->initializeAttrs(spec);
477 this->setTextureSamplerCnt(1);
478 }
479
Robert Phillips29f38542019-10-16 09:20:25 -0400480 // This needs to stay in sync w/ VertexSpec::vertexSize
Michael Ludwig467994d2018-12-03 14:58:31 +0000481 void initializeAttrs(const VertexSpec& spec) {
482 fNeedsPerspective = spec.deviceDimensionality() == 3;
Robert Phillips29f38542019-10-16 09:20:25 -0400483 fCoverageMode = spec.coverageMode();
Michael Ludwig93aeba02018-12-21 09:50:31 -0500484
485 if (fCoverageMode == CoverageMode::kWithPosition) {
486 if (fNeedsPerspective) {
487 fPosition = {"positionWithCoverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
488 } else {
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400489 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
490 fCoverage = {"coverage", kFloat_GrVertexAttribType, kFloat_GrSLType};
Michael Ludwig93aeba02018-12-21 09:50:31 -0500491 }
492 } else {
493 if (fNeedsPerspective) {
494 fPosition = {"position", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
495 } else {
496 fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
497 }
498 }
Michael Ludwig467994d2018-12-03 14:58:31 +0000499
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400500 // Need a geometry domain when the quads are AA and not rectilinear, since their AA
501 // outsetting can go beyond a half pixel.
502 if (spec.requiresGeometryDomain()) {
503 fGeomDomain = {"geomDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
504 }
505
Michael Ludwig467994d2018-12-03 14:58:31 +0000506 int localDim = spec.localDimensionality();
507 if (localDim == 3) {
508 fLocalCoord = {"localCoord", kFloat3_GrVertexAttribType, kFloat3_GrSLType};
509 } else if (localDim == 2) {
510 fLocalCoord = {"localCoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
511 } // else localDim == 0 and attribute remains uninitialized
512
513 if (ColorType::kByte == spec.colorType()) {
514 fColor = {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
515 } else if (ColorType::kHalf == spec.colorType()) {
516 fColor = {"color", kHalf4_GrVertexAttribType, kHalf4_GrSLType};
517 }
518
519 if (spec.hasDomain()) {
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400520 fTexDomain = {"texDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Michael Ludwig467994d2018-12-03 14:58:31 +0000521 }
522
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400523 this->setVertexAttributes(&fPosition, 6);
Michael Ludwig467994d2018-12-03 14:58:31 +0000524 }
525
526 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
527
Michael Ludwig93aeba02018-12-21 09:50:31 -0500528 Attribute fPosition; // May contain coverage as last channel
Jim Van Verthd5d9c212019-05-02 10:22:10 -0400529 Attribute fCoverage; // Used for non-perspective position to avoid Intel Metal issues
Michael Ludwig93aeba02018-12-21 09:50:31 -0500530 Attribute fColor; // May have coverage modulated in if the FPs support it
Michael Ludwig467994d2018-12-03 14:58:31 +0000531 Attribute fLocalCoord;
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400532 Attribute fGeomDomain; // Screen-space bounding box on geometry+aa outset
533 Attribute fTexDomain; // Texture-space bounding box on local coords
Michael Ludwig467994d2018-12-03 14:58:31 +0000534
Michael Ludwig93aeba02018-12-21 09:50:31 -0500535 // The positions attribute may have coverage built into it, so float3 is an ambiguous type
536 // and may mean 2d with coverage, or 3d with no coverage
Michael Ludwig467994d2018-12-03 14:58:31 +0000537 bool fNeedsPerspective;
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400538 // Should saturate() be called on the color? Only relevant when created with a texture.
539 Saturate fSaturate = Saturate::kNo;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500540 CoverageMode fCoverageMode;
Michael Ludwig467994d2018-12-03 14:58:31 +0000541
542 // Color space will be null and fSampler.isInitialized() returns false when the GP is configured
543 // to skip texturing.
544 sk_sp<GrColorSpaceXform> fTextureColorSpaceXform;
545 TextureSampler fSampler;
546
547 typedef GrGeometryProcessor INHERITED;
548};
549
550sk_sp<GrGeometryProcessor> MakeProcessor(const VertexSpec& spec) {
551 return QuadPerEdgeAAGeometryProcessor::Make(spec);
552}
553
554sk_sp<GrGeometryProcessor> MakeTexturedProcessor(const VertexSpec& spec, const GrShaderCaps& caps,
Robert Phillipsf272bea2019-10-17 08:56:16 -0400555 const GrBackendFormat& backendFormat,
Brian Salomon67529b22019-08-13 15:31:04 -0400556 const GrSamplerState& samplerState,
557 const GrSwizzle& swizzle, uint32_t extraSamplerKey,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400558 sk_sp<GrColorSpaceXform> textureColorSpaceXform,
559 Saturate saturate) {
Robert Phillipsf272bea2019-10-17 08:56:16 -0400560 return QuadPerEdgeAAGeometryProcessor::Make(spec, caps, backendFormat, samplerState, swizzle,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400561 extraSamplerKey, std::move(textureColorSpaceXform),
562 saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400563}
Michael Ludwigc182b942018-11-16 10:27:51 -0500564
565} // namespace GrQuadPerEdgeAA