diff --git a/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp b/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp index 357004d725ea..f3beb8dd6a3f 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp @@ -6826,6 +6826,41 @@ void verifyEvaluableHeapSamplerDirect() { require(perLane->info.samplers.size() == 1u && perLane->info.samplers[0].table != NoTable && perLane->descriptorSources.at(perLane->info.samplers[0].source).tableColumn.has_value(), "evaluable heap sampler: an S# read from a heap at an offset keyed per lane was not planned through a sampler table"); } +void verifyAdjustedTableSamplerDword3() { + using namespace ShaderRecompiler; + static TableTexture textures[2]; + alignas(4096) static TableGuest guest; + auto& heap = guest.heap; + std::fill(heap.begin(), heap.end(), std::array{}); + heap[0] = TableT(textures[0].bytes.data(), TableFormat8888UNorm, TableType2D); + heap[1] = TableT(textures[1].bytes.data(), TableFormat32UInt, TableType2D); + FillTableSrt(guest, heap.data(), 32u, 2u); + const auto userData = TableUserData(guest); + const auto tableSampler = [](std::uint32_t word0) { + auto code = TableWholeCode; + const auto direct = std::find(code.begin(), code.end(), 0xf4080200u); + require(direct != code.end() && direct[1] == 0xfa000010u, "adjusted table sampler: the program has no S# load from the SRT"); + code.erase(direct, direct + 2); + const auto shift = std::find(code.begin(), code.end(), 0x8f108510u); + require(shift != code.end(), "adjusted table sampler: the program has no key shift"); + code.insert(shift + 1, {0xf4280202u, 0x20000000u, 0x7d8a0080u, 0x8720ff6au, 0x000000ffu, 0x8f208c20u, 0x880b200bu}); + const auto sample = std::find(code.begin(), code.end(), 0xf09c0f08u); + require(sample != code.end(), "adjusted table sampler: the program has no image_sample_lz"); + *sample = word0; + return code; + }; + const auto adjusted = GetResourcePlan(TableRequest(tableSampler(0xf0800f09u), userData)); + require(adjusted->info.samplers.size() == 1u && adjusted->info.samplers[0].table != NoTable && adjusted->descriptorSources.at(adjusted->info.samplers[0].source).tableColumn.has_value(), "adjusted table sampler: an image_sample_a whose table S# dword 3 also holds a ballot in its reserved bits was not planned through a sampler table"); + std::uint32_t tableReads = 0; + for (const auto& memory : adjusted->memoryInfo) { + if (memory.kind != ResourceKind::ScalarBuffer) continue; + require(memory.planningOnly, "adjusted table sampler: a table descriptor read stayed a GPU memory access"); + tableReads++; + } + require(tableReads == 12u, "adjusted table sampler: the program does not read one T# and one S# from the table"); + expectFailure([&] { static_cast(GetResourcePlan(TableRequest(tableSampler(0xf0800f08u), userData))); }, "GetSamplerResource dword 0 is not a valid runtime value", "adjusted table sampler: an image_sample whose table S# dword 3 holds a ballot was accepted"); +} + void verifySelectedRecordUniformIndex() { using namespace ShaderRecompiler; static TableTexture textures[2]; @@ -13698,6 +13733,7 @@ int main(int argc, char** argv) { verifySelectedRecordOffsetCarry(); verifySelectedRecordDirectOperations(); verifyEvaluableHeapSamplerDirect(); + verifyAdjustedTableSamplerDword3(); verifySelectedRecordUniformIndex(); verifyChainedTableOffset(); verifyPointerChainImageTable(); diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp index f099c2d80ec7..e55deb739eb3 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp @@ -936,6 +936,21 @@ void RunTests(AgcDriver::VulkanDevice& device) { Require(!Faulted(classes), "image table: valid entries faulted:\n" + classes.log); Require(classes.imageTableCount == 3u, "image table: the table binding does not hold one element per distinct T# (" + std::to_string(classes.imageTableCount) + ")"); + const auto adjustedCode = [] { + constexpr std::size_t sample = 19u; + static_assert(TableCode[sample] == 0xf09c8f08u); + std::array code{}; + std::copy_n(TableCode.begin(), sample, code.begin()); + code[sample] = 0x8823ff23u; + code[sample + 1u] = 0x1000u; + std::copy(TableCode.begin() + sample, TableCode.end(), code.begin() + sample + 2u); + code[sample + 2u] = 0xf0808f09u; + return code; + }(); + const auto adjusted = Run(device, adjustedCode, 6, Lanes, 64); + Require(!Faulted(adjusted), "adjusted sampler table: a valid record faulted:\n" + adjusted.log); + Require(adjusted.words == classes.words, "adjusted sampler table: the reference texels changed"); + const auto invariantPhiCode = [] { std::array code{}; std::copy_n(TableCode.begin(), 19, code.begin()); diff --git a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp index 5b41ae74ed6f..31bda2b8d165 100644 --- a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp @@ -263,6 +263,7 @@ class Tracker { std::optional keyDomain; std::vector heapLive; std::vector heapPlanned; + std::vector adjustedWords; }; struct DescriptorGuard { @@ -296,7 +297,7 @@ class Tracker { } } - IrValue* canonicalizeSampleAdjustDword3(IrValue* value) { + IrValue* canonicalizeSampleAdjustDword3(IrValue* value, std::vector* stripped = nullptr) { for (;;) { value = value->Resolve(); if ((possibleU32Bits(value) & ~samplerDword3ReservedMask) == 0u) { @@ -309,16 +310,26 @@ class Tracker { IrValue* right = value->Argument(1)->Resolve(); const bool leftReserved = (possibleU32Bits(left) & ~samplerDword3ReservedMask) == 0u; const bool rightReserved = (possibleU32Bits(right) & ~samplerDword3ReservedMask) == 0u; - if (leftReserved) { - value = right; - } else if (rightReserved) { - value = left; - } else { + if (!leftReserved && !rightReserved) { return value; } + if (stripped != nullptr) { + stripped->push_back(value); + } + value = leftReserved ? right : left; } } + IrValue* tableSamplerDword3(IrValue& handle, IrValue* word, std::vector& stripped) { + if (word->Resolve()->Opcode() != IrOpcode::BitwiseOr32) return word; + const auto onlyAdjusted = std::ranges::all_of(handle.Uses(), [&](const IrValue* use) { + if (!ImageOpcodeInfoOf(use->Opcode()).needsSampler || use->ArgumentCount() < 2u || use->Argument(1)->Resolve() != &handle) return false; + const auto index = use->Flags().index; + return index < m_program.Resources().memoryInfo.size() && (m_program.Resources().memoryInfo[index].imageSampleFlags & RdnaImageSampleFlagAdjust) != 0u; + }); + return onlyAdjusted ? canonicalizeSampleAdjustDword3(word, &stripped) : word; + } + bool ValidateSource(const DescriptorSource& descriptor, std::uint32_t& badDword) const { constexpr SrtWalker walker; for (std::uint32_t i = 0; i < descriptor.dwordCount; i++) { @@ -851,8 +862,11 @@ class Tracker { FirstLaneOffset baseLane; bool laneBase = false; for (std::uint32_t dword = 0; dword < words; dword++) { - IrValue* read = ResolveInvariantPhi(m_program.Resources(), handle.Argument(dword)); + IrValue* word = handle.Argument(dword); + if (sampler && dword == 3u) word = tableSamplerDword3(handle, word, plan.adjustedWords); + IrValue* read = ResolveInvariantPhi(m_program.Resources(), word); if (read == nullptr) return false; + if (sampler && dword == 3u) read = tableSamplerDword3(handle, read, plan.adjustedWords); std::uint32_t memoryIndex = 0; const MemoryInfo* memory = ScalarReadMemory(*read, memoryIndex); const bool addressRead = memory == nullptr && (memory = ScalarAddressMemory(*read, memoryIndex)) != nullptr; @@ -2388,12 +2402,15 @@ class Tracker { } } } + const auto plannedUses = [&](const TableColumnPlan& plan, const IrValue& value, auto&& self) -> bool { + return std::ranges::all_of(value.Uses(), [&](const IrValue* user) { + return FindTableColumn(*user) != nullptr || (std::ranges::find(plan.adjustedWords, user) != plan.adjustedWords.end() && self(plan, *user, self)); + }); + }; for (const auto& plan : m_tableColumns) { for (std::size_t index = 0; index < plan.reads.size(); index++) { const IrValue* read = plan.reads[index]; - const bool planned = std::ranges::all_of(read->Uses(), [&](const IrValue* user) { - return FindTableColumn(*user) != nullptr; - }); + const bool planned = plannedUses(plan, *read, plannedUses); if (planned && std::ranges::find(m_tablePlanningReads, read) == m_tablePlanningReads.end()) { m_tablePlanningReads.push_back(read); m_tablePlanningMemory.push_back(plan.memory[index]);