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