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 | |
Robert Phillips | ef80d7b | 2021-09-14 16:10:56 -0400 | [diff] [blame^] | 8 | #ifndef QuadPerEdgeAA_DEFINED |
| 9 | #define QuadPerEdgeAA_DEFINED |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkPoint.h" |
| 12 | #include "include/core/SkPoint3.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/private/GrTypesPriv.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrGeometryProcessor.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrSamplerState.h" |
Michael Ludwig | 189c980 | 2019-11-21 11:21:12 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrVertexWriter.h" |
Michael Ludwig | fd4f4df | 2019-05-29 09:51:09 -0400 | [diff] [blame] | 18 | #include "src/gpu/geometry/GrQuad.h" |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 19 | #include "src/gpu/geometry/GrQuadUtils.h" |
Robert Phillips | 2145390 | 2021-08-27 16:05:04 -0400 | [diff] [blame] | 20 | #include "src/gpu/ops/TextureOp.h" |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 21 | |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 22 | class GrCaps; |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 23 | class GrColorSpaceXform; |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 24 | class GrMeshDrawTarget; |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 25 | class GrShaderCaps; |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 26 | struct GrVertexWriter; |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 27 | |
Robert Phillips | ef80d7b | 2021-09-14 16:10:56 -0400 | [diff] [blame^] | 28 | namespace skgpu::v1::QuadPerEdgeAA { |
Robert Phillips | 2145390 | 2021-08-27 16:05:04 -0400 | [diff] [blame] | 29 | using Saturate = skgpu::v1::TextureOp::Saturate; |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 30 | |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 31 | enum class CoverageMode { kNone, kWithPosition, kWithColor }; |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 32 | enum class Subset : bool { kNo = false, kYes = true }; |
Brian Osman | 2715bf5 | 2019-12-06 14:38:47 -0500 | [diff] [blame] | 33 | enum class ColorType { kNone, kByte, kFloat, kLast = kFloat }; |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 34 | static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1; |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 35 | |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 36 | enum class IndexBufferOption { |
| 37 | kPictureFramed, // geometrically AA'd -> 8 verts/quad + an index buffer |
| 38 | kIndexedRects, // non-AA'd but indexed -> 4 verts/quad + an index buffer |
| 39 | kTriStrips, // non-AA'd -> 4 verts/quad but no index buffer |
| 40 | kLast = kTriStrips |
| 41 | }; |
| 42 | static const int kIndexBufferOptionCount = static_cast<int>(IndexBufferOption::kLast) + 1; |
| 43 | |
Brian Salomon | b8c4add | 2021-06-28 09:20:44 -0400 | [diff] [blame] | 44 | IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads); |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 45 | |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 46 | // Gets the minimum ColorType that can represent a color. |
Brian Osman | 2715bf5 | 2019-12-06 14:38:47 -0500 | [diff] [blame] | 47 | ColorType MinColorType(SkPMColor4f); |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 48 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 49 | // Specifies the vertex configuration for an op that renders per-edge AA quads. The vertex |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 50 | // order (when enabled) is device position, color, local position, subset, aa edge equations. |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 51 | // This order matches the constructor argument order of VertexSpec and is the order that |
| 52 | // GPAttributes maintains. If hasLocalCoords is false, then the local quad type can be ignored. |
| 53 | struct VertexSpec { |
| 54 | public: |
Robert Phillips | 32803ff | 2019-10-23 08:26:08 -0400 | [diff] [blame] | 55 | VertexSpec() |
| 56 | : fDeviceQuadType(0) // kAxisAligned |
| 57 | , fLocalQuadType(0) // kAxisAligned |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 58 | , fIndexBufferOption(0) // kPictureFramed |
Robert Phillips | 32803ff | 2019-10-23 08:26:08 -0400 | [diff] [blame] | 59 | , fHasLocalCoords(false) |
| 60 | , fColorType(0) // kNone |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 61 | , fHasSubset(false) |
Robert Phillips | 32803ff | 2019-10-23 08:26:08 -0400 | [diff] [blame] | 62 | , fUsesCoverageAA(false) |
| 63 | , fCompatibleWithCoverageAsAlpha(false) |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 64 | , fRequiresGeometrySubset(false) {} |
Robert Phillips | 32803ff | 2019-10-23 08:26:08 -0400 | [diff] [blame] | 65 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 66 | VertexSpec(GrQuad::Type deviceQuadType, ColorType colorType, GrQuad::Type localQuadType, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 67 | bool hasLocalCoords, |
| 68 | Subset subset, GrAAType aa, bool coverageAsAlpha, |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 69 | IndexBufferOption indexBufferOption) |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 70 | : fDeviceQuadType(static_cast<unsigned>(deviceQuadType)) |
| 71 | , fLocalQuadType(static_cast<unsigned>(localQuadType)) |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 72 | , fIndexBufferOption(static_cast<unsigned>(indexBufferOption)) |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 73 | , fHasLocalCoords(hasLocalCoords) |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 74 | , fColorType(static_cast<unsigned>(colorType)) |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 75 | , fHasSubset(static_cast<unsigned>(subset)) |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 76 | , fUsesCoverageAA(aa == GrAAType::kCoverage) |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 77 | , fCompatibleWithCoverageAsAlpha(coverageAsAlpha) |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 78 | , fRequiresGeometrySubset(aa == GrAAType::kCoverage && |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 79 | deviceQuadType > GrQuad::Type::kRectilinear) { } |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 80 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 81 | GrQuad::Type deviceQuadType() const { return static_cast<GrQuad::Type>(fDeviceQuadType); } |
| 82 | GrQuad::Type localQuadType() const { return static_cast<GrQuad::Type>(fLocalQuadType); } |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 83 | IndexBufferOption indexBufferOption() const { |
| 84 | return static_cast<IndexBufferOption>(fIndexBufferOption); |
| 85 | } |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 86 | bool hasLocalCoords() const { return fHasLocalCoords; } |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 87 | ColorType colorType() const { return static_cast<ColorType>(fColorType); } |
| 88 | bool hasVertexColors() const { return ColorType::kNone != this->colorType(); } |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 89 | bool hasSubset() const { return fHasSubset; } |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 90 | bool usesCoverageAA() const { return fUsesCoverageAA; } |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 91 | bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; } |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 92 | bool requiresGeometrySubset() const { return fRequiresGeometrySubset; } |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 93 | // Will always be 2 or 3 |
| 94 | int deviceDimensionality() const; |
| 95 | // Will always be 0 if hasLocalCoords is false, otherwise will be 2 or 3 |
| 96 | int localDimensionality() const; |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 97 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 98 | int verticesPerQuad() const { return fUsesCoverageAA ? 8 : 4; } |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 99 | |
| 100 | CoverageMode coverageMode() const; |
| 101 | size_t vertexSize() const; |
| 102 | |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 103 | bool needsIndexBuffer() const { return this->indexBufferOption() != |
| 104 | IndexBufferOption::kTriStrips; } |
| 105 | |
Robert Phillips | cea290f | 2019-11-06 11:21:03 -0500 | [diff] [blame] | 106 | GrPrimitiveType primitiveType() const { |
| 107 | switch (this->indexBufferOption()) { |
| 108 | case IndexBufferOption::kPictureFramed: return GrPrimitiveType::kTriangles; |
| 109 | case IndexBufferOption::kIndexedRects: return GrPrimitiveType::kTriangles; |
| 110 | case IndexBufferOption::kTriStrips: return GrPrimitiveType::kTriangleStrip; |
| 111 | } |
| 112 | |
| 113 | SkUNREACHABLE; |
| 114 | } |
| 115 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 116 | private: |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 117 | static_assert(GrQuad::kTypeCount <= 4, "GrQuad::Type doesn't fit in 2 bits"); |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 118 | static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits"); |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 119 | static_assert(kIndexBufferOptionCount <= 4, "IndexBufferOption doesn't fit in 2 bits"); |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 120 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 121 | unsigned fDeviceQuadType: 2; |
| 122 | unsigned fLocalQuadType: 2; |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 123 | unsigned fIndexBufferOption: 2; |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 124 | unsigned fHasLocalCoords: 1; |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 125 | unsigned fColorType : 2; |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 126 | unsigned fHasSubset : 1; |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 127 | unsigned fUsesCoverageAA: 1; |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 128 | unsigned fCompatibleWithCoverageAsAlpha: 1; |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 129 | // The geometry subset serves to clip off pixels touched by quads with sharp corners that |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 130 | // would otherwise exceed the miter limit for the AA-outset geometry. |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 131 | unsigned fRequiresGeometrySubset : 1; |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 132 | }; |
| 133 | |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 134 | // A Tessellator is responsible for processing a series of device+local GrQuads into a VBO, |
| 135 | // as specified by a VertexSpec. This vertex data can then be processed by a GP created with |
| 136 | // MakeProcessor and/or MakeTexturedProcessor. |
| 137 | class Tessellator { |
| 138 | public: |
Michael Ludwig | 189c980 | 2019-11-21 11:21:12 -0500 | [diff] [blame] | 139 | explicit Tessellator(const VertexSpec& spec, char* vertices); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 140 | |
Michael Ludwig | 189c980 | 2019-11-21 11:21:12 -0500 | [diff] [blame] | 141 | // Calculates (as needed) inset and outset geometry for anti-aliasing, and appends all |
| 142 | // necessary position and vertex attributes required by this Tessellator's VertexSpec into |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 143 | // the 'vertices' the Tessellator was called with. The insetting and outsetting may |
| 144 | // damage the provided GrQuads (as this is intended to work with GrQuadBuffer::Iter). |
| 145 | // 'localQuad' can be null if the VertexSpec does not use local coords. |
| 146 | void append(GrQuad* deviceQuad, GrQuad* localQuad, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 147 | const SkPMColor4f& color, const SkRect& uvSubset, GrQuadAAFlags aaFlags); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 148 | |
Michael Ludwig | 189c980 | 2019-11-21 11:21:12 -0500 | [diff] [blame] | 149 | SkDEBUGCODE(char* vertices() const { return (char*) fVertexWriter.fPtr; }) |
| 150 | |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 151 | private: |
| 152 | // VertexSpec defines many unique ways to write vertex attributes, which can be handled |
| 153 | // generically by branching per-quad based on the VertexSpec. However, there are several |
| 154 | // specs that appear in the wild far more frequently, so they use explicit WriteQuadProcs |
| 155 | // that have no branches. |
| 156 | typedef void (*WriteQuadProc)(GrVertexWriter* vertices, const VertexSpec& spec, |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 157 | const GrQuad* deviceQuad, const GrQuad* localQuad, |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 158 | const float coverage[4], const SkPMColor4f& color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 159 | const SkRect& geomSubset, const SkRect& texSubset); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 160 | static WriteQuadProc GetWriteQuadProc(const VertexSpec& spec); |
| 161 | |
| 162 | GrQuadUtils::TessellationHelper fAAHelper; |
| 163 | VertexSpec fVertexSpec; |
Michael Ludwig | 189c980 | 2019-11-21 11:21:12 -0500 | [diff] [blame] | 164 | GrVertexWriter fVertexWriter; |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 165 | WriteQuadProc fWriteProc; |
| 166 | }; |
| 167 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 168 | GrGeometryProcessor* MakeProcessor(SkArenaAlloc*, const VertexSpec&); |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 169 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 170 | GrGeometryProcessor* MakeTexturedProcessor(SkArenaAlloc*, |
| 171 | const VertexSpec&, |
| 172 | const GrShaderCaps&, |
| 173 | const GrBackendFormat&, |
| 174 | GrSamplerState, |
| 175 | const GrSwizzle&, |
| 176 | sk_sp<GrColorSpaceXform> textureColorSpaceXform, |
| 177 | Saturate); |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 178 | |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 179 | // This method will return the correct index buffer for the specified indexBufferOption. |
| 180 | // It will, correctly, return nullptr if the indexBufferOption is kTriStrips. |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 181 | sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawTarget*, IndexBufferOption); |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 182 | |
Robert Phillips | 438d986 | 2019-11-14 12:46:05 -0500 | [diff] [blame] | 183 | // What is the maximum number of quads allowed for the specified indexBuffer option? |
| 184 | int QuadLimit(IndexBufferOption); |
| 185 | |
Chris Dalton | dbb833b | 2020-03-17 12:15:46 -0600 | [diff] [blame] | 186 | // This method will issue the draw call on the provided GrOpsRenderPass, as specified by the |
| 187 | // indexing method in vertexSpec. It is up to the calling code to allocate, fill in, and bind a |
| 188 | // vertex buffer, and to acquire and bind the correct index buffer (if needed) with |
| 189 | // GrPrimitiveRestart::kNo. |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 190 | // |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 191 | // @param runningQuadCount the number of quads already stored in 'vertexBuffer' and |
| 192 | // 'indexBuffer' e.g., different GrMeshes have already been placed in |
| 193 | // the buffers to allow dynamic state changes. |
| 194 | // @param quadCount the number of quads that will be drawn by the provided 'mesh'. |
| 195 | // A subsequent ConfigureMesh call would the use |
| 196 | // 'runningQuadCount' + 'quadCount' for its new 'runningQuadCount'. |
Chris Dalton | dbb833b | 2020-03-17 12:15:46 -0600 | [diff] [blame] | 197 | void IssueDraw(const GrCaps&, GrOpsRenderPass*, const VertexSpec&, int runningQuadCount, |
| 198 | int quadCount, int maxVerts, int absVertBufferOffset); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 199 | |
Robert Phillips | ef80d7b | 2021-09-14 16:10:56 -0400 | [diff] [blame^] | 200 | } // namespace skgpu::v1::QuadPerEdgeAA |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 201 | |
Robert Phillips | ef80d7b | 2021-09-14 16:10:56 -0400 | [diff] [blame^] | 202 | #endif // QuadPerEdgeAA_DEFINED |