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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "qqsbcollection_p.h"
#include <QtCore/QLockFile>
#include <QtCore/QSaveFile>
#include <QtCore/QCryptographicHash>
#include <rhi/qrhi.h>
QT_BEGIN_NAMESPACE
QQsbCollection::~QQsbCollection()
{
}
QDataStream &operator<<(QDataStream &stream, const QQsbCollection::Entry &entry)
{
return (stream << entry.key << entry.value);
}
QDataStream &operator>>(QDataStream &stream, QQsbCollection::Entry &entry)
{
QByteArray key;
qint64 value;
stream >> key >> value;
entry = QQsbCollection::Entry(key, value);
return stream;
}
size_t qHash(const QQsbCollection::Entry &entry, size_t)
{
return entry.hashKey;
}
bool operator==(const QQsbCollection::Entry &l, const QQsbCollection::Entry &r)
{
return (l.key == r.key);
}
QDataStream &operator<<(QDataStream &stream, const QQsbCollection::EntryDesc &entryDesc)
{
return (stream << entryDesc.materialKey
<< entryDesc.featureSet
<< entryDesc.vertShader.serialized()
<< entryDesc.fragShader.serialized());
}
QDataStream &operator>>(QDataStream &stream, QQsbCollection::EntryDesc &entryDesc)
{
QByteArray desc;
QQsbCollection::FeatureSet fs;
QByteArray vertData;
QByteArray fragData;
stream >> desc >> fs >> vertData >> fragData;
entryDesc.materialKey = desc;
entryDesc.featureSet = fs;
entryDesc.vertShader = QShader::fromSerialized(vertData);
entryDesc.fragShader = QShader::fromSerialized(fragData);
return stream;
}
static constexpr quint64 MagicaDS = 0x3933333335346337;
static constexpr qint64 HeaderSize = sizeof(qint64 /*startOffs*/) + sizeof(quint8 /*version*/) + sizeof(quint32 /*qtVersion*/) + sizeof(MagicaDS);
static constexpr quint32 QtVersion = (QT_VERSION_MAJOR << 16) | (QT_VERSION_MINOR << 8) | (QT_VERSION_PATCH);
bool QQsbCollection::readEndHeader(QDataStream &ds, qint64 *startPos, quint8 *version)
{
quint64 fileId = 0;
quint32 qtver = 0;
ds >> *startPos >> *version >> qtver >> fileId;
if (fileId != MagicaDS) {
qWarning("Corrupt qsbc file");
return false;
}
if (*version != Version::Two) {
qWarning("qsbc file has an unsupported version");
return false;
}
if (qtver != QtVersion) {
qWarning("qsbc file is for a different Qt version");
return false;
}
return true;
}
bool QQsbCollection::readEndHeader(QIODevice *device, EntryMap *entries, quint8 *version)
{
bool result = false;
const qint64 size = device->size();
if (device->seek(size - HeaderSize)) {
QDataStream ds(device);
ds.setVersion(QDataStream::Qt_6_0);
qint64 startPos = 0;
if (readEndHeader(ds, &startPos, version)) {
if (startPos >= 0 && startPos < size && device->seek(startPos)) {
ds >> *entries;
result = true;
}
}
}
return result;
}
void QQsbCollection::writeEndHeader(QDataStream &ds, qint64 startPos, quint8 version, quint64 magic)
{
ds << startPos << version << QtVersion << magic;
}
void QQsbCollection::writeEndHeader(QIODevice *device, const EntryMap &entries)
{
if (!device->atEnd()) {
device->seek(device->size() - 1);
Q_ASSERT(device->atEnd());
}
QDataStream ds(device);
ds.setVersion(QDataStream::Qt_6_0);
const qint64 startPos = device->pos();
ds << entries;
writeEndHeader(ds, startPos, quint8(Version::Two), MagicaDS);
}
QByteArray QQsbCollection::EntryDesc::generateSha(const QByteArray &materialKey, const FeatureSet &featureSet)
{
QCryptographicHash h(QCryptographicHash::Algorithm::Sha1);
h.addData(materialKey);
for (auto it = featureSet.cbegin(), end = featureSet.cend(); it != end; ++it) {
if (it.value())
h.addData(it.key());
}
return h.result().toHex();
}
QByteArray QQsbCollection::EntryDesc::generateSha() const
{
return generateSha(materialKey, featureSet);
}
QQsbCollection::EntryMap QQsbInMemoryCollection::availableEntries() const
{
return EntryMap(entries.keyBegin(), entries.keyEnd());
}
QQsbCollection::Entry QQsbInMemoryCollection::addEntry(const QByteArray &key, const EntryDesc &entryDesc)
{
Entry e(key);
if (!entries.contains(e)) {
entries.insert(e, entryDesc);
return e;
}
return {}; // can only add with a given key once
}
bool QQsbInMemoryCollection::extractEntry(Entry entry, EntryDesc &entryDesc)
{
auto it = entries.constFind(entry);
if (it != entries.constEnd()) {
entryDesc = *it;
return true;
}
return false;
}
void QQsbInMemoryCollection::clear()
{
entries.clear();
}
static inline QString lockFileName(const QString &name)
{
return name + QLatin1String(".lck");
}
bool QQsbInMemoryCollection::load(const QString &filename)
{
QLockFile lock(lockFileName(filename));
if (!lock.lock()) {
qWarning("Could not create shader cache lock file '%s'",
qPrintable(lock.fileName()));
return false;
}
QFile f(filename);
if (!f.open(QIODevice::ReadOnly)) {
qWarning("Failed to open qsbc file %s", qPrintable(filename));
return false;
}
EntryMap entryMap;
quint8 version = 0;
if (!readEndHeader(&f, &entryMap, &version)) {
qWarning("Ignoring qsbc file %s", qPrintable(filename));
return false;
}
f.seek(0);
const qint64 size = f.size();
clear();
for (const Entry &e : entryMap) {
const qint64 offset = e.value;
if (e.isValid() && offset >= 0 && size > offset && f.seek(offset)) {
QDataStream ds(&f);
ds.setVersion(QDataStream::Qt_6_0);
EntryDesc entryDesc;
ds >> entryDesc;
entries.insert(Entry(e.key), entryDesc);
}
}
return true;
}
bool QQsbInMemoryCollection::save(const QString &filename)
{
QLockFile lock(lockFileName(filename));
if (!lock.lock()) {
qWarning("Could not create shader cache lock file '%s'",
qPrintable(lock.fileName()));
return false;
}
#if QT_CONFIG(temporaryfile)
QSaveFile f(filename);
#else
QFile f(filename);
#endif
if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
qWarning("Failed to write qsbc file %s", qPrintable(filename));
return false;
}
QDataStream ds(&f);
ds.setVersion(QDataStream::Qt_6_0);
EntryMap entryMap;
for (auto it = entries.cbegin(), end = entries.cend(); it != end; ++it) {
const qint64 offset = f.pos();
ds << it.value();
entryMap.insert(Entry(it.key().key, offset));
}
writeEndHeader(&f, entryMap);
#if QT_CONFIG(temporaryfile)
return f.commit();
#else
return true;
#endif
}
QQsbIODeviceCollection::QQsbIODeviceCollection(const QString &filePath)
: file(filePath)
, device(file)
{
}
QQsbIODeviceCollection::QQsbIODeviceCollection(QIODevice &dev)
: device(dev)
, devOwner(DeviceOwner::Extern)
{
}
QQsbIODeviceCollection::~QQsbIODeviceCollection()
{
if (!entries.isEmpty() || device.isOpen())
unmap();
}
bool QQsbIODeviceCollection::map(MapMode mode)
{
if (device.isOpen()) {
// Make sure Truncate is set if we're writing.
if ((device.openMode() & QIODevice::WriteOnly) != 0) {
if ((device.openMode() & QIODevice::Truncate) == 0) {
qWarning("Open mode needs to have Truncate set for writing!");
return false;
}
if ((device.openMode() & QIODevice::Text) != 0) {
qWarning("Open mode can't have Text mode set!");
return false;
}
}
} else if (!device.open(QIODevice::OpenMode(mode))) {
qWarning("Unable to open device!");
return false;
}
if (mode == Write)
return true;
Q_ASSERT(mode == Read);
const bool ret = readEndHeader(&device, &entries, &version);
if (!ret)
unmap();
return ret;
}
void QQsbIODeviceCollection::unmap()
{
if (device.isOpen() && ((device.openMode() & Write) == Write)) {
if (!entries.isEmpty()) {
writeEndHeader(&device, entries);
} else {
if (devOwner == DeviceOwner::Self)
file.remove();
}
}
device.close();
entries.clear();
}
QQsbCollection::EntryMap QQsbIODeviceCollection::availableEntries() const
{
return entries;
}
QQsbCollection::Entry QQsbIODeviceCollection::addEntry(const QByteArray &key, const EntryDesc &entryDesc)
{
if (entries.contains(Entry(key)) || !map(MapMode::Write))
return {};
QDataStream ds(&device);
ds.setVersion(QDataStream::Qt_6_0);
const auto offset = device.pos();
ds << entryDesc;
Entry e(key, offset);
entries.insert(e);
return e;
}
bool QQsbIODeviceCollection::extractEntry(Entry entry, EntryDesc &entryDesc)
{
if (device.isOpen() && device.isReadable()) {
const qint64 offset = entry.value;
if (entry.isValid() && offset >= 0) {
const qint64 size = device.size();
if (size > offset && device.seek(offset)) {
QDataStream ds(&device);
ds.setVersion(QDataStream::Qt_6_0);
ds >> entryDesc;
return true;
}
} else {
qWarning("Entry not found id(%s), offset(%lld)", entry.key.constData(), entry.value);
}
} else {
qWarning("Unable to open file for reading");
}
return false;
}
static const char *borderText() { return "--------------------------------------------------------------------------------"; }
void QQsbIODeviceCollection::dumpInfo()
{
if (map(QQsbIODeviceCollection::Read)) {
qDebug("Number of entries in collection: %zu\n", size_t(entries.size()));
int i = 0;
qDebug("Qsbc version: %u", version);
for (const auto &e : std::as_const(entries)) {
qDebug("%s\n"
"Entry %d\n%s\n"
"Key: %s\n"
"Offset: %llu", borderText(), i++, borderText(), e.key.constData(), e.value);
QQsbCollection::EntryDesc ed;
if (extractEntry(e, ed)) {
qDebug() << ed.materialKey << Qt::endl
<< ed.featureSet << Qt::endl
<< ed.vertShader << Qt::endl
<< ed.fragShader;
} else {
qWarning("Extracting Qsb entry failed!");
}
}
}
unmap();
}
void QQsbIODeviceCollection::dumpInfo(const QString &file)
{
QQsbIODeviceCollection qsbc(file);
qsbc.dumpInfo();
}
void QQsbIODeviceCollection::dumpInfo(QIODevice &device)
{
QQsbIODeviceCollection qsbc(device);
qsbc.dumpInfo();
}
QT_END_NAMESPACE
|