From ed0b89e0424ba4ad19da2aa34cdab471ae231394 Mon Sep 17 00:00:00 2001 From: boykopovar Date: Tue, 6 Oct 2026 23:40:39 +0300 Subject: [PATCH] fix(shader): relocate prepared push constants across graphics stages --- .../src/Driver/Shaders/ShaderRegistry.cpp | 18 +++++++++++++++--- .../recompiler/PipelineSpecialization.hpp | 1 + core/shader/recompiler/Recompiler.cpp | 11 ++++++++++- core/shader/recompiler/RuntimeAbi.hpp | 2 +- .../src/SpirvMemory/SpirvBufferAccess.cpp | 5 ++++- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp index bff2ed883..0b02d0483 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp @@ -168,7 +168,13 @@ std::shared_ptr SourceHandleFor(const Shad snapshot.prepared->entries.push_back({codeOffset, handle}); return handle; } - throw std::runtime_error("AGC driver: prepared shader artifact is missing for the requested static ABI"); + std::string layouts; + for (const auto& entry : snapshot.prepared->entries) { + if (entry.codeOffset != codeOffset || entry.handle == nullptr || entry.handle->artifact == nullptr) continue; + const auto& layout = entry.handle->artifact->layout; + layouts += " [" + std::to_string(layout.pushConstantOffsetBytes) + "," + std::to_string(layout.pushConstantSizeBytes) + "]"; + } + throw std::runtime_error("AGC driver: prepared shader artifact is missing for the requested static ABI: address=" + std::to_string(request.shader.codeAddress) + " stage=" + std::to_string(static_cast(request.shader.stage)) + " wave=" + std::to_string(request.context.waveSize) + " pushOffset=" + std::to_string(request.layout.pushConstantOffsetBytes) + " pushCapacity=" + std::to_string(request.layout.pushConstantSizeBytes) + " preparedLayouts=" + layouts); } ShaderRecompiler::RectListShaders PreparedRectangle(const ShaderSnapshot& snapshot, std::uint64_t vertexId, std::uint64_t fragmentId) { @@ -190,7 +196,13 @@ ShaderRecompiler::PreparedShaderInvocation InvocationFor(const ShaderSnapshot& s invocationRequest.shader.code = ShaderRecompiler::GetPreparedCode(*entry.handle); if (auto invocation = ShaderRecompiler::PreparedShaderInvocation::TryCreate(invocationRequest, entry.handle)) return std::move(*invocation); } - throw std::runtime_error("AGC driver: prepared shader artifact is missing for the requested static ABI"); + std::string layouts; + for (const auto& entry : snapshot.prepared->entries) { + if (entry.codeOffset != codeOffset || entry.handle == nullptr || entry.handle->artifact == nullptr) continue; + const auto& layout = entry.handle->artifact->layout; + layouts += " [" + std::to_string(layout.pushConstantOffsetBytes) + "," + std::to_string(layout.pushConstantSizeBytes) + "]"; + } + throw std::runtime_error("AGC driver: prepared shader artifact is missing for the requested static ABI: address=" + std::to_string(request.shader.codeAddress) + " stage=" + std::to_string(static_cast(request.shader.stage)) + " wave=" + std::to_string(request.context.waveSize) + " pushOffset=" + std::to_string(request.layout.pushConstantOffsetBytes) + " pushCapacity=" + std::to_string(request.layout.pushConstantSizeBytes) + " preparedLayouts=" + layouts); } namespace { @@ -307,7 +319,7 @@ std::vector PrepareRegistered(const ShaderSnapshot& snap request.context.compute->partialThreads = {1, 1, 1}; append(); } else if (pixel) { - request.layout.pushConstantSizeBytes = ShaderRecompiler::MeshDrawPushOffsetBytes; + request.layout.pushConstantSizeBytes = 0; append(); } return entries; diff --git a/core/shader/recompiler/PipelineSpecialization.hpp b/core/shader/recompiler/PipelineSpecialization.hpp index f8f54d05b..cc84c769c 100644 --- a/core/shader/recompiler/PipelineSpecialization.hpp +++ b/core/shader/recompiler/PipelineSpecialization.hpp @@ -20,6 +20,7 @@ inline constexpr std::uint32_t ImageBase = 1024u; inline constexpr std::uint32_t ImageWords = 5u; inline constexpr std::uint32_t VertexBase = 4096u; inline constexpr std::uint32_t VertexWords = 6u; +inline constexpr std::uint32_t PushDataOffset = 8188u; inline constexpr std::uint32_t ExportBase = 8192u; inline constexpr std::uint32_t HeapCountBase = 8256u; inline constexpr std::uint32_t MipCountBase = 8320u; diff --git a/core/shader/recompiler/Recompiler.cpp b/core/shader/recompiler/Recompiler.cpp index d1bc73384..b247ddca6 100644 --- a/core/shader/recompiler/Recompiler.cpp +++ b/core/shader/recompiler/Recompiler.cpp @@ -515,6 +515,7 @@ RecompileResult materializeResult(const CompiledVariant& variant, const Recompil DescriptorBindingBuilder{}.Populate(bindings, variant.info.info, variant.info.stage, variant.info.userDataBase, snapshot, partialThreads(request), request.context.pixel ? std::span(request.context.pixel->targetExportMapping) : std::span{}); result.bindings = std::move(bindings.bindings); result.specialization = std::move(bindings.specialization); + if (variant.bindings.layout.UsesPushData()) result.specialization.push_back({PipelineSpecialization::PushDataOffset, request.layout.pushConstantOffsetBytes / 4u}); result.pushConstants = std::move(bindings.pushConstants); result.vertexAttributes.reserve(result.vertexInputs.size()); std::array vertexClasses{}; @@ -695,6 +696,7 @@ std::uint64_t snapshotHash(const RecompileRequest& request, const ResourceSnapsh mix(snapshot.uniformFill.words); mix(snapshot.uniformFill.value); for (const auto threads : partialThreads(request)) mix(threads); + mix(request.layout.pushConstantOffsetBytes); if (request.context.pixel) { for (const auto mapping : request.context.pixel->targetExportMapping) mix(mapping); } @@ -866,7 +868,14 @@ std::shared_ptr PrepareShader(const RecompileRequest& reques } bool MatchesPreparedShader(const RecompileRequest& request, const SourceHandle& handle) { - if (handle.source == nullptr || handle.artifact == nullptr || !sameLayout(handle.artifact->layout, request.layout)) return false; + if (handle.source == nullptr || handle.artifact == nullptr) return false; + const auto& layout = request.layout; + const auto& prepared = handle.artifact->bindings.layout; + if (layout.descriptorSet != handle.artifact->layout.descriptorSet || layout.firstBinding != handle.artifact->layout.firstBinding) return false; + if (layout.pushConstantOffsetBytes % 4u != 0u || layout.pushConstantSizeBytes % 4u != 0u || layout.pushConstantOffsetBytes > NativePushConstantSize || layout.pushConstantSizeBytes > NativePushConstantSize - layout.pushConstantOffsetBytes) return false; + const auto words = prepared.ShaderDataDwords(); + const bool usesPush = prepared.runtimeImageCount == 0u && words != 0u && words <= layout.pushConstantSizeBytes / 4u; + if (prepared.UsesPushData() != usesPush) return false; struct PreparedKeyStorage {}; auto& key = HostThreadLocal, PreparedKeyStorage>(); RecompileCacheKey::BuildInterface(request, key); diff --git a/core/shader/recompiler/RuntimeAbi.hpp b/core/shader/recompiler/RuntimeAbi.hpp index a4bfc0f35..0676d240e 100644 --- a/core/shader/recompiler/RuntimeAbi.hpp +++ b/core/shader/recompiler/RuntimeAbi.hpp @@ -9,7 +9,7 @@ namespace ShaderRecompiler::RuntimeAbi { -inline constexpr std::uint32_t Version = 7u; +inline constexpr std::uint32_t Version = 8u; inline constexpr std::uint32_t DescriptorSet = 0u; inline constexpr std::uint32_t StageCount = 4u; inline constexpr std::uint32_t PushConstantDwords = 32u; diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp index 356cb4edf..7416ff383 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvBufferAccess.cpp @@ -1,3 +1,4 @@ +#include "PipelineSpecialization.hpp" #include "SpirvBackend/SpirvMemory/SpirvBufferAccess.hpp" #include "SpirvBackend/SpirvEmitterHelpers.hpp" #include "SpirvBackend/SpirvMemory/SpirvTypes.hpp" @@ -36,7 +37,9 @@ std::uint32_t EmitShaderDataDwordLoad(SpirvEmitterState& state, std::uint32_t dw const auto pointer = state.module.AllocateId(); const auto value = state.module.AllocateId(); if (layout.UsesPushData()) { - state.module.AddFunction(spv::OpAccessChain, TypePushConstantElementPointer(state), pointer, state.pushConstantVariable, ConstantU32(state, 0), ConstantU32(state, dwordIndex + layout.pushDataStartDword)); + const auto base = state.module.SpecializationConstant(TypeU32(state), PipelineSpecialization::PushDataOffset, layout.pushDataStartDword); + const auto index = Binary(state, spv::OpIAdd, TypeU32(state), base, ConstantU32(state, dwordIndex)); + state.module.AddFunction(spv::OpAccessChain, TypePushConstantElementPointer(state), pointer, state.pushConstantVariable, ConstantU32(state, 0), index); } else if (state.shaderDataStorageVariable != 0) { state.module.AddFunction(spv::OpAccessChain, TypeStorageBufferElementPointer(state), pointer, state.shaderDataStorageVariable, ConstantU32(state, 0), ConstantU32(state, dwordIndex)); } else {