Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qquick3dshaderutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
5
6#include <QtCore/qfile.h>
7#include <QtQml/qqmlcontext.h>
8#include <QtQml/qqmlfile.h>
9
10#include "qquick3dviewport_p.h"
12#include "qquick3deffect_p.h"
13
15
237
239
241{
243}
244
245QByteArray resolveShader(const QUrl &fileUrl, const QQmlContext *context, QByteArray &shaderPathKey)
246{
248 QByteArray shaderData;
249 if (resolveShaderOverride(fileUrl, context, shaderData, shaderPathKey))
250 return shaderData;
251 }
252
253 if (!shaderPathKey.isEmpty())
254 shaderPathKey.append('>');
255
256 const QUrl loadUrl = context ? context->resolvedUrl(fileUrl) : fileUrl;
257 const QString filePath = QQmlFile::urlToLocalFileOrQrc(loadUrl);
258
259 QFile f(filePath);
261 shaderPathKey += loadUrl.fileName().toUtf8();
262 return f.readAll();
263 } else {
264 qWarning("Failed to read shader code from %s", qPrintable(filePath));
265 }
266
267 return QByteArray();
268}
269
270// These are the QMetaTypes that we convert into uniforms.
272 QMetaType::Double,
273 QMetaType::Bool,
274 QMetaType::QVector2D,
275 QMetaType::QVector3D,
276 QMetaType::QVector4D,
277 QMetaType::Int,
278 QMetaType::QColor,
279 QMetaType::QSize,
280 QMetaType::QSizeF,
281 QMetaType::QPoint,
282 QMetaType::QPointF,
283 QMetaType::QRect,
284 QMetaType::QRectF,
285 QMetaType::QQuaternion,
286 QMetaType::QMatrix4x4
287};
288
289template<>
291{
293 static QByteArray name() { return QByteArrayLiteral("float"); }
294};
295
296template<>
298{
300 static QByteArray name() { return QByteArrayLiteral("bool"); }
301};
302
303template<>
305{
307 static QByteArray name() { return QByteArrayLiteral("int"); }
308};
309
310template<>
312{
314 static QByteArray name() { return QByteArrayLiteral("vec2"); }
315};
316
317template<>
319{
321 static QByteArray name() { return QByteArrayLiteral("vec3"); }
322};
323
324template<>
326{
328 static QByteArray name() { return QByteArrayLiteral("vec4"); }
329};
330
331template<>
333{
335 static QByteArray name() { return QByteArrayLiteral("vec4"); }
336};
337
338template<>
340{
342 static QByteArray name() { return QByteArrayLiteral("vec2"); }
343};
344
345template<>
347{
349 static QByteArray name() { return QByteArrayLiteral("vec2"); }
350};
351
352template<>
354{
356 static QByteArray name() { return QByteArrayLiteral("vec2"); }
357};
358
359template<>
361{
363 static QByteArray name() { return QByteArrayLiteral("vec2"); }
364};
365
366template<>
368{
370 static QByteArray name() { return QByteArrayLiteral("vec4"); }
371};
372
373template<>
375{
377 static QByteArray name() { return QByteArrayLiteral("vec4"); }
378};
379
380template<>
382{
384 static QByteArray name() { return QByteArrayLiteral("vec4"); }
385};
386
387template<>
389{
391 static QByteArray name() { return QByteArrayLiteral("mat4"); }
392};
393
395{
396 switch (type.id()) {
397 case QMetaType::Double:
398 case QMetaType::Float:
400 case QMetaType::Bool:
402 case QMetaType::QVector2D:
404 case QMetaType::QVector3D:
406 case QMetaType::QVector4D:
408 case QMetaType::Int:
410 case QMetaType::QColor:
412 case QMetaType::QSize:
414 case QMetaType::QSizeF:
416 case QMetaType::QPoint:
418 case QMetaType::QPointF:
420 case QMetaType::QRect:
422 case QMetaType::QRectF:
424 case QMetaType::QQuaternion:
426 case QMetaType::QMatrix4x4:
428 default:
429 return QByteArray();
430 }
431}
432
434{
435 switch (type) {
466 default:
467 return QByteArray();
468 }
469}
470
472{
473 switch (type.id()) {
474 case QMetaType::Double:
475 case QMetaType::Float:
477 case QMetaType::Bool:
479 case QMetaType::QVector2D:
481 case QMetaType::QVector3D:
483 case QMetaType::QVector4D:
485 case QMetaType::Int:
487 case QMetaType::QColor:
489 case QMetaType::QSize:
491 case QMetaType::QSizeF:
493 case QMetaType::QPoint:
495 case QMetaType::QPointF:
497 case QMetaType::QRect:
499 case QMetaType::QRectF:
501 case QMetaType::QQuaternion:
503 case QMetaType::QMatrix4x4:
505 default:
507 }
508}
509
511{
512 return {std::begin(qssg_metatype_list), std::end(qssg_metatype_list)};
513}
514
515}
516
518{
520 switch (fmt) {
528 default:
529 break;
530 }
532}
533
535{
537 switch (fmt) {
545 default:
546 break;
547 }
549}
550
552{
554}
555
557{
559}
560
563{
564 if (!command)
565 return;
566
567 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
568 that->m_commands.push_back(command);
569}
570
573{
574 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
575 return that->m_commands.at(index);
576}
577
579{
580 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
581 return that->m_commands.size();
582}
583
585{
586 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
587 that->m_commands.clear();
588}
589
591{
593 nullptr,
598}
599
602{
603 if (!shader)
604 return;
605
606 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
607
608 // An append implementation CANNOT rely on the object (shader in this case)
609 // being complete. When the list references a Shader object living under
610 // another Effect, its properties may not be set at the point of this
611 // function being called, so accessing shader->stage is not allowed since
612 // it may still have its default value, not what is set from QML...
613
614 // the only thing we can do is to append to our list, do not try to be clever
615 that->m_shaders.append(shader);
616
619
620 emit that->changed();
621}
622
625{
626 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
627 return that->m_shaders.at(index);
628}
629
631{
632 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
633 return that->m_shaders.size();
634}
635
637{
638 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
639
641 shader->disconnect(that);
642
643 that->m_shaders.clear();
644
645 emit that->changed();
646}
647
649{
651 nullptr,
656}
657
659
661{
662}
663
665{
666 if (m_texture == texture)
667 return;
668
669 QObject *p = parent();
670 while (p != nullptr) {
671 if (QQuick3DCustomMaterial *mat = qobject_cast<QQuick3DCustomMaterial *>(p)) {
672 mat->setDynamicTextureMap(this);
674 break;
675 } else if (QQuick3DEffect *efx = qobject_cast<QQuick3DEffect *>(p)) {
676 efx->setDynamicTextureMap(this);
678 break;
679 }
680 p = p->parent();
681 }
682
683 if (p == nullptr) {
684 qWarning("A texture was defined out of Material or Effect");
685 }
686
689}
690
\inmodule QtCore
Definition qbytearray.h:57
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:106
QByteArray & append(char c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
\inmodule QtCore
Definition qfile.h:93
Definition qlist.h:74
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
\inmodule QtCore
Definition qmetatype.h:320
Type
\macro Q_DECLARE_OPAQUE_POINTER(PointerType)
Definition qmetatype.h:324
\inmodule QtCore
Definition qobject.h:90
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
\inmodule QtCore\reentrant
Definition qpoint.h:214
\inmodule QtCore\reentrant
Definition qpoint.h:23
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to QFile.
Definition qqmlfile.cpp:643
The QQmlListProperty class allows applications to expose list-like properties of QObject-derived clas...
Definition qqmllist.h:24
The QQuaternion class represents a quaternion consisting of a vector and scalar.
Definition qquaternion.h:21
static void attachWatcherPriv(SceneContext *sceneContext, CallContext *callContext, Setter setter, QQuick3DObject *newO, QObject *oldO)
static QQuick3DShaderUtilsBuffer::TextureFormat mapRenderTextureFormat(QSSGRenderTextureFormat::Format fmt)
static QSSGRenderTextureFormat::Format mapTextureFormat(QQuick3DShaderUtilsBuffer::TextureFormat fmt)
void setFormat(TextureFormat format)
QQmlListProperty< QQuick3DShaderUtilsRenderCommand > commands
static qsizetype qmlCommandCount(QQmlListProperty< QQuick3DShaderUtilsRenderCommand > *list)
static QQuick3DShaderUtilsRenderCommand * qmlCommandAt(QQmlListProperty< QQuick3DShaderUtilsRenderCommand > *list, qsizetype index)
static qsizetype qmlShaderCount(QQmlListProperty< QQuick3DShaderUtilsShader > *list)
static void qmlAppendCommand(QQmlListProperty< QQuick3DShaderUtilsRenderCommand > *list, QQuick3DShaderUtilsRenderCommand *command)
QVector< QQuick3DShaderUtilsRenderCommand * > m_commands
static void qmlCommandClear(QQmlListProperty< QQuick3DShaderUtilsRenderCommand > *list)
static void qmlShaderClear(QQmlListProperty< QQuick3DShaderUtilsShader > *list)
QVarLengthArray< QQuick3DShaderUtilsShader *, 2 > m_shaders
static void qmlAppendShader(QQmlListProperty< QQuick3DShaderUtilsShader > *list, QQuick3DShaderUtilsShader *shader)
static QQuick3DShaderUtilsShader * qmlShaderAt(QQmlListProperty< QQuick3DShaderUtilsShader > *list, qsizetype index)
QQmlListProperty< QQuick3DShaderUtilsShader > shaders
QQuick3DShaderUtilsTextureInput(QObject *p=nullptr)
void setTexture(QQuick3DTexture *texture)
\inmodule QtCore\reentrant
Definition qrect.h:483
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
Definition qsize.h:207
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QByteArray toUtf8() const &
Definition qstring.h:563
\inmodule QtCore
Definition qurl.h:94
QString fileName(ComponentFormattingOptions options=FullyDecoded) const
Definition qurl.cpp:2494
constexpr size_type size() const noexcept
const T & at(qsizetype idx) const
void append(const T &t)
The QVector2D class represents a vector or vertex in 2D space.
Definition qvectornd.h:31
The QVector3D class represents a vector or vertex in 3D space.
Definition qvectornd.h:171
The QVector4D class represents a vector or vertex in 4D space.
Definition qvectornd.h:330
\qmltype Shader \inherits Object \inqmlmodule QtQuick3D
QSSGRenderShaderDataType uniformType(QMetaType type)
static constexpr QMetaType::Type qssg_metatype_list[]
ResolveFunction resolveShaderOverride
QByteArray resolveShader(const QUrl &fileUrl, const QQmlContext *context, QByteArray &shaderPathKey)
void setResolveFunction(ResolveFunction fn)
QByteArray uniformTypeName(QMetaType type)
MetaTypeList supportedMetatypes()
bool(*)(const QUrl &url, const QQmlContext *context, QByteArray &shaderData, QByteArray &shaderPathKey) ResolveFunction
Combined button and popup list for selecting options.
static void * context
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
#define qWarning
Definition qlogging.h:162
GLuint index
[2]
GLfloat GLfloat f
GLenum type
GLenum GLuint texture
GLint GLsizei GLsizei GLenum format
GLuint shader
Definition qopenglext.h:665
GLfloat GLfloat p
[1]
QSSGRenderShaderDataType
#define qPrintable(string)
Definition qstring.h:1391
#define Q_EMIT
#define emit
ptrdiff_t qsizetype
Definition qtypes.h:70
QVideoFrameFormat::PixelFormat fmt
QList< int > list
[14]
QSSGRenderTextureFormat m_format
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()
static constexpr QSSGRenderShaderDataType type()