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