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;
40using QSSGUserShaderFragmentOutputs = QVector<QByteArrayView>;
48using QSSGShaderDefineList = QList<QSSGShaderDefine>;
79 return (body.size() > 0);
84 return propertyUniforms.size() > 0;
102
103
104
105
106
107
108
109
110
111
112
113
114
118using FlagType = quint32;
119enum class Feature : FlagType
121 LightProbe = (1 << 8),
122 IblOrientation = (1 << 9) + 1,
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,
145static constexpr FlagType IndexMask = 0xff;
146static constexpr quint32 Count = (
static_cast<FlagType>(Feature::LastFeature) & IndexMask);
148static const char *asDefineString(QSSGShaderFeatures::Feature feature);
149static Feature fromIndex(quint32 idx);
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);
157inline friend bool operator==(QSSGShaderFeatures a, QSSGShaderFeatures b) {
return a.flags == b.flags; }
159void disableTonemapping()
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);
168inline friend QDebug operator<<(QDebug stream,
const QSSGShaderFeatures &features)
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));
176 stream.nospace() <<
"QSSGShaderFeatures(";
177 for (
int i = 0; i < enabledFeatureStrings.size(); ++i)
178 stream.nospace() << (i > 0 ?
", " :
"") << enabledFeatureStrings[i];
179 stream.nospace() <<
")";
185Q_QUICK3DRUNTIMERENDER_EXPORT size_t qHash(QSSGShaderFeatures features)
noexcept;
200 return qHash(key) ^ qHash(features);
205 m_hashCode = generateHashCode(m_key, m_features);
210 return m_key == inOther.m_key && m_features == inOther.m_features;
216 return key.m_hashCode;
221 Q_DISABLE_COPY(QSSGShaderCache)
223 enum class ShaderType
231 friend class QSSGBuiltInRhiShaderCache;
233 typedef QHash<QSSGShaderCacheKey, QSSGRhiShaderPipelinePtr> TRhiShaderMap;
234 QSSGRhiContext &m_rhiContext;
235 TRhiShaderMap m_rhiShaders;
236 QByteArray m_insertStr;
237 QByteArray m_cacheKeyStr;
239 QQsbInMemoryCollection m_persistentShaderBakingCache;
240 QString m_persistentShaderStorageFileName;
241 QSSGBuiltInRhiShaderCache m_builtInShaders;
243 QSSGRhiShaderPipelinePtr loadBuiltinUncached(
const QByteArray &inKey,
int viewCount,
244 QSSGRhiShaderPipeline::StageFlags vertexStageFlags);
246 void addShaderPreprocessor(QByteArray &str,
247 const QByteArray &inKey,
248 ShaderType shaderType,
249 const QSSGShaderFeatures &inFeatures,
254 QSSGShaderCache(QSSGRhiContext &ctx,
255 const InitBakerFunc initBakeFn =
nullptr);
260 void releaseCachedResources();
265 const QSSGShaderFeatures &inFeatures);
268 const QByteArray &inKey,
269 const QSSGShaderFeatures &inFeatures,
270 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
273 const QSSGShaderFeatures &inFeatures,
274 QQsbCollection::Entry entry,
275 const QSSGRenderGraphObject &obj,
276 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
279 const QByteArray &inVert,
280 const QByteArray &inFrag,
281 const QSSGShaderFeatures &inFeatures,
282 QSSGRhiShaderPipeline::StageFlags stageFlags,
285 bool perTargetCompilation);
303 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 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
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