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