1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 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 | #ifndef QT3DRENDER_RENDER_APISHADERMANAGER_H |
41 | #define QT3DRENDER_RENDER_APISHADERMANAGER_H |
42 | |
43 | |
44 | // |
45 | // W A R N I N G |
46 | // ------------- |
47 | // |
48 | // This file is not part of the Qt API. It exists for the convenience |
49 | // of other Qt classes. This header file may change from version to |
50 | // version without notice, or even be removed. |
51 | // |
52 | // We mean it. |
53 | // |
54 | |
55 | #include <Qt3DCore/qnodeid.h> |
56 | #include <Qt3DRender/private/shader_p.h> |
57 | #include <QtCore/QReadLocker> |
58 | |
59 | QT_BEGIN_NAMESPACE |
60 | |
61 | namespace Qt3DRender { |
62 | |
63 | namespace Render { |
64 | |
65 | class Shader; |
66 | |
67 | template<class APIShader> |
68 | class APIShaderManager |
69 | { |
70 | public: |
71 | explicit APIShaderManager() |
72 | { |
73 | } |
74 | |
75 | ~APIShaderManager() |
76 | { |
77 | } |
78 | |
79 | QVector<APIShader *> takeActiveResources() const |
80 | { |
81 | QReadLocker lock(&m_readWriteLock); |
82 | return m_apiShaders.keys().toVector() + m_abandonedShaders; |
83 | } |
84 | |
85 | APIShader *lookupResource(Qt3DCore::QNodeId shaderId) |
86 | { |
87 | QReadLocker lock(&m_readWriteLock); |
88 | return m_nodeIdToAPIShader.value(shaderId); |
89 | } |
90 | |
91 | // Note: automatically adopts the Shader if it needs to be created |
92 | APIShader *createOrAdoptExisting(const Shader *shader) |
93 | { |
94 | // Try to find if an APIShader that matches shader |
95 | // already exists |
96 | |
97 | { |
98 | QReadLocker readLock(&m_readWriteLock); |
99 | { |
100 | const auto end = m_apiShaders.cend(); |
101 | for (auto it = m_apiShaders.cbegin(); it != end; ++it) |
102 | if (isSameShader(apiShader: it.key(), shaderNode: shader)) { |
103 | APIShader *apiShader = it.key(); |
104 | // Adopt if needed |
105 | readLock.unlock(); |
106 | adopt(apiShader, shader); |
107 | return apiShader; |
108 | } |
109 | } |
110 | |
111 | // Try to find if one of the scheduled for deletion APIShader |
112 | // could be reused |
113 | { |
114 | const auto end = m_abandonedShaders.end(); |
115 | for (auto it = m_abandonedShaders.begin(); it != end; ++it) |
116 | if (isSameShader(apiShader: *it, shaderNode: shader)) { |
117 | APIShader *apiShader = *it; |
118 | // Adopt if needed |
119 | readLock.unlock(); |
120 | // Remove from list of shaders scheduled for relase |
121 | m_abandonedShaders.erase(it); |
122 | adopt(apiShader, shader); |
123 | return apiShader; |
124 | } |
125 | } |
126 | } |
127 | |
128 | // If not create one |
129 | APIShader *apiShader = create(); |
130 | adopt(apiShader, shader); |
131 | return apiShader; |
132 | } |
133 | |
134 | // Should never be called from outside code |
135 | // but left public to maintain adopt/abandon symmetry |
136 | void adopt(APIShader *apiShader, const Shader *shader) |
137 | { |
138 | QWriteLocker lock(&m_readWriteLock); |
139 | if (!m_apiShaders[apiShader].contains(shader->peerId())) { |
140 | m_apiShaders[apiShader].push_back(shader->peerId()); |
141 | m_nodeIdToAPIShader.insert(shader->peerId(), apiShader); |
142 | } |
143 | } |
144 | |
145 | void abandon(APIShader *apiShader, const Shader *shader) |
146 | { |
147 | QWriteLocker lock(&m_readWriteLock); |
148 | APIShader *storedApiShader = m_nodeIdToAPIShader.take(shader->peerId()); |
149 | Q_ASSERT(apiShader != nullptr && apiShader == storedApiShader); |
150 | |
151 | QVector<Qt3DCore::QNodeId> &referencedShaderNodes = m_apiShaders[apiShader]; |
152 | referencedShaderNodes.removeAll(t: shader->peerId()); |
153 | |
154 | if (referencedShaderNodes.empty()) { |
155 | m_abandonedShaders.push_back(apiShader); |
156 | m_apiShaders.remove(apiShader); |
157 | } |
158 | } |
159 | |
160 | QVector<APIShader *> takeAbandonned() |
161 | { |
162 | QWriteLocker lock(&m_readWriteLock); |
163 | return std::move(m_abandonedShaders); |
164 | } |
165 | |
166 | QVector<APIShader *> takeUpdated() |
167 | { |
168 | QWriteLocker lock(&m_readWriteLock); |
169 | return std::move(m_updatedShaders); |
170 | } |
171 | |
172 | QVector<Qt3DCore::QNodeId> shaderIdsForProgram(APIShader *glShader) const |
173 | { |
174 | QReadLocker lock(&m_readWriteLock); |
175 | return m_apiShaders.value(glShader); |
176 | } |
177 | |
178 | void purge() |
179 | { |
180 | qDeleteAll(takeAbandonned()); |
181 | } |
182 | |
183 | private: |
184 | |
185 | bool isSameShader(const APIShader *apiShader, const Shader *shaderNode) |
186 | { |
187 | const QVector<QByteArray> nodeShaderCode = shaderNode->shaderCode(); |
188 | const QVector<QByteArray> apiShaderCode = apiShader->shaderCode(); |
189 | |
190 | const int s = nodeShaderCode.size(); |
191 | |
192 | Q_ASSERT(s == apiShaderCode.size()); |
193 | |
194 | for (int i = 0; i < s; ++i) |
195 | if (nodeShaderCode.at(i) != apiShaderCode.at(i)) |
196 | return false; |
197 | |
198 | return true; |
199 | } |
200 | |
201 | APIShader *create() |
202 | { |
203 | APIShader *apiShader = new APIShader(); |
204 | m_updatedShaders.push_back(apiShader); |
205 | return apiShader; |
206 | } |
207 | |
208 | |
209 | QHash<Qt3DCore::QNodeId, APIShader *> m_nodeIdToAPIShader; |
210 | QHash<APIShader *, QVector<Qt3DCore::QNodeId>> m_apiShaders; |
211 | |
212 | QVector<APIShader *> m_abandonedShaders; |
213 | QVector<APIShader *> m_updatedShaders; |
214 | |
215 | mutable QReadWriteLock m_readWriteLock; |
216 | }; |
217 | |
218 | } // Render |
219 | |
220 | } // Qt3DRender |
221 | |
222 | QT_END_NAMESPACE |
223 | |
224 | #endif // QT3DRENDER_RENDER_APISHADERMANAGER_H |
225 | |