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
qssgrenderskymaterialmanager_p.h
Go to the documentation of this file.
1// Copyright (C) 2026 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#ifndef QSSGRENDERSKYMATERIALMANAGER_H
6#define QSSGRENDERSKYMATERIALMANAGER_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtQuick3DRuntimeRender/private/qtquick3druntimerenderglobal_p.h>
20#include <QtQuick3DRuntimeRender/private/qssgrenderimagetexture_p.h>
21#include <QtQuick3DRuntimeRender/private/qssgrenderskymaterial_p.h>
22
23#include <QtCore/qhash.h>
24#include <QtCore/qlist.h>
25#include <QtCore/qmap.h>
26#include <QtCore/qsize.h>
27#include <QtCore/qvarlengtharray.h>
28
29#include <rhi/qrhi.h>
30
31#include <memory>
32
33QT_BEGIN_NAMESPACE
34
35class QSSGRenderContextInterface;
36class QSSGRhiShaderPipeline;
37
38using QSSGSkyIblCubeFaceRenderTargets = QVarLengthArray<QRhiTextureRenderTarget *, 6>;
39
45
53
54class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRenderSkyMaterialManager
55{
56 Q_DISABLE_COPY(QSSGRenderSkyMaterialManager)
57public:
58 explicit QSSGRenderSkyMaterialManager(const QSSGRenderContextInterface &inContext);
59 ~QSSGRenderSkyMaterialManager();
60
61 void releaseCachedResources();
62
63 QSSGRenderImageTexture resolve(QSSGRenderSkyMaterial *settings, QSSGPassKey passKey);
64
65private:
66 bool ensureEnvironmentMap(QSSGRenderSkyMaterial *settings, QSSGPassKey passKey);
67
68 struct FrameState;
69
70 bool computeFrameState(QSSGRenderSkyMaterial *inSky, FrameState &fs);
71 bool ensureTextures(FrameState &fs);
72 void deriveCycleState(QSSGRenderSkyMaterial *inSky, FrameState &fs);
73 void validateAndUpdateCacheKey(const FrameState &fs, QSSGRhiShaderPipeline *envShaderPipelineKey);
74 bool ensureSharedResources(FrameState &fs, QRhiCommandBuffer *cb);
75 bool renderEnvironmentCube(QSSGRenderSkyMaterial *inSky,
76 QSSGPassKey passKey,
77 const FrameState &fs,
78 const QSSGRhiShaderPipelinePtr &shaderPipeline,
79 QRhiCommandBuffer *cb,
80 QRhiResourceUpdateBatch *rub);
81 void runEnvironmentMipChain(const FrameState &fs, QRhiCommandBuffer *cb);
82 bool runPrefilterCycle(QSSGRenderSkyMaterial *inSky, const FrameState &fs, QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *&rub);
83 void initializeTailMips(const FrameState &fs, QRhiCommandBuffer *cb);
84
85 const QSSGRenderContextInterface &m_context;
86
87 QSSGRenderImageTexture m_skyIblTexture;
88
89 // The procedural cubemap is shared between IBL modes;
90 // m_prefilteredCubeMap is only allocated when IBL is enabled.
91 QRhiTexture *m_envCubeMap = nullptr;
92 QRhiTexture *m_prefilteredCubeMap = nullptr;
93 QSize m_cubeMapSize;
94 int m_prefilteredMipCount = 0;
95
96 // One non-mipmapped GL_TEXTURE_2D_ARRAY per specular mip level.
97 // Storing them separately (rather than as one mipmapped array) works
98 // around an Android GLES driver bug where glFramebufferTextureLayer
99 // silently ignores level > 0, causing all slice writes to land on
100 // mip 0 and corrupting the prefiltered result. Each texture here has
101 // exactly one mip level and is always attached at level 0.
102 QList<QRhiTexture *> m_prefilterAccumulators;
103
104 // Set when all slice accumulation is complete (iblRenderFrames >= 1) to trigger a
105 // dedicated normalize + irradiance frame rather than running it in the last slice frame.
106 bool m_finalizeIblPending = false;
107
108 int m_accumulatedSamples = 0;
109 int m_accumIblSampleCount = 0;
110 bool m_haveConvergedResult = false;
111 bool m_envTailMipsInitialized = false;
112 bool m_prefilteredTailMipsInitialized = false;
113
114 struct PrefilterResourceCache
115 {
116 // Invalidation keys
117 QSize environmentMapSize;
118 bool enableIBL = false;
119 int prefilterTotalMipCount = 0;
120 QRhiTexture *envCubeMap = nullptr;
121 QRhiTexture *prefilteredCubeMap = nullptr;
122 // One entry per specular mip level, mirroring m_prefilterAccumulators.
123 QList<QRhiTexture *> prefilterAccumulators;
124 QSSGRhiShaderPipeline *envShaderPipeline = nullptr;
125
126 // Cached resources.
127 QRhiBuffer *vertexBuffer = nullptr;
128 QRhiBuffer *uBuf = nullptr;
129 QRhiBuffer *uBufSlice = nullptr;
130 QRhiBuffer *uBufNormalize = nullptr;
131 QRhiBuffer *uBufIrradiance = nullptr;
132
133 QSSGSkyIblFaceTargets envFaceTargets;
134 QSSGSkyIblPrefilterTargets prefilterTargets;
135
136 // Per-mip face targets for the accumulator (one entry per specular mip level).
137 // Preserve variant: loads existing content so additive slices accumulate.
138 // Clear variant: clears before drawing, used on the first slice.
139 QList<QSSGSkyIblFaceTargets> accumPreserveFaceTargets;
140 QList<QSSGSkyIblFaceTargets> accumClearFaceTargets;
141
142 QRhiShaderResourceBindings *sliceSrb = nullptr;
143 // One SRB per specular mip level, each binding the corresponding
144 // per-mip accumulator texture so the normalize pass reads the right data.
145 QVarLengthArray<QRhiShaderResourceBindings *, 6> normalizeSrbs;
146 QRhiShaderResourceBindings *irradianceSrb = nullptr;
147
148 QRhiGraphicsPipeline *envMapPipeline = nullptr;
149 QRhiGraphicsPipeline *slicePipeline = nullptr;
150 QRhiGraphicsPipeline *normalizeCubePipeline = nullptr;
151 QRhiGraphicsPipeline *irradiancePipeline = nullptr;
152 };
153
154 PrefilterResourceCache m_cache;
155
156 void clearPrefilterCache();
157};
158
159using QSSGRenderSkyMaterialManagerPtr = std::shared_ptr<QSSGRenderSkyMaterialManager>;
160
161QT_END_NAMESPACE
162
163#endif
QSSGRenderImageTexture resolve(QSSGRenderSkyMaterial *settings, QSSGPassKey passKey)
friend class QSSGRenderContextInterface
QSSGSkyIblCubeFaceRenderTargets renderTargets
QRhiRenderPassDescriptor * renderPassDesc
QVarLengthArray< QSize, 6 > mipLevelSizes
QVarLengthArray< QSSGSkyIblCubeFaceRenderTargets, 6 > mipRenderTargetsMap
QRhiRenderPassDescriptor * renderPassDesc