fix(shader): validate and serialize pipeline specialization constants (wip)

This commit is contained in:
boykopovar
2026-10-06 19:43:35 +03:00
parent 73816838dc
commit 7f56e04157
3 changed files with 55 additions and 1 deletions
@@ -3,13 +3,55 @@
#include "prx/libSceAgcDriver/Graphics/include/Shaders.hpp"
#include <limits>
#include <map>
#include <set>
#include <spirv/unified1/spirv.hpp>
#include <vector>
namespace AgcDriver::Graphics {
class PipelineSpecialization {
public:
static void Validate(const ShaderRecompiler::RecompileResult& shader) {
const auto& words = shader.spirv;
Require(words.size() >= 5 && words[0] == spv::MagicNumber && words[3] != 0, "invalid specialization module header");
std::set<std::uint32_t> unsignedTypes;
std::map<std::uint32_t, std::uint32_t> constants;
std::map<std::uint32_t, std::uint32_t> decorations;
std::set<std::uint32_t> constantIds;
std::set<std::uint32_t> supplied;
for (const auto& constant : shader.specialization) Require(supplied.insert(constant.id).second, "duplicate pipeline specialization ID");
for (std::size_t cursor = 5; cursor < words.size();) {
const auto count = words[cursor] >> 16u;
const auto op = static_cast<spv::Op>(words[cursor] & 0xffffu);
Require(count != 0 && count <= words.size() - cursor, "truncated specialization module instruction");
const auto validId = [&](std::uint32_t id) { Require(id != 0 && id < words[3], "specialization ID exceeds module bound"); };
if (op == spv::OpTypeInt) {
Require(count == 4, "malformed integer type");
if (words[cursor + 2] == 32u && words[cursor + 3] == 0u) unsignedTypes.insert(words[cursor + 1]);
} else if (op == spv::OpSpecConstant) {
Require(count == 4, "specialization constant must have a 32-bit literal");
validId(words[cursor + 2]);
Require(constants.emplace(words[cursor + 2], words[cursor + 1]).second, "duplicate specialization result ID");
} else if (op == spv::OpDecorate && count >= 3 && words[cursor + 2] == spv::DecorationSpecId) {
Require(count == 4, "malformed SpecId decoration");
validId(words[cursor + 1]);
Require(decorations.emplace(words[cursor + 1], words[cursor + 3]).second && constantIds.insert(words[cursor + 3]).second, "duplicate SpecId decoration");
} else if (op == spv::OpSpecConstantTrue || op == spv::OpSpecConstantFalse || op == spv::OpSpecConstantComposite || op == spv::OpSpecConstantOp) {
throw std::runtime_error("AGC graphics: unsupported specialization constant representation");
}
cursor += count;
}
Require(constants.size() == decorations.size(), "specialization declarations and SpecId decorations disagree");
for (const auto& [id, type] : constants) {
Require(unsignedTypes.contains(type), "specialization constant must be uint32");
const auto decoration = decorations.find(id);
Require(decoration != decorations.end() && supplied.contains(decoration->second), "required pipeline specialization value is missing");
}
}
explicit PipelineSpecialization(const ShaderRecompiler::RecompileResult& shader) {
Validate(shader);
entries.reserve(shader.specialization.size());
for (std::size_t index = 0; index < shader.specialization.size(); ++index) {
const auto offset = index * sizeof(ShaderRecompiler::PipelineSpecializationConstant) + offsetof(ShaderRecompiler::PipelineSpecializationConstant, value);
@@ -1,5 +1,6 @@
#include "BdaAbi.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Pipeline.hpp"
#include "prx/libSceAgcDriver/Graphics/include/PipelineSpecialization.hpp"
#include "prx/libSceAgcDriver/Graphics/include/VertexInput.hpp"
#include <spirv/unified1/spirv.hpp>
#include <algorithm>
@@ -132,6 +133,7 @@ Module Inspect(const CompiledShader& compiled, const State& state, const VkPhysi
using Stage = ShaderRecompiler::ShaderStage;
Require(compiled.program != nullptr, "missing compiled shader");
const auto& shader = *compiled.program;
PipelineSpecialization::Validate(shader);
const auto stage = compiled.stage;
const bool vertex = stage == Stage::Vertex || stage == Stage::Local;
const bool fragment = stage == Stage::Fragment;
@@ -279,7 +281,6 @@ Module Inspect(const CompiledShader& compiled, const State& state, const VkPhysi
case spv::OpDecorationGroup:
case spv::OpGroupDecorate:
case spv::OpGroupMemberDecorate:
case spv::OpSpecConstant:
case spv::OpSpecConstantTrue:
case spv::OpSpecConstantFalse:
case spv::OpSpecConstantComposite:
@@ -357,6 +358,8 @@ Module Inspect(const CompiledShader& compiled, const State& state, const VkPhysi
case spv::OpConstant:
if (count == 4) module.constants.emplace(instruction[2], instruction[3]);
break;
case spv::OpSpecConstant:
break;
default: break;
}
cursor += count;
@@ -340,6 +340,10 @@ void decodeArtifact(Reader& reader, CompiledShaderArtifact& result) {
}
void encodeInvocation(Writer& writer, const ShaderInvocation& invocation) {
writer.List(invocation.specialization, [](Writer& out, const PipelineSpecializationConstant& constant) {
out.Value(constant.id);
out.Value(constant.value);
});
writer.List(invocation.bindings, encodeBinding);
writer.Value<std::uint64_t>(invocation.pushConstants.size());
for (const auto byte : invocation.pushConstants) writer.Value(static_cast<std::uint8_t>(byte));
@@ -352,6 +356,11 @@ void encodeInvocation(Writer& writer, const ShaderInvocation& invocation) {
}
void decodeInvocation(Reader& reader, ShaderInvocation& invocation) {
invocation.specializationId = 0;
reader.List(invocation.specialization, 8, [](Reader& in, PipelineSpecializationConstant& constant) {
in.Value(constant.id);
in.Value(constant.value);
});
reader.List(invocation.bindings, 8, decodeBinding);
std::vector<std::uint8_t> pushConstants;
reader.Values(pushConstants);