Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCCFiller.h" |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkPath.h" |
| 11 | #include "include/core/SkPoint.h" |
| 12 | #include "src/core/SkMathPriv.h" |
| 13 | #include "src/core/SkPathPriv.h" |
| 14 | #include "src/gpu/GrCaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrOnFlushResourceProvider.h" |
| 16 | #include "src/gpu/GrOpFlushState.h" |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 17 | #include <stdlib.h> |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 18 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 19 | using TriPointInstance = GrCCCoverageProcessor::TriPointInstance; |
| 20 | using QuadPointInstance = GrCCCoverageProcessor::QuadPointInstance; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 21 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 22 | GrCCFiller::GrCCFiller(Algorithm algorithm, int numPaths, int numSkPoints, int numSkVerbs, |
| 23 | int numConicWeights) |
| 24 | : fAlgorithm(algorithm) |
| 25 | , fGeometry(numSkPoints, numSkVerbs, numConicWeights) |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 26 | , fPathInfos(numPaths) |
Chris Dalton | 3917b1e | 2018-05-09 00:40:52 -0600 | [diff] [blame] | 27 | , fScissorSubBatches(numPaths) |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 28 | , fTotalPrimitiveCounts{PrimitiveTallies(), PrimitiveTallies()} { |
| 29 | // Batches decide what to draw by looking where the previous one ended. Define initial batches |
| 30 | // that "end" at the beginning of the data. These will not be drawn, but will only be be read by |
| 31 | // the first actual batch. |
| 32 | fScissorSubBatches.push_back() = {PrimitiveTallies(), SkIRect::MakeEmpty()}; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 33 | fBatches.push_back() = {PrimitiveTallies(), fScissorSubBatches.count(), PrimitiveTallies()}; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 36 | void GrCCFiller::parseDeviceSpaceFill(const SkPath& path, const SkPoint* deviceSpacePts, |
| 37 | GrScissorTest scissorTest, const SkIRect& clippedDevIBounds, |
| 38 | const SkIVector& devToAtlasOffset) { |
| 39 | SkASSERT(!fInstanceBuffer); // Can't call after prepareToDraw(). |
| 40 | SkASSERT(!path.isEmpty()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 41 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 42 | int currPathPointsIdx = fGeometry.points().count(); |
| 43 | int currPathVerbsIdx = fGeometry.verbs().count(); |
| 44 | PrimitiveTallies currPathPrimitiveCounts = PrimitiveTallies(); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 45 | |
| 46 | fGeometry.beginPath(); |
| 47 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 48 | const float* conicWeights = SkPathPriv::ConicWeightData(path); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 49 | int ptsIdx = 0; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 50 | int conicWeightsIdx = 0; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 51 | bool insideContour = false; |
| 52 | |
| 53 | for (SkPath::Verb verb : SkPathPriv::Verbs(path)) { |
| 54 | switch (verb) { |
| 55 | case SkPath::kMove_Verb: |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 56 | if (insideContour) { |
| 57 | currPathPrimitiveCounts += fGeometry.endContour(); |
| 58 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 59 | fGeometry.beginContour(deviceSpacePts[ptsIdx]); |
| 60 | ++ptsIdx; |
| 61 | insideContour = true; |
| 62 | continue; |
| 63 | case SkPath::kClose_Verb: |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 64 | if (insideContour) { |
| 65 | currPathPrimitiveCounts += fGeometry.endContour(); |
| 66 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 67 | insideContour = false; |
| 68 | continue; |
| 69 | case SkPath::kLine_Verb: |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 70 | fGeometry.lineTo(&deviceSpacePts[ptsIdx - 1]); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 71 | ++ptsIdx; |
| 72 | continue; |
| 73 | case SkPath::kQuad_Verb: |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 74 | fGeometry.quadraticTo(&deviceSpacePts[ptsIdx - 1]); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 75 | ptsIdx += 2; |
| 76 | continue; |
| 77 | case SkPath::kCubic_Verb: |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 78 | fGeometry.cubicTo(&deviceSpacePts[ptsIdx - 1]); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 79 | ptsIdx += 3; |
| 80 | continue; |
| 81 | case SkPath::kConic_Verb: |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 82 | fGeometry.conicTo(&deviceSpacePts[ptsIdx - 1], conicWeights[conicWeightsIdx]); |
| 83 | ptsIdx += 2; |
| 84 | ++conicWeightsIdx; |
| 85 | continue; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 86 | default: |
| 87 | SK_ABORT("Unexpected path verb."); |
| 88 | } |
| 89 | } |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 90 | SkASSERT(ptsIdx == path.countPoints()); |
| 91 | SkASSERT(conicWeightsIdx == SkPathPriv::ConicWeightCnt(path)); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 92 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 93 | if (insideContour) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 94 | currPathPrimitiveCounts += fGeometry.endContour(); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 95 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 96 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 97 | fPathInfos.emplace_back(scissorTest, devToAtlasOffset); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 98 | |
| 99 | // Tessellate fans from very large and/or simple paths, in order to reduce overdraw. |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 100 | int numVerbs = fGeometry.verbs().count() - currPathVerbsIdx - 1; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 101 | int64_t tessellationWork = (int64_t)numVerbs * (32 - SkCLZ(numVerbs)); // N log N. |
| 102 | int64_t fanningWork = (int64_t)clippedDevIBounds.height() * clippedDevIBounds.width(); |
| 103 | if (tessellationWork * (50*50) + (100*100) < fanningWork) { // Don't tessellate under 100x100. |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 104 | fPathInfos.back().tessellateFan( |
| 105 | fAlgorithm, path, fGeometry, currPathVerbsIdx, currPathPointsIdx, clippedDevIBounds, |
| 106 | &currPathPrimitiveCounts); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 109 | fTotalPrimitiveCounts[(int)scissorTest] += currPathPrimitiveCounts; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 110 | |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 111 | if (GrScissorTest::kEnabled == scissorTest) { |
| 112 | fScissorSubBatches.push_back() = {fTotalPrimitiveCounts[(int)GrScissorTest::kEnabled], |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 113 | clippedDevIBounds.makeOffset(devToAtlasOffset.fX, |
| 114 | devToAtlasOffset.fY)}; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 115 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 118 | void GrCCFiller::PathInfo::tessellateFan( |
| 119 | Algorithm algorithm, const SkPath& originalPath, const GrCCFillGeometry& geometry, |
| 120 | int verbsIdx, int ptsIdx, const SkIRect& clippedDevIBounds, |
| 121 | PrimitiveTallies* newTriangleCounts) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 122 | using Verb = GrCCFillGeometry::Verb; |
| 123 | SkASSERT(-1 == fFanTessellationCount); |
| 124 | SkASSERT(!fFanTessellation); |
| 125 | |
| 126 | const SkTArray<Verb, true>& verbs = geometry.verbs(); |
| 127 | const SkTArray<SkPoint, true>& pts = geometry.points(); |
| 128 | |
| 129 | newTriangleCounts->fTriangles = |
| 130 | newTriangleCounts->fWeightedTriangles = 0; |
| 131 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 132 | // Build an SkPath of the Redbook fan. |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 133 | SkPath fan; |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 134 | if (Algorithm::kCoverageCount == algorithm) { |
| 135 | // We use "winding" fill type right now because we are producing a coverage count, and must |
| 136 | // fill in every region that has non-zero wind. The path processor will convert coverage |
| 137 | // count to the appropriate fill type later. |
Mike Reed | 4241f5e | 2019-09-14 19:13:23 +0000 | [diff] [blame] | 138 | fan.setFillType(SkPath::kWinding_FillType); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 139 | } else { |
Chris Dalton | c096654 | 2019-09-23 20:16:03 -0600 | [diff] [blame^] | 140 | // When counting winding numbers in the stencil buffer, it works to use even/odd for the fan |
| 141 | // tessellation (where applicable). But we need to strip out inverse fill info because |
| 142 | // inverse-ness gets accounted for later on. |
| 143 | fan.setFillType(SkPath::ConvertToNonInverseFillType(originalPath.getFillType())); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 144 | } |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 145 | SkASSERT(Verb::kBeginPath == verbs[verbsIdx]); |
| 146 | for (int i = verbsIdx + 1; i < verbs.count(); ++i) { |
| 147 | switch (verbs[i]) { |
| 148 | case Verb::kBeginPath: |
| 149 | SK_ABORT("Invalid GrCCFillGeometry"); |
| 150 | continue; |
| 151 | |
| 152 | case Verb::kBeginContour: |
| 153 | fan.moveTo(pts[ptsIdx++]); |
| 154 | continue; |
| 155 | |
| 156 | case Verb::kLineTo: |
| 157 | fan.lineTo(pts[ptsIdx++]); |
| 158 | continue; |
| 159 | |
| 160 | case Verb::kMonotonicQuadraticTo: |
| 161 | case Verb::kMonotonicConicTo: |
| 162 | fan.lineTo(pts[ptsIdx + 1]); |
| 163 | ptsIdx += 2; |
| 164 | continue; |
| 165 | |
| 166 | case Verb::kMonotonicCubicTo: |
| 167 | fan.lineTo(pts[ptsIdx + 2]); |
| 168 | ptsIdx += 3; |
| 169 | continue; |
| 170 | |
| 171 | case Verb::kEndClosedContour: |
| 172 | case Verb::kEndOpenContour: |
| 173 | fan.close(); |
| 174 | continue; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | GrTessellator::WindingVertex* vertices = nullptr; |
Chris Dalton | c096654 | 2019-09-23 20:16:03 -0600 | [diff] [blame^] | 179 | SkASSERT(!fan.isInverseFillType()); |
| 180 | fFanTessellationCount = GrTessellator::PathToVertices( |
| 181 | fan, std::numeric_limits<float>::infinity(), SkRect::Make(clippedDevIBounds), |
| 182 | &vertices); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 183 | if (fFanTessellationCount <= 0) { |
| 184 | SkASSERT(0 == fFanTessellationCount); |
| 185 | SkASSERT(nullptr == vertices); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | SkASSERT(0 == fFanTessellationCount % 3); |
| 190 | for (int i = 0; i < fFanTessellationCount; i += 3) { |
| 191 | int tessWinding = vertices[i].fWinding; |
| 192 | SkASSERT(tessWinding == vertices[i + 1].fWinding); |
| 193 | SkASSERT(tessWinding == vertices[i + 2].fWinding); |
| 194 | |
| 195 | // Ensure this triangle's points actually wind in the same direction as tessWinding. |
| 196 | // CCPR shaders use the sign of wind to determine which direction to bloat, so even for |
| 197 | // "wound" triangles the winding sign and point ordering need to agree. |
| 198 | float ax = vertices[i].fPos.fX - vertices[i + 1].fPos.fX; |
| 199 | float ay = vertices[i].fPos.fY - vertices[i + 1].fPos.fY; |
| 200 | float bx = vertices[i].fPos.fX - vertices[i + 2].fPos.fX; |
| 201 | float by = vertices[i].fPos.fY - vertices[i + 2].fPos.fY; |
| 202 | float wind = ax*by - ay*bx; |
| 203 | if ((wind > 0) != (-tessWinding > 0)) { // Tessellator has opposite winding sense. |
| 204 | std::swap(vertices[i + 1].fPos, vertices[i + 2].fPos); |
| 205 | } |
| 206 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 207 | int weight = abs(tessWinding); |
| 208 | SkASSERT(SkPath::kEvenOdd_FillType != fan.getFillType() || weight == 1); |
| 209 | if (weight > 1 && Algorithm::kCoverageCount == algorithm) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 210 | ++newTriangleCounts->fWeightedTriangles; |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 211 | } else { |
| 212 | newTriangleCounts->fTriangles += weight; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | fFanTessellation.reset(vertices); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 219 | GrCCFiller::BatchID GrCCFiller::closeCurrentBatch() { |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 220 | SkASSERT(!fInstanceBuffer); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 221 | SkASSERT(!fBatches.empty()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 222 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 223 | const auto& lastBatch = fBatches.back(); |
Chris Dalton | a883aca | 2018-03-08 23:05:30 -0700 | [diff] [blame] | 224 | int maxMeshes = 1 + fScissorSubBatches.count() - lastBatch.fEndScissorSubBatchIdx; |
| 225 | fMaxMeshesPerDraw = SkTMax(fMaxMeshesPerDraw, maxMeshes); |
| 226 | |
| 227 | const auto& lastScissorSubBatch = fScissorSubBatches[lastBatch.fEndScissorSubBatchIdx - 1]; |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 228 | PrimitiveTallies batchTotalCounts = fTotalPrimitiveCounts[(int)GrScissorTest::kDisabled] - |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 229 | lastBatch.fEndNonScissorIndices; |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 230 | batchTotalCounts += fTotalPrimitiveCounts[(int)GrScissorTest::kEnabled] - |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 231 | lastScissorSubBatch.fEndPrimitiveIndices; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 232 | |
Chris Dalton | a883aca | 2018-03-08 23:05:30 -0700 | [diff] [blame] | 233 | // This will invalidate lastBatch. |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 234 | fBatches.push_back() = { |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 235 | fTotalPrimitiveCounts[(int)GrScissorTest::kDisabled], |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 236 | fScissorSubBatches.count(), |
| 237 | batchTotalCounts |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 238 | }; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 239 | return fBatches.count() - 1; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // Emits a contour's triangle fan. |
| 243 | // |
| 244 | // Classic Redbook fanning would be the triangles: [0 1 2], [0 2 3], ..., [0 n-2 n-1]. |
| 245 | // |
| 246 | // This function emits the triangle: [0 n/3 n*2/3], and then recurses on all three sides. The |
| 247 | // advantage to this approach is that for a convex-ish contour, it generates larger triangles. |
| 248 | // Classic fanning tends to generate long, skinny triangles, which are expensive to draw since they |
| 249 | // have a longer perimeter to rasterize and antialias. |
| 250 | // |
| 251 | // The indices array indexes the fan's points (think: glDrawElements), and must have at least log3 |
| 252 | // elements past the end for this method to use as scratch space. |
| 253 | // |
| 254 | // Returns the next triangle instance after the final one emitted. |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 255 | static TriPointInstance* emit_recursive_fan( |
| 256 | const SkTArray<SkPoint, true>& pts, SkTArray<int32_t, true>& indices, int firstIndex, |
| 257 | int indexCount, const Sk2f& devToAtlasOffset, TriPointInstance::Ordering ordering, |
| 258 | TriPointInstance out[]) { |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 259 | if (indexCount < 3) { |
| 260 | return out; |
| 261 | } |
| 262 | |
| 263 | int32_t oneThirdCount = indexCount / 3; |
| 264 | int32_t twoThirdsCount = (2 * indexCount) / 3; |
| 265 | out++->set(pts[indices[firstIndex]], pts[indices[firstIndex + oneThirdCount]], |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 266 | pts[indices[firstIndex + twoThirdsCount]], devToAtlasOffset, ordering); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 267 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 268 | out = emit_recursive_fan( |
| 269 | pts, indices, firstIndex, oneThirdCount + 1, devToAtlasOffset, ordering, out); |
| 270 | out = emit_recursive_fan( |
| 271 | pts, indices, firstIndex + oneThirdCount, twoThirdsCount - oneThirdCount + 1, |
| 272 | devToAtlasOffset, ordering, out); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 273 | |
| 274 | int endIndex = firstIndex + indexCount; |
| 275 | int32_t oldValue = indices[endIndex]; |
| 276 | indices[endIndex] = indices[firstIndex]; |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 277 | out = emit_recursive_fan( |
| 278 | pts, indices, firstIndex + twoThirdsCount, indexCount - twoThirdsCount + 1, |
| 279 | devToAtlasOffset, ordering, out); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 280 | indices[endIndex] = oldValue; |
| 281 | |
| 282 | return out; |
| 283 | } |
| 284 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 285 | void GrCCFiller::emitTessellatedFan( |
| 286 | const GrTessellator::WindingVertex* vertices, int numVertices, const Sk2f& devToAtlasOffset, |
| 287 | TriPointInstance::Ordering ordering, TriPointInstance* triPointInstanceData, |
| 288 | QuadPointInstance* quadPointInstanceData, GrCCFillGeometry::PrimitiveTallies* indices) { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 289 | for (int i = 0; i < numVertices; i += 3) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 290 | int weight = abs(vertices[i].fWinding); |
| 291 | SkASSERT(weight >= 1); |
| 292 | if (weight > 1 && Algorithm::kStencilWindingCount != fAlgorithm) { |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 293 | quadPointInstanceData[indices->fWeightedTriangles++].setW( |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 294 | vertices[i].fPos, vertices[i+1].fPos, vertices[i + 2].fPos, devToAtlasOffset, |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 295 | static_cast<float>(abs(vertices[i].fWinding))); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 296 | } else for (int j = 0; j < weight; ++j) { |
| 297 | // Unfortunately, there is not a way to increment stencil values by an amount larger |
| 298 | // than 1. Instead we draw the triangle 'weight' times. |
| 299 | triPointInstanceData[indices->fTriangles++].set( |
| 300 | vertices[i].fPos, vertices[i + 1].fPos, vertices[i + 2].fPos, devToAtlasOffset, |
| 301 | ordering); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 306 | bool GrCCFiller::prepareToDraw(GrOnFlushResourceProvider* onFlushRP) { |
| 307 | using Verb = GrCCFillGeometry::Verb; |
| 308 | SkASSERT(!fInstanceBuffer); |
| 309 | SkASSERT(fBatches.back().fEndNonScissorIndices == // Call closeCurrentBatch(). |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 310 | fTotalPrimitiveCounts[(int)GrScissorTest::kDisabled]); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 311 | SkASSERT(fBatches.back().fEndScissorSubBatchIdx == fScissorSubBatches.count()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 312 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 313 | auto triangleOrdering = (Algorithm::kCoverageCount == fAlgorithm) |
| 314 | ? TriPointInstance::Ordering::kXYTransposed |
| 315 | : TriPointInstance::Ordering::kXYInterleaved; |
| 316 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 317 | // Here we build a single instance buffer to share with every internal batch. |
| 318 | // |
| 319 | // CCPR processs 3 different types of primitives: triangles, quadratics, cubics. Each primitive |
| 320 | // type is further divided into instances that require a scissor and those that don't. This |
| 321 | // leaves us with 3*2 = 6 independent instance arrays to build for the GPU. |
| 322 | // |
| 323 | // Rather than place each instance array in its own GPU buffer, we allocate a single |
| 324 | // megabuffer and lay them all out side-by-side. We can offset the "baseInstance" parameter in |
| 325 | // our draw calls to direct the GPU to the applicable elements within a given array. |
| 326 | // |
| 327 | // We already know how big to make each of the 6 arrays from fTotalPrimitiveCounts, so layout is |
| 328 | // straightforward. Start with triangles and quadratics. They both view the instance buffer as |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 329 | // an array of TriPointInstance[], so we can begin at zero and lay them out one after the other. |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 330 | fBaseInstances[0].fTriangles = 0; |
| 331 | fBaseInstances[1].fTriangles = fBaseInstances[0].fTriangles + |
| 332 | fTotalPrimitiveCounts[0].fTriangles; |
| 333 | fBaseInstances[0].fQuadratics = fBaseInstances[1].fTriangles + |
| 334 | fTotalPrimitiveCounts[1].fTriangles; |
| 335 | fBaseInstances[1].fQuadratics = fBaseInstances[0].fQuadratics + |
| 336 | fTotalPrimitiveCounts[0].fQuadratics; |
| 337 | int triEndIdx = fBaseInstances[1].fQuadratics + fTotalPrimitiveCounts[1].fQuadratics; |
| 338 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 339 | // Wound triangles and cubics both view the same instance buffer as an array of |
| 340 | // QuadPointInstance[]. So, reinterpreting the instance data as QuadPointInstance[], we start |
| 341 | // them on the first index that will not overwrite previous TriPointInstance data. |
| 342 | int quadBaseIdx = |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 343 | GrSizeDivRoundUp(triEndIdx * sizeof(TriPointInstance), sizeof(QuadPointInstance)); |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 344 | fBaseInstances[0].fWeightedTriangles = quadBaseIdx; |
| 345 | fBaseInstances[1].fWeightedTriangles = fBaseInstances[0].fWeightedTriangles + |
| 346 | fTotalPrimitiveCounts[0].fWeightedTriangles; |
| 347 | fBaseInstances[0].fCubics = fBaseInstances[1].fWeightedTriangles + |
| 348 | fTotalPrimitiveCounts[1].fWeightedTriangles; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 349 | fBaseInstances[1].fCubics = fBaseInstances[0].fCubics + fTotalPrimitiveCounts[0].fCubics; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 350 | fBaseInstances[0].fConics = fBaseInstances[1].fCubics + fTotalPrimitiveCounts[1].fCubics; |
| 351 | fBaseInstances[1].fConics = fBaseInstances[0].fConics + fTotalPrimitiveCounts[0].fConics; |
| 352 | int quadEndIdx = fBaseInstances[1].fConics + fTotalPrimitiveCounts[1].fConics; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 353 | |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 354 | fInstanceBuffer = |
| 355 | onFlushRP->makeBuffer(GrGpuBufferType::kVertex, quadEndIdx * sizeof(QuadPointInstance)); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 356 | if (!fInstanceBuffer) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 357 | SkDebugf("WARNING: failed to allocate CCPR fill instance buffer.\n"); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 358 | return false; |
| 359 | } |
| 360 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 361 | TriPointInstance* triPointInstanceData = static_cast<TriPointInstance*>(fInstanceBuffer->map()); |
| 362 | QuadPointInstance* quadPointInstanceData = |
| 363 | reinterpret_cast<QuadPointInstance*>(triPointInstanceData); |
| 364 | SkASSERT(quadPointInstanceData); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 365 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 366 | PathInfo* nextPathInfo = fPathInfos.begin(); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 367 | Sk2f devToAtlasOffset; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 368 | PrimitiveTallies instanceIndices[2] = {fBaseInstances[0], fBaseInstances[1]}; |
| 369 | PrimitiveTallies* currIndices = nullptr; |
| 370 | SkSTArray<256, int32_t, true> currFan; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 371 | bool currFanIsTessellated = false; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 372 | |
| 373 | const SkTArray<SkPoint, true>& pts = fGeometry.points(); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 374 | int ptsIdx = -1; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 375 | int nextConicWeightIdx = 0; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 376 | |
| 377 | // Expand the ccpr verbs into GPU instance buffers. |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 378 | for (Verb verb : fGeometry.verbs()) { |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 379 | switch (verb) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 380 | case Verb::kBeginPath: |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 381 | SkASSERT(currFan.empty()); |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 382 | currIndices = &instanceIndices[(int)nextPathInfo->scissorTest()]; |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 383 | devToAtlasOffset = Sk2f(static_cast<float>(nextPathInfo->devToAtlasOffset().fX), |
| 384 | static_cast<float>(nextPathInfo->devToAtlasOffset().fY)); |
Chris Dalton | ad06544 | 2018-03-08 22:41:33 -0700 | [diff] [blame] | 385 | currFanIsTessellated = nextPathInfo->hasFanTessellation(); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 386 | if (currFanIsTessellated) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 387 | this->emitTessellatedFan( |
| 388 | nextPathInfo->fanTessellation(), nextPathInfo->fanTessellationCount(), |
| 389 | devToAtlasOffset, triangleOrdering, triPointInstanceData, |
| 390 | quadPointInstanceData, currIndices); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 391 | } |
| 392 | ++nextPathInfo; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 393 | continue; |
| 394 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 395 | case Verb::kBeginContour: |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 396 | SkASSERT(currFan.empty()); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 397 | ++ptsIdx; |
| 398 | if (!currFanIsTessellated) { |
| 399 | currFan.push_back(ptsIdx); |
| 400 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 401 | continue; |
| 402 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 403 | case Verb::kLineTo: |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 404 | ++ptsIdx; |
| 405 | if (!currFanIsTessellated) { |
| 406 | SkASSERT(!currFan.empty()); |
| 407 | currFan.push_back(ptsIdx); |
| 408 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 409 | continue; |
| 410 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 411 | case Verb::kMonotonicQuadraticTo: |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 412 | triPointInstanceData[currIndices->fQuadratics++].set( |
| 413 | &pts[ptsIdx], devToAtlasOffset, TriPointInstance::Ordering::kXYTransposed); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 414 | ptsIdx += 2; |
| 415 | if (!currFanIsTessellated) { |
| 416 | SkASSERT(!currFan.empty()); |
| 417 | currFan.push_back(ptsIdx); |
| 418 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 419 | continue; |
| 420 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 421 | case Verb::kMonotonicCubicTo: |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 422 | quadPointInstanceData[currIndices->fCubics++].set( |
| 423 | &pts[ptsIdx], devToAtlasOffset[0], devToAtlasOffset[1]); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 424 | ptsIdx += 3; |
| 425 | if (!currFanIsTessellated) { |
| 426 | SkASSERT(!currFan.empty()); |
| 427 | currFan.push_back(ptsIdx); |
| 428 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 429 | continue; |
| 430 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 431 | case Verb::kMonotonicConicTo: |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 432 | quadPointInstanceData[currIndices->fConics++].setW( |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 433 | &pts[ptsIdx], devToAtlasOffset, |
| 434 | fGeometry.getConicWeight(nextConicWeightIdx)); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 435 | ptsIdx += 2; |
| 436 | ++nextConicWeightIdx; |
| 437 | if (!currFanIsTessellated) { |
| 438 | SkASSERT(!currFan.empty()); |
| 439 | currFan.push_back(ptsIdx); |
| 440 | } |
| 441 | continue; |
| 442 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 443 | case Verb::kEndClosedContour: // endPt == startPt. |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 444 | if (!currFanIsTessellated) { |
| 445 | SkASSERT(!currFan.empty()); |
| 446 | currFan.pop_back(); |
| 447 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 448 | // fallthru. |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 449 | case Verb::kEndOpenContour: // endPt != startPt. |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 450 | SkASSERT(!currFanIsTessellated || currFan.empty()); |
| 451 | if (!currFanIsTessellated && currFan.count() >= 3) { |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 452 | int fanSize = currFan.count(); |
| 453 | // Reserve space for emit_recursive_fan. Technically this can grow to |
| 454 | // fanSize + log3(fanSize), but we approximate with log2. |
| 455 | currFan.push_back_n(SkNextLog2(fanSize)); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 456 | SkDEBUGCODE(TriPointInstance* end =) emit_recursive_fan( |
| 457 | pts, currFan, 0, fanSize, devToAtlasOffset, triangleOrdering, |
| 458 | triPointInstanceData + currIndices->fTriangles); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 459 | currIndices->fTriangles += fanSize - 2; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 460 | SkASSERT(triPointInstanceData + currIndices->fTriangles == end); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 461 | } |
| 462 | currFan.reset(); |
| 463 | continue; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | fInstanceBuffer->unmap(); |
| 468 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 469 | SkASSERT(nextPathInfo == fPathInfos.end()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 470 | SkASSERT(ptsIdx == pts.count() - 1); |
| 471 | SkASSERT(instanceIndices[0].fTriangles == fBaseInstances[1].fTriangles); |
| 472 | SkASSERT(instanceIndices[1].fTriangles == fBaseInstances[0].fQuadratics); |
| 473 | SkASSERT(instanceIndices[0].fQuadratics == fBaseInstances[1].fQuadratics); |
| 474 | SkASSERT(instanceIndices[1].fQuadratics == triEndIdx); |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 475 | SkASSERT(instanceIndices[0].fWeightedTriangles == fBaseInstances[1].fWeightedTriangles); |
| 476 | SkASSERT(instanceIndices[1].fWeightedTriangles == fBaseInstances[0].fCubics); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 477 | SkASSERT(instanceIndices[0].fCubics == fBaseInstances[1].fCubics); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 478 | SkASSERT(instanceIndices[1].fCubics == fBaseInstances[0].fConics); |
| 479 | SkASSERT(instanceIndices[0].fConics == fBaseInstances[1].fConics); |
| 480 | SkASSERT(instanceIndices[1].fConics == quadEndIdx); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 481 | |
| 482 | fMeshesScratchBuffer.reserve(fMaxMeshesPerDraw); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 483 | fScissorRectScratchBuffer.reserve(fMaxMeshesPerDraw); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 484 | |
| 485 | return true; |
| 486 | } |
| 487 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 488 | void GrCCFiller::drawFills( |
| 489 | GrOpFlushState* flushState, GrCCCoverageProcessor* proc, const GrPipeline& pipeline, |
| 490 | BatchID batchID, const SkIRect& drawBounds) const { |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 491 | using PrimitiveType = GrCCCoverageProcessor::PrimitiveType; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 492 | |
| 493 | SkASSERT(fInstanceBuffer); |
| 494 | |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 495 | GrResourceProvider* rp = flushState->resourceProvider(); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 496 | const PrimitiveTallies& batchTotalCounts = fBatches[batchID].fTotalPrimitiveCounts; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 497 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 498 | if (batchTotalCounts.fTriangles) { |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 499 | proc->reset(PrimitiveType::kTriangles, rp); |
| 500 | this->drawPrimitives( |
| 501 | flushState, *proc, pipeline, batchID, &PrimitiveTallies::fTriangles, drawBounds); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 502 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 503 | |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 504 | if (batchTotalCounts.fWeightedTriangles) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 505 | SkASSERT(Algorithm::kStencilWindingCount != fAlgorithm); |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 506 | proc->reset(PrimitiveType::kWeightedTriangles, rp); |
| 507 | this->drawPrimitives( |
| 508 | flushState, *proc, pipeline, batchID, &PrimitiveTallies::fWeightedTriangles, |
| 509 | drawBounds); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 510 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 511 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 512 | if (batchTotalCounts.fQuadratics) { |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 513 | proc->reset(PrimitiveType::kQuadratics, rp); |
| 514 | this->drawPrimitives( |
| 515 | flushState, *proc, pipeline, batchID, &PrimitiveTallies::fQuadratics, drawBounds); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | if (batchTotalCounts.fCubics) { |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 519 | proc->reset(PrimitiveType::kCubics, rp); |
| 520 | this->drawPrimitives( |
| 521 | flushState, *proc, pipeline, batchID, &PrimitiveTallies::fCubics, drawBounds); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 522 | } |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 523 | |
| 524 | if (batchTotalCounts.fConics) { |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 525 | proc->reset(PrimitiveType::kConics, rp); |
| 526 | this->drawPrimitives( |
| 527 | flushState, *proc, pipeline, batchID, &PrimitiveTallies::fConics, drawBounds); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 528 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 531 | void GrCCFiller::drawPrimitives( |
| 532 | GrOpFlushState* flushState, const GrCCCoverageProcessor& proc, const GrPipeline& pipeline, |
| 533 | BatchID batchID, int PrimitiveTallies::*instanceType, const SkIRect& drawBounds) const { |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 534 | SkASSERT(pipeline.isScissorEnabled()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 535 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 536 | // Don't call reset(), as that also resets the reserve count. |
| 537 | fMeshesScratchBuffer.pop_back_n(fMeshesScratchBuffer.count()); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 538 | fScissorRectScratchBuffer.pop_back_n(fScissorRectScratchBuffer.count()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 539 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 540 | SkASSERT(batchID > 0); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 541 | SkASSERT(batchID < fBatches.count()); |
| 542 | const Batch& previousBatch = fBatches[batchID - 1]; |
| 543 | const Batch& batch = fBatches[batchID]; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 544 | SkDEBUGCODE(int totalInstanceCount = 0); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 545 | |
| 546 | if (int instanceCount = batch.fEndNonScissorIndices.*instanceType - |
| 547 | previousBatch.fEndNonScissorIndices.*instanceType) { |
| 548 | SkASSERT(instanceCount > 0); |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 549 | int baseInstance = fBaseInstances[(int)GrScissorTest::kDisabled].*instanceType + |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 550 | previousBatch.fEndNonScissorIndices.*instanceType; |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 551 | proc.appendMesh(fInstanceBuffer, instanceCount, baseInstance, &fMeshesScratchBuffer); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 552 | fScissorRectScratchBuffer.push_back().setXYWH(0, 0, drawBounds.width(), |
| 553 | drawBounds.height()); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 554 | SkDEBUGCODE(totalInstanceCount += instanceCount); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | SkASSERT(previousBatch.fEndScissorSubBatchIdx > 0); |
| 558 | SkASSERT(batch.fEndScissorSubBatchIdx <= fScissorSubBatches.count()); |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 559 | int baseScissorInstance = fBaseInstances[(int)GrScissorTest::kEnabled].*instanceType; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 560 | for (int i = previousBatch.fEndScissorSubBatchIdx; i < batch.fEndScissorSubBatchIdx; ++i) { |
| 561 | const ScissorSubBatch& previousSubBatch = fScissorSubBatches[i - 1]; |
| 562 | const ScissorSubBatch& scissorSubBatch = fScissorSubBatches[i]; |
| 563 | int startIndex = previousSubBatch.fEndPrimitiveIndices.*instanceType; |
| 564 | int instanceCount = scissorSubBatch.fEndPrimitiveIndices.*instanceType - startIndex; |
| 565 | if (!instanceCount) { |
| 566 | continue; |
| 567 | } |
| 568 | SkASSERT(instanceCount > 0); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 569 | proc.appendMesh(fInstanceBuffer, instanceCount, baseScissorInstance + startIndex, |
| 570 | &fMeshesScratchBuffer); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 571 | fScissorRectScratchBuffer.push_back() = scissorSubBatch.fScissor; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 572 | SkDEBUGCODE(totalInstanceCount += instanceCount); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 575 | SkASSERT(fMeshesScratchBuffer.count() == fScissorRectScratchBuffer.count()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 576 | SkASSERT(fMeshesScratchBuffer.count() <= fMaxMeshesPerDraw); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 577 | SkASSERT(totalInstanceCount == batch.fTotalPrimitiveCounts.*instanceType); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 578 | |
| 579 | if (!fMeshesScratchBuffer.empty()) { |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 580 | proc.draw(flushState, pipeline, fScissorRectScratchBuffer.begin(), |
| 581 | fMeshesScratchBuffer.begin(), fMeshesScratchBuffer.count(), |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 582 | SkRect::Make(drawBounds)); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 583 | } |
| 584 | } |