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