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 "texture_p.h" |
41 | |
42 | #include <QDebug> |
43 | #include <QOpenGLFunctions> |
44 | #include <QOpenGLTexture> |
45 | #include <Qt3DCore/qpropertyupdatedchange.h> |
46 | #include <Qt3DCore/qpropertynodeaddedchange.h> |
47 | #include <Qt3DCore/qpropertynoderemovedchange.h> |
48 | |
49 | #include <Qt3DRender/private/texture_p.h> |
50 | #include <Qt3DRender/private/qabstracttexture_p.h> |
51 | #include <Qt3DRender/private/managers_p.h> |
52 | |
53 | QT_BEGIN_NAMESPACE |
54 | |
55 | using namespace Qt3DCore; |
56 | |
57 | namespace Qt3DRender { |
58 | namespace Render { |
59 | |
60 | Texture::Texture() |
61 | // We need backend -> frontend notifications to update the status of the texture |
62 | : BackendNode(ReadWrite) |
63 | , m_dirty(DirtyImageGenerators|DirtyProperties|DirtyParameters|DirtyDataGenerator) |
64 | , m_sharedTextureId(-1) |
65 | { |
66 | } |
67 | |
68 | Texture::~Texture() |
69 | { |
70 | // We do not abandon the api texture |
71 | // because if the dtor is called that means |
72 | // the manager was destroyed otherwise cleanup |
73 | // would have been called |
74 | } |
75 | |
76 | void Texture::addDirtyFlag(DirtyFlags flags) |
77 | { |
78 | QMutexLocker lock(&m_flagsMutex); |
79 | m_dirty |= flags; |
80 | if (m_renderer) |
81 | markDirty(changes: AbstractRenderer::TexturesDirty); |
82 | } |
83 | |
84 | Texture::DirtyFlags Texture::dirtyFlags() |
85 | { |
86 | QMutexLocker lock(&m_flagsMutex); |
87 | return m_dirty; |
88 | } |
89 | |
90 | void Texture::unsetDirty() |
91 | { |
92 | QMutexLocker lock(&m_flagsMutex); |
93 | m_dirty = Texture::NotDirty; |
94 | } |
95 | |
96 | // This is called by Renderer::updateGLResources |
97 | // when the texture has been marked for cleanup |
98 | void Texture::cleanup() |
99 | { |
100 | // Whoever calls this must make sure to also check if this |
101 | // texture is being referenced by a shared API specific texture (GLTexture) |
102 | m_dataFunctor.reset(); |
103 | m_textureImageIds.clear(); |
104 | m_pendingTextureDataUpdates.clear(); |
105 | m_sharedTextureId = -1; |
106 | |
107 | // set default values |
108 | m_properties = {}; |
109 | m_parameters = {}; |
110 | |
111 | m_dirty = NotDirty; |
112 | } |
113 | |
114 | void Texture::syncFromFrontEnd(const QNode *frontEnd, bool firstTime) |
115 | { |
116 | BackendNode::syncFromFrontEnd(frontEnd, firstTime); |
117 | const QAbstractTexture *node = qobject_cast<const QAbstractTexture *>(object: frontEnd); |
118 | if (!node) |
119 | return; |
120 | |
121 | TextureProperties p = m_properties; |
122 | p.width = node->width(); |
123 | p.height = node->height(); |
124 | p.depth = node->depth(); |
125 | p.format = node->format(); |
126 | p.target = node->target(); |
127 | p.generateMipMaps = node->generateMipMaps(); |
128 | p.layers = node->layers(); |
129 | p.samples = node->samples(); |
130 | p.mipLevels = static_cast<const QAbstractTexturePrivate*>(QAbstractTexturePrivate::get(q: node))->m_mipmapLevels; |
131 | if (p != m_properties) { |
132 | m_properties = p; |
133 | addDirtyFlag(flags: DirtyProperties); |
134 | } |
135 | |
136 | TextureParameters q = m_parameters; |
137 | q.magnificationFilter = node->magnificationFilter(); |
138 | q.minificationFilter = node->minificationFilter(); |
139 | q.wrapModeX = const_cast<QAbstractTexture *>(node)->wrapMode()->x(); |
140 | q.wrapModeY = const_cast<QAbstractTexture *>(node)->wrapMode()->y(); |
141 | q.wrapModeZ = const_cast<QAbstractTexture *>(node)->wrapMode()->z(); |
142 | q.maximumAnisotropy = node->maximumAnisotropy(); |
143 | q.comparisonFunction = node->comparisonFunction(); |
144 | q.comparisonMode = node->comparisonMode(); |
145 | if (q != m_parameters) { |
146 | m_parameters = q; |
147 | addDirtyFlag(flags: DirtyParameters); |
148 | } |
149 | |
150 | auto newGenerator = node->dataGenerator(); |
151 | if (newGenerator != m_dataFunctor) { |
152 | setDataGenerator(newGenerator); |
153 | QAbstractTexturePrivate *dTexture = static_cast<QAbstractTexturePrivate *>(QNodePrivate::get(q: const_cast<QNode *>(frontEnd))); |
154 | dTexture->setStatus(QAbstractTexture::Loading); |
155 | } |
156 | |
157 | QAbstractTexturePrivate *dnode = dynamic_cast<QAbstractTexturePrivate *>(QAbstractTexturePrivate::get(q: const_cast<QAbstractTexture *>(node))); |
158 | if (dnode) { |
159 | for (const QTextureDataUpdate &pendingUpdate : dnode->m_pendingDataUpdates) |
160 | addTextureDataUpdate(update: pendingUpdate); |
161 | dnode->m_pendingDataUpdates.clear(); |
162 | |
163 | auto ids = Qt3DCore::qIdsForNodes(nodes: dnode->m_textureImages); |
164 | std::sort(first: std::begin(cont&: ids), last: std::end(cont&: ids)); |
165 | if (ids != m_textureImageIds) { |
166 | m_textureImageIds = ids; |
167 | addDirtyFlag(flags: DirtyImageGenerators); |
168 | } |
169 | |
170 | if (dnode->m_sharedTextureId != m_sharedTextureId) { |
171 | m_sharedTextureId = dnode->m_sharedTextureId; |
172 | addDirtyFlag(flags: DirtySharedTextureId); |
173 | } |
174 | } |
175 | } |
176 | |
177 | // Called by sceneChangeEvent or TextureDownloadRequest (both in AspectThread context) |
178 | void Texture::setDataGenerator(const QTextureGeneratorPtr &generator) |
179 | { |
180 | m_dataFunctor = generator; |
181 | addDirtyFlag(flags: DirtyDataGenerator); |
182 | } |
183 | |
184 | bool Texture::isValid(TextureImageManager *manager) const |
185 | { |
186 | for (const QNodeId &id : m_textureImageIds) { |
187 | TextureImage *img = manager->lookupResource(id); |
188 | if (img == nullptr) |
189 | return false; |
190 | } |
191 | return true; |
192 | } |
193 | |
194 | void Texture::initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change) |
195 | { |
196 | const auto typedChange = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<QAbstractTextureData>>(src: change); |
197 | const auto &data = typedChange->data; |
198 | |
199 | m_properties.target = data.target; |
200 | m_properties.format = data.format; |
201 | m_properties.width = data.width; |
202 | m_properties.height = data.height; |
203 | m_properties.depth = data.depth; |
204 | m_properties.generateMipMaps = data.autoMipMap; |
205 | m_properties.layers = data.layers; |
206 | m_properties.samples = data.samples; |
207 | m_parameters.minificationFilter = data.minFilter; |
208 | m_parameters.magnificationFilter = data.magFilter; |
209 | m_parameters.wrapModeX = data.wrapModeX; |
210 | m_parameters.wrapModeY = data.wrapModeY; |
211 | m_parameters.wrapModeZ = data.wrapModeZ; |
212 | m_parameters.maximumAnisotropy = data.maximumAnisotropy; |
213 | m_parameters.comparisonFunction = data.comparisonFunction; |
214 | m_parameters.comparisonMode = data.comparisonMode; |
215 | m_dataFunctor = data.dataFunctor; |
216 | m_sharedTextureId = data.sharedTextureId; |
217 | |
218 | m_textureImageIds = data.textureImageIds; |
219 | if (m_textureImageIds.size()) |
220 | addDirtyFlag(flags: DirtyImageGenerators); |
221 | |
222 | const QVector<QTextureDataUpdate> initialDataUpdates = data.initialDataUpdates; |
223 | for (const QTextureDataUpdate &initialUpdate : initialDataUpdates) |
224 | addTextureDataUpdate(update: initialUpdate); |
225 | |
226 | addDirtyFlag(flags: DirtyFlags(DirtyImageGenerators|DirtyProperties|DirtyParameters)); |
227 | if (m_sharedTextureId > 0) |
228 | addDirtyFlag(flags: DirtySharedTextureId); |
229 | } |
230 | |
231 | void Texture::addTextureDataUpdate(const QTextureDataUpdate &update) |
232 | { |
233 | m_pendingTextureDataUpdates.push_back(t: update); |
234 | addDirtyFlag(flags: DirtyPendingDataUpdates); |
235 | } |
236 | |
237 | |
238 | TextureFunctor::TextureFunctor(AbstractRenderer *renderer, |
239 | TextureManager *textureNodeManager) |
240 | : m_renderer(renderer) |
241 | , m_textureNodeManager(textureNodeManager) |
242 | { |
243 | } |
244 | |
245 | Qt3DCore::QBackendNode *TextureFunctor::create(const Qt3DCore::QNodeCreatedChangeBasePtr &change) const |
246 | { |
247 | Texture *backend = m_textureNodeManager->getOrCreateResource(id: change->subjectId()); |
248 | backend->setRenderer(m_renderer); |
249 | // Remove id from cleanupList if for some reason we were in the dirty list of texture |
250 | // (Can happen when a node destroyed is followed by a node created change |
251 | // in the same loop, when changing parent for instance) |
252 | m_textureNodeManager->removeTextureIdToCleanup(id: change->subjectId()); |
253 | return backend; |
254 | } |
255 | |
256 | Qt3DCore::QBackendNode *TextureFunctor::get(Qt3DCore::QNodeId id) const |
257 | { |
258 | return m_textureNodeManager->lookupResource(id); |
259 | } |
260 | |
261 | void TextureFunctor::destroy(Qt3DCore::QNodeId id) const |
262 | { |
263 | m_textureNodeManager->addTextureIdToCleanup(id); |
264 | // We add ourselves to the dirty list to tell the shared texture managers |
265 | // in the renderer that this texture has been destroyed |
266 | m_textureNodeManager->releaseResource(id); |
267 | } |
268 | |
269 | |
270 | } // namespace Render |
271 | } // namespace Qt3DRender |
272 | |
273 | QT_END_NAMESPACE |
274 | |