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