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>;
80 featureFlags |=
static_cast<FeatureFlagsType>(flag);
82 featureFlags &= ~
static_cast<FeatureFlagsType>(flag);
87 return (featureFlags &
static_cast<FeatureFlagsType>(flag)) != 0;
101 return (body.size() > 0);
106 return propertyUniforms.size() > 0;
124
125
126
127
128
129
130
131
132
133
134
135
136
140using FlagType = quint32;
141enum class Feature : FlagType
143 LightProbe = (1 << 8),
144 IblOrientation = (1 << 9) + 1,
146 Ssao = (1 << 11) + 3,
147 DepthPass = (1 << 12) + 4,
148 OrthoShadowPass = (1 << 13) + 5,
149 PerspectiveShadowPass = (1 << 14) + 6,
150 LinearTonemapping = (1 << 15) + 7,
151 AcesTonemapping = (1 << 16) + 8,
152 HejlDawsonTonemapping = (1 << 17) + 9,
153 FilmicTonemapping = (1 << 18) + 10,
154 RGBELightProbe = (1 << 19) + 11,
155 OpaqueDepthPrePass = (1 << 20) + 12,
156 ReflectionProbe = (1 << 21) + 13,
157 ReduceMaxNumLights = (1 << 22) + 14,
158 Lightmap = (1 << 23) + 15,
159 DisableMultiView = (1 << 24) + 16,
160 ForceIblExposure = (1 << 25) + 17,
161 NormalPass = (1 << 26) + 18,
162 UserRenderPass = (1 << 27) + 19,
163 MotionVector = (1 << 28) + 20,
167static constexpr FlagType IndexMask = 0xff;
168static constexpr quint32 Count = (
static_cast<FlagType>(Feature::LastFeature) & IndexMask);
170static const char *asDefineString(QSSGShaderFeatures::Feature feature);
171static Feature fromIndex(quint32 idx);
173constexpr bool isNull()
const {
return flags == 0; }
174constexpr bool isSet(Feature feature)
const {
return (
static_cast<FlagType>(feature) & flags); }
175void set(Feature feature,
bool val);
179inline friend bool operator==(QSSGShaderFeatures a, QSSGShaderFeatures b) {
return a.flags == b.flags; }
181void disableTonemapping()
183 set(Feature::LinearTonemapping,
false);
184 set(Feature::AcesTonemapping,
false);
185 set(Feature::FilmicTonemapping,
false);
186 set(Feature::HejlDawsonTonemapping,
false);
187 set(Feature::ForceIblExposure,
false);
190inline friend QDebug operator<<(QDebug stream,
const QSSGShaderFeatures &features)
192 QVarLengthArray<
const char *, Count> enabledFeatureStrings;
193 for (quint32 idx = 0; idx < Count; ++idx) {
194 const Feature feature = fromIndex(idx);
195 if (features.isSet(feature))
196 enabledFeatureStrings.append(asDefineString(feature));
198 stream.nospace() <<
"QSSGShaderFeatures(";
199 for (
int i = 0; i < enabledFeatureStrings.size(); ++i)
200 stream.nospace() << (i > 0 ?
", " :
"") << enabledFeatureStrings[i];
201 stream.nospace() <<
")";
207Q_QUICK3DRUNTIMERENDER_EXPORT size_t qHash(QSSGShaderFeatures features)
noexcept;
222 return qHash(key) ^ qHash(features);
227 m_hashCode = generateHashCode(m_key, m_features);
232 return m_key == inOther.m_key && m_features == inOther.m_features;
238 return key.m_hashCode;
243 Q_DISABLE_COPY(QSSGShaderCache)
245 enum class ShaderType
253 friend class QSSGBuiltInRhiShaderCache;
255 typedef QHash<QSSGShaderCacheKey, QSSGRhiShaderPipelinePtr> TRhiShaderMap;
256 QSSGRhiContext &m_rhiContext;
257 TRhiShaderMap m_rhiShaders;
258 QByteArray m_insertStr;
259 QByteArray m_cacheKeyStr;
261 QQsbInMemoryCollection m_persistentShaderBakingCache;
262 QString m_persistentShaderStorageFileName;
263 QSSGBuiltInRhiShaderCache m_builtInShaders;
265 QSSGRhiShaderPipelinePtr loadBuiltinUncached(
const QByteArray &inKey,
int viewCount,
266 QSSGRhiShaderPipeline::StageFlags vertexStageFlags);
268 void addShaderPreprocessor(QByteArray &str,
269 const QByteArray &inKey,
270 ShaderType shaderType,
271 const QSSGShaderFeatures &inFeatures,
276 QSSGShaderCache(QSSGRhiContext &ctx,
277 const InitBakerFunc initBakeFn =
nullptr);
282 void releaseCachedResources();
287 const QSSGShaderFeatures &inFeatures);
290 const QByteArray &inKey,
291 const QSSGShaderFeatures &inFeatures,
292 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
295 const QSSGShaderFeatures &inFeatures,
296 QQsbCollection::Entry entry,
297 const QSSGRenderGraphObject &obj,
298 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
301 const QByteArray &inVert,
302 const QByteArray &inFrag,
303 const QSSGShaderFeatures &inFeatures,
304 QSSGRhiShaderPipeline::StageFlags stageFlags,
307 bool perTargetCompilation,
308 const QByteArray &inCacheKey = {});
311 const QByteArray &inComp,
313 bool perTargetCompilation,
314 const QByteArray &inCacheKey = {});
333 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)
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={})
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
bool hasPropertyUniforms() const
PropertyUniformsList propertyUniforms
bool hasUserAugmentation() const
void setFeatureFlag(FeatureFlags flag, bool enabled=true)
FeatureFlagsType featureFlags
QSSGShaderDefineList defines
bool isFeatureFlagSet(FeatureFlags flag) const
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 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