Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqsbcollection_p.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
6//
7// W A R N I N G
8// -------------
9//
10// This file is not part of the Qt API. It exists purely as an
11// implementation detail. This header file may change from version to
12// version without notice, or even be removed.
13//
14// We mean it.
15//
16
17#ifndef QQSBCOLLECTION_H
18#define QQSBCOLLECTION_H
19
20#include <QtQuick3DUtils/private/qtquick3dutilsglobal_p.h>
21
22#include <QtCore/qfile.h>
23#include <QtCore/qset.h>
24#include <QtCore/qmap.h>
25
26#include <rhi/qshader.h>
27
29
30class QRhiShaderStage;
31
32class Q_QUICK3DUTILS_EXPORT QQsbCollection
33{
34public:
35 virtual ~QQsbCollection();
36
37 struct Q_QUICK3DUTILS_EXPORT Entry
38 {
39 // 'value' is optional. hashing and comparison are based solely on 'key'.
40 Entry() = default;
41 explicit Entry(const QByteArray &key) : key(key)
42 {
43 hashKey = qHash(key);
44 }
45 Entry(const QByteArray &key, qint64 value) : key(key), value(value)
46 {
47 hashKey = qHash(key);
48 }
49 bool isValid() const { return !key.isEmpty(); }
50 QByteArray key;
51 qint64 value = -1;
52 size_t hashKey;
53 };
54
55 using FeatureSet = QMap<QByteArray, bool>; // QMap so it is ordered by key
56
57 template<typename T>
58 static FeatureSet toFeatureSet(const T &ssgFeatureSet)
59 {
60 FeatureSet ret;
61 for (quint32 i = 0, end = T::Count; i != end; ++i) {
62 auto def = T::fromIndex(i);
63 if (ssgFeatureSet.isSet(def))
64 ret.insert(T::asDefineString(def), true);
65 }
66 return ret;
67 }
68
69 struct Q_QUICK3DUTILS_EXPORT EntryDesc {
70 QByteArray materialKey;
71 FeatureSet featureSet;
72 QShader vertShader;
73 QShader fragShader;
74 QByteArray generateSha() const;
75 static QByteArray generateSha(const QByteArray &materialKey, const FeatureSet &featureSet);
76 };
77
78 using EntryMap = QSet<Entry>;
79 virtual EntryMap availableEntries() const = 0;
80 virtual Entry addEntry(const QByteArray &key, const EntryDesc &entryDesc) = 0;
81 virtual bool extractEntry(Entry entry, EntryDesc &entryDesc) = 0;
82
83protected:
84 enum Version : quint8
85 {
86 Unknown,
87 One = 0x10,
88 Two = 0x20
89 };
90 bool readEndHeader(QDataStream &ds, qint64 *startPos, quint8 *version);
91 void writeEndHeader(QDataStream &ds, qint64 startPos, quint8 version, quint64 magic);
92 bool readEndHeader(QIODevice *device, EntryMap *entries, quint8 *version);
93 void writeEndHeader(QIODevice *device, const EntryMap &entries);
94};
95
96Q_DECLARE_TYPEINFO(QQsbCollection::Entry, Q_PRIMITIVE_TYPE);
97
98Q_QUICK3DUTILS_EXPORT QDataStream &operator<<(QDataStream &stream, const QQsbCollection::Entry &entry);
99Q_QUICK3DUTILS_EXPORT QDataStream &operator>>(QDataStream &stream, QQsbCollection::Entry &entry);
100Q_QUICK3DUTILS_EXPORT QDataStream &operator<<(QDataStream &stream, const QQsbCollection::EntryDesc &entryDesc);
101Q_QUICK3DUTILS_EXPORT QDataStream &operator>>(QDataStream &stream, QQsbCollection::EntryDesc &entryDesc);
102
103Q_QUICK3DUTILS_EXPORT size_t qHash(const QQsbCollection::Entry &entry, size_t);
104Q_QUICK3DUTILS_EXPORT bool operator==(const QQsbCollection::Entry &l, const QQsbCollection::Entry &r);
105
106// Simple implementation backed by a hash table. Save and load are explicit and
107// all data is read and written. The file format is compatible with other
108// implementations.
109class Q_QUICK3DUTILS_EXPORT QQsbInMemoryCollection : public QQsbCollection
110{
111public:
112 QQsbInMemoryCollection() = default;
113
114 EntryMap availableEntries() const override;
115 Entry addEntry(const QByteArray &key, const EntryDesc &entryDesc) override;
116 bool extractEntry(Entry entry, EntryDesc &entryDesc) override;
117
118 void clear();
119
120 bool load(const QString &filename);
121 bool save(const QString &filename);
122
123private:
124 Q_DISABLE_COPY(QQsbInMemoryCollection);
125
126 QHash<Entry, EntryDesc> entries;
127};
128
129// Serial, direct-to/from-QIODevice implementation.
130class Q_QUICK3DUTILS_EXPORT QQsbIODeviceCollection : public QQsbCollection
131{
132public:
133 enum MapMode
134 {
135 Read = QIODevice::ReadOnly,
136 Write = (QIODevice::WriteOnly | QIODevice::Truncate)
137 };
138
139 explicit QQsbIODeviceCollection(const QString &filePath);
140 explicit QQsbIODeviceCollection(QIODevice &dev);
141 ~QQsbIODeviceCollection();
142
143 bool map(MapMode mode);
144 void unmap();
145
146 EntryMap availableEntries() const override;
147 Entry addEntry(const QByteArray &key, const EntryDesc &entryDesc) override;
148 bool extractEntry(Entry entry, EntryDesc &entryDesc) override;
149
150 void dumpInfo();
151 static void dumpInfo(const QString &device);
152 static void dumpInfo(QIODevice &device);
153
154private:
155 Q_DISABLE_COPY(QQsbIODeviceCollection);
156
157 enum class DeviceOwner : quint8
158 {
159 Self,
160 Extern
161 };
162 QFile file;
163 QIODevice &device;
164 DeviceOwner devOwner = DeviceOwner::Self;
165 quint8 version = Version::Unknown;
166 EntryMap entries;
167};
168
169QT_END_NAMESPACE
170
171#endif // QQSBCOLLECTION_H
friend bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are equal, otherwise returns false.
Definition qbytearray.h:807
\inmodule QtCore\reentrant
Definition qdatastream.h:50
Combined button and popup list for selecting options.
QDataStream & operator>>(QDataStream &s, QKeyCombination &combination)
static const char * borderText()
static constexpr qint64 HeaderSize
static QString lockFileName(const QString &name)
static constexpr quint64 MagicaDS
static constexpr quint32 QtVersion
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
Definition qsize.h:192