7#ifndef QSSG_RENDER_SHADER_CACHE_H
8#define QSSG_RENDER_SHADER_CACHE_H
21#include <QtQuick3DRuntimeRender/private/qtquick3druntimerenderglobal_p.h>
22#include <QtQuick3DUtils/private/qssgdataref_p.h>
23#include <QtQuick3DUtils/private/qqsbcollection_p.h>
25#include <QtQuick3DRuntimeRender/private/qssgrhicontext_p.h>
26#include <QtQuick3DRuntimeRender/private/qssgrendererimplshaders_p.h>
28#include <QtCore/QString>
29#include <QtCore/qcryptographichash.h>
30#include <QtCore/QSharedPointer>
31#include <QtCore/QVector>
35class QSSGRenderContextInterface;
36class QSSGRhiShaderPipeline;
41using QSSGUserShaderFragmentOutputs = QVector<QByteArrayView>;
49using QSSGShaderDefineList = QList<QSSGShaderDefine>;
81 return (body.size() > 0);
86 return propertyUniforms.size() > 0;
104
105
106
107
108
109
110
111
112
113
114
115
116
120using FlagType = quint32;
121enum class Feature : FlagType
123 LightProbe = (1 << 8),
124 IblOrientation = (1 << 9) + 1,
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,
147static constexpr FlagType IndexMask = 0xff;
148static constexpr quint32 Count = (
static_cast<FlagType>(Feature::LastFeature) & IndexMask);
150static const char *asDefineString(QSSGShaderFeatures::Feature feature);
151static Feature fromIndex(quint32 idx);
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);
159inline friend bool operator==(QSSGShaderFeatures a, QSSGShaderFeatures b) {
return a.flags == b.flags; }
161void disableTonemapping()
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);
170inline friend QDebug operator<<(QDebug stream,
const QSSGShaderFeatures &features)
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));
178 stream.nospace() <<
"QSSGShaderFeatures(";
179 for (
int i = 0; i < enabledFeatureStrings.size(); ++i)
180 stream.nospace() << (i > 0 ?
", " :
"") << enabledFeatureStrings[i];
181 stream.nospace() <<
")";
187Q_QUICK3DRUNTIMERENDER_EXPORT size_t qHash(QSSGShaderFeatures features)
noexcept;
202 return qHash(key) ^ qHash(features);
207 m_hashCode = generateHashCode(m_key, m_features);
212 return m_key == inOther.m_key && m_features == inOther.m_features;
218 return key.m_hashCode;
223 Q_DISABLE_COPY(QSSGShaderCache)
225 enum class ShaderType
233 friend class QSSGBuiltInRhiShaderCache;
235 typedef QHash<QSSGShaderCacheKey, QSSGRhiShaderPipelinePtr> TRhiShaderMap;
236 QSSGRhiContext &m_rhiContext;
237 TRhiShaderMap m_rhiShaders;
238 QByteArray m_insertStr;
239 QByteArray m_cacheKeyStr;
241 QQsbInMemoryCollection m_persistentShaderBakingCache;
242 QString m_persistentShaderStorageFileName;
243 QSSGBuiltInRhiShaderCache m_builtInShaders;
245 QSSGRhiShaderPipelinePtr loadBuiltinUncached(
const QByteArray &inKey,
int viewCount,
246 QSSGRhiShaderPipeline::StageFlags vertexStageFlags);
248 void addShaderPreprocessor(QByteArray &str,
249 const QByteArray &inKey,
250 ShaderType shaderType,
251 const QSSGShaderFeatures &inFeatures,
256 QSSGShaderCache(QSSGRhiContext &ctx,
257 const InitBakerFunc initBakeFn =
nullptr);
262 void releaseCachedResources();
267 const QSSGShaderFeatures &inFeatures);
270 const QByteArray &inKey,
271 const QSSGShaderFeatures &inFeatures,
272 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
275 const QSSGShaderFeatures &inFeatures,
276 QQsbCollection::Entry entry,
277 const QSSGRenderGraphObject &obj,
278 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
281 const QByteArray &inVert,
282 const QByteArray &inFrag,
283 const QSSGShaderFeatures &inFeatures,
284 QSSGRhiShaderPipeline::StageFlags stageFlags,
287 bool perTargetCompilation,
288 const QByteArray &inCacheKey = {});
307 using StatusCallback =
void(*)(
const QByteArray &descKey,
Status status,
const QString &err, QShader::Stage stage);
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
bool hasPropertyUniforms() const
PropertyUniformsList propertyUniforms
bool hasUserAugmentation() const
QSSGShaderDefineList defines
QSSGUserShaderFragmentOutputs outputs
constexpr QSSGCameraId getCameraId(const QSSGRenderGraphObject &o)
constexpr QSSGResourceId getResourceId(const QSSGRenderGraphObject &o)
T * getExtension(QSSGExtensionId extensionId)
T * getNode(QSSGNodeId nodeId)
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