shannon.woods@transgaming.com | bdf2d80 | 2013-02-28 23:16:20 +0000 | [diff] [blame] | 1 | #include "precompiled.h" |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 2 | // |
| 3 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style license that can be |
| 5 | // found in the LICENSE file. |
| 6 | // |
| 7 | |
| 8 | // IndexBuffer.cpp: Defines the abstract IndexBuffer class and IndexBufferInterface |
| 9 | // class with derivations, classes that perform graphics API agnostic index buffer operations. |
| 10 | |
| 11 | #include "libGLESv2/renderer/IndexBuffer.h" |
shannon.woods@transgaming.com | 486d9e9 | 2013-02-28 23:15:41 +0000 | [diff] [blame] | 12 | #include "libGLESv2/renderer/Renderer.h" |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 13 | |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 14 | namespace rx |
| 15 | { |
| 16 | |
daniel@transgaming.com | 2befccd | 2012-12-20 21:09:31 +0000 | [diff] [blame] | 17 | unsigned int IndexBuffer::mNextSerial = 1; |
| 18 | |
| 19 | IndexBuffer::IndexBuffer() |
| 20 | { |
| 21 | updateSerial(); |
| 22 | } |
| 23 | |
| 24 | IndexBuffer::~IndexBuffer() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | unsigned int IndexBuffer::getSerial() const |
| 29 | { |
| 30 | return mSerial; |
| 31 | } |
| 32 | |
| 33 | void IndexBuffer::updateSerial() |
| 34 | { |
| 35 | mSerial = mNextSerial++; |
| 36 | } |
| 37 | |
| 38 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 39 | IndexBufferInterface::IndexBufferInterface(Renderer *renderer, bool dynamic) : mRenderer(renderer) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 40 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 41 | mIndexBuffer = renderer->createIndexBuffer(); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 42 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 43 | mDynamic = dynamic; |
| 44 | mWritePosition = 0; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 45 | } |
| 46 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 47 | IndexBufferInterface::~IndexBufferInterface() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 48 | { |
| 49 | if (mIndexBuffer) |
| 50 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 51 | delete mIndexBuffer; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 55 | GLenum IndexBufferInterface::getIndexType() const |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 56 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 57 | return mIndexBuffer->getIndexType(); |
| 58 | } |
| 59 | |
| 60 | unsigned int IndexBufferInterface::getBufferSize() const |
| 61 | { |
| 62 | return mIndexBuffer->getBufferSize(); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 63 | } |
| 64 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 65 | unsigned int IndexBufferInterface::getSerial() const |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 66 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 67 | return mIndexBuffer->getSerial(); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 70 | bool IndexBufferInterface::mapBuffer(unsigned int size, void** outMappedMemory, unsigned int *streamOffset) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 71 | { |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 72 | // Protect against integer overflow |
| 73 | if (mWritePosition + size < mWritePosition) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 74 | { |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 75 | return false; |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 78 | if (!mIndexBuffer->mapBuffer(mWritePosition, size, outMappedMemory)) |
| 79 | { |
| 80 | if (outMappedMemory) |
| 81 | { |
| 82 | *outMappedMemory = NULL; |
| 83 | } |
| 84 | return false; |
| 85 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 86 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 87 | if (streamOffset) |
| 88 | { |
| 89 | *streamOffset = mWritePosition; |
| 90 | } |
| 91 | |
| 92 | mWritePosition += size; |
| 93 | return true; |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | bool IndexBufferInterface::unmapBuffer() |
| 97 | { |
| 98 | return mIndexBuffer->unmapBuffer(); |
| 99 | } |
| 100 | |
| 101 | IndexBuffer * IndexBufferInterface::getIndexBuffer() const |
| 102 | { |
| 103 | return mIndexBuffer; |
| 104 | } |
| 105 | |
| 106 | unsigned int IndexBufferInterface::getWritePosition() const |
| 107 | { |
| 108 | return mWritePosition; |
| 109 | } |
| 110 | |
| 111 | void IndexBufferInterface::setWritePosition(unsigned int writePosition) |
| 112 | { |
| 113 | mWritePosition = writePosition; |
| 114 | } |
| 115 | |
| 116 | bool IndexBufferInterface::discard() |
| 117 | { |
| 118 | return mIndexBuffer->discard(); |
| 119 | } |
| 120 | |
| 121 | bool IndexBufferInterface::setBufferSize(unsigned int bufferSize, GLenum indexType) |
| 122 | { |
| 123 | if (mIndexBuffer->getBufferSize() == 0) |
| 124 | { |
| 125 | return mIndexBuffer->initialize(bufferSize, indexType, mDynamic); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | return mIndexBuffer->setSize(bufferSize, indexType); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 133 | StreamingIndexBufferInterface::StreamingIndexBufferInterface(Renderer *renderer) : IndexBufferInterface(renderer, true) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 134 | { |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 135 | } |
| 136 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 137 | StreamingIndexBufferInterface::~StreamingIndexBufferInterface() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 138 | { |
| 139 | } |
| 140 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 141 | bool StreamingIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 142 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 143 | bool result = true; |
| 144 | unsigned int curBufferSize = getBufferSize(); |
Geoff Lang | d81cf64 | 2013-07-09 16:02:30 -0400 | [diff] [blame] | 145 | unsigned int writePos = getWritePosition(); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 146 | if (size > curBufferSize) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 147 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 148 | result = setBufferSize(std::max(size, 2 * curBufferSize), indexType); |
| 149 | setWritePosition(0); |
| 150 | } |
Geoff Lang | d81cf64 | 2013-07-09 16:02:30 -0400 | [diff] [blame] | 151 | else if (writePos + size > curBufferSize || writePos + size < writePos) |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 152 | { |
| 153 | if (!discard()) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 154 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 155 | return false; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 156 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 157 | setWritePosition(0); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 158 | } |
| 159 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 160 | return result; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 161 | } |
| 162 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 163 | |
| 164 | StaticIndexBufferInterface::StaticIndexBufferInterface(Renderer *renderer) : IndexBufferInterface(renderer, false) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 165 | { |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 166 | } |
| 167 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 168 | StaticIndexBufferInterface::~StaticIndexBufferInterface() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 169 | { |
| 170 | } |
| 171 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 172 | bool StaticIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 173 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 174 | unsigned int curSize = getBufferSize(); |
| 175 | if (curSize == 0) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 176 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 177 | return setBufferSize(size, indexType); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 178 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 179 | else if (curSize >= size && indexType == getIndexType()) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 180 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 181 | return true; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 182 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 183 | else |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 184 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 185 | ERR("Static index buffers can't be resized"); |
| 186 | UNREACHABLE(); |
| 187 | return false; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 188 | } |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 191 | IndexRangeCache *StaticIndexBufferInterface::getIndexRangeCache() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 192 | { |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame] | 193 | return &mIndexRangeCache; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | } |