| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 10 | #include "GrGLIndexBuffer.h" |
| 11 | #include "GrGpuGL.h" |
| 12 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 13 | #define GPUGL static_cast<GrGpuGL*>(getGpu()) |
| 14 | |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 15 | #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X) |
| 16 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 17 | GrGLIndexBuffer::GrGLIndexBuffer(GrGpuGL* gpu, |
| bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame^] | 18 | bool isWrapped, |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 19 | GrGLuint id, |
| 20 | size_t sizeInBytes, |
| 21 | bool dynamic) |
| bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame^] | 22 | : INHERITED(gpu, isWrapped, sizeInBytes, dynamic) |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 23 | , fBufferID(id) |
| 24 | , fLockPtr(NULL) { |
| 25 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 28 | void GrGLIndexBuffer::onRelease() { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | // make sure we've not been abandoned |
| bsalomon@google.com | 7283022 | 2013-01-23 20:25:22 +0000 | [diff] [blame^] | 30 | if (fBufferID && !this->isWrapped()) { |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 31 | GPUGL->notifyIndexBufferDelete(this); |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 32 | GL_CALL(DeleteBuffers(1, &fBufferID)); |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 33 | fBufferID = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | } |
| robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 35 | |
| 36 | INHERITED::onRelease(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 39 | void GrGLIndexBuffer::onAbandon() { |
| 40 | fBufferID = 0; |
| 41 | fLockPtr = NULL; |
| robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 42 | |
| 43 | INHERITED::onAbandon(); |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 46 | void GrGLIndexBuffer::bind() const { |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 47 | GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, fBufferID)); |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 48 | GPUGL->notifyIndexBufferBind(this); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 51 | GrGLuint GrGLIndexBuffer::bufferID() const { |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 52 | return fBufferID; |
| 53 | } |
| 54 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 55 | void* GrGLIndexBuffer::lock() { |
| 56 | GrAssert(fBufferID); |
| 57 | GrAssert(!isLocked()); |
| bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 58 | if (this->getGpu()->getCaps().bufferLockSupport()) { |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 59 | this->bind(); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 60 | // Let driver know it can discard the old data |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 61 | GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, |
| 62 | this->sizeInBytes(), |
| 63 | NULL, |
| 64 | this->dynamic() ? GR_GL_DYNAMIC_DRAW : |
| 65 | GR_GL_STATIC_DRAW)); |
| bsalomon@google.com | 56bfc5a | 2011-09-01 13:28:16 +0000 | [diff] [blame] | 66 | GR_GL_CALL_RET(GPUGL->glInterface(), |
| 67 | fLockPtr, |
| 68 | MapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, |
| 69 | GR_GL_WRITE_ONLY)); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 70 | |
| 71 | return fLockPtr; |
| 72 | } |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 76 | void* GrGLIndexBuffer::lockPtr() const { |
| 77 | return fLockPtr; |
| 78 | } |
| 79 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | void GrGLIndexBuffer::unlock() { |
| 81 | GrAssert(fBufferID); |
| 82 | GrAssert(isLocked()); |
| bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 83 | GrAssert(this->getGpu()->getCaps().bufferLockSupport()); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 85 | this->bind(); |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 86 | GL_CALL(UnmapBuffer(GR_GL_ELEMENT_ARRAY_BUFFER)); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 87 | fLockPtr = NULL; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | bool GrGLIndexBuffer::isLocked() const { |
| bsalomon@google.com | d850c18 | 2012-03-08 16:45:22 +0000 | [diff] [blame] | 91 | // this check causes a lot of noise in the gl log |
| 92 | #if 0 |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 93 | if (this->isValid() && this->getGpu()->getCaps().fBufferLockSupport) { |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 94 | this->bind(); |
| twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 95 | GrGLint mapped; |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 96 | GL_CALL(GetBufferParameteriv(GR_GL_ELEMENT_ARRAY_BUFFER, |
| 97 | GR_GL_BUFFER_MAPPED, &mapped)); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 98 | GrAssert(!!mapped == !!fLockPtr); |
| 99 | } |
| 100 | #endif |
| 101 | return NULL != fLockPtr; |
| 102 | } |
| 103 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 104 | bool GrGLIndexBuffer::updateData(const void* src, size_t srcSizeInBytes) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | GrAssert(fBufferID); |
| 106 | GrAssert(!isLocked()); |
| bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 107 | if (srcSizeInBytes > this->sizeInBytes()) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | return false; |
| 109 | } |
| bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 110 | this->bind(); |
| twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 111 | GrGLenum usage = dynamic() ? GR_GL_DYNAMIC_DRAW : GR_GL_STATIC_DRAW; |
| bsalomon@google.com | 5ffd5ba | 2012-03-01 15:29:07 +0000 | [diff] [blame] | 112 | |
| bsalomon@google.com | c1dd888 | 2012-03-02 20:36:18 +0000 | [diff] [blame] | 113 | #if GR_GL_USE_BUFFER_DATA_NULL_HINT |
| 114 | if (this->sizeInBytes() == srcSizeInBytes) { |
| 115 | GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, |
| 116 | srcSizeInBytes, src, usage)); |
| bsalomon@google.com | 5ffd5ba | 2012-03-01 15:29:07 +0000 | [diff] [blame] | 117 | } else { |
| bsalomon@google.com | c1dd888 | 2012-03-02 20:36:18 +0000 | [diff] [blame] | 118 | // Before we call glBufferSubData we give the driver a hint using |
| 119 | // glBufferData with NULL. This makes the old buffer contents |
| 120 | // inaccessible to future draws. The GPU may still be processing |
| 121 | // draws that reference the old contents. With this hint it can |
| 122 | // assign a different allocation for the new contents to avoid |
| 123 | // flushing the gpu past draws consuming the old contents. |
| 124 | GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, |
| 125 | this->sizeInBytes(), NULL, usage)); |
| 126 | GL_CALL(BufferSubData(GR_GL_ELEMENT_ARRAY_BUFFER, |
| 127 | 0, srcSizeInBytes, src)); |
| bsalomon@google.com | 5ffd5ba | 2012-03-01 15:29:07 +0000 | [diff] [blame] | 128 | } |
| bsalomon@google.com | c1dd888 | 2012-03-02 20:36:18 +0000 | [diff] [blame] | 129 | #else |
| 130 | // Note that we're cheating on the size here. Currently no methods |
| 131 | // allow a partial update that preserves contents of non-updated |
| 132 | // portions of the buffer (lock() does a glBufferData(..size, NULL..)) |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 133 | GL_CALL(BufferData(GR_GL_ELEMENT_ARRAY_BUFFER, |
| bsalomon@google.com | c1dd888 | 2012-03-02 20:36:18 +0000 | [diff] [blame] | 134 | srcSizeInBytes, src, usage)); |
| 135 | #endif |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 136 | return true; |
| 137 | } |