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" |
| 11 | #include "libANGLE/renderer/VertexArrayImpl.h" |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 12 | |
| 13 | namespace gl |
| 14 | { |
| 15 | |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 16 | VertexArray::VertexArray(rx::VertexArrayImpl *impl, GLuint id, size_t maxAttribs) |
Shannon Woods | aa2ab7d | 2014-06-24 17:51:51 -0400 | [diff] [blame] | 17 | : mId(id), |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 18 | mVertexArray(impl), |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame^] | 19 | mVertexAttributes(maxAttribs), |
| 20 | mMaxEnabledAttribute(0) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 21 | { |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame] | 22 | ASSERT(impl != NULL); |
Jamie Madill | 004a6f9 | 2013-07-10 15:13:38 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | VertexArray::~VertexArray() |
| 26 | { |
Brandon Jones | d38f926 | 2014-06-18 16:26:45 -0700 | [diff] [blame] | 27 | SafeDelete(mVertexArray); |
| 28 | |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 29 | for (size_t i = 0; i < getMaxAttribs(); i++) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 30 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 31 | mVertexAttributes[i].buffer.set(NULL); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 32 | } |
| 33 | mElementArrayBuffer.set(NULL); |
| 34 | } |
| 35 | |
Shannon Woods | aa2ab7d | 2014-06-24 17:51:51 -0400 | [diff] [blame] | 36 | GLuint VertexArray::id() const |
| 37 | { |
| 38 | return mId; |
| 39 | } |
| 40 | |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 41 | void VertexArray::detachBuffer(GLuint bufferName) |
| 42 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 43 | for (size_t attribute = 0; attribute < getMaxAttribs(); attribute++) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 44 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 45 | if (mVertexAttributes[attribute].buffer.id() == bufferName) |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 46 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 47 | mVertexAttributes[attribute].buffer.set(NULL); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | if (mElementArrayBuffer.id() == bufferName) |
| 52 | { |
| 53 | mElementArrayBuffer.set(NULL); |
| 54 | } |
| 55 | } |
| 56 | |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 57 | const VertexAttribute& VertexArray::getVertexAttribute(size_t attributeIndex) const |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 58 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 59 | ASSERT(attributeIndex < getMaxAttribs()); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 60 | return mVertexAttributes[attributeIndex]; |
| 61 | } |
| 62 | |
Geoff Lang | 5ead927 | 2015-03-25 12:27:43 -0400 | [diff] [blame] | 63 | const std::vector<VertexAttribute> &VertexArray::getVertexAttributes() const |
| 64 | { |
| 65 | return mVertexAttributes; |
| 66 | } |
| 67 | |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 68 | void VertexArray::setVertexAttribDivisor(GLuint index, GLuint divisor) |
| 69 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 70 | ASSERT(index < getMaxAttribs()); |
| 71 | mVertexAttributes[index].divisor = divisor; |
| 72 | mVertexArray->setAttributeDivisor(index, divisor); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void VertexArray::enableAttribute(unsigned int attributeIndex, bool enabledState) |
| 76 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 77 | ASSERT(attributeIndex < getMaxAttribs()); |
| 78 | mVertexAttributes[attributeIndex].enabled = enabledState; |
| 79 | mVertexArray->enableAttribute(attributeIndex, enabledState); |
Jamie Madill | aebf9dd | 2015-04-28 12:39:07 -0400 | [diff] [blame^] | 80 | |
| 81 | // Update state cache |
| 82 | if (enabledState) |
| 83 | { |
| 84 | mMaxEnabledAttribute = std::max(attributeIndex, mMaxEnabledAttribute); |
| 85 | } |
| 86 | else if (mMaxEnabledAttribute == attributeIndex) |
| 87 | { |
| 88 | while (mMaxEnabledAttribute > 0 && !mVertexAttributes[mMaxEnabledAttribute].enabled) |
| 89 | { |
| 90 | --mMaxEnabledAttribute; |
| 91 | } |
| 92 | } |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void VertexArray::setAttributeState(unsigned int attributeIndex, gl::Buffer *boundBuffer, GLint size, GLenum type, |
| 96 | bool normalized, bool pureInteger, GLsizei stride, const void *pointer) |
| 97 | { |
Brandon Jones | 5bf9829 | 2014-06-06 17:19:38 -0700 | [diff] [blame] | 98 | ASSERT(attributeIndex < getMaxAttribs()); |
| 99 | mVertexAttributes[attributeIndex].buffer.set(boundBuffer); |
| 100 | mVertexAttributes[attributeIndex].size = size; |
| 101 | mVertexAttributes[attributeIndex].type = type; |
| 102 | mVertexAttributes[attributeIndex].normalized = normalized; |
| 103 | mVertexAttributes[attributeIndex].pureInteger = pureInteger; |
| 104 | mVertexAttributes[attributeIndex].stride = stride; |
| 105 | mVertexAttributes[attributeIndex].pointer = pointer; |
| 106 | mVertexArray->setAttribute(attributeIndex, mVertexAttributes[attributeIndex]); |
| 107 | } |
| 108 | |
| 109 | void VertexArray::setElementArrayBuffer(Buffer *buffer) |
| 110 | { |
| 111 | mElementArrayBuffer.set(buffer); |
| 112 | mVertexArray->setElementArrayBuffer(buffer); |
Jamie Madill | 57a8972 | 2013-07-02 11:57:03 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Jamie Madill | dff5633 | 2015-01-05 16:17:00 -0500 | [diff] [blame] | 115 | } |