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