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