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 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame] | 109 | void VertexBufferInterface::reserveVertexSpace(const gl::VertexAttribute &attribute, GLsizei count, GLsizei instances) |
| 110 | { |
| 111 | mReservedSpace += mVertexBuffer->getSpaceRequired(attribute, count, instances); |
| 112 | } |
| 113 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame] | 114 | VertexBuffer* VertexBufferInterface::getVertexBuffer() const |
| 115 | { |
| 116 | return mVertexBuffer; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | StreamingVertexBufferInterface::StreamingVertexBufferInterface(rx::Renderer *renderer, std::size_t initialSize) : VertexBufferInterface(renderer, true) |
| 121 | { |
| 122 | setBufferSize(initialSize); |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 123 | } |
| 124 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 125 | StreamingVertexBufferInterface::~StreamingVertexBufferInterface() |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 126 | { |
| 127 | } |
| 128 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame] | 129 | bool StreamingVertexBufferInterface::reserveSpace(unsigned int size) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 130 | { |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame] | 131 | bool result = true; |
| 132 | unsigned int curBufferSize = getBufferSize(); |
| 133 | if (size > curBufferSize) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 134 | { |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame] | 135 | result = setBufferSize(std::max(size, 3 * curBufferSize / 2)); |
| 136 | setWritePosition(0); |
| 137 | } |
| 138 | else if (getWritePosition() + size > curBufferSize) |
| 139 | { |
| 140 | if (!discard()) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 141 | { |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame] | 142 | return false; |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 143 | } |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame] | 144 | setWritePosition(0); |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 145 | } |
| 146 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame] | 147 | return result; |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 148 | } |
| 149 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame] | 150 | StaticVertexBufferInterface::StaticVertexBufferInterface(rx::Renderer *renderer) : VertexBufferInterface(renderer, false) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 151 | { |
| 152 | } |
| 153 | |
daniel@transgaming.com | e4e4506 | 2012-12-20 20:56:53 +0000 | [diff] [blame] | 154 | StaticVertexBufferInterface::~StaticVertexBufferInterface() |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 155 | { |
| 156 | } |
| 157 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame] | 158 | int StaticVertexBufferInterface::lookupAttribute(const gl::VertexAttribute &attribute) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 159 | { |
| 160 | for (unsigned int element = 0; element < mCache.size(); element++) |
| 161 | { |
| 162 | if (mCache[element].type == attribute.mType && |
| 163 | mCache[element].size == attribute.mSize && |
| 164 | mCache[element].stride == attribute.stride() && |
shannon.woods%transgaming.com@gtempaccount.com | 8de4e6a | 2013-04-13 03:37:44 +0000 | [diff] [blame^] | 165 | mCache[element].normalized == attribute.mNormalized && |
| 166 | mCache[element].pureInteger == attribute.mPureInteger) |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 167 | { |
| 168 | if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride()) |
| 169 | { |
| 170 | return mCache[element].streamOffset; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return -1; |
| 176 | } |
| 177 | |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame] | 178 | bool StaticVertexBufferInterface::reserveSpace(unsigned int size) |
| 179 | { |
| 180 | unsigned int curSize = getBufferSize(); |
| 181 | if (curSize == 0) |
| 182 | { |
| 183 | setBufferSize(size); |
| 184 | return true; |
| 185 | } |
| 186 | else if (curSize >= size) |
| 187 | { |
| 188 | return true; |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | UNREACHABLE(); // Static vertex buffers can't be resized |
| 193 | return false; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | int StaticVertexBufferInterface::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances) |
| 198 | { |
| 199 | int attributeOffset = attrib.mOffset % attrib.stride(); |
shannon.woods%transgaming.com@gtempaccount.com | 8de4e6a | 2013-04-13 03:37:44 +0000 | [diff] [blame^] | 200 | VertexElement element = { attrib.mType, attrib.mSize, attrib.stride(), attrib.mNormalized, attrib.mPureInteger, attributeOffset, getWritePosition() }; |
daniel@transgaming.com | 4150d36 | 2012-12-20 21:07:43 +0000 | [diff] [blame] | 201 | mCache.push_back(element); |
| 202 | |
| 203 | return VertexBufferInterface::storeVertexAttributes(attrib, start, count, instances); |
| 204 | } |
| 205 | |
daniel@transgaming.com | 29787c3 | 2012-12-20 20:55:48 +0000 | [diff] [blame] | 206 | } |