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