1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/angle/src/libGLESv2/renderer/VertexBuffer.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,292 @@ 1.4 +#include "precompiled.h" 1.5 +// 1.6 +// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. 1.7 +// Use of this source code is governed by a BSD-style license that can be 1.8 +// found in the LICENSE file. 1.9 +// 1.10 + 1.11 +// VertexBuffer.cpp: Defines the abstract VertexBuffer class and VertexBufferInterface 1.12 +// class with derivations, classes that perform graphics API agnostic vertex buffer operations. 1.13 + 1.14 +#include "libGLESv2/renderer/VertexBuffer.h" 1.15 +#include "libGLESv2/renderer/Renderer.h" 1.16 +#include "libGLESv2/Context.h" 1.17 + 1.18 +namespace rx 1.19 +{ 1.20 + 1.21 +unsigned int VertexBuffer::mNextSerial = 1; 1.22 + 1.23 +VertexBuffer::VertexBuffer() 1.24 +{ 1.25 + updateSerial(); 1.26 +} 1.27 + 1.28 +VertexBuffer::~VertexBuffer() 1.29 +{ 1.30 +} 1.31 + 1.32 +void VertexBuffer::updateSerial() 1.33 +{ 1.34 + mSerial = mNextSerial++; 1.35 +} 1.36 + 1.37 +unsigned int VertexBuffer::getSerial() const 1.38 +{ 1.39 + return mSerial; 1.40 +} 1.41 + 1.42 +VertexBufferInterface::VertexBufferInterface(rx::Renderer *renderer, bool dynamic) : mRenderer(renderer) 1.43 +{ 1.44 + mDynamic = dynamic; 1.45 + mWritePosition = 0; 1.46 + mReservedSpace = 0; 1.47 + 1.48 + mVertexBuffer = renderer->createVertexBuffer(); 1.49 +} 1.50 + 1.51 +VertexBufferInterface::~VertexBufferInterface() 1.52 +{ 1.53 + delete mVertexBuffer; 1.54 +} 1.55 + 1.56 +unsigned int VertexBufferInterface::getSerial() const 1.57 +{ 1.58 + return mVertexBuffer->getSerial(); 1.59 +} 1.60 + 1.61 +unsigned int VertexBufferInterface::getBufferSize() const 1.62 +{ 1.63 + return mVertexBuffer->getBufferSize(); 1.64 +} 1.65 + 1.66 +bool VertexBufferInterface::setBufferSize(unsigned int size) 1.67 +{ 1.68 + if (mVertexBuffer->getBufferSize() == 0) 1.69 + { 1.70 + return mVertexBuffer->initialize(size, mDynamic); 1.71 + } 1.72 + else 1.73 + { 1.74 + return mVertexBuffer->setBufferSize(size); 1.75 + } 1.76 +} 1.77 + 1.78 +unsigned int VertexBufferInterface::getWritePosition() const 1.79 +{ 1.80 + return mWritePosition; 1.81 +} 1.82 + 1.83 +void VertexBufferInterface::setWritePosition(unsigned int writePosition) 1.84 +{ 1.85 + mWritePosition = writePosition; 1.86 +} 1.87 + 1.88 +bool VertexBufferInterface::discard() 1.89 +{ 1.90 + return mVertexBuffer->discard(); 1.91 +} 1.92 + 1.93 +bool VertexBufferInterface::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances, 1.94 + unsigned int *outStreamOffset) 1.95 +{ 1.96 + unsigned int spaceRequired; 1.97 + if (!mVertexBuffer->getSpaceRequired(attrib, count, instances, &spaceRequired)) 1.98 + { 1.99 + return false; 1.100 + } 1.101 + 1.102 + if (mWritePosition + spaceRequired < mWritePosition) 1.103 + { 1.104 + return false; 1.105 + } 1.106 + 1.107 + if (!reserveSpace(mReservedSpace)) 1.108 + { 1.109 + return false; 1.110 + } 1.111 + mReservedSpace = 0; 1.112 + 1.113 + if (!mVertexBuffer->storeVertexAttributes(attrib, start, count, instances, mWritePosition)) 1.114 + { 1.115 + return false; 1.116 + } 1.117 + 1.118 + if (outStreamOffset) 1.119 + { 1.120 + *outStreamOffset = mWritePosition; 1.121 + } 1.122 + 1.123 + mWritePosition += spaceRequired; 1.124 + 1.125 + return true; 1.126 +} 1.127 + 1.128 +bool VertexBufferInterface::storeRawData(const void* data, unsigned int size, unsigned int *outStreamOffset) 1.129 +{ 1.130 + if (mWritePosition + size < mWritePosition) 1.131 + { 1.132 + return false; 1.133 + } 1.134 + 1.135 + if (!reserveSpace(mReservedSpace)) 1.136 + { 1.137 + return false; 1.138 + } 1.139 + mReservedSpace = 0; 1.140 + 1.141 + if (!mVertexBuffer->storeRawData(data, size, mWritePosition)) 1.142 + { 1.143 + return false; 1.144 + } 1.145 + 1.146 + if (outStreamOffset) 1.147 + { 1.148 + *outStreamOffset = mWritePosition; 1.149 + } 1.150 + 1.151 + mWritePosition += size; 1.152 + 1.153 + return true; 1.154 +} 1.155 + 1.156 +bool VertexBufferInterface::reserveVertexSpace(const gl::VertexAttribute &attribute, GLsizei count, GLsizei instances) 1.157 +{ 1.158 + unsigned int requiredSpace; 1.159 + if (!mVertexBuffer->getSpaceRequired(attribute, count, instances, &requiredSpace)) 1.160 + { 1.161 + return false; 1.162 + } 1.163 + 1.164 + // Protect against integer overflow 1.165 + if (mReservedSpace + requiredSpace < mReservedSpace) 1.166 + { 1.167 + return false; 1.168 + } 1.169 + 1.170 + mReservedSpace += requiredSpace; 1.171 + return true; 1.172 +} 1.173 + 1.174 +bool VertexBufferInterface::reserveRawDataSpace(unsigned int size) 1.175 +{ 1.176 + // Protect against integer overflow 1.177 + if (mReservedSpace + size < mReservedSpace) 1.178 + { 1.179 + return false; 1.180 + } 1.181 + 1.182 + mReservedSpace += size; 1.183 + return true; 1.184 +} 1.185 + 1.186 +VertexBuffer* VertexBufferInterface::getVertexBuffer() const 1.187 +{ 1.188 + return mVertexBuffer; 1.189 +} 1.190 + 1.191 + 1.192 +StreamingVertexBufferInterface::StreamingVertexBufferInterface(rx::Renderer *renderer, std::size_t initialSize) : VertexBufferInterface(renderer, true) 1.193 +{ 1.194 + setBufferSize(initialSize); 1.195 +} 1.196 + 1.197 +StreamingVertexBufferInterface::~StreamingVertexBufferInterface() 1.198 +{ 1.199 +} 1.200 + 1.201 +bool StreamingVertexBufferInterface::reserveSpace(unsigned int size) 1.202 +{ 1.203 + bool result = true; 1.204 + unsigned int curBufferSize = getBufferSize(); 1.205 + if (size > curBufferSize) 1.206 + { 1.207 + result = setBufferSize(std::max(size, 3 * curBufferSize / 2)); 1.208 + setWritePosition(0); 1.209 + } 1.210 + else if (getWritePosition() + size > curBufferSize) 1.211 + { 1.212 + if (!discard()) 1.213 + { 1.214 + return false; 1.215 + } 1.216 + setWritePosition(0); 1.217 + } 1.218 + 1.219 + return result; 1.220 +} 1.221 + 1.222 +StaticVertexBufferInterface::StaticVertexBufferInterface(rx::Renderer *renderer) : VertexBufferInterface(renderer, false) 1.223 +{ 1.224 +} 1.225 + 1.226 +StaticVertexBufferInterface::~StaticVertexBufferInterface() 1.227 +{ 1.228 +} 1.229 + 1.230 +bool StaticVertexBufferInterface::lookupAttribute(const gl::VertexAttribute &attribute, unsigned int *outStreamOffset) 1.231 +{ 1.232 + for (unsigned int element = 0; element < mCache.size(); element++) 1.233 + { 1.234 + if (mCache[element].type == attribute.mType && 1.235 + mCache[element].size == attribute.mSize && 1.236 + mCache[element].stride == attribute.stride() && 1.237 + mCache[element].normalized == attribute.mNormalized) 1.238 + { 1.239 + if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride()) 1.240 + { 1.241 + if (outStreamOffset) 1.242 + { 1.243 + *outStreamOffset = mCache[element].streamOffset; 1.244 + } 1.245 + return true; 1.246 + } 1.247 + } 1.248 + } 1.249 + 1.250 + return false; 1.251 +} 1.252 + 1.253 +bool StaticVertexBufferInterface::reserveSpace(unsigned int size) 1.254 +{ 1.255 + unsigned int curSize = getBufferSize(); 1.256 + if (curSize == 0) 1.257 + { 1.258 + setBufferSize(size); 1.259 + return true; 1.260 + } 1.261 + else if (curSize >= size) 1.262 + { 1.263 + return true; 1.264 + } 1.265 + else 1.266 + { 1.267 + UNREACHABLE(); // Static vertex buffers can't be resized 1.268 + return false; 1.269 + } 1.270 +} 1.271 + 1.272 +bool StaticVertexBufferInterface::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances, 1.273 + unsigned int *outStreamOffset) 1.274 +{ 1.275 + unsigned int streamOffset; 1.276 + if (VertexBufferInterface::storeVertexAttributes(attrib, start, count, instances, &streamOffset)) 1.277 + { 1.278 + int attributeOffset = attrib.mOffset % attrib.stride(); 1.279 + VertexElement element = { attrib.mType, attrib.mSize, attrib.stride(), attrib.mNormalized, attributeOffset, streamOffset }; 1.280 + mCache.push_back(element); 1.281 + 1.282 + if (outStreamOffset) 1.283 + { 1.284 + *outStreamOffset = streamOffset; 1.285 + } 1.286 + 1.287 + return true; 1.288 + } 1.289 + else 1.290 + { 1.291 + return false; 1.292 + } 1.293 +} 1.294 + 1.295 +}