gfx/angle/src/libGLESv2/renderer/VertexBuffer.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #include "precompiled.h"
michael@0 2 //
michael@0 3 // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
michael@0 4 // Use of this source code is governed by a BSD-style license that can be
michael@0 5 // found in the LICENSE file.
michael@0 6 //
michael@0 7
michael@0 8 // VertexBuffer.cpp: Defines the abstract VertexBuffer class and VertexBufferInterface
michael@0 9 // class with derivations, classes that perform graphics API agnostic vertex buffer operations.
michael@0 10
michael@0 11 #include "libGLESv2/renderer/VertexBuffer.h"
michael@0 12 #include "libGLESv2/renderer/Renderer.h"
michael@0 13 #include "libGLESv2/Context.h"
michael@0 14
michael@0 15 namespace rx
michael@0 16 {
michael@0 17
michael@0 18 unsigned int VertexBuffer::mNextSerial = 1;
michael@0 19
michael@0 20 VertexBuffer::VertexBuffer()
michael@0 21 {
michael@0 22 updateSerial();
michael@0 23 }
michael@0 24
michael@0 25 VertexBuffer::~VertexBuffer()
michael@0 26 {
michael@0 27 }
michael@0 28
michael@0 29 void VertexBuffer::updateSerial()
michael@0 30 {
michael@0 31 mSerial = mNextSerial++;
michael@0 32 }
michael@0 33
michael@0 34 unsigned int VertexBuffer::getSerial() const
michael@0 35 {
michael@0 36 return mSerial;
michael@0 37 }
michael@0 38
michael@0 39 VertexBufferInterface::VertexBufferInterface(rx::Renderer *renderer, bool dynamic) : mRenderer(renderer)
michael@0 40 {
michael@0 41 mDynamic = dynamic;
michael@0 42 mWritePosition = 0;
michael@0 43 mReservedSpace = 0;
michael@0 44
michael@0 45 mVertexBuffer = renderer->createVertexBuffer();
michael@0 46 }
michael@0 47
michael@0 48 VertexBufferInterface::~VertexBufferInterface()
michael@0 49 {
michael@0 50 delete mVertexBuffer;
michael@0 51 }
michael@0 52
michael@0 53 unsigned int VertexBufferInterface::getSerial() const
michael@0 54 {
michael@0 55 return mVertexBuffer->getSerial();
michael@0 56 }
michael@0 57
michael@0 58 unsigned int VertexBufferInterface::getBufferSize() const
michael@0 59 {
michael@0 60 return mVertexBuffer->getBufferSize();
michael@0 61 }
michael@0 62
michael@0 63 bool VertexBufferInterface::setBufferSize(unsigned int size)
michael@0 64 {
michael@0 65 if (mVertexBuffer->getBufferSize() == 0)
michael@0 66 {
michael@0 67 return mVertexBuffer->initialize(size, mDynamic);
michael@0 68 }
michael@0 69 else
michael@0 70 {
michael@0 71 return mVertexBuffer->setBufferSize(size);
michael@0 72 }
michael@0 73 }
michael@0 74
michael@0 75 unsigned int VertexBufferInterface::getWritePosition() const
michael@0 76 {
michael@0 77 return mWritePosition;
michael@0 78 }
michael@0 79
michael@0 80 void VertexBufferInterface::setWritePosition(unsigned int writePosition)
michael@0 81 {
michael@0 82 mWritePosition = writePosition;
michael@0 83 }
michael@0 84
michael@0 85 bool VertexBufferInterface::discard()
michael@0 86 {
michael@0 87 return mVertexBuffer->discard();
michael@0 88 }
michael@0 89
michael@0 90 bool VertexBufferInterface::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances,
michael@0 91 unsigned int *outStreamOffset)
michael@0 92 {
michael@0 93 unsigned int spaceRequired;
michael@0 94 if (!mVertexBuffer->getSpaceRequired(attrib, count, instances, &spaceRequired))
michael@0 95 {
michael@0 96 return false;
michael@0 97 }
michael@0 98
michael@0 99 if (mWritePosition + spaceRequired < mWritePosition)
michael@0 100 {
michael@0 101 return false;
michael@0 102 }
michael@0 103
michael@0 104 if (!reserveSpace(mReservedSpace))
michael@0 105 {
michael@0 106 return false;
michael@0 107 }
michael@0 108 mReservedSpace = 0;
michael@0 109
michael@0 110 if (!mVertexBuffer->storeVertexAttributes(attrib, start, count, instances, mWritePosition))
michael@0 111 {
michael@0 112 return false;
michael@0 113 }
michael@0 114
michael@0 115 if (outStreamOffset)
michael@0 116 {
michael@0 117 *outStreamOffset = mWritePosition;
michael@0 118 }
michael@0 119
michael@0 120 mWritePosition += spaceRequired;
michael@0 121
michael@0 122 return true;
michael@0 123 }
michael@0 124
michael@0 125 bool VertexBufferInterface::storeRawData(const void* data, unsigned int size, unsigned int *outStreamOffset)
michael@0 126 {
michael@0 127 if (mWritePosition + size < mWritePosition)
michael@0 128 {
michael@0 129 return false;
michael@0 130 }
michael@0 131
michael@0 132 if (!reserveSpace(mReservedSpace))
michael@0 133 {
michael@0 134 return false;
michael@0 135 }
michael@0 136 mReservedSpace = 0;
michael@0 137
michael@0 138 if (!mVertexBuffer->storeRawData(data, size, mWritePosition))
michael@0 139 {
michael@0 140 return false;
michael@0 141 }
michael@0 142
michael@0 143 if (outStreamOffset)
michael@0 144 {
michael@0 145 *outStreamOffset = mWritePosition;
michael@0 146 }
michael@0 147
michael@0 148 mWritePosition += size;
michael@0 149
michael@0 150 return true;
michael@0 151 }
michael@0 152
michael@0 153 bool VertexBufferInterface::reserveVertexSpace(const gl::VertexAttribute &attribute, GLsizei count, GLsizei instances)
michael@0 154 {
michael@0 155 unsigned int requiredSpace;
michael@0 156 if (!mVertexBuffer->getSpaceRequired(attribute, count, instances, &requiredSpace))
michael@0 157 {
michael@0 158 return false;
michael@0 159 }
michael@0 160
michael@0 161 // Protect against integer overflow
michael@0 162 if (mReservedSpace + requiredSpace < mReservedSpace)
michael@0 163 {
michael@0 164 return false;
michael@0 165 }
michael@0 166
michael@0 167 mReservedSpace += requiredSpace;
michael@0 168 return true;
michael@0 169 }
michael@0 170
michael@0 171 bool VertexBufferInterface::reserveRawDataSpace(unsigned int size)
michael@0 172 {
michael@0 173 // Protect against integer overflow
michael@0 174 if (mReservedSpace + size < mReservedSpace)
michael@0 175 {
michael@0 176 return false;
michael@0 177 }
michael@0 178
michael@0 179 mReservedSpace += size;
michael@0 180 return true;
michael@0 181 }
michael@0 182
michael@0 183 VertexBuffer* VertexBufferInterface::getVertexBuffer() const
michael@0 184 {
michael@0 185 return mVertexBuffer;
michael@0 186 }
michael@0 187
michael@0 188
michael@0 189 StreamingVertexBufferInterface::StreamingVertexBufferInterface(rx::Renderer *renderer, std::size_t initialSize) : VertexBufferInterface(renderer, true)
michael@0 190 {
michael@0 191 setBufferSize(initialSize);
michael@0 192 }
michael@0 193
michael@0 194 StreamingVertexBufferInterface::~StreamingVertexBufferInterface()
michael@0 195 {
michael@0 196 }
michael@0 197
michael@0 198 bool StreamingVertexBufferInterface::reserveSpace(unsigned int size)
michael@0 199 {
michael@0 200 bool result = true;
michael@0 201 unsigned int curBufferSize = getBufferSize();
michael@0 202 if (size > curBufferSize)
michael@0 203 {
michael@0 204 result = setBufferSize(std::max(size, 3 * curBufferSize / 2));
michael@0 205 setWritePosition(0);
michael@0 206 }
michael@0 207 else if (getWritePosition() + size > curBufferSize)
michael@0 208 {
michael@0 209 if (!discard())
michael@0 210 {
michael@0 211 return false;
michael@0 212 }
michael@0 213 setWritePosition(0);
michael@0 214 }
michael@0 215
michael@0 216 return result;
michael@0 217 }
michael@0 218
michael@0 219 StaticVertexBufferInterface::StaticVertexBufferInterface(rx::Renderer *renderer) : VertexBufferInterface(renderer, false)
michael@0 220 {
michael@0 221 }
michael@0 222
michael@0 223 StaticVertexBufferInterface::~StaticVertexBufferInterface()
michael@0 224 {
michael@0 225 }
michael@0 226
michael@0 227 bool StaticVertexBufferInterface::lookupAttribute(const gl::VertexAttribute &attribute, unsigned int *outStreamOffset)
michael@0 228 {
michael@0 229 for (unsigned int element = 0; element < mCache.size(); element++)
michael@0 230 {
michael@0 231 if (mCache[element].type == attribute.mType &&
michael@0 232 mCache[element].size == attribute.mSize &&
michael@0 233 mCache[element].stride == attribute.stride() &&
michael@0 234 mCache[element].normalized == attribute.mNormalized)
michael@0 235 {
michael@0 236 if (mCache[element].attributeOffset == attribute.mOffset % attribute.stride())
michael@0 237 {
michael@0 238 if (outStreamOffset)
michael@0 239 {
michael@0 240 *outStreamOffset = mCache[element].streamOffset;
michael@0 241 }
michael@0 242 return true;
michael@0 243 }
michael@0 244 }
michael@0 245 }
michael@0 246
michael@0 247 return false;
michael@0 248 }
michael@0 249
michael@0 250 bool StaticVertexBufferInterface::reserveSpace(unsigned int size)
michael@0 251 {
michael@0 252 unsigned int curSize = getBufferSize();
michael@0 253 if (curSize == 0)
michael@0 254 {
michael@0 255 setBufferSize(size);
michael@0 256 return true;
michael@0 257 }
michael@0 258 else if (curSize >= size)
michael@0 259 {
michael@0 260 return true;
michael@0 261 }
michael@0 262 else
michael@0 263 {
michael@0 264 UNREACHABLE(); // Static vertex buffers can't be resized
michael@0 265 return false;
michael@0 266 }
michael@0 267 }
michael@0 268
michael@0 269 bool StaticVertexBufferInterface::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances,
michael@0 270 unsigned int *outStreamOffset)
michael@0 271 {
michael@0 272 unsigned int streamOffset;
michael@0 273 if (VertexBufferInterface::storeVertexAttributes(attrib, start, count, instances, &streamOffset))
michael@0 274 {
michael@0 275 int attributeOffset = attrib.mOffset % attrib.stride();
michael@0 276 VertexElement element = { attrib.mType, attrib.mSize, attrib.stride(), attrib.mNormalized, attributeOffset, streamOffset };
michael@0 277 mCache.push_back(element);
michael@0 278
michael@0 279 if (outStreamOffset)
michael@0 280 {
michael@0 281 *outStreamOffset = streamOffset;
michael@0 282 }
michael@0 283
michael@0 284 return true;
michael@0 285 }
michael@0 286 else
michael@0 287 {
michael@0 288 return false;
michael@0 289 }
michael@0 290 }
michael@0 291
michael@0 292 }

mercurial