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 | 7aea7e0 | 2016-05-10 10:39:45 -0400 | [diff] [blame] | 12 | #include "libANGLE/renderer/GLImplFactory.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 13 | #include "libANGLE/renderer/VertexArrayImpl.h" |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 14 | |
| 15 | namespace gl |
| 16 | { |
| 17 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 18 | VertexArrayState::VertexArrayState(size_t maxAttribs, size_t maxAttribBindings) |
| 19 | : mLabel(), mVertexBindings(maxAttribBindings), mMaxEnabledAttribute(0) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 20 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 21 | ASSERT(maxAttribs <= maxAttribBindings); |
| 22 | |
| 23 | for (size_t i = 0; i < maxAttribs; i++) |
| 24 | { |
| 25 | mVertexAttributes.emplace_back(static_cast<GLuint>(i)); |
| 26 | } |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 27 | } |
| 28 | |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame] | 29 | VertexArrayState::~VertexArrayState() |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 30 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 31 | for (auto &binding : mVertexBindings) |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 32 | { |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 33 | binding.setBuffer(nullptr); |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 34 | } |
| 35 | mElementArrayBuffer.set(nullptr); |
| 36 | } |
| 37 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 38 | VertexArray::VertexArray(rx::GLImplFactory *factory, |
| 39 | GLuint id, |
| 40 | size_t maxAttribs, |
| 41 | size_t maxAttribBindings) |
| 42 | : mId(id), |
| 43 | mState(maxAttribs, maxAttribBindings), |
| 44 | mVertexArray(factory->createVertexArray(mState)) |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 45 | { |
Jamie Madill | 004a6f9 | 2013-07-10 15:13:38 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | VertexArray::~VertexArray() |
| 49 | { |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame] | 50 | SafeDelete(mVertexArray); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Shannon Woods | aa2ab7d | 2014-06-24 17:51:51 -0400 | [diff] [blame] | 53 | GLuint VertexArray::id() const |
| 54 | { |
| 55 | return mId; |
| 56 | } |
| 57 | |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 58 | void VertexArray::setLabel(const std::string &label) |
| 59 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame] | 60 | mState.mLabel = label; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | const std::string &VertexArray::getLabel() const |
| 64 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame] | 65 | return mState.mLabel; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 68 | void VertexArray::detachBuffer(GLuint bufferName) |
| 69 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 70 | for (auto &binding : mState.mVertexBindings) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 71 | { |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 72 | if (binding.getBuffer().id() == bufferName) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 73 | { |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 74 | binding.setBuffer(nullptr); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame] | 78 | if (mState.mElementArrayBuffer.id() == bufferName) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 79 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame] | 80 | mState.mElementArrayBuffer.set(nullptr); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 84 | const VertexAttribute &VertexArray::getVertexAttribute(size_t attribIndex) const |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 85 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 86 | ASSERT(attribIndex < getMaxAttribs()); |
| 87 | return mState.mVertexAttributes[attribIndex]; |
| 88 | } |
| 89 | |
| 90 | const VertexBinding &VertexArray::getVertexBinding(size_t bindingIndex) const |
| 91 | { |
| 92 | ASSERT(bindingIndex < getMaxBindings()); |
| 93 | return mState.mVertexBindings[bindingIndex]; |
| 94 | } |
| 95 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 96 | size_t VertexArray::GetAttribIndex(size_t dirtyBit) |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 97 | { |
| 98 | static_assert(gl::MAX_VERTEX_ATTRIBS == gl::MAX_VERTEX_ATTRIB_BINDINGS, |
| 99 | "The stride of vertex attributes should equal to that of vertex bindings."); |
Shao | 80957d9 | 2017-02-20 21:25:59 +0800 | [diff] [blame] | 100 | ASSERT(dirtyBit > DIRTY_BIT_ELEMENT_ARRAY_BUFFER); |
| 101 | return (dirtyBit - DIRTY_BIT_ATTRIB_0_ENABLED) % gl::MAX_VERTEX_ATTRIBS; |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void VertexArray::bindVertexBuffer(size_t bindingIndex, |
| 105 | Buffer *boundBuffer, |
| 106 | GLintptr offset, |
| 107 | GLsizei stride) |
| 108 | { |
| 109 | ASSERT(bindingIndex < getMaxBindings()); |
| 110 | |
| 111 | VertexBinding *binding = &mState.mVertexBindings[bindingIndex]; |
| 112 | |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 113 | binding->setBuffer(boundBuffer); |
| 114 | binding->setOffset(offset); |
| 115 | binding->setStride(stride); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 116 | mDirtyBits.set(DIRTY_BIT_BINDING_0_BUFFER + bindingIndex); |
| 117 | } |
| 118 | |
| 119 | void VertexArray::setVertexAttribBinding(size_t attribIndex, size_t bindingIndex) |
| 120 | { |
| 121 | ASSERT(attribIndex < getMaxAttribs() && bindingIndex < getMaxBindings()); |
| 122 | |
Shao | 80957d9 | 2017-02-20 21:25:59 +0800 | [diff] [blame] | 123 | mState.mVertexAttributes[attribIndex].bindingIndex = static_cast<GLuint>(bindingIndex); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 124 | mDirtyBits.set(DIRTY_BIT_ATTRIB_0_BINDING + attribIndex); |
| 125 | } |
| 126 | |
| 127 | void VertexArray::setVertexBindingDivisor(size_t bindingIndex, GLuint divisor) |
| 128 | { |
| 129 | ASSERT(bindingIndex < getMaxBindings()); |
| 130 | |
Martin Radev | dd5f27e | 2017-06-07 10:17:09 +0300 | [diff] [blame^] | 131 | mState.mVertexBindings[bindingIndex].setDivisor(divisor); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 132 | mDirtyBits.set(DIRTY_BIT_BINDING_0_DIVISOR + bindingIndex); |
| 133 | } |
| 134 | |
| 135 | void VertexArray::setVertexAttribFormat(size_t attribIndex, |
| 136 | GLint size, |
| 137 | GLenum type, |
| 138 | bool normalized, |
| 139 | bool pureInteger, |
| 140 | GLintptr relativeOffset) |
| 141 | { |
| 142 | ASSERT(attribIndex < getMaxAttribs()); |
| 143 | |
| 144 | VertexAttribute *attrib = &mState.mVertexAttributes[attribIndex]; |
| 145 | |
| 146 | attrib->size = size; |
| 147 | attrib->type = type; |
| 148 | attrib->normalized = normalized; |
| 149 | attrib->pureInteger = pureInteger; |
| 150 | attrib->relativeOffset = relativeOffset; |
| 151 | mDirtyBits.set(DIRTY_BIT_ATTRIB_0_FORMAT + attribIndex); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 154 | void VertexArray::setVertexAttribDivisor(size_t index, GLuint divisor) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 155 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 156 | ASSERT(index < getMaxAttribs()); |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 157 | |
| 158 | setVertexAttribBinding(index, index); |
| 159 | setVertexBindingDivisor(index, divisor); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 162 | void VertexArray::enableAttribute(size_t attribIndex, bool enabledState) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 163 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 164 | ASSERT(attribIndex < getMaxAttribs()); |
Shao | 80957d9 | 2017-02-20 21:25:59 +0800 | [diff] [blame] | 165 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 166 | mState.mVertexAttributes[attribIndex].enabled = enabledState; |
| 167 | mDirtyBits.set(DIRTY_BIT_ATTRIB_0_ENABLED + attribIndex); |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame] | 168 | |
| 169 | // Update state cache |
| 170 | if (enabledState) |
| 171 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 172 | mState.mMaxEnabledAttribute = std::max(attribIndex + 1, mState.mMaxEnabledAttribute); |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame] | 173 | } |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 174 | else if (mState.mMaxEnabledAttribute == attribIndex + 1) |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame] | 175 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame] | 176 | while (mState.mMaxEnabledAttribute > 0 && |
| 177 | !mState.mVertexAttributes[mState.mMaxEnabledAttribute - 1].enabled) |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame] | 178 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame] | 179 | --mState.mMaxEnabledAttribute; |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame] | 180 | } |
| 181 | } |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 182 | } |
| 183 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 184 | void VertexArray::setAttributeState(size_t attribIndex, |
| 185 | gl::Buffer *boundBuffer, |
| 186 | GLint size, |
| 187 | GLenum type, |
| 188 | bool normalized, |
| 189 | bool pureInteger, |
| 190 | GLsizei stride, |
| 191 | const void *pointer) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 192 | { |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 193 | ASSERT(attribIndex < getMaxAttribs()); |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 194 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 195 | GLintptr offset = boundBuffer ? reinterpret_cast<GLintptr>(pointer) : 0; |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 196 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 197 | setVertexAttribFormat(attribIndex, size, type, normalized, pureInteger, 0); |
| 198 | setVertexAttribBinding(attribIndex, attribIndex); |
| 199 | |
| 200 | VertexAttribute &attrib = mState.mVertexAttributes[attribIndex]; |
| 201 | GLsizei effectiveStride = |
| 202 | stride != 0 ? stride : static_cast<GLsizei>(ComputeVertexAttributeTypeSize(attrib)); |
| 203 | attrib.pointer = pointer; |
| 204 | attrib.vertexAttribArrayStride = stride; |
| 205 | |
| 206 | bindVertexBuffer(attribIndex, boundBuffer, offset, effectiveStride); |
| 207 | |
| 208 | mDirtyBits.set(DIRTY_BIT_ATTRIB_0_POINTER + attribIndex); |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void VertexArray::setElementArrayBuffer(Buffer *buffer) |
| 212 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame] | 213 | mState.mElementArrayBuffer.set(buffer); |
Jamie Madill | 0b9e903 | 2015-08-17 11:51:52 +0000 | [diff] [blame] | 214 | mDirtyBits.set(DIRTY_BIT_ELEMENT_ARRAY_BUFFER); |
| 215 | } |
| 216 | |
Jamie Madill | dd43e6c | 2017-03-24 14:18:49 -0400 | [diff] [blame] | 217 | void VertexArray::syncImplState(const Context *context) |
Jamie Madill | 0b9e903 | 2015-08-17 11:51:52 +0000 | [diff] [blame] | 218 | { |
| 219 | if (mDirtyBits.any()) |
| 220 | { |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 221 | mVertexArray->syncState(context, mDirtyBits); |
Jamie Madill | 0b9e903 | 2015-08-17 11:51:52 +0000 | [diff] [blame] | 222 | mDirtyBits.reset(); |
| 223 | } |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 226 | } // namespace gl |