1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "qquick3dresourceloader_p.h"
#include "qquick3dmodel_p.h"
#include <QtQuick3DRuntimeRender/private/qssgrenderresourceloader_p.h>
QT_BEGIN_NAMESPACE
/*!
\qmltype ResourceLoader
\inqmlmodule QtQuick3D
\inherits Object3D
\brief Allows pre-loading of 3D resources.
ResourceLoader is used to pre-load resources for Qt Quick 3D. Normally
resources are only loaded when they are needed to render a frame, and are
unloaded when they are not used to render the scene. This aggressive
approach to resource lifetimes means that only the bare minimum of GPU
resources are used to render a frame, but for some dynamic scenes this
can lead to resources being loaded and released frequently. The
ResourceLoader component enables a finer grain control on the lifetimes
of resources in the scene. Resources listed in the ResourceLoader
component are loaded into GPU memory and will remain there until they
are removed from the ResourceLoader lists or the ResourceLoader is
destroyed.
ResourceLoader can also be used to make sure that large resources are
available before rendering a frame. Since resources are loaded only
when needed for a frame, this can lead to frames being dropped waiting
for a large resource to be loaded. By pre-loading large resources before
showing a scene, there is no risk of dropping any frames due to resources
being loaded during an animation.
For usage examples, see \l {Qt Quick 3D - Principled Material Example}
*/
/*!
\qmlproperty List<url> ResourceLoader::meshSources
This property defines a list of locations of mesh files containing geometry.
When a mesh file is added to this list, it will be loaded to the GPU and
cached. If these same mesh files are source is used by a /c Model they will
not need to be loaded again.
*/
/*!
\qmlproperty List<QtQuick3D::Texture> ResourceLoader::textures
This property defines a list of Texture resources that will be loaded to the
GPU and cached.
*/
/*!
\qmlproperty List<QtQuick3D::Geometry> ResourceLoader::geometries
This property defines a list of Geometry resources that will be loaded to the
GPU and cached.
*/
QQuick3DResourceLoader::QQuick3DResourceLoader(QQuick3DObject *parent)
: QQuick3DObject(*(new QQuick3DObjectPrivate(QQuick3DObjectPrivate::Type::ResourceLoader)), parent)
{
}
const QList<QUrl> &QQuick3DResourceLoader::meshSources() const
{
return m_meshSources;
}
void QQuick3DResourceLoader::setMeshSources(const QList<QUrl> &newMeshSources)
{
if (m_meshSources == newMeshSources)
return;
m_meshSources = newMeshSources;
emit meshSourcesChanged();
markDirty(QQuick3DResourceLoader::MeshesDirty);
}
QQmlListProperty<QQuick3DGeometry> QQuick3DResourceLoader::geometries()
{
return QQmlListProperty<QQuick3DGeometry>(this,
nullptr,
QQuick3DResourceLoader::qmlAppendGeometry,
QQuick3DResourceLoader::qmlGeometriesCount,
QQuick3DResourceLoader::qmlGeometryAt,
QQuick3DResourceLoader::qmlClearGeometries);
}
QQmlListProperty<QQuick3DTexture> QQuick3DResourceLoader::textures()
{
return QQmlListProperty<QQuick3DTexture>(this,
nullptr,
QQuick3DResourceLoader::qmlAppendTexture,
QQuick3DResourceLoader::qmlTexturesCount,
QQuick3DResourceLoader::qmlTextureAt,
QQuick3DResourceLoader::qmlClearTextures);
}
void QQuick3DResourceLoader::onGeometryDestroyed(QObject *object)
{
bool found = false;
for (int i = 0; i < m_geometries.size(); ++i) {
if (m_geometries[i] == object) {
m_geometries.removeAt(i--);
found = true;
}
}
if (found)
markDirty(QQuick3DResourceLoader::GeometriesDirty);
}
void QQuick3DResourceLoader::onTextureDestroyed(QObject *object)
{
bool found = false;
for (int i = 0; i < m_textures.size(); ++i) {
if (m_textures[i] == object) {
m_textures.removeAt(i--);
found = true;
}
}
if (found)
markDirty(QQuick3DResourceLoader::TexturesDirty);
}
QSSGRenderGraphObject *QQuick3DResourceLoader::updateSpatialNode(QSSGRenderGraphObject *node)
{
if (!node) {
markAllDirty();
node = new QSSGRenderResourceLoader();
}
QQuick3DObject::updateSpatialNode(node);
int dirtyAttribute = 0;
auto resourceLoaderNode = static_cast<QSSGRenderResourceLoader *>(node);
if (m_dirtyAttributes & MeshesDirty) {
resourceLoaderNode->meshes.clear();
for (const auto &mesh : std::as_const(m_meshSources))
resourceLoaderNode->meshes.push_back(QSSGRenderPath(QQuick3DModel::translateMeshSource(mesh, this)));
}
if (m_dirtyAttributes & TexturesDirty) {
resourceLoaderNode->textures.clear();
for (const auto &texture : std::as_const(m_textures)) {
auto graphObject = QQuick3DObjectPrivate::get(texture)->spatialNode;
if (graphObject)
resourceLoaderNode->textures.push_back(graphObject);
else
dirtyAttribute |= TexturesDirty;
}
}
if (m_dirtyAttributes & GeometriesDirty) {
resourceLoaderNode->geometries.clear();
for (const auto &geometry : std::as_const(m_geometries)) {
auto graphObject = QQuick3DObjectPrivate::get(geometry)->spatialNode;
if (graphObject)
resourceLoaderNode->geometries.push_back(graphObject);
else
dirtyAttribute |= GeometriesDirty;
}
}
m_dirtyAttributes = dirtyAttribute;
return resourceLoaderNode;
}
void QQuick3DResourceLoader::markAllDirty()
{
m_dirtyAttributes = 0xffffffff;
QQuick3DObject::markAllDirty();
}
void QQuick3DResourceLoader::itemChange(ItemChange change, const ItemChangeData &value)
{
if (change == QQuick3DObject::ItemSceneChange)
updateSceneManager(value.sceneManager);
}
void QQuick3DResourceLoader::qmlAppendGeometry(QQmlListProperty<QQuick3DGeometry> *list, QQuick3DGeometry *geometry)
{
if (geometry == nullptr)
return;
QQuick3DResourceLoader *self = static_cast<QQuick3DResourceLoader *>(list->object);
self->m_geometries.push_back(geometry);
self->markDirty(QQuick3DResourceLoader::GeometriesDirty);
if (geometry->parentItem() == nullptr) {
// If the material has no parent, check if it has a hierarchical parent that's a QQuick3DObject
// and re-parent it to that, e.g., inline materials
QQuick3DObject *parentItem = qobject_cast<QQuick3DObject *>(geometry->parent());
if (parentItem) {
geometry->setParentItem(parentItem);
} else { // If no valid parent was found, make sure the material refs our scene manager
const auto &sceneManager = QQuick3DObjectPrivate::get(self)->sceneManager;
if (sceneManager) {
QQuick3DObjectPrivate::get(geometry)->refSceneManager(*sceneManager);
}
}
}
// Make sure geometries are removed when destroyed
connect(geometry, &QQuick3DGeometry::destroyed, self, &QQuick3DResourceLoader::onGeometryDestroyed);
}
QQuick3DGeometry *QQuick3DResourceLoader::qmlGeometryAt(QQmlListProperty<QQuick3DGeometry> *list, qsizetype index)
{
QQuick3DResourceLoader *self = static_cast<QQuick3DResourceLoader *>(list->object);
if (index >= self->m_geometries.size()) {
qWarning("The index exceeds the range of valid geometries.");
return nullptr;
}
return self->m_geometries.at(index);
}
qsizetype QQuick3DResourceLoader::qmlGeometriesCount(QQmlListProperty<QQuick3DGeometry> *list)
{
QQuick3DResourceLoader *self = static_cast<QQuick3DResourceLoader *>(list->object);
return self->m_geometries.size();
}
void QQuick3DResourceLoader::qmlClearGeometries(QQmlListProperty<QQuick3DGeometry> *list)
{
QQuick3DResourceLoader *self = static_cast<QQuick3DResourceLoader *>(list->object);
for (const auto &geometry : std::as_const(self->m_geometries)) {
if (geometry->parentItem() == nullptr)
QQuick3DObjectPrivate::get(geometry)->derefSceneManager();
disconnect(geometry, &QQuick3DGeometry::destroyed, self, &QQuick3DResourceLoader::onGeometryDestroyed);
}
self->m_geometries.clear();
self->markDirty(QQuick3DResourceLoader::GeometriesDirty);
}
void QQuick3DResourceLoader::qmlAppendTexture(QQmlListProperty<QQuick3DTexture> *list, QQuick3DTexture *texture)
{
if (texture == nullptr)
return;
QQuick3DResourceLoader *self = static_cast<QQuick3DResourceLoader *>(list->object);
self->m_textures.push_back(texture);
self->markDirty(QQuick3DResourceLoader::TexturesDirty);
if (texture->parentItem() == nullptr) {
// If the material has no parent, check if it has a hierarchical parent that's a QQuick3DObject
// and re-parent it to that, e.g., inline materials
QQuick3DObject *parentItem = qobject_cast<QQuick3DObject *>(texture->parent());
if (parentItem) {
texture->setParentItem(parentItem);
} else { // If no valid parent was found, make sure the material refs our scene manager
const auto &sceneManager = QQuick3DObjectPrivate::get(self)->sceneManager;
if (sceneManager) {
QQuick3DObjectPrivate::get(texture)->refSceneManager(*sceneManager);
}
}
}
// Make sure TextureData are removed when destroyed
connect(texture, &QQuick3DTextureData::destroyed, self, &QQuick3DResourceLoader::onTextureDestroyed);
}
QQuick3DTexture *QQuick3DResourceLoader::qmlTextureAt(QQmlListProperty<QQuick3DTexture> *list, qsizetype index)
{
QQuick3DResourceLoader *self = static_cast<QQuick3DResourceLoader *>(list->object);
if (index >= self->m_textures.size()) {
qWarning("The index exceeds the range of valid texture data.");
return nullptr;
}
return self->m_textures.at(index);
}
qsizetype QQuick3DResourceLoader::qmlTexturesCount(QQmlListProperty<QQuick3DTexture> *list)
{
QQuick3DResourceLoader *self = static_cast<QQuick3DResourceLoader *>(list->object);
return self->m_textures.size();
}
void QQuick3DResourceLoader::qmlClearTextures(QQmlListProperty<QQuick3DTexture> *list)
{
QQuick3DResourceLoader *self = static_cast<QQuick3DResourceLoader *>(list->object);
for (const auto &data : std::as_const(self->m_textures)) {
if (data->parentItem() == nullptr)
QQuick3DObjectPrivate::get(data)->derefSceneManager();
disconnect(data, &QQuick3DTextureData::destroyed, self, &QQuick3DResourceLoader::onTextureDestroyed);
}
self->m_textures.clear();
self->markDirty(QQuick3DResourceLoader::TexturesDirty);
}
void QQuick3DResourceLoader::markDirty(ResourceLoaderDirtyType type)
{
if (!(m_dirtyAttributes & quint32(type))) {
m_dirtyAttributes |= quint32(type);
update();
}
}
void QQuick3DResourceLoader::updateSceneManager(QQuick3DSceneManager *sceneManager)
{
if (sceneManager) {
for (auto &geometry : m_geometries)
if (!geometry->parentItem() && !QQuick3DObjectPrivate::get(geometry)->sceneManager)
QQuick3DObjectPrivate::refSceneManager(geometry, *sceneManager);
for (auto &texture : m_textures)
if (!texture->parentItem() && !QQuick3DObjectPrivate::get(texture)->sceneManager)
QQuick3DObjectPrivate::refSceneManager(texture, *sceneManager);
} else {
for (auto &geometry : m_geometries)
QQuick3DObjectPrivate::derefSceneManager(geometry);
for (auto &texture : m_textures)
QQuick3DObjectPrivate::derefSceneManager(texture);
}
}
QT_END_NAMESPACE
|