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