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