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