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;
39class QSurfaceFormat;
40
41using QSSGUserShaderFragmentOutputs = QVector<QByteArrayView>;
42
48
49using QSSGShaderDefineList = QList<QSSGShaderDefine>;
50
52{
53public:
57
58 bool needsBaseColor = false;
59 bool needsRoughness = false;
60 bool needsMetalness = false;
61 bool needsDiffuseLight = false;
62 bool needsSpecularLight = false;
63 bool needsEmissiveLight = false;
64 bool needsWorldNormal = false;
65 bool needsWorldTangent = false;
66 bool needsWorldBinormal = false;
67 bool needsF0 = false;
68 bool needsF90 = false;
69
71
73
76
78
80 {
81 return (body.size() > 0);
82 }
83
85 {
86 return propertyUniforms.size() > 0;
87 }
88
89};
90
91
92
93struct Q_QUICK3DRUNTIMERENDER_EXPORT QSSGShaderFeatures
94{
95
96// There are a number of macros used to turn on or off various features.
97// This allows those features to be propagated into the shader cache's caching mechanism.
98// They will be translated into #define name value where value is 1 or zero depending on
99// if the feature is enabled or not.
100//
101// In snippets that use this feature the code would look something like this:
102
103/*
104#ifndef QSSG_ENABLE_<FEATURE>
105#define QSSG_ENABLE_<FEATURE> 0
106#endif
107
108void func()
109{
110 ...
111#if QSSG_ENABLE_<FEATURE>
112 ...
113#endif
114 ...
115}
116*/
117
118// NOTE: The order of these will affect generated keys, so re-ordering these
119// will break already baked shaders (e.g. shadergen).
120using FlagType = quint32;
121enum class Feature : FlagType
122{
123 LightProbe = (1 << 8),
124 IblOrientation = (1 << 9) + 1,
125 Ssm = (1 << 10) + 2,
126 Ssao = (1 << 11) + 3,
127 DepthPass = (1 << 12) + 4,
128 OrthoShadowPass = (1 << 13) + 5,
129 PerspectiveShadowPass = (1 << 14) + 6,
130 LinearTonemapping = (1 << 15) + 7,
131 AcesTonemapping = (1 << 16) + 8,
132 HejlDawsonTonemapping = (1 << 17) + 9,
133 FilmicTonemapping = (1 << 18) + 10,
134 RGBELightProbe = (1 << 19) + 11,
135 OpaqueDepthPrePass = (1 << 20) + 12,
136 ReflectionProbe = (1 << 21) + 13,
137 ReduceMaxNumLights = (1 << 22) + 14,
138 Lightmap = (1 << 23) + 15,
139 DisableMultiView = (1 << 24) + 16,
140 ForceIblExposure = (1 << 25) + 17,
141 NormalPass = (1 << 26) + 18,
142 UserRenderPass = (1 << 27) + 19,
143 MotionVector = (1 << 28) + 20,
144 LastFeature
145};
146
147static constexpr FlagType IndexMask = 0xff;
148static constexpr quint32 Count = (static_cast<FlagType>(Feature::LastFeature) & IndexMask);
149
150static const char *asDefineString(QSSGShaderFeatures::Feature feature);
151static Feature fromIndex(quint32 idx);
152
153constexpr bool isNull() const { return flags == 0; }
154constexpr bool isSet(Feature feature) const { return (static_cast<FlagType>(feature) & flags); }
155void set(Feature feature, bool val);
156
157FlagType flags = 0;
158
159inline friend bool operator==(QSSGShaderFeatures a, QSSGShaderFeatures b) { return a.flags == b.flags; }
160
161void disableTonemapping()
162{
163 set(Feature::LinearTonemapping, false);
164 set(Feature::AcesTonemapping, false);
165 set(Feature::FilmicTonemapping, false);
166 set(Feature::HejlDawsonTonemapping, false);
167 set(Feature::ForceIblExposure, false);
168}
169
170inline friend QDebug operator<<(QDebug stream, const QSSGShaderFeatures &features)
171{
172 QVarLengthArray<const char *, Count> enabledFeatureStrings;
173 for (quint32 idx = 0; idx < Count; ++idx) {
174 const Feature feature = fromIndex(idx);
175 if (features.isSet(feature))
176 enabledFeatureStrings.append(asDefineString(feature));
177 }
178 stream.nospace() << "QSSGShaderFeatures(";
179 for (int i = 0; i < enabledFeatureStrings.size(); ++i)
180 stream.nospace() << (i > 0 ? ", " : "") << enabledFeatureStrings[i];
181 stream.nospace() << ")";
182 return stream;
183}
184
185};
186
187Q_QUICK3DRUNTIMERENDER_EXPORT size_t qHash(QSSGShaderFeatures features) noexcept;
188
190{
194
195 explicit QSSGShaderCacheKey(const QByteArray &key = QByteArray()) : m_key(key), m_hashCode(0) {}
196
197 QSSGShaderCacheKey(const QSSGShaderCacheKey &other) = default;
199
200 static inline size_t generateHashCode(const QByteArray &key, QSSGShaderFeatures features)
201 {
202 return qHash(key) ^ qHash(features);
203 }
204
206 {
207 m_hashCode = generateHashCode(m_key, m_features);
208 }
209
210 bool operator==(const QSSGShaderCacheKey &inOther) const
211 {
212 return m_key == inOther.m_key && m_features == inOther.m_features;
213 }
214};
215
217{
218 return key.m_hashCode;
219}
220
221class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGShaderCache
222{
223 Q_DISABLE_COPY(QSSGShaderCache)
224public:
225 enum class ShaderType
226 {
227 Vertex = 0,
228 Fragment = 1
229 };
230
231 using InitBakerFunc = void (*)(QShaderBaker *baker, QRhi *rhi);
232private:
233 friend class QSSGBuiltInRhiShaderCache;
234
235 typedef QHash<QSSGShaderCacheKey, QSSGRhiShaderPipelinePtr> TRhiShaderMap;
236 QSSGRhiContext &m_rhiContext; // Not own, the RCI owns us and the QSSGRhiContext.
237 TRhiShaderMap m_rhiShaders;
238 QByteArray m_insertStr; // member to potentially reuse the allocation after clear
239 QByteArray m_cacheKeyStr; // same here
240 InitBakerFunc m_initBaker;
241 QQsbInMemoryCollection m_persistentShaderBakingCache;
242 QString m_persistentShaderStorageFileName;
243 QSSGBuiltInRhiShaderCache m_builtInShaders;
244
245 QSSGRhiShaderPipelinePtr loadBuiltinUncached(const QByteArray &inKey, int viewCount,
246 QSSGRhiShaderPipeline::StageFlags vertexStageFlags);
247
248 void addShaderPreprocessor(QByteArray &str,
249 const QByteArray &inKey,
250 ShaderType shaderType,
251 const QSSGShaderFeatures &inFeatures,
252 const QSSGUserShaderAugmentation &shaderAugmentation,
253 int viewCount);
254
255public:
256 QSSGShaderCache(QSSGRhiContext &ctx,
257 const InitBakerFunc initBakeFn = nullptr);
258 ~QSSGShaderCache();
259
260 QSSGRhiContext &rhiContext() { return m_rhiContext; }
261
262 void releaseCachedResources();
263
264 QQsbInMemoryCollection &persistentShaderBakingCache() { return m_persistentShaderBakingCache; }
265
267 const QSSGShaderFeatures &inFeatures);
268
270 const QByteArray &inKey,
271 const QSSGShaderFeatures &inFeatures,
272 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
273
275 const QSSGShaderFeatures &inFeatures,
276 QQsbCollection::Entry entry,
277 const QSSGRenderGraphObject &obj,
278 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
279
280 QSSGRhiShaderPipelinePtr compileForRhi(const QByteArray &inKey,
281 const QByteArray &inVert,
282 const QByteArray &inFrag,
283 const QSSGShaderFeatures &inFeatures,
284 QSSGRhiShaderPipeline::StageFlags stageFlags,
285 const QSSGUserShaderAugmentation &shaderAugmentation,
286 int viewCount,
287 bool perTargetCompilation,
288 const QByteArray &inCacheKey = {});
289
290 QSSGBuiltInRhiShaderCache &getBuiltInRhiShaders() { return m_builtInShaders; }
291
292 static QByteArray resourceFolder();
295 static void initBakerForPersistentUse(QShaderBaker *baker, QRhi *rhi);
296 static QList<QShaderVersion> glslTargetsForContext(const QSurfaceFormat &format, bool isGLESModule);
297};
298
299namespace QtQuick3DEditorHelpers {
300namespace ShaderBaker
301{
302 enum class Status : quint8
303 {
306 };
307 using StatusCallback = void(*)(const QByteArray &descKey, Status status, const QString &err, QShader::Stage stage);
308 Q_QUICK3DRUNTIMERENDER_EXPORT void setStatusCallback(StatusCallback cb);
309}
310
311namespace ShaderCache
312{
313 // Used by DS and the QML puppet!
314 // Note: Needs to be called before any QSSGShaderCache instance is created.
315 Q_QUICK3DRUNTIMERENDER_EXPORT void setAutomaticDiskCache(bool enable);
316 Q_QUICK3DRUNTIMERENDER_EXPORT bool isAutomaticDiskCacheEnabled();
317}
318
319}
320
321QT_END_NAMESPACE
322
323#endif
const std::unique_ptr< QSSGRhiContext > & rhiContext() const
QSSGBuiltInRhiShaderCache & getBuiltInRhiShaders()
QSSGRhiShaderPipelinePtr tryGetRhiShaderPipeline(const QByteArray &inKey, const QSSGShaderFeatures &inFeatures)
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, const QByteArray &inCacheKey={})
static QByteArray shaderCollectionFile()
static QList< QShaderVersion > glslTargetsForContext(const QSurfaceFormat &format, bool isGLESModule)
QQsbInMemoryCollection & persistentShaderBakingCache()
static QByteArray resourceFolder()
QSSGRhiShaderPipelinePtr newPipelineFromPregenerated(const QByteArray &inKey, const QSSGShaderFeatures &inFeatures, QQsbCollection::Entry entry, const QSSGRenderGraphObject &obj, QSSGRhiShaderPipeline::StageFlags stageFlags={})
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