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/qset.h>
32#include <QtCore/QVector>
33
34QT_BEGIN_NAMESPACE
35
36class QSSGRenderContextInterface;
37class QSSGRhiShaderPipeline;
38class QShaderBaker;
39class QRhi;
40class QSurfaceFormat;
41
42using QSSGUserShaderFragmentOutputs = QVector<QByteArrayView>;
43
49
50using QSSGShaderDefineList = QList<QSSGShaderDefine>;
51
53{
54public:
58
59 enum class FeatureFlags : quint32
60 {
61 None = 0,
62 BaseColor = (1 << 0),
63 Roughness = (1 << 1),
64 Metalness = (1 << 2),
65 DiffuseLight = (1 << 3),
66 SpecularLight = (1 << 4),
67 EmissiveLight = (1 << 5),
68 WorldNormal = (1 << 6),
69 WorldTangent = (1 << 7),
70 WorldBinormal = (1 << 8),
71 F0 = (1 << 9),
72 F90 = (1 << 10)
73 };
74
77
78 void setFeatureFlag(FeatureFlags flag, bool enabled = true)
79 {
80 if (enabled)
81 featureFlags |= static_cast<FeatureFlagsType>(flag);
82 else
83 featureFlags &= ~static_cast<FeatureFlagsType>(flag);
84 }
85
87 {
88 return (featureFlags & static_cast<FeatureFlagsType>(flag)) != 0;
89 }
90
92
94
97
99
101 {
102 return (body.size() > 0);
103 }
104
106 {
107 return propertyUniforms.size() > 0;
108 }
109
110};
111
113{
114public:
118 QSet<QByteArray> identifiers; // Used in the body, outside comments
119
120 bool needsLights = false;
123 bool needsCamera = false;
124 bool needsRhiProperties = false;
126 bool needsNormalTexture = false;
127 bool needsFrameIndex = false;
128 bool supportsMultiview = false; // Multiview is relevant when the compute shader uses screen textures or camera
129
133
135
137 {
138 return propertyUniforms.size() > 0;
139 }
140
141};
142
143
144
145struct Q_QUICK3DRUNTIMERENDER_EXPORT QSSGShaderFeatures
146{
147
148// There are a number of macros used to turn on or off various features.
149// This allows those features to be propagated into the shader cache's caching mechanism.
150// They will be translated into #define name value where value is 1 or zero depending on
151// if the feature is enabled or not.
152//
153// In snippets that use this feature the code would look something like this:
154
155/*
156#ifndef QSSG_ENABLE_<FEATURE>
157#define QSSG_ENABLE_<FEATURE> 0
158#endif
159
160void func()
161{
162 ...
163#if QSSG_ENABLE_<FEATURE>
164 ...
165#endif
166 ...
167}
168*/
169
170// NOTE: The order of these will affect generated keys, so re-ordering these
171// will break already baked shaders (e.g. shadergen).
172using FlagType = quint32;
173enum class Feature : FlagType
174{
175 LightProbe = (1 << 8),
176 IblOrientation = (1 << 9) + 1,
177 Ssm = (1 << 10) + 2,
178 Ssao = (1 << 11) + 3,
179 DepthPass = (1 << 12) + 4,
180 OrthoShadowPass = (1 << 13) + 5,
181 PerspectiveShadowPass = (1 << 14) + 6,
182 LinearTonemapping = (1 << 15) + 7,
183 AcesTonemapping = (1 << 16) + 8,
184 HejlDawsonTonemapping = (1 << 17) + 9,
185 FilmicTonemapping = (1 << 18) + 10,
186 RGBELightProbe = (1 << 19) + 11,
187 OpaqueDepthPrePass = (1 << 20) + 12,
188 ReflectionProbe = (1 << 21) + 13,
189 ReduceMaxNumLights = (1 << 22) + 14,
190 Lightmap = (1 << 23) + 15,
191 DisableMultiView = (1 << 24) + 16,
192 ForceIblExposure = (1 << 25) + 17,
193 NormalPass = (1 << 26) + 18,
194 UserRenderPass = (1 << 27) + 19,
195 MotionVector = (1 << 28) + 20,
196 LastFeature
197};
198
199static constexpr FlagType IndexMask = 0xff;
200static constexpr quint32 Count = (static_cast<FlagType>(Feature::LastFeature) & IndexMask);
201
202static const char *asDefineString(QSSGShaderFeatures::Feature feature);
203static Feature fromIndex(quint32 idx);
204
205constexpr bool isNull() const { return flags == 0; }
206constexpr bool isSet(Feature feature) const { return (static_cast<FlagType>(feature) & flags); }
207void set(Feature feature, bool val);
208
209FlagType flags = 0;
210
211inline friend bool operator==(QSSGShaderFeatures a, QSSGShaderFeatures b) { return a.flags == b.flags; }
212
213void disableTonemapping()
214{
215 set(Feature::LinearTonemapping, false);
216 set(Feature::AcesTonemapping, false);
217 set(Feature::FilmicTonemapping, false);
218 set(Feature::HejlDawsonTonemapping, false);
219 set(Feature::ForceIblExposure, false);
220}
221
222inline friend QDebug operator<<(QDebug stream, const QSSGShaderFeatures &features)
223{
224 QVarLengthArray<const char *, Count> enabledFeatureStrings;
225 for (quint32 idx = 0; idx < Count; ++idx) {
226 const Feature feature = fromIndex(idx);
227 if (features.isSet(feature))
228 enabledFeatureStrings.append(asDefineString(feature));
229 }
230 stream.nospace() << "QSSGShaderFeatures(";
231 for (int i = 0; i < enabledFeatureStrings.size(); ++i)
232 stream.nospace() << (i > 0 ? ", " : "") << enabledFeatureStrings[i];
233 stream.nospace() << ")";
234 return stream;
235}
236
237};
238
239Q_QUICK3DRUNTIMERENDER_EXPORT size_t qHash(QSSGShaderFeatures features) noexcept;
240
242{
246
247 explicit QSSGShaderCacheKey(const QByteArray &key = QByteArray()) : m_key(key), m_hashCode(0) {}
248
249 QSSGShaderCacheKey(const QSSGShaderCacheKey &other) = default;
251
252 static inline size_t generateHashCode(const QByteArray &key, QSSGShaderFeatures features)
253 {
254 return qHash(key) ^ qHash(features);
255 }
256
258 {
259 m_hashCode = generateHashCode(m_key, m_features);
260 }
261
262 bool operator==(const QSSGShaderCacheKey &inOther) const
263 {
264 return m_key == inOther.m_key && m_features == inOther.m_features;
265 }
266};
267
269{
270 return key.m_hashCode;
271}
272
273class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGShaderCache
274{
275 Q_DISABLE_COPY(QSSGShaderCache)
276public:
277 enum class ShaderType
278 {
279 Vertex = 0,
280 Fragment = 1
281 };
282
283 using InitBakerFunc = void (*)(QShaderBaker *baker, QRhi *rhi);
284private:
285 friend class QSSGBuiltInRhiShaderCache;
286
287 typedef QHash<QSSGShaderCacheKey, QSSGRhiShaderPipelinePtr> TRhiShaderMap;
288 QSSGRhiContext &m_rhiContext; // Not own, the RCI owns us and the QSSGRhiContext.
289 TRhiShaderMap m_rhiShaders;
290 QByteArray m_insertStr; // member to potentially reuse the allocation after clear
291 QByteArray m_cacheKeyStr; // same here
292 InitBakerFunc m_initBaker;
293 QQsbInMemoryCollection m_persistentShaderBakingCache;
294 QString m_persistentShaderStorageFileName;
295 QSSGBuiltInRhiShaderCache m_builtInShaders;
296
297 QSSGRhiShaderPipelinePtr loadBuiltinUncached(const QByteArray &inKey, int viewCount,
298 QSSGRhiShaderPipeline::StageFlags vertexStageFlags);
299
300 void addShaderPreprocessor(QByteArray &str,
301 const QByteArray &inKey,
302 ShaderType shaderType,
303 const QSSGShaderFeatures &inFeatures,
304 const QSSGUserShaderAugmentation &shaderAugmentation,
305 int viewCount);
306
307public:
308 QSSGShaderCache(QSSGRhiContext &ctx,
309 const InitBakerFunc initBakeFn = nullptr);
310 ~QSSGShaderCache();
311
312 QSSGRhiContext &rhiContext() { return m_rhiContext; }
313
314 void releaseCachedResources();
315
316 QQsbInMemoryCollection &persistentShaderBakingCache() { return m_persistentShaderBakingCache; }
317
319 const QSSGShaderFeatures &inFeatures);
320 bool hasRhiShaderPipeline(const QByteArray &inKey, const QSSGShaderFeatures &inFeatures) const;
321
323 const QByteArray &inKey,
324 const QSSGShaderFeatures &inFeatures,
325 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
326
328 const QSSGShaderFeatures &inFeatures,
329 QQsbCollection::Entry entry,
330 const QSSGRenderGraphObject &obj,
331 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
332
334 const QByteArray &inVert,
335 const QByteArray &inFrag,
336 const QSSGShaderFeatures &inFeatures,
337 QSSGRhiShaderPipeline::StageFlags stageFlags,
338 const QSSGUserShaderAugmentation &shaderAugmentation,
339 int viewCount,
340 bool perTargetCompilation,
341 const QByteArray &inCacheKey = {});
342
343 QSSGRhiShaderPipelinePtr compileComputePipeline(const QByteArray &inKey,
344 const QByteArray &inComp,
345 int viewCount,
346 bool perTargetCompilation,
347 const QByteArray &inCacheKey = {});
348
349 QSSGBuiltInRhiShaderCache &getBuiltInRhiShaders() { return m_builtInShaders; }
350
351 static QByteArray resourceFolder();
354 static void initBakerForPersistentUse(QShaderBaker *baker, QRhi *rhi);
355 static QList<QShaderVersion> glslTargetsForContext(const QSurfaceFormat &format, bool isGLESModule);
356 static bool isGlslEsTarget(QRhi *rhi);
357};
358
359namespace QtQuick3DEditorHelpers {
360namespace ShaderBaker
361{
362 enum class Status : quint8
363 {
366 };
367 using StatusCallback = void(*)(const QByteArray &descKey, Status status, const QString &err, QShader::Stage stage);
368 Q_QUICK3DRUNTIMERENDER_EXPORT void setStatusCallback(StatusCallback cb);
369}
370
371namespace ShaderCache
372{
373 // Used by DS and the QML puppet!
374 // Note: Needs to be called before any QSSGShaderCache instance is created.
375 Q_QUICK3DRUNTIMERENDER_EXPORT void setAutomaticDiskCache(bool enable);
376 Q_QUICK3DRUNTIMERENDER_EXPORT bool isAutomaticDiskCacheEnabled();
377}
378
379}
380
381QT_END_NAMESPACE
382
383#endif
const std::unique_ptr< QSSGRhiContext > & rhiContext() const
QSSGBuiltInRhiShaderCache & getBuiltInRhiShaders()
static bool isGlslEsTarget(QRhi *rhi)
QSSGRhiShaderPipelinePtr tryGetRhiShaderPipeline(const QByteArray &inKey, const QSSGShaderFeatures &inFeatures)
static void initBakerForPersistentUse(QShaderBaker *baker, QRhi *rhi)
static QByteArray shaderCollectionFile()
QSSGRhiShaderPipelinePtr compileGraphicsPipeline(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={})
bool hasRhiShaderPipeline(const QByteArray &inKey, const QSSGShaderFeatures &inFeatures) const
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={})
QSSGRhiShaderPipelinePtr compileComputePipeline(const QByteArray &inKey, const QByteArray &inComp, int viewCount, bool perTargetCompilation, const QByteArray &inCacheKey={})
void(*)(QShaderBaker *baker, QRhi *rhi) InitBakerFunc
PropertyUniformsList propertyUniforms
PropertyUniformsList propertyUniforms
void setFeatureFlag(FeatureFlags flag, bool enabled=true)
bool isFeatureFlagSet(FeatureFlags flag) const
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 QSurfaceFormat glContextFormat(QRhi *rhi, bool *isGLESModule)
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