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