refactor(shader): separate compiled artifacts from invocation data

This commit is contained in:
boykopovar
2026-10-05 07:57:47 +03:00
parent 68cebaee8b
commit f26c8b6112
6 changed files with 149 additions and 81 deletions
@@ -44,18 +44,13 @@ bool sameBindings(const std::vector<DescriptorBinding>& left, const std::vector<
return true;
}
void requireSameResult(const RecompileResult& left, const RecompileResult& right, const char* what) {
void requireSameArtifact(const CompiledShaderArtifact& left, const CompiledShaderArtifact& right, const char* what) {
const std::string prefix = std::string(what) + ": ";
require(left.spirv.Words() == right.spirv.Words(), prefix + "SPIR-V differs");
require(sameBindings(left.bindings, right.bindings), prefix + "bindings differ");
require(left.pushConstants == right.pushConstants, prefix + "push constants differ");
require(left.bdaAbiVersion == right.bdaAbiVersion, prefix + "BDA ABI version differs");
require(left.vertexAttributes.size() == right.vertexAttributes.size(), prefix + "vertex attribute count differs");
for (std::size_t i = 0; i < left.vertexAttributes.size(); ++i) {
const auto& a = left.vertexAttributes[i];
const auto& b = right.vertexAttributes[i];
require(a.location == b.location && a.components == b.components && a.resource.fields == b.resource.fields && a.fetchIndex == b.fetchIndex, prefix + "vertex attribute differs");
}
require(left.memoryOffsetDword == right.memoryOffsetDword, prefix + "memory offset differs");
require(left.hostSubgroupSize == right.hostSubgroupSize, prefix + "host subgroup size differs");
require(left.vertexInputs == right.vertexInputs, prefix + "vertex inputs differ");
require(left.vertexOffsetSgpr == right.vertexOffsetSgpr && left.instanceOffsetSgpr == right.instanceOffsetSgpr, prefix + "offset SGPRs differ");
require(left.vertexOffsetShared == right.vertexOffsetShared && left.instanceOffsetShared == right.instanceOffsetShared && left.vertexOffsetConflict == right.vertexOffsetConflict && left.instanceOffsetConflict == right.instanceOffsetConflict, prefix + "offset flags differ");
require(left.parameterExports == right.parameterExports, prefix + "parameter exports differ");
@@ -63,22 +58,33 @@ void requireSameResult(const RecompileResult& left, const RecompileResult& right
for (std::size_t i = 0; i < left.fragmentParameters.size(); ++i) {
const auto& a = left.fragmentParameters[i];
const auto& b = right.fragmentParameters[i];
require(a.location == b.location && a.sourceLocation == b.sourceLocation && a.flat == b.flat && a.perVertex == b.perVertex, prefix + "fragment parameter differs");
require(a.location == b.location && a.sourceLocation == b.sourceLocation && a.flat == b.flat && a.perVertex == b.perVertex && a.custom == b.custom, prefix + "fragment parameter differs");
}
}
void requireSameResult(const RecompileResult& left, const RecompileResult& right, const char* what) {
requireSameArtifact(left, right, what);
const std::string prefix = std::string(what) + ": ";
require(sameBindings(left.bindings, right.bindings), prefix + "bindings differ");
require(left.pushConstants == right.pushConstants, prefix + "push constants differ");
require(left.vertexAttributes.size() == right.vertexAttributes.size(), prefix + "vertex attribute count differs");
for (std::size_t i = 0; i < left.vertexAttributes.size(); ++i) {
const auto& a = left.vertexAttributes[i];
const auto& b = right.vertexAttributes[i];
require(a.location == b.location && a.components == b.components && a.resource.fields == b.resource.fields && a.fetchIndex == b.fetchIndex, prefix + "vertex attribute differs");
}
}
void requireSameVariant(const CompiledVariant& left, const CompiledVariant& right, const char* what) {
requireSameResult(left.result, right.result, what);
requireSameArtifact(left.artifact, right.artifact, what);
const std::string prefix = std::string(what) + ": ";
const auto& a = left.info;
const auto& b = right.info;
require(a.stage == b.stage && a.shaderHash == b.shaderHash && a.waveSize == b.waveSize && a.userDataBase == b.userDataBase && a.userDataCount == b.userDataCount && a.scratchDwords == b.scratchDwords && a.paramExportMask == b.paramExportMask, prefix + "compiled info differs");
require(a.info == b.info, prefix + "shader info differs");
require(a.bindings == b.bindings, prefix + "info binding layout differs");
require(sameBindings(left.bindings.bindings, right.bindings.bindings), prefix + "allocation bindings differ");
require(left.bindings.layout == right.bindings.layout, prefix + "allocation layout differs");
require(left.bindings.pushConstantOffsetBytes == right.bindings.pushConstantOffsetBytes && left.bindings.pushConstantSizeBytes == right.bindings.pushConstantSizeBytes, prefix + "push constant range differs");
require(left.bindings.pushConstants == right.bindings.pushConstants, prefix + "allocation push constants differ");
}
DescriptorBinding sampleBinding(std::uint32_t seed) {
@@ -115,6 +121,9 @@ RecompileResult sampleResult() {
result.bindings[1].imageShape.reset();
result.pushConstants = {std::byte{1}, std::byte{0xff}, std::byte{0}, std::byte{0x80}, std::byte{7}};
result.bdaAbiVersion = 3;
result.memoryOffsetDword = 7;
result.hostSubgroupSize = 32;
result.vertexInputs = {{1, 4, 2}, {5, 2, 0}};
result.vertexAttributes = {{1, 4, {{0x1000u, 0x20000u, 0x30u, 0x4u}}, 2}, {5, 2, {{9u, 8u, 7u, 6u}}, 0}};
result.vertexOffsetSgpr = 12;
result.instanceOffsetSgpr = -1;
@@ -123,16 +132,14 @@ RecompileResult sampleResult() {
result.vertexOffsetConflict = false;
result.instanceOffsetConflict = true;
result.parameterExports = {0, 3, 7};
result.fragmentParameters = {{0, 1, true, false}, {2, 3, false, true}};
result.fragmentParameters = {{0, 1, true, false, true}, {2, 3, false, true}};
result.variantId = 99;
return result;
}
CompiledVariant sampleVariant() {
CompiledVariant variant;
variant.result = sampleResult();
variant.result.bindings.clear();
variant.result.pushConstants.clear();
variant.artifact = sampleResult();
variant.layout = {0, 0, 0, 128};
auto& info = variant.info;
info.stage = IrShaderStage::Compute;
@@ -199,8 +206,6 @@ CompiledVariant sampleVariant() {
variant.bindings.layout = info.bindings;
variant.bindings.pushConstantOffsetBytes = 16;
variant.bindings.pushConstantSizeBytes = 112;
variant.bindings.bindings = {sampleBinding(3)};
variant.bindings.pushConstants = {std::byte{9}, std::byte{8}};
return variant;
}
@@ -274,6 +279,7 @@ void verifyEntryRoundTrip() {
CompiledVariant decoded;
require(ShaderDiskCache::DecodeEntry(file, key, decoded) == ShaderDiskCache::LoadStatus::Loaded, "an encoded entry does not decode");
requireSameVariant(variant, decoded, "entry round trip");
require(decoded.artifact.variantId == 0, "the artifact stored a per-process id");
for (std::size_t size = 0; size < file.size(); size += size < 512 ? 1 : 131) {
CompiledVariant partial;
@@ -292,6 +298,26 @@ void verifyEntryRoundTrip() {
auto otherKey = key;
otherKey.back() ^= std::byte{1};
require(ShaderDiskCache::DecodeEntry(file, otherKey, decoded) == ShaderDiskCache::LoadStatus::KeyMismatch, "an entry for another key loads");
auto previousVersion = file;
previousVersion[4] = std::byte{3};
require(ShaderDiskCache::DecodeEntry(previousVersion, key, decoded) == ShaderDiskCache::LoadStatus::Rejected, "an entry with the previous format version loads");
}
void verifyArtifactStorageIsolation() {
SampleRequest sample;
const auto key = sample.Key();
auto result = sampleResult();
auto variant = sampleVariant();
variant.artifact = result;
const auto before = ShaderDiskCache::EncodeEntry(key, variant);
result.bindings[0].guestDescriptor[0] ^= 0x10000u;
result.pushConstants[0] ^= std::byte{0xff};
result.vertexAttributes[0].resource.fields[0] ^= 0x10000u;
variant.artifact = result;
require(ShaderDiskCache::EncodeEntry(key, variant) == before, "invocation data changed the stored artifact");
result.vertexInputs[0].components = 2;
variant.artifact = result;
require(ShaderDiskCache::EncodeEntry(key, variant) != before, "the stored artifact ignores vertex input metadata");
}
void verifyKeySensitivity() {
@@ -535,6 +561,21 @@ void verifyFailureMemoSwitch(const char* self) {
require(std::system(command.c_str()) == 0, "the process with APS5_NO_FAILURE_MEMO=1 failed");
}
void verifyInvocationIsolation() {
ComputeRequest request(true);
const auto first = Recompile(request.request);
request.userData[0] += 0x10000u;
const auto second = Recompile(request.request);
require(first.variantId != 0 && first.variantId == second.variantId, "a buffer address change did not reuse the artifact");
require(second.cacheHit, "the second invocation missed the compiled cache");
requireSameArtifact(first, second, "invocations sharing an artifact");
require(first.spirv.data() == second.spirv.data(), "invocations duplicated SPIR-V storage");
require(!sameBindings(first.bindings, second.bindings) || first.pushConstants != second.pushConstants, "the second invocation reused stale resource data");
request.userData[0] -= 0x10000u;
const auto restored = Recompile(request.request);
requireSameResult(first, restored, "restored invocation");
}
void verifyDefaultDirectory(const char* self) {
setEnvironment("ANYPS5_SHADER_CACHE_DIR", "");
setEnvironment("ANYPS5_NO_SHADER_CACHE", "1");
@@ -559,11 +600,13 @@ int main(int argc, char** argv) {
setEnvironment("ANYPS5_SHADER_CACHE_DIR", directory.string());
verifyResultRoundTrip();
verifyEntryRoundTrip();
verifyArtifactStorageIsolation();
verifyKeySensitivity();
verifyStore();
verifyAcrossProcesses(argv[0]);
verifyEmissionFailureMemo();
verifyFailureMemoSwitch(argv[0]);
verifyInvocationIsolation();
std::error_code error;
std::filesystem::remove_all(directory, error);
std::cout << "shader disk cache tests passed\n";
+2 -2
View File
@@ -12,8 +12,8 @@ struct CompiledVariant {
ResourceSpecialization specialization;
BindingLayout layout;
CompiledShaderInfo info;
BindingAllocationResult bindings;
RecompileResult result;
CompiledBindingLayout bindings;
CompiledShaderArtifact artifact;
};
}
@@ -9,11 +9,14 @@
namespace ShaderRecompiler {
struct BindingAllocationResult {
std::vector<DescriptorBinding> bindings;
struct CompiledBindingLayout {
IrBindingLayout layout;
std::uint32_t pushConstantOffsetBytes = 0;
std::uint32_t pushConstantSizeBytes = 0;
};
struct BindingAllocationResult : CompiledBindingLayout {
std::vector<DescriptorBinding> bindings;
std::vector<std::byte> pushConstants;
};
+14 -17
View File
@@ -334,7 +334,7 @@ CompiledVariant compileVariant(const RecompileRequest& request, IrProgram progra
targetOptions.narrowSubgroupClock = request.target.narrowSubgroupClock;
constexpr SpirvEmitter spirvEmitter;
RecompileResult result;
CompiledShaderArtifact result;
result.variantId = nextVariantId();
try {
result.spirv = spirvEmitter.Emit(program, inputInfo, bindings, targetOptions);
@@ -366,20 +366,16 @@ CompiledVariant compileVariant(const RecompileRequest& request, IrProgram progra
for (const auto& input : program.Info().inputs) {
if (input.kind != StageInputKind::Parameter) continue;
if (input.location >= static_cast<std::uint32_t>(inputInfo.vertex->resourcesNum)) throw std::runtime_error("vertex attribute location exceeds resource count");
result.vertexAttributes.push_back({input.location, input.componentCount, {inputInfo.vertex->resources[input.location].fields}, inputInfo.vertex->resourcesDst[input.location].fetchIndex});
result.vertexInputs.push_back({input.location, input.componentCount, inputInfo.vertex->resourcesDst[input.location].fetchIndex});
}
}
result.bindings.clear();
result.pushConstants.clear();
for (auto& attribute : result.vertexAttributes) attribute.resource = {};
bindings.bindings.clear();
bindings.pushConstants.clear();
return {resourceSpecialization, request.layout, std::move(program).TakeCompiledInfo(), std::move(bindings), std::move(result)};
return {resourceSpecialization, request.layout, std::move(program).TakeCompiledInfo(), std::move(static_cast<CompiledBindingLayout&>(bindings)), std::move(result)};
}
RecompileResult materializeResult(const CompiledVariant& variant, const RecompileRequest& request, const ResourceSnapshot& snapshot) {
auto result = variant.result;
RecompileResult result;
static_cast<CompiledShaderArtifact&>(result) = variant.artifact;
BindingAllocationResult bindings;
bindings.layout = variant.bindings.layout;
bindings.pushConstantOffsetBytes = variant.bindings.pushConstantOffsetBytes;
@@ -387,9 +383,10 @@ RecompileResult materializeResult(const CompiledVariant& variant, const Recompil
DescriptorBindingBuilder{}.Populate(bindings, variant.info.info, variant.info.stage, variant.info.userDataBase, snapshot, partialThreads(request));
result.bindings = std::move(bindings.bindings);
result.pushConstants = std::move(bindings.pushConstants);
for (auto& attribute : result.vertexAttributes) {
if (!request.context.vertex || attribute.location >= request.context.vertex->resourcesNum) throw std::runtime_error("Shader cache: invalid vertex attribute metadata");
attribute.resource = request.context.vertex->resources[attribute.location];
result.vertexAttributes.reserve(result.vertexInputs.size());
for (const auto& input : result.vertexInputs) {
if (!request.context.vertex || input.location >= request.context.vertex->resourcesNum || input.location >= request.context.vertex->resources.size()) throw std::runtime_error("Shader cache: invalid vertex attribute metadata");
result.vertexAttributes.push_back({input.location, input.components, request.context.vertex->resources[input.location], input.fetchIndex});
}
return result;
}
@@ -418,7 +415,7 @@ std::shared_ptr<const CompiledVariant> findOrCompileVariant(SourceEntry& source,
if (ShaderDiskCache::Load(diskKey, loaded)) {
loaded.specialization = specialization;
loaded.layout = request.layout;
loaded.result.variantId = nextVariantId();
loaded.artifact.variantId = nextVariantId();
variant = std::make_shared<const CompiledVariant>(std::move(loaded));
}
}
@@ -564,9 +561,9 @@ std::shared_ptr<const RecompileResult> materializeMemoized(SourceEntry& source,
{
std::lock_guard lock(source.mutex);
variant = findOrCompileVariant(source, request, snapshot, specialization, cacheHit);
index = (variant->result.variantId * 0x9e3779b97f4a7c15ull) ^ hash;
index = (variant->artifact.variantId * 0x9e3779b97f4a7c15ull) ^ hash;
const auto found = source.memoIndex.find(index);
if (found != source.memoIndex.end() && found->second->variantId == variant->result.variantId && found->second->hash == hash) {
if (found != source.memoIndex.end() && found->second->variantId == variant->artifact.variantId && found->second->hash == hash) {
source.memo.splice(source.memo.begin(), source.memo, found->second);
counters.hits.fetch_add(1, std::memory_order_relaxed);
if (memoHit != nullptr) *memoHit = true;
@@ -584,7 +581,7 @@ std::shared_ptr<const RecompileResult> materializeMemoized(SourceEntry& source,
std::lock_guard lock(source.mutex);
const auto found = source.memoIndex.find(index);
if (found != source.memoIndex.end()) {
if (found->second->variantId == variant->result.variantId && found->second->hash == hash) {
if (found->second->variantId == variant->artifact.variantId && found->second->hash == hash) {
source.memo.splice(source.memo.begin(), source.memo, found->second);
shared = found->second->result;
} else {
@@ -593,7 +590,7 @@ std::shared_ptr<const RecompileResult> materializeMemoized(SourceEntry& source,
}
}
if (source.memoIndex.find(index) == source.memoIndex.end()) {
source.memo.push_front({variant->result.variantId, hash, shared});
source.memo.push_front({variant->artifact.variantId, hash, shared});
source.memoIndex.emplace(index, source.memo.begin());
while (source.memo.size() > ResultMemoEntries) {
const auto& last = source.memo.back();
+20 -9
View File
@@ -399,17 +399,21 @@ private:
std::shared_ptr<std::vector<std::uint32_t>> words;
};
struct RecompileResult {
struct VertexInput {
std::uint32_t location;
std::uint32_t components;
std::uint32_t fetchIndex;
bool operator==(const VertexInput& other) const = default;
};
struct CompiledShaderArtifact {
SharedSpirv spirv;
std::vector<DescriptorBinding> bindings;
std::vector<std::byte> pushConstants;
std::uint32_t memoryOffsetDword = 0;
std::uint32_t bdaAbiVersion = 0;
std::vector<VertexAttribute> vertexAttributes;
std::vector<VertexInput> vertexInputs;
std::int32_t vertexOffsetSgpr = -1;
std::int32_t instanceOffsetSgpr = -1;
// The offset SGPR is also read elsewhere in the program (so a value folded into the draw's
// first vertex / instance cannot stand in for it), or two SGPRs were added (the SGPR is -1).
bool vertexOffsetShared = false;
bool instanceOffsetShared = false;
bool vertexOffsetConflict = false;
@@ -417,12 +421,19 @@ struct RecompileResult {
std::uint32_t hostSubgroupSize = 0;
std::vector<std::uint32_t> parameterExports;
std::vector<FragmentParameter> fragmentParameters;
bool cacheHit = false;
// Identifies the compiled variant the result came from: equal ids mean identical SPIR-V and
// bindings, so drivers can reuse pipeline objects. Zero when unknown.
std::uint64_t variantId = 0;
};
struct ShaderInvocation {
std::vector<DescriptorBinding> bindings;
std::vector<std::byte> pushConstants;
std::vector<VertexAttribute> vertexAttributes;
};
struct RecompileResult : CompiledShaderArtifact, ShaderInvocation {
bool cacheHit = false;
};
[[nodiscard]] RecompileResult Recompile(const RecompileRequest& request);
// The resource plan, snapshot and specialization a driver captured for the request (see
+46 -32
View File
@@ -63,9 +63,12 @@ std::filesystem::path ShaderCacheDirectory() {
namespace ShaderRecompiler::ShaderDiskCache {
#if defined(__linux__) && defined(__x86_64__) && defined(__GLIBCXX__)
static_assert(sizeof(RecompileResult) == 176, "RecompileResult changed: update EncodeResult and DecodeResult");
static_assert(sizeof(CompiledShaderArtifact) == 120, "CompiledShaderArtifact changed: update the artifact encoder");
static_assert(sizeof(ShaderInvocation) == 72, "ShaderInvocation changed: update the invocation encoder");
static_assert(sizeof(RecompileResult) == 200, "RecompileResult changed: update EncodeResult and DecodeResult");
static_assert(sizeof(DescriptorBinding) == 448, "DescriptorBinding changed: update the binding encoder");
static_assert(sizeof(VertexAttribute) == 28, "VertexAttribute changed: update the attribute encoder");
static_assert(sizeof(VertexInput) == 12, "VertexInput changed: update the vertex input encoder");
static_assert(sizeof(FragmentParameter) == 12, "FragmentParameter changed: update the parameter encoder");
static_assert(sizeof(CompiledShaderInfo) == 304, "CompiledShaderInfo changed: update the info encoder");
static_assert(sizeof(ShaderInfo) == 200, "ShaderInfo changed: update the info encoder");
@@ -78,6 +81,7 @@ static_assert(sizeof(StageOutput) == 48, "StageOutput changed: update the info e
static_assert(sizeof(IrBindingLayout) == 64, "IrBindingLayout changed: update the layout encoder");
static_assert(sizeof(IrDescriptorBinding) == 32, "IrDescriptorBinding changed: update the layout encoder");
static_assert(sizeof(BindingAllocationResult) == 120, "BindingAllocationResult changed: update the allocation encoder");
static_assert(sizeof(CompiledBindingLayout) == 72, "CompiledBindingLayout changed: update the allocation encoder");
static_assert(sizeof(ResourceSpecialization) == 72, "ResourceSpecialization changed: update BuildKey");
static_assert(sizeof(ResourceSpecialization::Buffer) == 16, "ResourceSpecialization::Buffer changed: update BuildKey");
static_assert(sizeof(ResourceSpecialization::Image) == 48, "ResourceSpecialization::Image changed: update BuildKey");
@@ -280,17 +284,13 @@ void decodeBinding(Reader& reader, DescriptorBinding& binding) {
reader.Values(binding.imageSamplers);
}
void encodeResult(Writer& writer, const RecompileResult& result) {
void encodeArtifact(Writer& writer, const CompiledShaderArtifact& result) {
writer.Values(std::span<const std::uint32_t>(result.spirv.Words()));
writer.List(result.bindings, encodeBinding);
writer.Value<std::uint64_t>(result.pushConstants.size());
for (const auto byte : result.pushConstants) writer.Value(static_cast<std::uint8_t>(byte));
writer.Value(result.bdaAbiVersion);
writer.Value(result.memoryOffsetDword);
writer.List(result.vertexAttributes, [](Writer& out, const VertexAttribute& attribute) {
writer.List(result.vertexInputs, [](Writer& out, const VertexInput& attribute) {
out.Value(attribute.location);
out.Value(attribute.components);
for (const auto field : attribute.resource.fields) out.Value(field);
out.Value(attribute.fetchIndex);
});
writer.Value(result.vertexOffsetSgpr);
@@ -310,21 +310,15 @@ void encodeResult(Writer& writer, const RecompileResult& result) {
});
}
void decodeResult(Reader& reader, RecompileResult& result) {
void decodeArtifact(Reader& reader, CompiledShaderArtifact& result) {
std::vector<std::uint32_t> words;
reader.Values(words);
result.spirv = std::move(words);
reader.List(result.bindings, 8, decodeBinding);
std::vector<std::uint8_t> pushConstants;
reader.Values(pushConstants);
result.pushConstants.resize(pushConstants.size());
if (!pushConstants.empty()) std::memcpy(result.pushConstants.data(), pushConstants.data(), pushConstants.size());
reader.Value(result.bdaAbiVersion);
reader.Value(result.memoryOffsetDword);
reader.List(result.vertexAttributes, 28, [](Reader& in, VertexAttribute& attribute) {
reader.List(result.vertexInputs, 12, [](Reader& in, VertexInput& attribute) {
in.Value(attribute.location);
in.Value(attribute.components);
for (auto& field : attribute.resource.fields) in.Value(field);
in.Value(attribute.fetchIndex);
});
reader.Value(result.vertexOffsetSgpr);
@@ -342,10 +336,35 @@ void decodeResult(Reader& reader, RecompileResult& result) {
in.Value(parameter.perVertex);
in.Value(parameter.custom);
});
result.cacheHit = false;
result.variantId = 0;
}
void encodeInvocation(Writer& writer, const ShaderInvocation& invocation) {
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));
writer.List(invocation.vertexAttributes, [](Writer& out, const VertexAttribute& attribute) {
out.Value(attribute.location);
out.Value(attribute.components);
for (const auto field : attribute.resource.fields) out.Value(field);
out.Value(attribute.fetchIndex);
});
}
void decodeInvocation(Reader& reader, ShaderInvocation& invocation) {
reader.List(invocation.bindings, 8, decodeBinding);
std::vector<std::uint8_t> pushConstants;
reader.Values(pushConstants);
invocation.pushConstants.resize(pushConstants.size());
if (!pushConstants.empty()) std::memcpy(invocation.pushConstants.data(), pushConstants.data(), pushConstants.size());
reader.List(invocation.vertexAttributes, 28, [](Reader& in, VertexAttribute& attribute) {
in.Value(attribute.location);
in.Value(attribute.components);
for (auto& field : attribute.resource.fields) in.Value(field);
in.Value(attribute.fetchIndex);
});
}
void encodeLayout(Writer& writer, const IrBindingLayout& layout) {
writer.Value(layout.pushDataStartDword);
writer.Value(layout.memoryOffsetDword);
@@ -560,24 +579,16 @@ void decodeInfo(Reader& reader, CompiledShaderInfo& compiled) {
decodeLayout(reader, compiled.bindings);
}
void encodeAllocation(Writer& writer, const BindingAllocationResult& allocation) {
writer.List(allocation.bindings, encodeBinding);
void encodeAllocation(Writer& writer, const CompiledBindingLayout& allocation) {
encodeLayout(writer, allocation.layout);
writer.Value(allocation.pushConstantOffsetBytes);
writer.Value(allocation.pushConstantSizeBytes);
writer.Value<std::uint64_t>(allocation.pushConstants.size());
for (const auto byte : allocation.pushConstants) writer.Value(static_cast<std::uint8_t>(byte));
}
void decodeAllocation(Reader& reader, BindingAllocationResult& allocation) {
reader.List(allocation.bindings, 8, decodeBinding);
void decodeAllocation(Reader& reader, CompiledBindingLayout& allocation) {
decodeLayout(reader, allocation.layout);
reader.Value(allocation.pushConstantOffsetBytes);
reader.Value(allocation.pushConstantSizeBytes);
std::vector<std::uint8_t> pushConstants;
reader.Values(pushConstants);
allocation.pushConstants.resize(pushConstants.size());
if (!pushConstants.empty()) std::memcpy(allocation.pushConstants.data(), pushConstants.data(), pushConstants.size());
}
constexpr std::string_view NeutralSwitches[] = {
@@ -847,20 +858,23 @@ std::string EntryName(std::span<const std::byte> key) {
void EncodeResult(const RecompileResult& result, std::vector<std::byte>& out) {
Writer writer(out);
encodeResult(writer, result);
encodeArtifact(writer, result);
encodeInvocation(writer, result);
}
bool DecodeResult(std::span<const std::byte> bytes, RecompileResult& result) {
Reader reader(bytes);
decodeResult(reader, result);
decodeArtifact(reader, result);
decodeInvocation(reader, result);
result.cacheHit = false;
return reader.Done();
}
std::vector<std::byte> EncodeEntry(std::span<const std::byte> key, const CompiledVariant& variant) {
std::vector<std::byte> payload;
payload.reserve(variant.result.spirv.size() * sizeof(std::uint32_t) + 4096);
payload.reserve(variant.artifact.spirv.size() * sizeof(std::uint32_t) + 4096);
Writer writer(payload);
encodeResult(writer, variant.result);
encodeArtifact(writer, variant.artifact);
encodeInfo(writer, variant.info);
encodeAllocation(writer, variant.bindings);
const FileHeader header{FileMagic, FormatVersion, SourceVersion(), key.size(), payload.size(), HashBytes(key), HashBytes(payload)};
@@ -885,13 +899,13 @@ LoadStatus DecodeEntry(std::span<const std::byte> file, std::span<const std::byt
if (HashBytes(payload) != header.payloadHash) return LoadStatus::Rejected;
Reader reader(payload);
CompiledVariant decoded;
decodeResult(reader, decoded.result);
decodeArtifact(reader, decoded.artifact);
decodeInfo(reader, decoded.info);
decodeAllocation(reader, decoded.bindings);
if (!reader.Done()) return LoadStatus::Rejected;
variant.info = std::move(decoded.info);
variant.bindings = std::move(decoded.bindings);
variant.result = std::move(decoded.result);
variant.artifact = std::move(decoded.artifact);
return LoadStatus::Loaded;
}