blob: c02be5617b8df5f278af3a02b2a1c5345861a522 [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
8#ifndef GrQuadPerEdgeAA_DEFINED
9#define GrQuadPerEdgeAA_DEFINED
10
11#include "GrColor.h"
Brian Osman78dc72c2018-12-03 13:20:43 +000012#include "GrPrimitiveProcessor.h"
Michael Ludwigc182b942018-11-16 10:27:51 -050013#include "GrQuad.h"
Michael Ludwig460eb5e2018-10-29 11:09:29 -040014#include "GrSamplerState.h"
15#include "GrTypesPriv.h"
Brian Osman78dc72c2018-12-03 13:20:43 +000016#include "glsl/GrGLSLPrimitiveProcessor.h"
Michael Ludwig460eb5e2018-10-29 11:09:29 -040017#include "SkPoint.h"
18#include "SkPoint3.h"
19
Brian Osman78dc72c2018-12-03 13:20:43 +000020class GrGLSLColorSpaceXformHelper;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040021
Michael Ludwigc182b942018-11-16 10:27:51 -050022namespace GrQuadPerEdgeAA {
23
Michael Ludwig460eb5e2018-10-29 11:09:29 -040024 enum class Domain : bool { kNo = false, kYes = true };
Brian Osman3d139a42018-11-19 10:42:10 -050025 enum class ColorType { kNone, kByte, kHalf, kLast = kHalf };
26 static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040027
Michael Ludwigc182b942018-11-16 10:27:51 -050028 // Specifies the vertex configuration for an op that renders per-edge AA quads. The vertex
29 // order (when enabled) is device position, color, local position, domain, aa edge equations.
30 // This order matches the constructor argument order of VertexSpec and is the order that
31 // GPAttributes maintains. If hasLocalCoords is false, then the local quad type can be ignored.
32 struct VertexSpec {
33 public:
Brian Osman3d139a42018-11-19 10:42:10 -050034 VertexSpec(GrQuadType deviceQuadType, ColorType colorType, GrQuadType localQuadType,
Michael Ludwigc182b942018-11-16 10:27:51 -050035 bool hasLocalCoords, Domain domain, GrAAType aa)
36 : fDeviceQuadType(static_cast<unsigned>(deviceQuadType))
37 , fLocalQuadType(static_cast<unsigned>(localQuadType))
38 , fHasLocalCoords(hasLocalCoords)
Brian Osman3d139a42018-11-19 10:42:10 -050039 , fColorType(static_cast<unsigned>(colorType))
Michael Ludwigc182b942018-11-16 10:27:51 -050040 , fHasDomain(static_cast<unsigned>(domain))
41 , fUsesCoverageAA(aa == GrAAType::kCoverage) { }
Michael Ludwig460eb5e2018-10-29 11:09:29 -040042
Michael Ludwigc182b942018-11-16 10:27:51 -050043 GrQuadType deviceQuadType() const { return static_cast<GrQuadType>(fDeviceQuadType); }
44 GrQuadType localQuadType() const { return static_cast<GrQuadType>(fLocalQuadType); }
45 bool hasLocalCoords() const { return fHasLocalCoords; }
Brian Osman3d139a42018-11-19 10:42:10 -050046 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
47 bool hasVertexColors() const { return ColorType::kNone != this->colorType(); }
Michael Ludwigc182b942018-11-16 10:27:51 -050048 bool hasDomain() const { return fHasDomain; }
49 bool usesCoverageAA() const { return fUsesCoverageAA; }
Michael Ludwig460eb5e2018-10-29 11:09:29 -040050
Michael Ludwigc182b942018-11-16 10:27:51 -050051 // Will always be 2 or 3
52 int deviceDimensionality() const;
53 // Will always be 0 if hasLocalCoords is false, otherwise will be 2 or 3
54 int localDimensionality() const;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040055
Michael Ludwigc182b942018-11-16 10:27:51 -050056 private:
57 static_assert(kGrQuadTypeCount <= 4, "GrQuadType doesn't fit in 2 bits");
Brian Osman3d139a42018-11-19 10:42:10 -050058 static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits");
Michael Ludwig460eb5e2018-10-29 11:09:29 -040059
Michael Ludwigc182b942018-11-16 10:27:51 -050060 unsigned fDeviceQuadType: 2;
61 unsigned fLocalQuadType: 2;
62 unsigned fHasLocalCoords: 1;
Brian Osman3d139a42018-11-19 10:42:10 -050063 unsigned fColorType : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -050064 unsigned fHasDomain: 1;
65 unsigned fUsesCoverageAA: 1;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040066 };
67
Brian Osman78dc72c2018-12-03 13:20:43 +000068 // Utility class that manages the attribute state necessary to render a particular batch of
69 // quads. It is similar to a geometry processor but is meant to be included in a has-a
70 // relationship by specialized GP's that provide further functionality on top of the per-edge AA
71 // coverage.
72 //
73 // For performance reasons, this uses fixed names for the attribute variables; since it defines
74 // the majority of attributes a GP will likely need, this shouldn't be too limiting.
75 //
76 // In terms of responsibilities, the actual geometry processor must still call emitTransforms(),
77 // using the localCoords() attribute as the 4th argument; it must set the transform data helper
78 // to use the identity matrix; it must manage the color space transform for the quad's paint
79 // color; it should include getKey() in the geometry processor's key builder; and it should
80 // add these attributes at construction time.
81 class GPAttributes {
82 public:
83 using Attribute = GrPrimitiveProcessor::Attribute;
Michael Ludwig20e909e2018-10-30 10:43:57 -040084
Brian Osman78dc72c2018-12-03 13:20:43 +000085 GPAttributes(const VertexSpec& vertexSpec);
86
87 const Attribute& positionsAndMaxCoverage() const { return fPositions; }
88 const Attribute& colors() const { return fColors; }
89 const Attribute& localCoords() const { return fLocalCoords; }
90 const Attribute& domain() const { return fDomain; }
91 const Attribute& edgeDistances() const { return fAAEdgeDistances; }
92
93 bool hasVertexColors() const { return fColors.isInitialized(); }
94
95 bool usesCoverageAA() const { return fAAEdgeDistances.isInitialized(); }
96
97 bool hasLocalCoords() const { return fLocalCoords.isInitialized(); }
98
99 bool hasDomain() const { return fDomain.isInitialized(); }
100
101 bool needsPerspectiveInterpolation() const;
102
103 const Attribute* attributes() const { return &fPositions; }
104 int attributeCount() const { return 5; }
105
106 uint32_t getKey() const;
107
108 // Functions to be called at appropriate times in a processor's onEmitCode() block. These
109 // are separated into discrete pieces so that they can be interleaved with the rest of the
110 // processor's shader code as needed. The functions take char* arguments for the names of
111 // variables the emitted code must declare, so that the calling GP can ensure there's no
112 // naming conflicts with their own code.
113
114 // Returns the GrShaderVar to store in GrGPArgs
115 GrShaderVar emitPosition(GrGLSLPrimitiveProcessor::EmitArgs& args,
116 const char* posVarName) const;
117
118 void emitColor(GrGLSLPrimitiveProcessor::EmitArgs& args, const char* colorVarName) const;
119
120 // localCoordName will be declared as a float2, with any domain applied after any
121 // perspective division is performed.
122 //
123 // Note: this should only be used if the local coordinates need to be passed separately
124 // from the standard coord transform process that is used by FPs.
125 // FIXME: This can go in two directions from here, if GrTextureOp stops needing per-quad
126 // domains it can be removed and GrTextureOp rewritten to use coord transforms. Or
127 // emitTransform() in the primitive builder can be updated to have a notion of domain for
128 // local coords, and all domain-needing code (blurs, filters, etc.) can switch to that magic
129 void emitExplicitLocalCoords(GrGLSLPrimitiveProcessor::EmitArgs& args,
130 const char* localCoordName, const char* domainVarName) const;
131
132 void emitCoverage(GrGLSLPrimitiveProcessor::EmitArgs& args, const char* edgeDistName) const;
133 private:
134 Attribute fPositions; // named "position" in SkSL
135 Attribute fColors; // named "color" in SkSL
136 Attribute fLocalCoords; // named "localCoord" in SkSL
137 Attribute fDomain; // named "domain" in SkSL
138 Attribute fAAEdgeDistances; // named "aaEdgeDist" in SkSL
139
140 // The positions attribute is always a vec4 and can't be used to encode perspectiveness
141 bool fNeedsPerspective;
142 };
Michael Ludwig20e909e2018-10-30 10:43:57 -0400143
Michael Ludwigc182b942018-11-16 10:27:51 -0500144 // Fill vertices with the vertex data needed to represent the given quad. The device position,
145 // local coords, vertex color, domain, and edge coefficients will be written and/or computed
146 // based on the configuration in the vertex spec; if that attribute is disabled in the spec,
147 // then its corresponding function argument is ignored.
148 //
149 // Returns the advanced pointer in vertices.
Brian Osman78dc72c2018-12-03 13:20:43 +0000150 // TODO4F: Switch GrColor to GrVertexColor
Michael Ludwigc182b942018-11-16 10:27:51 -0500151 void* Tessellate(void* vertices, const VertexSpec& spec, const GrPerspQuad& deviceQuad,
Brian Osman3d139a42018-11-19 10:42:10 -0500152 const SkPMColor4f& color, const GrPerspQuad& localQuad, const SkRect& domain,
Michael Ludwigc182b942018-11-16 10:27:51 -0500153 GrQuadAAFlags aa);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400154
Michael Ludwigc182b942018-11-16 10:27:51 -0500155} // namespace GrQuadPerEdgeAA
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400156
157#endif // GrQuadPerEdgeAA_DEFINED