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" |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame] | 11 | |
| 12 | #include "libANGLE/Context.h" |
| 13 | #include "libANGLE/renderer/d3d/ContextD3D.h" |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 14 | |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 15 | namespace rx |
| 16 | { |
| 17 | |
daniel@transgaming.com | 2befccd | 2012-12-20 21:09:31 +0000 | [diff] [blame] | 18 | unsigned int IndexBuffer::mNextSerial = 1; |
| 19 | |
| 20 | IndexBuffer::IndexBuffer() |
| 21 | { |
| 22 | updateSerial(); |
| 23 | } |
| 24 | |
| 25 | IndexBuffer::~IndexBuffer() |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | unsigned int IndexBuffer::getSerial() const |
| 30 | { |
| 31 | return mSerial; |
| 32 | } |
| 33 | |
| 34 | void IndexBuffer::updateSerial() |
| 35 | { |
| 36 | mSerial = mNextSerial++; |
| 37 | } |
| 38 | |
| 39 | |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 40 | IndexBufferInterface::IndexBufferInterface(BufferFactoryD3D *factory, bool dynamic) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 41 | { |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 42 | mIndexBuffer = factory->createIndexBuffer(); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 43 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 44 | mDynamic = dynamic; |
| 45 | mWritePosition = 0; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 46 | } |
| 47 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 48 | IndexBufferInterface::~IndexBufferInterface() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 49 | { |
| 50 | if (mIndexBuffer) |
| 51 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 52 | delete mIndexBuffer; |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 56 | GLenum IndexBufferInterface::getIndexType() const |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 57 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 58 | return mIndexBuffer->getIndexType(); |
| 59 | } |
| 60 | |
| 61 | unsigned int IndexBufferInterface::getBufferSize() const |
| 62 | { |
| 63 | return mIndexBuffer->getBufferSize(); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 64 | } |
| 65 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 66 | unsigned int IndexBufferInterface::getSerial() const |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 67 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 68 | return mIndexBuffer->getSerial(); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame] | 71 | angle::Result IndexBufferInterface::mapBuffer(const gl::Context *context, |
| 72 | unsigned int size, |
| 73 | void **outMappedMemory, |
| 74 | unsigned int *streamOffset) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 75 | { |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 76 | // Protect against integer overflow |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame] | 77 | bool check = (mWritePosition + size < mWritePosition); |
Jamie Madill | e39e8f4 | 2018-10-05 08:17:38 -0400 | [diff] [blame^] | 78 | ANGLE_CHECK(GetImplAs<ContextD3D>(context), !check, |
| 79 | "Mapping of internal index buffer would cause an integer overflow.", E_OUTOFMEMORY); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 80 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame] | 81 | angle::Result error = mIndexBuffer->mapBuffer(context, mWritePosition, size, outMappedMemory); |
| 82 | if (error == angle::Result::Stop()) |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 83 | { |
| 84 | if (outMappedMemory) |
| 85 | { |
Yunchao He | d7297bf | 2017-04-19 15:27:10 +0800 | [diff] [blame] | 86 | *outMappedMemory = nullptr; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 87 | } |
Geoff Lang | c9e69b1 | 2014-09-08 16:06:25 -0400 | [diff] [blame] | 88 | return error; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 89 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 90 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 91 | if (streamOffset) |
| 92 | { |
| 93 | *streamOffset = mWritePosition; |
| 94 | } |
| 95 | |
| 96 | mWritePosition += size; |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame] | 97 | return angle::Result::Continue(); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame] | 100 | angle::Result IndexBufferInterface::unmapBuffer(const gl::Context *context) |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 101 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 102 | return mIndexBuffer->unmapBuffer(context); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | IndexBuffer * IndexBufferInterface::getIndexBuffer() const |
| 106 | { |
| 107 | return mIndexBuffer; |
| 108 | } |
| 109 | |
| 110 | unsigned int IndexBufferInterface::getWritePosition() const |
| 111 | { |
| 112 | return mWritePosition; |
| 113 | } |
| 114 | |
| 115 | void IndexBufferInterface::setWritePosition(unsigned int writePosition) |
| 116 | { |
| 117 | mWritePosition = writePosition; |
| 118 | } |
| 119 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame] | 120 | angle::Result IndexBufferInterface::discard(const gl::Context *context) |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 121 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 122 | return mIndexBuffer->discard(context); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame] | 125 | angle::Result IndexBufferInterface::setBufferSize(const gl::Context *context, |
| 126 | unsigned int bufferSize, |
| 127 | GLenum indexType) |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 128 | { |
| 129 | if (mIndexBuffer->getBufferSize() == 0) |
| 130 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 131 | return mIndexBuffer->initialize(context, bufferSize, indexType, mDynamic); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 132 | } |
| 133 | else |
| 134 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 135 | return mIndexBuffer->setSize(context, bufferSize, indexType); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 139 | StreamingIndexBufferInterface::StreamingIndexBufferInterface(BufferFactoryD3D *factory) |
| 140 | : IndexBufferInterface(factory, true) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 141 | { |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 142 | } |
| 143 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 144 | StreamingIndexBufferInterface::~StreamingIndexBufferInterface() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 145 | { |
| 146 | } |
| 147 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame] | 148 | angle::Result StreamingIndexBufferInterface::reserveBufferSpace(const gl::Context *context, |
| 149 | unsigned int size, |
| 150 | GLenum indexType) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 151 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 152 | unsigned int curBufferSize = getBufferSize(); |
Geoff Lang | d81cf64 | 2013-07-09 16:02:30 -0400 | [diff] [blame] | 153 | unsigned int writePos = getWritePosition(); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 154 | if (size > curBufferSize) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 155 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 156 | ANGLE_TRY(setBufferSize(context, std::max(size, 2 * curBufferSize), indexType)); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 157 | setWritePosition(0); |
| 158 | } |
Geoff Lang | d81cf64 | 2013-07-09 16:02:30 -0400 | [diff] [blame] | 159 | else if (writePos + size > curBufferSize || writePos + size < writePos) |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 160 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 161 | ANGLE_TRY(discard(context)); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 162 | setWritePosition(0); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame] | 165 | return angle::Result::Continue(); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 166 | } |
| 167 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 168 | |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 169 | StaticIndexBufferInterface::StaticIndexBufferInterface(BufferFactoryD3D *factory) |
| 170 | : IndexBufferInterface(factory, false) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 171 | { |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 172 | } |
| 173 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 174 | StaticIndexBufferInterface::~StaticIndexBufferInterface() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 175 | { |
| 176 | } |
| 177 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame] | 178 | angle::Result StaticIndexBufferInterface::reserveBufferSpace(const gl::Context *context, |
| 179 | unsigned int size, |
| 180 | GLenum indexType) |
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 | unsigned int curSize = getBufferSize(); |
| 183 | if (curSize == 0) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 184 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 185 | return setBufferSize(context, size, indexType); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 186 | } |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame] | 187 | |
| 188 | ASSERT(curSize >= size && indexType == getIndexType()); |
| 189 | return angle::Result::Continue(); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Jamie Madill | 87718a8 | 2017-11-13 15:01:19 -0500 | [diff] [blame] | 192 | } // namespace rx |