Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3dshaderutils_p.h
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// Qt-Security score:significant reason:default
4
5
6#ifndef QQUICK3DSHADERUTILS_H
7#define QQUICK3DSHADERUTILS_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists purely as an
14// implementation detail. This header file may change from version to
15// version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include <QtQuick3D/qtquick3dglobal.h>
21#include <QtQuick3D/private/qquick3dobject_p.h>
22#include <QtQuick3D/private/qquick3dtexture_p.h>
23#include <QtQuick3D/private/qquick3dmaterial_p.h>
24#include <QtQuick3D/private/qquick3dshaderimage_p.h>
25#include <QtQuick3D/private/qquick3dshaderstoragebuffer_p.h>
26
27#include <QtQuick3DUtils/private/qssgrenderbasetypes_p.h>
28
29#include <QtQuick3DRuntimeRender/private/qssgrendercommands_p.h>
30
31#include <QtCore/qpointer.h>
32
33#include <functional>
34
35QT_BEGIN_NAMESPACE
36
37class QQuick3DShaderUtilsShader;
38class QQmlContext;
39class QQuick3DRenderPass;
40
41namespace QSSGShaderUtils {
42
44using ResolveFunction = bool (*)(const QUrl &url, const QQmlContext *context, QByteArray &shaderData, QByteArray &shaderPathKey);
46Q_QUICK3D_EXPORT QByteArray resolveShader(const QUrl &fileUrl, const QQmlContext *context, QByteArray &shaderPathKey);
47Q_QUICK3D_EXPORT MetaTypeList supportedMetatypes();
48
49
50template<QMetaType::Type>
52{
53};
54
55Q_QUICK3D_EXPORT QByteArray uniformTypeName(QMetaType type);
56Q_QUICK3D_EXPORT QByteArray uniformTypeName(QSSGRenderShaderValue::Type type);
58// True for a pointer to Texture or one of its subclasses, such as CubeMapTexture.
59Q_QUICK3D_EXPORT bool isTextureType(QMetaType type);
60// The texture held by a property value of such a type, or null.
61Q_QUICK3D_EXPORT QQuick3DTexture *textureFromValue(const QVariant &value);
62// The GLSL sampler type to declare for a texture (sampler2D for a null texture).
63Q_QUICK3D_EXPORT QByteArray samplerTypeName(const QQuick3DTexture *texture);
64}
65
66class Q_QUICK3D_EXPORT QQuick3DShaderUtilsTextureInput : public QQuick3DObject
67{
68 Q_OBJECT
69 Q_PROPERTY(QQuick3DTexture *texture READ texture WRITE setTexture NOTIFY textureChanged)
70 Q_PROPERTY(bool enabled MEMBER enabled NOTIFY enabledChanged)
71
72 QML_NAMED_ELEMENT(TextureInput)
73
74public:
75 explicit QQuick3DShaderUtilsTextureInput(QQuick3DObject *p = nullptr);
76 virtual ~QQuick3DShaderUtilsTextureInput();
77 QQuick3DTexture *m_texture = nullptr;
78 bool enabled = true;
79 QByteArray name;
80 QQuick3DTexture *texture() const
81 {
82 return m_texture;
83 }
84
85public Q_SLOTS:
86 void setTexture(QQuick3DTexture *texture);
87
88Q_SIGNALS:
89 void textureChanged();
90 void enabledChanged();
91};
92
93class Q_QUICK3D_EXPORT QQuick3DShaderUtilsBuffer : public QQuick3DObject
94{
95 Q_OBJECT
96 Q_PROPERTY(TextureFormat format READ format WRITE setFormat NOTIFY changed)
97 Q_PROPERTY(TextureFilterOperation textureFilterOperation READ textureFilterOperation WRITE setTextureFilterOperation NOTIFY changed)
98 Q_PROPERTY(TextureCoordOperation textureCoordOperation READ textureCoordOperation WRITE setTextureCoordOperation NOTIFY changed)
99 Q_PROPERTY(float sizeMultiplier MEMBER sizeMultiplier NOTIFY changed)
100 Q_PROPERTY(AllocateBufferFlagValues bufferFlags READ bufferFlags WRITE setBufferFlags NOTIFY changed)
101 Q_PROPERTY(QByteArray name MEMBER name NOTIFY changed)
102
103 QML_NAMED_ELEMENT(Buffer)
104
105public:
106 QQuick3DShaderUtilsBuffer() = default;
107 ~QQuick3DShaderUtilsBuffer() override = default;
108
109 enum class TextureFilterOperation // must match QSSGRenderTextureFilterOp
110 {
111 Unknown = 0,
112 Nearest,
113 Linear
114 };
115 Q_ENUM(TextureFilterOperation)
116
117 enum class TextureCoordOperation // must match QSSGRenderTextureCoordOp
118 {
119 Unknown = 0,
120 ClampToEdge,
121 MirroredRepeat,
122 Repeat
123 };
124 Q_ENUM(TextureCoordOperation)
125
126 enum class AllocateBufferFlagValues
127 {
128 None = 0,
129 SceneLifetime = 1
130 };
131 Q_ENUM(AllocateBufferFlagValues)
132
133 enum class TextureFormat {
134 Unknown = 0,
135 RGBA8,
136 RGBA16F,
137 RGBA32F,
138 R8,
139 R16,
140 R16F,
141 R32F
142 };
143 Q_ENUM(TextureFormat)
144
145 QSSGAllocateBuffer command {};
146 TextureFilterOperation textureFilterOperation() const { return TextureFilterOperation(command.m_filterOp); }
147 void setTextureFilterOperation(TextureFilterOperation op);
148
149 TextureCoordOperation textureCoordOperation() const { return TextureCoordOperation(command.m_texCoordOp); }
150 void setTextureCoordOperation(TextureCoordOperation texCoordOp);
151 float &sizeMultiplier = command.m_sizeMultiplier;
152 QSSGCommand *cloneCommand() { return new QSSGAllocateBuffer(command); }
153
154 TextureFormat format() const;
155 void setFormat(TextureFormat format);
156
157 AllocateBufferFlagValues bufferFlags() const { return AllocateBufferFlagValues(int(command.m_bufferFlags)); }
158 void setBufferFlags(AllocateBufferFlagValues flag);
159
160 QByteArray &name = command.m_name;
161
162 static QSSGRenderTextureFormat::Format mapTextureFormat(QQuick3DShaderUtilsBuffer::TextureFormat fmt);
163 static QQuick3DShaderUtilsBuffer::TextureFormat mapRenderTextureFormat(QSSGRenderTextureFormat::Format fmt);
164
165Q_SIGNALS:
166 void changed();
167};
168
169class Q_QUICK3D_EXPORT QQuick3DShaderUtilsRenderCommand : public QQuick3DObject
170{
171 Q_OBJECT
172
173 QML_NAMED_ELEMENT(Command)
174
175public:
176 QQuick3DShaderUtilsRenderCommand() = default;
177 ~QQuick3DShaderUtilsRenderCommand() override = default;
178 virtual QSSGCommand *cloneCommand() { Q_ASSERT(0); return nullptr; }
179 virtual int bufferCount() const { return 0; }
180 virtual QQuick3DShaderUtilsBuffer *bufferAt(int idx) const { Q_UNUSED(idx); return nullptr; }
181
182Q_SIGNALS:
183 void changed();
184};
185
186class Q_QUICK3D_EXPORT QQuick3DShaderUtilsBufferInput : public QQuick3DShaderUtilsRenderCommand
187{
188 Q_OBJECT
189 Q_PROPERTY(QQuick3DShaderUtilsBuffer *buffer READ buffer WRITE setBuffer)
190 Q_PROPERTY(QByteArray sampler MEMBER sampler)
191
192 QML_NAMED_ELEMENT(BufferInput)
193
194public:
195 QQuick3DShaderUtilsBufferInput() = default;
196 ~QQuick3DShaderUtilsBufferInput() override = default;
197 QSSGApplyBufferValue command { QByteArray(), QByteArray() };
198 QByteArray &sampler = command.m_samplerName;
199 QSSGCommand *cloneCommand() override { return new QSSGApplyBufferValue(command); }
200
201 int bufferCount() const override { return (m_buffer != nullptr) ? 1 : 0; }
202 QQuick3DShaderUtilsBuffer *bufferAt(int idx) const override
203 {
204 Q_ASSERT(idx < 1 && idx >= 0);
205 return (m_buffer && idx == 0) ? m_buffer : nullptr;
206 }
207
208 QQuick3DShaderUtilsBuffer *buffer() const { return m_buffer; }
209 void setBuffer(QQuick3DShaderUtilsBuffer *buffer) {
210 if (m_buffer == buffer)
211 return;
212
213 if (buffer) {
214 Q_ASSERT(!buffer->name.isEmpty());
215 command.m_bufferName = buffer->name;
216 }
217 m_buffer = buffer;
218 }
219
220 QQuick3DShaderUtilsBuffer *m_buffer = nullptr;
221
222};
223
224class Q_QUICK3D_EXPORT QQuick3DRenderPassTargetBlend
225{
226 Q_GADGET
227 Q_PROPERTY(bool enable MEMBER enable)
228 Q_PROPERTY(ColorMask colorWrite MEMBER colorWrite)
229 Q_PROPERTY(BlendFactor srcColor MEMBER srcColor)
230 Q_PROPERTY(BlendFactor dstColor MEMBER dstColor)
231 Q_PROPERTY(BlendOperation opColor MEMBER opColor)
232 Q_PROPERTY(BlendFactor srcAlpha MEMBER srcAlpha)
233 Q_PROPERTY(BlendFactor dstAlpha MEMBER dstAlpha)
234 Q_PROPERTY(BlendOperation opAlpha MEMBER opAlpha)
235 QML_VALUE_TYPE(renderTargetBlend)
236 QML_ADDED_IN_VERSION(6, 11)
237
238public:
239 enum ColorMaskComponent : quint32 {
240 R = 1 << 0,
241 G = 1 << 1,
242 B = 1 << 2,
243 A = 1 << 3,
244 };
245 Q_DECLARE_FLAGS(ColorMask, ColorMaskComponent)
246
247 enum class BlendFactor {
248 Zero,
249 One,
250 SrcColor,
251 OneMinusSrcColor,
252 DstColor,
253 OneMinusDstColor,
254 SrcAlpha,
255 OneMinusSrcAlpha,
256 DstAlpha,
257 OneMinusDstAlpha,
258 ConstantColor,
259 OneMinusConstantColor,
260 ConstantAlpha,
261 OneMinusConstantAlpha,
262 SrcAlphaSaturate,
263 Src1Color,
264 OneMinusSrc1Color,
265 Src1Alpha,
266 OneMinusSrc1Alpha
267 };
268 Q_ENUM(BlendFactor)
269
270 enum class BlendOperation {
271 Add,
272 Subtract,
273 ReverseSubtract,
274 Min,
275 Max
276 };
277 Q_ENUM(BlendOperation)
278
279 ColorMask colorWrite = ColorMask(0xF); // R | G | B | A
280 bool enable = false;
281 BlendFactor srcColor = BlendFactor::One;
282 BlendFactor dstColor = BlendFactor::OneMinusSrcAlpha;
283 BlendOperation opColor = BlendOperation::Add;
284 BlendFactor srcAlpha = BlendFactor::One;
285 BlendFactor dstAlpha = BlendFactor::OneMinusSrcAlpha;
286 BlendOperation opAlpha = BlendOperation::Add;
287
288 bool operator==(const QQuick3DRenderPassTargetBlend &other) const
289 {
290 return colorWrite == other.colorWrite
291 && enable == other.enable
292 && srcColor == other.srcColor
293 && dstColor == other.dstColor
294 && opColor == other.opColor
295 && srcAlpha == other.srcAlpha
296 && dstAlpha == other.dstAlpha
297 && opAlpha == other.opAlpha;
298 }
299
300 QQuick3DRenderPassTargetBlend() = default;
301 QQuick3DRenderPassTargetBlend(QRhiGraphicsPipeline::TargetBlend targetBlend)
302 : colorWrite(ColorMask(targetBlend.colorWrite.toInt()))
303 , enable(targetBlend.enable)
304 , srcColor(BlendFactor(targetBlend.srcColor))
305 , dstColor(BlendFactor(targetBlend.dstColor))
306 , opColor(BlendOperation(targetBlend.opColor))
307 , srcAlpha(BlendFactor(targetBlend.srcAlpha))
308 , dstAlpha(BlendFactor(targetBlend.dstAlpha))
309 , opAlpha(BlendOperation(targetBlend.opAlpha))
310 {
311 }
312
313 QRhiGraphicsPipeline::TargetBlend toRhiTargetBlend() const
314 {
315 QRhiGraphicsPipeline::TargetBlend tb;
316 tb.colorWrite = QRhiGraphicsPipeline::ColorMask(colorWrite.toInt());
317 tb.enable = enable;
318 tb.srcColor = QRhiGraphicsPipeline::BlendFactor(int(srcColor));
319 tb.dstColor = QRhiGraphicsPipeline::BlendFactor(int(dstColor));
320 tb.opColor = QRhiGraphicsPipeline::BlendOp(int(opColor));
321 tb.srcAlpha = QRhiGraphicsPipeline::BlendFactor(int(srcAlpha));
322 tb.dstAlpha = QRhiGraphicsPipeline::BlendFactor(int(dstAlpha));
323 tb.opAlpha = QRhiGraphicsPipeline::BlendOp(int(opAlpha));
324 return tb;
325 }
326};
327
335
336class Q_QUICK3D_EXPORT QQuick3DShaderUtilsPipelineStateOverride : public QQuick3DShaderUtilsRenderCommand
337{
338 Q_OBJECT
339 Q_PROPERTY(bool depthTestEnabled READ depthTestEnabled WRITE setDepthTestEnabled RESET resetDepthTestEnabled NOTIFY depthTestEnabledChanged)
340 Q_PROPERTY(bool depthWriteEnabled READ depthWriteEnabled WRITE setDepthWriteEnabled RESET resetDepthWriteEnabled NOTIFY depthWriteEnabledChanged)
341 Q_PROPERTY(bool blendEnabled READ blendEnabled WRITE setBlendEnabled RESET resetBlendEnabled NOTIFY blendEnabledChanged FINAL)
342 Q_PROPERTY(bool usesStencilReference READ usesStencilReference WRITE setUsesStencilReference RESET resetUsesStencilReference NOTIFY usesStencilReferenceChanged)
343 Q_PROPERTY(bool usesScissor READ usesScissor WRITE setUsesScissor RESET resetUsesScissor NOTIFY usesScissorChanged)
344 Q_PROPERTY(CompareOperation depthFunction READ depthFunction WRITE setDepthFunction RESET resetDepthFunction NOTIFY depthFunctionChanged)
345 Q_PROPERTY(CullMode cullMode READ cullMode WRITE setCullMode RESET resetCullMode NOTIFY cullModeChanged)
346 Q_PROPERTY(PolygonMode polygonMode READ polygonMode WRITE setPolygonMode RESET resetPolygonMode NOTIFY polygonModeChanged)
347 Q_PROPERTY(quint32 stencilWriteMask READ stencilWriteMask WRITE setStencilWriteMask RESET resetStencilWriteMask NOTIFY stencilWriteMaskChanged)
348 Q_PROPERTY(quint32 stencilReference READ stencilReference WRITE setStencilReference RESET resetStencilReference NOTIFY stencilReferenceChanged)
349 Q_PROPERTY(QRectF viewport READ viewport WRITE setViewport RESET resetViewport NOTIFY viewportChanged)
350 Q_PROPERTY(QRect scissor READ scissor WRITE setScissor RESET resetScissor NOTIFY scissorChanged)
351 Q_PROPERTY(QQuick3DRenderPassTargetBlend targetBlend0 READ targetBlend0 WRITE setTargetBlend0 RESET resetTargetBlend0 NOTIFY targetBlend0Changed)
352 Q_PROPERTY(QQuick3DRenderPassTargetBlend targetBlend1 READ targetBlend1 WRITE setTargetBlend1 RESET resetTargetBlend1 NOTIFY targetBlend1Changed)
353 Q_PROPERTY(QQuick3DRenderPassTargetBlend targetBlend2 READ targetBlend2 WRITE setTargetBlend2 RESET resetTargetBlend2 NOTIFY targetBlend2Changed)
354 Q_PROPERTY(QQuick3DRenderPassTargetBlend targetBlend3 READ targetBlend3 WRITE setTargetBlend3 RESET resetTargetBlend3 NOTIFY targetBlend3Changed)
355 Q_PROPERTY(QQuick3DRenderPassTargetBlend targetBlend4 READ targetBlend4 WRITE setTargetBlend4 RESET resetTargetBlend4 NOTIFY targetBlend4Changed)
356 Q_PROPERTY(QQuick3DRenderPassTargetBlend targetBlend5 READ targetBlend5 WRITE setTargetBlend5 RESET resetTargetBlend5 NOTIFY targetBlend5Changed)
357 Q_PROPERTY(QQuick3DRenderPassTargetBlend targetBlend6 READ targetBlend6 WRITE setTargetBlend6 RESET resetTargetBlend6 NOTIFY targetBlend6Changed)
358 Q_PROPERTY(QQuick3DRenderPassTargetBlend targetBlend7 READ targetBlend7 WRITE setTargetBlend7 RESET resetTargetBlend7 NOTIFY targetBlend7Changed)
359 QML_NAMED_ELEMENT(PipelineStateOverride)
360 QML_ADDED_IN_VERSION(6, 11)
361
362public:
363 enum class CompareOperation {
364 Never,
365 Less,
366 Equal,
367 LessOrEqual,
368 Greater,
369 NotEqual,
370 GreaterOrEqual,
371 Always
372 };
373 Q_ENUM(CompareOperation)
374
375 enum class CullMode {
376 None,
377 Front,
378 Back
379 };
380 Q_ENUM(CullMode)
381
382 enum class PolygonMode {
383 Fill,
384 Line
385 };
386 Q_ENUM(PolygonMode)
387
388 QQuick3DShaderUtilsPipelineStateOverride();
389 ~QQuick3DShaderUtilsPipelineStateOverride() override;
390
391 bool depthTestEnabled() const;
392 void setDepthTestEnabled(bool newDepthTestEnabled);
393 void resetDepthTestEnabled();
394 bool depthWriteEnabled() const;
395 void setDepthWriteEnabled(bool newDepthWriteEnabled);
396 void resetDepthWriteEnabled();
397
398 bool blendEnabled() const;
399 void setBlendEnabled(bool newBlendEnabled);
400 void resetBlendEnabled();
401
402 bool usesStencilReference() const;
403 void setUsesStencilReference(bool newUsesStencilReference);
404 void resetUsesStencilReference();
405
406 bool usesScissor() const;
407 void setUsesScissor(bool newUsesScissor);
408 void resetUsesScissor();
409
410 CompareOperation depthFunction() const;
411 void setDepthFunction(CompareOperation newDepthFunction);
412 void resetDepthFunction();
413
414 CullMode cullMode() const;
415 void setCullMode(CullMode newCullMode);
416 void resetCullMode();
417
418 PolygonMode polygonMode() const;
419 void setPolygonMode(PolygonMode newPolygonMode);
420 void resetPolygonMode();
421
422 quint32 stencilWriteMask() const;
423 void setStencilWriteMask(quint32 newStencilWriteMask);
424 void resetStencilWriteMask();
425
426 quint32 stencilReference() const;
427 void setStencilReference(quint32 newStencilReference);
428 void resetStencilReference();
429
430 QRectF viewport() const;
431 void setViewport(const QRectF &newViewport);
432 void resetViewport();
433
434 QRect scissor() const;
435 void setScissor(const QRect &newScissor);
436 void resetScissor();
437
438 QQuick3DRenderPassTargetBlend targetBlend0() const;
439 void setTargetBlend0(const QQuick3DRenderPassTargetBlend &newTargetBlend0);
440 void resetTargetBlend0();
441
442 QQuick3DRenderPassTargetBlend targetBlend1() const;
443 void setTargetBlend1(const QQuick3DRenderPassTargetBlend &newTargetBlend1);
444 void resetTargetBlend1();
445
446 QQuick3DRenderPassTargetBlend targetBlend2() const;
447 void setTargetBlend2(const QQuick3DRenderPassTargetBlend &newTargetBlend2);
448 void resetTargetBlend2();
449
450 QQuick3DRenderPassTargetBlend targetBlend3() const;
451 void setTargetBlend3(const QQuick3DRenderPassTargetBlend &newTargetBlend3);
452 void resetTargetBlend3();
453
454 QQuick3DRenderPassTargetBlend targetBlend4() const;
455 void setTargetBlend4(const QQuick3DRenderPassTargetBlend &newTargetBlend4);
456 void resetTargetBlend4();
457
458 QQuick3DRenderPassTargetBlend targetBlend5() const;
459 void setTargetBlend5(const QQuick3DRenderPassTargetBlend &newTargetBlend5);
460 void resetTargetBlend5();
461
462 QQuick3DRenderPassTargetBlend targetBlend6() const;
463 void setTargetBlend6(const QQuick3DRenderPassTargetBlend &newTargetBlend6);
464 void resetTargetBlend6();
465
466 QQuick3DRenderPassTargetBlend targetBlend7() const;
467 void setTargetBlend7(const QQuick3DRenderPassTargetBlend &newTargetBlend7);
468 void resetTargetBlend7();
469
470Q_SIGNALS:
471 void depthTestEnabledChanged();
472 void depthWriteEnabledChanged();
473 void blendEnabledChanged();
474 void usesStencilReferenceChanged();
475 void usesScissorChanged();
476 void depthFunctionChanged();
477 void cullModeChanged();
478 void polygonModeChanged();
479 void stencilWriteMaskChanged();
480 void stencilReferenceChanged();
481 void viewportChanged();
482 void scissorChanged();
483 void targetBlend0Changed();
484 void targetBlend1Changed();
485 void targetBlend2Changed();
486 void targetBlend3Changed();
487 void targetBlend4Changed();
488 void targetBlend5Changed();
489 void targetBlend6Changed();
490 void targetBlend7Changed();
491
492private:
493 QSSGCommand *cloneCommand() override { return new QSSGPipelineStateOverrideCommand(command); }
494 QSSGPipelineStateOverrideCommand command { };
495};
496
497
498class Q_QUICK3D_EXPORT QQuick3DShaderUtilsApplyValue : public QQuick3DShaderUtilsRenderCommand
499{
500 Q_OBJECT
501 Q_PROPERTY(QByteArray target MEMBER target)
502 Q_PROPERTY(QVariant value MEMBER value)
503
504 QML_NAMED_ELEMENT(SetUniformValue)
505
506public:
507 QQuick3DShaderUtilsApplyValue() = default;
508 ~QQuick3DShaderUtilsApplyValue() override = default;
509 QSSGCommand *cloneCommand() override { return new QSSGApplyValue(command); }
510 QSSGApplyValue command { };
511 QVariant &value = command.m_value;
512 QByteArray &target = command.m_propertyName;
513};
514
515class Q_QUICK3D_EXPORT QQuick3DShaderUtilsRenderablesFilter : public QQuick3DShaderUtilsRenderCommand
516{
517 Q_OBJECT
518
519 Q_PROPERTY(quint32 layerMask MEMBER layerMask)
520 Q_PROPERTY(RenderableTypes renderableTypes READ renderableTypes WRITE setRenderableTypes)
521 QML_NAMED_ELEMENT(RenderablesFilter)
522 QML_ADDED_IN_VERSION(6, 11)
523
524public:
525 enum class RenderableType : quint32 {
526 None = 0x0,
527 Opaque = 0x1,
528 Transparent = 0x2,
529 };
530 Q_DECLARE_FLAGS(RenderableTypes, RenderableType)
531 Q_FLAG(RenderableTypes)
532
533 QQuick3DShaderUtilsRenderablesFilter() = default;
534 ~QQuick3DShaderUtilsRenderablesFilter() override;
535
536 RenderableTypes renderableTypes() const;
537 void setRenderableTypes(RenderableTypes types);
538
539 QSSGCommand *cloneCommand() override { return new QSSGRenderablesFilterCommand(command); }
540 QSSGRenderablesFilterCommand command { };
541
542 quint32 &layerMask = command.layerMask;
543};
544
545class Q_QUICK3D_EXPORT QQuick3DShaderUtilsRenderPassTexture : public QQuick3DObject
546{
547 Q_OBJECT
548 Q_PROPERTY(TextureFormat format READ format WRITE setFormat FINAL)
549 QML_NAMED_ELEMENT(RenderPassTexture)
550 QML_ADDED_IN_VERSION(6, 11)
551
552public:
553 enum class TextureFormat {
554 Unknown = 0,
555 RGBA8,
556 RGBA16F,
557 RGBA32F,
558 R8,
559 R16,
560 R16F,
561 R32F,
562 Depth16,
563 Depth24,
564 Depth32,
565 Depth24Stencil8,
566 };
567 Q_ENUM(TextureFormat)
568
569 explicit QQuick3DShaderUtilsRenderPassTexture(QQuick3DObject *parent = nullptr);
570 ~QQuick3DShaderUtilsRenderPassTexture() override;
571
572 TextureFormat format() const;
573 void setFormat(TextureFormat newFormat);
574
575 // The backend's allocate command, or null until the object has been
576 // synchronized into a scene. Only valid on the render thread.
577 QSSGAllocateTexturePtr command() const;
578
579protected:
580 QSSGRenderGraphObject *updateSpatialNode(QSSGRenderGraphObject *node) override;
581
582private:
583 static QSSGRenderTextureFormat asRenderTextureFormat(QQuick3DShaderUtilsRenderPassTexture::TextureFormat fmt);
584
585 TextureFormat m_format = TextureFormat::RGBA8;
586};
587
588// Class for user defined color attachments in render passes
589class Q_QUICK3D_EXPORT QQuick3DShaderUtilsRenderPassColorAttachment : public QQuick3DShaderUtilsRenderCommand
590{
591 Q_OBJECT
592 Q_PROPERTY(QQuick3DShaderUtilsRenderPassTexture *target READ target WRITE setTarget)
593 Q_PROPERTY(QByteArray name READ name WRITE setName FINAL)
594 QML_NAMED_ELEMENT(ColorAttachment)
595 QML_ADDED_IN_VERSION(6, 11)
596
597public:
598 QQuick3DShaderUtilsRenderPassColorAttachment() = default;
599 ~QQuick3DShaderUtilsRenderPassColorAttachment() override;
600
601 QByteArray name() const;
602 void setName(const QByteArray &newName);
603
604 QQuick3DShaderUtilsRenderPassTexture *target() const { return m_target; }
605 void setTarget(QQuick3DShaderUtilsRenderPassTexture *target);
606
607 QSSGCommand *cloneCommand() override;
608
609protected:
610 void itemChange(ItemChange change, const ItemChangeData &value) override;
611
612private:
613 void updateSceneManager(QQuick3DSceneManager *sceneManager);
614
615 QQuick3DShaderUtilsRenderPassTexture *m_target = nullptr;
616 QByteArray m_name;
617};
618
619class Q_QUICK3D_EXPORT QQuick3DShaderUtilsRenderPassDepthTextureAttachment : public QQuick3DShaderUtilsRenderCommand
620{
621 Q_OBJECT
622 Q_PROPERTY(QQuick3DShaderUtilsRenderPassTexture *target READ target WRITE setTarget)
623 QML_NAMED_ELEMENT(DepthTextureAttachment)
624 QML_ADDED_IN_VERSION(6, 11)
625
626public:
627 QQuick3DShaderUtilsRenderPassDepthTextureAttachment() = default;
628 ~QQuick3DShaderUtilsRenderPassDepthTextureAttachment() override;
629
630 QQuick3DShaderUtilsRenderPassTexture *target() const { return m_target; }
631 void setTarget(QQuick3DShaderUtilsRenderPassTexture *target);
632
633 QSSGCommand *cloneCommand() override;
634
635protected:
636 void itemChange(ItemChange change, const ItemChangeData &value) override;
637
638private:
639 void updateSceneManager(QQuick3DSceneManager *sceneManager);
640
641 QQuick3DShaderUtilsRenderPassTexture *m_target = nullptr;
642};
643
644class Q_QUICK3D_EXPORT QQuick3DShaderUtilsRenderPassDepthStencilAttachment : public QQuick3DShaderUtilsRenderCommand
645{
646 Q_OBJECT
647 QML_NAMED_ELEMENT(DepthStencilAttachment)
648 QML_ADDED_IN_VERSION(6, 11)
649
650public:
651 QQuick3DShaderUtilsRenderPassDepthStencilAttachment() = default;
652 ~QQuick3DShaderUtilsRenderPassDepthStencilAttachment() override;
653
654 QSSGCommand *cloneCommand() override;
655
656};
657
658class Q_QUICK3D_EXPORT QQuick3DShaderUtilsRenderPassAddDefine : public QQuick3DShaderUtilsRenderCommand
659{
660 Q_OBJECT
661 Q_PROPERTY(QByteArray name READ name WRITE setName NOTIFY nameChanged)
662 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
663 QML_NAMED_ELEMENT(AddDefine)
664 QML_ADDED_IN_VERSION(6, 11)
665
666public:
667 QQuick3DShaderUtilsRenderPassAddDefine();
668 ~QQuick3DShaderUtilsRenderPassAddDefine() override;
669
670 QSSGCommand *cloneCommand() override;
671
672 QByteArray name() const;
673 void setName(const QByteArray &newName);
674
675 int value() const;
676 void setValue(int newValue);
677
678 QSSGAddShaderDefine command;
679
680Q_SIGNALS:
681 void nameChanged();
682 void valueChanged();
683};
684
685class Q_QUICK3D_EXPORT QQuick3DShaderUtilsSubRenderPass : public QQuick3DShaderUtilsRenderCommand
686{
687 Q_OBJECT
688 Q_PROPERTY(QQuick3DRenderPass *renderPass READ renderPass WRITE setRenderPass NOTIFY renderPassChanged FINAL)
689 QML_NAMED_ELEMENT(SubRenderPass)
690 QML_ADDED_IN_VERSION(6, 11)
691
692public:
693 QQuick3DShaderUtilsSubRenderPass() = default;
694 ~QQuick3DShaderUtilsSubRenderPass() override;
695
696 QQuick3DRenderPass *renderPass() const;
697 void setRenderPass(QQuick3DRenderPass *newRenderPass);
698
699 QSSGCommand *cloneCommand() override;
700
701signals:
702 void renderPassChanged();
703
704protected:
705 virtual QSSGRenderGraphObject *updateSpatialNode(QSSGRenderGraphObject *node) final;
706 virtual void itemChange(ItemChange change, const ItemChangeData &value) final;
707
708private:
709 void updateSceneManager(QQuick3DSceneManager *sceneManager);
710
711 QPointer<QQuick3DRenderPass> m_renderPass;
712 mutable bool m_hasWarnedAboutInvalidId = false;
713};
714
715class Q_QUICK3D_EXPORT QQuick3DShaderUtilsRenderPass : public QQuick3DObject
716{
717 Q_OBJECT
718 Q_PROPERTY(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> commands READ commands FINAL)
719 Q_PROPERTY(QQuick3DShaderUtilsBuffer *output MEMBER outputBuffer)
720 Q_PROPERTY(QQmlListProperty<QQuick3DShaderUtilsShader> shaders READ shaders)
721 QML_NAMED_ELEMENT(Pass)
722 QML_ADDED_IN_VERSION(6, 11)
723
724public:
725 QQuick3DShaderUtilsRenderPass() = default;
726 ~QQuick3DShaderUtilsRenderPass() override;
727
728 static void qmlAppendCommand(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list, QQuick3DShaderUtilsRenderCommand *command);
729 static QQuick3DShaderUtilsRenderCommand *qmlCommandAt(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list, qsizetype index);
730 static qsizetype qmlCommandCount(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list);
731 static void qmlCommandClear(QQmlListProperty<QQuick3DShaderUtilsRenderCommand> *list);
732
733 static void qmlAppendShader(QQmlListProperty<QQuick3DShaderUtilsShader> *list, QQuick3DShaderUtilsShader *shader);
734 static QQuick3DShaderUtilsShader *qmlShaderAt(QQmlListProperty<QQuick3DShaderUtilsShader> *list, qsizetype index);
735 static qsizetype qmlShaderCount(QQmlListProperty<QQuick3DShaderUtilsShader> *list);
736 static void qmlShaderClear(QQmlListProperty<QQuick3DShaderUtilsShader> *list);
737
738 QQmlListProperty<QQuick3DShaderUtilsRenderCommand> commands();
739 QVector<QQuick3DShaderUtilsRenderCommand *> m_commands;
740 QQuick3DShaderUtilsBuffer *outputBuffer = nullptr;
741 QQmlListProperty<QQuick3DShaderUtilsShader> shaders();
742 QVarLengthArray<QQuick3DShaderUtilsShader *, 2> m_shaders;
743
744Q_SIGNALS:
745 void changed();
746};
747
748class Q_QUICK3D_EXPORT QQuick3DShaderUtilsShader : public QQuick3DObject
749{
750 Q_OBJECT
751 Q_PROPERTY(QUrl shader MEMBER shader NOTIFY shaderChanged)
752 Q_PROPERTY(Stage stage MEMBER stage NOTIFY stageChanged)
753
754 QML_NAMED_ELEMENT(Shader)
755
756public:
757 QQuick3DShaderUtilsShader() = default;
758 virtual ~QQuick3DShaderUtilsShader() = default;
759 enum class Stage : quint8
760 {
761 Vertex = 0,
762 Fragment = 1
763 };
764 Q_ENUM(Stage)
765
766 QUrl shader;
767 Stage stage = Stage::Fragment;
768
769Q_SIGNALS:
770 void shaderChanged();
771 void stageChanged();
772};
773
774template<typename T, typename std::enable_if_t<std::is_base_of_v<QQuick3DObject, T>, bool> = true>
776{
777public:
779
780 static const char *superClassName()
781 {
782 return T::staticMetaObject.className();
783 }
784};
785
786class QQuick3DPropertyChangedTracker;
787
789{
791public:
793
794public Q_SLOTS:
797
798private:
799 QQuick3DPropertyChangedTracker *m_tracker = nullptr;
800 QMetaProperty m_property;
801};
802
803class Q_QUICK3D_EXPORT QQuick3DPropertyChangedTracker
804{
805public:
806 enum class DirtyPropertyHint
807 {
808 Value,
809 Reference,
810 };
811
812 template<typename T>
813 explicit QQuick3DPropertyChangedTracker(QQuick3DObject *owner, QQuick3DSuperClassInfo<T> info)
814 : m_owner(owner)
815 , m_superClassName(info.superClassName())
816 {
817 Q_ASSERT(m_owner != nullptr);
818 }
819 virtual ~QQuick3DPropertyChangedTracker();
820
821 using UniformProperty = QSSGUserShaderAugmentation::Property;
822 using UniformPropertyList = QVector<UniformProperty>;
823protected:
824 friend class QQuick3DPropertyWatcher;
825
826 // Will re-use already extracted list and update dirty properties
827 QList<UniformProperty> extractProperties();
828
829 void addPropertyWatcher(QMetaProperty property, DirtyPropertyHint hint, QQuick3DObject *object = nullptr);
830 void addDirtyProperty(int pid);
831 void updateTrackedSceneManager(QQuick3DSceneManager *sceneManager);
832
833 virtual void markTrackedPropertyDirty(QMetaProperty property, DirtyPropertyHint hint) = 0;
834
835 struct Tracked
836 {
837 QQuick3DPropertyWatcher *watcher = nullptr;
838 QPointer<QQuick3DObject> object;
839 int pid = -1;
840 QPointer<QQuick3DSceneManager> sceneManager;
841 QMetaObject::Connection destroyedConnection;
842 };
843
844 QHash<int, Tracked> m_trackedProperties;
845 QQuick3DObject *m_owner = nullptr;
846 const char *m_superClassName = nullptr;
847 QList<UniformProperty> m_propertyList; // always sorted by propertyIndex
848
849private:
850 // Watchers on the objects a texture property refers to (TextureInput, Texture and its
851 // texture provider). Rebound whenever the referenced object changes so that textures
852 // assigned after the first extraction are tracked as well.
853 struct TextureWatch
854 {
855 QPointer<QQuick3DShaderUtilsTextureInput> textureInput;
856 QPointer<QQuick3DTexture> texture;
857 QMetaObject::Connection textureInputConnections[2];
858 QMetaObject::Connection textureConnections[2];
859 QMetaObject::Connection textureProviderConnection;
860 };
861
862 std::optional<UniformProperty> createOrUpdateTrackedProperty(const QMetaProperty property, bool addWatchers);
863 void markPropertyDirty(QMetaProperty property, DirtyPropertyHint hint);
864 void watchTextureInput(QMetaProperty property, QQuick3DShaderUtilsTextureInput *textureInput);
865 void watchTexture(QMetaProperty property, QQuick3DTexture *texture);
866 void watchTextureProvider(QMetaProperty property, QQuick3DTexture *texture);
867
868 QHash<int, TextureWatch> m_textureWatchers;
869 std::vector<int> m_dirtyProperties;
870 bool m_extracted = false;
871};
872
873class Q_QUICK3D_EXPORT QQuick3DShaderUtilsCommandDispatch : public QQuick3DShaderUtilsRenderCommand
874{
875 Q_OBJECT
876 Q_PROPERTY(int groupCountX READ groupCountX WRITE setGroupCountX NOTIFY groupCountXChanged FINAL)
877 Q_PROPERTY(int groupCountY READ groupCountY WRITE setGroupCountY NOTIFY groupCountYChanged FINAL)
878 Q_PROPERTY(int groupCountZ READ groupCountZ WRITE setGroupCountZ NOTIFY groupCountZChanged FINAL)
879 QML_NAMED_ELEMENT(Dispatch)
880 QML_ADDED_IN_VERSION(6, 13)
881
882public:
883 QQuick3DShaderUtilsCommandDispatch() = default;
884 ~QQuick3DShaderUtilsCommandDispatch() override = default;
885 int groupCountX() const { return m_groupCount.x; };
886 int groupCountY() const { return m_groupCount.y; };
887 int groupCountZ() const { return m_groupCount.z; };
888 QSSGCommand *cloneCommand() override;
889public Q_SLOTS:
890 void setGroupCountX(int groupCountX);
891 void setGroupCountY(int groupCountY);
892 void setGroupCountZ(int groupCountZ);
893Q_SIGNALS:
894 void groupCountXChanged();
895 void groupCountYChanged();
896 void groupCountZChanged();
897private:
898 using ivec3 = QSSGRenderShaderValue::ivec3;
899 ivec3 m_groupCount = {1, 1, 1};
900};
901
902namespace QSSGShaderUtils {
903
904bool Q_QUICK3D_EXPORT handleBufferProperty(QQuick3DShaderStorageBuffer *buffer, const QByteArray &name, int i,
907
908bool Q_QUICK3D_EXPORT handleImageProperty(QQuick3DShaderImage *image, const QByteArray &name, int i,
911
912QByteArray Q_QUICK3D_EXPORT toShaderFormat(QQuick3DShaderImage::ImageFormat imageFormat);
913QByteArray Q_QUICK3D_EXPORT toShaderImageType(QQuick3DShaderImage::ImageShape shape,
915
916bool Q_QUICK3D_EXPORT resolveResourceProperties(const QObject *owner, const QList<QSSGBaseTypeProperty> &properties,
918void Q_QUICK3D_EXPORT updateResourceProperties(const QObject *owner, const QList<QSSGBaseTypeProperty> &properties);
919}
920
921class Q_QUICK3D_EXPORT QQuick3DShaderResourceTracker
922{
923public:
924 QQuick3DShaderResourceTracker(QQuick3DObject *owner, std::function<void()> onResourceChanged);
925 ~QQuick3DShaderResourceTracker();
926 Q_DISABLE_COPY_MOVE(QQuick3DShaderResourceTracker)
927
928 static QQuick3DObject *resourceForProperty(const QObject *owner, const QMetaProperty &property,
929 bool *isResourceProperty = nullptr);
930
931 void setResources(const QHash<int, QQuick3DObject *> &resources);
932 void trackProperties(int firstPropertyIndex, const QMetaMethod &onPropertyChanged);
933 QList<int> resourceProperties() const { return m_resources.keys(); }
934 bool updateResources();
935 bool hasResourceLayoutChanged() const;
936 void updateSceneManager(QQuick3DSceneManager *sceneManager);
937
938private:
939 struct Reference
940 {
941 int useCount = 0;
942 QPointer<QQuick3DSceneManager> sceneManager;
943 QMetaObject::Connection destroyedConnection;
944 QMetaObject::Connection formatConnection;
945 QMetaObject::Connection shapeConnection;
946 };
947
948 void addReference(QQuick3DObject *object);
949 void removeReference(QQuick3DObject *object);
950 static QByteArray layoutKey(QQuick3DObject *object);
951
952 QQuick3DObject *m_owner = nullptr;
953 std::function<void()> m_onResourceChanged;
954 QHash<int, QQuick3DObject *> m_resources;
955 QHash<int, QByteArray> m_boundLayouts;
956 QHash<QQuick3DObject *, Reference> m_references;
957};
958
959QT_END_NAMESPACE
960
961Q_DECLARE_OPAQUE_POINTER(QQuick3DShaderUtilsTextureInput)
962
963#endif // QQUICK3DSHADERUTILS_H
QQuick3DSuperClassInfo()=default
static const char * superClassName()
\qmltype Shader \inherits QtObject \inqmlmodule QtQuick3D
bool handleBufferProperty(QQuick3DShaderStorageBuffer *buffer, const QByteArray &name, int i, QByteArrayList &metadata, QList< QSSGBaseTypeProperty > &properties)
void updateImageProperty(QQuick3DShaderImage *image, const QSSGBaseTypeProperty &property)
ResolveFunction resolveShaderOverride
QByteArray toShaderImageType(QQuick3DShaderImage::ImageShape shape, QQuick3DShaderImage::ImageFormat imageFormat)
QByteArray toShaderFormat(QQuick3DShaderImage::ImageFormat imageFormat)
QSSGRenderShaderValue::Type uniformType(QMetaType type)
QByteArray uniformTypeName(QMetaType type)
bool(*)(const QUrl &url, const QQmlContext *context, QByteArray &shaderData, QByteArray &shaderPathKey) ResolveFunction
QByteArray uniformTypeName(QSSGRenderShaderValue::Type type)
static constexpr QMetaType::Type qssg_metatype_list[]
void updateBufferProperty(QQuick3DShaderStorageBuffer *buffer, const QSSGBaseTypeProperty &property)
bool handleImageProperty(QQuick3DShaderImage *image, const QByteArray &name, int i, QByteArrayList &metadata, QList< QSSGBaseTypeProperty > &properties)
QByteArray resolveShader(const QUrl &fileUrl, const QQmlContext *context, QByteArray &shaderPathKey)
bool isTextureType(QMetaType type)
bool resolveResourceProperties(const QObject *owner, const QList< QSSGBaseTypeProperty > &properties, QByteArrayList &metadata)
MetaTypeList supportedMetatypes()
QQuick3DTexture * textureFromValue(const QVariant &value)
QByteArray samplerTypeName(const QQuick3DTexture *texture)
void updateResourceProperties(const QObject *owner, const QList< QSSGBaseTypeProperty > &properties)
void setResolveFunction(ResolveFunction fn)
Combined button and popup list for selecting options.