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