blob: 24451396e8a9d9432e189fb1dcaa3a91dc057612 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkPoint.h"
12#include "include/core/SkPoint3.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/GrTypesPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrGeometryProcessor.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040016#include "src/gpu/GrSamplerState.h"
Michael Ludwig189c9802019-11-21 11:21:12 -050017#include "src/gpu/GrVertexWriter.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040018#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig73dbea62019-11-19 14:55:36 -050019#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/ops/GrMeshDrawOp.h"
Brian Salomonf19f9ca2019-09-18 15:54:26 -040021#include "src/gpu/ops/GrTextureOp.h"
Michael Ludwig460eb5e2018-10-29 11:09:29 -040022
Brian Osman8fa7ab42019-03-18 10:22:42 -040023class GrCaps;
Michael Ludwig467994d2018-12-03 14:58:31 +000024class GrColorSpaceXform;
25class GrShaderCaps;
Michael Ludwig73dbea62019-11-19 14:55:36 -050026struct GrVertexWriter;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040027
Michael Ludwigc182b942018-11-16 10:27:51 -050028namespace GrQuadPerEdgeAA {
Brian Salomonf19f9ca2019-09-18 15:54:26 -040029 using Saturate = GrTextureOp::Saturate;
Michael Ludwigc182b942018-11-16 10:27:51 -050030
Robert Phillips29f38542019-10-16 09:20:25 -040031 enum class CoverageMode { kNone, kWithPosition, kWithColor };
Brian Salomon2432d062020-04-16 20:48:09 -040032 enum class Subset : bool { kNo = false, kYes = true };
Brian Osman2715bf52019-12-06 14:38:47 -050033 enum class ColorType { kNone, kByte, kFloat, kLast = kFloat };
Brian Osman3d139a42018-11-19 10:42:10 -050034 static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040035
Robert Phillipsc554dcf2019-10-28 11:43:55 -040036 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
44 IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads);
45
Brian Salomon1d835422019-03-13 16:11:44 -040046 // Gets the minimum ColorType that can represent a color.
Brian Osman2715bf52019-12-06 14:38:47 -050047 ColorType MinColorType(SkPMColor4f);
Brian Salomon1d835422019-03-13 16:11:44 -040048
Michael Ludwigc182b942018-11-16 10:27:51 -050049 // Specifies the vertex configuration for an op that renders per-edge AA quads. The vertex
Brian Salomon2432d062020-04-16 20:48:09 -040050 // order (when enabled) is device position, color, local position, subset, aa edge equations.
Michael Ludwigc182b942018-11-16 10:27:51 -050051 // 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 Phillips32803ff2019-10-23 08:26:08 -040055 VertexSpec()
56 : fDeviceQuadType(0) // kAxisAligned
57 , fLocalQuadType(0) // kAxisAligned
Robert Phillipsc554dcf2019-10-28 11:43:55 -040058 , fIndexBufferOption(0) // kPictureFramed
Robert Phillips32803ff2019-10-23 08:26:08 -040059 , fHasLocalCoords(false)
60 , fColorType(0) // kNone
Brian Salomon2432d062020-04-16 20:48:09 -040061 , fHasSubset(false)
Robert Phillips32803ff2019-10-23 08:26:08 -040062 , fUsesCoverageAA(false)
63 , fCompatibleWithCoverageAsAlpha(false)
Brian Salomon2432d062020-04-16 20:48:09 -040064 , fRequiresGeometrySubset(false) {}
Robert Phillips32803ff2019-10-23 08:26:08 -040065
Michael Ludwigde4c58c2019-06-04 09:12:59 -040066 VertexSpec(GrQuad::Type deviceQuadType, ColorType colorType, GrQuad::Type localQuadType,
Brian Salomon2432d062020-04-16 20:48:09 -040067 bool hasLocalCoords,
68 Subset subset, GrAAType aa, bool coverageAsAlpha,
Robert Phillipsc554dcf2019-10-28 11:43:55 -040069 IndexBufferOption indexBufferOption)
Michael Ludwigc182b942018-11-16 10:27:51 -050070 : fDeviceQuadType(static_cast<unsigned>(deviceQuadType))
71 , fLocalQuadType(static_cast<unsigned>(localQuadType))
Robert Phillipsc554dcf2019-10-28 11:43:55 -040072 , fIndexBufferOption(static_cast<unsigned>(indexBufferOption))
Michael Ludwigc182b942018-11-16 10:27:51 -050073 , fHasLocalCoords(hasLocalCoords)
Brian Osman3d139a42018-11-19 10:42:10 -050074 , fColorType(static_cast<unsigned>(colorType))
Brian Salomon2432d062020-04-16 20:48:09 -040075 , fHasSubset(static_cast<unsigned>(subset))
Michael Ludwig93aeba02018-12-21 09:50:31 -050076 , fUsesCoverageAA(aa == GrAAType::kCoverage)
Michael Ludwigdcfbe322019-04-01 14:55:54 -040077 , fCompatibleWithCoverageAsAlpha(coverageAsAlpha)
Brian Salomon2432d062020-04-16 20:48:09 -040078 , fRequiresGeometrySubset(aa == GrAAType::kCoverage &&
Michael Ludwigde4c58c2019-06-04 09:12:59 -040079 deviceQuadType > GrQuad::Type::kRectilinear) { }
Michael Ludwig460eb5e2018-10-29 11:09:29 -040080
Michael Ludwigde4c58c2019-06-04 09:12:59 -040081 GrQuad::Type deviceQuadType() const { return static_cast<GrQuad::Type>(fDeviceQuadType); }
82 GrQuad::Type localQuadType() const { return static_cast<GrQuad::Type>(fLocalQuadType); }
Robert Phillipsc554dcf2019-10-28 11:43:55 -040083 IndexBufferOption indexBufferOption() const {
84 return static_cast<IndexBufferOption>(fIndexBufferOption);
85 }
Michael Ludwigc182b942018-11-16 10:27:51 -050086 bool hasLocalCoords() const { return fHasLocalCoords; }
Brian Osman3d139a42018-11-19 10:42:10 -050087 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
88 bool hasVertexColors() const { return ColorType::kNone != this->colorType(); }
Brian Salomon2432d062020-04-16 20:48:09 -040089 bool hasSubset() const { return fHasSubset; }
Michael Ludwigc182b942018-11-16 10:27:51 -050090 bool usesCoverageAA() const { return fUsesCoverageAA; }
Brian Osman605c6d52019-03-15 12:10:35 -040091 bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; }
Brian Salomon2432d062020-04-16 20:48:09 -040092 bool requiresGeometrySubset() const { return fRequiresGeometrySubset; }
Michael Ludwigc182b942018-11-16 10:27:51 -050093 // 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 Ludwig460eb5e2018-10-29 11:09:29 -040097
Michael Ludwig93aeba02018-12-21 09:50:31 -050098 int verticesPerQuad() const { return fUsesCoverageAA ? 8 : 4; }
Robert Phillips29f38542019-10-16 09:20:25 -040099
100 CoverageMode coverageMode() const;
101 size_t vertexSize() const;
102
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400103 bool needsIndexBuffer() const { return this->indexBufferOption() !=
104 IndexBufferOption::kTriStrips; }
105
Robert Phillipscea290f2019-11-06 11:21:03 -0500106 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 Ludwigc182b942018-11-16 10:27:51 -0500116 private:
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400117 static_assert(GrQuad::kTypeCount <= 4, "GrQuad::Type doesn't fit in 2 bits");
Brian Osman3d139a42018-11-19 10:42:10 -0500118 static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits");
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400119 static_assert(kIndexBufferOptionCount <= 4, "IndexBufferOption doesn't fit in 2 bits");
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400120
Michael Ludwigc182b942018-11-16 10:27:51 -0500121 unsigned fDeviceQuadType: 2;
122 unsigned fLocalQuadType: 2;
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400123 unsigned fIndexBufferOption: 2;
Michael Ludwigc182b942018-11-16 10:27:51 -0500124 unsigned fHasLocalCoords: 1;
Brian Osman3d139a42018-11-19 10:42:10 -0500125 unsigned fColorType : 2;
Brian Salomon2432d062020-04-16 20:48:09 -0400126 unsigned fHasSubset : 1;
Michael Ludwigc182b942018-11-16 10:27:51 -0500127 unsigned fUsesCoverageAA: 1;
Brian Osman605c6d52019-03-15 12:10:35 -0400128 unsigned fCompatibleWithCoverageAsAlpha: 1;
Brian Salomon2432d062020-04-16 20:48:09 -0400129 // The geometry subset serves to clip off pixels touched by quads with sharp corners that
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400130 // would otherwise exceed the miter limit for the AA-outset geometry.
Brian Salomon2432d062020-04-16 20:48:09 -0400131 unsigned fRequiresGeometrySubset : 1;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400132 };
133
Michael Ludwig73dbea62019-11-19 14:55:36 -0500134 // 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 Ludwig189c9802019-11-21 11:21:12 -0500139 explicit Tessellator(const VertexSpec& spec, char* vertices);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500140
Michael Ludwig189c9802019-11-21 11:21:12 -0500141 // 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 Ludwig704d5402019-11-25 09:43:37 -0500143 // 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 Salomon2432d062020-04-16 20:48:09 -0400147 const SkPMColor4f& color, const SkRect& uvSubset, GrQuadAAFlags aaFlags);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500148
Michael Ludwig189c9802019-11-21 11:21:12 -0500149 SkDEBUGCODE(char* vertices() const { return (char*) fVertexWriter.fPtr; })
150
Michael Ludwig73dbea62019-11-19 14:55:36 -0500151 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 Ludwig704d5402019-11-25 09:43:37 -0500157 const GrQuad* deviceQuad, const GrQuad* localQuad,
Michael Ludwig73dbea62019-11-19 14:55:36 -0500158 const float coverage[4], const SkPMColor4f& color,
Brian Salomon2432d062020-04-16 20:48:09 -0400159 const SkRect& geomSubset, const SkRect& texSubset);
Michael Ludwig73dbea62019-11-19 14:55:36 -0500160 static WriteQuadProc GetWriteQuadProc(const VertexSpec& spec);
161
162 GrQuadUtils::TessellationHelper fAAHelper;
163 VertexSpec fVertexSpec;
Michael Ludwig189c9802019-11-21 11:21:12 -0500164 GrVertexWriter fVertexWriter;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500165 WriteQuadProc fWriteProc;
166 };
167
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500168 GrGeometryProcessor* MakeProcessor(SkArenaAlloc*, const VertexSpec&);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400169
Brian Salomonccb61422020-01-09 10:46:36 -0500170 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 Ludwig20e909e2018-10-30 10:43:57 -0400178
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400179 // This method will return the correct index buffer for the specified indexBufferOption.
180 // It will, correctly, return nullptr if the indexBufferOption is kTriStrips.
181 sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawOp::Target*, IndexBufferOption);
182
Robert Phillips438d9862019-11-14 12:46:05 -0500183 // What is the maximum number of quads allowed for the specified indexBuffer option?
184 int QuadLimit(IndexBufferOption);
185
Chris Daltondbb833b2020-03-17 12:15:46 -0600186 // 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 Ludwig93aeba02018-12-21 09:50:31 -0500190 //
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400191 // @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 Daltondbb833b2020-03-17 12:15:46 -0600197 void IssueDraw(const GrCaps&, GrOpsRenderPass*, const VertexSpec&, int runningQuadCount,
198 int quadCount, int maxVerts, int absVertBufferOffset);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500199
Michael Ludwigc182b942018-11-16 10:27:51 -0500200} // namespace GrQuadPerEdgeAA
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400201
202#endif // GrQuadPerEdgeAA_DEFINED