Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 1 | /* |
| 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 Osman | 78dc72c | 2018-12-03 13:20:43 +0000 | [diff] [blame^] | 12 | #include "GrPrimitiveProcessor.h" |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 13 | #include "GrQuad.h" |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 14 | #include "GrSamplerState.h" |
| 15 | #include "GrTypesPriv.h" |
Brian Osman | 78dc72c | 2018-12-03 13:20:43 +0000 | [diff] [blame^] | 16 | #include "glsl/GrGLSLPrimitiveProcessor.h" |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 17 | #include "SkPoint.h" |
| 18 | #include "SkPoint3.h" |
| 19 | |
Brian Osman | 78dc72c | 2018-12-03 13:20:43 +0000 | [diff] [blame^] | 20 | class GrGLSLColorSpaceXformHelper; |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 21 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 22 | namespace GrQuadPerEdgeAA { |
| 23 | |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 24 | enum class Domain : bool { kNo = false, kYes = true }; |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 25 | enum class ColorType { kNone, kByte, kHalf, kLast = kHalf }; |
| 26 | static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1; |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 27 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 28 | // 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 Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 34 | VertexSpec(GrQuadType deviceQuadType, ColorType colorType, GrQuadType localQuadType, |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 35 | bool hasLocalCoords, Domain domain, GrAAType aa) |
| 36 | : fDeviceQuadType(static_cast<unsigned>(deviceQuadType)) |
| 37 | , fLocalQuadType(static_cast<unsigned>(localQuadType)) |
| 38 | , fHasLocalCoords(hasLocalCoords) |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 39 | , fColorType(static_cast<unsigned>(colorType)) |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 40 | , fHasDomain(static_cast<unsigned>(domain)) |
| 41 | , fUsesCoverageAA(aa == GrAAType::kCoverage) { } |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 42 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 43 | GrQuadType deviceQuadType() const { return static_cast<GrQuadType>(fDeviceQuadType); } |
| 44 | GrQuadType localQuadType() const { return static_cast<GrQuadType>(fLocalQuadType); } |
| 45 | bool hasLocalCoords() const { return fHasLocalCoords; } |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 46 | ColorType colorType() const { return static_cast<ColorType>(fColorType); } |
| 47 | bool hasVertexColors() const { return ColorType::kNone != this->colorType(); } |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 48 | bool hasDomain() const { return fHasDomain; } |
| 49 | bool usesCoverageAA() const { return fUsesCoverageAA; } |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 50 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 51 | // 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 Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 55 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 56 | private: |
| 57 | static_assert(kGrQuadTypeCount <= 4, "GrQuadType doesn't fit in 2 bits"); |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 58 | static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits"); |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 59 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 60 | unsigned fDeviceQuadType: 2; |
| 61 | unsigned fLocalQuadType: 2; |
| 62 | unsigned fHasLocalCoords: 1; |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 63 | unsigned fColorType : 2; |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 64 | unsigned fHasDomain: 1; |
| 65 | unsigned fUsesCoverageAA: 1; |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 66 | }; |
| 67 | |
Brian Osman | 78dc72c | 2018-12-03 13:20:43 +0000 | [diff] [blame^] | 68 | // 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 Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 84 | |
Brian Osman | 78dc72c | 2018-12-03 13:20:43 +0000 | [diff] [blame^] | 85 | 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 Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 143 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 144 | // 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 Osman | 78dc72c | 2018-12-03 13:20:43 +0000 | [diff] [blame^] | 150 | // TODO4F: Switch GrColor to GrVertexColor |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 151 | void* Tessellate(void* vertices, const VertexSpec& spec, const GrPerspQuad& deviceQuad, |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 152 | const SkPMColor4f& color, const GrPerspQuad& localQuad, const SkRect& domain, |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 153 | GrQuadAAFlags aa); |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 154 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 155 | } // namespace GrQuadPerEdgeAA |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 156 | |
| 157 | #endif // GrQuadPerEdgeAA_DEFINED |