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