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