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