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