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