blob: 427e19cb40de04a78962ddf0a621ab2bf1dd4e71 [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 Ludwigfd4f4df2019-05-29 09:51:09 -040017#include "src/gpu/geometry/GrQuad.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/ops/GrMeshDrawOp.h"
Brian Salomonf19f9ca2019-09-18 15:54:26 -040019#include "src/gpu/ops/GrTextureOp.h"
Michael Ludwig460eb5e2018-10-29 11:09:29 -040020
Brian Osman8fa7ab42019-03-18 10:22:42 -040021class GrCaps;
Michael Ludwig467994d2018-12-03 14:58:31 +000022class GrColorSpaceXform;
23class GrShaderCaps;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040024
Michael Ludwigc182b942018-11-16 10:27:51 -050025namespace GrQuadPerEdgeAA {
Brian Salomonf19f9ca2019-09-18 15:54:26 -040026 using Saturate = GrTextureOp::Saturate;
Michael Ludwigc182b942018-11-16 10:27:51 -050027
Robert Phillips29f38542019-10-16 09:20:25 -040028 enum class CoverageMode { kNone, kWithPosition, kWithColor };
Michael Ludwig460eb5e2018-10-29 11:09:29 -040029 enum class Domain : bool { kNo = false, kYes = true };
Brian Osman3d139a42018-11-19 10:42:10 -050030 enum class ColorType { kNone, kByte, kHalf, kLast = kHalf };
31 static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040032
Robert Phillipsc554dcf2019-10-28 11:43:55 -040033 enum class IndexBufferOption {
34 kPictureFramed, // geometrically AA'd -> 8 verts/quad + an index buffer
35 kIndexedRects, // non-AA'd but indexed -> 4 verts/quad + an index buffer
36 kTriStrips, // non-AA'd -> 4 verts/quad but no index buffer
37 kLast = kTriStrips
38 };
39 static const int kIndexBufferOptionCount = static_cast<int>(IndexBufferOption::kLast) + 1;
40
41 IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads);
42
Brian Salomon1d835422019-03-13 16:11:44 -040043 // Gets the minimum ColorType that can represent a color.
Brian Osman8fa7ab42019-03-18 10:22:42 -040044 ColorType MinColorType(SkPMColor4f, GrClampType, const GrCaps&);
Brian Salomon1d835422019-03-13 16:11:44 -040045
Michael Ludwigc182b942018-11-16 10:27:51 -050046 // Specifies the vertex configuration for an op that renders per-edge AA quads. The vertex
47 // order (when enabled) is device position, color, local position, domain, aa edge equations.
48 // This order matches the constructor argument order of VertexSpec and is the order that
49 // GPAttributes maintains. If hasLocalCoords is false, then the local quad type can be ignored.
50 struct VertexSpec {
51 public:
Robert Phillips32803ff2019-10-23 08:26:08 -040052 VertexSpec()
53 : fDeviceQuadType(0) // kAxisAligned
54 , fLocalQuadType(0) // kAxisAligned
Robert Phillipsc554dcf2019-10-28 11:43:55 -040055 , fIndexBufferOption(0) // kPictureFramed
Robert Phillips32803ff2019-10-23 08:26:08 -040056 , fHasLocalCoords(false)
57 , fColorType(0) // kNone
58 , fHasDomain(false)
59 , fUsesCoverageAA(false)
60 , fCompatibleWithCoverageAsAlpha(false)
Robert Phillipsc554dcf2019-10-28 11:43:55 -040061 , fRequiresGeometryDomain(false) {}
Robert Phillips32803ff2019-10-23 08:26:08 -040062
Michael Ludwigde4c58c2019-06-04 09:12:59 -040063 VertexSpec(GrQuad::Type deviceQuadType, ColorType colorType, GrQuad::Type localQuadType,
Robert Phillipsc554dcf2019-10-28 11:43:55 -040064 bool hasLocalCoords, Domain domain, GrAAType aa, bool coverageAsAlpha,
65 IndexBufferOption indexBufferOption)
Michael Ludwigc182b942018-11-16 10:27:51 -050066 : fDeviceQuadType(static_cast<unsigned>(deviceQuadType))
67 , fLocalQuadType(static_cast<unsigned>(localQuadType))
Robert Phillipsc554dcf2019-10-28 11:43:55 -040068 , fIndexBufferOption(static_cast<unsigned>(indexBufferOption))
Michael Ludwigc182b942018-11-16 10:27:51 -050069 , fHasLocalCoords(hasLocalCoords)
Brian Osman3d139a42018-11-19 10:42:10 -050070 , fColorType(static_cast<unsigned>(colorType))
Michael Ludwigc182b942018-11-16 10:27:51 -050071 , fHasDomain(static_cast<unsigned>(domain))
Michael Ludwig93aeba02018-12-21 09:50:31 -050072 , fUsesCoverageAA(aa == GrAAType::kCoverage)
Michael Ludwigdcfbe322019-04-01 14:55:54 -040073 , fCompatibleWithCoverageAsAlpha(coverageAsAlpha)
74 , fRequiresGeometryDomain(aa == GrAAType::kCoverage &&
Michael Ludwigde4c58c2019-06-04 09:12:59 -040075 deviceQuadType > GrQuad::Type::kRectilinear) { }
Michael Ludwig460eb5e2018-10-29 11:09:29 -040076
Michael Ludwigde4c58c2019-06-04 09:12:59 -040077 GrQuad::Type deviceQuadType() const { return static_cast<GrQuad::Type>(fDeviceQuadType); }
78 GrQuad::Type localQuadType() const { return static_cast<GrQuad::Type>(fLocalQuadType); }
Robert Phillipsc554dcf2019-10-28 11:43:55 -040079 IndexBufferOption indexBufferOption() const {
80 return static_cast<IndexBufferOption>(fIndexBufferOption);
81 }
Michael Ludwigc182b942018-11-16 10:27:51 -050082 bool hasLocalCoords() const { return fHasLocalCoords; }
Brian Osman3d139a42018-11-19 10:42:10 -050083 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
84 bool hasVertexColors() const { return ColorType::kNone != this->colorType(); }
Michael Ludwigc182b942018-11-16 10:27:51 -050085 bool hasDomain() const { return fHasDomain; }
86 bool usesCoverageAA() const { return fUsesCoverageAA; }
Brian Osman605c6d52019-03-15 12:10:35 -040087 bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; }
Michael Ludwigdcfbe322019-04-01 14:55:54 -040088 bool requiresGeometryDomain() const { return fRequiresGeometryDomain; }
Michael Ludwigc182b942018-11-16 10:27:51 -050089 // Will always be 2 or 3
90 int deviceDimensionality() const;
91 // Will always be 0 if hasLocalCoords is false, otherwise will be 2 or 3
92 int localDimensionality() const;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040093
Michael Ludwig93aeba02018-12-21 09:50:31 -050094 int verticesPerQuad() const { return fUsesCoverageAA ? 8 : 4; }
Robert Phillips29f38542019-10-16 09:20:25 -040095
96 CoverageMode coverageMode() const;
97 size_t vertexSize() const;
98
Robert Phillipsfd0c3b52019-11-01 08:44:42 -040099 bool needsIndexBuffer() const { return this->indexBufferOption() !=
100 IndexBufferOption::kTriStrips; }
101
Michael Ludwigc182b942018-11-16 10:27:51 -0500102 private:
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400103 static_assert(GrQuad::kTypeCount <= 4, "GrQuad::Type doesn't fit in 2 bits");
Brian Osman3d139a42018-11-19 10:42:10 -0500104 static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits");
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400105 static_assert(kIndexBufferOptionCount <= 4, "IndexBufferOption doesn't fit in 2 bits");
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400106
Michael Ludwigc182b942018-11-16 10:27:51 -0500107 unsigned fDeviceQuadType: 2;
108 unsigned fLocalQuadType: 2;
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400109 unsigned fIndexBufferOption: 2;
Michael Ludwigc182b942018-11-16 10:27:51 -0500110 unsigned fHasLocalCoords: 1;
Brian Osman3d139a42018-11-19 10:42:10 -0500111 unsigned fColorType : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -0500112 unsigned fHasDomain: 1;
113 unsigned fUsesCoverageAA: 1;
Brian Osman605c6d52019-03-15 12:10:35 -0400114 unsigned fCompatibleWithCoverageAsAlpha: 1;
Michael Ludwigdcfbe322019-04-01 14:55:54 -0400115 // The geometry domain serves to clip off pixels touched by quads with sharp corners that
116 // would otherwise exceed the miter limit for the AA-outset geometry.
117 unsigned fRequiresGeometryDomain: 1;
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400118 };
119
Michael Ludwig467994d2018-12-03 14:58:31 +0000120 sk_sp<GrGeometryProcessor> MakeProcessor(const VertexSpec& spec);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400121
Brian Salomon67529b22019-08-13 15:31:04 -0400122 sk_sp<GrGeometryProcessor> MakeTexturedProcessor(
Robert Phillipsf272bea2019-10-17 08:56:16 -0400123 const VertexSpec& spec, const GrShaderCaps& caps, const GrBackendFormat&,
Greg Daniel2c3398d2019-06-19 11:58:01 -0400124 const GrSamplerState& samplerState, const GrSwizzle& swizzle, uint32_t extraSamplerKey,
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400125 sk_sp<GrColorSpaceXform> textureColorSpaceXform, Saturate saturate);
Michael Ludwig20e909e2018-10-30 10:43:57 -0400126
Michael Ludwigc182b942018-11-16 10:27:51 -0500127 // Fill vertices with the vertex data needed to represent the given quad. The device position,
128 // local coords, vertex color, domain, and edge coefficients will be written and/or computed
129 // based on the configuration in the vertex spec; if that attribute is disabled in the spec,
130 // then its corresponding function argument is ignored.
131 //
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400132 // Tessellation is based on the quad type of the vertex spec, not the provided GrQuad's
Michael Ludwig41f395d2019-05-23 13:59:45 -0400133 // so that all quads in a batch are tessellated the same.
134 //
Michael Ludwigc182b942018-11-16 10:27:51 -0500135 // Returns the advanced pointer in vertices.
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400136 void* Tessellate(void* vertices, const VertexSpec& spec, const GrQuad& deviceQuad,
137 const SkPMColor4f& color, const GrQuad& localQuad, const SkRect& domain,
Michael Ludwigc182b942018-11-16 10:27:51 -0500138 GrQuadAAFlags aa);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400139
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400140 // This method will return the correct index buffer for the specified indexBufferOption.
141 // It will, correctly, return nullptr if the indexBufferOption is kTriStrips.
142 sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawOp::Target*, IndexBufferOption);
143
144 // This method will configure the vertex and index data of the provided 'mesh' to comply
145 // with the indexing method specified in the vertexSpec. It is up to the calling code
146 // to allocate and fill in the vertex data and acquire the correct indexBuffer if it is needed.
Michael Ludwig93aeba02018-12-21 09:50:31 -0500147 //
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400148 // @param runningQuadCount the number of quads already stored in 'vertexBuffer' and
149 // 'indexBuffer' e.g., different GrMeshes have already been placed in
150 // the buffers to allow dynamic state changes.
151 // @param quadCount the number of quads that will be drawn by the provided 'mesh'.
152 // A subsequent ConfigureMesh call would the use
153 // 'runningQuadCount' + 'quadCount' for its new 'runningQuadCount'.
154 void ConfigureMesh(GrMesh* mesh, const VertexSpec&, int runningQuadCount, int quadCount,
155 int maxVerts, sk_sp<const GrBuffer> vertexBuffer,
156 sk_sp<const GrBuffer> indexBuffer, int absVertBufferOffset);
Michael Ludwig93aeba02018-12-21 09:50:31 -0500157
Michael Ludwigc182b942018-11-16 10:27:51 -0500158} // namespace GrQuadPerEdgeAA
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400159
160#endif // GrQuadPerEdgeAA_DEFINED