Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5#ifndef QSSG_RENDER_SHADER_CACHE_H
6#define QSSG_RENDER_SHADER_CACHE_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtQuick3DRuntimeRender/private/qtquick3druntimerenderglobal_p.h>
20#include <QtQuick3DUtils/private/qssgdataref_p.h>
21#include <QtQuick3DUtils/private/qqsbcollection_p.h>
22
23#include <QtQuick3DRuntimeRender/private/qssgrhicontext_p.h>
24#include <QtQuick3DRuntimeRender/private/qssgrendererimplshaders_p.h>
25
26#include <QtCore/QString>
27#include <QtCore/qcryptographichash.h>
28#include <QtCore/QSharedPointer>
29#include <QtCore/QVector>
30
32
35class QShaderBaker;
36class QRhi;
37
38struct Q_QUICK3DRUNTIMERENDER_EXPORT QSSGShaderFeatures
39{
40
41// There are a number of macros used to turn on or off various features.
42// This allows those features to be propagated into the shader cache's caching mechanism.
43// They will be translated into #define name value where value is 1 or zero depending on
44// if the feature is enabled or not.
45//
46// In snippets that use this feature the code would look something like this:
47
48/*
49#ifndef QSSG_ENABLE_<FEATURE>
50#define QSSG_ENABLE_<FEATURE> 0
51#endif
52
53void func()
54{
55 ...
56#if QSSG_ENABLE_<FEATURE>
57 ...
58#endif
59 ...
60}
61*/
62
63// NOTE: The order of these will affect generated keys, so re-ordering these
64// will break already baked shaders (e.g. shadergen).
66enum class Feature : FlagType
67{
68 LightProbe = (1 << 8),
69 IblOrientation = (1 << 9) + 1,
70 Ssm = (1 << 10) + 2,
71 Ssao = (1 << 11) + 3,
72 DepthPass = (1 << 12) + 4,
73 OrthoShadowPass = (1 << 13) + 5,
74 CubeShadowPass = (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
87 LastFeature
88};
89
90static constexpr FlagType IndexMask = 0xff;
91static constexpr quint32 Count = (static_cast<FlagType>(Feature::LastFeature) & IndexMask);
92
93static const char *asDefineString(QSSGShaderFeatures::Feature feature);
94static Feature fromIndex(quint32 idx);
95
96constexpr bool isNull() const { return flags == 0; }
97constexpr bool isSet(Feature feature) const { return (static_cast<FlagType>(feature) & flags); }
98void set(Feature feature, bool val);
99
101
102inline friend bool operator==(QSSGShaderFeatures a, QSSGShaderFeatures b) { return a.flags == b.flags; }
103
105{
106 set(Feature::LinearTonemapping, false);
107 set(Feature::AcesTonemapping, false);
108 set(Feature::FilmicTonemapping, false);
109 set(Feature::HejlDawsonTonemapping, false);
110 set(Feature::ForceIblExposure, false);
111}
112
113inline friend QDebug operator<<(QDebug stream, const QSSGShaderFeatures &features)
114{
115 QVarLengthArray<const char *, Count> enabledFeatureStrings;
116 for (quint32 idx = 0; idx < Count; ++idx) {
117 const Feature feature = fromIndex(idx);
118 if (features.isSet(feature))
119 enabledFeatureStrings.append(asDefineString(feature));
120 }
121 stream.nospace() << "QSSGShaderFeatures(";
122 for (int i = 0; i < enabledFeatureStrings.size(); ++i)
123 stream.nospace() << (i > 0 ? ", " : "") << enabledFeatureStrings[i];
124 stream.nospace() << ")";
125 return stream;
126}
127
128};
129
130Q_QUICK3DRUNTIMERENDER_EXPORT size_t qHash(QSSGShaderFeatures features) noexcept;
131
133{
136 size_t m_hashCode = 0;
137
139
142
143 static inline size_t generateHashCode(const QByteArray &key, QSSGShaderFeatures features)
144 {
145 return qHash(key) ^ qHash(features);
146 }
147
152
153 bool operator==(const QSSGShaderCacheKey &inOther) const
154 {
155 return m_key == inOther.m_key && m_features == inOther.m_features;
156 }
157};
158
159inline size_t qHash(const QSSGShaderCacheKey &key)
160{
161 return key.m_hashCode;
162}
163
164class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGShaderCache
165{
166 Q_DISABLE_COPY(QSSGShaderCache)
167public:
168 enum class ShaderType
169 {
170 Vertex = 0,
171 Fragment = 1
172 };
173
174 using InitBakerFunc = void (*)(QShaderBaker *baker, QRhi *rhi);
175private:
177
178 typedef QHash<QSSGShaderCacheKey, QSSGRhiShaderPipelinePtr> TRhiShaderMap;
179 QSSGRhiContext &m_rhiContext; // Not own, the RCI owns us and the QSSGRhiContext.
180 TRhiShaderMap m_rhiShaders;
181 QByteArray m_insertStr; // member to potentially reuse the allocation after clear
182 QByteArray m_cacheKeyStr; // same here
183 InitBakerFunc m_initBaker;
184 QQsbInMemoryCollection m_persistentShaderBakingCache;
185 QString m_persistentShaderStorageFileName;
186 QSSGBuiltInRhiShaderCache m_builtInShaders;
187
188 QSSGRhiShaderPipelinePtr loadBuiltinUncached(const QByteArray &inKey, int viewCount);
189
190 void addShaderPreprocessor(QByteArray &str,
191 const QByteArray &inKey,
192 ShaderType shaderType,
193 const QSSGShaderFeatures &inFeatures,
194 int viewCount);
195
196public:
198 const InitBakerFunc initBakeFn = nullptr);
200
201 void releaseCachedResources();
202
203 QQsbInMemoryCollection &persistentShaderBakingCache() { return m_persistentShaderBakingCache; }
204
205 QSSGRhiShaderPipelinePtr tryGetRhiShaderPipeline(const QByteArray &inKey,
206 const QSSGShaderFeatures &inFeatures);
207
208 QSSGRhiShaderPipelinePtr tryNewPipelineFromPersistentCache(const QByteArray &qsbcKey,
209 const QByteArray &inKey,
210 const QSSGShaderFeatures &inFeatures,
211 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
212
213 QSSGRhiShaderPipelinePtr newPipelineFromPregenerated(const QByteArray &inKey,
214 const QSSGShaderFeatures &inFeatures,
217 QSSGRhiShaderPipeline::StageFlags stageFlags = {});
218
219 QSSGRhiShaderPipelinePtr compileForRhi(const QByteArray &inKey,
220 const QByteArray &inVert,
221 const QByteArray &inFrag,
222 const QSSGShaderFeatures &inFeatures,
223 QSSGRhiShaderPipeline::StageFlags stageFlags,
224 int viewCount,
225 bool perTargetCompilation);
226
227 QSSGBuiltInRhiShaderCache &getBuiltInRhiShaders() { return m_builtInShaders; }
228
229 static QByteArray resourceFolder();
230 static QByteArray shaderCollectionFile();
231};
232
233namespace QtQuick3DEditorHelpers {
234namespace ShaderBaker
235{
236 enum class Status : quint8
237 {
238 Success,
239 Error
240 };
241 using StatusCallback = void(*)(const QByteArray &descKey, Status status, const QString &err, QShader::Stage stage);
242 Q_QUICK3DRUNTIMERENDER_EXPORT void setStatusCallback(StatusCallback cb);
243}
244
245namespace ShaderCache
246{
247 // Used by DS and the QML puppet!
248 // Note: Needs to be called before any QSSGShaderCache instance is created.
249 Q_QUICK3DRUNTIMERENDER_EXPORT void setAutomaticDiskCache(bool enable);
250 Q_QUICK3DRUNTIMERENDER_EXPORT bool isAutomaticDiskCacheEnabled();
251}
252
253}
254
256
257#endif
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:1804
\inmodule QtQuick3D
QSSGBuiltInRhiShaderCache & getBuiltInRhiShaders()
QQsbInMemoryCollection & persistentShaderBakingCache()
void(*)(QShaderBaker *baker, QRhi *rhi) InitBakerFunc
Stage
Describes the stage of the graphics pipeline the shader is suitable for.
Definition qshader.h:83
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
EGLContext ctx
QString str
[2]
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()
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
EGLStreamKHR stream
GLboolean GLboolean GLboolean b
GLuint64 key
GLboolean GLboolean GLboolean GLboolean a
[7]
GLbitfield flags
GLboolean enable
GLhandleARB obj
[2]
GLuint GLfloat * val
GLuint entry
Q_QUICK3DRUNTIMERENDER_EXPORT size_t qHash(QSSGShaderFeatures features) noexcept
std::shared_ptr< QSSGRhiShaderPipeline > QSSGRhiShaderPipelinePtr
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
unsigned int quint32
Definition qtypes.h:50
unsigned char quint8
Definition qtypes.h:46
QFuture< QSet< QChar > > set
[10]
QSharedPointer< T > other(t)
[5]
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
constexpr bool isSet(Feature feature) const
friend QDebug operator<<(QDebug stream, const QSSGShaderFeatures &features)
friend bool operator==(QSSGShaderFeatures a, QSSGShaderFeatures b)
constexpr bool isNull() const
\qmltype MapCircle \instantiates QDeclarativeCircleMapItem \inqmlmodule QtLocation