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