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); |
| 78 | ANGLE_CHECK_HR(GetImplAs<ContextD3D>(context), !check, |
| 79 | "Mapping of internal index buffer would cause an integer overflow.", |
| 80 | E_OUTOFMEMORY); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 81 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame^] | 82 | angle::Result error = mIndexBuffer->mapBuffer(context, mWritePosition, size, outMappedMemory); |
| 83 | if (error == angle::Result::Stop()) |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 84 | { |
| 85 | if (outMappedMemory) |
| 86 | { |
Yunchao He | d7297bf | 2017-04-19 15:27:10 +0800 | [diff] [blame] | 87 | *outMappedMemory = nullptr; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 88 | } |
Geoff Lang | c9e69b1 | 2014-09-08 16:06:25 -0400 | [diff] [blame] | 89 | return error; |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 90 | } |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 91 | |
Geoff Lang | a36ead4 | 2013-08-02 11:54:08 -0400 | [diff] [blame] | 92 | if (streamOffset) |
| 93 | { |
| 94 | *streamOffset = mWritePosition; |
| 95 | } |
| 96 | |
| 97 | mWritePosition += size; |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame^] | 98 | return angle::Result::Continue(); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame^] | 101 | angle::Result IndexBufferInterface::unmapBuffer(const gl::Context *context) |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 102 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 103 | return mIndexBuffer->unmapBuffer(context); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | IndexBuffer * IndexBufferInterface::getIndexBuffer() const |
| 107 | { |
| 108 | return mIndexBuffer; |
| 109 | } |
| 110 | |
| 111 | unsigned int IndexBufferInterface::getWritePosition() const |
| 112 | { |
| 113 | return mWritePosition; |
| 114 | } |
| 115 | |
| 116 | void IndexBufferInterface::setWritePosition(unsigned int writePosition) |
| 117 | { |
| 118 | mWritePosition = writePosition; |
| 119 | } |
| 120 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame^] | 121 | angle::Result IndexBufferInterface::discard(const gl::Context *context) |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 122 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 123 | return mIndexBuffer->discard(context); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame^] | 126 | angle::Result IndexBufferInterface::setBufferSize(const gl::Context *context, |
| 127 | unsigned int bufferSize, |
| 128 | GLenum indexType) |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 129 | { |
| 130 | if (mIndexBuffer->getBufferSize() == 0) |
| 131 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 132 | return mIndexBuffer->initialize(context, bufferSize, indexType, mDynamic); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 133 | } |
| 134 | else |
| 135 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 136 | return mIndexBuffer->setSize(context, bufferSize, indexType); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 140 | StreamingIndexBufferInterface::StreamingIndexBufferInterface(BufferFactoryD3D *factory) |
| 141 | : IndexBufferInterface(factory, true) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 142 | { |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 143 | } |
| 144 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 145 | StreamingIndexBufferInterface::~StreamingIndexBufferInterface() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 146 | { |
| 147 | } |
| 148 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame^] | 149 | angle::Result StreamingIndexBufferInterface::reserveBufferSpace(const gl::Context *context, |
| 150 | unsigned int size, |
| 151 | GLenum indexType) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 152 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 153 | unsigned int curBufferSize = getBufferSize(); |
Geoff Lang | d81cf64 | 2013-07-09 16:02:30 -0400 | [diff] [blame] | 154 | unsigned int writePos = getWritePosition(); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 155 | if (size > curBufferSize) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 156 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 157 | ANGLE_TRY(setBufferSize(context, std::max(size, 2 * curBufferSize), indexType)); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 158 | setWritePosition(0); |
| 159 | } |
Geoff Lang | d81cf64 | 2013-07-09 16:02:30 -0400 | [diff] [blame] | 160 | else if (writePos + size > curBufferSize || writePos + size < writePos) |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 161 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 162 | ANGLE_TRY(discard(context)); |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 163 | setWritePosition(0); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame^] | 166 | return angle::Result::Continue(); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 167 | } |
| 168 | |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 169 | |
Jamie Madill | fd1bf4e | 2015-03-31 09:46:02 -0400 | [diff] [blame] | 170 | StaticIndexBufferInterface::StaticIndexBufferInterface(BufferFactoryD3D *factory) |
| 171 | : IndexBufferInterface(factory, false) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 172 | { |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 173 | } |
| 174 | |
daniel@transgaming.com | 50cc725 | 2012-12-20 21:09:23 +0000 | [diff] [blame] | 175 | StaticIndexBufferInterface::~StaticIndexBufferInterface() |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 176 | { |
| 177 | } |
| 178 | |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame^] | 179 | angle::Result StaticIndexBufferInterface::reserveBufferSpace(const gl::Context *context, |
| 180 | unsigned int size, |
| 181 | GLenum indexType) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 182 | { |
daniel@transgaming.com | 1e3a804 | 2012-12-20 21:09:55 +0000 | [diff] [blame] | 183 | unsigned int curSize = getBufferSize(); |
| 184 | if (curSize == 0) |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 185 | { |
Jamie Madill | b156590 | 2018-07-27 08:12:48 -0400 | [diff] [blame] | 186 | return setBufferSize(context, size, indexType); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 187 | } |
Jamie Madill | ec1fe5b | 2018-08-10 10:05:52 -0400 | [diff] [blame^] | 188 | |
| 189 | ASSERT(curSize >= size && indexType == getIndexType()); |
| 190 | return angle::Result::Continue(); |
daniel@transgaming.com | 955377e | 2012-12-20 21:09:15 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Jamie Madill | 87718a8 | 2017-11-13 15:01:19 -0500 | [diff] [blame] | 193 | } // namespace rx |