blob: 8d51e9b469a7241ff261125c63a5e3ebbd073321 [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)
Geoff Lang6c521b72015-12-10 17:45:46 -050018 : mLabel(), mVertexAttributes(maxAttribs), mMaxEnabledAttribute(0)
Jamie Madill57a89722013-07-02 11:57:03 -040019{
Jamie Madill8e344942015-07-09 14:22:07 -040020}
21
22VertexArray::Data::~Data()
23{
24 for (size_t i = 0; i < getMaxAttribs(); i++)
25 {
26 mVertexAttributes[i].buffer.set(nullptr);
27 }
28 mElementArrayBuffer.set(nullptr);
29}
30
31VertexArray::VertexArray(rx::ImplFactory *factory, GLuint id, size_t maxAttribs)
32 : mId(id),
33 mVertexArray(factory->createVertexArray(mData)),
34 mData(maxAttribs)
35{
Jamie Madill004a6f92013-07-10 15:13:38 -040036}
37
38VertexArray::~VertexArray()
39{
Brandon Jonesd38f9262014-06-18 16:26:45 -070040 SafeDelete(mVertexArray);
Jamie Madill57a89722013-07-02 11:57:03 -040041}
42
Shannon Woodsaa2ab7d2014-06-24 17:51:51 -040043GLuint VertexArray::id() const
44{
45 return mId;
46}
47
Geoff Lang6c521b72015-12-10 17:45:46 -050048void VertexArray::setLabel(const std::string &label)
49{
50 mData.mLabel = label;
51}
52
53const std::string &VertexArray::getLabel() const
54{
55 return mData.mLabel;
56}
57
Jamie Madill57a89722013-07-02 11:57:03 -040058void VertexArray::detachBuffer(GLuint bufferName)
59{
Brandon Jones5bf98292014-06-06 17:19:38 -070060 for (size_t attribute = 0; attribute < getMaxAttribs(); attribute++)
Jamie Madill57a89722013-07-02 11:57:03 -040061 {
Jamie Madill8e344942015-07-09 14:22:07 -040062 if (mData.mVertexAttributes[attribute].buffer.id() == bufferName)
Jamie Madill57a89722013-07-02 11:57:03 -040063 {
Jamie Madill8e344942015-07-09 14:22:07 -040064 mData.mVertexAttributes[attribute].buffer.set(nullptr);
Jamie Madill57a89722013-07-02 11:57:03 -040065 }
66 }
67
Jamie Madill8e344942015-07-09 14:22:07 -040068 if (mData.mElementArrayBuffer.id() == bufferName)
Jamie Madill57a89722013-07-02 11:57:03 -040069 {
Jamie Madill8e344942015-07-09 14:22:07 -040070 mData.mElementArrayBuffer.set(nullptr);
Jamie Madill57a89722013-07-02 11:57:03 -040071 }
72}
73
Jamie Madill8e344942015-07-09 14:22:07 -040074const VertexAttribute &VertexArray::getVertexAttribute(size_t attributeIndex) const
Jamie Madill57a89722013-07-02 11:57:03 -040075{
Brandon Jones5bf98292014-06-06 17:19:38 -070076 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -040077 return mData.mVertexAttributes[attributeIndex];
Jamie Madill57a89722013-07-02 11:57:03 -040078}
79
Jamie Madill8e344942015-07-09 14:22:07 -040080void VertexArray::setVertexAttribDivisor(size_t index, GLuint divisor)
Jamie Madill57a89722013-07-02 11:57:03 -040081{
Brandon Jones5bf98292014-06-06 17:19:38 -070082 ASSERT(index < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -040083 mData.mVertexAttributes[index].divisor = divisor;
Jamie Madill0b9e9032015-08-17 11:51:52 +000084 mDirtyBits.set(DIRTY_BIT_ATTRIB_0_DIVISOR + index);
Jamie Madill57a89722013-07-02 11:57:03 -040085}
86
Jamie Madill8e344942015-07-09 14:22:07 -040087void VertexArray::enableAttribute(size_t attributeIndex, bool enabledState)
Jamie Madill57a89722013-07-02 11:57:03 -040088{
Brandon Jones5bf98292014-06-06 17:19:38 -070089 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -040090 mData.mVertexAttributes[attributeIndex].enabled = enabledState;
Jamie Madill0b9e9032015-08-17 11:51:52 +000091 mDirtyBits.set(DIRTY_BIT_ATTRIB_0_ENABLED + attributeIndex);
Jamie Madillaebf9dd2015-04-28 12:39:07 -040092
93 // Update state cache
94 if (enabledState)
95 {
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040096 mData.mMaxEnabledAttribute = std::max(attributeIndex + 1, mData.mMaxEnabledAttribute);
Jamie Madillaebf9dd2015-04-28 12:39:07 -040097 }
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040098 else if (mData.mMaxEnabledAttribute == attributeIndex + 1)
Jamie Madillaebf9dd2015-04-28 12:39:07 -040099 {
Jamie Madillcc7bbaf2015-07-21 15:14:10 -0400100 while (mData.mMaxEnabledAttribute > 0 &&
101 !mData.mVertexAttributes[mData.mMaxEnabledAttribute - 1].enabled)
Jamie Madillaebf9dd2015-04-28 12:39:07 -0400102 {
Jamie Madill8e344942015-07-09 14:22:07 -0400103 --mData.mMaxEnabledAttribute;
Jamie Madillaebf9dd2015-04-28 12:39:07 -0400104 }
105 }
Jamie Madill57a89722013-07-02 11:57:03 -0400106}
107
Jamie Madill8e344942015-07-09 14:22:07 -0400108void VertexArray::setAttributeState(size_t attributeIndex, gl::Buffer *boundBuffer, GLint size, GLenum type,
Jamie Madill57a89722013-07-02 11:57:03 -0400109 bool normalized, bool pureInteger, GLsizei stride, const void *pointer)
110{
Brandon Jones5bf98292014-06-06 17:19:38 -0700111 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -0400112
113 VertexAttribute *attrib = &mData.mVertexAttributes[attributeIndex];
114
115 attrib->buffer.set(boundBuffer);
116 attrib->size = size;
117 attrib->type = type;
118 attrib->normalized = normalized;
119 attrib->pureInteger = pureInteger;
120 attrib->stride = stride;
121 attrib->pointer = pointer;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000122 mDirtyBits.set(DIRTY_BIT_ATTRIB_0_POINTER + attributeIndex);
Brandon Jones5bf98292014-06-06 17:19:38 -0700123}
124
125void VertexArray::setElementArrayBuffer(Buffer *buffer)
126{
Jamie Madill8e344942015-07-09 14:22:07 -0400127 mData.mElementArrayBuffer.set(buffer);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000128 mDirtyBits.set(DIRTY_BIT_ELEMENT_ARRAY_BUFFER);
129}
130
131void VertexArray::syncImplState()
132{
133 if (mDirtyBits.any())
134 {
135 mVertexArray->syncState(mDirtyBits);
136 mDirtyBits.reset();
137 }
Jamie Madill57a89722013-07-02 11:57:03 -0400138}
139
Jamie Madilldff56332015-01-05 16:17:00 -0500140}