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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "qquick3dtexturedata.h"
#include "qquick3dtexturedata_p.h"
#include <QtQuick3DUtils/private/qssgutils_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrendertexturedata_p.h>
QT_BEGIN_NAMESPACE
/*!
\qmltype TextureData
\inherits Object3D
\inqmlmodule QtQuick3D
\nativetype QQuick3DTextureData
\brief Base type for custom texture data.
Custom texture data allows using application-generated texture data, that
can possibly change dynamically as well. To use custom texture data set the
\l{Texture::textureData}{textureData} property of \l {Texture} to reference
a TextureData object.
Custom Texture data is implemented in C++ by creating a QQuick3DTextureData
instance, often subclassing it. The QQuick3DTextureData type is registered to
QML under the name of TextureData. Once the subclass is registered to QML,
Texture objects can start referencing it.
An example of when to use this API is when there is a need to procedurally
generate a texture at runtime rather than loading a static image from a file.
\code
import MyCustomTexture 1.0
Model {
source: "#Cube"
materials: [
DefaultMaterial {
diffuseMap: diffuseMapCustomTexture
}
]
}
Texture {
id: diffuseMapCustomTexture
textureData: MyCustomTextureData {
}
}
\endcode
\sa Texture
*/
/*!
\class QQuick3DTextureData
\inmodule QtQuick3D
\inherits QQuick3DObject
\since 6.0
\brief Base class for defining custom texture data.
The QQuick3DTextureData class can be used to specify custom texture data for a
\l {Texture} in a Qt Quick 3D scene.
While not strictly required, the typical usage is to inherit from this class.
The subclass is then exposed to QML by registering it to the type system. The
\l{Texture::textureData}{textureData} property of a \l {QtQuick3D::Texture}{Texture}
can then be set to reference an instance of the registered type.
Example implementation:
\code
class CustomTextureData : public QQuick3DTextureData
{
Q_OBJECT
... properties ...
public:
CustomTextureData() { regenerateTextureData(); }
void setProperty(...)
{
// Change relevant internal data.
// ...
// Update the texture data
regenerateTextureData();
// Finally, trigger an update. This is relevant in case nothing else
// is changed in the scene; this way we make sure a new frame will
// be rendered
update();
}
private:
void regenerateTextureData()
{
QByteArray textureData;
textureData = generateTextureData();
setTextureData(textureData);
setSize(QSize(256, 256));
setFormat(QQuick3DTextureData::Format::RGBA8)
setHasTransparency(true);
}
};
\endcode
This class can then be registered as a QML type and used with \l {QtQuick3D::Texture}{Texture}.
In Qt 5 type registration happened with qmlRegisterType:
\code
qmlRegisterType<MyCustomTextureData>("Example", 1, 0, "MyCustomTextureData");
\endcode
In Qt 6 the default approach is to use automatic registration with the help
of the build system. Instead of calling qmlRegisterType, the \c{.pro} file
can now contain:
\code
CONFIG += qmltypes
QML_IMPORT_NAME = Example
QML_IMPORT_MAJOR_VERSION = 1
\endcode
With CMake, automatic registration is the default behavior, so no special
settings are needed beyond basic QML module setup:
\code
qt_add_qml_module(application
URI Example
VERSION 1.0
)
\endcode
The class implementation should add QML_NAMED_ELEMENT:
\code
class CustomTextureData : public QQuick3DTextureData
{
Q_OBJECT
QML_NAMED_ELEMENT(MyCustomTextureData)
...
};
\endcode
The QML code can then use the custom type:
\code
import Example 1.0
Model {
source: "#Cube"
materials: [
DefaultMaterial {
diffuseMap: diffuseMapCustomTexture
}
]
Texture {
id: diffuseMapCustomTexture
textureData: MyCustomTextureData { }
}
}
\endcode
*/
/*!
\enum QQuick3DTextureData::Format
Returns the color format of the texture data assigned in \l textureData property.
\value None The color format is not defined
\value RGBA8 The color format is considered as 8-bit integer in R, G, B and alpha channels.
\value RGBA16F The color format is considered as 16-bit float in R,G,B and alpha channels.
\value RGBA32F The color format is considered as 32-bit float in R, G, B and alpha channels.
\value RGBE8 The color format is considered as 8-bit mantissa in the R, G, and B channels and 8-bit shared exponent.
\value R8 The color format is considered as 8-bit integer in R channel.
\value R16 The color format is considered as 16-bit integer in R channel.
\value R16F The color format is considered as 16-bit float in R channel.
\value R32F The color format is considered as 32-bit float R channel.
\value BC1 The color format is considred as BC1 compressed format with R, G, B, and alpha channels.
\value BC2 The color format is considred as BC2 compressed format with R, G, B, and alpha channels.
\value BC3 The color format is considred as BC3 compressed format with R, G, B, and alpha channels.
\value BC4 The color format is considred as BC4 compressed format with one color channel.
\value BC5 The color format is considred as BC5 compressed format with two color channels.
\value BC6H The color format is considred as BC6H compressed format with three high dynamic range color channels.
\value BC7 The color format is considred as BC7 compressed format with R, G, B, and alpha channels.
\value DXT1_RGBA The color format is considered as DXT1 compressed format with R, G, B and alpha channels.
\value DXT1_RGB The color format is considered as DXT1 compressed format with R, G and B channels.
\value DXT3_RGBA The color format is considered as DXT3 compressed format with R, G, B and alpha channels.
\value DXT5_RGBA The color format is considered as DXT5 compressed format with R, G, B and alpha channels.
\value ETC2_RGB8 The color format is considered as ETC2 compressed format for RGB888 data
\value ETC2_RGB8A1 The color format is considered as ETC2 compressed format for RGBA data where alpha is 1-bit.
\value ETC2_RGBA8 The color format is considered as ETC2 compressed format with RGBA8888 data.
\value ASTC_4x4 The color format is considered as ASTC compressed format with 4x4 block footprint.
\value ASTC_5x4 The color format is considered as ASTC compressed format with 5x4 block footprint.
\value ASTC_5x5 The color format is considered as ASTC compressed format with 5x5 block footprint.
\value ASTC_6x5 The color format is considered as ASTC compressed format with 6x5 block footprint.
\value ASTC_6x6 The color format is considered as ASTC compressed format with 6x6 block footprint.
\value ASTC_8x5 The color format is considered as ASTC compressed format with 8x5 block footprint.
\value ASTC_8x6 The color format is considered as ASTC compressed format with 8x6 block footprint.
\value ASTC_8x8 The color format is considered as ASTC compressed format with 8x8 block footprint.
\value ASTC_10x5 The color format is considered as ASTC compressed format with 10x5 block footprint.
\value ASTC_10x6 The color format is considered as ASTC compressed format with 10x6 block footprint.
\value ASTC_10x8 The color format is considered as ASTC compressed format with 10x8 block footprint.
\value ASTC_10x10 The color format is considered as ASTC compressed format with 10x10 block footprint.
\value ASTC_12x10 The color format is considered as ASTC compressed format with 12x10 block footprint.
\value ASTC_12x12 The color format is considered as ASTC compressed format with 12x12 block footprint.
\note With the exception of \c RGBA8, not every format is supported at runtime as this
depends on which backend is being used as well which hardware is being used.
\note \c RGBE is internally represented as an \c RGBA8 but is intepreted as described when used
as a lightProbe or skybox texture.
\note Using the value \c None will assume the default value of \c RGBA8
*/
QQuick3DTextureDataPrivate::QQuick3DTextureDataPrivate()
: QQuick3DObjectPrivate(QQuick3DTextureDataPrivate::Type::TextureData)
{
}
QQuick3DTextureData::QQuick3DTextureData(QQuick3DObject *parent)
: QQuick3DObject(*new QQuick3DTextureDataPrivate, parent)
{
}
QQuick3DTextureData::~QQuick3DTextureData()
{
}
/*!
Returns the current texture data defined by this item.
*/
const QByteArray QQuick3DTextureData::textureData() const
{
const Q_D(QQuick3DTextureData);
return d->textureData;
}
/*!
Sets the texture data. The contents of \a data must respect the \l size and
\l format properties as the backend will try and upload and use the data as if
it were a texture of size and format, and if there is any deviation the result
will be somewhere between incorrect rendering of the texture, or potentially a crash.
*/
void QQuick3DTextureData::setTextureData(const QByteArray &data)
{
Q_D(QQuick3DTextureData);
d->textureData = data;
d->textureDataDirty = true;
update();
}
/*!
Returns the size of the texture data in pixels.
*/
QSize QQuick3DTextureData::size() const
{
const Q_D(QQuick3DTextureData);
return d->size;
}
/*!
Sets the \a size of the texture data in pixels.
*/
void QQuick3DTextureData::setSize(const QSize &size)
{
Q_D(QQuick3DTextureData);
d->size = size;
update();
}
/*!
Returns the depth of the texture data in pixels.
*/
int QQuick3DTextureData::depth() const
{
const Q_D(QQuick3DTextureData);
return d->depth;
}
/*!
Sets the \a depth of the texture data in pixels. Setting the depth above
0 means that the texture is handled as a 3D texture.
*/
void QQuick3DTextureData::setDepth(int depth)
{
Q_D(QQuick3DTextureData);
d->depth = depth;
update();
}
/*!
Returns the format of the texture data.
*/
QQuick3DTextureData::Format QQuick3DTextureData::format() const
{
const Q_D(QQuick3DTextureData);
return d->format;
}
/*!
Sets the \a format of the texture data.
The default format is /c RGBA8
*/
void QQuick3DTextureData::setFormat(QQuick3DTextureData::Format format)
{
Q_D(QQuick3DTextureData);
d->format = format;
update();
}
/*!
Returns \c true if the texture data has transparency.
The default value is \c false.
*/
bool QQuick3DTextureData::hasTransparency() const
{
const Q_D(QQuick3DTextureData);
return d->hasTransparency;
}
/*!
Set \a hasTransparency to true if the texture data has an active alpha
channel with non-opaque values.
This is used as an optimization by the engine so that for formats that
do support an alpha channel do not need to have each value checked for
non-opaque values.
*/
void QQuick3DTextureData::setHasTransparency(bool hasTransparency)
{
Q_D(QQuick3DTextureData);
d->hasTransparency = hasTransparency;
update();
}
static QSSGRenderTextureFormat::Format convertToBackendFormat(QQuick3DTextureData::Format format)
{
switch (format) {
case QQuick3DTextureData::None:
case QQuick3DTextureData::RGBA8:
return QSSGRenderTextureFormat::RGBA8;
case QQuick3DTextureData::RGBA16F:
return QSSGRenderTextureFormat::RGBA16F;
case QQuick3DTextureData::RGBA32F:
return QSSGRenderTextureFormat::RGBA32F;
case QQuick3DTextureData::RGBE8:
return QSSGRenderTextureFormat::RGBE8;
case QQuick3DTextureData::R8:
return QSSGRenderTextureFormat::R8;
case QQuick3DTextureData::R16:
return QSSGRenderTextureFormat::R16;
case QQuick3DTextureData::R16F:
return QSSGRenderTextureFormat::R16F;
case QQuick3DTextureData::R32F:
return QSSGRenderTextureFormat::R32F;
case QQuick3DTextureData::BC1:
return QSSGRenderTextureFormat::BC1;
case QQuick3DTextureData::BC2:
return QSSGRenderTextureFormat::BC2;
case QQuick3DTextureData::BC3:
return QSSGRenderTextureFormat::BC3;
case QQuick3DTextureData::BC4:
return QSSGRenderTextureFormat::BC4;
case QQuick3DTextureData::BC5:
return QSSGRenderTextureFormat::BC5;
case QQuick3DTextureData::BC6H:
return QSSGRenderTextureFormat::BC6H;
case QQuick3DTextureData::BC7:
return QSSGRenderTextureFormat::BC7;
case QQuick3DTextureData::DXT1_RGBA:
return QSSGRenderTextureFormat::RGBA_DXT1;
case QQuick3DTextureData::DXT1_RGB:
return QSSGRenderTextureFormat::RGB_DXT1;
case QQuick3DTextureData::DXT3_RGBA:
return QSSGRenderTextureFormat::RGBA_DXT3;
case QQuick3DTextureData::DXT5_RGBA:
return QSSGRenderTextureFormat::RGBA_DXT5;
case QQuick3DTextureData::ETC2_RGB8:
return QSSGRenderTextureFormat::RGB8_ETC2;
case QQuick3DTextureData::ETC2_RGB8A1:
return QSSGRenderTextureFormat::RGB8_PunchThrough_Alpha1_ETC2;
case QQuick3DTextureData::ETC2_RGBA8:
return QSSGRenderTextureFormat::RGBA8_ETC2_EAC;
case QQuick3DTextureData::ASTC_4x4:
return QSSGRenderTextureFormat::RGBA_ASTC_4x4;
case QQuick3DTextureData::ASTC_5x4:
return QSSGRenderTextureFormat::RGBA_ASTC_5x4;
case QQuick3DTextureData::ASTC_5x5:
return QSSGRenderTextureFormat::RGBA_ASTC_5x5;
case QQuick3DTextureData::ASTC_6x5:
return QSSGRenderTextureFormat::RGBA_ASTC_6x5;
case QQuick3DTextureData::ASTC_6x6:
return QSSGRenderTextureFormat::RGBA_ASTC_6x6;
case QQuick3DTextureData::ASTC_8x5:
return QSSGRenderTextureFormat::RGBA_ASTC_8x5;
case QQuick3DTextureData::ASTC_8x6:
return QSSGRenderTextureFormat::RGBA_ASTC_8x6;
case QQuick3DTextureData::ASTC_8x8:
return QSSGRenderTextureFormat::RGBA_ASTC_8x8;
case QQuick3DTextureData::ASTC_10x5:
return QSSGRenderTextureFormat::RGBA_ASTC_10x5;
case QQuick3DTextureData::ASTC_10x6:
return QSSGRenderTextureFormat::RGBA_ASTC_10x6;
case QQuick3DTextureData::ASTC_10x8:
return QSSGRenderTextureFormat::RGBA_ASTC_10x8;
case QQuick3DTextureData::ASTC_10x10:
return QSSGRenderTextureFormat::RGBA_ASTC_10x10;
case QQuick3DTextureData::ASTC_12x10:
return QSSGRenderTextureFormat::RGBA_ASTC_12x10;
case QQuick3DTextureData::ASTC_12x12:
return QSSGRenderTextureFormat::RGBA_ASTC_12x12;
default:
return QSSGRenderTextureFormat::RGBA8;
}
}
/*!
\internal
*/
QSSGRenderGraphObject *QQuick3DTextureData::updateSpatialNode(QSSGRenderGraphObject *node)
{
Q_D(QQuick3DTextureData);
if (!node) {
markAllDirty();
node = new QSSGRenderTextureData();
}
QQuick3DObject::updateSpatialNode(node);
auto *textureData = static_cast<QSSGRenderTextureData*>(node);
bool changed = false;
// Use a dirty flag so we don't compare large buffer values
if (d->textureDataDirty) {
d->textureDataDirty = false;
textureData->setTextureData(d->textureData);
changed = true;
}
// Can't use qUpdateIfNeeded unfortunately
if (d->size != textureData->size()) {
textureData->setSize(d->size);
changed = true;
}
if (d->depth != textureData->depth()) {
textureData->setDepth(d->depth);
changed = true;
}
QSSGRenderTextureFormat format = convertToBackendFormat(d->format);
if (format != textureData->format()) {
textureData->setFormat(format);
changed = true;
}
if (d->hasTransparency != textureData->hasTransparency()) {
textureData->setHasTransparency(d->hasTransparency);
changed = true;
}
if (changed)
emit textureDataNodeDirty();
DebugViewHelpers::ensureDebugObjectName(textureData, this);
return node;
}
void QQuick3DTextureData::markAllDirty()
{
QQuick3DObject::markAllDirty();
}
QT_END_NAMESPACE
|