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 | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 11 | #include "libANGLE/renderer/ImplFactory.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 12 | #include "libANGLE/renderer/VertexArrayImpl.h" |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 13 | |
| 14 | namespace gl |
| 15 | { |
| 16 | |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 17 | VertexArrayState::VertexArrayState(size_t maxAttribs) |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 18 | : mLabel(), mVertexAttributes(maxAttribs), mMaxEnabledAttribute(0) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 19 | { |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 20 | } |
| 21 | |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 22 | VertexArrayState::~VertexArrayState() |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 23 | { |
| 24 | for (size_t i = 0; i < getMaxAttribs(); i++) |
| 25 | { |
| 26 | mVertexAttributes[i].buffer.set(nullptr); |
| 27 | } |
| 28 | mElementArrayBuffer.set(nullptr); |
| 29 | } |
| 30 | |
| 31 | VertexArray::VertexArray(rx::ImplFactory *factory, GLuint id, size_t maxAttribs) |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 32 | : mId(id), mState(maxAttribs), mVertexArray(factory->createVertexArray(mState)) |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 33 | { |
Jamie Madill | 004a6f9 | 2013-07-10 15:13:38 -0400 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | VertexArray::~VertexArray() |
| 37 | { |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame] | 38 | SafeDelete(mVertexArray); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 39 | } |
| 40 | |
Shannon Woods | aa2ab7d | 2014-06-24 17:51:51 -0400 | [diff] [blame] | 41 | GLuint VertexArray::id() const |
| 42 | { |
| 43 | return mId; |
| 44 | } |
| 45 | |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 46 | void VertexArray::setLabel(const std::string &label) |
| 47 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 48 | mState.mLabel = label; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | const std::string &VertexArray::getLabel() const |
| 52 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 53 | return mState.mLabel; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 56 | void VertexArray::detachBuffer(GLuint bufferName) |
| 57 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 58 | for (size_t attribute = 0; attribute < getMaxAttribs(); attribute++) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 59 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 60 | if (mState.mVertexAttributes[attribute].buffer.id() == bufferName) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 61 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 62 | mState.mVertexAttributes[attribute].buffer.set(nullptr); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 66 | if (mState.mElementArrayBuffer.id() == bufferName) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 67 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 68 | mState.mElementArrayBuffer.set(nullptr); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 72 | const VertexAttribute &VertexArray::getVertexAttribute(size_t attributeIndex) const |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 73 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 74 | ASSERT(attributeIndex < getMaxAttribs()); |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 75 | return mState.mVertexAttributes[attributeIndex]; |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 78 | void VertexArray::setVertexAttribDivisor(size_t index, GLuint divisor) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 79 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 80 | ASSERT(index < getMaxAttribs()); |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 81 | mState.mVertexAttributes[index].divisor = divisor; |
Jamie Madill | 0b9e903 | 2015-08-17 11:51:52 +0000 | [diff] [blame] | 82 | mDirtyBits.set(DIRTY_BIT_ATTRIB_0_DIVISOR + index); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 85 | void VertexArray::enableAttribute(size_t attributeIndex, bool enabledState) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 86 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 87 | ASSERT(attributeIndex < getMaxAttribs()); |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 88 | mState.mVertexAttributes[attributeIndex].enabled = enabledState; |
Jamie Madill | 0b9e903 | 2015-08-17 11:51:52 +0000 | [diff] [blame] | 89 | mDirtyBits.set(DIRTY_BIT_ATTRIB_0_ENABLED + attributeIndex); |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame] | 90 | |
| 91 | // Update state cache |
| 92 | if (enabledState) |
| 93 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 94 | mState.mMaxEnabledAttribute = std::max(attributeIndex + 1, mState.mMaxEnabledAttribute); |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame] | 95 | } |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 96 | else if (mState.mMaxEnabledAttribute == attributeIndex + 1) |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame] | 97 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 98 | while (mState.mMaxEnabledAttribute > 0 && |
| 99 | !mState.mVertexAttributes[mState.mMaxEnabledAttribute - 1].enabled) |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame] | 100 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 101 | --mState.mMaxEnabledAttribute; |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame] | 102 | } |
| 103 | } |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 104 | } |
| 105 | |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 106 | void VertexArray::setAttributeState(size_t attributeIndex, gl::Buffer *boundBuffer, GLint size, GLenum type, |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 107 | bool normalized, bool pureInteger, GLsizei stride, const void *pointer) |
| 108 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 109 | ASSERT(attributeIndex < getMaxAttribs()); |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 110 | |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 111 | VertexAttribute *attrib = &mState.mVertexAttributes[attributeIndex]; |
Jamie Madill | 8e34494 | 2015-07-09 14:22:07 -0400 | [diff] [blame] | 112 | |
| 113 | attrib->buffer.set(boundBuffer); |
| 114 | attrib->size = size; |
| 115 | attrib->type = type; |
| 116 | attrib->normalized = normalized; |
| 117 | attrib->pureInteger = pureInteger; |
| 118 | attrib->stride = stride; |
| 119 | attrib->pointer = pointer; |
Jamie Madill | 0b9e903 | 2015-08-17 11:51:52 +0000 | [diff] [blame] | 120 | mDirtyBits.set(DIRTY_BIT_ATTRIB_0_POINTER + attributeIndex); |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void VertexArray::setElementArrayBuffer(Buffer *buffer) |
| 124 | { |
Jamie Madill | 3f57268 | 2016-04-26 13:41:36 -0400 | [diff] [blame^] | 125 | mState.mElementArrayBuffer.set(buffer); |
Jamie Madill | 0b9e903 | 2015-08-17 11:51:52 +0000 | [diff] [blame] | 126 | mDirtyBits.set(DIRTY_BIT_ELEMENT_ARRAY_BUFFER); |
| 127 | } |
| 128 | |
| 129 | void VertexArray::syncImplState() |
| 130 | { |
| 131 | if (mDirtyBits.any()) |
| 132 | { |
| 133 | mVertexArray->syncState(mDirtyBits); |
| 134 | mDirtyBits.reset(); |
| 135 | } |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Jamie Madill | dff5633 | 2015-01-05 16:17:00 -0500 | [diff] [blame] | 138 | } |