diff --git a/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Memory/BufferFill.hpp b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Memory/BufferFill.hpp new file mode 100644 index 000000000..7b27661f9 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/Execution/include/Driver/Memory/BufferFill.hpp @@ -0,0 +1,54 @@ +#ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_DRIVER_BUFFERFILL_HPP +#define CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_DRIVER_BUFFERFILL_HPP + +#include "Recompiler.hpp" +#include +#include +#include +#include +#include +#include + +namespace AgcDriver::DriverDetail { + +inline constexpr std::array PatternFillKernel{ + 0xbfa00001u, 0xd7460000u, 0x04010c0cu, 0xf4201a84u, 0xfa000000u, 0xbf8cc07fu, + 0x7da8006au, 0xbf880009u, 0xf4201a84u, 0xfa000004u, 0xbf8cc07fu, 0x3602006au, + 0xe0002000u, 0x80000101u, 0xbf8c3f70u, 0xe0102000u, 0x80010100u, 0xbf810000u +}; + +inline bool MatchesPatternFillKernel(std::span code, std::span userData, const ShaderRecompiler::ShaderComputeStageInfo& compute) { + if (userData.size() != 12 || compute.numThreads != std::array{64, 1, 1} || !compute.groupIdEnable[0] || compute.threadIdComponentCount != 1) return false; + if (code.size() < PatternFillKernel.size() || !std::equal(PatternFillKernel.begin(), PatternFillKernel.end(), code.begin())) return false; + for (const auto first : {0u, 4u, 8u}) { + if ((userData[first + 1] & 0xffff0000u) != (first == 8 ? 0x00100000u : 0x00040000u)) return false; + if (userData[first + 3] != (first == 8 ? 0x0004dfacu : 0x00014004u)) return false; + } + return userData[2] >= 1 && userData[10] >= 1; +} + +struct PatternFillRange { + std::uint64_t source, destination, control, invocations; + std::uint32_t records; + + [[nodiscard]] std::optional UniformBytes(std::uint32_t count, std::uint32_t mask) const { + if (mask != 0) return std::nullopt; + const auto bytes = std::min({invocations, std::uint64_t{count}, std::uint64_t{records}}) * 4u; + if (destination % 16 != 0 || bytes % 16 != 0) return std::nullopt; + if (bytes != 0 && ((source < destination + bytes && destination < source + 4) || (control < destination + bytes && destination < control + 8))) return std::nullopt; + return static_cast(bytes); + } +}; + +inline std::optional DecodePatternFillRange(std::span userData, std::span packet) { + if (userData.size() != 12 || packet.size() < 5 || packet[2] != 1 || packet[3] != 1) return std::nullopt; + const std::uint64_t invocations = (packet[4] & 0x20u) != 0 ? packet[1] : std::uint64_t{packet[1]} * 64u; + if (invocations > (std::uint64_t{1} << 32u)) return std::nullopt; + const auto base = [&](std::size_t word) { return userData[word] | (static_cast(userData[word + 1] & 0xffffu) << 32u); }; + if (base(0) % 4 != 0 || base(8) % 4 != 0) return std::nullopt; + return PatternFillRange{base(0), base(4), base(8), invocations, userData[6]}; +} + +} + +#endif diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Memory/BufferFill.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Memory/BufferFill.cpp index 2d614b5b4..fb3a799ed 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Memory/BufferFill.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Memory/BufferFill.cpp @@ -1,4 +1,5 @@ #include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver/Memory/BufferFill.hpp" #include "ThreadOwned.hpp" #include "prx/libSceAgcDriver/Execution/include/Driver/Diagnostics.hpp" #include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" @@ -10,7 +11,9 @@ namespace AgcDriver::DriverDetail { bool Driver::matchesFillKernel(std::span code, const std::vector& userData, const ShaderRecompiler::ShaderComputeStageInfo& compute) { static const bool enabled = std::getenv("APS5_NO_FILL_HLE") == nullptr; + static const bool patternEnabled = std::getenv("APS5_NO_PATTERN_FILL_HLE") == nullptr; if (!enabled || userData.size() < 8 || compute.numThreads[0] != 64 || compute.numThreads[1] != 1 || compute.numThreads[2] != 1) return false; + if (patternEnabled && MatchesPatternFillKernel(code, userData, compute)) return true; static constexpr std::array fillKernel{0xd7460004u, 0x04010c08u, 0x7e000204u, 0x7e020205u, 0x7e040206u, 0x7e060207u, 0xe01c2000u, 0x80000004u, 0xbf810000u}; if (code.size() < fillKernel.size() || !std::equal(fillKernel.begin(), fillKernel.end(), code.begin())) return false; @@ -89,19 +92,43 @@ void Driver::fillClearCount(const Graphics::StorageTexture::FillCoverage& covera bool Driver::fillBuffer(QueueState& queue, std::uint32_t queueId, std::span packet, std::span code, const std::vector& userData, const ShaderRecompiler::ShaderComputeStageInfo& compute, const std::shared_ptr& localDevice) { if (!matchesFillKernel(code, userData, compute)) return false; - const auto numRecords = userData[2]; - std::array groups{packet[1], packet[2], packet[3]}; - if ((packet[4] & 0x20u) != 0) { - for (std::uint32_t axis = 0; axis < 3; ++axis) { - const auto threads = std::max(readRegister(queue.shader, 0x207 + axis) & 0xffffu, 1u); - groups[axis] = (groups[axis] + threads - 1) / threads; + std::uint64_t base; + std::size_t bytes; + std::array pattern; + std::unique_lock inputLock(GuestMemory::GpuMutex(), std::defer_lock); + if (MatchesPatternFillKernel(code, userData, compute)) { + const auto range = DecodePatternFillRange(userData, packet); + if (!range) return false; + if (range->invocations == 0) return true; + GuestMemory::TagGpuLockSite(GuestMemory::GpuLockSite::Fill); + inputLock.lock(); + recordLabelsForPacket(localDevice.get(), queueId); + if (!GuestMemory::Accessible(reinterpret_cast(range->control), 8)) return false; + std::array control; + GuestMemory::Read(range->control, std::as_writable_bytes(std::span(control)), 4); + const auto count = range->UniformBytes(control[0], control[1]); + if (!count) return false; + if (*count == 0) return true; + if (!GuestMemory::Accessible(reinterpret_cast(range->source), 4)) return false; + GuestMemory::Read(range->source, std::as_writable_bytes(std::span(pattern).first(1)), 4); + std::fill(pattern.begin() + 1, pattern.end(), pattern[0]); + base = range->destination; + bytes = *count; + } else { + const auto numRecords = userData[2]; + std::array groups{packet[1], packet[2], packet[3]}; + if ((packet[4] & 0x20u) != 0) { + for (std::uint32_t axis = 0; axis < 3; ++axis) { + const auto threads = std::max(readRegister(queue.shader, 0x207 + axis) & 0xffffu, 1u); + groups[axis] = (groups[axis] + threads - 1) / threads; + } } + if (groups[1] != 1 || groups[2] != 1) return false; + const auto records = std::min(static_cast(groups[0]) * 64u, numRecords); + base = userData[0] | (static_cast(userData[1] & 0xffffu) << 32u); + bytes = static_cast(records * 16u); + pattern = {userData[4], userData[5], userData[6], userData[7]}; } - if (groups[1] != 1 || groups[2] != 1) return false; - const auto records = std::min(static_cast(groups[0]) * 64u, numRecords); - const auto base = userData[0] | (static_cast(userData[1] & 0xffffu) << 32u); - const auto bytes = static_cast(records * 16u); - const std::array pattern{userData[4], userData[5], userData[6], userData[7]}; static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr; static std::atomic fills{0}, filledBytes{0}, cpuFills{0}; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/PatternBufferFill.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/PatternBufferFill.cpp new file mode 100644 index 000000000..9e0872423 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/PatternBufferFill.cpp @@ -0,0 +1,208 @@ +#include "prx/libSceAgcDriver/Execution/include/Driver.hpp" +#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp" +#include "prx/libSceAgcDriver/Execution/include/Driver/Memory/BufferFill.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using namespace AgcDriver::DriverDetail; +alignas(256) constexpr auto Code = PatternFillKernel; +alignas(256) std::array Pattern{0, 0x12345678u, 0xdeadbeefu, 0x7fc12345u}; +alignas(256) std::array Control{}; +alignas(256) std::array Output{}; + +std::array UserData(void* output, std::uint32_t records) { + std::array data{}; + const auto descriptor = [&](std::size_t first, const void* pointer, std::uint32_t stride, std::uint32_t count, std::uint32_t format) { + const auto address = reinterpret_cast(pointer); + data[first] = static_cast(address); + data[first + 1] = static_cast(address >> 32u) | (stride << 16u); + data[first + 2] = count; + data[first + 3] = format; + }; + descriptor(0, Pattern.data(), 4, Pattern.size(), 0x14004u); + descriptor(4, output, 4, records, 0x14004u); + descriptor(8, Control.data(), 16, 1, 0x4dfacu); + return data; +} + +void RangeTests() { + const ShaderRecompiler::ShaderComputeStageInfo compute{{64, 1, 1}, 0, {true, false, false}, false, 1}; + const auto data = UserData(Output.data(), 256); + Require(MatchesPatternFillKernel(Code, data, compute), "pattern fill kernel was not recognized"); + for (std::size_t word = 0; word < Code.size(); ++word) { + auto altered = Code; + altered[word] ^= 1; + Require(!MatchesPatternFillKernel(altered, data, compute), "a different shader matched the fill kernel"); + } + Require(!MatchesPatternFillKernel(std::span(Code).first(17), data, compute), "truncated fill shader matched"); + for (const auto word : {1u, 3u, 5u, 7u, 9u, 11u}) { + auto altered = data; + altered[word] ^= word % 4 == 1 ? 0x80000000u : 0x1000u; + Require(!MatchesPatternFillKernel(Code, altered, compute), "unsupported fill descriptor matched"); + } + auto disabled = compute; + disabled.groupIdEnable[0] = false; + Require(!MatchesPatternFillKernel(Code, data, disabled), "fill with no group id matched"); + disabled = compute; + disabled.numThreads[0] = 32; + Require(!MatchesPatternFillKernel(Code, data, disabled), "different fill group size matched"); + std::array packet{0xc0031500u, 2, 1, 1, 0x41}; + auto range = DecodePatternFillRange(data, packet); + Require(range && range->UniformBytes(256, 0) == 512, "fill exceeded launched threads"); + Require(range->UniformBytes(80, 0) == 320, "fill exceeded its control count"); + Require(!range->UniformBytes(80, 1), "nonuniform pattern matched a uniform fill"); + Require(!range->UniformBytes(79, 0), "partial 16-byte fill matched"); + Require(range->UniformBytes(0, 0) == 0, "empty control count was not empty"); + packet[1] = 68; + packet[4] |= 0x20; + range = DecodePatternFillRange(data, packet); + Require(range && range->UniformBytes(256, 0) == 272, "partial workgroup was rounded up"); + range->records = 64; + Require(range->UniformBytes(256, 0) == 256, "fill exceeded output descriptor"); + range->source = range->destination + 16; + Require(!range->UniformBytes(256, 0), "aliased pattern was accepted"); + range->source = 0x1000; + range->control = range->destination - 4; + Require(!range->UniformBytes(256, 0), "aliased control was accepted"); + packet[1] = 0; + Require(DecodePatternFillRange(data, packet)->invocations == 0, "empty dispatch gained invocations"); + packet[1] = 0x4000001u; + packet[4] = 0x41; + Require(!DecodePatternFillRange(data, packet), "wrapped thread indices were accepted"); + packet[1] = 1; + packet[2] = 2; + Require(!DecodePatternFillRange(data, packet), "multidimensional fill was accepted"); +} + +std::vector Commands(std::span data, std::uint32_t threads, std::uint32_t wave, bool partial, bool indirect) { + std::vector words; + const auto registers = [&](std::uint32_t first, std::span values) { + words.push_back(0xc0007600u | (static_cast(values.size()) << 16u)); + words.push_back(first); + words.insert(words.end(), values.begin(), values.end()); + }; + const std::array shape{64, 1, 1}; + registers(0x207, shape); + const auto address = reinterpret_cast(Code.data()); + const std::array program{static_cast(address >> 8u), static_cast(address >> 40u)}; + registers(0x20c, program); + const std::array resource{(12u << 1u) | (1u << 7u)}; + registers(0x213, resource); + registers(0x240, data); + const auto initiator = 0x41u | (wave == 32 ? 0x8000u : 0u) | (partial ? 0x20u : 0u); + if (indirect) { + static std::array arguments; + arguments = {threads / 64, 1, 1}; + const auto pointer = reinterpret_cast(arguments.data()); + words.insert(words.end(), {0xc0021600u, static_cast(pointer), static_cast(pointer >> 32u), initiator}); + } else { + words.insert(words.end(), {0xc0031500u, partial ? threads : threads / 64, 1, 1, initiator}); + } + return words; +} + +void Submit(const std::vector& words, void* output = Output.data(), std::size_t bytes = sizeof(Output)) { + Packet packet{const_cast(words.data()), static_cast(words.size()), 0, {}}; + AgcDriver::Submit(&packet, 0); + AgcDriverWaitIdle_nid_postfix(); + AgcDriver::GuestMemory::FlushGpuWrites(reinterpret_cast(output), bytes); +} + +void ExecuteTests() { + struct Case { std::uint32_t pattern, count, records, threads, mask; bool partial, indirect; }; + constexpr std::array cases{{ + {0, 256, 256, 256, 0, false, false}, + {0xffffffffu, 256, 256, 128, 0, false, false}, + {0x7fc12345u, 80, 256, 256, 0, false, false}, + {0x80000000u, 256, 64, 256, 0, false, false}, + {0xdeadbeefu, 256, 256, 68, 0, true, false}, + {0x3f800000u, 0, 256, 64, 0, false, false}, + {0x80808080u, 256, 256, 0, 0, false, false}, + {0xffffffffu, 1, 256, 64, 0, false, false}, + {0, 256, 7, 64, 0, false, false}, + {0x01010101u, 128, 256, 128, 0, false, true}, + {0, 128, 256, 128, 3, false, false} + }}; + for (const auto wave : {32u, 64u}) { + for (const auto& item : cases) { + Output.fill(0xcafebabeu); + Pattern[0] = item.pattern; + Control = {item.count, item.mask, 0, 0}; + const auto words = Commands(UserData(Output.data(), item.records), item.threads, wave, item.partial, item.indirect); + Submit(words); + const auto count = std::min({item.count, item.records, item.threads}); + for (std::size_t i = 0; i < Output.size(); ++i) { + const auto expected = i < count ? Pattern[i & item.mask] : 0xcafebabeu; + Require(Output[i] == expected, "pattern fill mismatch at " + std::to_string(i) + " wave " + std::to_string(wave) + " count " + std::to_string(item.count) + " got " + std::to_string(Output[i]) + " expected " + std::to_string(expected)); + } + } + } + + Output.fill(0xcafebabeu); + Pattern[0] = 0x40404040u; + const auto firstPattern = Pattern; + Control = {256, 3, 0, 0}; + auto words = Commands(UserData(Output.data(), 256), 256, 32, false, false); + const auto label = [&](std::uint32_t* target, std::uint32_t value) { + const auto address = reinterpret_cast(target); + words.insert(words.end(), {0xc0064900u, 0x514u, (1u << 29u) | (2u << 24u), static_cast(address), static_cast(address >> 32u), value, 0, 0}); + }; + label(&Control[0], 128); + label(&Control[1], 0); + label(&Pattern[0], 0xababababu); + const auto next = Commands(UserData(Output.data(), 256), 256, 32, false, false); + words.insert(words.end(), next.begin(), next.end()); + Submit(words); + for (std::size_t i = 0; i < Output.size(); ++i) { + const auto expected = i < 128 ? 0xababababu : i < 256 ? firstPattern[i & 3u] : 0xcafebabeu; + Require(Output[i] == expected, "fill did not observe preceding GPU results and labels at " + std::to_string(i)); + } +} + +void Benchmark() { + constexpr std::size_t bytes = 64u << 20u; + std::vector storage(bytes / 4 + 4); + auto* output = reinterpret_cast((reinterpret_cast(storage.data()) + 15u) & ~std::uintptr_t{15}); + Pattern[0] = 0xffffffffu; + Control = {bytes / 4, 0, 0, 0}; + const auto words = Commands(UserData(output, bytes / 4), bytes / 4, 32, false, false); + Submit(words, output, bytes); + std::array times; + for (auto& ms : times) { + const auto start = std::chrono::steady_clock::now(); + Submit(words, output, bytes); + ms = std::chrono::duration(std::chrono::steady_clock::now() - start).count(); + Require(std::all_of(output, output + bytes / 4, [](auto value) { return value == 0xffffffffu; }), "benchmark fill was incomplete"); + } + std::sort(times.begin(), times.end()); + std::printf("64 MiB fill median %.3f ms, min %.3f, max %.3f\n", times[4], times.front(), times.back()); +} + +} + +int main(int argc, char** argv) { + try { + RangeTests(); + { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + } + if (argc == 2 && std::string_view(argv[1]) == "--benchmark") Benchmark(); + else ExecuteTests(); + AgcDriverShutdown_nid_postfix(); + std::puts("pattern buffer fill tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + AgcDriverShutdown_nid_postfix(); + return 1; + } +}