blob: 2992b44a165764c148abc11fba5641ae54925063 [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);
57 mVertexArray->setAttribute(attribute, mData.mVertexAttributes[attribute]);
Jamie Madill57a89722013-07-02 11:57:03 -040058 }
59 }
60
Jamie Madill8e344942015-07-09 14:22:07 -040061 if (mData.mElementArrayBuffer.id() == bufferName)
Jamie Madill57a89722013-07-02 11:57:03 -040062 {
Jamie Madill8e344942015-07-09 14:22:07 -040063 mData.mElementArrayBuffer.set(nullptr);
Geoff Langee09b582015-06-16 15:13:52 -070064 mVertexArray->setElementArrayBuffer(nullptr);
Jamie Madill57a89722013-07-02 11:57:03 -040065 }
66}
67
Jamie Madill8e344942015-07-09 14:22:07 -040068const VertexAttribute &VertexArray::getVertexAttribute(size_t attributeIndex) const
Jamie Madill57a89722013-07-02 11:57:03 -040069{
Brandon Jones5bf98292014-06-06 17:19:38 -070070 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -040071 return mData.mVertexAttributes[attributeIndex];
Jamie Madill57a89722013-07-02 11:57:03 -040072}
73
Jamie Madill8e344942015-07-09 14:22:07 -040074void VertexArray::setVertexAttribDivisor(size_t index, GLuint divisor)
Jamie Madill57a89722013-07-02 11:57:03 -040075{
Brandon Jones5bf98292014-06-06 17:19:38 -070076 ASSERT(index < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -040077 mData.mVertexAttributes[index].divisor = divisor;
Brandon Jones5bf98292014-06-06 17:19:38 -070078 mVertexArray->setAttributeDivisor(index, divisor);
Jamie Madill57a89722013-07-02 11:57:03 -040079}
80
Jamie Madill8e344942015-07-09 14:22:07 -040081void VertexArray::enableAttribute(size_t attributeIndex, bool enabledState)
Jamie Madill57a89722013-07-02 11:57:03 -040082{
Brandon Jones5bf98292014-06-06 17:19:38 -070083 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -040084 mData.mVertexAttributes[attributeIndex].enabled = enabledState;
Brandon Jones5bf98292014-06-06 17:19:38 -070085 mVertexArray->enableAttribute(attributeIndex, enabledState);
Jamie Madillaebf9dd2015-04-28 12:39:07 -040086
87 // Update state cache
88 if (enabledState)
89 {
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040090 mData.mMaxEnabledAttribute = std::max(attributeIndex + 1, mData.mMaxEnabledAttribute);
Jamie Madillaebf9dd2015-04-28 12:39:07 -040091 }
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040092 else if (mData.mMaxEnabledAttribute == attributeIndex + 1)
Jamie Madillaebf9dd2015-04-28 12:39:07 -040093 {
Jamie Madillcc7bbaf2015-07-21 15:14:10 -040094 while (mData.mMaxEnabledAttribute > 0 &&
95 !mData.mVertexAttributes[mData.mMaxEnabledAttribute - 1].enabled)
Jamie Madillaebf9dd2015-04-28 12:39:07 -040096 {
Jamie Madill8e344942015-07-09 14:22:07 -040097 --mData.mMaxEnabledAttribute;
Jamie Madillaebf9dd2015-04-28 12:39:07 -040098 }
99 }
Jamie Madill57a89722013-07-02 11:57:03 -0400100}
101
Jamie Madill8e344942015-07-09 14:22:07 -0400102void VertexArray::setAttributeState(size_t attributeIndex, gl::Buffer *boundBuffer, GLint size, GLenum type,
Jamie Madill57a89722013-07-02 11:57:03 -0400103 bool normalized, bool pureInteger, GLsizei stride, const void *pointer)
104{
Brandon Jones5bf98292014-06-06 17:19:38 -0700105 ASSERT(attributeIndex < getMaxAttribs());
Jamie Madill8e344942015-07-09 14:22:07 -0400106
107 VertexAttribute *attrib = &mData.mVertexAttributes[attributeIndex];
108
109 attrib->buffer.set(boundBuffer);
110 attrib->size = size;
111 attrib->type = type;
112 attrib->normalized = normalized;
113 attrib->pureInteger = pureInteger;
114 attrib->stride = stride;
115 attrib->pointer = pointer;
116
117 mVertexArray->setAttribute(attributeIndex, *attrib);
Brandon Jones5bf98292014-06-06 17:19:38 -0700118}
119
120void VertexArray::setElementArrayBuffer(Buffer *buffer)
121{
Jamie Madill8e344942015-07-09 14:22:07 -0400122 mData.mElementArrayBuffer.set(buffer);
Brandon Jones5bf98292014-06-06 17:19:38 -0700123 mVertexArray->setElementArrayBuffer(buffer);
Jamie Madill57a89722013-07-02 11:57:03 -0400124}
125
Jamie Madilldff56332015-01-05 16:17:00 -0500126}