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 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 70 | int IndexBufferInterface::mapBuffer(unsigned int size, void** outMappedMemory) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 71 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 72 | if (!mIndexBuffer->mapBuffer(mWritePosition, size, outMappedMemory)) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 73 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 74 | *outMappedMemory = NULL; |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | int oldWritePos = static_cast<int>(mWritePosition); |
| 79 | mWritePosition += size; |
| 80 | |
| 81 | return oldWritePos; |
| 82 | } |
| 83 | |
| 84 | bool IndexBufferInterface::unmapBuffer() |
| 85 | { |
| 86 | return mIndexBuffer->unmapBuffer(); |
| 87 | } |
| 88 | |
| 89 | IndexBuffer * IndexBufferInterface::getIndexBuffer() const |
| 90 | { |
| 91 | return mIndexBuffer; |
| 92 | } |
| 93 | |
| 94 | unsigned int IndexBufferInterface::getWritePosition() const |
| 95 | { |
| 96 | return mWritePosition; |
| 97 | } |
| 98 | |
| 99 | void IndexBufferInterface::setWritePosition(unsigned int writePosition) |
| 100 | { |
| 101 | mWritePosition = writePosition; |
| 102 | } |
| 103 | |
| 104 | bool IndexBufferInterface::discard() |
| 105 | { |
| 106 | return mIndexBuffer->discard(); |
| 107 | } |
| 108 | |
| 109 | bool IndexBufferInterface::setBufferSize(unsigned int bufferSize, GLenum indexType) |
| 110 | { |
| 111 | if (mIndexBuffer->getBufferSize() == 0) |
| 112 | { |
| 113 | return mIndexBuffer->initialize(bufferSize, indexType, mDynamic); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | return mIndexBuffer->setSize(bufferSize, indexType); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 121 | StreamingIndexBufferInterface::StreamingIndexBufferInterface(Renderer *renderer) : IndexBufferInterface(renderer, true) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 122 | { |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 123 | } |
| 124 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 125 | StreamingIndexBufferInterface::~StreamingIndexBufferInterface() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 126 | { |
| 127 | } |
| 128 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 129 | bool StreamingIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 130 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 131 | bool result = true; |
| 132 | unsigned int curBufferSize = getBufferSize(); |
| 133 | if (size > curBufferSize) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 134 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 135 | result = setBufferSize(std::max(size, 2 * curBufferSize), indexType); |
| 136 | setWritePosition(0); |
| 137 | } |
| 138 | else if (getWritePosition() + size > curBufferSize) |
| 139 | { |
| 140 | if (!discard()) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 141 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 142 | return false; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 143 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 144 | setWritePosition(0); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 145 | } |
| 146 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 147 | return result; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 148 | } |
| 149 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 150 | |
| 151 | StaticIndexBufferInterface::StaticIndexBufferInterface(Renderer *renderer) : IndexBufferInterface(renderer, false) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 152 | { |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 153 | } |
| 154 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 155 | StaticIndexBufferInterface::~StaticIndexBufferInterface() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 156 | { |
| 157 | } |
| 158 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 159 | bool StaticIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 160 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 161 | unsigned int curSize = getBufferSize(); |
| 162 | if (curSize == 0) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 163 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 164 | return setBufferSize(size, indexType); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 165 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 166 | else if (curSize >= size && indexType == getIndexType()) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 167 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 168 | return true; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 169 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 170 | else |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 171 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 172 | ERR("Static index buffers can't be resized"); |
| 173 | UNREACHABLE(); |
| 174 | return false; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 175 | } |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 176 | } |
| 177 | |
shannon.woods@transgaming.com | 486d9e9 | 2013-02-28 23:15:41 +0000 | [diff] [blame] | 178 | unsigned int StaticIndexBufferInterface::lookupRange(intptr_t offset, GLsizei count, unsigned int *minIndex, unsigned int *maxIndex) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 179 | { |
| 180 | IndexRange range = {offset, count}; |
| 181 | |
| 182 | std::map<IndexRange, IndexResult>::iterator res = mCache.find(range); |
| 183 | |
| 184 | if (res == mCache.end()) |
| 185 | { |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | *minIndex = res->second.minIndex; |
| 190 | *maxIndex = res->second.maxIndex; |
| 191 | return res->second.streamOffset; |
| 192 | } |
| 193 | |
shannon.woods@transgaming.com | 486d9e9 | 2013-02-28 23:15:41 +0000 | [diff] [blame] | 194 | void StaticIndexBufferInterface::addRange(intptr_t offset, GLsizei count, unsigned int minIndex, unsigned int maxIndex, unsigned int streamOffset) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 195 | { |
| 196 | IndexRange indexRange = {offset, count}; |
| 197 | IndexResult indexResult = {minIndex, maxIndex, streamOffset}; |
| 198 | mCache[indexRange] = indexResult; |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | |