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