| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
| 10 | #include "GrInOrderDrawBuffer.h" |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 11 | #include "GrRenderTarget.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 12 | #include "GrTexture.h" |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 13 | #include "GrBufferAllocPool.h" |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 14 | #include "GrIndexBuffer.h" |
| 15 | #include "GrVertexBuffer.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | #include "GrGpu.h" |
| 17 | |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 18 | GrInOrderDrawBuffer::GrInOrderDrawBuffer(const GrGpu* gpu, |
| 19 | GrVertexBufferAllocPool* vertexPool, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 20 | GrIndexBufferAllocPool* indexPool) |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 21 | : fAutoFlushTarget(NULL) |
| 22 | , fClipSet(true) |
| robertphillips@google.com | 6970557 | 2012-03-21 19:46:50 +0000 | [diff] [blame] | 23 | , fVertexPool(*vertexPool) |
| 24 | , fIndexPool(*indexPool) |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 25 | , fLastRectVertexLayout(0) |
| 26 | , fQuadIndexBuffer(NULL) |
| 27 | , fMaxQuads(0) |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 28 | , fCurrQuad(0) |
| 29 | , fFlushing(false) { |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 30 | |
| 31 | fCaps = gpu->getCaps(); |
| 32 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 33 | GrAssert(NULL != vertexPool); |
| 34 | GrAssert(NULL != indexPool); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 35 | |
| 36 | GeometryPoolState& poolState = fGeoPoolStateStack.push_back(); |
| 37 | poolState.fUsedPoolVertexBytes = 0; |
| 38 | poolState.fUsedPoolIndexBytes = 0; |
| 39 | #if GR_DEBUG |
| 40 | poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0; |
| 41 | poolState.fPoolStartVertex = ~0; |
| 42 | poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0; |
| 43 | poolState.fPoolStartIndex = ~0; |
| 44 | #endif |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 45 | fInstancedDrawTracker.reset(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | GrInOrderDrawBuffer::~GrInOrderDrawBuffer() { |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 49 | this->reset(); |
| bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 50 | // This must be called by before the GrDrawTarget destructor |
| 51 | this->releaseGeometry(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 52 | GrSafeUnref(fQuadIndexBuffer); |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 53 | GrSafeUnref(fAutoFlushTarget); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 56 | void GrInOrderDrawBuffer::setQuadIndexBuffer(const GrIndexBuffer* indexBuffer) { |
| 57 | bool newIdxBuffer = fQuadIndexBuffer != indexBuffer; |
| 58 | if (newIdxBuffer) { |
| 59 | GrSafeUnref(fQuadIndexBuffer); |
| 60 | fQuadIndexBuffer = indexBuffer; |
| 61 | GrSafeRef(fQuadIndexBuffer); |
| 62 | fCurrQuad = 0; |
| 63 | fMaxQuads = (NULL == indexBuffer) ? 0 : indexBuffer->maxQuads(); |
| 64 | } else { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 65 | GrAssert((NULL == indexBuffer && 0 == fMaxQuads) || |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 66 | (indexBuffer->maxQuads() == fMaxQuads)); |
| 67 | } |
| 68 | } |
| 69 | |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 70 | //////////////////////////////////////////////////////////////////////////////// |
| 71 | |
| 72 | void GrInOrderDrawBuffer::resetDrawTracking() { |
| 73 | fCurrQuad = 0; |
| 74 | fInstancedDrawTracker.reset(); |
| 75 | } |
| 76 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 77 | void GrInOrderDrawBuffer::drawRect(const GrRect& rect, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 78 | const GrMatrix* matrix, |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 79 | StageMask stageMask, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 80 | const GrRect* srcRects[], |
| 81 | const GrMatrix* srcMatrices[]) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 82 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 83 | GrAssert(!(NULL == fQuadIndexBuffer && fCurrQuad)); |
| 84 | GrAssert(!(fDraws.empty() && fCurrQuad)); |
| 85 | GrAssert(!(0 != fMaxQuads && NULL == fQuadIndexBuffer)); |
| 86 | |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 87 | GrDrawState* drawState = this->drawState(); |
| 88 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 89 | // if we have a quad IB then either append to the previous run of |
| 90 | // rects or start a new run |
| 91 | if (fMaxQuads) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 92 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 93 | bool appendToPreviousDraw = false; |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 94 | GrVertexLayout layout = GetRectVertexLayout(stageMask, srcRects); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 95 | AutoReleaseGeometry geo(this, layout, 4, 0); |
| bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 96 | if (!geo.succeeded()) { |
| 97 | GrPrintf("Failed to get space for vertices!\n"); |
| 98 | return; |
| 99 | } |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 100 | GrMatrix combinedMatrix = drawState->getViewMatrix(); |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 101 | // We go to device space so that matrix changes allow us to concat |
| 102 | // rect draws. When the caller has provided explicit source rects |
| 103 | // then we don't want to modify the sampler matrices. Otherwise we do |
| 104 | // we have to account for the view matrix change in the sampler |
| 105 | // matrices. |
| 106 | StageMask devCoordMask = (NULL == srcRects) ? stageMask : 0; |
| 107 | GrDrawTarget::AutoDeviceCoordDraw adcd(this, devCoordMask); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 108 | if (NULL != matrix) { |
| 109 | combinedMatrix.preConcat(*matrix); |
| 110 | } |
| 111 | |
| 112 | SetRectVertices(rect, &combinedMatrix, srcRects, srcMatrices, layout, geo.vertices()); |
| 113 | |
| 114 | // we don't want to miss an opportunity to batch rects together |
| 115 | // simply because the clip has changed if the clip doesn't affect |
| 116 | // the rect. |
| 117 | bool disabledClip = false; |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 118 | if (drawState->isClipState() && fClip.isRect()) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 119 | |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 120 | GrRect clipRect = fClip.getRect(0); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 121 | // If the clip rect touches the edge of the viewport, extended it |
| 122 | // out (close) to infinity to avoid bogus intersections. |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 123 | // We might consider a more exact clip to viewport if this |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 124 | // conservative test fails. |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 125 | const GrRenderTarget* target = drawState->getRenderTarget(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 126 | if (0 >= clipRect.fLeft) { |
| 127 | clipRect.fLeft = GR_ScalarMin; |
| 128 | } |
| 129 | if (target->width() <= clipRect.fRight) { |
| 130 | clipRect.fRight = GR_ScalarMax; |
| 131 | } |
| 132 | if (0 >= clipRect.top()) { |
| 133 | clipRect.fTop = GR_ScalarMin; |
| 134 | } |
| 135 | if (target->height() <= clipRect.fBottom) { |
| 136 | clipRect.fBottom = GR_ScalarMax; |
| 137 | } |
| 138 | int stride = VertexSize(layout); |
| 139 | bool insideClip = true; |
| 140 | for (int v = 0; v < 4; ++v) { |
| 141 | const GrPoint& p = *GetVertexPoint(geo.vertices(), v, stride); |
| 142 | if (!clipRect.contains(p)) { |
| 143 | insideClip = false; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | if (insideClip) { |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 148 | drawState->disableState(GrDrawState::kClip_StateBit); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 149 | disabledClip = true; |
| 150 | } |
| 151 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 152 | if (!needsNewClip() && !needsNewState() && fCurrQuad > 0 && |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 153 | fCurrQuad < fMaxQuads && layout == fLastRectVertexLayout) { |
| 154 | |
| 155 | int vsize = VertexSize(layout); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 156 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 157 | Draw& lastDraw = fDraws.back(); |
| 158 | |
| 159 | GrAssert(lastDraw.fIndexBuffer == fQuadIndexBuffer); |
| bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 160 | GrAssert(kTriangles_GrPrimitiveType == lastDraw.fPrimitiveType); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 161 | GrAssert(0 == lastDraw.fVertexCount % 4); |
| 162 | GrAssert(0 == lastDraw.fIndexCount % 6); |
| 163 | GrAssert(0 == lastDraw.fStartIndex); |
| 164 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 165 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 166 | bool clearSinceLastDraw = |
| 167 | fClears.count() && |
| 168 | fClears.back().fBeforeDrawIdx == fDraws.count(); |
| 169 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 170 | appendToPreviousDraw = |
| 171 | !clearSinceLastDraw && |
| 172 | lastDraw.fVertexBuffer == poolState.fPoolVertexBuffer && |
| 173 | (fCurrQuad * 4 + lastDraw.fStartVertex) == poolState.fPoolStartVertex; |
| 174 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 175 | if (appendToPreviousDraw) { |
| 176 | lastDraw.fVertexCount += 4; |
| 177 | lastDraw.fIndexCount += 6; |
| 178 | fCurrQuad += 1; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 179 | // we reserved above, so we should be the first |
| 180 | // use of this vertex reserveation. |
| 181 | GrAssert(0 == poolState.fUsedPoolVertexBytes); |
| 182 | poolState.fUsedPoolVertexBytes = 4 * vsize; |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | if (!appendToPreviousDraw) { |
| 186 | this->setIndexSourceToBuffer(fQuadIndexBuffer); |
| bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 187 | this->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 4, 6); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 188 | fCurrQuad = 1; |
| 189 | fLastRectVertexLayout = layout; |
| 190 | } |
| 191 | if (disabledClip) { |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 192 | drawState->enableState(GrDrawState::kClip_StateBit); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 193 | } |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 194 | fInstancedDrawTracker.reset(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 195 | } else { |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 196 | INHERITED::drawRect(rect, matrix, stageMask, srcRects, srcMatrices); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 200 | void GrInOrderDrawBuffer::drawIndexedInstances(GrPrimitiveType type, |
| 201 | int instanceCount, |
| 202 | int verticesPerInstance, |
| 203 | int indicesPerInstance) { |
| 204 | if (!verticesPerInstance || !indicesPerInstance) { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | const GeometrySrcState& geomSrc = this->getGeomSrc(); |
| 209 | |
| 210 | // we only attempt to concat the case when reserved verts are used with |
| 211 | // an index buffer. |
| 212 | if (kReserved_GeometrySrcType == geomSrc.fVertexSrc && |
| 213 | kBuffer_GeometrySrcType == geomSrc.fIndexSrc) { |
| 214 | |
| 215 | Draw* draw = NULL; |
| 216 | // if the last draw used the same indices/vertices per shape then we |
| 217 | // may be able to append to it. |
| 218 | if (verticesPerInstance == fInstancedDrawTracker.fVerticesPerInstance && |
| 219 | indicesPerInstance == fInstancedDrawTracker.fIndicesPerInstance) { |
| 220 | GrAssert(fDraws.count()); |
| 221 | draw = &fDraws.back(); |
| 222 | } |
| 223 | |
| 224 | bool clipChanged = this->needsNewClip(); |
| 225 | bool stateChanged = this->needsNewState(); |
| 226 | if (clipChanged) { |
| robertphillips@google.com | 49d9fd5 | 2012-05-23 11:44:08 +0000 | [diff] [blame] | 227 | this->storeClip(); |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 228 | } |
| 229 | if (stateChanged) { |
| 230 | this->pushState(); |
| 231 | } |
| 232 | |
| 233 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 234 | const GrVertexBuffer* vertexBuffer = poolState.fPoolVertexBuffer; |
| 235 | |
| 236 | // Check whether the draw is compatible with this draw in order to |
| 237 | // append |
| 238 | if (NULL == draw || |
| 239 | clipChanged || |
| 240 | stateChanged || |
| 241 | draw->fIndexBuffer != geomSrc.fIndexBuffer || |
| 242 | draw->fPrimitiveType != type || |
| 243 | draw->fVertexBuffer != vertexBuffer) { |
| 244 | |
| 245 | draw = &fDraws.push_back(); |
| 246 | draw->fClipChanged = clipChanged; |
| 247 | draw->fStateChanged = stateChanged; |
| 248 | draw->fIndexBuffer = geomSrc.fIndexBuffer; |
| 249 | geomSrc.fIndexBuffer->ref(); |
| 250 | draw->fVertexBuffer = vertexBuffer; |
| 251 | vertexBuffer->ref(); |
| 252 | draw->fPrimitiveType = type; |
| 253 | draw->fStartIndex = 0; |
| 254 | draw->fIndexCount = 0; |
| 255 | draw->fStartVertex = poolState.fPoolStartVertex; |
| 256 | draw->fVertexCount = 0; |
| 257 | draw->fVertexLayout = geomSrc.fVertexLayout; |
| 258 | } else { |
| 259 | GrAssert(!(draw->fIndexCount % indicesPerInstance)); |
| 260 | GrAssert(!(draw->fVertexCount % verticesPerInstance)); |
| 261 | GrAssert(poolState.fPoolStartVertex == draw->fStartVertex + |
| 262 | draw->fVertexCount); |
| 263 | } |
| 264 | |
| 265 | // how many instances can be in a single draw |
| 266 | int maxInstancesPerDraw = this->indexCountInCurrentSource() / |
| 267 | indicesPerInstance; |
| 268 | if (!maxInstancesPerDraw) { |
| 269 | return; |
| 270 | } |
| 271 | // how many instances should be concat'ed onto draw |
| 272 | int instancesToConcat = maxInstancesPerDraw - draw->fVertexCount / |
| 273 | verticesPerInstance; |
| 274 | if (maxInstancesPerDraw > instanceCount) { |
| 275 | maxInstancesPerDraw = instanceCount; |
| 276 | if (instancesToConcat > instanceCount) { |
| 277 | instancesToConcat = instanceCount; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | // update the amount of reserved data actually referenced in draws |
| 282 | size_t vertexBytes = instanceCount * verticesPerInstance * |
| 283 | VertexSize(draw->fVertexLayout); |
| 284 | poolState.fUsedPoolVertexBytes = |
| 285 | GrMax(poolState.fUsedPoolVertexBytes, vertexBytes); |
| 286 | |
| 287 | while (instanceCount) { |
| 288 | if (!instancesToConcat) { |
| 289 | int startVertex = draw->fStartVertex + draw->fVertexCount; |
| 290 | draw = &fDraws.push_back(); |
| 291 | draw->fClipChanged = false; |
| 292 | draw->fStateChanged = false; |
| 293 | draw->fIndexBuffer = geomSrc.fIndexBuffer; |
| 294 | geomSrc.fIndexBuffer->ref(); |
| 295 | draw->fVertexBuffer = vertexBuffer; |
| 296 | vertexBuffer->ref(); |
| 297 | draw->fPrimitiveType = type; |
| 298 | draw->fStartIndex = 0; |
| 299 | draw->fStartVertex = startVertex; |
| 300 | draw->fVertexCount = 0; |
| 301 | draw->fVertexLayout = geomSrc.fVertexLayout; |
| 302 | instancesToConcat = maxInstancesPerDraw; |
| 303 | } |
| 304 | draw->fVertexCount += instancesToConcat * verticesPerInstance; |
| 305 | draw->fIndexCount += instancesToConcat * indicesPerInstance; |
| 306 | instanceCount -= instancesToConcat; |
| 307 | instancesToConcat = 0; |
| 308 | } |
| 309 | |
| 310 | // update draw tracking for next draw |
| 311 | fCurrQuad = 0; |
| 312 | fInstancedDrawTracker.fVerticesPerInstance = verticesPerInstance; |
| 313 | fInstancedDrawTracker.fIndicesPerInstance = indicesPerInstance; |
| 314 | } else { |
| 315 | this->INHERITED::drawIndexedInstances(type, |
| 316 | instanceCount, |
| 317 | verticesPerInstance, |
| 318 | indicesPerInstance); |
| 319 | } |
| 320 | |
| 321 | } |
| 322 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 323 | void GrInOrderDrawBuffer::onDrawIndexed(GrPrimitiveType primitiveType, |
| 324 | int startVertex, |
| 325 | int startIndex, |
| 326 | int vertexCount, |
| 327 | int indexCount) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 328 | |
| 329 | if (!vertexCount || !indexCount) { |
| 330 | return; |
| 331 | } |
| 332 | |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 333 | this->resetDrawTracking(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 334 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 335 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 336 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 337 | Draw& draw = fDraws.push_back(); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 338 | draw.fPrimitiveType = primitiveType; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 339 | draw.fStartVertex = startVertex; |
| 340 | draw.fStartIndex = startIndex; |
| 341 | draw.fVertexCount = vertexCount; |
| 342 | draw.fIndexCount = indexCount; |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 343 | |
| 344 | draw.fClipChanged = this->needsNewClip(); |
| 345 | if (draw.fClipChanged) { |
| robertphillips@google.com | 49d9fd5 | 2012-05-23 11:44:08 +0000 | [diff] [blame] | 346 | this->storeClip(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | draw.fStateChanged = this->needsNewState(); |
| 350 | if (draw.fStateChanged) { |
| 351 | this->pushState(); |
| 352 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 353 | |
| bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 354 | draw.fVertexLayout = this->getVertexLayout(); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 355 | switch (this->getGeomSrc().fVertexSrc) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 356 | case kBuffer_GeometrySrcType: |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 357 | draw.fVertexBuffer = this->getGeomSrc().fVertexBuffer; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 358 | break; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 359 | case kReserved_GeometrySrcType: // fallthrough |
| 360 | case kArray_GeometrySrcType: { |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 361 | size_t vertexBytes = (vertexCount + startVertex) * |
| bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 362 | VertexSize(draw.fVertexLayout); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 363 | poolState.fUsedPoolVertexBytes = |
| 364 | GrMax(poolState.fUsedPoolVertexBytes, vertexBytes); |
| 365 | draw.fVertexBuffer = poolState.fPoolVertexBuffer; |
| 366 | draw.fStartVertex += poolState.fPoolStartVertex; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 367 | break; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 368 | } |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 369 | default: |
| 370 | GrCrash("unknown geom src type"); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 371 | } |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 372 | draw.fVertexBuffer->ref(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 373 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 374 | switch (this->getGeomSrc().fIndexSrc) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 375 | case kBuffer_GeometrySrcType: |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 376 | draw.fIndexBuffer = this->getGeomSrc().fIndexBuffer; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 377 | break; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 378 | case kReserved_GeometrySrcType: // fallthrough |
| 379 | case kArray_GeometrySrcType: { |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 380 | size_t indexBytes = (indexCount + startIndex) * sizeof(uint16_t); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 381 | poolState.fUsedPoolIndexBytes = |
| 382 | GrMax(poolState.fUsedPoolIndexBytes, indexBytes); |
| 383 | draw.fIndexBuffer = poolState.fPoolIndexBuffer; |
| bsalomon@google.com | d127ffe | 2012-02-24 20:11:52 +0000 | [diff] [blame] | 384 | draw.fStartIndex += poolState.fPoolStartIndex; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 385 | break; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 386 | } |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 387 | default: |
| 388 | GrCrash("unknown geom src type"); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 389 | } |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 390 | draw.fIndexBuffer->ref(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 393 | void GrInOrderDrawBuffer::onDrawNonIndexed(GrPrimitiveType primitiveType, |
| 394 | int startVertex, |
| 395 | int vertexCount) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 396 | if (!vertexCount) { |
| 397 | return; |
| 398 | } |
| 399 | |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 400 | this->resetDrawTracking(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 401 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 402 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 403 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 404 | Draw& draw = fDraws.push_back(); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 405 | draw.fPrimitiveType = primitiveType; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 406 | draw.fStartVertex = startVertex; |
| 407 | draw.fStartIndex = 0; |
| 408 | draw.fVertexCount = vertexCount; |
| 409 | draw.fIndexCount = 0; |
| 410 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 411 | draw.fClipChanged = this->needsNewClip(); |
| 412 | if (draw.fClipChanged) { |
| robertphillips@google.com | 49d9fd5 | 2012-05-23 11:44:08 +0000 | [diff] [blame] | 413 | this->storeClip(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | draw.fStateChanged = this->needsNewState(); |
| 417 | if (draw.fStateChanged) { |
| 418 | this->pushState(); |
| 419 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 420 | |
| bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 421 | draw.fVertexLayout = this->getVertexLayout(); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 422 | switch (this->getGeomSrc().fVertexSrc) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 423 | case kBuffer_GeometrySrcType: |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 424 | draw.fVertexBuffer = this->getGeomSrc().fVertexBuffer; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 425 | break; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 426 | case kReserved_GeometrySrcType: // fallthrough |
| 427 | case kArray_GeometrySrcType: { |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 428 | size_t vertexBytes = (vertexCount + startVertex) * |
| bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 429 | VertexSize(draw.fVertexLayout); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 430 | poolState.fUsedPoolVertexBytes = |
| 431 | GrMax(poolState.fUsedPoolVertexBytes, vertexBytes); |
| 432 | draw.fVertexBuffer = poolState.fPoolVertexBuffer; |
| 433 | draw.fStartVertex += poolState.fPoolStartVertex; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 434 | break; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 435 | } |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 436 | default: |
| 437 | GrCrash("unknown geom src type"); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 438 | } |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 439 | draw.fVertexBuffer->ref(); |
| 440 | draw.fIndexBuffer = NULL; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 443 | void GrInOrderDrawBuffer::onStencilPath(const GrPath&, GrPathFill) { |
| 444 | GrCrash("Not implemented yet. Should not get here."); |
| 445 | } |
| 446 | |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 447 | void GrInOrderDrawBuffer::clear(const GrIRect* rect, |
| 448 | GrColor color, |
| 449 | GrRenderTarget* renderTarget) { |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 450 | GrIRect r; |
| 451 | if (NULL == rect) { |
| 452 | // We could do something smart and remove previous draws and clears to |
| 453 | // the current render target. If we get that smart we have to make sure |
| 454 | // those draws aren't read before this clear (render-to-texture). |
| 455 | r.setLTRB(0, 0, |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 456 | this->getDrawState().getRenderTarget()->width(), |
| 457 | this->getDrawState().getRenderTarget()->height()); |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 458 | rect = &r; |
| 459 | } |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 460 | Clear& clr = fClears.push_back(); |
| 461 | clr.fColor = color; |
| 462 | clr.fBeforeDrawIdx = fDraws.count(); |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 463 | clr.fRect = *rect; |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 464 | clr.fRenderTarget = renderTarget; |
| 465 | GrSafeRef(clr.fRenderTarget); |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 468 | void GrInOrderDrawBuffer::reset() { |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 469 | GrAssert(1 == fGeoPoolStateStack.count()); |
| 470 | this->resetVertexSource(); |
| 471 | this->resetIndexSource(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 472 | int numDraws = fDraws.count(); |
| 473 | for (int d = 0; d < numDraws; ++d) { |
| 474 | // we always have a VB, but not always an IB |
| 475 | GrAssert(NULL != fDraws[d].fVertexBuffer); |
| 476 | fDraws[d].fVertexBuffer->unref(); |
| 477 | GrSafeUnref(fDraws[d].fIndexBuffer); |
| 478 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 479 | fDraws.reset(); |
| 480 | fStates.reset(); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 481 | |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 482 | fClears.reset(); |
| 483 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 484 | fVertexPool.reset(); |
| 485 | fIndexPool.reset(); |
| 486 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 487 | fClips.reset(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 488 | |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 489 | this->resetDrawTracking(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void GrInOrderDrawBuffer::playback(GrDrawTarget* target) { |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 493 | GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc); |
| 494 | GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc); |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 495 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 496 | GrAssert(NULL != target); |
| 497 | GrAssert(target != this); // not considered and why? |
| 498 | |
| bsalomon@google.com | 898d9e5 | 2011-04-26 13:22:33 +0000 | [diff] [blame] | 499 | int numDraws = fDraws.count(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 500 | if (!numDraws) { |
| 501 | return; |
| 502 | } |
| 503 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 504 | fVertexPool.unlock(); |
| 505 | fIndexPool.unlock(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 506 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 507 | GrDrawTarget::AutoClipRestore acr(target); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 508 | AutoGeometryPush agp(target); |
| bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 509 | GrDrawState* prevDrawState = target->drawState(); |
| 510 | prevDrawState->ref(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 511 | |
| bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 512 | int currState = ~0; |
| 513 | int currClip = ~0; |
| 514 | int currClear = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 515 | |
| bsalomon@google.com | 898d9e5 | 2011-04-26 13:22:33 +0000 | [diff] [blame] | 516 | for (int i = 0; i < numDraws; ++i) { |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 517 | while (currClear < fClears.count() && |
| 518 | i == fClears[currClear].fBeforeDrawIdx) { |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 519 | target->clear(&fClears[currClear].fRect, |
| 520 | fClears[currClear].fColor, |
| 521 | fClears[currClear].fRenderTarget); |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 522 | ++currClear; |
| 523 | } |
| 524 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 525 | const Draw& draw = fDraws[i]; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 526 | if (draw.fStateChanged) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 527 | ++currState; |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 528 | target->setDrawState(&fStates[currState]); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 529 | } |
| 530 | if (draw.fClipChanged) { |
| 531 | ++currClip; |
| 532 | target->setClip(fClips[currClip]); |
| 533 | } |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 534 | |
| 535 | target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer); |
| 536 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 537 | if (draw.fIndexCount) { |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 538 | target->setIndexSourceToBuffer(draw.fIndexBuffer); |
| 539 | } |
| 540 | |
| 541 | if (draw.fIndexCount) { |
| 542 | target->drawIndexed(draw.fPrimitiveType, |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 543 | draw.fStartVertex, |
| 544 | draw.fStartIndex, |
| 545 | draw.fVertexCount, |
| 546 | draw.fIndexCount); |
| 547 | } else { |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 548 | target->drawNonIndexed(draw.fPrimitiveType, |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 549 | draw.fStartVertex, |
| 550 | draw.fVertexCount); |
| 551 | } |
| 552 | } |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 553 | while (currClear < fClears.count()) { |
| 554 | GrAssert(fDraws.count() == fClears[currClear].fBeforeDrawIdx); |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 555 | target->clear(&fClears[currClear].fRect, |
| 556 | fClears[currClear].fColor, |
| 557 | fClears[currClear].fRenderTarget); |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 558 | ++currClear; |
| 559 | } |
| bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 560 | target->setDrawState(prevDrawState); |
| 561 | prevDrawState->unref(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 564 | void GrInOrderDrawBuffer::setAutoFlushTarget(GrDrawTarget* target) { |
| 565 | GrSafeAssign(fAutoFlushTarget, target); |
| 566 | } |
| 567 | |
| 568 | void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace( |
| 569 | GrVertexLayout vertexLayout, |
| 570 | int vertexCount, |
| 571 | int indexCount) { |
| 572 | if (NULL != fAutoFlushTarget) { |
| 573 | // We use geometryHints() to know whether to flush the draw buffer. We |
| 574 | // can't flush if we are inside an unbalanced pushGeometrySource. |
| 575 | // Moreover, flushing blows away vertex and index data that was |
| 576 | // previously reserved. So if the vertex or index data is pulled from |
| 577 | // reserved space and won't be released by this request then we can't |
| 578 | // flush. |
| 579 | bool insideGeoPush = fGeoPoolStateStack.count() > 1; |
| 580 | |
| 581 | bool unreleasedVertexSpace = |
| 582 | !vertexCount && |
| 583 | kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc; |
| 584 | |
| 585 | bool unreleasedIndexSpace = |
| 586 | !indexCount && |
| 587 | kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc; |
| 588 | |
| 589 | // we don't want to finalize any reserved geom on the target since |
| 590 | // we don't know that the client has finished writing to it. |
| 591 | bool targetHasReservedGeom = |
| 592 | fAutoFlushTarget->hasReservedVerticesOrIndices(); |
| 593 | |
| 594 | int vcount = vertexCount; |
| 595 | int icount = indexCount; |
| 596 | |
| 597 | if (!insideGeoPush && |
| 598 | !unreleasedVertexSpace && |
| 599 | !unreleasedIndexSpace && |
| 600 | !targetHasReservedGeom && |
| 601 | this->geometryHints(vertexLayout, &vcount, &icount)) { |
| 602 | |
| 603 | this->flushTo(fAutoFlushTarget); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 608 | bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout, |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 609 | int* vertexCount, |
| 610 | int* indexCount) const { |
| 611 | // we will recommend a flush if the data could fit in a single |
| 612 | // preallocated buffer but none are left and it can't fit |
| 613 | // in the current buffer (which may not be prealloced). |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 614 | bool flush = false; |
| 615 | if (NULL != indexCount) { |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 616 | int32_t currIndices = fIndexPool.currentBufferIndices(); |
| 617 | if (*indexCount > currIndices && |
| 618 | (!fIndexPool.preallocatedBuffersRemaining() && |
| 619 | *indexCount <= fIndexPool.preallocatedBufferIndices())) { |
| 620 | |
| 621 | flush = true; |
| 622 | } |
| 623 | *indexCount = currIndices; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 624 | } |
| 625 | if (NULL != vertexCount) { |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 626 | int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout); |
| 627 | if (*vertexCount > currVertices && |
| 628 | (!fVertexPool.preallocatedBuffersRemaining() && |
| 629 | *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 630 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 631 | flush = true; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 632 | } |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 633 | *vertexCount = currVertices; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 634 | } |
| 635 | return flush; |
| 636 | } |
| 637 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 638 | bool GrInOrderDrawBuffer::onReserveVertexSpace(GrVertexLayout vertexLayout, |
| 639 | int vertexCount, |
| 640 | void** vertices) { |
| 641 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 642 | GrAssert(vertexCount > 0); |
| 643 | GrAssert(NULL != vertices); |
| 644 | GrAssert(0 == poolState.fUsedPoolVertexBytes); |
| 645 | |
| 646 | *vertices = fVertexPool.makeSpace(vertexLayout, |
| 647 | vertexCount, |
| 648 | &poolState.fPoolVertexBuffer, |
| 649 | &poolState.fPoolStartVertex); |
| 650 | return NULL != *vertices; |
| 651 | } |
| 652 | |
| 653 | bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) { |
| 654 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 655 | GrAssert(indexCount > 0); |
| 656 | GrAssert(NULL != indices); |
| 657 | GrAssert(0 == poolState.fUsedPoolIndexBytes); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 658 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 659 | *indices = fIndexPool.makeSpace(indexCount, |
| 660 | &poolState.fPoolIndexBuffer, |
| 661 | &poolState.fPoolStartIndex); |
| 662 | return NULL != *indices; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 665 | void GrInOrderDrawBuffer::releaseReservedVertexSpace() { |
| 666 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 667 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 668 | |
| 669 | GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc); |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 670 | |
| 671 | // When the caller reserved vertex buffer space we gave it back a pointer |
| 672 | // provided by the vertex buffer pool. At each draw we tracked the largest |
| 673 | // offset into the pool's pointer that was referenced. Now we return to the |
| 674 | // pool any portion at the tail of the allocation that no draw referenced. |
| 675 | size_t reservedVertexBytes = VertexSize(geoSrc.fVertexLayout) * |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 676 | geoSrc.fVertexCount; |
| 677 | fVertexPool.putBack(reservedVertexBytes - |
| 678 | poolState.fUsedPoolVertexBytes); |
| 679 | poolState.fUsedPoolVertexBytes = 0; |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 680 | poolState.fPoolVertexBuffer = NULL; |
| 681 | poolState.fPoolStartVertex = 0; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 684 | void GrInOrderDrawBuffer::releaseReservedIndexSpace() { |
| 685 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 686 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 687 | |
| 688 | GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc); |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 689 | |
| 690 | // Similar to releaseReservedVertexSpace we return any unused portion at |
| 691 | // the tail |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 692 | size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount; |
| 693 | fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes); |
| 694 | poolState.fUsedPoolIndexBytes = 0; |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 695 | poolState.fPoolIndexBuffer = NULL; |
| 696 | poolState.fPoolStartIndex = 0; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 699 | void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray, |
| 700 | int vertexCount) { |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 701 | |
| 702 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 703 | GrAssert(0 == poolState.fUsedPoolVertexBytes); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 704 | #if GR_DEBUG |
| 705 | bool success = |
| 706 | #endif |
| bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 707 | fVertexPool.appendVertices(this->getVertexLayout(), |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 708 | vertexCount, |
| 709 | vertexArray, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 710 | &poolState.fPoolVertexBuffer, |
| 711 | &poolState.fPoolStartVertex); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 712 | GR_DEBUGASSERT(success); |
| 713 | } |
| 714 | |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 715 | void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray, |
| 716 | int indexCount) { |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 717 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 718 | GrAssert(0 == poolState.fUsedPoolIndexBytes); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 719 | #if GR_DEBUG |
| 720 | bool success = |
| 721 | #endif |
| 722 | fIndexPool.appendIndices(indexCount, |
| 723 | indexArray, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 724 | &poolState.fPoolIndexBuffer, |
| 725 | &poolState.fPoolStartIndex); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 726 | GR_DEBUGASSERT(success); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 729 | void GrInOrderDrawBuffer::releaseVertexArray() { |
| 730 | // When the client provides an array as the vertex source we handled it |
| 731 | // by copying their array into reserved space. |
| 732 | this->GrInOrderDrawBuffer::releaseReservedVertexSpace(); |
| 733 | } |
| 734 | |
| 735 | void GrInOrderDrawBuffer::releaseIndexArray() { |
| 736 | // When the client provides an array as the index source we handled it |
| 737 | // by copying their array into reserved space. |
| 738 | this->GrInOrderDrawBuffer::releaseReservedIndexSpace(); |
| 739 | } |
| 740 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 741 | void GrInOrderDrawBuffer::geometrySourceWillPush() { |
| 742 | GeometryPoolState& poolState = fGeoPoolStateStack.push_back(); |
| 743 | poolState.fUsedPoolVertexBytes = 0; |
| 744 | poolState.fUsedPoolIndexBytes = 0; |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 745 | this->resetDrawTracking(); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 746 | #if GR_DEBUG |
| 747 | poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0; |
| 748 | poolState.fPoolStartVertex = ~0; |
| 749 | poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0; |
| 750 | poolState.fPoolStartIndex = ~0; |
| 751 | #endif |
| 752 | } |
| 753 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 754 | void GrInOrderDrawBuffer::geometrySourceWillPop( |
| 755 | const GeometrySrcState& restoredState) { |
| 756 | GrAssert(fGeoPoolStateStack.count() > 1); |
| 757 | fGeoPoolStateStack.pop_back(); |
| 758 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 759 | // we have to assume that any slack we had in our vertex/index data |
| 760 | // is now unreleasable because data may have been appended later in the |
| 761 | // pool. |
| 762 | if (kReserved_GeometrySrcType == restoredState.fVertexSrc || |
| 763 | kArray_GeometrySrcType == restoredState.fVertexSrc) { |
| 764 | poolState.fUsedPoolVertexBytes = |
| 765 | VertexSize(restoredState.fVertexLayout) * |
| 766 | restoredState.fVertexCount; |
| 767 | } |
| 768 | if (kReserved_GeometrySrcType == restoredState.fIndexSrc || |
| 769 | kArray_GeometrySrcType == restoredState.fIndexSrc) { |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 770 | poolState.fUsedPoolIndexBytes = sizeof(uint16_t) * |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 771 | restoredState.fIndexCount; |
| 772 | } |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 773 | this->resetDrawTracking(); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 776 | bool GrInOrderDrawBuffer::needsNewState() const { |
| 777 | if (fStates.empty()) { |
| 778 | return true; |
| 779 | } else { |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 780 | return fStates.back() != this->getDrawState(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 781 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 784 | void GrInOrderDrawBuffer::pushState() { |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 785 | fStates.push_back(this->getDrawState()); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 786 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 787 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 788 | bool GrInOrderDrawBuffer::needsNewClip() const { |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 789 | if (this->getDrawState().isClipState()) { |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 790 | if (fClips.empty() || (fClipSet && fClips.back() != fClip)) { |
| 791 | return true; |
| 792 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 793 | } |
| 794 | return false; |
| 795 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 796 | |
| robertphillips@google.com | 49d9fd5 | 2012-05-23 11:44:08 +0000 | [diff] [blame] | 797 | void GrInOrderDrawBuffer::storeClip() { |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 798 | fClips.push_back() = fClip; |
| 799 | fClipSet = false; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 800 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 801 | |
| bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 802 | void GrInOrderDrawBuffer::clipWillBeSet(const GrClip& newClip) { |
| 803 | INHERITED::clipWillBeSet(newClip); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 804 | fClipSet = true; |
| 805 | } |