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