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