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