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
qssgrhicontext_p.h
Go to the documentation of this file.
1// Copyright (C) 2023 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 QSSGRHICONTEXT_P_H
7#define QSSGRHICONTEXT_P_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 <QtGui/rhi/qrhi.h>
21
22#include <QtQuick3DRuntimeRender/qtquick3druntimerenderexports.h>
23#include <QtQuick3DUtils/private/qssgrenderbasetypes_p.h>
24#include <QtQuick3DUtils/private/qssgutils_p.h>
25#include <ssg/qssgrhicontext.h>
26#include <QtQuick3DRuntimeRender/private/qssgrhiresourcehandle_p.h>
27
28#include <QtCore/qhash.h>
29#include <QtCore/qvarlengtharray.h>
30
31#include <array>
32
33QT_BEGIN_NAMESPACE
34
35struct QSSGRenderLayer;
36struct QSSGRenderSkyMaterial;
37struct QSSGRenderInstanceTable;
38struct QSSGRenderModel;
39struct QSSGRenderMesh;
40class QSSGRenderGraphObject;
41class QSSGBufferManager;
42class QSSGRenderUserPassBase;
43
45{
47 static const InputAssemblerState &get(const QSSGRhiGraphicsPipelineState &ps) { return ps.ia; }
48 static InputAssemblerState &get(QSSGRhiGraphicsPipelineState &ps) { return ps.ia; }
49};
50
51using QSSGRhiInputAssemblerState = QSSGRhiInputAssemblerStatePrivate::InputAssemblerState;
52
54{
55 static void setShaderPipeline(QSSGRhiGraphicsPipelineState &ps, const QSSGRhiShaderPipeline *pipeline)
56 {
57 ps.shaderPipeline = pipeline;
58 }
59
60 static constexpr const QSSGRhiShaderPipeline *getShaderPipeline(const QSSGRhiGraphicsPipelineState &ps)
61 {
62 return ps.shaderPipeline;
63 }
64};
65
66namespace QSSGRhiHelpers
67{
68
69inline QRhiSampler::Filter toRhi(QSSGRenderTextureFilterOp op)
70{
71 switch (op) {
72 case QSSGRenderTextureFilterOp::Nearest:
73 return QRhiSampler::Nearest;
74 case QSSGRenderTextureFilterOp::Linear:
75 return QRhiSampler::Linear;
76 case QSSGRenderTextureFilterOp::None:
77 return QRhiSampler::Linear;
78 }
79
80 Q_UNREACHABLE_RETURN(QRhiSampler::Linear);
81}
82
83inline QRhiSampler::AddressMode toRhi(QSSGRenderTextureCoordOp tiling)
84{
85 switch (tiling) {
86 case QSSGRenderTextureCoordOp::Repeat:
87 return QRhiSampler::Repeat;
88 case QSSGRenderTextureCoordOp::MirroredRepeat:
89 return QRhiSampler::Mirror;
90 case QSSGRenderTextureCoordOp::ClampToEdge:
91 return QRhiSampler::ClampToEdge;
92 case QSSGRenderTextureCoordOp::Unknown:
93 return QRhiSampler::ClampToEdge;
94 }
95
96 Q_UNREACHABLE_RETURN(QRhiSampler::ClampToEdge);
97}
98
99inline QRhiGraphicsPipeline::CullMode toCullMode(QSSGCullFaceMode cullFaceMode)
100{
101 switch (cullFaceMode) {
102 case QSSGCullFaceMode::Back:
103 return QRhiGraphicsPipeline::Back;
104 case QSSGCullFaceMode::Front:
105 return QRhiGraphicsPipeline::Front;
106 case QSSGCullFaceMode::Disabled:
107 return QRhiGraphicsPipeline::None;
108 case QSSGCullFaceMode::FrontAndBack:
109 qWarning("FrontAndBack cull mode not supported");
110 return QRhiGraphicsPipeline::None;
111 case QSSGCullFaceMode::Unknown:
112 return QRhiGraphicsPipeline::None;
113 }
114
115 Q_UNREACHABLE_RETURN(QRhiGraphicsPipeline::None);
116}
117
118QRhiVertexInputAttribute::Format toVertexInputFormat(QSSGRenderComponentType compType, quint32 numComps);
119QRhiGraphicsPipeline::Topology toTopology(QSSGRenderDrawMode drawMode);
120// Fills out inputLayout.attributes[].location based on
121// inputLayoutInputNames and the provided shader reflection info.
122void bakeVertexInputLocations(QSSGRhiInputAssemblerState *ia, const QSSGRhiShaderPipeline &shaders, int instanceBufferBinding = -1);
123// The type the shaders of a pipeline declare the named sampler with, Sampler2D if none of them does.
124QShaderDescription::VariableType samplerType(const QSSGRhiShaderPipeline &shaders, const QByteArray &name);
125// A dummy texture of the type a sampler is declared with, so that a missing cube map,
126// for example, is not replaced by a 2D texture.
127QRhiTexture *dummyTextureForSampler(QSSGRhiContext *rhiCtx, QShaderDescription::VariableType samplerType,
128 QRhiResourceUpdateBatch *rub, int arraySize = 2,
129 const QSize &size = QSize(64, 64), const QColor &fillColor = Qt::black);
130
131} // namespace QSSGRhiHelpers
132
133inline bool operator==(const QSSGRhiGraphicsPipelineState &a, const QSSGRhiGraphicsPipelineState &b) Q_DECL_NOTHROW
134{
135 const auto &ia_a = QSSGRhiInputAssemblerStatePrivate::get(a);
136 const auto &ia_b = QSSGRhiInputAssemblerStatePrivate::get(b);
137 const auto compareTargetBlend = [](const std::array<QRhiGraphicsPipeline::TargetBlend, 8> &a,
138 const std::array<QRhiGraphicsPipeline::TargetBlend, 8> &b,
139 const int count) -> bool {
140 for (int i = 0; i < count; i++) {
141 if (a[i].colorWrite != b[i].colorWrite
142 || a[i].srcColor != b[i].srcColor
143 || a[i].dstColor != b[i].dstColor
144 || a[i].opColor != b[i].opColor
145 || a[i].srcAlpha != b[i].srcAlpha
146 || a[i].dstAlpha != b[i].dstAlpha
147 || a[i].opAlpha != b[i].opAlpha) {
148 return false;
149 }
150 }
151 return true;
152 };
153 // The viewport and scissor rects are intentionally not compared: they are
154 // dynamic state, never baked into a QRhiGraphicsPipeline, and comparing
155 // them made the pipeline cache key size-dependent, growing the cache with
156 // a new set of pipelines for every window size!
157 return QSSGRhiGraphicsPipelineStatePrivate::getShaderPipeline(a) == QSSGRhiGraphicsPipelineStatePrivate::getShaderPipeline(b)
158 && a.samples == b.samples
159 && a.flags == b.flags
160 && a.stencilRef == b.stencilRef
161 && (std::memcmp(&a.stencilOpFrontState, &b.stencilOpFrontState, sizeof(QRhiGraphicsPipeline::StencilOpState)) == 0)
162 && a.stencilWriteMask == b.stencilWriteMask
163 && a.depthFunc == b.depthFunc
164 && a.cullMode == b.cullMode
165 && a.depthBias == b.depthBias
166 && a.slopeScaledDepthBias == b.slopeScaledDepthBias
167 && ia_a.topology == ia_b.topology
168 && ia_a.inputLayout == ia_b.inputLayout
169 && a.colorAttachmentCount == b.colorAttachmentCount
170 && compareTargetBlend(a.targetBlend, b.targetBlend, a.colorAttachmentCount)
171 && a.lineWidth == b.lineWidth
172 && a.polygonMode == b.polygonMode
173 && a.viewCount == b.viewCount;
174}
175
176inline bool operator!=(const QSSGRhiGraphicsPipelineState &a, const QSSGRhiGraphicsPipelineState &b) Q_DECL_NOTHROW
177{
178 return !(a == b);
179}
180
181inline size_t qHash(const QSSGRhiGraphicsPipelineState &s, size_t seed) Q_DECL_NOTHROW
182{
183 // do not bother with all fields
184 return qHash(QSSGRhiGraphicsPipelineStatePrivate::getShaderPipeline(s), seed)
185 ^ qHash(s.samples)
186 ^ qHash(s.viewCount)
187 ^ qHash(s.targetBlend[0].dstColor)
188 ^ qHash(s.depthFunc)
189 ^ qHash(s.cullMode)
190 ^ qHash(s.colorAttachmentCount)
191 ^ qHash(s.lineWidth)
192 ^ qHash(s.polygonMode)
193 ^ qHashBits(&s.stencilOpFrontState, sizeof(QRhiGraphicsPipeline::StencilOpState))
194 ^ (s.flags)
195 ^ (s.stencilRef << 6)
196 ^ (s.stencilWriteMask << 7);
197}
198
199// The lookup keys can be somewhat complicated due to having to handle cases
200// like "render a model in a shared scene between multiple View3Ds" (here both
201// the View3D ('layer/cid') and the model ('model') act as the lookup key since
202// while the model is the same, we still want different uniform buffers per
203// View3D). 'resource' is the resource the model is drawn with, typically a
204// material; many resources can map to one model. When 'resource' refers to a
205// material it must be the material pointer and nothing else, since
206// cleanupDrawCallDataForResource() finds the entries to release for a
207// to-be-destroyed material by matching on 'resource'. 'mapEntry' is the
208// per-light shadow map entry or per-probe reflection map entry being rendered
209// into, when the draw is part of a shadow or reflection pass, otherwise null.
210// 'resourceIdx' disambiguates multiple entries for the same resource and map
211// entry, e.g. the cube map face and the subset offset.
212//
213struct QSSGRhiDrawCallDataKey
214{
215 const void *cid = nullptr; // Usually the sub-pass (see usage of QSSGPassKey)
216 const void *model = nullptr;
217 const void *resource = nullptr;
218 const void *mapEntry = nullptr;
219 quintptr resourceIdx = 0;
220};
221
222class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRhiBuffer
223{
224 Q_DISABLE_COPY(QSSGRhiBuffer)
225public:
226 QSSGRhiBuffer(QSSGRhiContext &context,
227 QRhiBuffer::Type type,
228 QRhiBuffer::UsageFlags usageMask,
229 quint32 stride,
230 qsizetype size,
231 QRhiCommandBuffer::IndexFormat indexFormat = QRhiCommandBuffer::IndexUInt16);
232
233 virtual ~QSSGRhiBuffer();
234
235 QRhiBuffer *buffer() const { return m_buffer; }
236 quint32 stride() const { return m_stride; }
238 const quint32 sz = quint32(m_buffer->size());
239 Q_ASSERT((sz % m_stride) == 0);
240 return sz / m_stride;
241 }
242 QRhiCommandBuffer::IndexFormat indexFormat() const { return m_indexFormat; }
243
244private:
245 QSSGRhiContext &m_context;
246 QRhiBuffer *m_buffer = nullptr;
247 quint32 m_stride;
248 QRhiCommandBuffer::IndexFormat m_indexFormat;
249};
250
252{
253 char name[64];
255
256private:
257 size_t offset = SIZE_MAX;
258 bool maybeExists = true;
259 friend class QSSGRhiShaderPipeline;
260};
261
263{
264 char name[64];
267
268private:
269 size_t offset = SIZE_MAX;
270 size_t size = 0;
271 bool maybeExists = true;
272 friend class QSSGRhiShaderPipeline;
273};
274
275using QSSGRhiBufferPtr = std::shared_ptr<QSSGRhiBuffer>;
276
277inline bool operator==(const QSSGRhiSamplerDescription &a, const QSSGRhiSamplerDescription &b) Q_DECL_NOTHROW
278{
279 return a.hTiling == b.hTiling && a.vTiling == b.vTiling && a.zTiling == b.zTiling
280 && a.minFilter == b.minFilter && a.magFilter == b.magFilter
281 && a.mipmap == b.mipmap;
282}
283
284inline bool operator!=(const QSSGRhiSamplerDescription &a, const QSSGRhiSamplerDescription &b) Q_DECL_NOTHROW
285{
286 return !(a == b);
287}
288
289struct QSSGRhiTexture
290{
291 QByteArray name;
292 QRhiTexture *texture = nullptr;
293 QSSGRhiSamplerDescription samplerDesc;
294};
295
311
312// these are our current shader limits
313#define QSSG_MAX_NUM_LIGHTS 16
314#define QSSG_MAX_NUM_DIRECTIONAL_LIGHTS 4
315#define QSSG_REDUCED_MAX_NUM_LIGHTS 4
316#define QSSG_REDUCED_MAX_NUM_DIRECTIONAL_LIGHTS 1
317
351
377
381
385
386class QSSGRhiShaderResourceBindingList;
387class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRhiShaderPipeline
388{
389 Q_DISABLE_COPY(QSSGRhiShaderPipeline)
390public:
391 explicit QSSGRhiShaderPipeline(QSSGRhiContext &context) : m_context(context) { }
392
393 QSSGRhiContext &context() const { return m_context; }
394 bool isNull() const { return m_stages.isEmpty(); }
395
396 enum StageFlag {
397 // Indicates that this shaderpipeline object is not going to be used with
398 // a QSSGRhiInputAssemblerState, i.e. bakeVertexInputLocations() will
399 // not be called.
400 UsedWithoutIa = 0x01
401 };
403
404 void addStage(const QRhiShaderStage &stage, StageFlags flags = {});
405 const QRhiShaderStage *cbeginStages() const { return m_stages.cbegin(); }
406 const QRhiShaderStage *cendStages() const { return m_stages.cend(); }
407
409 for (const QRhiShaderStage &s : m_stages) {
410 if (s.type() == QRhiShaderStage::Vertex)
411 return &s;
412 }
413 return nullptr;
414 }
416 for (const QRhiShaderStage &s : m_stages) {
417 if (s.type() == QRhiShaderStage::Fragment)
418 return &s;
419 }
420 return nullptr;
421 }
423 for (const QRhiShaderStage &s : m_stages) {
424 if (s.type() == QRhiShaderStage::Compute)
425 return &s;
426 }
427 return nullptr;
428 }
429
430 int ub0Size() const { return m_ub0Size; }
431 int ub0LightDataOffset() const { return m_ub0NextUBufOffset; }
433
435
436 // This struct is used purely for performance. It is used to quickly store
437 // and index common uniform names using the storeIndex argument in the
438 // setUniform method.
440 {
476 int pointSizeIdx = -1;
483 int fogColorIdx = -1;
491 int viewSize = -1;
492 int samples = -1;
494
501 } commonUniformIndices;
502
504 int transform0 = -1;
505 int transform1 = -1;
506 int transform2 = -1;
507 int color = -1;
508 int data = -1;
509 int index = -1; // qt_instanceIndex, in a binding of its own after the instance buffer
510 } instanceLocations;
511
512 enum class UniformFlag {
513 Mat3 = 0x01
514 };
515 Q_DECLARE_FLAGS(UniformFlags, UniformFlag)
516
517 void setUniformValue(char *ubufData, const char *name, const QVariant &value, QSSGRenderShaderValue::Type type);
518 void setUniform(char *ubufData, const char *name, const void *data, size_t size, int *storeIndex = nullptr, UniformFlags flags = {});
519 void setUniformArray(char *ubufData, const char *name, const void *data, size_t itemCount, QSSGRenderShaderValue::Type type, int *storeIndex = nullptr);
520 int bindingForTexture(const char *name, int hint = -1);
521 int bindingForStorageImage(QByteArrayView name);
522 int bindingForStorageBuffer(QByteArrayView name);
523
524 void setShaderResources(char *ubufData,
525 QSSGBufferManager &theBufferManager,
526 const QByteArray &inPropertyName,
527 const QVariant &propertyValue,
528 QSSGRenderShaderValue::Type inPropertyType);
529
530 void setLightsEnabled(bool enable) { m_lightsEnabled = enable; }
531 bool isLightingEnabled() const { return m_lightsEnabled; }
532
533 void ensureCombinedUniformBuffer(QRhiBuffer **ubuf);
534 void ensureUniformBuffer(QRhiBuffer **ubuf);
535
536 void setLightProbeTexture(QRhiTexture *texture,
537 QSSGRenderTextureCoordOp hTile = QSSGRenderTextureCoordOp::ClampToEdge,
538 QSSGRenderTextureCoordOp vTile = QSSGRenderTextureCoordOp::ClampToEdge)
539 {
540 m_lightProbeTexture = texture; m_lightProbeHorzTile = hTile; m_lightProbeVertTile = vTile;
541 }
542 QRhiTexture *lightProbeTexture() const { return m_lightProbeTexture; }
544 {
545 return {m_lightProbeHorzTile, m_lightProbeVertTile};
546 }
547
548 void setScreenTexture(QRhiTexture *texture) { m_screenTexture = texture; }
549 QRhiTexture *screenTexture() const { return m_screenTexture; }
550
551 void setDepthTexture(QRhiTexture *texture) { m_depthTexture = texture; }
552 QRhiTexture *depthTexture() const { return m_depthTexture; }
553
554 void setNormalTexture(QRhiTexture *texture) { m_normalTexture = texture; }
555 QRhiTexture *normalTexture() const { return m_normalTexture; }
556
557 void setSsaoTexture(QRhiTexture *texture) { m_ssaoTexture = texture; }
558 QRhiTexture *ssaoTexture() const { return m_ssaoTexture; }
559
560 void setLightmapTexture(QRhiTexture *texture) { m_lightmapTexture = texture; }
561 QRhiTexture *lightmapTexture() const { return m_lightmapTexture; }
562
563 void setMotionVectorTexture(QRhiTexture *texture) { m_motionVectorTexture = texture; }
564 QRhiTexture *MotionVectorTexture() const { return m_motionVectorTexture; }
565 void setShadowMapAtlasTexture(QRhiTexture *texture) { m_shadowMapAtlasTexture = texture; }
566 QRhiTexture *shadowMapAtlasTexture() const { return m_shadowMapAtlasTexture; }
567
568 void setShadowMapBlueNoiseTexture(QRhiTexture *texture) { m_shadowMapBlueNoiseTexture = texture; }
569 QRhiTexture *shadowMapBlueNoiseTexture() const { return m_shadowMapBlueNoiseTexture; }
570
571 void setOITImages(QRhiTexture *accumulator, QRhiTexture *auxiliary, QRhiTexture *counter)
572 {
573 m_oitImages[0] = accumulator;
574 m_oitImages[1] = auxiliary;
575 m_oitImages[2] = counter;
576 }
577 QRhiTexture **oitImages() { return m_oitImages; }
578
579 void resetExtraTextures() { m_extraTextures.clear(); }
580 void addExtraTexture(const QSSGRhiTexture &t) { m_extraTextures.append(t); }
581 int extraTextureCount() const { return m_extraTextures.size(); }
582 const QSSGRhiTexture &extraTextureAt(int index) const { return m_extraTextures[index]; }
583 QSSGRhiTexture &extraTextureAt(int index) { return m_extraTextures[index]; }
584
586 {
587 int binding = 0;
590 QRhiBuffer *buffer = nullptr;
593 StorageResourceBinding(int binding, const QByteArray &name, QRhiShaderResourceBinding::Type type, QRhiBuffer *buffer)
594 : binding(binding), name(name), type(type), buffer(buffer), texture(nullptr) {}
595 StorageResourceBinding(int binding, const QByteArray &name, QRhiShaderResourceBinding::Type type, QRhiTexture *texture)
596 : binding(binding), name(name), type(type), buffer(nullptr), texture(texture) {}
597 };
598 using StorageBindingMap = QMap<int, StorageResourceBinding>;
599
600 void addStorageBufferBinding(const QByteArray &name, QRhiShaderResourceBinding::Type type, QRhiBuffer *buffer, int binding);
601 void addStorageImageBinding(const QByteArray &name, QRhiShaderResourceBinding::Type type, QRhiTexture *texture, int binding);
602 void applyAndClearStorageBindings(QRhiShaderResourceBinding::StageFlags stage, QSSGRhiShaderResourceBindingList &bindings);
603 const StorageBindingMap &storageBufferBindings() const { return m_storageBufferBindings; }
604 const StorageBindingMap &storageImageBindings() const { return m_storageImageBindings; }
605
606 QSSGShaderLightsUniformData &lightsUniformData() { return m_lightsUniformData; }
607 QSSGShaderDirectionalLightsUniformData &directionalLightsUniformData() { return m_directionalLightsUniformData; }
608 InstanceLocations instanceBufferLocations() const { return instanceLocations; }
609
610 int offsetOfUniform(const QByteArray &name);
611
612private:
613 QSSGRhiContext &m_context;
614 QVarLengthArray<QRhiShaderStage, 2> m_stages;
615 int m_ub0Size = 0;
616 int m_ub0NextUBufOffset = 0;
617 QHash<QByteArray, QShaderDescription::BlockVariable> m_ub0;
618 QHash<QSSGRhiInputAssemblerState::InputSemantic, QShaderDescription::InOutVariable> m_vertexInputs;
619 QHash<QByteArray, QShaderDescription::InOutVariable> m_combinedImageSamplers;
620 QHash<QByteArray, QShaderDescription::InOutVariable> m_storageImages;
621 QHash<QByteArray, QShaderDescription::StorageBlock> m_storageBuffers;
622 int m_materialImageSamplerBindings[size_t(QSSGRhiSamplerBindingHints::BindingMapSize)];
623
624 QVarLengthArray<QSSGRhiShaderUniform, 32> m_uniforms; // members of the main (binding 0) uniform buffer
625 QVarLengthArray<QSSGRhiShaderUniformArray, 8> m_uniformArrays;
626 QHash<QByteArray, size_t> m_uniformIndex; // Maps uniform name to index in m_uniforms and m_uniformArrays
627
628 // transient (per-object) data; pointers are all non-owning
629 bool m_lightsEnabled = false;
630 QSSGShaderLightsUniformData m_lightsUniformData;
631 QSSGShaderDirectionalLightsUniformData m_directionalLightsUniformData;
632 QRhiTexture *m_shadowMapAtlasTexture = nullptr;
633 QRhiTexture *m_shadowMapBlueNoiseTexture = nullptr;
634 QRhiTexture *m_lightProbeTexture = nullptr;
635 QSSGRenderTextureCoordOp m_lightProbeHorzTile = QSSGRenderTextureCoordOp::ClampToEdge;
636 QSSGRenderTextureCoordOp m_lightProbeVertTile = QSSGRenderTextureCoordOp::ClampToEdge;
637 QRhiTexture *m_screenTexture = nullptr;
638 QRhiTexture *m_depthTexture = nullptr;
639 QRhiTexture *m_normalTexture = nullptr;
640 QRhiTexture *m_ssaoTexture = nullptr;
641 QRhiTexture *m_lightmapTexture = nullptr;
642 QRhiTexture *m_oitImages[3] = {nullptr};
643 QRhiTexture *m_motionVectorTexture = nullptr;
644 QVarLengthArray<QSSGRhiTexture, 8> m_extraTextures;
645
646 QMap<int, StorageResourceBinding> m_storageImageBindings;
647 QMap<int, StorageResourceBinding> m_storageBufferBindings;
648};
649
650Q_DECLARE_OPERATORS_FOR_FLAGS(QSSGRhiShaderPipeline::StageFlags)
651Q_DECLARE_OPERATORS_FOR_FLAGS(QSSGRhiShaderPipeline::UniformFlags)
652
653using QSSGRhiShaderPipelinePtr = std::shared_ptr<QSSGRhiShaderPipeline>;
654
655class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRhiShaderResourceBindingList
656{
657public:
658 static const int MAX_SIZE = 32;
659
660 int p = 0;
661 size_t h = 0;
662 QRhiShaderResourceBinding v[MAX_SIZE];
663
664 void clear() { p = 0; h = 0; }
665
666 QSSGRhiShaderResourceBindingList() { }
667
668 QSSGRhiShaderResourceBindingList(const QSSGRhiShaderResourceBindingList &other)
669 : p(other.p),
670 h(other.h)
671 {
672 for (int i = 0; i < p; ++i)
673 v[i] = other.v[i];
674 }
675
676 QSSGRhiShaderResourceBindingList &operator=(const QSSGRhiShaderResourceBindingList &other) Q_DECL_NOTHROW
677 {
678 if (this != &other) {
679 p = other.p;
680 h = other.h;
681 for (int i = 0; i < p; ++i)
682 v[i] = other.v[i];
683 }
684 return *this;
685 }
686
687 void addUniformBuffer(int binding, QRhiShaderResourceBinding::StageFlags stage, QRhiBuffer *buf, int offset = 0 , int size = 0, bool hasDynamicOffset = false);
688 void addTexture(int binding, QRhiShaderResourceBinding::StageFlags stage, QRhiTexture *tex, QRhiSampler *sampler);
689 void addImageLoad(int binding, QRhiShaderResourceBinding::StageFlags stage, QRhiTexture *tex, int level);
690 void addImageStore(int binding, QRhiShaderResourceBinding::StageFlags stage, QRhiTexture *tex, int level);
691 void addImageLoadStore(int binding, QRhiShaderResourceBinding::StageFlags stage, QRhiTexture *tex, int level);
692 void addStorageBufferLoad(int binding, QRhiShaderResourceBinding::StageFlags stage, QRhiBuffer *buf, int offset = 0 , int size = 0);
693 void addStorageBufferStore(int binding, QRhiShaderResourceBinding::StageFlags stage, QRhiBuffer *buf, int offset = 0 , int size = 0);
694 void addStorageBufferLoadStore(int binding, QRhiShaderResourceBinding::StageFlags stage, QRhiBuffer *buf, int offset = 0 , int size = 0);
695private:
696 template<QRhiShaderResourceBinding::Type T>
697 void fillImage(QRhiShaderResourceBinding::Data *d, int binding, QRhiShaderResourceBinding::StageFlags stage, QRhiTexture *tex, int level);
698 template<QRhiShaderResourceBinding::Type T>
699 void fillStorageBuffer(QRhiShaderResourceBinding::Data *d, int binding, QRhiShaderResourceBinding::StageFlags stage, QRhiBuffer *buf, int offset, int size);
700 QRhiShaderResourceBinding::Data *newBinding();
701};
702
703inline bool operator==(const QSSGRhiShaderResourceBindingList &a, const QSSGRhiShaderResourceBindingList &b) Q_DECL_NOTHROW
704{
705 if (a.h != b.h)
706 return false;
707 if (a.p != b.p)
708 return false;
709 for (int i = 0; i < a.p; ++i) {
710 if (a.v[i] != b.v[i])
711 return false;
712 }
713 return true;
714}
715
716inline bool operator!=(const QSSGRhiShaderResourceBindingList &a, const QSSGRhiShaderResourceBindingList &b) Q_DECL_NOTHROW
717{
718 return !(a == b);
719}
720
721inline size_t qHash(const QSSGRhiShaderResourceBindingList &bl, size_t seed) Q_DECL_NOTHROW
722{
723 return bl.h ^ seed;
724}
725
726struct QSSGRhiDrawCallData
727{
728 QRhiBuffer *ubuf = nullptr; // owned
729 QRhiShaderResourceBindings *srb = nullptr; // not owned
730 QSSGRhiShaderResourceBindingList bindings;
731 QRhiGraphicsPipeline *pipeline = nullptr; // not owned
732 size_t renderTargetDescriptionHash = 0;
733 QVector<quint32> renderTargetDescription;
734 QSSGRhiGraphicsPipelineState ps;
735
736 void reset()
737 {
738 delete ubuf;
739 ubuf = nullptr;
740 srb = nullptr;
741 pipeline = nullptr;
742 }
743};
744
745// A managed rhi texture is a wrapper around a QRhiTexture that is owned by the QSSG RHI context
746// and can be used by the user code without worrying about its lifetime.
747class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGManagedRhiTexture
748{
749 enum class Private { Initialize };
750public:
751 // Valid for as long as the wrapper is, see isValid().
752 QRhiTexture *texture() const { return m_texture; }
753
754 [[nodiscard]] bool isValid() const { return m_texture != nullptr; }
755
756 QSSGManagedRhiTexture() = default;
757 QSSGManagedRhiTexture(QSSGRhiContextPrivate &rhiCtxD, QRhiTexture *texture, Private);
758 ~QSSGManagedRhiTexture();
759
760 QSSGManagedRhiTexture(const QSSGManagedRhiTexture &other);
761 QSSGManagedRhiTexture &operator=(const QSSGManagedRhiTexture &other);
762
763 QSSGManagedRhiTexture(QSSGManagedRhiTexture &&other) noexcept;
764 QSSGManagedRhiTexture &operator=(QSSGManagedRhiTexture &&other) noexcept;
765
766 void invalidate();
767
768private:
769 friend class QSSGRhiContextPrivate;
770
771 // The context owns the QRhiTexture and tracks how many wrappers reference
772 // it. The context clears both pointers when it releases the texture, so a
773 // wrapper that outlives the context has nothing left to do.
774 QSSGRhiContextPrivate *m_rhiCtxD = nullptr;
775 QRhiTexture *m_texture = nullptr;
776};
777
779{
780public:
783
784 void setDescription(QSSGRhiContext &rhiCtx, QRhiTextureRenderTargetDescription rtDesc, QRhiTextureRenderTarget::Flags = {});
785
786 void setName(const QByteArray &name) { rtName = name; }
787 const QByteArray &getName() const { return rtName; }
788
789 // Invalid when the render target has no depth texture.
790 const QSSGManagedRhiTexture &getDepthTexture() const { return depthTexture; }
791 const std::unique_ptr<QRhiRenderBuffer> &getDepthStencil() const { return depthStencil; }
794
795 size_t colorAttachmentCount() const { return textures.size(); }
796 const QSSGManagedRhiTexture &getColorTexture(int index) const { return textures[index]; }
797
798 bool isValid() const { return (textures.size() > 0) && rt && rpDesc; }
799
800 void resetRenderTarget();
801 // Retires the handle for the depth-stencil in registry, if any.
802 void reset(QSSGRhiResourceRegistry *registry);
803
804private:
805 friend class QSSGUserRenderPassManager;
806
807 enum Dirty : quint32
808 {
809 ColorTextureDirty = 0x1,
810 DepthStencilDirty = 0x2,
811 DepthTextureDirty = 0x4,
812 };
813
814 void invalidate(QSSGRhiResourceRegistry *registry);
815 void releaseDepthStencil(QSSGRhiResourceRegistry *registry);
816
818
819 QVarLengthArray<QSSGManagedRhiTexture, 4> textures {};
820 std::unique_ptr<QRhiRenderBuffer> depthStencil;
821 QSSGManagedRhiTexture depthTexture; // either depthStencil or depthTexture are valid, never both
822
823 std::unique_ptr<QRhiRenderPassDescriptor> rpDesc;
824 std::unique_ptr<QRhiTextureRenderTarget> rt;
825 QByteArray rtName;
826 DirtyT dirty = 0;
827};
828
829using QSSGRhiRenderableTextureV2Ptr = std::shared_ptr<QSSGRhiRenderableTextureV2>;
830
831struct Q_AUTOTEST_EXPORT QSSGRhiRenderableTexture
832{
833 QRhiTexture *texture = nullptr;
834 QRhiRenderBuffer *depthStencil = nullptr;
835 QRhiTexture *depthTexture = nullptr; // either depthStencil or depthTexture are valid, never both
836 QRhiRenderPassDescriptor *rpDesc = nullptr;
837 QRhiTextureRenderTarget *rt = nullptr;
838 bool isValid() const { return texture && rpDesc && rt; }
839 void resetRenderTarget() {
840 delete rt;
841 rt = nullptr;
842 delete rpDesc;
843 rpDesc = nullptr;
844 }
845 // Retires the handles for the textures and the depth-stencil in
846 // registry, if any, and releases them with deleteLater().
847 void reset(QSSGRhiResourceRegistry *registry);
848};
849
851{
852 float d = 0.0f;
854};
855
856struct QSSGRenderSubset;
857
858// Where the renderer draws the instances of a model, see
859// QSSGRenderDataHelpers::calcInstanceTransforms(): a point p of the mesh is drawn at
860// global * instance * local * p, where instance is the transform in the instance table.
862{
864 std::array<QVector3D, 3> localAxes; // the columns of the 3x3 rotation and scale part of local
865 QVector3D boundsCenter; // local * the center of the bounds of the subset
866 float boundsRadius = 0.0f; // the radius of the bounds of the subset, in the space of the mesh
867
869 {
870 return a.global == b.global && a.localAxes == b.localAxes
871 && a.boundsCenter == b.boundsCenter
873 }
875 {
876 return !(a == b);
877 }
878};
879
881{
882 QRhiBuffer *buffer = nullptr;
887 // The global instance transform and the translation of the local one that the data was
888 // culled and sorted with, see instanceWorldPosition() in qssglayerrenderdata.cpp.
891 QByteArray lodData; // the instances in the range of lodMin/lodMax, see prepareInstancing()
892 // The row in the table of each instance in lodData, for INSTANCE_INDEX.
894 // The rows of the instances in buffer, for the shaders that read INSTANCE_INDEX, uploaded
895 // again when the data is rebuilt, see QSSGLayerRenderData::prepareInstancing().
898 // Incremented every time the data in buffer is rebuilt, so that the levels of the automatic
899 // per-instance LOD, which are built from it, are rebuilt too, see
900 // SubsetLodData::sourceGeneration.
902 float lodMin = -1.0f;
903 float lodMax = -1.0f;
904 // The table the data was built from, and its generation then.
905 const QSSGRenderInstanceTable *table = nullptr;
907 bool owned = true;
908 bool sorting = false;
909 // Automatic per-instance LOD
910 static constexpr int MaxAutoLodLevels = 12;
912 // The instances of all the levels, level after level, as uploaded to buffer.
913 QRhiBuffer *buffer = nullptr;
915 // The row in the table of each instance in data, for INSTANCE_INDEX, and the buffer they
916 // are uploaded to, for the shaders that read it, again when the levels are rebuilt, see
917 // QSSGLayerRenderData::prepareAutoLodInstancing().
921 quint32 generation = 0; // incremented every time the levels are rebuilt
922 std::array<int, MaxAutoLodLevels> counts {}; // the number of instances of each level
923 int levelCount = 0;
924 // The squared distances at which the levels switch, for an instance drawn with a scale
925 // of 1, that the levels were built with, see
926 // QSSGLayerRenderData::prepareAutoLodInstancing().
930 QMatrix4x4 cameraViewProjection; // fingerprint for frustum change detection
932 // QSSGRhiInstanceBufferData::generation of the copy the levels were built from
934 // The table the levels were built from, and its generation then.
935 const QSSGRenderInstanceTable *table = nullptr;
937 quint32 lastUsedFrame = 0; // QSSGRenderer::frameCount(), see releaseUnusedAutoLodData()
938 bool culled = false;
939 bool orthographic = false; // the camera the levels were built for
940 bool tinted = false;
941 bool valid = false;
942 };
943 // One entry per subset, as subsets may have different LOD counts, and per layer, as each
944 // View3D that draws the model has its own camera and draws in the same frame as the others.
945 // Like the meshes in QSSGBufferManager, an entry is used in a frame when the model is in the
946 // layer, culled or not, and released at the end of a frame of the layer that did not use it.
947 using SubsetLodKey = std::pair<const QSSGRenderSubset *, const QSSGRenderLayer *>;
949};
950
961
963{
964public:
967 struct {
968 size_t srbLayoutDescriptionHash;
969 } extra;
970 static QSSGComputePipelineStateKey create(const QShader &shader,
971 const QRhiShaderResourceBindings *srb)
972 {
973 const QVector<quint32> srbDesc = srb->serializedLayoutDescription();
974 return { shader, srbDesc, { qHash(srbDesc) } };
975 }
976};
977
978inline bool operator==(const QSSGComputePipelineStateKey &a, const QSSGComputePipelineStateKey &b) Q_DECL_NOTHROW
979{
980 return a.shader == b.shader && a.srbLayoutDescription == b.srbLayoutDescription;
981}
982
983inline bool operator!=(const QSSGComputePipelineStateKey &a, const QSSGComputePipelineStateKey &b) Q_DECL_NOTHROW
984{
985 return !(a == b);
986}
987
988inline size_t qHash(const QSSGComputePipelineStateKey &k, size_t seed = 0) Q_DECL_NOTHROW
989{
990 return qHash(k.shader, seed) ^ k.extra.srbLayoutDescriptionHash;
991}
992
993struct QSSGRhiDummyTextureKey
994{
995 QRhiTexture::Flags flags;
996 QSize size;
997 QColor color;
998 int arraySize;
999};
1000
1001inline size_t qHash(const QSSGRhiDummyTextureKey &k, size_t seed) Q_DECL_NOTHROW
1002{
1003 return qHash(k.flags, seed)
1004 ^ qHash(k.size.width() ^ k.size.height() ^ k.color.red() ^ k.color.green()
1005 ^ k.color.blue() ^ k.color.alpha() ^ k.arraySize);
1006}
1007
1008inline bool operator==(const QSSGRhiDummyTextureKey &a, const QSSGRhiDummyTextureKey &b) Q_DECL_NOTHROW
1009{
1010 return a.flags == b.flags && a.size == b.size && a.color == b.color && a.arraySize == b.arraySize;
1011}
1012
1013inline bool operator!=(const QSSGRhiDummyTextureKey &a, const QSSGRhiDummyTextureKey &b) Q_DECL_NOTHROW
1014{
1015 return !(a == b);
1016}
1017
1018class QSSGGraphicsPipelineStateKey
1019{
1020public:
1021 QSSGRhiGraphicsPipelineState state;
1022 QVector<quint32> renderTargetDescription;
1023 QVector<quint32> srbLayoutDescription;
1024 struct {
1025 size_t renderTargetDescriptionHash;
1026 size_t srbLayoutDescriptionHash;
1027 } extra;
1028 static QSSGGraphicsPipelineStateKey create(const QSSGRhiGraphicsPipelineState &state,
1029 const QRhiRenderPassDescriptor *rpDesc,
1030 const QRhiShaderResourceBindings *srb)
1031 {
1032 const QVector<quint32> rtDesc = rpDesc->serializedFormat();
1033 const QVector<quint32> srbDesc = srb->serializedLayoutDescription();
1034 return { state, rtDesc, srbDesc, { qHash(rtDesc), qHash(srbDesc) } };
1035 }
1036};
1037
1038inline bool operator==(const QSSGGraphicsPipelineStateKey &a, const QSSGGraphicsPipelineStateKey &b) Q_DECL_NOTHROW
1039{
1040 return a.state == b.state
1041 && a.renderTargetDescription == b.renderTargetDescription
1042 && a.srbLayoutDescription == b.srbLayoutDescription;
1043}
1044
1045inline bool operator!=(const QSSGGraphicsPipelineStateKey &a, const QSSGGraphicsPipelineStateKey &b) Q_DECL_NOTHROW
1046{
1047 return !(a == b);
1048}
1049
1050inline size_t qHash(const QSSGGraphicsPipelineStateKey &k, size_t seed) Q_DECL_NOTHROW
1051{
1052 return qHash(k.state, seed)
1053 ^ k.extra.renderTargetDescriptionHash
1054 ^ k.extra.srbLayoutDescriptionHash;
1055}
1056
1057#define QSSGRHICTX_STAT(ctx, f)
1058 for (bool qssgrhictxlog_enabled = QSSGRhiContextStats::get(*ctx).isEnabled(); qssgrhictxlog_enabled; qssgrhictxlog_enabled = false)
1059 QSSGRhiContextStats::get(*ctx).f
1060
1061class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRhiContextStats
1062{
1063public:
1064 [[nodiscard]] static QSSGRhiContextStats &get(QSSGRhiContext &rhiCtx);
1065 [[nodiscard]] static const QSSGRhiContextStats &get(const QSSGRhiContext &rhiCtx);
1066
1067 struct DrawInfo {
1068 quint64 callCount = 0;
1069 quint64 vertexOrIndexCount = 0;
1070 };
1071 struct InstancedDrawInfo {
1072 quint64 callCount = 0;
1073 quint64 vertexOrIndexCount = 0;
1074 quint64 instanceCount = 0;
1075 };
1076 struct RenderPassInfo {
1077 QByteArray rtName;
1078 QSize pixelSize;
1079 DrawInfo indexedDraws;
1080 DrawInfo draws;
1081 InstancedDrawInfo instancedIndexedDraws;
1082 InstancedDrawInfo instancedDraws;
1083 };
1084 struct PerLayerInfo {
1085 PerLayerInfo()
1086 {
1087 externalRenderPass.rtName = QByteArrayLiteral("Qt Quick");
1088 }
1089
1090 // The main render pass if renderMode==Offscreen, plus render passes
1091 // for shadow maps, postprocessing effects, etc.
1092 QVector<RenderPassInfo> renderPasses;
1093
1094 // An Underlay/Overlay/Inline renderMode will make the View3D add stuff
1095 // to a render pass managed by Qt Quick. (external == not under the
1096 // control of Qt Quick 3D)
1097 RenderPassInfo externalRenderPass;
1098
1099 int currentRenderPassIndex = -1;
1100 };
1101 struct GlobalInfo { // global as in per QSSGRhiContext which is per-QQuickWindow
1102 quint64 meshDataSize = 0;
1103 quint64 imageDataSize = 0;
1104 qint64 materialGenerationTime = 0;
1105 qint64 effectGenerationTime = 0;
1106 };
1107
1108 QHash<QSSGRenderLayer *, PerLayerInfo> perLayerInfo;
1109 GlobalInfo globalInfo;
1110
1111 QSSGRhiContextStats(QSSGRhiContext &context)
1112 : rhiCtx(&context)
1113 {
1114 }
1115
1116 // The data collected have four consumers:
1117 //
1118 // - Printed on debug output when QSG_RENDERER_DEBUG has the relevant key.
1119 // (this way the debug output from the 2D scenegraph renderer and these 3D
1120 // statistics appear nicely intermixed)
1121 // - Passed on to the QML profiler when profiling is enabled.
1122 // - DebugView via QQuick3DRenderStats.
1123 // - When tracing is enabled
1124 //
1125 // The first two are enabled globally, but DebugView needs a dynamic
1126 // enable/disable since we want to collect data when a DebugView item
1127 // becomes visible, but not otherwise.
1128
1129 static bool profilingEnabled();
1130 static bool rendererDebugEnabled();
1131
1132 bool isEnabled() const;
1133 void drawIndexed(quint32 indexCount, quint32 instanceCount);
1134 void draw(quint32 vertexCount, quint32 instanceCount);
1135
1136 void meshDataSizeChanges(quint64 newSize) // can be called outside start-stop
1137 {
1138 globalInfo.meshDataSize = newSize;
1139 }
1140
1141 void imageDataSizeChanges(quint64 newSize) // can be called outside start-stop
1142 {
1143 globalInfo.imageDataSize = newSize;
1144 }
1145
1146 void registerMaterialShaderGenerationTime(qint64 ms)
1147 {
1148 globalInfo.materialGenerationTime += ms;
1149 }
1150
1151 void registerEffectShaderGenerationTime(qint64 ms)
1152 {
1153 globalInfo.effectGenerationTime += ms;
1154 }
1155
1156 static quint64 totalDrawCallCountForPass(const QSSGRhiContextStats::RenderPassInfo &pass)
1157 {
1158 return pass.draws.callCount
1159 + pass.indexedDraws.callCount
1160 + pass.instancedDraws.callCount
1161 + pass.instancedIndexedDraws.callCount;
1162 }
1163
1164 static quint64 totalVertexCountForPass(const QSSGRhiContextStats::RenderPassInfo &pass)
1165 {
1166 return pass.draws.vertexOrIndexCount
1167 + pass.indexedDraws.vertexOrIndexCount
1168 + pass.instancedDraws.vertexOrIndexCount
1169 + pass.instancedIndexedDraws.vertexOrIndexCount;
1170 }
1171
1172 void start(QSSGRenderLayer *layer);
1173 void stop(QSSGRenderLayer *layer);
1174 void beginRenderPass(QRhiTextureRenderTarget *rt);
1175 void endRenderPass();
1176 void printRenderPass(const RenderPassInfo &rp);
1177 void cleanupLayerInfo(QSSGRenderLayer *layer);
1178
1179 QSSGRhiContext *rhiCtx;
1180 QSSGRenderLayer *layerKey = nullptr;
1181 QSet<QSSGRenderLayer *> dynamicDataSources;
1182};
1183
1184class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRhiContextPrivate
1185{
1186 Q_DECLARE_PUBLIC(QSSGRhiContext)
1187
1188 explicit QSSGRhiContextPrivate(QSSGRhiContext &rhiCtx, QRhi *rhi_)
1189 : q_ptr(&rhiCtx)
1190 , m_rhi(rhi_)
1191 , m_stats(rhiCtx)
1192 , m_resourceRegistry(rhi_)
1193 {}
1194
1195public:
1196 // The records for the QSSGRhiResourceHandles given out for the resources
1197 // of this context. Retire a resource here before releasing or replacing
1198 // it, including when it is rebuilt in place with a new size.
1199 [[nodiscard]] QSSGRhiResourceRegistry &resourceRegistry() { return m_resourceRegistry; }
1200
1201 using Textures = QSet<QRhiTexture *>;
1202 using Meshes = QSet<QSSGRenderMesh *>;
1203
1204 [[nodiscard]] static QSSGRhiContextPrivate *get(QSSGRhiContext *q) { return q->d_ptr.get(); }
1205 [[nodiscard]] static const QSSGRhiContextPrivate *get(const QSSGRhiContext *q) { return q->d_ptr.get(); }
1206
1207 [[nodiscard]] static bool shaderDebuggingEnabled();
1208 [[nodiscard]] static bool editorMode();
1209
1210 void setMainRenderPassDescriptor(QRhiRenderPassDescriptor *rpDesc);
1211 void setCommandBuffer(QRhiCommandBuffer *cb);
1212 void setRenderTarget(QRhiRenderTarget *rt);
1213 void setMainPassSampleCount(int samples);
1214 void setMainPassViewCount(int viewCount);
1215
1216 void releaseCachedResources();
1217
1218 void registerTexture(QRhiTexture *texture);
1219 void releaseTexture(QRhiTexture *texture);
1220
1221 void registerMesh(QSSGRenderMesh *mesh);
1222 void releaseMesh(QSSGRenderMesh *mesh);
1223
1224 QSSGManagedRhiTexture create(QRhiTexture *texture);
1225
1226 // Textures referenced through QSSGManagedRhiTexture wrappers, e.g. the
1227 // attachments of the user render passes. The context owns these textures
1228 // and deletes them once the last wrapper is gone, using deleteLater() so a
1229 // texture is never released while the frame that dropped it is still
1230 // being recorded.
1231 void registerManagedTexture(QSSGManagedRhiTexture *wrapper);
1232 void unregisterManagedTexture(QSSGManagedRhiTexture *wrapper);
1233 void relocateManagedTexture(QSSGManagedRhiTexture *from, QSSGManagedRhiTexture *to);
1235
1236 QRhiShaderResourceBindings *srb(const QSSGRhiShaderResourceBindingList &bindings);
1237 void releaseCachedSrb(QSSGRhiShaderResourceBindingList &bindings);
1238
1239 QRhiGraphicsPipeline *pipeline(const QSSGRhiGraphicsPipelineState &ps,
1240 QRhiRenderPassDescriptor *rpDesc,
1241 QRhiShaderResourceBindings *srb);
1242
1243 QRhiGraphicsPipeline *pipeline(const QSSGGraphicsPipelineStateKey &key,
1244 QRhiRenderPassDescriptor *rpDesc,
1245 QRhiShaderResourceBindings *srb);
1246
1247 QRhiComputePipeline *computePipeline(const QShader &shader,
1248 QRhiShaderResourceBindings *srb);
1249
1251 QRhiShaderResourceBindings *srb);
1252
1253 QSSGRhiDrawCallData &drawCallData(const QSSGRhiDrawCallDataKey &key);
1254 void releaseDrawCallData(QSSGRhiDrawCallData &dcd);
1255 void cleanupDrawCallData(const QSSGRenderModel *model);
1256 // The data of the automatic per-instance LOD that layer did not use in the frame, as the
1257 // model got a mesh without levels of detail, was hidden or removed from the layer.
1258 void releaseUnusedAutoLodData(const QSSGRenderLayer *layer, quint32 frame);
1259 // All the data of the automatic per-instance LOD of layer, which is going away.
1260 void releaseAutoLodData(const QSSGRenderLayer *layer);
1261 void cleanupDrawCallDataForResource(const void *resource);
1262 void cleanupDrawCallDataForCid(const void *cid);
1263 void cleanupDrawCallDataForSkyMaterial(QSSGRenderSkyMaterial *skyMaterial);
1264 void cleanupDrawCallDataForUserPass(const QSSGRenderUserPassBase *userPass);
1265
1266 QSSGRhiInstanceBufferData &instanceBufferData(QSSGRenderInstanceTable *instanceTable);
1267 void releaseInstanceBuffer(QSSGRenderInstanceTable *instanceTable);
1268
1269 // The instance data of a model that culls or depth sorts its table, which depends on the
1270 // transform of the model and so is not shared with the other models drawing the table.
1271 QSSGRhiInstanceBufferData &instanceBufferData(const QSSGRenderModel *model);
1272 // Releases the culled or sorted copy of a model that no longer needs one, and draws the
1273 // data of the table instead.
1274 void releaseModelInstanceCopy(const QSSGRenderModel *model);
1275 void releaseModelInstanceData(const QSSGRenderModel *model);
1276
1277 QSSGRhiParticleData &particleData(const QSSGRenderGraphObject *particlesOrModel);
1278
1279 QSSGRhiContext *q_ptr = nullptr;
1280 QRhi *m_rhi = nullptr;
1281
1285 Textures m_textures;
1289
1291
1299 // Scratch space for distributing instances to the levels of the automatic per-instance LOD,
1300 // shared by all the models, see prepareAutoLodInstancing().
1304
1308
1309private:
1310 void refManagedTexture(QRhiTexture *texture);
1311 [[nodiscard]] bool derefManagedTexture(QRhiTexture *texture);
1312};
1313
1314inline bool operator==(const QSSGRhiDrawCallDataKey &a, const QSSGRhiDrawCallDataKey &b) noexcept
1315{
1316 return a.cid == b.cid && a.model == b.model && a.resource == b.resource && a.mapEntry == b.mapEntry
1317 && a.resourceIdx == b.resourceIdx;
1318}
1319
1320inline bool operator!=(const QSSGRhiDrawCallDataKey &a, const QSSGRhiDrawCallDataKey &b) noexcept
1321{
1322 return !(a == b);
1323}
1324
1325inline size_t qHash(const QSSGRhiDrawCallDataKey &k, size_t seed = 0) noexcept
1326{
1327 return qHashMulti(seed, k.cid, k.model, k.resource, k.mapEntry, k.resourceIdx);
1328}
1329
1330QT_END_NAMESPACE
1331
1332#endif // QSSGRHICONTEXT_P_H
friend bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are equal, otherwise returns false.
Definition qbytearray.h:834
friend bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are different, otherwise returns false.
Definition qbytearray.h:845
QObject * q_ptr
Definition qobject.h:73
Q_DECLARE_FLAGS(InitFlags, InitFlag)
static QSSGComputePipelineStateKey create(const QShader &shader, const QRhiShaderResourceBindings *srb)
QVector< quint32 > srbLayoutDescription
QRhiBuffer * buffer() const
QRhiCommandBuffer::IndexFormat indexFormat() const
quint32 stride() const
quint32 numVertices() const
QHash< const QSSGRenderModel *, QSSGRhiInstanceBufferData > m_instanceBuffersPerModel
void setMainRenderPassDescriptor(QRhiRenderPassDescriptor *rpDesc)
void releaseInstanceBuffer(QSSGRenderInstanceTable *instanceTable)
QSSGRhiInstanceBufferData & instanceBufferData(const QSSGRenderModel *model)
void releaseDrawCallData(QSSGRhiDrawCallData &dcd)
QRhiRenderPassDescriptor * m_mainRpDesc
QRhiShaderResourceBindings * srb(const QSSGRhiShaderResourceBindingList &bindings)
QSSGRhiDrawCallData & drawCallData(const QSSGRhiDrawCallDataKey &key)
QRhiGraphicsPipeline * pipeline(const QSSGRhiGraphicsPipelineState &ps, QRhiRenderPassDescriptor *rpDesc, QRhiShaderResourceBindings *srb)
void cleanupDrawCallDataForCid(const void *cid)
void releaseCachedSrb(QSSGRhiShaderResourceBindingList &bindings)
void releaseAutoLodData(const QSSGRenderLayer *layer)
void releaseUnusedAutoLodData(const QSSGRenderLayer *layer, quint32 frame)
void releaseMesh(QSSGRenderMesh *mesh)
QRhiComputePipeline * computePipeline(const QSSGComputePipelineStateKey &key, QRhiShaderResourceBindings *srb)
QHash< QSSGRenderInstanceTable *, QSSGRhiInstanceBufferData > m_instanceBuffers
void setCommandBuffer(QRhiCommandBuffer *cb)
QVector< QPair< QSSGRhiSamplerDescription, QRhiSampler * > > m_samplers
void releaseModelInstanceCopy(const QSSGRenderModel *model)
void relocateManagedTexture(QSSGManagedRhiTexture *from, QSSGManagedRhiTexture *to)
void setMainPassSampleCount(int samples)
void unregisterManagedTexture(QSSGManagedRhiTexture *wrapper)
QRhiRenderTarget * m_rt
void cleanupDrawCallData(const QSSGRenderModel *model)
QHash< const QSSGRenderGraphObject *, QSSGRhiParticleData > m_particleData
QSSGRhiContextStats m_stats
void cleanupDrawCallDataForResource(const void *resource)
QHash< QSSGRhiDummyTextureKey, QRhiTexture * > m_dummyTextures
void registerMesh(QSSGRenderMesh *mesh)
static bool shaderDebuggingEnabled()
QSSGRhiParticleData & particleData(const QSSGRenderGraphObject *particlesOrModel)
void setRenderTarget(QRhiRenderTarget *rt)
QHash< QSSGRhiShaderResourceBindingList, QRhiShaderResourceBindings * > m_srbCache
QSet< QSSGManagedRhiTexture * > m_managedTextureWrappers
void cleanupDrawCallDataForUserPass(const QSSGRenderUserPassBase *userPass)
QHash< QSSGRhiDrawCallDataKey, QSSGRhiDrawCallData > m_drawCallData
void registerManagedTexture(QSSGManagedRhiTexture *wrapper)
void setMainPassViewCount(int viewCount)
void releaseModelInstanceData(const QSSGRenderModel *model)
QRhiCommandBuffer * m_cb
QRhiComputePipeline * computePipeline(const QShader &shader, QRhiShaderResourceBindings *srb)
QList< quint8 > m_autoLodLevelOfInstance
QHash< QSSGComputePipelineStateKey, QRhiComputePipeline * > m_computePipelines
QHash< QRhiTexture *, size_t > m_managedTextureRefs
QHash< QSSGGraphicsPipelineStateKey, QRhiGraphicsPipeline * > m_pipelines
QSSGRhiResourceRegistry m_resourceRegistry
void cleanupDrawCallDataForSkyMaterial(QSSGRenderSkyMaterial *skyMaterial)
QSSGRhiInstanceBufferData & instanceBufferData(QSSGRenderInstanceTable *instanceTable)
const std::unique_ptr< QRhiRenderPassDescriptor > & getRenderPassDescriptor() const
const QSSGManagedRhiTexture & getDepthTexture() const
void setName(const QByteArray &name)
void reset(QSSGRhiResourceRegistry *registry)
const std::unique_ptr< QRhiTextureRenderTarget > & getRenderTarget() const
const QSSGManagedRhiTexture & getColorTexture(int index) const
QSSGRhiRenderableTextureV2()=default
const std::unique_ptr< QRhiRenderBuffer > & getDepthStencil() const
const QByteArray & getName() const
~QSSGRhiRenderableTextureV2()=default
void setUniformValue(char *ubufData, const char *name, const QVariant &value, QSSGRenderShaderValue::Type type)
const QRhiShaderStage * cendStages() const
QRhiTexture * ssaoTexture() const
const QHash< QSSGRhiInputAssemblerState::InputSemantic, QShaderDescription::InOutVariable > & vertexInputs() const
QRhiTexture * lightProbeTexture() const
QRhiTexture * shadowMapAtlasTexture() const
void applyAndClearStorageBindings(QRhiShaderResourceBinding::StageFlags stage, QSSGRhiShaderResourceBindingList &bindings)
int bindingForStorageImage(QByteArrayView name)
QPair< QSSGRenderTextureCoordOp, QSSGRenderTextureCoordOp > lightProbeTiling() const
InstanceLocations instanceBufferLocations() const
const StorageBindingMap & storageImageBindings() const
int bindingForStorageBuffer(QByteArrayView name)
void addStage(const QRhiShaderStage &stage, StageFlags flags={})
QRhiTexture * screenTexture() const
void ensureUniformBuffer(QRhiBuffer **ubuf)
QRhiTexture * MotionVectorTexture() const
QRhiTexture * shadowMapBlueNoiseTexture() const
const QRhiShaderStage * cbeginStages() const
const QRhiShaderStage * fragmentStage() const
QRhiTexture * depthTexture() const
void addExtraTexture(const QSSGRhiTexture &t)
void addStorageBufferBinding(const QByteArray &name, QRhiShaderResourceBinding::Type type, QRhiBuffer *buffer, int binding)
void setLightsEnabled(bool enable)
QSSGShaderDirectionalLightsUniformData & directionalLightsUniformData()
int offsetOfUniform(const QByteArray &name)
int bindingForTexture(const char *name, int hint=-1)
void ensureCombinedUniformBuffer(QRhiBuffer **ubuf)
void setShaderResources(char *ubufData, QSSGBufferManager &theBufferManager, const QByteArray &inPropertyName, const QVariant &propertyValue, QSSGRenderShaderValue::Type inPropertyType)
QRhiTexture ** oitImages()
void setUniformArray(char *ubufData, const char *name, const void *data, size_t itemCount, QSSGRenderShaderValue::Type type, int *storeIndex=nullptr)
QRhiTexture * normalTexture() const
const QRhiShaderStage * computeStage() const
int ub0DirectionalLightDataOffset() const
const QRhiShaderStage * vertexStage() const
QSSGShaderLightsUniformData & lightsUniformData()
void setUniform(char *ubufData, const char *name, const void *data, size_t size, int *storeIndex=nullptr, UniformFlags flags={})
QRhiTexture * lightmapTexture() const
const QSSGRhiTexture & extraTextureAt(int index) const
const StorageBindingMap & storageBufferBindings() const
QRhiVertexInputAttribute::Format toVertexInputFormat(QSSGRenderComponentType compType, quint32 numComps)
QRhiGraphicsPipeline::CullMode toCullMode(QSSGCullFaceMode cullFaceMode)
QRhiGraphicsPipeline::Topology toTopology(QSSGRenderDrawMode drawMode)
QRhiSampler::Filter toRhi(QSSGRenderTextureFilterOp op)
static bool skyIblCreatePrefilterTargets(QRhi *rhi, QRhiTexture *texture, const QSize &environmentMapSize, const QByteArray &namePrefix, QSSGSkyIblPrefilterTargets *outTargets, bool preserveColorContents=false)
static constexpr QRhiTexture::Format cTextureFormat
static bool ensureSrb(QRhi *rhi, QRhiShaderResourceBindings *&dst, std::initializer_list< QRhiShaderResourceBinding > bindings, const char *errorMessage)
static bool skyIblCreateFaceTargets(QRhi *rhi, QRhiTexture *texture, const QByteArray &namePrefix, QSSGSkyIblFaceTargets *outTargets, bool preserveColorContents=false)
static QRhiGraphicsPipeline * createPrefilterPipeline(QRhi *rhi, const QRhiVertexInputLayout &inputLayout, const PrefilterPipelineConfig &cfg, const char *errorMessage)
static void drawCubeFace(QRhiCommandBuffer *cb, QSSGRhiContext *ctx, QRhiTextureRenderTarget *rt, QSize viewport, QRhiGraphicsPipeline *pipeline, QRhiShaderResourceBindings *srb, const QRhiCommandBuffer::VertexInput &vbufBinding, const QVector< QPair< int, quint32 > > &dynamicOffsets, const QByteArray &profilerLabel, const QByteArray &passDebugLabel, const QColor &clearColor=QColor(0, 0, 0, 1))
static void skyIblInitializeUnrenderedMips(QRhi *rhi, QRhiCommandBuffer *cb, QSSGRhiContext *context, QRhiTexture *texture, int firstMip, int mipCountExclusive, const QByteArray &debugObjectName)
static bool ensureDynamicUBuf(QRhi *rhi, QRhiBuffer *&dst, int size, const char *errorMessage)
static QVarLengthArray< QMatrix4x4, 6 > skyIblEnvironmentMapViews(QRhi *rhi)
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
Definition qsize.h:192
#define QSSGRHICTX_STAT(ctx, f)
#define QSSG_MAX_NUM_LIGHTS
#define QSSG_MAX_NUM_DIRECTIONAL_LIGHTS
QSSGRhiSamplerBindingHints
static constexpr const QSSGRhiShaderPipeline * getShaderPipeline(const QSSGRhiGraphicsPipelineState &ps)
static const InputAssemblerState & get(const QSSGRhiGraphicsPipelineState &ps)
static InputAssemblerState & get(QSSGRhiGraphicsPipelineState &ps)
QVarLengthArray< float, MaxAutoLodLevels > lodDistancesSq
const QSSGRenderInstanceTable * table
std::array< int, MaxAutoLodLevels > counts
QHash< SubsetLodKey, SubsetLodData > subsetLodData
QList< QSSGRhiSortData > sortData
std::pair< const QSSGRenderSubset *, const QSSGRenderLayer * > SubsetLodKey
const QSSGRenderInstanceTable * table
QList< qint32 > lodInstanceIndices
static constexpr int MaxAutoLodLevels
std::array< QVector3D, 3 > localAxes
friend bool operator!=(const QSSGRhiInstancePlacement &a, const QSSGRhiInstancePlacement &b)
friend bool operator==(const QSSGRhiInstancePlacement &a, const QSSGRhiInstancePlacement &b)
QList< QSSGRhiSortData > sortData
QVarLengthArray< ImageIndices, 16 > imageIndices
StorageResourceBinding(int binding, const QByteArray &name, QRhiShaderResourceBinding::Type type, QRhiBuffer *buffer)
QSSGShaderDirectionalLightData directionalLightData[QSSG_MAX_NUM_DIRECTIONAL_LIGHTS]
QSSGShaderLightData lightData[QSSG_MAX_NUM_LIGHTS]