blob: 4afc361963c478db9fb52cb23fa47616c698de49 [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 Madill0b9e9032015-08-17 11:51:52 +000076 mDirtyBits.set(DIRTY_BIT_ATTRIB_0_DIVISOR + index);
Jamie Madill57a89722013-07-02 11:57:03 -040077}
78
Jamie Madill8e344942015-07-09 14:22:07 -040079void VertexArray::enableAttribute(size_t attributeIndex, bool enabledState)
Jamie Madill57a89722013-07-02 11:57:03 -040080{
Brandon Jones5bf98292014-06-06 17:19:38 -070081 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -040082 mData.mVertexAttributes[attributeIndex].enabled = enabledState;
Jamie Madill0b9e9032015-08-17 11:51:52 +000083 mDirtyBits.set(DIRTY_BIT_ATTRIB_0_ENABLED + attributeIndex);
Jamie Madillaebf9dd2015-04-28 12:39:07 -040084
85 // Update state cache
86 if (enabledState)
87 {
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040088 mData.mMaxEnabledAttribute = std::max(attributeIndex + 1, mData.mMaxEnabledAttribute);
Jamie Madillaebf9dd2015-04-28 12:39:07 -040089 }
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040090 else if (mData.mMaxEnabledAttribute == attributeIndex + 1)
Jamie Madillaebf9dd2015-04-28 12:39:07 -040091 {
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040092 while (mData.mMaxEnabledAttribute > 0 &&
93 !mData.mVertexAttributes[mData.mMaxEnabledAttribute - 1].enabled)
Jamie Madillaebf9dd2015-04-28 12:39:07 -040094 {
Jamie Madill8e344942015-07-09 14:22:07 -040095 --mData.mMaxEnabledAttribute;
Jamie Madillaebf9dd2015-04-28 12:39:07 -040096 }
97 }
Jamie Madill57a89722013-07-02 11:57:03 -040098}
99
Jamie Madill8e344942015-07-09 14:22:07 -0400100void VertexArray::setAttributeState(size_t attributeIndex, gl::Buffer *boundBuffer, GLint size, GLenum type,
Jamie Madill57a89722013-07-02 11:57:03 -0400101 bool normalized, bool pureInteger, GLsizei stride, const void *pointer)
102{
Brandon Jones5bf98292014-06-06 17:19:38 -0700103 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -0400104
105 VertexAttribute *attrib = &mData.mVertexAttributes[attributeIndex];
106
107 attrib->buffer.set(boundBuffer);
108 attrib->size = size;
109 attrib->type = type;
110 attrib->normalized = normalized;
111 attrib->pureInteger = pureInteger;
112 attrib->stride = stride;
113 attrib->pointer = pointer;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000114 mDirtyBits.set(DIRTY_BIT_ATTRIB_0_POINTER + attributeIndex);
Brandon Jones5bf98292014-06-06 17:19:38 -0700115}
116
117void VertexArray::setElementArrayBuffer(Buffer *buffer)
118{
Jamie Madill8e344942015-07-09 14:22:07 -0400119 mData.mElementArrayBuffer.set(buffer);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000120 mDirtyBits.set(DIRTY_BIT_ELEMENT_ARRAY_BUFFER);
121}
122
123void VertexArray::syncImplState()
124{
125 if (mDirtyBits.any())
126 {
127 mVertexArray->syncState(mDirtyBits);
128 mDirtyBits.reset();
129 }
Jamie Madill57a89722013-07-02 11:57:03 -0400130}
131
Jamie Madilldff56332015-01-05 16:17:00 -0500132}