1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "openglvertexarrayobject_p.h" |
41 | #include <submissioncontext_p.h> |
42 | #include <renderer_p.h> |
43 | #include <glresourcemanagers_p.h> |
44 | #include <Qt3DRender/private/managers_p.h> |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | namespace Qt3DRender { |
49 | namespace Render { |
50 | namespace OpenGL { |
51 | |
52 | OpenGLVertexArrayObject::OpenGLVertexArrayObject() |
53 | : m_ctx(nullptr) |
54 | , m_specified(false) |
55 | , m_supportsVao(false) |
56 | {} |
57 | |
58 | void OpenGLVertexArrayObject::bind() |
59 | { |
60 | Q_ASSERT(m_ctx); |
61 | if (m_supportsVao) { |
62 | Q_ASSERT(!m_vao.isNull()); |
63 | Q_ASSERT(m_vao->isCreated()); |
64 | m_vao->bind(); |
65 | } else { |
66 | // Unbind any other VAO that may have been bound and not released correctly |
67 | if (m_ctx->m_currentVAO != nullptr && m_ctx->m_currentVAO != this) |
68 | m_ctx->m_currentVAO->release(); |
69 | |
70 | m_ctx->m_currentVAO = this; |
71 | // We need to specify array and vertex attributes |
72 | for (const SubmissionContext::VAOVertexAttribute &attr : qAsConst(t&: m_vertexAttributes)) |
73 | m_ctx->enableAttribute(attr); |
74 | if (!m_indexAttribute.isNull()) |
75 | m_ctx->bindGLBuffer(buffer: m_ctx->m_renderer->glResourceManagers()->glBufferManager()->data(handle: m_indexAttribute), |
76 | type: GLBuffer::IndexBuffer); |
77 | } |
78 | } |
79 | |
80 | void OpenGLVertexArrayObject::release() |
81 | { |
82 | Q_ASSERT(m_ctx); |
83 | if (m_supportsVao) { |
84 | Q_ASSERT(!m_vao.isNull()); |
85 | Q_ASSERT(m_vao->isCreated()); |
86 | m_vao->release(); |
87 | } else { |
88 | if (m_ctx->m_currentVAO == this) { |
89 | for (const SubmissionContext::VAOVertexAttribute &attr : qAsConst(t&: m_vertexAttributes)) |
90 | m_ctx->disableAttribute(attr); |
91 | m_ctx->m_currentVAO = nullptr; |
92 | } |
93 | } |
94 | } |
95 | |
96 | // called from Render thread |
97 | void OpenGLVertexArrayObject::create(SubmissionContext *ctx, const VAOIdentifier &key) |
98 | { |
99 | QMutexLocker lock(&m_mutex); |
100 | |
101 | Q_ASSERT(!m_ctx && !m_vao); |
102 | |
103 | m_ctx = ctx; |
104 | m_supportsVao = m_ctx->supportsVAO(); |
105 | if (m_supportsVao) { |
106 | m_vao.reset(other: new QOpenGLVertexArrayObject()); |
107 | m_vao->create(); |
108 | } |
109 | m_owners = key; |
110 | } |
111 | |
112 | VAOIdentifier OpenGLVertexArrayObject::key() const |
113 | { |
114 | return m_owners; |
115 | } |
116 | |
117 | // called from Render thread |
118 | void OpenGLVertexArrayObject::destroy() |
119 | { |
120 | QMutexLocker locker(&m_mutex); |
121 | |
122 | Q_ASSERT(m_ctx); |
123 | cleanup(); |
124 | } |
125 | |
126 | void OpenGLVertexArrayObject::cleanup() |
127 | { |
128 | m_vao.reset(); |
129 | m_ctx = nullptr; |
130 | m_specified = false; |
131 | m_supportsVao = false; |
132 | m_indexAttribute = SubmissionContext::VAOIndexAttribute(); |
133 | m_vertexAttributes.clear(); |
134 | } |
135 | |
136 | // called from job |
137 | bool OpenGLVertexArrayObject::isAbandoned(GeometryManager *geomMgr, GLShaderManager *shaderMgr) |
138 | { |
139 | QMutexLocker lock(&m_mutex); |
140 | |
141 | if (!m_ctx) |
142 | return false; |
143 | |
144 | const bool geometryExists = (geomMgr->data(handle: m_owners.first) != nullptr); |
145 | const bool shaderExists = (shaderMgr->lookupResource(shaderId: m_owners.second) != nullptr); |
146 | |
147 | return !geometryExists || !shaderExists; |
148 | } |
149 | |
150 | void OpenGLVertexArrayObject::saveVertexAttribute(const SubmissionContext::VAOVertexAttribute &attr) |
151 | { |
152 | // Remove any vertexAttribute already at location |
153 | for (auto i = m_vertexAttributes.size() - 1; i >= 0; --i) { |
154 | if (m_vertexAttributes.at(i).location == attr.location) { |
155 | m_vertexAttributes.removeAt(i); |
156 | break; |
157 | } |
158 | } |
159 | m_vertexAttributes.push_back(t: attr); |
160 | } |
161 | |
162 | |
163 | } // namespace OpenGL |
164 | } // namespace Render |
165 | } // namespace Qt3DRender |
166 | |
167 | QT_END_NAMESPACE |
168 |