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 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 10 | #include "libANGLE/renderer/d3d/IndexBuffer.h" |
| 11 | #include "libANGLE/renderer/d3d/RendererD3D.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 | |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 38 | IndexBufferInterface::IndexBufferInterface(BufferFactoryD3D *factory, bool dynamic) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 39 | { |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 40 | mIndexBuffer = factory->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 | |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 69 | gl::Error 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 | { |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 74 | return gl::OutOfMemory() |
| 75 | << "Mapping of internal index buffer would cause an integer overflow."; |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Geoff Lang | c9e69b1 | 2014-09-08 16:06:25 -0400 | [diff] [blame] | 78 | gl::Error error = mIndexBuffer->mapBuffer(mWritePosition, size, outMappedMemory); |
| 79 | if (error.isError()) |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 80 | { |
| 81 | if (outMappedMemory) |
| 82 | { |
Yunchao He | d7297bf | 2017-04-19 15:27:10 +0800 | [diff] [blame] | 83 | *outMappedMemory = nullptr; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 84 | } |
Geoff Lang | c9e69b1 | 2014-09-08 16:06:25 -0400 | [diff] [blame] | 85 | return error; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 86 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 87 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 88 | if (streamOffset) |
| 89 | { |
| 90 | *streamOffset = mWritePosition; |
| 91 | } |
| 92 | |
| 93 | mWritePosition += size; |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 94 | return gl::NoError(); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Geoff Lang | c9e69b1 | 2014-09-08 16:06:25 -0400 | [diff] [blame] | 97 | gl::Error IndexBufferInterface::unmapBuffer() |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 98 | { |
| 99 | return mIndexBuffer->unmapBuffer(); |
| 100 | } |
| 101 | |
| 102 | IndexBuffer * IndexBufferInterface::getIndexBuffer() const |
| 103 | { |
| 104 | return mIndexBuffer; |
| 105 | } |
| 106 | |
| 107 | unsigned int IndexBufferInterface::getWritePosition() const |
| 108 | { |
| 109 | return mWritePosition; |
| 110 | } |
| 111 | |
| 112 | void IndexBufferInterface::setWritePosition(unsigned int writePosition) |
| 113 | { |
| 114 | mWritePosition = writePosition; |
| 115 | } |
| 116 | |
Geoff Lang | c9e69b1 | 2014-09-08 16:06:25 -0400 | [diff] [blame] | 117 | gl::Error IndexBufferInterface::discard() |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 118 | { |
| 119 | return mIndexBuffer->discard(); |
| 120 | } |
| 121 | |
Geoff Lang | c9e69b1 | 2014-09-08 16:06:25 -0400 | [diff] [blame] | 122 | gl::Error IndexBufferInterface::setBufferSize(unsigned int bufferSize, GLenum indexType) |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 123 | { |
| 124 | if (mIndexBuffer->getBufferSize() == 0) |
| 125 | { |
| 126 | return mIndexBuffer->initialize(bufferSize, indexType, mDynamic); |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | return mIndexBuffer->setSize(bufferSize, indexType); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 134 | StreamingIndexBufferInterface::StreamingIndexBufferInterface(BufferFactoryD3D *factory) |
| 135 | : IndexBufferInterface(factory, true) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 136 | { |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 137 | } |
| 138 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 139 | StreamingIndexBufferInterface::~StreamingIndexBufferInterface() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 140 | { |
| 141 | } |
| 142 | |
Geoff Lang | c9e69b1 | 2014-09-08 16:06:25 -0400 | [diff] [blame] | 143 | gl::Error StreamingIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType) |
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 | unsigned int curBufferSize = getBufferSize(); |
Geoff Lang | d81cf64 | 2013-07-09 16:02:30 -0400 | [diff] [blame] | 146 | unsigned int writePos = getWritePosition(); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 147 | if (size > curBufferSize) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 148 | { |
Jamie Madill | 87718a8 | 2017-11-13 15:01:19 -0500 | [diff] [blame^] | 149 | ANGLE_TRY(setBufferSize(std::max(size, 2 * curBufferSize), indexType)); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 150 | setWritePosition(0); |
| 151 | } |
Geoff Lang | d81cf64 | 2013-07-09 16:02:30 -0400 | [diff] [blame] | 152 | else if (writePos + size > curBufferSize || writePos + size < writePos) |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 153 | { |
Jamie Madill | 87718a8 | 2017-11-13 15:01:19 -0500 | [diff] [blame^] | 154 | ANGLE_TRY(discard()); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 155 | setWritePosition(0); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 156 | } |
| 157 | |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 158 | return gl::NoError(); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 159 | } |
| 160 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 161 | |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 162 | StaticIndexBufferInterface::StaticIndexBufferInterface(BufferFactoryD3D *factory) |
| 163 | : IndexBufferInterface(factory, 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 | |
Geoff Lang | c9e69b1 | 2014-09-08 16:06:25 -0400 | [diff] [blame] | 171 | gl::Error 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 | { |
He Yunchao | acd1898 | 2017-01-04 10:46:42 +0800 | [diff] [blame] | 180 | return gl::NoError(); |
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 | UNREACHABLE(); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 185 | return gl::InternalError() << "Internal static index buffers can't be resized"; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 186 | } |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Jamie Madill | 87718a8 | 2017-11-13 15:01:19 -0500 | [diff] [blame^] | 189 | } // namespace rx |