blob: 3489a319748eee2aced343e7728c1c2487776ed7 [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"
Jamie Madill8e344942015-07-09 14:22:07 -040011#include "libANGLE/renderer/ImplFactory.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050012#include "libANGLE/renderer/VertexArrayImpl.h"
Jamie Madill57a89722013-07-02 11:57:03 -040013
14namespace gl
15{
16
Jamie Madill8e344942015-07-09 14:22:07 -040017VertexArray::Data::Data(size_t maxAttribs)
18 : mVertexAttributes(maxAttribs),
Jamie Madillaebf9dd2015-04-28 12:39:07 -040019 mMaxEnabledAttribute(0)
Jamie Madill57a89722013-07-02 11:57:03 -040020{
Jamie Madill8e344942015-07-09 14:22:07 -040021}
22
23VertexArray::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
32VertexArray::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 Madill004a6f92013-07-10 15:13:38 -040038}
39
40VertexArray::~VertexArray()
41{
Brandon Jonesd38f9262014-06-18 16:26:45 -070042 SafeDelete(mVertexArray);
Jamie Madill57a89722013-07-02 11:57:03 -040043}
44
Shannon Woodsaa2ab7d2014-06-24 17:51:51 -040045GLuint VertexArray::id() const
46{
47 return mId;
48}
49
Jamie Madill57a89722013-07-02 11:57:03 -040050void VertexArray::detachBuffer(GLuint bufferName)
51{
Brandon Jones5bf98292014-06-06 17:19:38 -070052 for (size_t attribute = 0; attribute < getMaxAttribs(); attribute++)
Jamie Madill57a89722013-07-02 11:57:03 -040053 {
Jamie Madill8e344942015-07-09 14:22:07 -040054 if (mData.mVertexAttributes[attribute].buffer.id() == bufferName)
Jamie Madill57a89722013-07-02 11:57:03 -040055 {
Jamie Madill8e344942015-07-09 14:22:07 -040056 mData.mVertexAttributes[attribute].buffer.set(nullptr);
Jamie Madill57a89722013-07-02 11:57:03 -040057 }
58 }
59
Jamie Madill8e344942015-07-09 14:22:07 -040060 if (mData.mElementArrayBuffer.id() == bufferName)
Jamie Madill57a89722013-07-02 11:57:03 -040061 {
Jamie Madill8e344942015-07-09 14:22:07 -040062 mData.mElementArrayBuffer.set(nullptr);
Jamie Madill57a89722013-07-02 11:57:03 -040063 }
64}
65
Jamie Madill8e344942015-07-09 14:22:07 -040066const VertexAttribute &VertexArray::getVertexAttribute(size_t attributeIndex) const
Jamie Madill57a89722013-07-02 11:57:03 -040067{
Brandon Jones5bf98292014-06-06 17:19:38 -070068 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -040069 return mData.mVertexAttributes[attributeIndex];
Jamie Madill57a89722013-07-02 11:57:03 -040070}
71
Jamie Madill8e344942015-07-09 14:22:07 -040072void VertexArray::setVertexAttribDivisor(size_t index, GLuint divisor)
Jamie Madill57a89722013-07-02 11:57:03 -040073{
Brandon Jones5bf98292014-06-06 17:19:38 -070074 ASSERT(index < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -040075 mData.mVertexAttributes[index].divisor = divisor;
Jamie Madill57a89722013-07-02 11:57:03 -040076}
77
Jamie Madill8e344942015-07-09 14:22:07 -040078void VertexArray::enableAttribute(size_t attributeIndex, bool enabledState)
Jamie Madill57a89722013-07-02 11:57:03 -040079{
Brandon Jones5bf98292014-06-06 17:19:38 -070080 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -040081 mData.mVertexAttributes[attributeIndex].enabled = enabledState;
Jamie Madillaebf9dd2015-04-28 12:39:07 -040082
83 // Update state cache
84 if (enabledState)
85 {
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040086 mData.mMaxEnabledAttribute = std::max(attributeIndex + 1, mData.mMaxEnabledAttribute);
Jamie Madillaebf9dd2015-04-28 12:39:07 -040087 }
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040088 else if (mData.mMaxEnabledAttribute == attributeIndex + 1)
Jamie Madillaebf9dd2015-04-28 12:39:07 -040089 {
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040090 while (mData.mMaxEnabledAttribute > 0 &&
91 !mData.mVertexAttributes[mData.mMaxEnabledAttribute - 1].enabled)
Jamie Madillaebf9dd2015-04-28 12:39:07 -040092 {
Jamie Madill8e344942015-07-09 14:22:07 -040093 --mData.mMaxEnabledAttribute;
Jamie Madillaebf9dd2015-04-28 12:39:07 -040094 }
95 }
Jamie Madill57a89722013-07-02 11:57:03 -040096}
97
Jamie Madill8e344942015-07-09 14:22:07 -040098void VertexArray::setAttributeState(size_t attributeIndex, gl::Buffer *boundBuffer, GLint size, GLenum type,
Jamie Madill57a89722013-07-02 11:57:03 -040099 bool normalized, bool pureInteger, GLsizei stride, const void *pointer)
100{
Brandon Jones5bf98292014-06-06 17:19:38 -0700101 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -0400102
103 VertexAttribute *attrib = &mData.mVertexAttributes[attributeIndex];
104
105 attrib->buffer.set(boundBuffer);
106 attrib->size = size;
107 attrib->type = type;
108 attrib->normalized = normalized;
109 attrib->pureInteger = pureInteger;
110 attrib->stride = stride;
111 attrib->pointer = pointer;
Brandon Jones5bf98292014-06-06 17:19:38 -0700112}
113
114void VertexArray::setElementArrayBuffer(Buffer *buffer)
115{
Jamie Madill8e344942015-07-09 14:22:07 -0400116 mData.mElementArrayBuffer.set(buffer);
Jamie Madill57a89722013-07-02 11:57:03 -0400117}
118
Jamie Madilldff56332015-01-05 16:17:00 -0500119}