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(); |
Geoff Lang | d81cf64 | 2013-07-09 16:02:30 -0400 | [diff] [blame] | 133 | unsigned int writePos = getWritePosition(); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 134 | if (size > curBufferSize) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 135 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 136 | result = setBufferSize(std::max(size, 2 * curBufferSize), indexType); |
| 137 | setWritePosition(0); |
| 138 | } |
Geoff Lang | d81cf64 | 2013-07-09 16:02:30 -0400 | [diff] [blame] | 139 | else if (writePos + size > curBufferSize || writePos + size < writePos) |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 140 | { |
| 141 | if (!discard()) |
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 | return false; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 144 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 145 | setWritePosition(0); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 146 | } |
| 147 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 148 | return result; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 149 | } |
| 150 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 151 | |
| 152 | StaticIndexBufferInterface::StaticIndexBufferInterface(Renderer *renderer) : IndexBufferInterface(renderer, false) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 153 | { |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 154 | } |
| 155 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 156 | StaticIndexBufferInterface::~StaticIndexBufferInterface() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 157 | { |
| 158 | } |
| 159 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 160 | bool StaticIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 161 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 162 | unsigned int curSize = getBufferSize(); |
| 163 | if (curSize == 0) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 164 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 165 | return setBufferSize(size, indexType); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 166 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 167 | else if (curSize >= size && indexType == getIndexType()) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 168 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 169 | return true; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 170 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 171 | else |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 172 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 173 | ERR("Static index buffers can't be resized"); |
| 174 | UNREACHABLE(); |
| 175 | return false; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 176 | } |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame^] | 179 | IndexRangeCache *StaticIndexBufferInterface::getIndexRangeCache() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 180 | { |
Geoff Lang | f23eb28 | 2013-07-22 10:52:19 -0400 | [diff] [blame^] | 181 | return &mIndexRangeCache; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | } |