Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2013 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 | // Implementation of the state class for mananging GLES 3 Vertex Array Objects. |
| 7 | // |
| 8 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 9 | #include "libANGLE/VertexArray.h" |
| 10 | #include "libANGLE/Buffer.h" |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 11 | #include "libANGLE/Context.h" |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 12 | #include "libANGLE/renderer/BufferImpl.h" |
Jamie Madill | 7aea7e0 | 2016-05-10 10:39:45 -0400 | [diff] [blame] | 13 | #include "libANGLE/renderer/GLImplFactory.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 14 | #include "libANGLE/renderer/VertexArrayImpl.h" |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 15 | |
| 16 | namespace gl |
| 17 | { |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 18 | namespace |
| 19 | { |
| 20 | bool IsElementArrayBufferSubjectIndex(angle::SubjectIndex subjectIndex) |
| 21 | { |
| 22 | return (subjectIndex == MAX_VERTEX_ATTRIBS); |
| 23 | } |
Jamie Madill | cd0a0a3 | 2018-10-18 18:41:57 -0400 | [diff] [blame^] | 24 | |
| 25 | constexpr angle::SubjectIndex kElementArrayBufferIndex = MAX_VERTEX_ATTRIBS; |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 26 | } // anonymous namespce |
| 27 | |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 28 | // VertexArrayState implementation. |
Jamie Madill | cd0a0a3 | 2018-10-18 18:41:57 -0400 | [diff] [blame^] | 29 | VertexArrayState::VertexArrayState(VertexArray *vertexArray, |
| 30 | size_t maxAttribs, |
| 31 | size_t maxAttribBindings) |
| 32 | : mElementArrayBuffer(vertexArray, kElementArrayBufferIndex) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 33 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 34 | ASSERT(maxAttribs <= maxAttribBindings); |
| 35 | |
| 36 | for (size_t i = 0; i < maxAttribs; i++) |
| 37 | { |
| 38 | mVertexAttributes.emplace_back(static_cast<GLuint>(i)); |
Jamie Madill | ad63728 | 2018-07-31 11:22:14 -0400 | [diff] [blame] | 39 | mVertexBindings.emplace_back(static_cast<GLuint>(i)); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 40 | } |
Jamie Madill | dc358af | 2018-07-31 11:22:13 -0400 | [diff] [blame] | 41 | |
| 42 | // Initially all attributes start as "client" with no buffer bound. |
| 43 | mClientMemoryAttribsMask.set(); |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame] | 46 | VertexArrayState::~VertexArrayState() |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 47 | { |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Jamie Madill | 51af38b | 2018-04-15 08:50:56 -0400 | [diff] [blame] | 50 | bool VertexArrayState::hasEnabledNullPointerClientArray() const |
| 51 | { |
| 52 | return (mNullPointerClientMemoryAttribsMask & mEnabledAttributesMask).any(); |
| 53 | } |
| 54 | |
Jamie Madill | ad63728 | 2018-07-31 11:22:14 -0400 | [diff] [blame] | 55 | AttributesMask VertexArrayState::getBindingToAttributesMask(GLuint bindingIndex) const |
Jiawei Shao | 6a5d98c | 2018-05-04 15:42:20 +0800 | [diff] [blame] | 56 | { |
| 57 | ASSERT(bindingIndex < MAX_VERTEX_ATTRIB_BINDINGS); |
Jamie Madill | ad63728 | 2018-07-31 11:22:14 -0400 | [diff] [blame] | 58 | return mVertexBindings[bindingIndex].getBoundAttributesMask(); |
Jiawei Shao | 6a5d98c | 2018-05-04 15:42:20 +0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Set an attribute using a new binding. |
| 62 | void VertexArrayState::setAttribBinding(size_t attribIndex, GLuint newBindingIndex) |
| 63 | { |
| 64 | ASSERT(attribIndex < MAX_VERTEX_ATTRIBS && newBindingIndex < MAX_VERTEX_ATTRIB_BINDINGS); |
| 65 | |
Jamie Madill | bdc610a | 2018-07-30 10:26:00 -0400 | [diff] [blame] | 66 | VertexAttribute &attrib = mVertexAttributes[attribIndex]; |
| 67 | |
Jiawei Shao | 6a5d98c | 2018-05-04 15:42:20 +0800 | [diff] [blame] | 68 | // Update the binding-attribute map. |
Jamie Madill | bdc610a | 2018-07-30 10:26:00 -0400 | [diff] [blame] | 69 | const GLuint oldBindingIndex = attrib.bindingIndex; |
Jiawei Shao | 6a5d98c | 2018-05-04 15:42:20 +0800 | [diff] [blame] | 70 | ASSERT(oldBindingIndex != newBindingIndex); |
| 71 | |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 72 | VertexBinding &oldBinding = mVertexBindings[oldBindingIndex]; |
| 73 | VertexBinding &newBinding = mVertexBindings[newBindingIndex]; |
Jiawei Shao | 6a5d98c | 2018-05-04 15:42:20 +0800 | [diff] [blame] | 74 | |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 75 | ASSERT(oldBinding.getBoundAttributesMask().test(attribIndex) && |
| 76 | !newBinding.getBoundAttributesMask().test(attribIndex)); |
| 77 | |
| 78 | oldBinding.resetBoundAttribute(attribIndex); |
| 79 | newBinding.setBoundAttribute(attribIndex); |
Jiawei Shao | 6a5d98c | 2018-05-04 15:42:20 +0800 | [diff] [blame] | 80 | |
| 81 | // Set the attribute using the new binding. |
Jamie Madill | bdc610a | 2018-07-30 10:26:00 -0400 | [diff] [blame] | 82 | attrib.bindingIndex = newBindingIndex; |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 83 | attrib.updateCachedElementLimit(newBinding); |
| 84 | |
| 85 | bool isMapped = newBinding.getBuffer().get() && newBinding.getBuffer()->isMapped(); |
| 86 | mCachedMappedArrayBuffers.set(attribIndex, isMapped); |
| 87 | mCachedEnabledMappedArrayBuffers.set(attribIndex, isMapped && attrib.enabled); |
Jiawei Shao | 6a5d98c | 2018-05-04 15:42:20 +0800 | [diff] [blame] | 88 | } |
| 89 | |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 90 | // VertexArray implementation. |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 91 | VertexArray::VertexArray(rx::GLImplFactory *factory, |
| 92 | GLuint id, |
| 93 | size_t maxAttribs, |
| 94 | size_t maxAttribBindings) |
| 95 | : mId(id), |
Jamie Madill | cd0a0a3 | 2018-10-18 18:41:57 -0400 | [diff] [blame^] | 96 | mState(this, maxAttribs, maxAttribBindings), |
| 97 | mVertexArray(factory->createVertexArray(mState)) |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 98 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 99 | for (size_t attribIndex = 0; attribIndex < maxAttribBindings; ++attribIndex) |
| 100 | { |
| 101 | mArrayBufferObserverBindings.emplace_back(this, attribIndex); |
| 102 | } |
Jamie Madill | 004a6f9 | 2013-07-10 15:13:38 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 105 | void VertexArray::onDestroy(const Context *context) |
| 106 | { |
James Darpinian | e8a93c6 | 2018-01-04 18:02:24 -0800 | [diff] [blame] | 107 | bool isBound = context->isCurrentVertexArray(this); |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 108 | for (VertexBinding &binding : mState.mVertexBindings) |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 109 | { |
James Darpinian | e8a93c6 | 2018-01-04 18:02:24 -0800 | [diff] [blame] | 110 | binding.setBuffer(context, nullptr, isBound); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 111 | } |
James Darpinian | e8a93c6 | 2018-01-04 18:02:24 -0800 | [diff] [blame] | 112 | if (isBound && mState.mElementArrayBuffer.get()) |
Jamie Madill | ca8eda4 | 2018-10-18 18:41:56 -0400 | [diff] [blame] | 113 | mState.mElementArrayBuffer->onNonTFBindingChanged(-1); |
Jamie Madill | cd0a0a3 | 2018-10-18 18:41:57 -0400 | [diff] [blame^] | 114 | mState.mElementArrayBuffer.bind(context, nullptr); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 115 | mVertexArray->destroy(context); |
| 116 | SafeDelete(mVertexArray); |
| 117 | delete this; |
| 118 | } |
| 119 | |
Jamie Madill | 004a6f9 | 2013-07-10 15:13:38 -0400 | [diff] [blame] | 120 | VertexArray::~VertexArray() |
| 121 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 122 | ASSERT(!mVertexArray); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Shannon Woods | aa2ab7d | 2014-06-24 17:51:51 -0400 | [diff] [blame] | 125 | GLuint VertexArray::id() const |
| 126 | { |
| 127 | return mId; |
| 128 | } |
| 129 | |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 130 | void VertexArray::setLabel(const std::string &label) |
| 131 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame] | 132 | mState.mLabel = label; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | const std::string &VertexArray::getLabel() const |
| 136 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame] | 137 | return mState.mLabel; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 140 | void VertexArray::detachBuffer(const Context *context, GLuint bufferName) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 141 | { |
James Darpinian | e8a93c6 | 2018-01-04 18:02:24 -0800 | [diff] [blame] | 142 | bool isBound = context->isCurrentVertexArray(this); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 143 | for (auto &binding : mState.mVertexBindings) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 144 | { |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame] | 145 | if (binding.getBuffer().id() == bufferName) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 146 | { |
James Darpinian | e8a93c6 | 2018-01-04 18:02:24 -0800 | [diff] [blame] | 147 | binding.setBuffer(context, nullptr, isBound); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Jamie Madill | cd0a0a3 | 2018-10-18 18:41:57 -0400 | [diff] [blame^] | 151 | if (mState.mElementArrayBuffer.get() && mState.mElementArrayBuffer->id() == bufferName) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 152 | { |
James Darpinian | e8a93c6 | 2018-01-04 18:02:24 -0800 | [diff] [blame] | 153 | if (isBound && mState.mElementArrayBuffer.get()) |
Jamie Madill | ca8eda4 | 2018-10-18 18:41:56 -0400 | [diff] [blame] | 154 | mState.mElementArrayBuffer->onNonTFBindingChanged(-1); |
Jamie Madill | cd0a0a3 | 2018-10-18 18:41:57 -0400 | [diff] [blame^] | 155 | mState.mElementArrayBuffer.bind(context, nullptr); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 159 | const VertexAttribute &VertexArray::getVertexAttribute(size_t attribIndex) const |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 160 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 161 | ASSERT(attribIndex < getMaxAttribs()); |
| 162 | return mState.mVertexAttributes[attribIndex]; |
| 163 | } |
| 164 | |
| 165 | const VertexBinding &VertexArray::getVertexBinding(size_t bindingIndex) const |
| 166 | { |
| 167 | ASSERT(bindingIndex < getMaxBindings()); |
| 168 | return mState.mVertexBindings[bindingIndex]; |
| 169 | } |
| 170 | |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 171 | size_t VertexArray::GetVertexIndexFromDirtyBit(size_t dirtyBit) |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 172 | { |
| 173 | static_assert(gl::MAX_VERTEX_ATTRIBS == gl::MAX_VERTEX_ATTRIB_BINDINGS, |
| 174 | "The stride of vertex attributes should equal to that of vertex bindings."); |
Shao | 80957d9 | 2017-02-20 21:25:59 +0800 | [diff] [blame] | 175 | ASSERT(dirtyBit > DIRTY_BIT_ELEMENT_ARRAY_BUFFER); |
Jamie Madill | e858cb1 | 2018-03-27 09:44:32 -0400 | [diff] [blame] | 176 | return (dirtyBit - DIRTY_BIT_ATTRIB_0) % gl::MAX_VERTEX_ATTRIBS; |
| 177 | } |
| 178 | |
| 179 | void VertexArray::setDirtyAttribBit(size_t attribIndex, DirtyAttribBitType dirtyAttribBit) |
| 180 | { |
| 181 | mDirtyBits.set(DIRTY_BIT_ATTRIB_0 + attribIndex); |
| 182 | mDirtyAttribBits[attribIndex].set(dirtyAttribBit); |
| 183 | } |
| 184 | |
| 185 | void VertexArray::setDirtyBindingBit(size_t bindingIndex, DirtyBindingBitType dirtyBindingBit) |
| 186 | { |
| 187 | mDirtyBits.set(DIRTY_BIT_BINDING_0 + bindingIndex); |
| 188 | mDirtyBindingBits[bindingIndex].set(dirtyBindingBit); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 189 | } |
| 190 | |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 191 | void VertexArray::bindVertexBufferImpl(const Context *context, |
| 192 | size_t bindingIndex, |
| 193 | Buffer *boundBuffer, |
| 194 | GLintptr offset, |
| 195 | GLsizei stride) |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 196 | { |
| 197 | ASSERT(bindingIndex < getMaxBindings()); |
James Darpinian | e8a93c6 | 2018-01-04 18:02:24 -0800 | [diff] [blame] | 198 | bool isBound = context->isCurrentVertexArray(this); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 199 | |
| 200 | VertexBinding *binding = &mState.mVertexBindings[bindingIndex]; |
| 201 | |
James Darpinian | e8a93c6 | 2018-01-04 18:02:24 -0800 | [diff] [blame] | 202 | binding->setBuffer(context, boundBuffer, isBound); |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame] | 203 | binding->setOffset(offset); |
| 204 | binding->setStride(stride); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 205 | |
| 206 | updateObserverBinding(bindingIndex); |
Jamie Madill | bdc610a | 2018-07-30 10:26:00 -0400 | [diff] [blame] | 207 | updateCachedBufferBindingSize(binding); |
Jamie Madill | 7267aa6 | 2018-04-17 15:28:21 -0400 | [diff] [blame] | 208 | updateCachedTransformFeedbackBindingValidation(bindingIndex, boundBuffer); |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 209 | updateCachedMappedArrayBuffers(binding); |
Jamie Madill | dc358af | 2018-07-31 11:22:13 -0400 | [diff] [blame] | 210 | |
| 211 | // Update client memory attribute pointers. Affects all bound attributes. |
| 212 | if (boundBuffer) |
| 213 | { |
Jamie Madill | ad63728 | 2018-07-31 11:22:14 -0400 | [diff] [blame] | 214 | mState.mClientMemoryAttribsMask &= ~binding->getBoundAttributesMask(); |
Jamie Madill | dc358af | 2018-07-31 11:22:13 -0400 | [diff] [blame] | 215 | } |
| 216 | else |
| 217 | { |
Jamie Madill | ad63728 | 2018-07-31 11:22:14 -0400 | [diff] [blame] | 218 | mState.mClientMemoryAttribsMask |= binding->getBoundAttributesMask(); |
Jamie Madill | dc358af | 2018-07-31 11:22:13 -0400 | [diff] [blame] | 219 | } |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void VertexArray::bindVertexBuffer(const Context *context, |
| 223 | size_t bindingIndex, |
| 224 | Buffer *boundBuffer, |
| 225 | GLintptr offset, |
| 226 | GLsizei stride) |
| 227 | { |
| 228 | bindVertexBufferImpl(context, bindingIndex, boundBuffer, offset, stride); |
Jamie Madill | e858cb1 | 2018-03-27 09:44:32 -0400 | [diff] [blame] | 229 | setDirtyBindingBit(bindingIndex, DIRTY_BINDING_BUFFER); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 230 | } |
| 231 | |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 232 | void VertexArray::setVertexAttribBinding(const Context *context, |
| 233 | size_t attribIndex, |
| 234 | GLuint bindingIndex) |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 235 | { |
| 236 | ASSERT(attribIndex < getMaxAttribs() && bindingIndex < getMaxBindings()); |
| 237 | |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 238 | if (mState.mVertexAttributes[attribIndex].bindingIndex != bindingIndex) |
| 239 | { |
| 240 | // In ES 3.0 contexts, the binding cannot change, hence the code below is unreachable. |
| 241 | ASSERT(context->getClientVersion() >= ES_3_1); |
Jiawei Shao | 6a5d98c | 2018-05-04 15:42:20 +0800 | [diff] [blame] | 242 | |
| 243 | mState.setAttribBinding(attribIndex, bindingIndex); |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 244 | |
Jamie Madill | e858cb1 | 2018-03-27 09:44:32 -0400 | [diff] [blame] | 245 | setDirtyAttribBit(attribIndex, DIRTY_ATTRIB_BINDING); |
Jamie Madill | dc358af | 2018-07-31 11:22:13 -0400 | [diff] [blame] | 246 | |
| 247 | // Update client attribs mask. |
| 248 | bool hasBuffer = mState.mVertexBindings[bindingIndex].getBuffer().get() != nullptr; |
| 249 | mState.mClientMemoryAttribsMask.set(attribIndex, !hasBuffer); |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 250 | } |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void VertexArray::setVertexBindingDivisor(size_t bindingIndex, GLuint divisor) |
| 254 | { |
| 255 | ASSERT(bindingIndex < getMaxBindings()); |
| 256 | |
Jamie Madill | bdc610a | 2018-07-30 10:26:00 -0400 | [diff] [blame] | 257 | VertexBinding &binding = mState.mVertexBindings[bindingIndex]; |
| 258 | |
| 259 | binding.setDivisor(divisor); |
Jamie Madill | e858cb1 | 2018-03-27 09:44:32 -0400 | [diff] [blame] | 260 | setDirtyBindingBit(bindingIndex, DIRTY_BINDING_DIVISOR); |
Jamie Madill | bdc610a | 2018-07-30 10:26:00 -0400 | [diff] [blame] | 261 | |
| 262 | // Trigger updates in all bound attributes. |
| 263 | for (size_t attribIndex : binding.getBoundAttributesMask()) |
| 264 | { |
| 265 | mState.mVertexAttributes[attribIndex].updateCachedElementLimit(binding); |
| 266 | } |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 267 | } |
| 268 | |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 269 | void VertexArray::setVertexAttribFormatImpl(size_t attribIndex, |
| 270 | GLint size, |
| 271 | GLenum type, |
| 272 | bool normalized, |
| 273 | bool pureInteger, |
| 274 | GLuint relativeOffset) |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 275 | { |
| 276 | ASSERT(attribIndex < getMaxAttribs()); |
| 277 | |
| 278 | VertexAttribute *attrib = &mState.mVertexAttributes[attribIndex]; |
| 279 | |
| 280 | attrib->size = size; |
| 281 | attrib->type = type; |
| 282 | attrib->normalized = normalized; |
| 283 | attrib->pureInteger = pureInteger; |
| 284 | attrib->relativeOffset = relativeOffset; |
Brandon Jones | c405ae7 | 2017-12-06 14:15:03 -0800 | [diff] [blame] | 285 | mState.mVertexAttributesTypeMask.setIndex(GetVertexAttributeBaseType(*attrib), attribIndex); |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void VertexArray::setVertexAttribFormat(size_t attribIndex, |
| 289 | GLint size, |
| 290 | GLenum type, |
| 291 | bool normalized, |
| 292 | bool pureInteger, |
| 293 | GLuint relativeOffset) |
| 294 | { |
| 295 | setVertexAttribFormatImpl(attribIndex, size, type, normalized, pureInteger, relativeOffset); |
Jamie Madill | e858cb1 | 2018-03-27 09:44:32 -0400 | [diff] [blame] | 296 | setDirtyAttribBit(attribIndex, DIRTY_ATTRIB_FORMAT); |
Jamie Madill | bdc610a | 2018-07-30 10:26:00 -0400 | [diff] [blame] | 297 | |
| 298 | VertexAttribute &attrib = mState.mVertexAttributes[attribIndex]; |
| 299 | attrib.updateCachedElementLimit(mState.mVertexBindings[attrib.bindingIndex]); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 302 | void VertexArray::setVertexAttribDivisor(const Context *context, size_t attribIndex, GLuint divisor) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 303 | { |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 304 | ASSERT(attribIndex < getMaxAttribs()); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 305 | |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 306 | setVertexAttribBinding(context, attribIndex, static_cast<GLuint>(attribIndex)); |
| 307 | setVertexBindingDivisor(attribIndex, divisor); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 310 | void VertexArray::enableAttribute(size_t attribIndex, bool enabledState) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 311 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 312 | ASSERT(attribIndex < getMaxAttribs()); |
Shao | 80957d9 | 2017-02-20 21:25:59 +0800 | [diff] [blame] | 313 | |
Jamie Madill | bdc610a | 2018-07-30 10:26:00 -0400 | [diff] [blame] | 314 | VertexAttribute &attrib = mState.mVertexAttributes[attribIndex]; |
| 315 | |
Shahbaz Youssefi | 337bd69 | 2018-08-22 16:16:38 -0400 | [diff] [blame] | 316 | if (mState.mEnabledAttributesMask.test(attribIndex) == enabledState) |
| 317 | { |
| 318 | return; |
| 319 | } |
| 320 | |
Jamie Madill | bdc610a | 2018-07-30 10:26:00 -0400 | [diff] [blame] | 321 | attrib.enabled = enabledState; |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 322 | |
Jamie Madill | e858cb1 | 2018-03-27 09:44:32 -0400 | [diff] [blame] | 323 | setDirtyAttribBit(attribIndex, DIRTY_ATTRIB_ENABLED); |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame] | 324 | |
| 325 | // Update state cache |
Jamie Madill | d078c68 | 2018-01-02 11:50:24 -0500 | [diff] [blame] | 326 | mState.mEnabledAttributesMask.set(attribIndex, enabledState); |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 327 | mState.mCachedEnabledMappedArrayBuffers = |
| 328 | mState.mCachedMappedArrayBuffers & mState.mEnabledAttributesMask; |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 329 | } |
| 330 | |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 331 | void VertexArray::setVertexAttribPointer(const Context *context, |
| 332 | size_t attribIndex, |
| 333 | gl::Buffer *boundBuffer, |
| 334 | GLint size, |
| 335 | GLenum type, |
| 336 | bool normalized, |
| 337 | bool pureInteger, |
| 338 | GLsizei stride, |
| 339 | const void *pointer) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 340 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 341 | ASSERT(attribIndex < getMaxAttribs()); |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 342 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 343 | GLintptr offset = boundBuffer ? reinterpret_cast<GLintptr>(pointer) : 0; |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 344 | |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 345 | setVertexAttribFormatImpl(attribIndex, size, type, normalized, pureInteger, 0); |
| 346 | setVertexAttribBinding(context, attribIndex, static_cast<GLuint>(attribIndex)); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 347 | |
| 348 | VertexAttribute &attrib = mState.mVertexAttributes[attribIndex]; |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 349 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 350 | GLsizei effectiveStride = |
| 351 | stride != 0 ? stride : static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)); |
| 352 | attrib.pointer = pointer; |
| 353 | attrib.vertexAttribArrayStride = stride; |
| 354 | |
Shao | dde78e8 | 2017-05-22 14:13:27 +0800 | [diff] [blame] | 355 | bindVertexBufferImpl(context, attribIndex, boundBuffer, offset, effectiveStride); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 356 | |
Jamie Madill | e858cb1 | 2018-03-27 09:44:32 -0400 | [diff] [blame] | 357 | setDirtyAttribBit(attribIndex, DIRTY_ATTRIB_POINTER); |
Jamie Madill | bcef322 | 2018-04-13 15:19:11 -0400 | [diff] [blame] | 358 | |
Jamie Madill | 51af38b | 2018-04-15 08:50:56 -0400 | [diff] [blame] | 359 | mState.mNullPointerClientMemoryAttribsMask.set(attribIndex, |
| 360 | boundBuffer == nullptr && pointer == nullptr); |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 363 | angle::Result VertexArray::syncState(const Context *context) |
Jamie Madill | 0b9e903 | 2015-08-17 11:51:52 +0000 | [diff] [blame] | 364 | { |
| 365 | if (mDirtyBits.any()) |
| 366 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 367 | mDirtyBitsGuard = mDirtyBits; |
Frank Henigman | 0af5b86 | 2018-03-27 20:19:33 -0400 | [diff] [blame] | 368 | ANGLE_TRY( |
| 369 | mVertexArray->syncState(context, mDirtyBits, mDirtyAttribBits, mDirtyBindingBits)); |
Jamie Madill | 0b9e903 | 2015-08-17 11:51:52 +0000 | [diff] [blame] | 370 | mDirtyBits.reset(); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 371 | mDirtyBitsGuard.reset(); |
Jamie Madill | e858cb1 | 2018-03-27 09:44:32 -0400 | [diff] [blame] | 372 | |
| 373 | // This is a bit of an implementation hack - but since we know the implementation |
| 374 | // details of the dirty bit class it should always have the same effect as iterating |
| 375 | // individual attribs. We could also look into schemes where iterating the dirty |
| 376 | // bit set also resets it as you pass through it. |
| 377 | memset(&mDirtyAttribBits, 0, sizeof(mDirtyAttribBits)); |
| 378 | memset(&mDirtyBindingBits, 0, sizeof(mDirtyBindingBits)); |
Jamie Madill | 0b9e903 | 2015-08-17 11:51:52 +0000 | [diff] [blame] | 379 | } |
Jamie Madill | 6f755b2 | 2018-10-09 12:48:54 -0400 | [diff] [blame] | 380 | return angle::Result::Continue(); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 381 | } |
| 382 | |
Jamie Madill | bf5177d | 2018-08-21 12:58:20 -0400 | [diff] [blame] | 383 | void VertexArray::onBindingChanged(const Context *context, int incr) |
James Darpinian | e8a93c6 | 2018-01-04 18:02:24 -0800 | [diff] [blame] | 384 | { |
| 385 | if (mState.mElementArrayBuffer.get()) |
Jamie Madill | ca8eda4 | 2018-10-18 18:41:56 -0400 | [diff] [blame] | 386 | mState.mElementArrayBuffer->onNonTFBindingChanged(incr); |
James Darpinian | e8a93c6 | 2018-01-04 18:02:24 -0800 | [diff] [blame] | 387 | for (auto &binding : mState.mVertexBindings) |
| 388 | { |
Jamie Madill | bf5177d | 2018-08-21 12:58:20 -0400 | [diff] [blame] | 389 | binding.onContainerBindingChanged(context, incr); |
James Darpinian | e8a93c6 | 2018-01-04 18:02:24 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 393 | VertexArray::DirtyBitType VertexArray::getDirtyBitFromIndex(bool contentsChanged, |
| 394 | angle::SubjectIndex index) const |
| 395 | { |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 396 | if (IsElementArrayBufferSubjectIndex(index)) |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 397 | { |
| 398 | return contentsChanged ? DIRTY_BIT_ELEMENT_ARRAY_BUFFER_DATA |
| 399 | : DIRTY_BIT_ELEMENT_ARRAY_BUFFER; |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | // Note: this currently just gets the top-level dirty bit. |
| 404 | ASSERT(index < mArrayBufferObserverBindings.size()); |
| 405 | return static_cast<DirtyBitType>( |
| 406 | (contentsChanged ? DIRTY_BIT_BUFFER_DATA_0 : DIRTY_BIT_BINDING_0) + index); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | void VertexArray::onSubjectStateChange(const gl::Context *context, |
| 411 | angle::SubjectIndex index, |
| 412 | angle::SubjectMessage message) |
| 413 | { |
Jamie Madill | 7267aa6 | 2018-04-17 15:28:21 -0400 | [diff] [blame] | 414 | switch (message) |
| 415 | { |
| 416 | case angle::SubjectMessage::CONTENTS_CHANGED: |
| 417 | setDependentDirtyBit(context, true, index); |
| 418 | break; |
| 419 | |
| 420 | case angle::SubjectMessage::STORAGE_CHANGED: |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 421 | if (!IsElementArrayBufferSubjectIndex(index)) |
Jamie Madill | 7267aa6 | 2018-04-17 15:28:21 -0400 | [diff] [blame] | 422 | { |
Jamie Madill | bdc610a | 2018-07-30 10:26:00 -0400 | [diff] [blame] | 423 | updateCachedBufferBindingSize(&mState.mVertexBindings[index]); |
Jamie Madill | 7267aa6 | 2018-04-17 15:28:21 -0400 | [diff] [blame] | 424 | } |
Jamie Madill | a2d1d2d | 2018-08-01 11:34:46 -0400 | [diff] [blame] | 425 | setDependentDirtyBit(context, false, index); |
Jamie Madill | 7267aa6 | 2018-04-17 15:28:21 -0400 | [diff] [blame] | 426 | break; |
| 427 | |
| 428 | case angle::SubjectMessage::BINDING_CHANGED: |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 429 | if (!IsElementArrayBufferSubjectIndex(index)) |
Jamie Madill | 7267aa6 | 2018-04-17 15:28:21 -0400 | [diff] [blame] | 430 | { |
| 431 | const Buffer *buffer = mState.mVertexBindings[index].getBuffer().get(); |
| 432 | updateCachedTransformFeedbackBindingValidation(index, buffer); |
| 433 | } |
| 434 | break; |
| 435 | |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 436 | case angle::SubjectMessage::RESOURCE_MAPPED: |
| 437 | if (!IsElementArrayBufferSubjectIndex(index)) |
| 438 | { |
| 439 | updateCachedMappedArrayBuffers(&mState.mVertexBindings[index]); |
Jamie Madill | d84b673 | 2018-09-06 15:54:35 -0400 | [diff] [blame] | 440 | onStateChange(context, angle::SubjectMessage::RESOURCE_MAPPED); |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 441 | } |
| 442 | break; |
| 443 | |
| 444 | case angle::SubjectMessage::RESOURCE_UNMAPPED: |
| 445 | setDependentDirtyBit(context, true, index); |
| 446 | |
| 447 | if (!IsElementArrayBufferSubjectIndex(index)) |
| 448 | { |
| 449 | updateCachedMappedArrayBuffers(&mState.mVertexBindings[index]); |
Jamie Madill | d84b673 | 2018-09-06 15:54:35 -0400 | [diff] [blame] | 450 | onStateChange(context, angle::SubjectMessage::RESOURCE_UNMAPPED); |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 451 | } |
| 452 | break; |
| 453 | |
Jamie Madill | 7267aa6 | 2018-04-17 15:28:21 -0400 | [diff] [blame] | 454 | default: |
| 455 | UNREACHABLE(); |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void VertexArray::setDependentDirtyBit(const gl::Context *context, |
| 461 | bool contentsChanged, |
| 462 | angle::SubjectIndex index) |
| 463 | { |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 464 | DirtyBitType dirtyBit = getDirtyBitFromIndex(contentsChanged, index); |
| 465 | ASSERT(!mDirtyBitsGuard.valid() || mDirtyBitsGuard.value().test(dirtyBit)); |
| 466 | mDirtyBits.set(dirtyBit); |
Jamie Madill | a11819d | 2018-07-30 10:26:01 -0400 | [diff] [blame] | 467 | onStateChange(context, angle::SubjectMessage::CONTENTS_CHANGED); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | void VertexArray::updateObserverBinding(size_t bindingIndex) |
| 471 | { |
| 472 | Buffer *boundBuffer = mState.mVertexBindings[bindingIndex].getBuffer().get(); |
Jamie Madill | 472ddc8 | 2018-10-18 18:41:56 -0400 | [diff] [blame] | 473 | mArrayBufferObserverBindings[bindingIndex].bind(boundBuffer); |
Jamie Madill | 0946393 | 2018-04-04 05:26:59 -0400 | [diff] [blame] | 474 | } |
| 475 | |
Jamie Madill | bdc610a | 2018-07-30 10:26:00 -0400 | [diff] [blame] | 476 | void VertexArray::updateCachedBufferBindingSize(VertexBinding *binding) |
Jamie Madill | 02c9c04 | 2018-04-17 13:43:48 -0400 | [diff] [blame] | 477 | { |
Jamie Madill | bdc610a | 2018-07-30 10:26:00 -0400 | [diff] [blame] | 478 | for (size_t boundAttribute : binding->getBoundAttributesMask()) |
| 479 | { |
| 480 | mState.mVertexAttributes[boundAttribute].updateCachedElementLimit(*binding); |
| 481 | } |
Jamie Madill | 02c9c04 | 2018-04-17 13:43:48 -0400 | [diff] [blame] | 482 | } |
| 483 | |
Jamie Madill | 7267aa6 | 2018-04-17 15:28:21 -0400 | [diff] [blame] | 484 | void VertexArray::updateCachedTransformFeedbackBindingValidation(size_t bindingIndex, |
| 485 | const Buffer *buffer) |
| 486 | { |
| 487 | const bool hasConflict = buffer && buffer->isBoundForTransformFeedbackAndOtherUse(); |
| 488 | mCachedTransformFeedbackConflictedBindingsMask.set(bindingIndex, hasConflict); |
| 489 | } |
| 490 | |
Jamie Madill | ac43aaa | 2018-07-31 11:22:13 -0400 | [diff] [blame] | 491 | bool VertexArray::hasTransformFeedbackBindingConflict(const gl::Context *context) const |
Jamie Madill | 7267aa6 | 2018-04-17 15:28:21 -0400 | [diff] [blame] | 492 | { |
| 493 | // Fast check first. |
| 494 | if (!mCachedTransformFeedbackConflictedBindingsMask.any()) |
| 495 | { |
| 496 | return false; |
| 497 | } |
| 498 | |
Jamie Madill | ac43aaa | 2018-07-31 11:22:13 -0400 | [diff] [blame] | 499 | const AttributesMask &activeAttribues = context->getStateCache().getActiveBufferedAttribsMask(); |
| 500 | |
Jamie Madill | 7267aa6 | 2018-04-17 15:28:21 -0400 | [diff] [blame] | 501 | // Slow check. We must ensure that the conflicting attributes are enabled/active. |
| 502 | for (size_t attribIndex : activeAttribues) |
| 503 | { |
| 504 | const VertexAttribute &attrib = mState.mVertexAttributes[attribIndex]; |
| 505 | if (mCachedTransformFeedbackConflictedBindingsMask[attrib.bindingIndex]) |
| 506 | { |
| 507 | return true; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | return false; |
| 512 | } |
| 513 | |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 514 | void VertexArray::updateCachedMappedArrayBuffers(VertexBinding *binding) |
Jamie Madill | cc73f24 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 515 | { |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 516 | Buffer *buffer = binding->getBuffer().get(); |
| 517 | if (buffer && buffer->isMapped()) |
Jamie Madill | cc73f24 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 518 | { |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 519 | mState.mCachedMappedArrayBuffers |= binding->getBoundAttributesMask(); |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | mState.mCachedMappedArrayBuffers &= ~binding->getBoundAttributesMask(); |
Jamie Madill | cc73f24 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 524 | } |
| 525 | |
Jamie Madill | d69a5f1 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 526 | mState.mCachedEnabledMappedArrayBuffers = |
| 527 | mState.mCachedMappedArrayBuffers & mState.mEnabledAttributesMask; |
Jamie Madill | cc73f24 | 2018-08-01 11:34:48 -0400 | [diff] [blame] | 528 | } |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 529 | } // namespace gl |