blob: afa433247cb0e2f93d67440266838267b4725d03 [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 Lang66988742015-12-22 19:39:19 +000018 : mVertexAttributes(maxAttribs),
19 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{
Jamie Madill004a6f92013-07-10 15:13:38 -040037}
38
39VertexArray::~VertexArray()
40{
Brandon Jonesd38f9262014-06-18 16:26:45 -070041 SafeDelete(mVertexArray);
Jamie Madill57a89722013-07-02 11:57:03 -040042}
43
Shannon Woodsaa2ab7d2014-06-24 17:51:51 -040044GLuint VertexArray::id() const
45{
46 return mId;
47}
48
Jamie Madill57a89722013-07-02 11:57:03 -040049void VertexArray::detachBuffer(GLuint bufferName)
50{
Brandon Jones5bf98292014-06-06 17:19:38 -070051 for (size_t attribute = 0; attribute < getMaxAttribs(); attribute++)
Jamie Madill57a89722013-07-02 11:57:03 -040052 {
Jamie Madill8e344942015-07-09 14:22:07 -040053 if (mData.mVertexAttributes[attribute].buffer.id() == bufferName)
Jamie Madill57a89722013-07-02 11:57:03 -040054 {
Jamie Madill8e344942015-07-09 14:22:07 -040055 mData.mVertexAttributes[attribute].buffer.set(nullptr);
Jamie Madill57a89722013-07-02 11:57:03 -040056 }
57 }
58
Jamie Madill8e344942015-07-09 14:22:07 -040059 if (mData.mElementArrayBuffer.id() == bufferName)
Jamie Madill57a89722013-07-02 11:57:03 -040060 {
Jamie Madill8e344942015-07-09 14:22:07 -040061 mData.mElementArrayBuffer.set(nullptr);
Jamie Madill57a89722013-07-02 11:57:03 -040062 }
63}
64
Jamie Madill8e344942015-07-09 14:22:07 -040065const VertexAttribute &VertexArray::getVertexAttribute(size_t attributeIndex) const
Jamie Madill57a89722013-07-02 11:57:03 -040066{
Brandon Jones5bf98292014-06-06 17:19:38 -070067 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -040068 return mData.mVertexAttributes[attributeIndex];
Jamie Madill57a89722013-07-02 11:57:03 -040069}
70
Jamie Madill8e344942015-07-09 14:22:07 -040071void VertexArray::setVertexAttribDivisor(size_t index, GLuint divisor)
Jamie Madill57a89722013-07-02 11:57:03 -040072{
Brandon Jones5bf98292014-06-06 17:19:38 -070073 ASSERT(index < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -040074 mData.mVertexAttributes[index].divisor = divisor;
Jamie Madill0b9e9032015-08-17 11:51:52 +000075 mDirtyBits.set(DIRTY_BIT_ATTRIB_0_DIVISOR + index);
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 Madill0b9e9032015-08-17 11:51:52 +000082 mDirtyBits.set(DIRTY_BIT_ATTRIB_0_ENABLED + attributeIndex);
Jamie Madillaebf9dd2015-04-28 12:39:07 -040083
84 // Update state cache
85 if (enabledState)
86 {
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040087 mData.mMaxEnabledAttribute = std::max(attributeIndex + 1, mData.mMaxEnabledAttribute);
Jamie Madillaebf9dd2015-04-28 12:39:07 -040088 }
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040089 else if (mData.mMaxEnabledAttribute == attributeIndex + 1)
Jamie Madillaebf9dd2015-04-28 12:39:07 -040090 {
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040091 while (mData.mMaxEnabledAttribute > 0 &&
92 !mData.mVertexAttributes[mData.mMaxEnabledAttribute - 1].enabled)
Jamie Madillaebf9dd2015-04-28 12:39:07 -040093 {
Jamie Madill8e344942015-07-09 14:22:07 -040094 --mData.mMaxEnabledAttribute;
Jamie Madillaebf9dd2015-04-28 12:39:07 -040095 }
96 }
Jamie Madill57a89722013-07-02 11:57:03 -040097}
98
Jamie Madill8e344942015-07-09 14:22:07 -040099void VertexArray::setAttributeState(size_t attributeIndex, gl::Buffer *boundBuffer, GLint size, GLenum type,
Jamie Madill57a89722013-07-02 11:57:03 -0400100 bool normalized, bool pureInteger, GLsizei stride, const void *pointer)
101{
Brandon Jones5bf98292014-06-06 17:19:38 -0700102 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -0400103
104 VertexAttribute *attrib = &mData.mVertexAttributes[attributeIndex];
105
106 attrib->buffer.set(boundBuffer);
107 attrib->size = size;
108 attrib->type = type;
109 attrib->normalized = normalized;
110 attrib->pureInteger = pureInteger;
111 attrib->stride = stride;
112 attrib->pointer = pointer;
Jamie Madill0b9e9032015-08-17 11:51:52 +0000113 mDirtyBits.set(DIRTY_BIT_ATTRIB_0_POINTER + attributeIndex);
Brandon Jones5bf98292014-06-06 17:19:38 -0700114}
115
116void VertexArray::setElementArrayBuffer(Buffer *buffer)
117{
Jamie Madill8e344942015-07-09 14:22:07 -0400118 mData.mElementArrayBuffer.set(buffer);
Jamie Madill0b9e9032015-08-17 11:51:52 +0000119 mDirtyBits.set(DIRTY_BIT_ELEMENT_ARRAY_BUFFER);
120}
121
122void VertexArray::syncImplState()
123{
124 if (mDirtyBits.any())
125 {
126 mVertexArray->syncState(mDirtyBits);
127 mDirtyBits.reset();
128 }
Jamie Madill57a89722013-07-02 11:57:03 -0400129}
130
Jamie Madilldff56332015-01-05 16:17:00 -0500131}