blob: a920a779b8c7652b110fc4dd0528b3fc18f2244a [file] [log] [blame]
Jamie Madill57a89722013-07-02 11:57:03 -04001//
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 Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/VertexArray.h"
10#include "libANGLE/Buffer.h"
11#include "libANGLE/renderer/VertexArrayImpl.h"
Jamie Madill57a89722013-07-02 11:57:03 -040012
13namespace gl
14{
15
Brandon Jones5bf98292014-06-06 17:19:38 -070016VertexArray::VertexArray(rx::VertexArrayImpl *impl, GLuint id, size_t maxAttribs)
Shannon Woodsaa2ab7d2014-06-24 17:51:51 -040017 : mId(id),
Brandon Jones5bf98292014-06-06 17:19:38 -070018 mVertexArray(impl),
Jamie Madillaebf9dd2015-04-28 12:39:07 -040019 mVertexAttributes(maxAttribs),
20 mMaxEnabledAttribute(0)
Jamie Madill57a89722013-07-02 11:57:03 -040021{
Brandon Jonesd38f9262014-06-18 16:26:45 -070022 ASSERT(impl != NULL);
Jamie Madill004a6f92013-07-10 15:13:38 -040023}
24
25VertexArray::~VertexArray()
26{
Brandon Jonesd38f9262014-06-18 16:26:45 -070027 SafeDelete(mVertexArray);
28
Brandon Jones5bf98292014-06-06 17:19:38 -070029 for (size_t i = 0; i < getMaxAttribs(); i++)
Jamie Madill57a89722013-07-02 11:57:03 -040030 {
Brandon Jones5bf98292014-06-06 17:19:38 -070031 mVertexAttributes[i].buffer.set(NULL);
Jamie Madill57a89722013-07-02 11:57:03 -040032 }
33 mElementArrayBuffer.set(NULL);
34}
35
Shannon Woodsaa2ab7d2014-06-24 17:51:51 -040036GLuint VertexArray::id() const
37{
38 return mId;
39}
40
Jamie Madill57a89722013-07-02 11:57:03 -040041void VertexArray::detachBuffer(GLuint bufferName)
42{
Brandon Jones5bf98292014-06-06 17:19:38 -070043 for (size_t attribute = 0; attribute < getMaxAttribs(); attribute++)
Jamie Madill57a89722013-07-02 11:57:03 -040044 {
Brandon Jones5bf98292014-06-06 17:19:38 -070045 if (mVertexAttributes[attribute].buffer.id() == bufferName)
Jamie Madill57a89722013-07-02 11:57:03 -040046 {
Brandon Jones5bf98292014-06-06 17:19:38 -070047 mVertexAttributes[attribute].buffer.set(NULL);
Jamie Madill57a89722013-07-02 11:57:03 -040048 }
49 }
50
51 if (mElementArrayBuffer.id() == bufferName)
52 {
53 mElementArrayBuffer.set(NULL);
54 }
55}
56
Brandon Jones5bf98292014-06-06 17:19:38 -070057const VertexAttribute& VertexArray::getVertexAttribute(size_t attributeIndex) const
Jamie Madill57a89722013-07-02 11:57:03 -040058{
Brandon Jones5bf98292014-06-06 17:19:38 -070059 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill57a89722013-07-02 11:57:03 -040060 return mVertexAttributes[attributeIndex];
61}
62
Geoff Lang5ead9272015-03-25 12:27:43 -040063const std::vector<VertexAttribute> &VertexArray::getVertexAttributes() const
64{
65 return mVertexAttributes;
66}
67
Jamie Madill57a89722013-07-02 11:57:03 -040068void VertexArray::setVertexAttribDivisor(GLuint index, GLuint divisor)
69{
Brandon Jones5bf98292014-06-06 17:19:38 -070070 ASSERT(index < getMaxAttribs());
71 mVertexAttributes[index].divisor = divisor;
72 mVertexArray->setAttributeDivisor(index, divisor);
Jamie Madill57a89722013-07-02 11:57:03 -040073}
74
75void VertexArray::enableAttribute(unsigned int attributeIndex, bool enabledState)
76{
Brandon Jones5bf98292014-06-06 17:19:38 -070077 ASSERT(attributeIndex < getMaxAttribs());
78 mVertexAttributes[attributeIndex].enabled = enabledState;
79 mVertexArray->enableAttribute(attributeIndex, enabledState);
Jamie Madillaebf9dd2015-04-28 12:39:07 -040080
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 Madill57a89722013-07-02 11:57:03 -040093}
94
95void VertexArray::setAttributeState(unsigned int attributeIndex, gl::Buffer *boundBuffer, GLint size, GLenum type,
96 bool normalized, bool pureInteger, GLsizei stride, const void *pointer)
97{
Brandon Jones5bf98292014-06-06 17:19:38 -070098 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
109void VertexArray::setElementArrayBuffer(Buffer *buffer)
110{
111 mElementArrayBuffer.set(buffer);
112 mVertexArray->setElementArrayBuffer(buffer);
Jamie Madill57a89722013-07-02 11:57:03 -0400113}
114
Jamie Madilldff56332015-01-05 16:17:00 -0500115}