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