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