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