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