5#ifndef QSSG_RENDER_SHADER_CACHE_H
6#define QSSG_RENDER_SHADER_CACHE_H
19#include <QtQuick3DRuntimeRender/private/qtquick3druntimerenderglobal_p.h>
20#include <QtQuick3DUtils/private/qssgdataref_p.h>
21#include <QtQuick3DUtils/private/qqsbcollection_p.h>
23#include <QtQuick3DRuntimeRender/private/qssgrhicontext_p.h>
24#include <QtQuick3DRuntimeRender/private/qssgrendererimplshaders_p.h>
26#include <QtCore/QString>
27#include <QtCore/qcryptographichash.h>
28#include <QtCore/QSharedPointer>
29#include <QtCore/QVector>
33class QSSGRenderContextInterface;
34class QSSGRhiShaderPipeline;
49
50
51
52
53
54
55
56
57
58
59
60
61
65using FlagType = quint32;
66enum class Feature : FlagType
68 LightProbe = (1 << 8),
69 IblOrientation = (1 << 9) + 1,
72 DepthPass = (1 << 12) + 4,
73 OrthoShadowPass = (1 << 13) + 5,
74 PerspectiveShadowPass = (1 << 14) + 6,
75 LinearTonemapping = (1 << 15) + 7,
76 AcesTonemapping = (1 << 16) + 8,
77 HejlDawsonTonemapping = (1 << 17) + 9,
78 FilmicTonemapping = (1 << 18) + 10,
79 RGBELightProbe = (1 << 19) + 11,
80 OpaqueDepthPrePass = (1 << 20) + 12,
81 ReflectionProbe = (1 << 21) + 13,
82 ReduceMaxNumLights = (1 << 22) + 14,
83 Lightmap = (1 << 23) + 15,
84 DisableMultiView = (1 << 24) + 16,
85 ForceIblExposure = (1 << 25) + 17,
86 NormalPass = (1 << 26) + 18,
91static constexpr FlagType IndexMask = 0xff;
92static constexpr quint32 Count = (
static_cast<FlagType>(Feature::LastFeature) & IndexMask);
94static const char *asDefineString(QSSGShaderFeatures::Feature feature);
95static Feature fromIndex(quint32 idx);
97constexpr bool isNull()
const {
return flags == 0; }
98constexpr bool isSet(Feature feature)
const {
return (
static_cast<FlagType>(feature) & flags); }
99void set(Feature feature,
bool val);
103inline friend bool operator==(QSSGShaderFeatures a, QSSGShaderFeatures b) {
return a.flags == b.flags; }
105void disableTonemapping()
107 set(Feature::LinearTonemapping,
false);
108 set(Feature::AcesTonemapping,
false);
109 set(Feature::FilmicTonemapping,
false);
110 set(Feature::HejlDawsonTonemapping,
false);
111 set(Feature::ForceIblExposure,
false);
114inline friend QDebug operator<<(QDebug stream,
const QSSGShaderFeatures &features)
116 QVarLengthArray<
const char *, Count> enabledFeatureStrings;
117 for (quint32 idx = 0; idx < Count; ++idx) {
118 const Feature feature = fromIndex(idx);
119 if (features.isSet(feature))
120 enabledFeatureStrings.append(asDefineString(feature));
122 stream.nospace() <<
"QSSGShaderFeatures(";
123 for (
int i = 0; i < enabledFeatureStrings.size(); ++i)
124 stream.nospace() << (i > 0 ?
", " :
"") << enabledFeatureStrings[i];
125 stream.nospace() <<
")";
131Q_QUICK3DRUNTIMERENDER_EXPORT size_t qHash(QSSGShaderFeatures features)
noexcept;
146 return qHash(key) ^ qHash(features);
151 m_hashCode = generateHashCode(m_key, m_features);
156 return m_key == inOther.m_key && m_features == inOther.m_features;
162 return key.m_hashCode;
167 Q_DISABLE_COPY(QSSGShaderCache)
169 enum class ShaderType
177 friend class QSSGBuiltInRhiShaderCache;
179 typedef QHash<QSSGShaderCacheKey, QSSGRhiShaderPipelinePtr> TRhiShaderMap;
180 QSSGRhiContext &m_rhiContext;
181 TRhiShaderMap m_rhiShaders;
182 QByteArray m_insertStr;
183 QByteArray m_cacheKeyStr;
185 QQsbInMemoryCollection m_persistentShaderBakingCache;
186 QString m_persistentShaderStorageFileName;
187 QSSGBuiltInRhiShaderCache m_builtInShaders;
189 QSSGRhiShaderPipelinePtr loadBuiltinUncached(
const QByteArray &inKey,
int viewCount);
191 void addShaderPreprocessor(QByteArray &str,
192 const QByteArray &inKey,
193 ShaderType shaderType,
194 const QSSGShaderFeatures &inFeatures,
198 QSSGShaderCache(QSSGRhiContext &ctx,
199 const InitBakerFunc initBakeFn =
nullptr);
202 void releaseCachedResources();
207 const QSSGShaderFeatures &inFeatures);
210 const QByteArray &inKey,
211 const QSSGShaderFeatures &inFeatures,
212 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
215 const QSSGShaderFeatures &inFeatures,
216 QQsbCollection::Entry entry,
217 const QSSGRenderGraphObject &obj,
218 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
221 const QByteArray &inVert,
222 const QByteArray &inFrag,
223 const QSSGShaderFeatures &inFeatures,
224 QSSGRhiShaderPipeline::StageFlags stageFlags,
226 bool perTargetCompilation);
242 using StatusCallback =
void(*)(
const QByteArray &descKey,
Status status,
const QString &err, QShader::Stage stage);
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={})
QSSGRhiShaderPipelinePtr compileForRhi(const QByteArray &inKey, const QByteArray &inVert, const QByteArray &inFrag, const QSSGShaderFeatures &inFeatures, QSSGRhiShaderPipeline::StageFlags stageFlags, int viewCount, bool perTargetCompilation)
QSSGRhiShaderPipelinePtr tryNewPipelineFromPersistentCache(const QByteArray &qsbcKey, const QByteArray &inKey, const QSSGShaderFeatures &inFeatures, QSSGRhiShaderPipeline::StageFlags stageFlags={})
void(*)(QShaderBaker *baker, QRhi *rhi) InitBakerFunc
constexpr QSSGCameraId getCameraId(const QSSGRenderGraphObject &o)
constexpr QSSGResourceId getResourceId(const QSSGRenderGraphObject &o)
T * getExtension(QSSGExtensionId extensionId)
T * getNode(QSSGNodeId nodeId)
T * getResource(QSSGResourceId resId)
constexpr QSSGExtensionId getExtensionId(const QSSGRenderGraphObject &o)
constexpr bool isNull(QSSGTypeId id)
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
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 void initBakerForPersistentUse(QShaderBaker *, QRhi *)
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