Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2015 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 | |
| 7 | // VertexArrayGL.cpp: Implements the class methods for VertexArrayGL. |
| 8 | |
| 9 | #include "libANGLE/renderer/gl/VertexArrayGL.h" |
| 10 | |
| 11 | #include "common/debug.h" |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame^] | 12 | #include "libANGLE/Buffer.h" |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 13 | #include "libANGLE/angletypes.h" |
| 14 | #include "libANGLE/renderer/gl/BufferGL.h" |
| 15 | #include "libANGLE/renderer/gl/FunctionsGL.h" |
| 16 | #include "libANGLE/renderer/gl/StateManagerGL.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 17 | |
| 18 | namespace rx |
| 19 | { |
| 20 | |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 21 | VertexArrayGL::VertexArrayGL(const FunctionsGL *functions, StateManagerGL *stateManager) |
| 22 | : VertexArrayImpl(), |
| 23 | mFunctions(functions), |
| 24 | mStateManager(stateManager), |
| 25 | mVertexArrayID(0), |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame^] | 26 | mElementArrayBuffer(), |
| 27 | mAttributes(), |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 28 | mAppliedElementArrayBuffer(0), |
| 29 | mAppliedAttributes() |
| 30 | { |
| 31 | ASSERT(mFunctions); |
| 32 | ASSERT(mStateManager); |
| 33 | mFunctions->genVertexArrays(1, &mVertexArrayID); |
| 34 | |
| 35 | // Set the cached vertex attribute array size |
| 36 | GLint maxVertexAttribs; |
| 37 | mFunctions->getIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs); |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame^] | 38 | mAttributes.resize(maxVertexAttribs); |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 39 | mAppliedAttributes.resize(maxVertexAttribs); |
| 40 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 41 | |
| 42 | VertexArrayGL::~VertexArrayGL() |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 43 | { |
| 44 | if (mVertexArrayID != 0) |
| 45 | { |
| 46 | mFunctions->deleteVertexArrays(1, &mVertexArrayID); |
| 47 | mVertexArrayID = 0; |
| 48 | } |
| 49 | |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame^] | 50 | mElementArrayBuffer.set(nullptr); |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 51 | for (size_t idx = 0; idx < mAppliedAttributes.size(); idx++) |
| 52 | { |
| 53 | mAppliedAttributes[idx].buffer.set(NULL); |
| 54 | } |
| 55 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 56 | |
| 57 | void VertexArrayGL::setElementArrayBuffer(const gl::Buffer *buffer) |
| 58 | { |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame^] | 59 | mElementArrayBuffer.set(buffer); |
| 60 | } |
| 61 | |
| 62 | void VertexArrayGL::setAttribute(size_t idx, const gl::VertexAttribute &attr) |
| 63 | { |
| 64 | mAttributes[idx] = attr; |
| 65 | } |
| 66 | |
| 67 | void VertexArrayGL::setAttributeDivisor(size_t idx, GLuint divisor) |
| 68 | { |
| 69 | mAttributes[idx].divisor = divisor; |
| 70 | } |
| 71 | |
| 72 | void VertexArrayGL::enableAttribute(size_t idx, bool enabledState) |
| 73 | { |
| 74 | mAttributes[idx].enabled = enabledState; |
| 75 | } |
| 76 | |
| 77 | void VertexArrayGL::syncState() const |
| 78 | { |
| 79 | mStateManager->bindVertexArray(mVertexArrayID); |
| 80 | |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 81 | GLuint elementArrayBufferID = 0; |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame^] | 82 | if (mElementArrayBuffer.get() != nullptr) |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 83 | { |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame^] | 84 | const BufferGL *bufferGL = GetImplAs<BufferGL>(mElementArrayBuffer.get()); |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 85 | elementArrayBufferID = bufferGL->getBufferID(); |
| 86 | } |
| 87 | |
| 88 | if (elementArrayBufferID != mAppliedElementArrayBuffer) |
| 89 | { |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 90 | mStateManager->bindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementArrayBufferID); |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 91 | mAppliedElementArrayBuffer = elementArrayBufferID; |
| 92 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 93 | |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame^] | 94 | for (size_t idx = 0; idx < mAttributes.size(); idx++) |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 95 | { |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame^] | 96 | if (mAppliedAttributes[idx] != mAttributes[idx]) |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 97 | { |
Geoff Lang | 6ae6efc | 2015-03-09 14:42:35 -0400 | [diff] [blame^] | 98 | if (mAttributes[idx].enabled) |
| 99 | { |
| 100 | mFunctions->enableVertexAttribArray(idx); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | mFunctions->disableVertexAttribArray(idx); |
| 105 | } |
| 106 | |
| 107 | const gl::Buffer *arrayBuffer = mAttributes[idx].buffer.get(); |
| 108 | if (arrayBuffer != nullptr) |
| 109 | { |
| 110 | const BufferGL *arrayBufferGL = GetImplAs<BufferGL>(arrayBuffer); |
| 111 | mStateManager->bindBuffer(GL_ARRAY_BUFFER, arrayBufferGL->getBufferID()); |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | // This will take some extra work, core OpenGL doesn't support binding raw data pointers |
| 116 | // to VAOs |
| 117 | UNIMPLEMENTED(); |
| 118 | } |
| 119 | |
| 120 | if (mAttributes[idx].pureInteger) |
| 121 | { |
| 122 | mFunctions->vertexAttribIPointer(idx, mAttributes[idx].size, mAttributes[idx].type, |
| 123 | mAttributes[idx].stride, mAttributes[idx].pointer); |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | mFunctions->vertexAttribPointer(idx, mAttributes[idx].size, mAttributes[idx].type, |
| 128 | mAttributes[idx].normalized, mAttributes[idx].stride, |
| 129 | mAttributes[idx].pointer); |
| 130 | } |
| 131 | |
| 132 | mFunctions->vertexAttribDivisor(idx, mAttributes[idx].divisor); |
| 133 | |
| 134 | mAppliedAttributes[idx] = mAttributes[idx]; |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 135 | } |
Geoff Lang | ba4c4a8 | 2015-02-24 12:38:46 -0500 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | GLuint VertexArrayGL::getVertexArrayID() const |
| 140 | { |
| 141 | return mVertexArrayID; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | } |