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
qssgrendershadercache_p.h
Go to the documentation of this file.
1// Copyright (C) 2008-2012 NVIDIA Corporation.
2// Copyright (C) 2019 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6
7#ifndef QSSG_RENDER_SHADER_CACHE_H
8#define QSSG_RENDER_SHADER_CACHE_H
9
10//
11// W A R N I N G
12// -------------
13//
14// This file is not part of the Qt API. It exists purely as an
15// implementation detail. This header file may change from version to
16// version without notice, or even be removed.
17//
18// We mean it.
19//
20
21#include <QtQuick3DRuntimeRender/private/qtquick3druntimerenderglobal_p.h>
22#include <QtQuick3DUtils/private/qssgdataref_p.h>
23#include <QtQuick3DUtils/private/qqsbcollection_p.h>
24
25#include <QtQuick3DRuntimeRender/private/qssgrhicontext_p.h>
26#include <QtQuick3DRuntimeRender/private/qssgrendererimplshaders_p.h>
27
28#include <QtCore/QString>
29#include <QtCore/qcryptographichash.h>
30#include <QtCore/QSharedPointer>
31#include <QtCore/QVector>
32
33QT_BEGIN_NAMESPACE
34
35class QSSGRenderContextInterface;
36class QSSGRhiShaderPipeline;
37class QShaderBaker;
38class QRhi;
39
40using QSSGUserShaderFragmentOutputs = QVector<QByteArrayView>;
41
47
48using QSSGShaderDefineList = QList<QSSGShaderDefine>;
49
51{
52public:
55
56 bool needsBaseColor = false;
57 bool needsRoughness = false;
58 bool needsMetalness = false;
59 bool needsDiffuseLight = false;
60 bool needsSpecularLight = false;
61 bool needsEmissiveLight = false;
62 bool needsWorldNormal = false;
63 bool needsWorldTangent = false;
64 bool needsWorldBinormal = false;
65 bool needsF0 = false;
66 bool needsF90 = false;
67
69
71
74
76
78 {
79 return (body.size() > 0);
80 }
81
83 {
84 return propertyUniforms.size() > 0;
85 }
86
87};
88
89
90
91struct Q_QUICK3DRUNTIMERENDER_EXPORT QSSGShaderFeatures
92{
93
94// There are a number of macros used to turn on or off various features.
95// This allows those features to be propagated into the shader cache's caching mechanism.
96// They will be translated into #define name value where value is 1 or zero depending on
97// if the feature is enabled or not.
98//
99// In snippets that use this feature the code would look something like this:
100
101/*
102#ifndef QSSG_ENABLE_<FEATURE>
103#define QSSG_ENABLE_<FEATURE> 0
104#endif
105
106void func()
107{
108 ...
109#if QSSG_ENABLE_<FEATURE>
110 ...
111#endif
112 ...
113}
114*/
115
116// NOTE: The order of these will affect generated keys, so re-ordering these
117// will break already baked shaders (e.g. shadergen).
118using FlagType = quint32;
119enum class Feature : FlagType
120{
121 LightProbe = (1 << 8),
122 IblOrientation = (1 << 9) + 1,
123 Ssm = (1 << 10) + 2,
124 Ssao = (1 << 11) + 3,
125 DepthPass = (1 << 12) + 4,
126 OrthoShadowPass = (1 << 13) + 5,
127 PerspectiveShadowPass = (1 << 14) + 6,
128 LinearTonemapping = (1 << 15) + 7,
129 AcesTonemapping = (1 << 16) + 8,
130 HejlDawsonTonemapping = (1 << 17) + 9,
131 FilmicTonemapping = (1 << 18) + 10,
132 RGBELightProbe = (1 << 19) + 11,
133 OpaqueDepthPrePass = (1 << 20) + 12,
134 ReflectionProbe = (1 << 21) + 13,
135 ReduceMaxNumLights = (1 << 22) + 14,
136 Lightmap = (1 << 23) + 15,
137 DisableMultiView = (1 << 24) + 16,
138 ForceIblExposure = (1 << 25) + 17,
139 NormalPass = (1 << 26) + 18,
140 UserRenderPass = (1 << 27) + 19,
141 MotionVector = (1 << 28) + 20,
142 LastFeature
143};
144
145static constexpr FlagType IndexMask = 0xff;
146static constexpr quint32 Count = (static_cast<FlagType>(Feature::LastFeature) & IndexMask);
147
148static const char *asDefineString(QSSGShaderFeatures::Feature feature);
149static Feature fromIndex(quint32 idx);
150
151constexpr bool isNull() const { return flags == 0; }
152constexpr bool isSet(Feature feature) const { return (static_cast<FlagType>(feature) & flags); }
153void set(Feature feature, bool val);
154
155FlagType flags = 0;
156
157inline friend bool operator==(QSSGShaderFeatures a, QSSGShaderFeatures b) { return a.flags == b.flags; }
158
159void disableTonemapping()
160{
161 set(Feature::LinearTonemapping, false);
162 set(Feature::AcesTonemapping, false);
163 set(Feature::FilmicTonemapping, false);
164 set(Feature::HejlDawsonTonemapping, false);
165 set(Feature::ForceIblExposure, false);
166}
167
168inline friend QDebug operator<<(QDebug stream, const QSSGShaderFeatures &features)
169{
170 QVarLengthArray<const char *, Count> enabledFeatureStrings;
171 for (quint32 idx = 0; idx < Count; ++idx) {
172 const Feature feature = fromIndex(idx);
173 if (features.isSet(feature))
174 enabledFeatureStrings.append(asDefineString(feature));
175 }
176 stream.nospace() << "QSSGShaderFeatures(";
177 for (int i = 0; i < enabledFeatureStrings.size(); ++i)
178 stream.nospace() << (i > 0 ? ", " : "") << enabledFeatureStrings[i];
179 stream.nospace() << ")";
180 return stream;
181}
182
183};
184
185Q_QUICK3DRUNTIMERENDER_EXPORT size_t qHash(QSSGShaderFeatures features) noexcept;
186
188{
192
193 explicit QSSGShaderCacheKey(const QByteArray &key = QByteArray()) : m_key(key), m_hashCode(0) {}
194
195 QSSGShaderCacheKey(const QSSGShaderCacheKey &other) = default;
197
198 static inline size_t generateHashCode(const QByteArray &key, QSSGShaderFeatures features)
199 {
200 return qHash(key) ^ qHash(features);
201 }
202
204 {
205 m_hashCode = generateHashCode(m_key, m_features);
206 }
207
208 bool operator==(const QSSGShaderCacheKey &inOther) const
209 {
210 return m_key == inOther.m_key && m_features == inOther.m_features;
211 }
212};
213
215{
216 return key.m_hashCode;
217}
218
219class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGShaderCache
220{
221 Q_DISABLE_COPY(QSSGShaderCache)
222public:
223 enum class ShaderType
224 {
225 Vertex = 0,
226 Fragment = 1
227 };
228
229 using InitBakerFunc = void (*)(QShaderBaker *baker, QRhi *rhi);
230private:
231 friend class QSSGBuiltInRhiShaderCache;
232
233 typedef QHash<QSSGShaderCacheKey, QSSGRhiShaderPipelinePtr> TRhiShaderMap;
234 QSSGRhiContext &m_rhiContext; // Not own, the RCI owns us and the QSSGRhiContext.
235 TRhiShaderMap m_rhiShaders;
236 QByteArray m_insertStr; // member to potentially reuse the allocation after clear
237 QByteArray m_cacheKeyStr; // same here
238 InitBakerFunc m_initBaker;
239 QQsbInMemoryCollection m_persistentShaderBakingCache;
240 QString m_persistentShaderStorageFileName;
241 QSSGBuiltInRhiShaderCache m_builtInShaders;
242
243 QSSGRhiShaderPipelinePtr loadBuiltinUncached(const QByteArray &inKey, int viewCount,
244 QSSGRhiShaderPipeline::StageFlags vertexStageFlags);
245
246 void addShaderPreprocessor(QByteArray &str,
247 const QByteArray &inKey,
248 ShaderType shaderType,
249 const QSSGShaderFeatures &inFeatures,
250 const QSSGUserShaderAugmentation &shaderAugmentation,
251 int viewCount);
252
253public:
254 QSSGShaderCache(QSSGRhiContext &ctx,
255 const InitBakerFunc initBakeFn = nullptr);
256 ~QSSGShaderCache();
257
258 QSSGRhiContext &rhiContext() { return m_rhiContext; }
259
260 void releaseCachedResources();
261
262 QQsbInMemoryCollection &persistentShaderBakingCache() { return m_persistentShaderBakingCache; }
263
265 const QSSGShaderFeatures &inFeatures);
266
268 const QByteArray &inKey,
269 const QSSGShaderFeatures &inFeatures,
270 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
271
273 const QSSGShaderFeatures &inFeatures,
274 QQsbCollection::Entry entry,
275 const QSSGRenderGraphObject &obj,
276 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
277
278 QSSGRhiShaderPipelinePtr compileForRhi(const QByteArray &inKey,
279 const QByteArray &inVert,
280 const QByteArray &inFrag,
281 const QSSGShaderFeatures &inFeatures,
282 QSSGRhiShaderPipeline::StageFlags stageFlags,
283 const QSSGUserShaderAugmentation &shaderAugmentation,
284 int viewCount,
285 bool perTargetCompilation);
286
287 QSSGBuiltInRhiShaderCache &getBuiltInRhiShaders() { return m_builtInShaders; }
288
289 static QByteArray resourceFolder();
292 static void initBakerForPersistentUse(QShaderBaker *baker, QRhi *rhi);
293};
294
295namespace QtQuick3DEditorHelpers {
296namespace ShaderBaker
297{
298 enum class Status : quint8
299 {
302 };
303 using StatusCallback = void(*)(const QByteArray &descKey, Status status, const QString &err, QShader::Stage stage);
304 Q_QUICK3DRUNTIMERENDER_EXPORT void setStatusCallback(StatusCallback cb);
305}
306
307namespace ShaderCache
308{
309 // Used by DS and the QML puppet!
310 // Note: Needs to be called before any QSSGShaderCache instance is created.
311 Q_QUICK3DRUNTIMERENDER_EXPORT void setAutomaticDiskCache(bool enable);
312 Q_QUICK3DRUNTIMERENDER_EXPORT bool isAutomaticDiskCacheEnabled();
313}
314
315}
316
317QT_END_NAMESPACE
318
319#endif
const std::unique_ptr< QSSGRhiContext > & rhiContext() const
QSSGBuiltInRhiShaderCache & getBuiltInRhiShaders()
QSSGRhiShaderPipelinePtr tryGetRhiShaderPipeline(const QByteArray &inKey, const QSSGShaderFeatures &inFeatures)
static QByteArray shaderCollectionFile()
QQsbInMemoryCollection & persistentShaderBakingCache()
static QByteArray resourceFolder()
QSSGRhiShaderPipelinePtr newPipelineFromPregenerated(const QByteArray &inKey, const QSSGShaderFeatures &inFeatures, QQsbCollection::Entry entry, const QSSGRenderGraphObject &obj, QSSGRhiShaderPipeline::StageFlags stageFlags={})
static void initBakerForPersistentUse(QShaderBaker *baker, QRhi *rhi)
QSSGRhiShaderPipelinePtr compileForRhi(const QByteArray &inKey, const QByteArray &inVert, const QByteArray &inFrag, const QSSGShaderFeatures &inFeatures, QSSGRhiShaderPipeline::StageFlags stageFlags, const QSSGUserShaderAugmentation &shaderAugmentation, int viewCount, bool perTargetCompilation)
static QByteArray particleShaderCollectionFile()
QSSGRhiShaderPipelinePtr tryNewPipelineFromPersistentCache(const QByteArray &qsbcKey, const QByteArray &inKey, const QSSGShaderFeatures &inFeatures, QSSGRhiShaderPipeline::StageFlags stageFlags={})
void(*)(QShaderBaker *baker, QRhi *rhi) InitBakerFunc
PropertyUniformsList propertyUniforms
QSSGUserShaderFragmentOutputs outputs
constexpr QSSGCameraId getCameraId(const QSSGRenderGraphObject &o)
constexpr QSSGResourceId getResourceId(const QSSGRenderGraphObject &o)
T * getExtension(QSSGExtensionId extensionId)
static constexpr bool isSceneRoot(QSSGRenderGraphObject::Type type) noexcept
T * getResource(QSSGResourceId resId)
static constexpr bool isUserRenderPass(QSSGRenderGraphObject::Type type) noexcept
constexpr QSSGExtensionId getExtensionId(const QSSGRenderGraphObject &o)
static constexpr bool hasInternalFlag(const QSSGRenderGraphObject &obj, InternalFlags flag)
constexpr bool isNull(QSSGTypeId id)
static constexpr bool hasInternalFlags(const QSSGRenderGraphObject &obj)
T * getCamera(QSSGCameraId cameraId)
static constexpr bool isTextureProvider(QSSGRenderGraphObject::Type type) noexcept
constexpr QSSGNodeId getNodeId(const QSSGRenderGraphObject &o)
static constexpr QSSGRenderGraphObject::BaseType getBaseType(QSSGRenderGraphObject::Type type) noexcept
Combined button and popup list for selecting options.
Q_QUICK3DRUNTIMERENDER_EXPORT void setStatusCallback(StatusCallback cb)
void(*)(const QByteArray &descKey, Status status, const QString &err, QShader::Stage stage) StatusCallback
Q_QUICK3DRUNTIMERENDER_EXPORT void setAutomaticDiskCache(bool enable)
Q_QUICK3DRUNTIMERENDER_EXPORT bool isAutomaticDiskCacheEnabled()
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
static QString persistentQsbcFileName()
static bool isAutoDiskCacheEnabled()
size_t qHash(QSSGShaderFeatures features) noexcept
static bool ensureWritableDir(const QString &name)
static bool s_autoDiskCacheEnabled
static QString dumpFilename(QShader::Stage stage)
static QString persistentQsbcDir()
static void initBakerForNonPersistentUse(QShaderBaker *, QRhi *)
static constexpr DefineEntry DefineTable[]
size_t qHash(const QSSGShaderCacheKey &key)
static size_t generateHashCode(const QByteArray &key, QSSGShaderFeatures features)
QSSGShaderCacheKey & operator=(const QSSGShaderCacheKey &other)=default
bool operator==(const QSSGShaderCacheKey &inOther) const
QSSGShaderCacheKey(const QSSGShaderCacheKey &other)=default
QSSGShaderCacheKey(const QByteArray &key=QByteArray())
QSSGShaderFeatures m_features