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