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