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