Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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<>
290struct ShaderType<QMetaType::Double>
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<>
386
387template<>
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
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
533
550
555
560
561void QQuick3DShaderUtilsRenderPass::qmlAppendCommand(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list,
563{
564 if (!command)
565 return;
566
567 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
568 that->m_commands.push_back(command);
569}
570
571QQuick3DShaderUtilsRenderCommand *QQuick3DShaderUtilsRenderPass::qmlCommandAt(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list,
573{
574 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
575 return that->m_commands.at(index);
576}
577
578qsizetype QQuick3DShaderUtilsRenderPass::qmlCommandCount(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list)
579{
580 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
581 return that->m_commands.size();
582}
583
584void QQuick3DShaderUtilsRenderPass::qmlCommandClear(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list)
585{
586 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
587 that->m_commands.clear();
588}
589
590QQmlListProperty<QQuick3DShaderUtilsRenderCommand> QQuick3DShaderUtilsRenderPass::commands()
591{
592 return QQmlListProperty<QQuick3DShaderUtilsRenderCommand>(this,
593 nullptr,
598}
599
600void QQuick3DShaderUtilsRenderPass::qmlAppendShader(QQmlListProperty<QQuick3DShaderUtilsShader> *list,
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
630qsizetype QQuick3DShaderUtilsRenderPass::qmlShaderCount(QQmlListProperty<QQuick3DShaderUtilsShader> *list)
631{
632 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
633 return that->m_shaders.size();
634}
635
636void QQuick3DShaderUtilsRenderPass::qmlShaderClear(QQmlListProperty<QQuick3DShaderUtilsShader> *list)
637{
638 QQuick3DShaderUtilsRenderPass *that = qobject_cast<QQuick3DShaderUtilsRenderPass *>(list->object);
639
640 for (QQuick3DShaderUtilsShader *shader : that->m_shaders)
641 shader->disconnect(that);
642
643 that->m_shaders.clear();
644
645 emit that->changed();
646}
647
648QQmlListProperty<QQuick3DShaderUtilsShader> QQuick3DShaderUtilsRenderPass::shaders()
649{
650 return QQmlListProperty<QQuick3DShaderUtilsShader>(this,
651 nullptr,
656}
657
659
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 TextureInput was defined without a CustomMaterial or Effect ancestor. This should be avoided.");
685 }
686
689}
690
\inmodule QtCore
Definition qbytearray.h:57
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
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
\inmodule QtCore
Definition qmetatype.h:341
Type
\macro Q_DECLARE_OPAQUE_POINTER(PointerType)
Definition qmetatype.h:345
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qpoint.h:25
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 \l{QFile}.
Definition qqmlfile.cpp:742
The QQuaternion class represents a quaternion consisting of a vector and scalar.
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)
static void qmlCommandClear(QQmlListProperty< QQuick3DShaderUtilsRenderCommand > *list)
static void qmlShaderClear(QQmlListProperty< QQuick3DShaderUtilsShader > *list)
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:484
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
Definition qsize.h:208
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QByteArray toUtf8() const &
Definition qstring.h:634
\inmodule QtCore
Definition qurl.h:94
QString fileName(ComponentFormattingOptions options=FullyDecoded) const
Definition qurl.cpp:2497
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
QList< QMetaType::Type > MetaTypeList
static constexpr QMetaType::Type qssg_metatype_list[]
ResolveFunction resolveShaderOverride
bool(*)(const QUrl &url, const QQmlContext *context, QByteArray &shaderData, QByteArray &shaderPathKey) ResolveFunction
QByteArray resolveShader(const QUrl &fileUrl, const QQmlContext *context, QByteArray &shaderPathKey)
QSSGRenderShaderValue::Type uniformType(QMetaType type)
void setResolveFunction(ResolveFunction fn)
QByteArray uniformTypeName(QMetaType type)
MetaTypeList supportedMetatypes()
Combined button and popup list for selecting options.
static void * context
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
#define qWarning
Definition qlogging.h:166
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]
#define qPrintable(string)
Definition qstring.h:1531
#define Q_EMIT
#define emit
ptrdiff_t qsizetype
Definition qtypes.h:165
QVideoFrameFormat::PixelFormat fmt
QList< int > list
[14]
QSSGRenderTextureFormat m_format
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()
static constexpr QSSGRenderShaderValue::Type type()