Convert string tables in QShaderDescription to use string literals [2/2]

Avoid allocating all these strings on load. Part 2: JSON keys.

Pick-to: 6.3 6.2
Change-Id: I1724a58d700509c3af90ad1b87cb1bccae1075b8
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Christian Strømme <christian.stromme@qt.io>
This commit is contained in:
Marc Mutz 2022-02-04 10:13:27 +01:00
parent bfd21b888b
commit e128ed2c5d

View File

@ -806,53 +806,55 @@ QDebug operator<<(QDebug dbg, const QShaderDescription::StorageBlock &blk)
} }
#endif #endif
static const QString nameKey = QLatin1String("name"); #define JSON_KEY(key) static constexpr QLatin1String key ## Key() noexcept { return QLatin1String( #key ); }
static const QString typeKey = QLatin1String("type"); JSON_KEY(name)
static const QString locationKey = QLatin1String("location"); JSON_KEY(type)
static const QString bindingKey = QLatin1String("binding"); JSON_KEY(location)
static const QString setKey = QLatin1String("set"); JSON_KEY(binding)
static const QString imageFormatKey = QLatin1String("imageFormat"); JSON_KEY(set)
static const QString imageFlagsKey = QLatin1String("imageFlags"); JSON_KEY(imageFormat)
static const QString offsetKey = QLatin1String("offset"); JSON_KEY(imageFlags)
static const QString arrayDimsKey = QLatin1String("arrayDims"); JSON_KEY(offset)
static const QString arrayStrideKey = QLatin1String("arrayStride"); JSON_KEY(arrayDims)
static const QString matrixStrideKey = QLatin1String("matrixStride"); JSON_KEY(arrayStride)
static const QString matrixRowMajorKey = QLatin1String("matrixRowMajor"); JSON_KEY(matrixStride)
static const QString structMembersKey = QLatin1String("structMembers"); JSON_KEY(matrixRowMajor)
static const QString membersKey = QLatin1String("members"); JSON_KEY(structMembers)
static const QString inputsKey = QLatin1String("inputs"); JSON_KEY(members)
static const QString outputsKey = QLatin1String("outputs"); JSON_KEY(inputs)
static const QString uniformBlocksKey = QLatin1String("uniformBlocks"); JSON_KEY(outputs)
static const QString blockNameKey = QLatin1String("blockName"); JSON_KEY(uniformBlocks)
static const QString structNameKey = QLatin1String("structName"); JSON_KEY(blockName)
static const QString instanceNameKey = QLatin1String("instanceName"); JSON_KEY(structName)
static const QString sizeKey = QLatin1String("size"); JSON_KEY(instanceName)
static const QString knownSizeKey = QLatin1String("knownSize"); JSON_KEY(size)
static const QString pushConstantBlocksKey = QLatin1String("pushConstantBlocks"); JSON_KEY(knownSize)
static const QString storageBlocksKey = QLatin1String("storageBlocks"); JSON_KEY(pushConstantBlocks)
static const QString combinedImageSamplersKey = QLatin1String("combinedImageSamplers"); JSON_KEY(storageBlocks)
static const QString storageImagesKey = QLatin1String("storageImages"); JSON_KEY(combinedImageSamplers)
static const QString localSizeKey = QLatin1String("localSize"); JSON_KEY(storageImages)
static const QString separateImagesKey = QLatin1String("separateImages"); JSON_KEY(localSize)
static const QString separateSamplersKey = QLatin1String("separateSamplers"); JSON_KEY(separateImages)
JSON_KEY(separateSamplers)
#undef JSON_KEY
static void addDeco(QJsonObject *obj, const QShaderDescription::InOutVariable &v) static void addDeco(QJsonObject *obj, const QShaderDescription::InOutVariable &v)
{ {
if (v.location >= 0) if (v.location >= 0)
(*obj)[locationKey] = v.location; (*obj)[locationKey()] = v.location;
if (v.binding >= 0) if (v.binding >= 0)
(*obj)[bindingKey] = v.binding; (*obj)[bindingKey()] = v.binding;
if (v.descriptorSet >= 0) if (v.descriptorSet >= 0)
(*obj)[setKey] = v.descriptorSet; (*obj)[setKey()] = v.descriptorSet;
if (v.imageFormat != QShaderDescription::ImageFormatUnknown) if (v.imageFormat != QShaderDescription::ImageFormatUnknown)
(*obj)[imageFormatKey] = imageFormatStr(v.imageFormat); (*obj)[imageFormatKey()] = imageFormatStr(v.imageFormat);
if (v.imageFlags) if (v.imageFlags)
(*obj)[imageFlagsKey] = int(v.imageFlags); (*obj)[imageFlagsKey()] = int(v.imageFlags);
if (!v.arrayDims.isEmpty()) { if (!v.arrayDims.isEmpty()) {
QJsonArray dimArr; QJsonArray dimArr;
for (int dim : v.arrayDims) for (int dim : v.arrayDims)
dimArr.append(dim); dimArr.append(dim);
(*obj)[arrayDimsKey] = dimArr; (*obj)[arrayDimsKey()] = dimArr;
} }
} }
@ -871,8 +873,8 @@ static void serializeDecorations(QDataStream *stream, const QShaderDescription::
static QJsonObject inOutObject(const QShaderDescription::InOutVariable &v) static QJsonObject inOutObject(const QShaderDescription::InOutVariable &v)
{ {
QJsonObject obj; QJsonObject obj;
obj[nameKey] = QString::fromUtf8(v.name); obj[nameKey()] = QString::fromUtf8(v.name);
obj[typeKey] = typeStr(v.type); obj[typeKey()] = typeStr(v.type);
addDeco(&obj, v); addDeco(&obj, v);
return obj; return obj;
} }
@ -887,27 +889,27 @@ static void serializeInOutVar(QDataStream *stream, const QShaderDescription::InO
static QJsonObject blockMemberObject(const QShaderDescription::BlockVariable &v) static QJsonObject blockMemberObject(const QShaderDescription::BlockVariable &v)
{ {
QJsonObject obj; QJsonObject obj;
obj[nameKey] = QString::fromUtf8(v.name); obj[nameKey()] = QString::fromUtf8(v.name);
obj[typeKey] = typeStr(v.type); obj[typeKey()] = typeStr(v.type);
obj[offsetKey] = v.offset; obj[offsetKey()] = v.offset;
obj[sizeKey] = v.size; obj[sizeKey()] = v.size;
if (!v.arrayDims.isEmpty()) { if (!v.arrayDims.isEmpty()) {
QJsonArray dimArr; QJsonArray dimArr;
for (int dim : v.arrayDims) for (int dim : v.arrayDims)
dimArr.append(dim); dimArr.append(dim);
obj[arrayDimsKey] = dimArr; obj[arrayDimsKey()] = dimArr;
} }
if (v.arrayStride) if (v.arrayStride)
obj[arrayStrideKey] = v.arrayStride; obj[arrayStrideKey()] = v.arrayStride;
if (v.matrixStride) if (v.matrixStride)
obj[matrixStrideKey] = v.matrixStride; obj[matrixStrideKey()] = v.matrixStride;
if (v.matrixIsRowMajor) if (v.matrixIsRowMajor)
obj[matrixRowMajorKey] = true; obj[matrixRowMajorKey()] = true;
if (!v.structMembers.isEmpty()) { if (!v.structMembers.isEmpty()) {
QJsonArray arr; QJsonArray arr;
for (const QShaderDescription::BlockVariable &sv : v.structMembers) for (const QShaderDescription::BlockVariable &sv : v.structMembers)
arr.append(blockMemberObject(sv)); arr.append(blockMemberObject(sv));
obj[structMembersKey] = arr; obj[structMembersKey()] = arr;
} }
return obj; return obj;
} }
@ -937,114 +939,114 @@ QJsonDocument QShaderDescriptionPrivate::makeDoc()
for (const QShaderDescription::InOutVariable &v : qAsConst(inVars)) for (const QShaderDescription::InOutVariable &v : qAsConst(inVars))
jinputs.append(inOutObject(v)); jinputs.append(inOutObject(v));
if (!jinputs.isEmpty()) if (!jinputs.isEmpty())
root[inputsKey] = jinputs; root[inputsKey()] = jinputs;
QJsonArray joutputs; QJsonArray joutputs;
for (const QShaderDescription::InOutVariable &v : qAsConst(outVars)) for (const QShaderDescription::InOutVariable &v : qAsConst(outVars))
joutputs.append(inOutObject(v)); joutputs.append(inOutObject(v));
if (!joutputs.isEmpty()) if (!joutputs.isEmpty())
root[outputsKey] = joutputs; root[outputsKey()] = joutputs;
QJsonArray juniformBlocks; QJsonArray juniformBlocks;
for (const QShaderDescription::UniformBlock &b : uniformBlocks) { for (const QShaderDescription::UniformBlock &b : uniformBlocks) {
QJsonObject juniformBlock; QJsonObject juniformBlock;
juniformBlock[blockNameKey] = QString::fromUtf8(b.blockName); juniformBlock[blockNameKey()] = QString::fromUtf8(b.blockName);
juniformBlock[structNameKey] = QString::fromUtf8(b.structName); juniformBlock[structNameKey()] = QString::fromUtf8(b.structName);
juniformBlock[sizeKey] = b.size; juniformBlock[sizeKey()] = b.size;
if (b.binding >= 0) if (b.binding >= 0)
juniformBlock[bindingKey] = b.binding; juniformBlock[bindingKey()] = b.binding;
if (b.descriptorSet >= 0) if (b.descriptorSet >= 0)
juniformBlock[setKey] = b.descriptorSet; juniformBlock[setKey()] = b.descriptorSet;
QJsonArray members; QJsonArray members;
for (const QShaderDescription::BlockVariable &v : b.members) for (const QShaderDescription::BlockVariable &v : b.members)
members.append(blockMemberObject(v)); members.append(blockMemberObject(v));
juniformBlock[membersKey] = members; juniformBlock[membersKey()] = members;
juniformBlocks.append(juniformBlock); juniformBlocks.append(juniformBlock);
} }
if (!juniformBlocks.isEmpty()) if (!juniformBlocks.isEmpty())
root[uniformBlocksKey] = juniformBlocks; root[uniformBlocksKey()] = juniformBlocks;
QJsonArray jpushConstantBlocks; QJsonArray jpushConstantBlocks;
for (const QShaderDescription::PushConstantBlock &b : pushConstantBlocks) { for (const QShaderDescription::PushConstantBlock &b : pushConstantBlocks) {
QJsonObject jpushConstantBlock; QJsonObject jpushConstantBlock;
jpushConstantBlock[nameKey] = QString::fromUtf8(b.name); jpushConstantBlock[nameKey()] = QString::fromUtf8(b.name);
jpushConstantBlock[sizeKey] = b.size; jpushConstantBlock[sizeKey()] = b.size;
QJsonArray members; QJsonArray members;
for (const QShaderDescription::BlockVariable &v : b.members) for (const QShaderDescription::BlockVariable &v : b.members)
members.append(blockMemberObject(v)); members.append(blockMemberObject(v));
jpushConstantBlock[membersKey] = members; jpushConstantBlock[membersKey()] = members;
jpushConstantBlocks.append(jpushConstantBlock); jpushConstantBlocks.append(jpushConstantBlock);
} }
if (!jpushConstantBlocks.isEmpty()) if (!jpushConstantBlocks.isEmpty())
root[pushConstantBlocksKey] = jpushConstantBlocks; root[pushConstantBlocksKey()] = jpushConstantBlocks;
QJsonArray jstorageBlocks; QJsonArray jstorageBlocks;
for (const QShaderDescription::StorageBlock &b : storageBlocks) { for (const QShaderDescription::StorageBlock &b : storageBlocks) {
QJsonObject jstorageBlock; QJsonObject jstorageBlock;
jstorageBlock[blockNameKey] = QString::fromUtf8(b.blockName); jstorageBlock[blockNameKey()] = QString::fromUtf8(b.blockName);
jstorageBlock[instanceNameKey] = QString::fromUtf8(b.instanceName); jstorageBlock[instanceNameKey()] = QString::fromUtf8(b.instanceName);
jstorageBlock[knownSizeKey] = b.knownSize; jstorageBlock[knownSizeKey()] = b.knownSize;
if (b.binding >= 0) if (b.binding >= 0)
jstorageBlock[bindingKey] = b.binding; jstorageBlock[bindingKey()] = b.binding;
if (b.descriptorSet >= 0) if (b.descriptorSet >= 0)
jstorageBlock[setKey] = b.descriptorSet; jstorageBlock[setKey()] = b.descriptorSet;
QJsonArray members; QJsonArray members;
for (const QShaderDescription::BlockVariable &v : b.members) for (const QShaderDescription::BlockVariable &v : b.members)
members.append(blockMemberObject(v)); members.append(blockMemberObject(v));
jstorageBlock[membersKey] = members; jstorageBlock[membersKey()] = members;
jstorageBlocks.append(jstorageBlock); jstorageBlocks.append(jstorageBlock);
} }
if (!jstorageBlocks.isEmpty()) if (!jstorageBlocks.isEmpty())
root[storageBlocksKey] = jstorageBlocks; root[storageBlocksKey()] = jstorageBlocks;
QJsonArray jcombinedSamplers; QJsonArray jcombinedSamplers;
for (const QShaderDescription::InOutVariable &v : qAsConst(combinedImageSamplers)) { for (const QShaderDescription::InOutVariable &v : qAsConst(combinedImageSamplers)) {
QJsonObject sampler; QJsonObject sampler;
sampler[nameKey] = QString::fromUtf8(v.name); sampler[nameKey()] = QString::fromUtf8(v.name);
sampler[typeKey] = typeStr(v.type); sampler[typeKey()] = typeStr(v.type);
addDeco(&sampler, v); addDeco(&sampler, v);
jcombinedSamplers.append(sampler); jcombinedSamplers.append(sampler);
} }
if (!jcombinedSamplers.isEmpty()) if (!jcombinedSamplers.isEmpty())
root[combinedImageSamplersKey] = jcombinedSamplers; root[combinedImageSamplersKey()] = jcombinedSamplers;
QJsonArray jstorageImages; QJsonArray jstorageImages;
for (const QShaderDescription::InOutVariable &v : qAsConst(storageImages)) { for (const QShaderDescription::InOutVariable &v : qAsConst(storageImages)) {
QJsonObject image; QJsonObject image;
image[nameKey] = QString::fromUtf8(v.name); image[nameKey()] = QString::fromUtf8(v.name);
image[typeKey] = typeStr(v.type); image[typeKey()] = typeStr(v.type);
addDeco(&image, v); addDeco(&image, v);
jstorageImages.append(image); jstorageImages.append(image);
} }
if (!jstorageImages.isEmpty()) if (!jstorageImages.isEmpty())
root[storageImagesKey] = jstorageImages; root[storageImagesKey()] = jstorageImages;
QJsonArray jlocalSize; QJsonArray jlocalSize;
for (int i = 0; i < 3; ++i) for (int i = 0; i < 3; ++i)
jlocalSize.append(QJsonValue(int(localSize[i]))); jlocalSize.append(QJsonValue(int(localSize[i])));
root[localSizeKey] = jlocalSize; root[localSizeKey()] = jlocalSize;
QJsonArray jseparateImages; QJsonArray jseparateImages;
for (const QShaderDescription::InOutVariable &v : qAsConst(separateImages)) { for (const QShaderDescription::InOutVariable &v : qAsConst(separateImages)) {
QJsonObject image; QJsonObject image;
image[nameKey] = QString::fromUtf8(v.name); image[nameKey()] = QString::fromUtf8(v.name);
image[typeKey] = typeStr(v.type); image[typeKey()] = typeStr(v.type);
addDeco(&image, v); addDeco(&image, v);
jseparateImages.append(image); jseparateImages.append(image);
} }
if (!jseparateImages.isEmpty()) if (!jseparateImages.isEmpty())
root[separateImagesKey] = jseparateImages; root[separateImagesKey()] = jseparateImages;
QJsonArray jseparateSamplers; QJsonArray jseparateSamplers;
for (const QShaderDescription::InOutVariable &v : qAsConst(separateSamplers)) { for (const QShaderDescription::InOutVariable &v : qAsConst(separateSamplers)) {
QJsonObject sampler; QJsonObject sampler;
sampler[nameKey] = QString::fromUtf8(v.name); sampler[nameKey()] = QString::fromUtf8(v.name);
sampler[typeKey] = typeStr(v.type); sampler[typeKey()] = typeStr(v.type);
addDeco(&sampler, v); addDeco(&sampler, v);
jseparateSamplers.append(sampler); jseparateSamplers.append(sampler);
} }
if (!jseparateSamplers.isEmpty()) if (!jseparateSamplers.isEmpty())
root[separateSamplersKey] = jseparateSamplers; root[separateSamplersKey()] = jseparateSamplers;
return QJsonDocument(root); return QJsonDocument(root);
} }