From 71851d66a3f1992b53bbd443bd5313ac22d470ec Mon Sep 17 00:00:00 2001 From: Gjon Gojcevic Date: Wed, 7 Oct 2026 22:21:45 -0400 Subject: [PATCH] fix(recompiler): take point-filtered sampler copies' descriptors from their source sampler When one guest sampler is paired both with an image that needs point filtering (sint, depth-bits or converted format) and with an ordinary image, ResourceMaterializer::Apply appends a point-filtered copy of the sampler after the original samplers. The resource snapshot only holds the original samplers, so DescriptorBindingBuilder::Populate threw "guest sampler index is out of range" for the copy, both in the sampler binding and in the unnormalized-coordinate check. SamplerResource::copyOf records the sampler a copy was made from, and the guest sampler descriptor of a copy is read from that sampler; the point-filter rewrite of word 2 still applies to the copy. The field is part of the disk cache encoding: cache format version 8. Co-Authored-By: Claude Opus 5.5 --- .../libSceAgcDriver/tests/ShaderDiskCache.cpp | 2 +- .../execution/ImageSampleSharedSampler.cpp | 153 ++++++++++++++++++ .../IrMetadata/Resources.hpp | 3 + .../src/DescriptorBindingBuilder.cpp | 9 +- .../Optimization/src/ResourceMaterializer.cpp | 1 + core/shader/recompiler/ShaderDiskCache.cpp | 6 +- 6 files changed, 167 insertions(+), 7 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/ImageSampleSharedSampler.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp b/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp index 56cc59dab..913cce3b7 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp @@ -172,7 +172,7 @@ CompiledVariant sampleVariant() { image.indirectSearchIterations = 3; image.indirectResources = {1, 2, 3}; info.info.images = {image}; - info.info.samplers = {{7, 0x10, true, false, SamplerUseExplicitLod | SamplerUseGather}}; + info.info.samplers = {{7, 0x10, 3, true, false, SamplerUseExplicitLod | SamplerUseGather}}; info.info.sampledPairs = {{0, 0, 0x10}}; StageInput input{}; input.kind = StageInputKind::GlobalInvocationId; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageSampleSharedSampler.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageSampleSharedSampler.cpp new file mode 100644 index 000000000..50fbcbaf0 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageSampleSharedSampler.cpp @@ -0,0 +1,153 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 32; +constexpr std::uint32_t Size = 8; +constexpr std::uint32_t Format32SInt = 21; +constexpr std::uint32_t Format8888UNorm = 56; +constexpr std::uint32_t Type2D = 9; +constexpr std::uint32_t SwizzleX111 = 0x24cu; +constexpr std::uint32_t IdentitySwizzle = 0xfacu; +constexpr std::uint32_t SampleX = 3; +constexpr std::uint32_t SampleY = 5; +constexpr float Tolerance = 1.0f / 64.0f; +constexpr std::array LinearSampler{0x00000092u, 0x00fff000u, 0x00500000u, 0u}; + +alignas(256) std::array Output{}; +alignas(256) std::array Integers{}; +alignas(256) std::array Colors{}; + +alignas(256) constexpr std::array Code{ + 0x34060085u, 0x7e2802ffu, 0x3ef00000u, 0x7e2a02ffu, 0x3f300000u, 0xf09c0f08u, 0x00610a14u, 0xf09c0f08u, + 0x00640e14u, 0xbf8c3f70u, 0xe0781000u, 0x80000a03u, 0xe0781010u, 0x80000e03u, 0xbf810000u, 0xbf810000u, +}; + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +std::uint32_t PitchBytes(std::uint32_t format, std::size_t storage) { + const auto mips = AgcDriver::Graphics::ComputeMipLayout(AgcDriver::Graphics::TextureTileMode::kLinear, format, Size, Size, 1u); + Require(AgcDriver::Graphics::ComputeSurfaceSize(mips, 1) <= storage, "image sample shared sampler: a texture does not fit its storage"); + return mips[0].pitchBytes; +} + +std::int32_t Integer(std::uint32_t x, std::uint32_t y) { + return -static_cast(100u * x + 7u * y + 1u); +} + +void Fill() { + const auto integerPitch = PitchBytes(Format32SInt, Integers.size()); + const auto colorPitch = PitchBytes(Format8888UNorm, Colors.size()); + Integers.fill(0x5au); + Colors.fill(0x5au); + for (std::uint32_t y = 0; y < Size; ++y) { + for (std::uint32_t x = 0; x < Size; ++x) { + const auto bits = static_cast(Integer(x, y)); + auto* integer = &Integers[static_cast(y) * integerPitch + x * 4u]; + for (std::uint32_t byte = 0; byte < 4u; ++byte) integer[byte] = static_cast(bits >> (byte * 8u)); + auto* color = &Colors[static_cast(y) * colorPitch + x * 4u]; + color[0] = x > SampleX ? 255u : 0u; + color[1] = 0u; + color[2] = 0u; + color[3] = 255u; + } + } +} + +std::array BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu), bytes, 0x01016facu}; +} + +std::array TextureDescriptor(const void* texels, std::uint32_t format, std::uint32_t swizzle) { + const auto address = static_cast(reinterpret_cast(texels)); + return { + static_cast(address >> 8u), + static_cast((address >> 40u) & 0xffu) | (format << 20u) | (((Size - 1u) & 3u) << 30u), + ((Size - 1u) >> 2u) | ((Size - 1u) << 14u), + swizzle | (Type2D << 28u), + 0u, + 0u, + 0u, + 0u, + }; +} + +void Run(AgcDriver::VulkanDevice& device) { + Output.fill(0xdeadbeefu); + std::vector userData(24, 0u); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size() * 4u)); + const auto integers = TextureDescriptor(Integers.data(), Format32SInt, SwizzleX111); + const auto colors = TextureDescriptor(Colors.data(), Format8888UNorm, IdentitySwizzle); + std::copy(output.begin(), output.end(), userData.begin()); + std::copy(integers.begin(), integers.end(), userData.begin() + 4); + std::copy(LinearSampler.begin(), LinearSampler.end(), userData.begin() + 12); + std::copy(colors.begin(), colors.end(), userData.begin() + 16); + const std::span code(Code); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {true, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + const auto result = ShaderRecompiler::Recompile(request); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); +} + +void Check(AgcDriver::VulkanDevice& device) { + Fill(); + Run(device); + const std::array integer{static_cast(Integer(SampleX, SampleY)), 1u, 1u, 1u}; + const std::array color{0.25f, 0.0f, 0.0f, 1.0f}; + for (std::uint32_t lane = 0; lane < Threads; ++lane) { + for (std::uint32_t component = 0; component < 4u; ++component) { + const auto actual = Output[lane * 8u + component]; + Require(actual == integer[component], "32_SINT image_sample_lz through a bilinear sampler shared with 8_8_8_8_UNORM: lane " + std::to_string(lane) + " component " + std::to_string(component) + " is " + Hex(actual) + ", expected " + Hex(integer[component])); + } + for (std::uint32_t component = 0; component < 4u; ++component) { + const auto bits = Output[lane * 8u + 4u + component]; + const auto actual = std::bit_cast(bits); + Require(std::fabs(actual - color[component]) <= Tolerance, "8_8_8_8_UNORM image_sample_lz through a bilinear sampler shared with 32_SINT: lane " + std::to_string(lane) + " component " + std::to_string(component) + " is " + Hex(bits) + ", expected " + std::to_string(color[component])); + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Check(*device); + std::puts("image sample shared sampler tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp index ef670f5c9..4725071cb 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp @@ -104,8 +104,11 @@ inline bool ImageSampleExplicitLod(std::uint32_t flags, IrShaderStage stage) { } struct SamplerResource { + static constexpr std::uint32_t NoCopy = std::numeric_limits::max(); + std::uint32_t source = 0; std::uint32_t firstUsePc = 0; + std::uint32_t copyOf = NoCopy; bool forcePointFiltering = false; bool depthCompare = false; std::uint8_t uses = 0; diff --git a/core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp b/core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp index 6cd17b6b3..226a3fe63 100644 --- a/core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp +++ b/core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp @@ -152,11 +152,12 @@ std::vector GuestImagesDescriptor(const std::vector GuestSamplersDescriptor(const std::vector& resources, const ResourceSnapshot& snapshot) { +std::vector GuestSamplersDescriptor(const ShaderInfo& info, const std::vector& resources, const ResourceSnapshot& snapshot) { std::vector result; std::uint32_t dwordCount = 0; for (std::size_t i = 0; i < resources.size(); i++) { - const std::uint32_t r = resources[i]; + const std::uint32_t copyOf = info.samplers.at(resources[i]).copyOf; + const std::uint32_t r = copyOf != SamplerResource::NoCopy ? copyOf : resources[i]; if (r >= snapshot.samplers.size()) { fail("DescriptorBindingBuilder::Populate guest sampler index is out of range"); } @@ -202,7 +203,7 @@ struct UnnormalizedProof { UnnormalizedProof ProveUnnormalized(const ShaderInfo& info, const ResourceSnapshot& snapshot) { UnnormalizedProof proof{std::vector(info.samplers.size()), std::vector(info.images.size())}; for (std::uint32_t r = 0; r < info.samplers.size(); r++) { - if ((GuestSamplersDescriptor({r}, snapshot)[0] & ForceUnnormalizedBit) == 0u) { + if ((GuestSamplersDescriptor(info, {r}, snapshot)[0] & ForceUnnormalizedBit) == 0u) { continue; } const auto& sampler = info.samplers[r]; @@ -345,7 +346,7 @@ void DescriptorBindingBuilder::Populate(BindingAllocationResult& allocation, con } break; case DescriptorRole::GuestSamplers: - physical.guestDescriptor = GuestSamplersDescriptor(logical.resources, snapshot); + physical.guestDescriptor = GuestSamplersDescriptor(info, logical.resources, snapshot); for (std::size_t element = 0; element < logical.resources.size(); ++element) { const auto& sampler = info.samplers.at(logical.resources[element]); physical.samplerDepthCompare.push_back(sampler.depthCompare); diff --git a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp index ee397fe99..6f2eaca98 100644 --- a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp @@ -763,6 +763,7 @@ void ResourceMaterializer::Apply(IrProgram& program, const ResourceSpecializatio } auto sampler = samplers[index]; sampler.forcePointFiltering = true; + sampler.copyOf = index; samplers.push_back(sampler); } } diff --git a/core/shader/recompiler/ShaderDiskCache.cpp b/core/shader/recompiler/ShaderDiskCache.cpp index 90ba3a161..92b962d84 100644 --- a/core/shader/recompiler/ShaderDiskCache.cpp +++ b/core/shader/recompiler/ShaderDiskCache.cpp @@ -71,7 +71,7 @@ static_assert(sizeof(CompiledShaderInfo) == 304, "CompiledShaderInfo changed: up static_assert(sizeof(ShaderInfo) == 200, "ShaderInfo changed: update the info encoder"); static_assert(sizeof(BufferResource) == 36, "BufferResource changed: update the info encoder"); static_assert(sizeof(ImageResource) == 96, "ImageResource changed: update the info encoder"); -static_assert(sizeof(SamplerResource) == 12, "SamplerResource changed: update the info encoder"); +static_assert(sizeof(SamplerResource) == 16, "SamplerResource changed: update the info encoder"); static_assert(sizeof(SampledResourcePair) == 12, "SampledResourcePair changed: update the info encoder"); static_assert(sizeof(StageInput) == 56, "StageInput changed: update the info encoder"); static_assert(sizeof(StageOutput) == 48, "StageOutput changed: update the info encoder"); @@ -428,6 +428,7 @@ void encodeInfo(Writer& writer, const CompiledShaderInfo& compiled) { writer.List(info.samplers, [](Writer& out, const SamplerResource& sampler) { out.Value(sampler.source); out.Value(sampler.firstUsePc); + out.Value(sampler.copyOf); out.Value(sampler.forcePointFiltering); out.Value(sampler.depthCompare); out.Value(sampler.uses); @@ -519,9 +520,10 @@ void decodeInfo(Reader& reader, CompiledShaderInfo& compiled) { in.Value(image.indirectSearchIterations); in.Values(image.indirectResources); }); - reader.List(info.samplers, 11, [](Reader& in, SamplerResource& sampler) { + reader.List(info.samplers, 15, [](Reader& in, SamplerResource& sampler) { in.Value(sampler.source); in.Value(sampler.firstUsePc); + in.Value(sampler.copyOf); in.Value(sampler.forcePointFiltering); in.Value(sampler.depthCompare); in.Value(sampler.uses);