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 | |
Michael Ludwig | fd4f4df | 2019-05-29 09:51:09 -0400 | [diff] [blame] | 8 | #include "src/gpu/ops/GrQuadPerEdgeAA.h" |
| 9 | |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 10 | #include "include/private/SkVx.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrVertexWriter.h" |
| 12 | #include "src/gpu/SkGr.h" |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 13 | #include "src/gpu/geometry/GrQuadUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h" |
| 15 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 16 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 17 | #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h" |
| 18 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 19 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 20 | |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame] | 21 | |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 22 | namespace { |
| 23 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 24 | // Writes four vertices in triangle strip order, including the additional data for local |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 25 | // coordinates, geometry + texture domains, color, and coverage as needed to satisfy the vertex spec |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 26 | static void write_quad(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec, |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 27 | GrQuadPerEdgeAA::CoverageMode mode, const skvx::Vec<4, float>& coverage, |
| 28 | SkPMColor4f color4f, const SkRect& geomDomain, const SkRect& texDomain, |
| 29 | const GrQuad& deviceQuad, const GrQuad& localQuad) { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 30 | static constexpr auto If = GrVertexWriter::If<float>; |
| 31 | |
Michael Ludwig | 553e9a9 | 2018-11-29 12:38:35 -0500 | [diff] [blame] | 32 | for (int i = 0; i < 4; ++i) { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 33 | // save position, this is a float2 or float3 or float4 depending on the combination of |
| 34 | // perspective and coverage mode. |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 35 | vb->write(deviceQuad.x(i), deviceQuad.y(i), |
| 36 | If(spec.deviceQuadType() == GrQuad::Type::kPerspective, deviceQuad.w(i)), |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 37 | If(mode == GrQuadPerEdgeAA::CoverageMode::kWithPosition, coverage[i])); |
Michael Ludwig | 4921dc3 | 2018-12-03 14:57:29 +0000 | [diff] [blame] | 38 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 39 | // save color |
| 40 | if (spec.hasVertexColors()) { |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 41 | bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kHalf; |
Michael Ludwig | e6266a2 | 2019-03-07 11:24:32 -0500 | [diff] [blame] | 42 | vb->write(GrVertexColor( |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 43 | color4f * (mode == GrQuadPerEdgeAA::CoverageMode::kWithColor ? coverage[i] : 1.f), |
| 44 | wide)); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // save local position |
| 48 | if (spec.hasLocalCoords()) { |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 49 | vb->write(localQuad.x(i), localQuad.y(i), |
| 50 | If(spec.localQuadType() == GrQuad::Type::kPerspective, localQuad.w(i))); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 51 | } |
| 52 | |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 53 | // save the geometry domain |
| 54 | if (spec.requiresGeometryDomain()) { |
| 55 | vb->write(geomDomain); |
| 56 | } |
| 57 | |
| 58 | // save the texture domain |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 59 | if (spec.hasDomain()) { |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 60 | vb->write(texDomain); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 65 | } // anonymous namespace |
| 66 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 67 | namespace GrQuadPerEdgeAA { |
| 68 | |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 69 | IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads) { |
| 70 | if (aa == GrAAType::kCoverage) { |
| 71 | return IndexBufferOption::kPictureFramed; |
| 72 | } else if (numQuads > 1) { |
| 73 | return IndexBufferOption::kIndexedRects; |
| 74 | } else { |
| 75 | return IndexBufferOption::kTriStrips; |
| 76 | } |
| 77 | } |
| 78 | |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 79 | // This is a more elaborate version of SkPMColor4fNeedsWideColor that allows "no color" for white |
| 80 | ColorType MinColorType(SkPMColor4f color, GrClampType clampType, const GrCaps& caps) { |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 81 | if (color == SK_PMColor4fWHITE) { |
| 82 | return ColorType::kNone; |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 83 | } else { |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 84 | return SkPMColor4fNeedsWideColor(color, clampType, caps) ? ColorType::kHalf |
| 85 | : ColorType::kByte; |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 89 | ////////////////// Tessellate Implementation |
| 90 | |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 91 | void* Tessellate(void* vertices, const VertexSpec& spec, const GrQuad& deviceQuad, |
| 92 | const SkPMColor4f& color4f, const GrQuad& localQuad, const SkRect& domain, |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 93 | GrQuadAAFlags aaFlags) { |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 94 | SkASSERT(deviceQuad.quadType() <= spec.deviceQuadType()); |
| 95 | SkASSERT(!spec.hasLocalCoords() || localQuad.quadType() <= spec.localQuadType()); |
| 96 | |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 97 | GrQuadPerEdgeAA::CoverageMode mode = spec.coverageMode(); |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 98 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 99 | GrVertexWriter vb{vertices}; |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 100 | if (spec.usesCoverageAA()) { |
| 101 | SkASSERT(mode == CoverageMode::kWithPosition || mode == CoverageMode::kWithColor); |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 102 | // Must calculate inner and outer quadrilaterals for the vertex coverage ramps, and possibly |
| 103 | // a geometry domain |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 104 | SkRect geomDomain; |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 105 | if (spec.requiresGeometryDomain()) { |
| 106 | geomDomain = deviceQuad.bounds(); |
| 107 | geomDomain.outset(0.5f, 0.5f); // account for AA expansion |
Michael Ludwig | e6266a2 | 2019-03-07 11:24:32 -0500 | [diff] [blame] | 108 | } |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 109 | |
Michael Ludwig | 22a04cd | 2019-11-18 15:34:28 -0500 | [diff] [blame] | 110 | GrQuadUtils::TessellationHelper helper; |
Michael Ludwig | d84dd4b | 2019-11-05 12:03:12 -0500 | [diff] [blame] | 111 | if (aaFlags == GrQuadAAFlags::kNone) { |
| 112 | // Have to write the coverage AA vertex structure, but there's no math to be done for a |
| 113 | // non-aa quad batched into a coverage AA op. |
| 114 | write_quad(&vb, spec, mode, 1.f, color4f, geomDomain, domain, deviceQuad, localQuad); |
| 115 | // Since we pass the same corners in, the outer vertex structure will have 0 area and |
| 116 | // the coverage interpolation from 1 to 0 will not be visible. |
| 117 | write_quad(&vb, spec, mode, 0.f, color4f, geomDomain, domain, deviceQuad, localQuad); |
| 118 | } else { |
| 119 | // TODO(michaelludwig) - Update TessellateHelper to select processing functions based on |
| 120 | // the vertexspec once per op, and then burn through all quads with the selected |
| 121 | // function ptr. |
Michael Ludwig | 22a04cd | 2019-11-18 15:34:28 -0500 | [diff] [blame] | 122 | helper.reset(deviceQuad, spec.hasLocalCoords() ? &localQuad : nullptr); |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 123 | |
Michael Ludwig | d84dd4b | 2019-11-05 12:03:12 -0500 | [diff] [blame] | 124 | // Edge inset/outset distance ordered LBTR, set to 0.5 for a half pixel if the AA flag |
| 125 | // is turned on, or 0.0 if the edge is not anti-aliased. |
| 126 | skvx::Vec<4, float> edgeDistances; |
| 127 | if (aaFlags == GrQuadAAFlags::kAll) { |
| 128 | edgeDistances = 0.5f; |
| 129 | } else { |
| 130 | edgeDistances = { (aaFlags & GrQuadAAFlags::kLeft) ? 0.5f : 0.f, |
| 131 | (aaFlags & GrQuadAAFlags::kBottom) ? 0.5f : 0.f, |
| 132 | (aaFlags & GrQuadAAFlags::kTop) ? 0.5f : 0.f, |
| 133 | (aaFlags & GrQuadAAFlags::kRight) ? 0.5f : 0.f }; |
| 134 | } |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 135 | |
Michael Ludwig | d84dd4b | 2019-11-05 12:03:12 -0500 | [diff] [blame] | 136 | // Write inner vertices first |
| 137 | GrQuad aaDeviceQuad, aaLocalQuad; |
| 138 | skvx::Vec<4, float> coverage = helper.inset(edgeDistances, &aaDeviceQuad, &aaLocalQuad); |
| 139 | write_quad(&vb, spec, mode, coverage, color4f, geomDomain, domain, |
| 140 | aaDeviceQuad, aaLocalQuad); |
| 141 | |
| 142 | // Then outer vertices, which use 0.f for their coverage |
| 143 | helper.outset(edgeDistances, &aaDeviceQuad, &aaLocalQuad); |
| 144 | write_quad(&vb, spec, mode, 0.f, color4f, geomDomain, domain, |
| 145 | aaDeviceQuad, aaLocalQuad); |
| 146 | } |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 147 | } else { |
| 148 | // No outsetting needed, just write a single quad with full coverage |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 149 | SkASSERT(mode == CoverageMode::kNone && !spec.requiresGeometryDomain()); |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 150 | write_quad(&vb, spec, mode, 1.f, color4f, SkRect::MakeEmpty(), domain, |
| 151 | deviceQuad, localQuad); |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 152 | } |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 153 | |
| 154 | return vb.fPtr; |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 155 | } |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 156 | |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 157 | sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawOp::Target* target, |
| 158 | IndexBufferOption indexBufferOption) { |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 159 | auto resourceProvider = target->resourceProvider(); |
| 160 | |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 161 | switch (indexBufferOption) { |
| 162 | case IndexBufferOption::kPictureFramed: return resourceProvider->refAAQuadIndexBuffer(); |
| 163 | case IndexBufferOption::kIndexedRects: return resourceProvider->refNonAAQuadIndexBuffer(); |
| 164 | case IndexBufferOption::kTriStrips: // fall through |
| 165 | default: return nullptr; |
| 166 | } |
| 167 | } |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 168 | |
Robert Phillips | 438d986 | 2019-11-14 12:46:05 -0500 | [diff] [blame] | 169 | int QuadLimit(IndexBufferOption option) { |
| 170 | switch (option) { |
| 171 | case IndexBufferOption::kPictureFramed: return GrResourceProvider::MaxNumAAQuads(); |
| 172 | case IndexBufferOption::kIndexedRects: return GrResourceProvider::MaxNumNonAAQuads(); |
| 173 | case IndexBufferOption::kTriStrips: return SK_MaxS32; // not limited by an indexBuffer |
| 174 | } |
| 175 | |
| 176 | SkUNREACHABLE; |
| 177 | } |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 178 | |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 179 | void ConfigureMesh(GrMesh* mesh, const VertexSpec& spec, |
| 180 | int runningQuadCount, int quadsInDraw, int maxVerts, |
| 181 | sk_sp<const GrBuffer> vertexBuffer, |
| 182 | sk_sp<const GrBuffer> indexBuffer, int absVertBufferOffset) { |
| 183 | SkASSERT(vertexBuffer); |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 184 | |
Robert Phillips | cea290f | 2019-11-06 11:21:03 -0500 | [diff] [blame] | 185 | mesh->setPrimitiveType(spec.primitiveType()); |
| 186 | |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 187 | if (spec.indexBufferOption() == IndexBufferOption::kTriStrips) { |
| 188 | SkASSERT(!indexBuffer); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 189 | |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 190 | mesh->setNonIndexedNonInstanced(4); |
| 191 | int offset = absVertBufferOffset + |
| 192 | runningQuadCount * GrResourceProvider::NumVertsPerNonAAQuad(); |
| 193 | mesh->setVertexData(std::move(vertexBuffer), offset); |
| 194 | return; |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 197 | SkASSERT(spec.indexBufferOption() == IndexBufferOption::kPictureFramed || |
| 198 | spec.indexBufferOption() == IndexBufferOption::kIndexedRects); |
| 199 | SkASSERT(indexBuffer); |
| 200 | |
| 201 | int baseIndex, numIndicesToDraw; |
Robert Phillips | 8e083ee | 2019-11-01 15:07:15 -0400 | [diff] [blame] | 202 | int minVertex, maxVertex; |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 203 | |
| 204 | if (spec.indexBufferOption() == IndexBufferOption::kPictureFramed) { |
| 205 | SkASSERT(runningQuadCount + quadsInDraw <= GrResourceProvider::MaxNumAAQuads()); |
| 206 | // AA uses 8 vertices and 30 indices per quad, basically nested rectangles |
| 207 | baseIndex = runningQuadCount * GrResourceProvider::NumIndicesPerAAQuad(); |
| 208 | numIndicesToDraw = quadsInDraw * GrResourceProvider::NumIndicesPerAAQuad(); |
Robert Phillips | 8e083ee | 2019-11-01 15:07:15 -0400 | [diff] [blame] | 209 | minVertex = runningQuadCount * GrResourceProvider::NumVertsPerAAQuad(); |
| 210 | maxVertex = (runningQuadCount + quadsInDraw) * GrResourceProvider::NumVertsPerAAQuad(); |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 211 | } else { |
| 212 | SkASSERT(runningQuadCount + quadsInDraw <= GrResourceProvider::MaxNumNonAAQuads()); |
| 213 | // Non-AA uses 4 vertices and 6 indices per quad |
| 214 | baseIndex = runningQuadCount * GrResourceProvider::NumIndicesPerNonAAQuad(); |
| 215 | numIndicesToDraw = quadsInDraw * GrResourceProvider::NumIndicesPerNonAAQuad(); |
Robert Phillips | 8e083ee | 2019-11-01 15:07:15 -0400 | [diff] [blame] | 216 | minVertex = runningQuadCount * GrResourceProvider::NumVertsPerNonAAQuad(); |
| 217 | maxVertex = (runningQuadCount + quadsInDraw) * GrResourceProvider::NumVertsPerNonAAQuad(); |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Robert Phillips | 8e083ee | 2019-11-01 15:07:15 -0400 | [diff] [blame] | 220 | mesh->setIndexed(std::move(indexBuffer), numIndicesToDraw, baseIndex, minVertex, maxVertex, |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 221 | GrPrimitiveRestart::kNo); |
| 222 | mesh->setVertexData(std::move(vertexBuffer), absVertBufferOffset); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 225 | ////////////////// VertexSpec Implementation |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 226 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 227 | int VertexSpec::deviceDimensionality() const { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 228 | return this->deviceQuadType() == GrQuad::Type::kPerspective ? 3 : 2; |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | int VertexSpec::localDimensionality() const { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 232 | return fHasLocalCoords ? (this->localQuadType() == GrQuad::Type::kPerspective ? 3 : 2) : 0; |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 233 | } |
| 234 | |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 235 | CoverageMode VertexSpec::coverageMode() const { |
| 236 | if (this->usesCoverageAA()) { |
| 237 | if (this->compatibleWithCoverageAsAlpha() && this->hasVertexColors() && |
| 238 | !this->requiresGeometryDomain()) { |
| 239 | // Using a geometric domain acts as a second source of coverage and folding |
| 240 | // the original coverage into color makes it impossible to apply the color's |
| 241 | // alpha to the geometric domain's coverage when the original shape is clipped. |
| 242 | return CoverageMode::kWithColor; |
| 243 | } else { |
| 244 | return CoverageMode::kWithPosition; |
| 245 | } |
| 246 | } else { |
| 247 | return CoverageMode::kNone; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // This needs to stay in sync w/ QuadPerEdgeAAGeometryProcessor::initializeAttrs |
| 252 | size_t VertexSpec::vertexSize() const { |
| 253 | bool needsPerspective = (this->deviceDimensionality() == 3); |
| 254 | CoverageMode coverageMode = this->coverageMode(); |
| 255 | |
| 256 | size_t count = 0; |
| 257 | |
| 258 | if (coverageMode == CoverageMode::kWithPosition) { |
| 259 | if (needsPerspective) { |
| 260 | count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType); |
| 261 | } else { |
| 262 | count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType) + |
| 263 | GrVertexAttribTypeSize(kFloat_GrVertexAttribType); |
| 264 | } |
| 265 | } else { |
| 266 | if (needsPerspective) { |
| 267 | count += GrVertexAttribTypeSize(kFloat3_GrVertexAttribType); |
| 268 | } else { |
| 269 | count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if (this->requiresGeometryDomain()) { |
| 274 | count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType); |
| 275 | } |
| 276 | |
| 277 | count += this->localDimensionality() * GrVertexAttribTypeSize(kFloat_GrVertexAttribType); |
| 278 | |
| 279 | if (ColorType::kByte == this->colorType()) { |
| 280 | count += GrVertexAttribTypeSize(kUByte4_norm_GrVertexAttribType); |
| 281 | } else if (ColorType::kHalf == this->colorType()) { |
| 282 | count += GrVertexAttribTypeSize(kHalf4_GrVertexAttribType); |
| 283 | } |
| 284 | |
| 285 | if (this->hasDomain()) { |
| 286 | count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType); |
| 287 | } |
| 288 | |
| 289 | return count; |
| 290 | } |
| 291 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 292 | ////////////////// Geometry Processor Implementation |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 293 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 294 | class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor { |
| 295 | public: |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 296 | using Saturate = GrTextureOp::Saturate; |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 297 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 298 | static sk_sp<GrGeometryProcessor> Make(const VertexSpec& spec) { |
| 299 | return sk_sp<QuadPerEdgeAAGeometryProcessor>(new QuadPerEdgeAAGeometryProcessor(spec)); |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 302 | static sk_sp<GrGeometryProcessor> Make(const VertexSpec& vertexSpec, const GrShaderCaps& caps, |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 303 | const GrBackendFormat& backendFormat, |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 304 | const GrSamplerState& samplerState, |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 305 | const GrSwizzle& swizzle, |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 306 | sk_sp<GrColorSpaceXform> textureColorSpaceXform, |
| 307 | Saturate saturate) { |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 308 | return sk_sp<QuadPerEdgeAAGeometryProcessor>(new QuadPerEdgeAAGeometryProcessor( |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 309 | vertexSpec, caps, backendFormat, samplerState, swizzle, |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 310 | std::move(textureColorSpaceXform), saturate)); |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 311 | } |
| 312 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 313 | const char* name() const override { return "QuadPerEdgeAAGeometryProcessor"; } |
Michael Ludwig | 024e262 | 2018-11-30 13:25:55 -0500 | [diff] [blame] | 314 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 315 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 316 | // texturing, device-dimensions are single bit flags |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 317 | uint32_t x = (fTexDomain.isInitialized() ? 0 : 0x1) |
| 318 | | (fSampler.isInitialized() ? 0 : 0x2) |
| 319 | | (fNeedsPerspective ? 0 : 0x4) |
| 320 | | (fSaturate == Saturate::kNo ? 0 : 0x8); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 321 | // local coords require 2 bits (3 choices), 00 for none, 01 for 2d, 10 for 3d |
| 322 | if (fLocalCoord.isInitialized()) { |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 323 | x |= kFloat3_GrVertexAttribType == fLocalCoord.cpuType() ? 0x10 : 0x20; |
Brian Osman | 78dc72c | 2018-12-03 13:20:43 +0000 | [diff] [blame] | 324 | } |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 325 | // similar for colors, 00 for none, 01 for bytes, 10 for half-floats |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 326 | if (fColor.isInitialized()) { |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 327 | x |= kUByte4_norm_GrVertexAttribType == fColor.cpuType() ? 0x40 : 0x80; |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 328 | } |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 329 | // and coverage mode, 00 for none, 01 for withposition, 10 for withcolor, 11 for |
| 330 | // position+geomdomain |
| 331 | SkASSERT(!fGeomDomain.isInitialized() || fCoverageMode == CoverageMode::kWithPosition); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 332 | if (fCoverageMode != CoverageMode::kNone) { |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 333 | x |= fGeomDomain.isInitialized() |
| 334 | ? 0x300 |
| 335 | : (CoverageMode::kWithPosition == fCoverageMode ? 0x100 : 0x200); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | b->add32(GrColorSpaceXform::XformKey(fTextureColorSpaceXform.get())); |
| 339 | b->add32(x); |
Brian Osman | 78dc72c | 2018-12-03 13:20:43 +0000 | [diff] [blame] | 340 | } |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 341 | |
| 342 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override { |
| 343 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
| 344 | public: |
| 345 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc, |
| 346 | FPCoordTransformIter&& transformIter) override { |
| 347 | const auto& gp = proc.cast<QuadPerEdgeAAGeometryProcessor>(); |
| 348 | if (gp.fLocalCoord.isInitialized()) { |
| 349 | this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter); |
| 350 | } |
| 351 | fTextureColorSpaceXformHelper.setData(pdman, gp.fTextureColorSpaceXform.get()); |
| 352 | } |
| 353 | |
| 354 | private: |
| 355 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 356 | using Interpolation = GrGLSLVaryingHandler::Interpolation; |
| 357 | |
| 358 | const auto& gp = args.fGP.cast<QuadPerEdgeAAGeometryProcessor>(); |
| 359 | fTextureColorSpaceXformHelper.emitCode(args.fUniformHandler, |
| 360 | gp.fTextureColorSpaceXform.get()); |
| 361 | |
| 362 | args.fVaryingHandler->emitAttributes(gp); |
| 363 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 364 | if (gp.fCoverageMode == CoverageMode::kWithPosition) { |
| 365 | // Strip last channel from the vertex attribute to remove coverage and get the |
| 366 | // actual position |
| 367 | if (gp.fNeedsPerspective) { |
| 368 | args.fVertBuilder->codeAppendf("float3 position = %s.xyz;", |
| 369 | gp.fPosition.name()); |
| 370 | } else { |
| 371 | args.fVertBuilder->codeAppendf("float2 position = %s.xy;", |
| 372 | gp.fPosition.name()); |
| 373 | } |
| 374 | gpArgs->fPositionVar = {"position", |
| 375 | gp.fNeedsPerspective ? kFloat3_GrSLType |
| 376 | : kFloat2_GrSLType, |
| 377 | GrShaderVar::kNone_TypeModifier}; |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 378 | } else { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 379 | // No coverage to eliminate |
| 380 | gpArgs->fPositionVar = gp.fPosition.asShaderVar(); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 381 | } |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 382 | |
| 383 | // Handle local coordinates if they exist |
| 384 | if (gp.fLocalCoord.isInitialized()) { |
| 385 | // NOTE: If the only usage of local coordinates is for the inline texture fetch |
| 386 | // before FPs, then there are no registered FPCoordTransforms and this ends up |
| 387 | // emitting nothing, so there isn't a duplication of local coordinates |
| 388 | this->emitTransforms(args.fVertBuilder, |
| 389 | args.fVaryingHandler, |
| 390 | args.fUniformHandler, |
| 391 | gp.fLocalCoord.asShaderVar(), |
| 392 | args.fFPCoordTransformHandler); |
| 393 | } |
| 394 | |
| 395 | // Solid color before any texturing gets modulated in |
| 396 | if (gp.fColor.isInitialized()) { |
Michael Ludwig | 3d2753e | 2019-03-29 14:36:32 -0400 | [diff] [blame] | 397 | SkASSERT(gp.fCoverageMode != CoverageMode::kWithColor || !gp.fNeedsPerspective); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 398 | // The color cannot be flat if the varying coverage has been modulated into it |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 399 | args.fVaryingHandler->addPassThroughAttribute(gp.fColor, args.fOutputColor, |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 400 | gp.fCoverageMode == CoverageMode::kWithColor ? |
| 401 | Interpolation::kInterpolated : Interpolation::kCanBeFlat); |
| 402 | } else { |
| 403 | // Output color must be initialized to something |
| 404 | args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputColor); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | // If there is a texture, must also handle texture coordinates and reading from |
| 408 | // the texture in the fragment shader before continuing to fragment processors. |
| 409 | if (gp.fSampler.isInitialized()) { |
| 410 | // Texture coordinates clamped by the domain on the fragment shader; if the GP |
| 411 | // has a texture, it's guaranteed to have local coordinates |
| 412 | args.fFragBuilder->codeAppend("float2 texCoord;"); |
| 413 | if (gp.fLocalCoord.cpuType() == kFloat3_GrVertexAttribType) { |
| 414 | // Can't do a pass through since we need to perform perspective division |
| 415 | GrGLSLVarying v(gp.fLocalCoord.gpuType()); |
| 416 | args.fVaryingHandler->addVarying(gp.fLocalCoord.name(), &v); |
| 417 | args.fVertBuilder->codeAppendf("%s = %s;", |
| 418 | v.vsOut(), gp.fLocalCoord.name()); |
| 419 | args.fFragBuilder->codeAppendf("texCoord = %s.xy / %s.z;", |
| 420 | v.fsIn(), v.fsIn()); |
| 421 | } else { |
| 422 | args.fVaryingHandler->addPassThroughAttribute(gp.fLocalCoord, "texCoord"); |
| 423 | } |
| 424 | |
| 425 | // Clamp the now 2D localCoordName variable by the domain if it is provided |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 426 | if (gp.fTexDomain.isInitialized()) { |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 427 | args.fFragBuilder->codeAppend("float4 domain;"); |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 428 | args.fVaryingHandler->addPassThroughAttribute(gp.fTexDomain, "domain", |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 429 | Interpolation::kCanBeFlat); |
| 430 | args.fFragBuilder->codeAppend( |
| 431 | "texCoord = clamp(texCoord, domain.xy, domain.zw);"); |
| 432 | } |
| 433 | |
| 434 | // Now modulate the starting output color by the texture lookup |
| 435 | args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor); |
| 436 | args.fFragBuilder->appendTextureLookupAndModulate( |
| 437 | args.fOutputColor, args.fTexSamplers[0], "texCoord", kFloat2_GrSLType, |
| 438 | &fTextureColorSpaceXformHelper); |
| 439 | args.fFragBuilder->codeAppend(";"); |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 440 | if (gp.fSaturate == Saturate::kYes) { |
| 441 | args.fFragBuilder->codeAppendf("%s = saturate(%s);", |
| 442 | args.fOutputColor, args.fOutputColor); |
| 443 | } |
| 444 | } else { |
| 445 | // Saturate is only intended for use with a proxy to account for the fact |
| 446 | // that GrTextureOp skips SkPaint conversion, which normally handles this. |
| 447 | SkASSERT(gp.fSaturate == Saturate::kNo); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // And lastly, output the coverage calculation code |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 451 | if (gp.fCoverageMode == CoverageMode::kWithPosition) { |
| 452 | GrGLSLVarying coverage(kFloat_GrSLType); |
| 453 | args.fVaryingHandler->addVarying("coverage", &coverage); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 454 | if (gp.fNeedsPerspective) { |
Michael Ludwig | 3d2753e | 2019-03-29 14:36:32 -0400 | [diff] [blame] | 455 | // Multiply by "W" in the vertex shader, then by 1/w (sk_FragCoord.w) in |
| 456 | // the fragment shader to get screen-space linear coverage. |
| 457 | args.fVertBuilder->codeAppendf("%s = %s.w * %s.z;", |
| 458 | coverage.vsOut(), gp.fPosition.name(), |
| 459 | gp.fPosition.name()); |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 460 | args.fFragBuilder->codeAppendf("float coverage = %s * sk_FragCoord.w;", |
| 461 | coverage.fsIn()); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 462 | } else { |
Jim Van Verth | d5d9c21 | 2019-05-02 10:22:10 -0400 | [diff] [blame] | 463 | args.fVertBuilder->codeAppendf("%s = %s;", |
| 464 | coverage.vsOut(), gp.fCoverage.name()); |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 465 | args.fFragBuilder->codeAppendf("float coverage = %s;", coverage.fsIn()); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 466 | } |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 467 | |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 468 | if (gp.fGeomDomain.isInitialized()) { |
| 469 | // Calculate distance from sk_FragCoord to the 4 edges of the domain |
| 470 | // and clamp them to (0, 1). Use the minimum of these and the original |
| 471 | // coverage. This only has to be done in the exterior triangles, the |
| 472 | // interior of the quad geometry can never be clipped by the domain box. |
| 473 | args.fFragBuilder->codeAppend("float4 geoDomain;"); |
| 474 | args.fVaryingHandler->addPassThroughAttribute(gp.fGeomDomain, "geoDomain", |
| 475 | Interpolation::kCanBeFlat); |
| 476 | args.fFragBuilder->codeAppend( |
| 477 | "if (coverage < 0.5) {" |
| 478 | " float4 dists4 = clamp(float4(1, 1, -1, -1) * " |
| 479 | "(sk_FragCoord.xyxy - geoDomain), 0, 1);" |
| 480 | " float2 dists2 = dists4.xy * dists4.zw;" |
| 481 | " coverage = min(coverage, dists2.x * dists2.y);" |
| 482 | "}"); |
| 483 | } |
| 484 | |
| 485 | args.fFragBuilder->codeAppendf("%s = half4(half(coverage));", |
| 486 | args.fOutputCoverage); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 487 | } else { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 488 | // Set coverage to 1, since it's either non-AA or the coverage was already |
| 489 | // folded into the output color |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 490 | SkASSERT(!gp.fGeomDomain.isInitialized()); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 491 | args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | GrGLSLColorSpaceXformHelper fTextureColorSpaceXformHelper; |
| 495 | }; |
| 496 | return new GLSLProcessor; |
| 497 | } |
| 498 | |
| 499 | private: |
| 500 | QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec) |
| 501 | : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID) |
| 502 | , fTextureColorSpaceXform(nullptr) { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 503 | SkASSERT(!spec.hasDomain()); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 504 | this->initializeAttrs(spec); |
| 505 | this->setTextureSamplerCnt(0); |
| 506 | } |
| 507 | |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 508 | QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec, |
| 509 | const GrShaderCaps& caps, |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 510 | const GrBackendFormat& backendFormat, |
Greg Daniel | 7a82edf | 2018-12-04 10:54:34 -0500 | [diff] [blame] | 511 | const GrSamplerState& samplerState, |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 512 | const GrSwizzle& swizzle, |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 513 | sk_sp<GrColorSpaceXform> textureColorSpaceXform, |
| 514 | Saturate saturate) |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 515 | : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID) |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 516 | , fSaturate(saturate) |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 517 | , fTextureColorSpaceXform(std::move(textureColorSpaceXform)) |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 518 | , fSampler(samplerState, backendFormat, swizzle) { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 519 | SkASSERT(spec.hasLocalCoords()); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 520 | this->initializeAttrs(spec); |
| 521 | this->setTextureSamplerCnt(1); |
| 522 | } |
| 523 | |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 524 | // This needs to stay in sync w/ VertexSpec::vertexSize |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 525 | void initializeAttrs(const VertexSpec& spec) { |
| 526 | fNeedsPerspective = spec.deviceDimensionality() == 3; |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 527 | fCoverageMode = spec.coverageMode(); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 528 | |
| 529 | if (fCoverageMode == CoverageMode::kWithPosition) { |
| 530 | if (fNeedsPerspective) { |
| 531 | fPosition = {"positionWithCoverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType}; |
| 532 | } else { |
Jim Van Verth | d5d9c21 | 2019-05-02 10:22:10 -0400 | [diff] [blame] | 533 | fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 534 | fCoverage = {"coverage", kFloat_GrVertexAttribType, kFloat_GrSLType}; |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 535 | } |
| 536 | } else { |
| 537 | if (fNeedsPerspective) { |
| 538 | fPosition = {"position", kFloat3_GrVertexAttribType, kFloat3_GrSLType}; |
| 539 | } else { |
| 540 | fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 541 | } |
| 542 | } |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 543 | |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 544 | // Need a geometry domain when the quads are AA and not rectilinear, since their AA |
| 545 | // outsetting can go beyond a half pixel. |
| 546 | if (spec.requiresGeometryDomain()) { |
| 547 | fGeomDomain = {"geomDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType}; |
| 548 | } |
| 549 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 550 | int localDim = spec.localDimensionality(); |
| 551 | if (localDim == 3) { |
| 552 | fLocalCoord = {"localCoord", kFloat3_GrVertexAttribType, kFloat3_GrSLType}; |
| 553 | } else if (localDim == 2) { |
| 554 | fLocalCoord = {"localCoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 555 | } // else localDim == 0 and attribute remains uninitialized |
| 556 | |
| 557 | if (ColorType::kByte == spec.colorType()) { |
| 558 | fColor = {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType}; |
| 559 | } else if (ColorType::kHalf == spec.colorType()) { |
| 560 | fColor = {"color", kHalf4_GrVertexAttribType, kHalf4_GrSLType}; |
| 561 | } |
| 562 | |
| 563 | if (spec.hasDomain()) { |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 564 | fTexDomain = {"texDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType}; |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Jim Van Verth | d5d9c21 | 2019-05-02 10:22:10 -0400 | [diff] [blame] | 567 | this->setVertexAttributes(&fPosition, 6); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | const TextureSampler& onTextureSampler(int) const override { return fSampler; } |
| 571 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 572 | Attribute fPosition; // May contain coverage as last channel |
Jim Van Verth | d5d9c21 | 2019-05-02 10:22:10 -0400 | [diff] [blame] | 573 | Attribute fCoverage; // Used for non-perspective position to avoid Intel Metal issues |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 574 | Attribute fColor; // May have coverage modulated in if the FPs support it |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 575 | Attribute fLocalCoord; |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 576 | Attribute fGeomDomain; // Screen-space bounding box on geometry+aa outset |
| 577 | Attribute fTexDomain; // Texture-space bounding box on local coords |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 578 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 579 | // The positions attribute may have coverage built into it, so float3 is an ambiguous type |
| 580 | // and may mean 2d with coverage, or 3d with no coverage |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 581 | bool fNeedsPerspective; |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 582 | // Should saturate() be called on the color? Only relevant when created with a texture. |
| 583 | Saturate fSaturate = Saturate::kNo; |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 584 | CoverageMode fCoverageMode; |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 585 | |
| 586 | // Color space will be null and fSampler.isInitialized() returns false when the GP is configured |
| 587 | // to skip texturing. |
| 588 | sk_sp<GrColorSpaceXform> fTextureColorSpaceXform; |
| 589 | TextureSampler fSampler; |
| 590 | |
| 591 | typedef GrGeometryProcessor INHERITED; |
| 592 | }; |
| 593 | |
| 594 | sk_sp<GrGeometryProcessor> MakeProcessor(const VertexSpec& spec) { |
| 595 | return QuadPerEdgeAAGeometryProcessor::Make(spec); |
| 596 | } |
| 597 | |
| 598 | sk_sp<GrGeometryProcessor> MakeTexturedProcessor(const VertexSpec& spec, const GrShaderCaps& caps, |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 599 | const GrBackendFormat& backendFormat, |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 600 | const GrSamplerState& samplerState, |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 601 | const GrSwizzle& swizzle, |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 602 | sk_sp<GrColorSpaceXform> textureColorSpaceXform, |
| 603 | Saturate saturate) { |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 604 | return QuadPerEdgeAAGeometryProcessor::Make(spec, caps, backendFormat, samplerState, swizzle, |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 605 | std::move(textureColorSpaceXform), saturate); |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 606 | } |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 607 | |
| 608 | } // namespace GrQuadPerEdgeAA |