7#include <QJsonDocument>
9#include <QJsonParseError>
10#include <QRegularExpression>
22 static const QRegularExpression identifier(QStringLiteral(
"^[A-Za-z_][A-Za-z0-9_]*$"));
27 const double scale = std::max({1.0, std::abs(left), std::abs(right)});
28 return std::abs(left - right) <= 1.0e-12 * scale;
33 return value.toString().trimmed();
35 return value.toObject().value(
"file").toString().trimmed();
41 if (!file.open(QIODevice::ReadOnly)) {
42 error = QObject::tr(
"Could not open %1: %2").arg(path, file.errorString());
46 QJsonParseError parseError;
47 document = QJsonDocument::fromJson(file.readAll(), &parseError);
48 if (parseError.error != QJsonParseError::NoError || !document.isObject()) {
49 error = QObject::tr(
"Could not parse %1 at offset %2: %3").arg(path).arg(parseError.offset).arg(parseError.errorString());
52 if (!document.object().value(
"shaders").isArray()) {
53 error = QObject::tr(
"%1 must contain a 'shaders' array.").arg(path);
61 if (!file.open(QIODevice::WriteOnly)) {
62 error = QObject::tr(
"Could not write %1: %2").arg(path, file.errorString());
65 const QByteArray json = document.toJson(QJsonDocument::Indented);
66 if (file.write(json) != json.size() || !file.commit()) {
67 error = QObject::tr(
"Could not finish writing %1: %2").arg(path, file.errorString());
76 const QString jsonPath = directory +
"/" + JSON_MANIFEST_NAME;
77 if (QFileInfo(jsonPath).isFile())
79 const QString textPath = directory +
"/" + TEXT_MANIFEST_NAME;
80 if (QFileInfo(textPath).isFile())
93 error = QObject::tr(
"No library.json or index.txt found in %1.").arg(directory);
96 if (!path.endsWith(QStringLiteral(
".json"), Qt::CaseInsensitive))
99 QJsonDocument document;
100 if (!load_json_document(path, document, error))
102 const QJsonValue value = document.object().value(QStringLiteral(
"backend"));
103 if (value.isUndefined() || value.isNull())
105 if (!value.isString()) {
106 error = QObject::tr(
"%1 has a non-string 'backend' value.").arg(path);
109 const std::optional<Backend> backend =
backend_from_id(value.toString());
111 error = QObject::tr(
"%1 has an unsupported backend '%2'.").arg(path, value.toString());
119 if (path.isEmpty()) {
120 error = QObject::tr(
"No library.json or index.txt found in %1.").arg(directory);
123 if (!path.endsWith(QStringLiteral(
".json"), Qt::CaseInsensitive))
126 QJsonDocument document;
127 if (!load_json_document(path, document, error))
129 const QJsonValue value = document.object().value(QStringLiteral(
"library_type"));
130 if (value.isUndefined() || value.isNull())
132 if (!value.isString()) {
133 error = QObject::tr(
"%1 has a non-string 'library_type' value.").arg(path);
136 const QString type = value.toString().trimmed().toLower();
137 if (type == QStringLiteral(
"source"))
139 if (type == QStringLiteral(
"runtime"))
141 error = QObject::tr(
"%1 has an unsupported library_type '%2'.").arg(path, value.toString());
149 if (path.isEmpty()) {
150 error = QObject::tr(
"No library.json or index.txt found in %1.").arg(directory);
154 if (path.endsWith(
".json", Qt::CaseInsensitive)) {
155 QJsonDocument document;
156 if (!load_json_document(path, document, error))
158 const QJsonArray entries = document.object().value(
"shaders").toArray();
159 for (
const QJsonValue &entry : entries) {
160 const QString file = json_entry_file(entry);
161 if (file.isEmpty()) {
162 error = QObject::tr(
"%1 contains a shader entry without a file name.").arg(path);
165 shaders.append(file);
171 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
172 error = QObject::tr(
"Could not open %1: %2").arg(path, file.errorString());
175 QTextStream input(&file);
176 while (!input.atEnd())
177 shaders.append(input.readLine().trimmed());
184 if (path.isEmpty()) {
185 error = QObject::tr(
"No library.json or index.txt found in %1.").arg(directory);
189 if (path.endsWith(
".json", Qt::CaseInsensitive)) {
190 QJsonDocument document;
191 if (!load_json_document(path, document, error))
194 QJsonObject root = document.object();
195 const QJsonArray previousEntries = root.value(
"shaders").toArray();
196 QJsonArray updatedEntries;
197 for (
const QString &shader : shaders) {
198 QJsonValue preservedEntry;
200 for (
const QJsonValue &entry : previousEntries) {
201 if (json_entry_file(entry).compare(shader, Qt::CaseInsensitive) == 0) {
202 preservedEntry = entry;
207 updatedEntries.append(found ? preservedEntry : QJsonValue(shader));
209 if (!root.contains(
"version"))
210 root.insert(
"version", 1);
211 root.insert(
"shaders", updatedEntries);
212 return write_json_document(path, QJsonDocument(root), error);
215 QSaveFile file(path);
216 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
217 error = QObject::tr(
"Could not write %1: %2").arg(path, file.errorString());
220 QTextStream output(&file);
221 for (
const QString &shader : shaders)
222 output << shader <<
"\n";
224 if (output.status() != QTextStream::Ok || !file.commit()) {
225 error = QObject::tr(
"Could not finish writing %1: %2").arg(path, file.errorString());
235 if (!shaders.contains(shader, Qt::CaseInsensitive))
236 shaders.append(shader);
245 bool removed =
false;
246 for (
auto it = shaders.begin(); it != shaders.end();) {
247 if (it->compare(shader, Qt::CaseInsensitive) == 0) {
248 it = shaders.erase(it);
255 error = QObject::tr(
"Shader '%1' is not present in the library manifest.").arg(shader);
265 const QString jsonPath = directory +
"/" + JSON_MANIFEST_NAME;
266 if (QFileInfo(jsonPath).isFile())
269 const QString textPath = directory +
"/" + TEXT_MANIFEST_NAME;
270 QFile file(textPath);
271 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
272 error = QObject::tr(
"Could not open %1: %2").arg(textPath, file.errorString());
277 QTextStream input(&file);
278 while (!input.atEnd()) {
279 const QString shader = input.readLine().trimmed();
280 if (!shader.isEmpty())
281 shaders.append(shader);
294 for (
const QString &shader : shaders)
295 entries.append(shader);
297 root.insert(
"version", 1);
298 root.insert(
"backend", QStringLiteral(
"acmx2"));
299 root.insert(
"library_type", QStringLiteral(
"source"));
300 root.insert(
"shaders", entries);
301 return write_json_document(directory +
"/" + JSON_MANIFEST_NAME, QJsonDocument(root), error);
304 QSaveFile file(directory +
"/" + TEXT_MANIFEST_NAME);
305 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
306 error = QObject::tr(
"Could not create index.txt: %1").arg(file.errorString());
309 QTextStream output(&file);
310 for (
const QString &shader : shaders)
311 output << shader <<
"\n";
313 if (output.status() != QTextStream::Ok || !file.commit()) {
314 error = QObject::tr(
"Could not finish creating index.txt: %1").arg(file.errorString());
320 bool load_custom_uniforms(
const QString &directory, QList<CustomUniformDefinition> &uniforms, QString &error) {
323 const QString path = directory +
"/" + JSON_MANIFEST_NAME;
324 if (!QFileInfo(path).isFile()) {
325 error = QObject::tr(
"Custom uniforms require %1.").arg(path);
329 QJsonDocument document;
330 if (!load_json_document(path, document, error))
333 const QJsonValue value = document.object().value(
"custom_uniforms");
334 if (value.isUndefined() || value.isNull())
336 if (!value.isObject()) {
337 error = QObject::tr(
"%1 field 'custom_uniforms' must be an object.").arg(path);
341 const QJsonObject entries = value.toObject();
342 bool hasExplicitSlots =
false;
343 bool hasImplicitSlots =
false;
344 std::vector<int> occupiedSlots;
345 for (
auto it = entries.constBegin(); it != entries.constEnd(); ++it) {
346 if (!it.value().isObject()) {
347 error = QObject::tr(
"Custom uniform '%1' must be an object.").arg(it.key());
350 const QJsonObject entry = it.value().toObject();
352 uniform.
name = it.key();
353 if (!valid_custom_uniform_name(uniform.
name)) {
354 error = QObject::tr(
"Custom uniform '%1' is not a valid GLSL identifier.").arg(uniform.
name);
357 uniform.
minimum = entry.value(
"minimum").toDouble(0.0);
358 uniform.
maximum = entry.value(
"maximum").toDouble(1.0);
359 uniform.
step = entry.value(
"step").toDouble(0.01);
360 uniform.
value = entry.value(
"value").toDouble(uniform.
minimum);
361 const QJsonValue slotValue = entry.value(
"slot");
362 if (!slotValue.isUndefined() && !slotValue.isNull()) {
363 const double numericSlot = slotValue.toDouble(-1.0);
364 uniform.
slot =
static_cast<int>(numericSlot);
366 error = QObject::tr(
"Custom uniform '%1' has an invalid or duplicate slot.").arg(uniform.
name);
369 occupiedSlots.push_back(uniform.
slot);
370 hasExplicitSlots =
true;
372 hasImplicitSlots =
true;
375 error = QObject::tr(
"Custom uniform '%1' has an invalid range or value.").arg(uniform.
name);
379 uniforms.append(uniform);
381 if (hasExplicitSlots && hasImplicitSlots) {
382 error = QObject::tr(
"Custom uniforms must either all specify slots or all omit them.");
385 if (hasExplicitSlots) {
387 for (
int index = 0; index < uniforms.size(); ++index) {
388 if (uniforms.at(index).slot != index) {
389 error = QObject::tr(
"Custom-uniform slots must be contiguous from zero.");
397 bool write_custom_uniforms(
const QString &directory,
const QList<CustomUniformDefinition> &uniforms, QString &error) {
403 const QString path = directory +
"/" + JSON_MANIFEST_NAME;
404 QJsonDocument document;
405 if (!load_json_document(path, document, error))
409 const bool hasExplicitSlots = std::any_of(uniforms.cbegin(), uniforms.cend(), [](
const CustomUniformDefinition &uniform) { return uniform.slot >= 0; });
410 std::vector<int> writtenSlots;
411 QStringList writtenNames;
413 if (!valid_custom_uniform_name(uniform.
name)) {
414 error = QObject::tr(
"Custom uniform '%1' is not a valid GLSL identifier.").arg(uniform.
name);
417 if (writtenNames.contains(uniform.
name, Qt::CaseSensitive)) {
418 error = QObject::tr(
"Duplicate custom uniform '%1'.").arg(uniform.
name);
421 writtenNames.append(uniform.
name);
423 error = QObject::tr(
"Custom uniform '%1' has an invalid range or value.").arg(uniform.
name);
426 if ((hasExplicitSlots && uniform.
slot < 0) || (!hasExplicitSlots && uniform.
slot >= 0)) {
427 error = QObject::tr(
"Custom uniforms must either all specify slots or all omit them.");
431 error = QObject::tr(
"Custom uniform '%1' has an invalid or duplicate slot.").arg(uniform.
name);
434 if (hasExplicitSlots)
435 writtenSlots.push_back(uniform.
slot);
437 if (hasExplicitSlots)
438 entry.insert(
"slot", uniform.
slot);
439 entry.insert(
"minimum", uniform.
minimum);
440 entry.insert(
"maximum", uniform.
maximum);
441 entry.insert(
"step", uniform.
step);
443 entries.insert(uniform.
name, entry);
445 if (hasExplicitSlots) {
446 std::sort(writtenSlots.begin(), writtenSlots.end());
447 for (std::size_t index = 0; index < writtenSlots.size(); ++index) {
448 if (writtenSlots[index] !=
static_cast<int>(index)) {
449 error = QObject::tr(
"Custom-uniform slots must be contiguous from zero.");
455 QJsonObject root = document.object();
456 if (entries.isEmpty())
457 root.remove(
"custom_uniforms");
459 root.insert(
"custom_uniforms", entries);
460 return write_json_document(path, QJsonDocument(root), error);
465 QList<CustomUniformDefinition> leftUniforms;
466 QList<CustomUniformDefinition> rightUniforms;
470 if (leftUniforms.size() != rightUniforms.size()) {
474 for (
int index = 0; index < leftUniforms.size(); ++index) {
constexpr std::uint32_t kShaderSelectionMaxUniformName
constexpr std::uint32_t kShaderSelectionMaxCustomUniforms
QString shader_manifest_path(const QString &directory)
Resolve library.json first, then fall back to index.txt.
bool custom_uniform_metadata_matches(const QString &leftDirectory, const QString &rightDirectory, bool &matches, QString &error)
Compare two manifests' custom-uniform ABI and numeric metadata.
QDateTime shader_manifest_last_modified(const QString &directory)
Return the selected manifest's modification time.
bool load_custom_uniforms(const QString &directory, QList< CustomUniformDefinition > &uniforms, QString &error)
Load custom float-uniform controls from library.json.
std::optional< ShaderLibraryType > shader_manifest_library_type(const QString &directory, QString &error)
Read and validate an optional top-level library_type value.
bool remove_shader_manifest_entry(const QString &directory, const QString &shader, QString &error)
Remove one shader from the preferred manifest, without deleting its file.
bool shader_manifest_exists(const QString &directory)
Return true when either supported manifest exists.
bool load_shader_manifest(const QString &directory, QStringList &shaders, QString &error)
Load shader filenames from the preferred manifest in a directory.
bool append_shader_manifest(const QString &directory, const QString &shader, QString &error)
Add one shader to the preferred manifest if it is not already present.
std::optional< Backend > backend_from_id(const QString &value)
bool migrate_index_manifest_to_json(const QString &directory, bool &created, QString &error)
Create library.json from index.txt when a JSON manifest is absent.
bool write_custom_uniforms(const QString &directory, const QList< CustomUniformDefinition > &uniforms, QString &error)
Rewrite custom float-uniform controls while preserving all other JSON fields.
bool create_shader_manifest(const QString &directory, ShaderManifestFormat format, const QStringList &shaders, QString &error)
Create a new manifest in the requested format.
bool write_shader_manifest(const QString &directory, const QStringList &shaders, QString &error)
Rewrite the preferred manifest, preserving JSON fields when possible.
std::optional< Backend > shader_manifest_backend(const QString &directory, QString &error)
Read an optional top-level backend hint from library.json.
bool valid_custom_uniform_name(const QString &name)
constexpr auto JSON_MANIFEST_NAME
bool nearly_equal(double left, double right)
bool load_json_document(const QString &path, QJsonDocument &document, QString &error)
bool write_json_document(const QString &path, const QJsonDocument &document, QString &error)
QString json_entry_file(const QJsonValue &value)
constexpr auto TEXT_MANIFEST_NAME
Optional JSON and legacy text shader-library manifests.