blob: 8f56f55bd9925be9f285dafc10ff0eb4e80ce0a7 [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 Madill3f572682016-04-26 13:41:36 -040017VertexArrayState::VertexArrayState(size_t maxAttribs)
Geoff Lang70d0f492015-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
Jamie Madill3f572682016-04-26 13:41:36 -040022VertexArrayState::~VertexArrayState()
Jamie Madill8e344942015-07-09 14:22:07 -040023{
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)
Jamie Madill3f572682016-04-26 13:41:36 -040032 : mId(id), mState(maxAttribs), mVertexArray(factory->createVertexArray(mState))
Jamie Madill8e344942015-07-09 14:22:07 -040033{
Jamie Madill004a6f92013-07-10 15:13:38 -040034}
35
36VertexArray::~VertexArray()
37{
Brandon Jonesd38f9262014-06-18 16:26:45 -070038 SafeDelete(mVertexArray);
Jamie Madill57a89722013-07-02 11:57:03 -040039}
40
Shannon Woodsaa2ab7d2014-06-24 17:51:51 -040041GLuint VertexArray::id() const
42{
43 return mId;
44}
45
Geoff Lang70d0f492015-12-10 17:45:46 -050046void VertexArray::setLabel(const std::string &label)
47{
Jamie Madill3f572682016-04-26 13:41:36 -040048 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -050049}
50
51const std::string &VertexArray::getLabel() const
52{
Jamie Madill3f572682016-04-26 13:41:36 -040053 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -050054}
55
Jamie Madill57a89722013-07-02 11:57:03 -040056void VertexArray::detachBuffer(GLuint bufferName)
57{
Brandon Jones5bf98292014-06-06 17:19:38 -070058 for (size_t attribute = 0; attribute < getMaxAttribs(); attribute++)
Jamie Madill57a89722013-07-02 11:57:03 -040059 {
Jamie Madill3f572682016-04-26 13:41:36 -040060 if (mState.mVertexAttributes[attribute].buffer.id() == bufferName)
Jamie Madill57a89722013-07-02 11:57:03 -040061 {
Jamie Madill3f572682016-04-26 13:41:36 -040062 mState.mVertexAttributes[attribute].buffer.set(nullptr);
Jamie Madill57a89722013-07-02 11:57:03 -040063 }
64 }
65
Jamie Madill3f572682016-04-26 13:41:36 -040066 if (mState.mElementArrayBuffer.id() == bufferName)
Jamie Madill57a89722013-07-02 11:57:03 -040067 {
Jamie Madill3f572682016-04-26 13:41:36 -040068 mState.mElementArrayBuffer.set(nullptr);
Jamie Madill57a89722013-07-02 11:57:03 -040069 }
70}
71
Jamie Madill8e344942015-07-09 14:22:07 -040072const VertexAttribute &VertexArray::getVertexAttribute(size_t attributeIndex) const
Jamie Madill57a89722013-07-02 11:57:03 -040073{
Brandon Jones5bf98292014-06-06 17:19:38 -070074 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill3f572682016-04-26 13:41:36 -040075 return mState.mVertexAttributes[attributeIndex];
Jamie Madill57a89722013-07-02 11:57:03 -040076}
77
Jamie Madill8e344942015-07-09 14:22:07 -040078void VertexArray::setVertexAttribDivisor(size_t index, GLuint divisor)
Jamie Madill57a89722013-07-02 11:57:03 -040079{
Brandon Jones5bf98292014-06-06 17:19:38 -070080 ASSERT(index < getMaxAttribs());
Jamie Madill3f572682016-04-26 13:41:36 -040081 mState.mVertexAttributes[index].divisor = divisor;
Jamie Madill0b9e9032015-08-17 11:51:52 +000082 mDirtyBits.set(DIRTY_BIT_ATTRIB_0_DIVISOR + index);
Jamie Madill57a89722013-07-02 11:57:03 -040083}
84
Jamie Madill8e344942015-07-09 14:22:07 -040085void VertexArray::enableAttribute(size_t attributeIndex, bool enabledState)
Jamie Madill57a89722013-07-02 11:57:03 -040086{
Brandon Jones5bf98292014-06-06 17:19:38 -070087 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill3f572682016-04-26 13:41:36 -040088 mState.mVertexAttributes[attributeIndex].enabled = enabledState;
Jamie Madill0b9e9032015-08-17 11:51:52 +000089 mDirtyBits.set(DIRTY_BIT_ATTRIB_0_ENABLED + attributeIndex);
Jamie Madillaebf9dd2015-04-28 12:39:07 -040090
91 // Update state cache
92 if (enabledState)
93 {
Jamie Madill3f572682016-04-26 13:41:36 -040094 mState.mMaxEnabledAttribute = std::max(attributeIndex + 1, mState.mMaxEnabledAttribute);
Jamie Madillaebf9dd2015-04-28 12:39:07 -040095 }
Jamie Madill3f572682016-04-26 13:41:36 -040096 else if (mState.mMaxEnabledAttribute == attributeIndex + 1)
Jamie Madillaebf9dd2015-04-28 12:39:07 -040097 {
Jamie Madill3f572682016-04-26 13:41:36 -040098 while (mState.mMaxEnabledAttribute > 0 &&
99 !mState.mVertexAttributes[mState.mMaxEnabledAttribute - 1].enabled)
Jamie Madillaebf9dd2015-04-28 12:39:07 -0400100 {
Jamie Madill3f572682016-04-26 13:41:36 -0400101 --mState.mMaxEnabledAttribute;
Jamie Madillaebf9dd2015-04-28 12:39:07 -0400102 }
103 }
Jamie Madill57a89722013-07-02 11:57:03 -0400104}
105
Jamie Madill8e344942015-07-09 14:22:07 -0400106void VertexArray::setAttributeState(size_t attributeIndex, gl::Buffer *boundBuffer, GLint size, GLenum type,
Jamie Madill57a89722013-07-02 11:57:03 -0400107 bool normalized, bool pureInteger, GLsizei stride, const void *pointer)
108{
Brandon Jones5bf98292014-06-06 17:19:38 -0700109 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -0400110
Jamie Madill3f572682016-04-26 13:41:36 -0400111 VertexAttribute *attrib = &mState.mVertexAttributes[attributeIndex];
Jamie Madill8e344942015-07-09 14:22:07 -0400112
113 attrib->buffer.set(boundBuffer);
114 attrib->size = size;
115 attrib->type = type;
116 attrib->normalized = normalized;
117 attrib->pureInteger = pureInteger;
118 attrib->stride = stride;
119 attrib->pointer = pointer;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000120 mDirtyBits.set(DIRTY_BIT_ATTRIB_0_POINTER + attributeIndex);
Brandon Jones5bf98292014-06-06 17:19:38 -0700121}
122
123void VertexArray::setElementArrayBuffer(Buffer *buffer)
124{
Jamie Madill3f572682016-04-26 13:41:36 -0400125 mState.mElementArrayBuffer.set(buffer);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000126 mDirtyBits.set(DIRTY_BIT_ELEMENT_ARRAY_BUFFER);
127}
128
129void VertexArray::syncImplState()
130{
131 if (mDirtyBits.any())
132 {
133 mVertexArray->syncState(mDirtyBits);
134 mDirtyBits.reset();
135 }
Jamie Madill57a89722013-07-02 11:57:03 -0400136}
137
Jamie Madilldff56332015-01-05 16:17:00 -0500138}