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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkPoint.h" |
| 12 | #include "include/core/SkPoint3.h" |
| 13 | #include "include/gpu/GrSamplerState.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/private/GrTypesPriv.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrGeometryProcessor.h" |
Michael Ludwig | fd4f4df | 2019-05-29 09:51:09 -0400 | [diff] [blame] | 17 | #include "src/gpu/geometry/GrQuad.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/ops/GrMeshDrawOp.h" |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 19 | |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 20 | class GrCaps; |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 21 | class GrColorSpaceXform; |
| 22 | class GrShaderCaps; |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 23 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 24 | namespace GrQuadPerEdgeAA { |
| 25 | |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 26 | enum class Domain : bool { kNo = false, kYes = true }; |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 27 | enum class ColorType { kNone, kByte, kHalf, kLast = kHalf }; |
| 28 | static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1; |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 29 | |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 30 | // Gets the minimum ColorType that can represent a color. |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 31 | ColorType MinColorType(SkPMColor4f, GrClampType, const GrCaps&); |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 32 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 33 | // Specifies the vertex configuration for an op that renders per-edge AA quads. The vertex |
| 34 | // order (when enabled) is device position, color, local position, domain, aa edge equations. |
| 35 | // This order matches the constructor argument order of VertexSpec and is the order that |
| 36 | // GPAttributes maintains. If hasLocalCoords is false, then the local quad type can be ignored. |
| 37 | struct VertexSpec { |
| 38 | public: |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 39 | VertexSpec(GrQuad::Type deviceQuadType, ColorType colorType, GrQuad::Type localQuadType, |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 40 | bool hasLocalCoords, Domain domain, GrAAType aa, bool coverageAsAlpha) |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 41 | : fDeviceQuadType(static_cast<unsigned>(deviceQuadType)) |
| 42 | , fLocalQuadType(static_cast<unsigned>(localQuadType)) |
| 43 | , fHasLocalCoords(hasLocalCoords) |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 44 | , fColorType(static_cast<unsigned>(colorType)) |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 45 | , fHasDomain(static_cast<unsigned>(domain)) |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 46 | , fUsesCoverageAA(aa == GrAAType::kCoverage) |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 47 | , fCompatibleWithCoverageAsAlpha(coverageAsAlpha) |
| 48 | , fRequiresGeometryDomain(aa == GrAAType::kCoverage && |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 49 | deviceQuadType > GrQuad::Type::kRectilinear) { } |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 50 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 51 | GrQuad::Type deviceQuadType() const { return static_cast<GrQuad::Type>(fDeviceQuadType); } |
| 52 | GrQuad::Type localQuadType() const { return static_cast<GrQuad::Type>(fLocalQuadType); } |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 53 | bool hasLocalCoords() const { return fHasLocalCoords; } |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 54 | ColorType colorType() const { return static_cast<ColorType>(fColorType); } |
| 55 | bool hasVertexColors() const { return ColorType::kNone != this->colorType(); } |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 56 | bool hasDomain() const { return fHasDomain; } |
| 57 | bool usesCoverageAA() const { return fUsesCoverageAA; } |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 58 | bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; } |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 59 | bool requiresGeometryDomain() const { return fRequiresGeometryDomain; } |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 60 | // Will always be 2 or 3 |
| 61 | int deviceDimensionality() const; |
| 62 | // Will always be 0 if hasLocalCoords is false, otherwise will be 2 or 3 |
| 63 | int localDimensionality() const; |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 64 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 65 | int verticesPerQuad() const { return fUsesCoverageAA ? 8 : 4; } |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 66 | private: |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 67 | 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] | 68 | static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits"); |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 69 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 70 | unsigned fDeviceQuadType: 2; |
| 71 | unsigned fLocalQuadType: 2; |
| 72 | unsigned fHasLocalCoords: 1; |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 73 | unsigned fColorType : 2; |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 74 | unsigned fHasDomain: 1; |
| 75 | unsigned fUsesCoverageAA: 1; |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 76 | unsigned fCompatibleWithCoverageAsAlpha: 1; |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 77 | // The geometry domain serves to clip off pixels touched by quads with sharp corners that |
| 78 | // would otherwise exceed the miter limit for the AA-outset geometry. |
| 79 | unsigned fRequiresGeometryDomain: 1; |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 80 | }; |
| 81 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 82 | sk_sp<GrGeometryProcessor> MakeProcessor(const VertexSpec& spec); |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 83 | |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 84 | sk_sp<GrGeometryProcessor> MakeTexturedProcessor( |
| 85 | const VertexSpec& spec, const GrShaderCaps& caps, GrTextureType textureType, |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 86 | const GrSamplerState& samplerState, const GrSwizzle& swizzle, uint32_t extraSamplerKey, |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 87 | sk_sp<GrColorSpaceXform> textureColorSpaceXform); |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 88 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 89 | // Fill vertices with the vertex data needed to represent the given quad. The device position, |
| 90 | // local coords, vertex color, domain, and edge coefficients will be written and/or computed |
| 91 | // based on the configuration in the vertex spec; if that attribute is disabled in the spec, |
| 92 | // then its corresponding function argument is ignored. |
| 93 | // |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 94 | // Tessellation is based on the quad type of the vertex spec, not the provided GrQuad's |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 95 | // so that all quads in a batch are tessellated the same. |
| 96 | // |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 97 | // Returns the advanced pointer in vertices. |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 98 | void* Tessellate(void* vertices, const VertexSpec& spec, const GrQuad& deviceQuad, |
| 99 | const SkPMColor4f& color, const GrQuad& localQuad, const SkRect& domain, |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 100 | GrQuadAAFlags aa); |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 101 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 102 | // The mesh will have its index data configured to meet the expectations of the Tessellate() |
| 103 | // function, but it the calling code must handle filling a vertex buffer via Tessellate() and |
| 104 | // then assigning it to the returned mesh. |
| 105 | // |
| 106 | // Returns false if the index data could not be allocated. |
| 107 | bool ConfigureMeshIndices(GrMeshDrawOp::Target* target, GrMesh* mesh, const VertexSpec& spec, |
| 108 | int quadCount); |
| 109 | |
| 110 | static constexpr int kNumAAQuadsInIndexBuffer = 512; |
| 111 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 112 | } // namespace GrQuadPerEdgeAA |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 113 | |
| 114 | #endif // GrQuadPerEdgeAA_DEFINED |