daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +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 | |
daniel@transgaming.com | dcfb1f7 | 2012-12-20 20:57:03 +0000 | [diff] [blame] | 7 | // VertexBuffer.cpp: Defines the abstract VertexBuffer class and VertexBufferInterface |
| 8 | // class with derivations, classes that perform graphics API agnostic vertex buffer operations. |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 9 | |
| 10 | #include "libGLESv2/renderer/VertexBuffer.h" |
| 11 | |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 12 | namespace rx |
| 13 | { |
| 14 | |
daniel@transgaming.com | dcfb1f7 | 2012-12-20 20:57:03 +0000 | [diff] [blame] | 15 | unsigned int VertexBuffer::mNextSerial = 1; |
| 16 | |
| 17 | VertexBuffer::VertexBuffer() |
| 18 | { |
| 19 | updateSerial(); |
| 20 | } |
| 21 | |
| 22 | VertexBuffer::~VertexBuffer() |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | void VertexBuffer::updateSerial() |
| 27 | { |
| 28 | mSerial = mNextSerial++; |
| 29 | } |
| 30 | |
| 31 | unsigned int VertexBuffer::getSerial() const |
| 32 | { |
| 33 | return mSerial; |
| 34 | } |
| 35 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 36 | VertexBufferInterface::VertexBufferInterface(rx::Renderer *renderer, bool dynamic) : mRenderer(renderer) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 37 | { |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 38 | mDynamic = dynamic; |
daniel@transgaming.com | a41d07f | 2012-12-20 20:55:57 +0000 | [diff] [blame] | 39 | mWritePosition = 0; |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 40 | mReservedSpace = 0; |
| 41 | |
| 42 | mVertexBuffer = renderer->createVertexBuffer(); |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 43 | } |
| 44 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 45 | VertexBufferInterface::~VertexBufferInterface() |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 46 | { |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 47 | delete mVertexBuffer; |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 48 | } |
| 49 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 50 | unsigned int VertexBufferInterface::getSerial() const |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 51 | { |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 52 | return mVertexBuffer->getSerial(); |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 53 | } |
| 54 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 55 | unsigned int VertexBufferInterface::getBufferSize() const |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 56 | { |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 57 | return mVertexBuffer->getBufferSize(); |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 58 | } |
| 59 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 60 | bool VertexBufferInterface::setBufferSize(unsigned int size) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 61 | { |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 62 | if (mVertexBuffer->getBufferSize() == 0) |
| 63 | { |
| 64 | return mVertexBuffer->initialize(size, mDynamic); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | return mVertexBuffer->setBufferSize(size); |
| 69 | } |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 70 | } |
| 71 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 72 | unsigned int VertexBufferInterface::getWritePosition() const |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 73 | { |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 74 | return mWritePosition; |
| 75 | } |
| 76 | |
| 77 | void VertexBufferInterface::setWritePosition(unsigned int writePosition) |
| 78 | { |
| 79 | mWritePosition = writePosition; |
| 80 | } |
| 81 | |
| 82 | bool VertexBufferInterface::discard() |
| 83 | { |
| 84 | return mVertexBuffer->discard(); |
| 85 | } |
| 86 | |
| 87 | int VertexBufferInterface::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances) |
| 88 | { |
| 89 | if (!reserveSpace(mReservedSpace)) |
| 90 | { |
| 91 | return -1; |
| 92 | } |
| 93 | mReservedSpace = 0; |
| 94 | |
| 95 | if (!mVertexBuffer->storeVertexAttributes(attrib, start, count, instances, mWritePosition)) |
| 96 | { |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | int oldWritePos = static_cast<int>(mWritePosition); |
| 101 | mWritePosition += mVertexBuffer->getSpaceRequired(attrib, count, instances); |
| 102 | |
| 103 | return oldWritePos; |
| 104 | } |
| 105 | |
| 106 | int VertexBufferInterface::storeRawData(const void* data, unsigned int size) |
| 107 | { |
| 108 | if (!reserveSpace(mReservedSpace)) |
| 109 | { |
| 110 | return -1; |
| 111 | } |
| 112 | mReservedSpace = 0; |
| 113 | |
| 114 | if (!mVertexBuffer->storeRawData(data, size, mWritePosition)) |
| 115 | { |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | int oldWritePos = static_cast<int>(mWritePosition); |
| 120 | mWritePosition += size; |
| 121 | |
| 122 | return oldWritePos; |
| 123 | } |
| 124 | |
| 125 | void VertexBufferInterface::reserveVertexSpace(const gl::VertexAttribute &attribute, GLsizei count, GLsizei instances) |
| 126 | { |
| 127 | mReservedSpace += mVertexBuffer->getSpaceRequired(attribute, count, instances); |
| 128 | } |
| 129 | |
| 130 | void VertexBufferInterface::reserveRawDataSpace(unsigned int size) |
| 131 | { |
| 132 | mReservedSpace += size; |
| 133 | } |
| 134 | |
| 135 | VertexBuffer* VertexBufferInterface::getVertexBuffer() const |
| 136 | { |
| 137 | return mVertexBuffer; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | StreamingVertexBufferInterface::StreamingVertexBufferInterface(rx::Renderer *renderer, std::size_t initialSize) : VertexBufferInterface(renderer, true) |
| 142 | { |
| 143 | setBufferSize(initialSize); |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 144 | } |
| 145 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 146 | StreamingVertexBufferInterface::~StreamingVertexBufferInterface() |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 147 | { |
| 148 | } |
| 149 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 150 | bool StreamingVertexBufferInterface::reserveSpace(unsigned int size) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 151 | { |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 152 | bool result = true; |
| 153 | unsigned int curBufferSize = getBufferSize(); |
| 154 | if (size > curBufferSize) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 155 | { |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 156 | result = setBufferSize(std::max(size, 3 * curBufferSize / 2)); |
| 157 | setWritePosition(0); |
| 158 | } |
| 159 | else if (getWritePosition() + size > curBufferSize) |
| 160 | { |
| 161 | if (!discard()) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 162 | { |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 163 | return false; |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 164 | } |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 165 | setWritePosition(0); |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 166 | } |
| 167 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 168 | return result; |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 169 | } |
| 170 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 171 | StaticVertexBufferInterface::StaticVertexBufferInterface(rx::Renderer *renderer) : VertexBufferInterface(renderer, false) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 172 | { |
| 173 | } |
| 174 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 175 | StaticVertexBufferInterface::~StaticVertexBufferInterface() |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 176 | { |
| 177 | } |
| 178 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 179 | int StaticVertexBufferInterface::lookupAttribute(const gl::VertexAttribute &attribute) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 180 | { |
| 181 | for (unsigned int element = 0; element < mCache.size(); element++) |
| 182 | { |
| 183 | if (mCache[element].type == attribute.mType && |
| 184 | mCache[element].size == attribute.mSize && |
| 185 | mCache[element].stride == attribute.stride() && |
| 186 | mCache[element].normalized == attribute.mNormalized) |
| 187 | { |
| 188 | if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride()) |
| 189 | { |
| 190 | return mCache[element].streamOffset; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return -1; |
| 196 | } |
| 197 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame^] | 198 | bool StaticVertexBufferInterface::reserveSpace(unsigned int size) |
| 199 | { |
| 200 | unsigned int curSize = getBufferSize(); |
| 201 | if (curSize == 0) |
| 202 | { |
| 203 | setBufferSize(size); |
| 204 | return true; |
| 205 | } |
| 206 | else if (curSize >= size) |
| 207 | { |
| 208 | return true; |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | UNREACHABLE(); // Static vertex buffers can't be resized |
| 213 | return false; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | int StaticVertexBufferInterface::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances) |
| 218 | { |
| 219 | int attributeOffset = attrib.mOffset % attrib.stride(); |
| 220 | VertexElement element = { attrib.mType, attrib.mSize, attrib.stride(), attrib.mNormalized, attributeOffset, getWritePosition() }; |
| 221 | mCache.push_back(element); |
| 222 | |
| 223 | return VertexBufferInterface::storeVertexAttributes(attrib, start, count, instances); |
| 224 | } |
| 225 | |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 226 | } |