Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2015 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 | |
| 7 | // VertexArrayGL.cpp: Implements the class methods for VertexArrayGL. |
| 8 | |
| 9 | #include "libANGLE/renderer/gl/VertexArrayGL.h" |
| 10 | |
| 11 | #include "common/debug.h" |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 12 | #include "common/mathutil.h" |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 13 | #include "common/utilities.h" |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame] | 14 | #include "libANGLE/Buffer.h" |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 15 | #include "libANGLE/angletypes.h" |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 16 | #include "libANGLE/formatutils.h" |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 17 | #include "libANGLE/renderer/gl/BufferGL.h" |
| 18 | #include "libANGLE/renderer/gl/FunctionsGL.h" |
| 19 | #include "libANGLE/renderer/gl/StateManagerGL.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 20 | |
| 21 | namespace rx |
| 22 | { |
| 23 | |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 24 | VertexArrayGL::VertexArrayGL(const FunctionsGL *functions, StateManagerGL *stateManager) |
| 25 | : VertexArrayImpl(), |
| 26 | mFunctions(functions), |
| 27 | mStateManager(stateManager), |
| 28 | mVertexArrayID(0), |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame] | 29 | mElementArrayBuffer(), |
| 30 | mAttributes(), |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 31 | mAppliedElementArrayBuffer(0), |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 32 | mAppliedAttributes(), |
| 33 | mStreamingElementArrayBufferSize(0), |
| 34 | mStreamingElementArrayBuffer(0), |
| 35 | mStreamingArrayBufferSize(0), |
| 36 | mStreamingArrayBuffer(0) |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 37 | { |
| 38 | ASSERT(mFunctions); |
| 39 | ASSERT(mStateManager); |
| 40 | mFunctions->genVertexArrays(1, &mVertexArrayID); |
| 41 | |
| 42 | // Set the cached vertex attribute array size |
| 43 | GLint maxVertexAttribs; |
| 44 | mFunctions->getIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs); |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame] | 45 | mAttributes.resize(maxVertexAttribs); |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 46 | mAppliedAttributes.resize(maxVertexAttribs); |
| 47 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 48 | |
| 49 | VertexArrayGL::~VertexArrayGL() |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 50 | { |
Geoff Lang | 1eb708e | 2015-05-04 14:58:23 -0400 | [diff] [blame] | 51 | mStateManager->deleteVertexArray(mVertexArrayID); |
| 52 | mVertexArrayID = 0; |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 53 | |
Geoff Lang | 1eb708e | 2015-05-04 14:58:23 -0400 | [diff] [blame] | 54 | mStateManager->deleteBuffer(mStreamingElementArrayBuffer); |
| 55 | mStreamingElementArrayBufferSize = 0; |
| 56 | mStreamingElementArrayBuffer = 0; |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 57 | |
Geoff Lang | 1eb708e | 2015-05-04 14:58:23 -0400 | [diff] [blame] | 58 | mStateManager->deleteBuffer(mStreamingArrayBuffer); |
| 59 | mStreamingArrayBufferSize = 0; |
| 60 | mStreamingArrayBuffer = 0; |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 61 | |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame] | 62 | mElementArrayBuffer.set(nullptr); |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 63 | for (size_t idx = 0; idx < mAttributes.size(); idx++) |
| 64 | { |
| 65 | mAttributes[idx].buffer.set(NULL); |
| 66 | } |
| 67 | |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 68 | for (size_t idx = 0; idx < mAppliedAttributes.size(); idx++) |
| 69 | { |
| 70 | mAppliedAttributes[idx].buffer.set(NULL); |
| 71 | } |
| 72 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 73 | |
| 74 | void VertexArrayGL::setElementArrayBuffer(const gl::Buffer *buffer) |
| 75 | { |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame] | 76 | mElementArrayBuffer.set(buffer); |
| 77 | } |
| 78 | |
| 79 | void VertexArrayGL::setAttribute(size_t idx, const gl::VertexAttribute &attr) |
| 80 | { |
| 81 | mAttributes[idx] = attr; |
| 82 | } |
| 83 | |
| 84 | void VertexArrayGL::setAttributeDivisor(size_t idx, GLuint divisor) |
| 85 | { |
| 86 | mAttributes[idx].divisor = divisor; |
| 87 | } |
| 88 | |
| 89 | void VertexArrayGL::enableAttribute(size_t idx, bool enabledState) |
| 90 | { |
| 91 | mAttributes[idx].enabled = enabledState; |
| 92 | } |
| 93 | |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 94 | gl::Error VertexArrayGL::syncDrawArraysState(GLint first, GLsizei count) const |
| 95 | { |
| 96 | return syncDrawState(first, count, GL_NONE, nullptr, nullptr); |
| 97 | } |
| 98 | |
| 99 | gl::Error VertexArrayGL::syncDrawElementsState(GLsizei count, GLenum type, const GLvoid *indices, const GLvoid **outIndices) const |
| 100 | { |
| 101 | return syncDrawState(0, count, type, indices, outIndices); |
| 102 | } |
| 103 | |
| 104 | gl::Error VertexArrayGL::syncDrawState(GLint first, GLsizei count, GLenum type, const GLvoid *indices, const GLvoid **outIndices) const |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame] | 105 | { |
| 106 | mStateManager->bindVertexArray(mVertexArrayID); |
| 107 | |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 108 | // Check if any attributes need to be streamed, determines if the index range needs to be computed |
| 109 | bool attributesNeedStreaming = doAttributesNeedStreaming(); |
| 110 | |
| 111 | // Determine if an index buffer needs to be streamed and the range of vertices that need to be copied |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 112 | gl::RangeUI indexRange(0, 0); |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 113 | if (type != GL_NONE) |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 114 | { |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 115 | gl::Error error = syncIndexData(count, type, indices, attributesNeedStreaming, &indexRange, outIndices); |
| 116 | if (error.isError()) |
| 117 | { |
| 118 | return error; |
| 119 | } |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | // Not an indexed call, set the range to [first, first + count) |
| 124 | indexRange.start = first; |
| 125 | indexRange.end = first + count; |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 126 | } |
| 127 | |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 128 | // Sync the vertex attribute state and track what data needs to be streamed |
| 129 | size_t streamingDataSize = 0; |
| 130 | size_t maxAttributeDataSize = 0; |
| 131 | gl::Error error = syncAttributeState(attributesNeedStreaming, indexRange, &streamingDataSize, &maxAttributeDataSize); |
| 132 | if (error.isError()) |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 133 | { |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 134 | return error; |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 135 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 136 | |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 137 | if (streamingDataSize > 0) |
| 138 | { |
| 139 | ASSERT(attributesNeedStreaming); |
| 140 | |
| 141 | gl::Error error = streamAttributes(streamingDataSize, maxAttributeDataSize, indexRange); |
| 142 | if (error.isError()) |
| 143 | { |
| 144 | return error; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return gl::Error(GL_NO_ERROR); |
| 149 | } |
| 150 | |
| 151 | bool VertexArrayGL::doAttributesNeedStreaming() const |
| 152 | { |
| 153 | // TODO: if GLES, nothing needs to be streamed |
| 154 | for (size_t idx = 0; idx < mAttributes.size(); idx++) |
| 155 | { |
| 156 | if (mAttributes[idx].enabled && mAttributes[idx].buffer.get() == nullptr) |
| 157 | { |
| 158 | return true; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return false; |
| 163 | } |
| 164 | |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 165 | gl::Error VertexArrayGL::syncAttributeState(bool attributesNeedStreaming, const gl::RangeUI &indexRange, |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 166 | size_t *outStreamingDataSize, size_t *outMaxAttributeDataSize) const |
| 167 | { |
| 168 | *outStreamingDataSize = 0; |
| 169 | *outMaxAttributeDataSize = 0; |
| 170 | |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame] | 171 | for (size_t idx = 0; idx < mAttributes.size(); idx++) |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 172 | { |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 173 | // Always sync the enabled and divisor state, they are required for both streaming and buffered |
| 174 | // attributes |
| 175 | if (mAppliedAttributes[idx].enabled != mAttributes[idx].enabled) |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 176 | { |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame] | 177 | if (mAttributes[idx].enabled) |
| 178 | { |
| 179 | mFunctions->enableVertexAttribArray(idx); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | mFunctions->disableVertexAttribArray(idx); |
| 184 | } |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 185 | mAppliedAttributes[idx].enabled = mAttributes[idx].enabled; |
| 186 | } |
| 187 | if (mAppliedAttributes[idx].divisor != mAttributes[idx].divisor) |
| 188 | { |
| 189 | mFunctions->vertexAttribDivisor(idx, mAttributes[idx].divisor); |
| 190 | mAppliedAttributes[idx].divisor = mAttributes[idx].divisor; |
| 191 | } |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame] | 192 | |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 193 | if (mAttributes[idx].enabled && mAttributes[idx].buffer.get() == nullptr) |
| 194 | { |
| 195 | ASSERT(attributesNeedStreaming); |
| 196 | |
| 197 | const size_t streamedVertexCount = indexRange.end - indexRange.start + 1; |
| 198 | |
| 199 | // If streaming is going to be required, compute the size of the required buffer |
| 200 | // and how much slack space at the beginning of the buffer will be required by determining |
| 201 | // the attribute with the largest data size. |
| 202 | size_t typeSize = ComputeVertexAttributeTypeSize(mAttributes[idx]); |
| 203 | *outStreamingDataSize += typeSize * streamedVertexCount; |
| 204 | *outMaxAttributeDataSize = std::max(*outMaxAttributeDataSize, typeSize); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | // Sync the attribute with no translation |
| 209 | if (mAppliedAttributes[idx] != mAttributes[idx]) |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame] | 210 | { |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 211 | const gl::Buffer *arrayBuffer = mAttributes[idx].buffer.get(); |
Geoff Lang | 851cd58 | 2015-05-26 16:47:23 -0400 | [diff] [blame^] | 212 | if (arrayBuffer != nullptr) |
| 213 | { |
| 214 | const BufferGL *arrayBufferGL = GetImplAs<BufferGL>(arrayBuffer); |
| 215 | mStateManager->bindBuffer(GL_ARRAY_BUFFER, arrayBufferGL->getBufferID()); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | mStateManager->bindBuffer(GL_ARRAY_BUFFER, 0); |
| 220 | } |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame] | 221 | |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 222 | if (mAttributes[idx].pureInteger) |
| 223 | { |
| 224 | mFunctions->vertexAttribIPointer(idx, mAttributes[idx].size, mAttributes[idx].type, |
| 225 | mAttributes[idx].stride, mAttributes[idx].pointer); |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | mFunctions->vertexAttribPointer(idx, mAttributes[idx].size, mAttributes[idx].type, |
| 230 | mAttributes[idx].normalized, mAttributes[idx].stride, |
| 231 | mAttributes[idx].pointer); |
| 232 | } |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame] | 233 | |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 234 | mAppliedAttributes[idx] = mAttributes[idx]; |
| 235 | } |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 236 | } |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 237 | } |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 238 | |
| 239 | return gl::Error(GL_NO_ERROR); |
| 240 | } |
| 241 | |
| 242 | gl::Error VertexArrayGL::syncIndexData(GLsizei count, GLenum type, const GLvoid *indices, bool attributesNeedStreaming, |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 243 | gl::RangeUI *outIndexRange, const GLvoid **outIndices) const |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 244 | { |
| 245 | ASSERT(outIndices); |
| 246 | |
| 247 | // Need to check the range of indices if attributes need to be streamed |
| 248 | if (mElementArrayBuffer.get() != nullptr) |
| 249 | { |
| 250 | const BufferGL *bufferGL = GetImplAs<BufferGL>(mElementArrayBuffer.get()); |
| 251 | GLuint elementArrayBufferID = bufferGL->getBufferID(); |
| 252 | if (elementArrayBufferID != mAppliedElementArrayBuffer) |
| 253 | { |
| 254 | mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArrayBufferID); |
| 255 | mAppliedElementArrayBuffer = elementArrayBufferID; |
| 256 | } |
| 257 | |
| 258 | // Only compute the index range if the attributes also need to be streamed |
| 259 | if (attributesNeedStreaming) |
| 260 | { |
| 261 | ptrdiff_t elementArrayBufferOffset = reinterpret_cast<ptrdiff_t>(indices); |
Geoff Lang | 520c4ae | 2015-05-05 13:12:36 -0400 | [diff] [blame] | 262 | gl::Error error = mElementArrayBuffer->getIndexRange(type, static_cast<size_t>(elementArrayBufferOffset), count, outIndexRange); |
| 263 | if (error.isError()) |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 264 | { |
Geoff Lang | 520c4ae | 2015-05-05 13:12:36 -0400 | [diff] [blame] | 265 | return error; |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
| 269 | // Indices serves as an offset into the index buffer in this case, use the same value for the draw call |
| 270 | *outIndices = indices; |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | // Need to stream the index buffer |
| 275 | // TODO: if GLES, nothing needs to be streamed |
| 276 | |
| 277 | // Only compute the index range if the attributes also need to be streamed |
| 278 | if (attributesNeedStreaming) |
| 279 | { |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 280 | *outIndexRange = gl::ComputeIndexRange(type, indices, count); |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // Allocate the streaming element array buffer |
| 284 | if (mStreamingElementArrayBuffer == 0) |
| 285 | { |
| 286 | mFunctions->genBuffers(1, &mStreamingElementArrayBuffer); |
| 287 | mStreamingElementArrayBufferSize = 0; |
| 288 | } |
| 289 | |
| 290 | mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, mStreamingElementArrayBuffer); |
| 291 | mAppliedElementArrayBuffer = mStreamingElementArrayBuffer; |
| 292 | |
| 293 | // Make sure the element array buffer is large enough |
| 294 | const gl::Type &indexTypeInfo = gl::GetTypeInfo(type); |
| 295 | size_t requiredStreamingBufferSize = indexTypeInfo.bytes * count; |
| 296 | if (requiredStreamingBufferSize > mStreamingElementArrayBufferSize) |
| 297 | { |
| 298 | // Copy the indices in while resizing the buffer |
| 299 | mFunctions->bufferData(GL_ELEMENT_ARRAY_BUFFER, requiredStreamingBufferSize, indices, GL_DYNAMIC_DRAW); |
| 300 | mStreamingElementArrayBufferSize = requiredStreamingBufferSize; |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | // Put the indices at the beginning of the buffer |
| 305 | mFunctions->bufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, requiredStreamingBufferSize, indices); |
| 306 | } |
| 307 | |
| 308 | // Set the index offset for the draw call to zero since the supplied index pointer is to client data |
| 309 | *outIndices = nullptr; |
| 310 | } |
| 311 | |
| 312 | return gl::Error(GL_NO_ERROR); |
| 313 | } |
| 314 | |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 315 | gl::Error VertexArrayGL::streamAttributes(size_t streamingDataSize, size_t maxAttributeDataSize, const gl::RangeUI &indexRange) const |
Geoff Lang | 7c82bc4 | 2015-03-09 16:18:08 -0400 | [diff] [blame] | 316 | { |
| 317 | if (mStreamingArrayBuffer == 0) |
| 318 | { |
| 319 | mFunctions->genBuffers(1, &mStreamingArrayBuffer); |
| 320 | mStreamingArrayBufferSize = 0; |
| 321 | } |
| 322 | |
| 323 | // If first is greater than zero, a slack space needs to be left at the beginning of the buffer so that |
| 324 | // the same 'first' argument can be passed into the draw call. |
| 325 | const size_t bufferEmptySpace = maxAttributeDataSize * indexRange.start; |
| 326 | const size_t requiredBufferSize = streamingDataSize + bufferEmptySpace; |
| 327 | |
| 328 | mStateManager->bindBuffer(GL_ARRAY_BUFFER, mStreamingArrayBuffer); |
| 329 | if (requiredBufferSize > mStreamingArrayBufferSize) |
| 330 | { |
| 331 | mFunctions->bufferData(GL_ARRAY_BUFFER, requiredBufferSize, nullptr, GL_DYNAMIC_DRAW); |
| 332 | mStreamingArrayBufferSize = requiredBufferSize; |
| 333 | } |
| 334 | |
| 335 | // Unmapping a buffer can return GL_FALSE to indicate that the system has corrupted the data |
| 336 | // somehow (such as by a screen change), retry writing the data a few times and return OUT_OF_MEMORY |
| 337 | // if that fails. |
| 338 | GLboolean unmapResult = GL_FALSE; |
| 339 | size_t unmapRetryAttempts = 5; |
| 340 | while (unmapResult != GL_TRUE && --unmapRetryAttempts > 0) |
| 341 | { |
| 342 | uint8_t *bufferPointer = reinterpret_cast<uint8_t*>(mFunctions->mapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY)); |
| 343 | size_t curBufferOffset = bufferEmptySpace; |
| 344 | |
| 345 | const size_t streamedVertexCount = indexRange.end - indexRange.start + 1; |
| 346 | |
| 347 | for (size_t idx = 0; idx < mAttributes.size(); idx++) |
| 348 | { |
| 349 | if (mAttributes[idx].enabled && mAttributes[idx].buffer.get() == nullptr) |
| 350 | { |
| 351 | const size_t sourceStride = ComputeVertexAttributeStride(mAttributes[idx]); |
| 352 | const size_t destStride = ComputeVertexAttributeTypeSize(mAttributes[idx]); |
| 353 | |
| 354 | const uint8_t *inputPointer = reinterpret_cast<const uint8_t*>(mAttributes[idx].pointer); |
| 355 | |
| 356 | // Pack the data when copying it, user could have supplied a very large stride that would |
| 357 | // cause the buffer to be much larger than needed. |
| 358 | if (destStride == sourceStride) |
| 359 | { |
| 360 | // Can copy in one go, the data is packed |
| 361 | memcpy(bufferPointer + curBufferOffset, |
| 362 | inputPointer + (sourceStride * indexRange.start), |
| 363 | destStride * streamedVertexCount); |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | // Copy each vertex individually |
| 368 | for (size_t vertexIdx = indexRange.start; vertexIdx <= indexRange.end; vertexIdx++) |
| 369 | { |
| 370 | memcpy(bufferPointer + curBufferOffset + (destStride * vertexIdx), |
| 371 | inputPointer + (sourceStride * vertexIdx), |
| 372 | destStride); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // Compute where the 0-index vertex would be. |
| 377 | const size_t vertexStartOffset = curBufferOffset - (indexRange.start * destStride); |
| 378 | |
| 379 | mFunctions->vertexAttribPointer(idx, mAttributes[idx].size, mAttributes[idx].type, |
| 380 | mAttributes[idx].normalized, destStride, |
| 381 | reinterpret_cast<const GLvoid*>(vertexStartOffset)); |
| 382 | |
| 383 | curBufferOffset += destStride * streamedVertexCount; |
| 384 | |
| 385 | // Mark the applied attribute as dirty by setting an invalid size so that if it doesn't |
| 386 | // need to be streamed later, there is no chance that the caching will skip it. |
| 387 | mAppliedAttributes[idx].size = static_cast<GLuint>(-1); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | unmapResult = mFunctions->unmapBuffer(GL_ARRAY_BUFFER); |
| 392 | } |
| 393 | |
| 394 | if (unmapResult != GL_TRUE) |
| 395 | { |
| 396 | return gl::Error(GL_OUT_OF_MEMORY, "Failed to unmap the client data streaming buffer."); |
| 397 | } |
| 398 | |
| 399 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | GLuint VertexArrayGL::getVertexArrayID() const |
| 403 | { |
| 404 | return mVertexArrayID; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | } |