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