reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 2 | Copyright 2011 Google Inc. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "GrInOrderDrawBuffer.h" |
| 19 | #include "GrTexture.h" |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 20 | #include "GrBufferAllocPool.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 21 | #include "GrIndexBuffer.h" |
| 22 | #include "GrVertexBuffer.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | #include "GrGpu.h" |
| 24 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 25 | GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrVertexBufferAllocPool* vertexPool, |
| 26 | GrIndexBufferAllocPool* indexPool) : |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | fDraws(DRAWS_BLOCK_SIZE, fDrawsStorage), |
| 28 | fStates(STATES_BLOCK_SIZE, fStatesStorage), |
| 29 | fClips(CLIPS_BLOCK_SIZE, fClipsStorage), |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 30 | fClipSet(true), |
| 31 | |
| 32 | fLastRectVertexLayout(0), |
| 33 | fQuadIndexBuffer(NULL), |
| 34 | fMaxQuads(0), |
| 35 | fCurrQuad(0), |
| 36 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 37 | fVertexPool(*vertexPool), |
| 38 | fCurrPoolVertexBuffer(NULL), |
| 39 | fCurrPoolStartVertex(0), |
| 40 | fIndexPool(*indexPool), |
| 41 | fCurrPoolIndexBuffer(NULL), |
| 42 | fCurrPoolStartIndex(0), |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | fReservedVertexBytes(0), |
| 44 | fReservedIndexBytes(0), |
| 45 | fUsedReservedVertexBytes(0), |
| 46 | fUsedReservedIndexBytes(0) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 47 | GrAssert(NULL != vertexPool); |
| 48 | GrAssert(NULL != indexPool); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | GrInOrderDrawBuffer::~GrInOrderDrawBuffer() { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 52 | this->reset(); |
| 53 | GrSafeUnref(fQuadIndexBuffer); |
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 | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 75 | void GrInOrderDrawBuffer::drawRect(const GrRect& rect, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 76 | const GrMatrix* matrix, |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 77 | StageBitfield stageEnableBitfield, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 78 | const GrRect* srcRects[], |
| 79 | const GrMatrix* srcMatrices[]) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 80 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 81 | GrAssert(!(NULL == fQuadIndexBuffer && fCurrQuad)); |
| 82 | GrAssert(!(fDraws.empty() && fCurrQuad)); |
| 83 | GrAssert(!(0 != fMaxQuads && NULL == fQuadIndexBuffer)); |
| 84 | |
| 85 | // if we have a quad IB then either append to the previous run of |
| 86 | // rects or start a new run |
| 87 | if (fMaxQuads) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 88 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 89 | bool appendToPreviousDraw = false; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 90 | GrVertexLayout layout = GetRectVertexLayout(stageEnableBitfield, srcRects); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 91 | AutoReleaseGeometry geo(this, layout, 4, 0); |
| 92 | AutoViewMatrixRestore avmr(this); |
| 93 | GrMatrix combinedMatrix = this->getViewMatrix(); |
| 94 | this->setViewMatrix(GrMatrix::I()); |
| 95 | if (NULL != matrix) { |
| 96 | combinedMatrix.preConcat(*matrix); |
| 97 | } |
| 98 | |
| 99 | SetRectVertices(rect, &combinedMatrix, srcRects, srcMatrices, layout, geo.vertices()); |
| 100 | |
| 101 | // we don't want to miss an opportunity to batch rects together |
| 102 | // simply because the clip has changed if the clip doesn't affect |
| 103 | // the rect. |
| 104 | bool disabledClip = false; |
| 105 | if (this->isClipState() && fClip.isRect()) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 106 | |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 107 | GrRect clipRect = fClip.getRect(0); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 108 | // If the clip rect touches the edge of the viewport, extended it |
| 109 | // out (close) to infinity to avoid bogus intersections. |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 110 | // We might consider a more exact clip to viewport if this |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 111 | // conservative test fails. |
| 112 | const GrRenderTarget* target = this->getRenderTarget(); |
| 113 | if (0 >= clipRect.fLeft) { |
| 114 | clipRect.fLeft = GR_ScalarMin; |
| 115 | } |
| 116 | if (target->width() <= clipRect.fRight) { |
| 117 | clipRect.fRight = GR_ScalarMax; |
| 118 | } |
| 119 | if (0 >= clipRect.top()) { |
| 120 | clipRect.fTop = GR_ScalarMin; |
| 121 | } |
| 122 | if (target->height() <= clipRect.fBottom) { |
| 123 | clipRect.fBottom = GR_ScalarMax; |
| 124 | } |
| 125 | int stride = VertexSize(layout); |
| 126 | bool insideClip = true; |
| 127 | for (int v = 0; v < 4; ++v) { |
| 128 | const GrPoint& p = *GetVertexPoint(geo.vertices(), v, stride); |
| 129 | if (!clipRect.contains(p)) { |
| 130 | insideClip = false; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | if (insideClip) { |
| 135 | this->disableState(kClip_StateBit); |
| 136 | disabledClip = true; |
| 137 | } |
| 138 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 139 | if (!needsNewClip() && !needsNewState() && fCurrQuad > 0 && |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 140 | fCurrQuad < fMaxQuads && layout == fLastRectVertexLayout) { |
| 141 | |
| 142 | int vsize = VertexSize(layout); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 143 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 144 | Draw& lastDraw = fDraws.back(); |
| 145 | |
| 146 | GrAssert(lastDraw.fIndexBuffer == fQuadIndexBuffer); |
| 147 | GrAssert(kTriangles_PrimitiveType == lastDraw.fPrimitiveType); |
| 148 | GrAssert(0 == lastDraw.fVertexCount % 4); |
| 149 | GrAssert(0 == lastDraw.fIndexCount % 6); |
| 150 | GrAssert(0 == lastDraw.fStartIndex); |
| 151 | |
| 152 | appendToPreviousDraw = lastDraw.fVertexBuffer == fCurrPoolVertexBuffer && |
| 153 | (fCurrQuad * 4 + lastDraw.fStartVertex) == fCurrPoolStartVertex; |
| 154 | if (appendToPreviousDraw) { |
| 155 | lastDraw.fVertexCount += 4; |
| 156 | lastDraw.fIndexCount += 6; |
| 157 | fCurrQuad += 1; |
| 158 | GrAssert(0 == fUsedReservedVertexBytes); |
| 159 | fUsedReservedVertexBytes = 4 * vsize; |
| 160 | } |
| 161 | } |
| 162 | if (!appendToPreviousDraw) { |
| 163 | this->setIndexSourceToBuffer(fQuadIndexBuffer); |
| 164 | drawIndexed(kTriangles_PrimitiveType, 0, 0, 4, 6); |
| 165 | fCurrQuad = 1; |
| 166 | fLastRectVertexLayout = layout; |
| 167 | } |
| 168 | if (disabledClip) { |
| 169 | this->enableState(kClip_StateBit); |
| 170 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 171 | } else { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 172 | INHERITED::drawRect(rect, matrix, stageEnableBitfield, srcRects, srcMatrices); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 176 | void GrInOrderDrawBuffer::drawIndexed(GrPrimitiveType primitiveType, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 177 | int startVertex, |
| 178 | int startIndex, |
| 179 | int vertexCount, |
| 180 | int indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 181 | |
| 182 | if (!vertexCount || !indexCount) { |
| 183 | return; |
| 184 | } |
| 185 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 186 | fCurrQuad = 0; |
| 187 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 188 | Draw& draw = fDraws.push_back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 189 | draw.fPrimitiveType = primitiveType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 190 | draw.fStartVertex = startVertex; |
| 191 | draw.fStartIndex = startIndex; |
| 192 | draw.fVertexCount = vertexCount; |
| 193 | draw.fIndexCount = indexCount; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 194 | |
| 195 | draw.fClipChanged = this->needsNewClip(); |
| 196 | if (draw.fClipChanged) { |
| 197 | this->pushClip(); |
| 198 | } |
| 199 | |
| 200 | draw.fStateChanged = this->needsNewState(); |
| 201 | if (draw.fStateChanged) { |
| 202 | this->pushState(); |
| 203 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 204 | |
| 205 | draw.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 206 | switch (fGeometrySrc.fVertexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 207 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 208 | draw.fVertexBuffer = fGeometrySrc.fVertexBuffer; |
| 209 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 210 | case kReserved_GeometrySrcType: { |
| 211 | size_t vertexBytes = (vertexCount + startVertex) * |
| 212 | VertexSize(fGeometrySrc.fVertexLayout); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 213 | fUsedReservedVertexBytes = GrMax(fUsedReservedVertexBytes, vertexBytes); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 214 | } // fallthrough |
| 215 | case kArray_GeometrySrcType: |
| 216 | draw.fVertexBuffer = fCurrPoolVertexBuffer; |
| 217 | draw.fStartVertex += fCurrPoolStartVertex; |
| 218 | break; |
| 219 | default: |
| 220 | GrCrash("unknown geom src type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 221 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 222 | draw.fVertexBuffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 223 | |
| 224 | switch (fGeometrySrc.fIndexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 225 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 226 | draw.fIndexBuffer = fGeometrySrc.fIndexBuffer; |
| 227 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 228 | case kReserved_GeometrySrcType: { |
| 229 | size_t indexBytes = (indexCount + startIndex) * sizeof(uint16_t); |
| 230 | fUsedReservedIndexBytes = GrMax(fUsedReservedIndexBytes, indexBytes); |
| 231 | } // fallthrough |
| 232 | case kArray_GeometrySrcType: |
| 233 | draw.fIndexBuffer = fCurrPoolIndexBuffer; |
| 234 | draw.fStartIndex += fCurrPoolStartVertex; |
| 235 | break; |
| 236 | default: |
| 237 | GrCrash("unknown geom src type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 238 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 239 | draw.fIndexBuffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 240 | } |
| 241 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 242 | void GrInOrderDrawBuffer::drawNonIndexed(GrPrimitiveType primitiveType, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 243 | int startVertex, |
| 244 | int vertexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 245 | if (!vertexCount) { |
| 246 | return; |
| 247 | } |
| 248 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 249 | fCurrQuad = 0; |
| 250 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 251 | Draw& draw = fDraws.push_back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 252 | draw.fPrimitiveType = primitiveType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 253 | draw.fStartVertex = startVertex; |
| 254 | draw.fStartIndex = 0; |
| 255 | draw.fVertexCount = vertexCount; |
| 256 | draw.fIndexCount = 0; |
| 257 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 258 | draw.fClipChanged = this->needsNewClip(); |
| 259 | if (draw.fClipChanged) { |
| 260 | this->pushClip(); |
| 261 | } |
| 262 | |
| 263 | draw.fStateChanged = this->needsNewState(); |
| 264 | if (draw.fStateChanged) { |
| 265 | this->pushState(); |
| 266 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 267 | |
| 268 | draw.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 269 | switch (fGeometrySrc.fVertexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 270 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 271 | draw.fVertexBuffer = fGeometrySrc.fVertexBuffer; |
| 272 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 273 | case kReserved_GeometrySrcType: { |
| 274 | size_t vertexBytes = (vertexCount + startVertex) * |
| 275 | VertexSize(fGeometrySrc.fVertexLayout); |
| 276 | fUsedReservedVertexBytes = GrMax(fUsedReservedVertexBytes, |
| 277 | vertexBytes); |
| 278 | } // fallthrough |
| 279 | case kArray_GeometrySrcType: |
| 280 | draw.fVertexBuffer = fCurrPoolVertexBuffer; |
| 281 | draw.fStartVertex += fCurrPoolStartVertex; |
| 282 | break; |
| 283 | default: |
| 284 | GrCrash("unknown geom src type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 285 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 286 | draw.fVertexBuffer->ref(); |
| 287 | draw.fIndexBuffer = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void GrInOrderDrawBuffer::reset() { |
| 291 | GrAssert(!fReservedGeometry.fLocked); |
| 292 | uint32_t numStates = fStates.count(); |
| 293 | for (uint32_t i = 0; i < numStates; ++i) { |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 294 | const DrState& dstate = this->accessSavedDrawState(fStates[i]); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 295 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 296 | GrSafeUnref(dstate.fTextures[s]); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 297 | } |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 298 | GrSafeUnref(dstate.fRenderTarget); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 299 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 300 | int numDraws = fDraws.count(); |
| 301 | for (int d = 0; d < numDraws; ++d) { |
| 302 | // we always have a VB, but not always an IB |
| 303 | GrAssert(NULL != fDraws[d].fVertexBuffer); |
| 304 | fDraws[d].fVertexBuffer->unref(); |
| 305 | GrSafeUnref(fDraws[d].fIndexBuffer); |
| 306 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 307 | fDraws.reset(); |
| 308 | fStates.reset(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 309 | |
| 310 | fVertexPool.reset(); |
| 311 | fIndexPool.reset(); |
| 312 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 313 | fClips.reset(); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 314 | |
| 315 | fCurrQuad = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void GrInOrderDrawBuffer::playback(GrDrawTarget* target) { |
| 319 | GrAssert(NULL != target); |
| 320 | GrAssert(target != this); // not considered and why? |
| 321 | |
| 322 | uint32_t numDraws = fDraws.count(); |
| 323 | if (!numDraws) { |
| 324 | return; |
| 325 | } |
| 326 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 327 | fVertexPool.unlock(); |
| 328 | fIndexPool.unlock(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 329 | |
| 330 | GrDrawTarget::AutoStateRestore asr(target); |
| 331 | GrDrawTarget::AutoClipRestore acr(target); |
| 332 | // important to not mess with reserve/lock geometry in the target with this |
| 333 | // on the stack. |
| 334 | GrDrawTarget::AutoGeometrySrcRestore agsr(target); |
| 335 | |
| 336 | uint32_t currState = ~0; |
| 337 | uint32_t currClip = ~0; |
| 338 | |
| 339 | for (uint32_t i = 0; i < numDraws; ++i) { |
| 340 | const Draw& draw = fDraws[i]; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 341 | if (draw.fStateChanged) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 342 | ++currState; |
| 343 | target->restoreDrawState(fStates[currState]); |
| 344 | } |
| 345 | if (draw.fClipChanged) { |
| 346 | ++currClip; |
| 347 | target->setClip(fClips[currClip]); |
| 348 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 349 | uint32_t vertexReserveCount = 0; |
| 350 | uint32_t indexReserveCount = 0; |
| 351 | |
| 352 | target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer); |
| 353 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 354 | if (draw.fIndexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 355 | target->setIndexSourceToBuffer(draw.fIndexBuffer); |
| 356 | } |
| 357 | |
| 358 | if (draw.fIndexCount) { |
| 359 | target->drawIndexed(draw.fPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 360 | draw.fStartVertex, |
| 361 | draw.fStartIndex, |
| 362 | draw.fVertexCount, |
| 363 | draw.fIndexCount); |
| 364 | } else { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 365 | target->drawNonIndexed(draw.fPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 366 | draw.fStartVertex, |
| 367 | draw.fVertexCount); |
| 368 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 369 | if (vertexReserveCount || indexReserveCount) { |
| 370 | target->releaseReservedGeometry(); |
| 371 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 376 | int* vertexCount, |
| 377 | int* indexCount) const { |
| 378 | // we will recommend a flush if the data could fit in a single |
| 379 | // preallocated buffer but none are left and it can't fit |
| 380 | // in the current buffer (which may not be prealloced). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 381 | bool flush = false; |
| 382 | if (NULL != indexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 383 | int32_t currIndices = fIndexPool.currentBufferIndices(); |
| 384 | if (*indexCount > currIndices && |
| 385 | (!fIndexPool.preallocatedBuffersRemaining() && |
| 386 | *indexCount <= fIndexPool.preallocatedBufferIndices())) { |
| 387 | |
| 388 | flush = true; |
| 389 | } |
| 390 | *indexCount = currIndices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 391 | } |
| 392 | if (NULL != vertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 393 | int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout); |
| 394 | if (*vertexCount > currVertices && |
| 395 | (!fVertexPool.preallocatedBuffersRemaining() && |
| 396 | *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 397 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 398 | flush = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 399 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 400 | *vertexCount = currVertices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 401 | } |
| 402 | return flush; |
| 403 | } |
| 404 | |
| 405 | bool GrInOrderDrawBuffer::acquireGeometryHelper(GrVertexLayout vertexLayout, |
| 406 | void** vertices, |
| 407 | void** indices) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 408 | GrAssert(!fReservedGeometry.fLocked); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 409 | if (fReservedGeometry.fVertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 410 | GrAssert(NULL != vertices); |
| 411 | GrAssert(0 == fReservedVertexBytes); |
| 412 | GrAssert(0 == fUsedReservedVertexBytes); |
| 413 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 414 | fReservedVertexBytes = VertexSize(vertexLayout) * |
| 415 | fReservedGeometry.fVertexCount; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 416 | *vertices = fVertexPool.makeSpace(vertexLayout, |
| 417 | fReservedGeometry.fVertexCount, |
| 418 | &fCurrPoolVertexBuffer, |
| 419 | &fCurrPoolStartVertex); |
| 420 | if (NULL == *vertices) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 421 | return false; |
| 422 | } |
| 423 | } |
| 424 | if (fReservedGeometry.fIndexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 425 | GrAssert(NULL != indices); |
| 426 | GrAssert(0 == fReservedIndexBytes); |
| 427 | GrAssert(0 == fUsedReservedIndexBytes); |
| 428 | |
| 429 | *indices = fIndexPool.makeSpace(fReservedGeometry.fIndexCount, |
| 430 | &fCurrPoolIndexBuffer, |
| 431 | &fCurrPoolStartIndex); |
| 432 | if (NULL == *indices) { |
| 433 | fVertexPool.putBack(fReservedVertexBytes); |
| 434 | fReservedVertexBytes = 0; |
| 435 | fCurrPoolVertexBuffer = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 436 | return false; |
| 437 | } |
| 438 | } |
| 439 | return true; |
| 440 | } |
| 441 | |
| 442 | void GrInOrderDrawBuffer::releaseGeometryHelper() { |
| 443 | GrAssert(fUsedReservedVertexBytes <= fReservedVertexBytes); |
| 444 | GrAssert(fUsedReservedIndexBytes <= fReservedIndexBytes); |
| 445 | |
| 446 | size_t vertexSlack = fReservedVertexBytes - fUsedReservedVertexBytes; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 447 | fVertexPool.putBack(vertexSlack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 448 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 449 | size_t indexSlack = fReservedIndexBytes - fUsedReservedIndexBytes; |
| 450 | fIndexPool.putBack(indexSlack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 451 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 452 | fReservedVertexBytes = 0; |
| 453 | fReservedIndexBytes = 0; |
| 454 | fUsedReservedVertexBytes = 0; |
| 455 | fUsedReservedIndexBytes = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 456 | fCurrPoolVertexBuffer = 0; |
| 457 | fCurrPoolStartVertex = 0; |
| 458 | |
| 459 | } |
| 460 | |
| 461 | void GrInOrderDrawBuffer::setVertexSourceToArrayHelper(const void* vertexArray, |
| 462 | int vertexCount) { |
| 463 | GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fVertexCount); |
| 464 | #if GR_DEBUG |
| 465 | bool success = |
| 466 | #endif |
| 467 | fVertexPool.appendVertices(fGeometrySrc.fVertexLayout, |
| 468 | vertexCount, |
| 469 | vertexArray, |
| 470 | &fCurrPoolVertexBuffer, |
| 471 | &fCurrPoolStartVertex); |
| 472 | GR_DEBUGASSERT(success); |
| 473 | } |
| 474 | |
| 475 | void GrInOrderDrawBuffer::setIndexSourceToArrayHelper(const void* indexArray, |
| 476 | int indexCount) { |
| 477 | GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fIndexCount); |
| 478 | #if GR_DEBUG |
| 479 | bool success = |
| 480 | #endif |
| 481 | fIndexPool.appendIndices(indexCount, |
| 482 | indexArray, |
| 483 | &fCurrPoolIndexBuffer, |
| 484 | &fCurrPoolStartIndex); |
| 485 | GR_DEBUGASSERT(success); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 486 | } |
| 487 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 488 | bool GrInOrderDrawBuffer::needsNewState() const { |
| 489 | if (fStates.empty()) { |
| 490 | return true; |
| 491 | } else { |
| 492 | const DrState& old = this->accessSavedDrawState(fStates.back()); |
| 493 | return old != fCurrDrawState; |
| 494 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 495 | } |
| 496 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 497 | void GrInOrderDrawBuffer::pushState() { |
| 498 | for (int s = 0; s < kNumStages; ++s) { |
| 499 | GrSafeRef(fCurrDrawState.fTextures[s]); |
| 500 | } |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame^] | 501 | GrSafeRef(fCurrDrawState.fRenderTarget); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 502 | this->saveCurrentDrawState(&fStates.push_back()); |
| 503 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 504 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 505 | bool GrInOrderDrawBuffer::needsNewClip() const { |
| 506 | if (fCurrDrawState.fFlagBits & kClip_StateBit) { |
| 507 | if (fClips.empty() || (fClipSet && fClips.back() != fClip)) { |
| 508 | return true; |
| 509 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 510 | } |
| 511 | return false; |
| 512 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 513 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 514 | void GrInOrderDrawBuffer::pushClip() { |
| 515 | fClips.push_back() = fClip; |
| 516 | fClipSet = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 517 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 518 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 519 | void GrInOrderDrawBuffer::clipWillBeSet(const GrClip& newClip) { |
| 520 | fClipSet = true; |
| 521 | } |