mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(libSceAgcDriver): back the global data share with driver-owned guest memory
Shader GDS accesses bind the same 64 KiB guest range that DMA_DATA packets read and write, so both go through the existing CPU/GPU coherence tracking. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -464,6 +464,7 @@ target_include_directories(agc_driver_graphics_tests PRIVATE
|
||||
target_link_libraries(agc_driver_graphics_tests PRIVATE
|
||||
shader_recompiler_agc_driver
|
||||
libc
|
||||
libkernel
|
||||
)
|
||||
|
||||
set_target_properties(agc_driver_graphics_tests PROPERTIES CXX_EXTENSIONS OFF)
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
|
||||
namespace AgcDriver::Pm4 {
|
||||
|
||||
constexpr std::size_t GdsBytes = 0x10000;
|
||||
std::uint64_t GdsAddress();
|
||||
|
||||
struct DrawParameters {
|
||||
std::uint64_t indexAddress;
|
||||
std::uint32_t indexCount;
|
||||
|
||||
@@ -10,7 +10,20 @@
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
extern "C" int APS5_VABI sceKernelMapFlexibleMemory(void** addr_in_out, size_t len, int prot, int flags);
|
||||
|
||||
namespace AgcDriver::Pm4 {
|
||||
|
||||
std::uint64_t GdsAddress() {
|
||||
static const std::uint64_t address = [] {
|
||||
constexpr int CpuGpuReadWrite = 0x33;
|
||||
void* mapped = nullptr;
|
||||
if (sceKernelMapFlexibleMemory(&mapped, GdsBytes, CpuGpuReadWrite, 0) != 0 || mapped == nullptr) throw std::runtime_error("cannot allocate the global data share");
|
||||
return reinterpret_cast<std::uint64_t>(mapped);
|
||||
}();
|
||||
return address;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
std::string ToHex(std::uint32_t value) {
|
||||
@@ -99,17 +112,6 @@ std::vector<std::byte> copySource(std::uint64_t source, std::size_t bytes, bool
|
||||
}
|
||||
|
||||
constexpr std::uint32_t DmaSelectGds = 1;
|
||||
constexpr std::size_t GdsBytes = 0x10000;
|
||||
|
||||
struct GdsStorage {
|
||||
std::mutex mutex;
|
||||
std::array<std::byte, GdsBytes> bytes{};
|
||||
};
|
||||
|
||||
GdsStorage& Gds() {
|
||||
static GdsStorage storage;
|
||||
return storage;
|
||||
}
|
||||
|
||||
bool gdsRange(std::uint64_t offset, std::size_t bytes) {
|
||||
return offset <= GdsBytes && bytes <= GdsBytes - offset;
|
||||
@@ -118,9 +120,7 @@ bool gdsRange(std::uint64_t offset, std::size_t bytes) {
|
||||
std::vector<std::byte> dmaSourceBytes(std::span<const std::uint32_t> packet) {
|
||||
const std::size_t bytes = packet[6] & 0x3ffffffu;
|
||||
if (dmaSource(packet) != DmaSelectGds) return copySource(address(packet[2], packet[3]), bytes, dmaSource(packet) == 2);
|
||||
auto& gds = Gds();
|
||||
std::lock_guard lock(gds.mutex);
|
||||
return {gds.bytes.begin() + packet[2], gds.bytes.begin() + packet[2] + bytes};
|
||||
return copySource(GdsAddress() + packet[2], bytes, false);
|
||||
}
|
||||
|
||||
void copyMemory(std::uint64_t source, std::uint64_t destination, std::size_t bytes, bool immediate) {
|
||||
@@ -842,13 +842,7 @@ void Execute(std::span<const std::uint32_t> packet, QueueState& queue) {
|
||||
const std::size_t bytes = packet[6] & 0x3ffffffu;
|
||||
if (bytes == 0) return;
|
||||
auto data = dmaSourceBytes(packet);
|
||||
if (dmaDestination(packet) == DmaSelectGds) {
|
||||
auto& gds = Gds();
|
||||
std::lock_guard lock(gds.mutex);
|
||||
std::copy(data.begin(), data.end(), gds.bytes.begin() + packet[4]);
|
||||
return;
|
||||
}
|
||||
const auto destination = address(packet[4], packet[5]);
|
||||
const auto destination = dmaDestination(packet) == DmaSelectGds ? GdsAddress() + packet[4] : address(packet[4], packet[5]);
|
||||
GuestMemory::CheckRange(reinterpret_cast<void*>(destination), bytes, 1, true);
|
||||
GuestMemory::Write(destination, data);
|
||||
return;
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
|
||||
#include "prx/libSceAgcDriver/Execution/include/Pm4.hpp"
|
||||
#include "prx/libSceAgcDriver/Graphics/include/TextureTiling.hpp"
|
||||
#include "prx/libSceAgcDriver/Graphics/include/DccMetadata.hpp"
|
||||
#include "prx/libSceAgcDriver/Graphics/include/TextureFormat.hpp"
|
||||
@@ -906,7 +907,7 @@ void ShaderResources::buildPrepare(std::span<const CompiledShader> shaders, cons
|
||||
Require(binding.descriptorSet == 0, "unexpected descriptor set: every shader resource must use descriptor set zero");
|
||||
Require(occupied.insert(binding.binding).second, "duplicate shader binding");
|
||||
const bool addressRole = binding.role == ShaderRecompiler::DescriptorRole::BdaPagetable || binding.role == ShaderRecompiler::DescriptorRole::FaultBuffer;
|
||||
const bool bufferRole = addressRole || binding.role == ShaderRecompiler::DescriptorRole::GuestBuffers || binding.role == ShaderRecompiler::DescriptorRole::ShaderData || binding.role == ShaderRecompiler::DescriptorRole::FlattenedSrt;
|
||||
const bool bufferRole = addressRole || binding.role == ShaderRecompiler::DescriptorRole::GuestBuffers || binding.role == ShaderRecompiler::DescriptorRole::ShaderData || binding.role == ShaderRecompiler::DescriptorRole::FlattenedSrt || binding.role == ShaderRecompiler::DescriptorRole::Gds;
|
||||
const bool imageRole = binding.role == ShaderRecompiler::DescriptorRole::GuestImages || binding.role == ShaderRecompiler::DescriptorRole::GuestSamplers;
|
||||
if (imageRole) {
|
||||
if (binding.kind == ShaderRecompiler::DescriptorKind::SampledImage || binding.kind == ShaderRecompiler::DescriptorKind::StorageImage) stageResources += binding.count;
|
||||
@@ -945,6 +946,12 @@ void ShaderResources::buildPrepare(std::span<const CompiledShader> shaders, cons
|
||||
} else if (addressRole) {
|
||||
item.allocations.push_back(allocations.size());
|
||||
allocations.push_back({0, 0, false, nullptr, binding.role});
|
||||
} else if (binding.role == ShaderRecompiler::DescriptorRole::Gds) {
|
||||
Require(binding.count == 1 && binding.guestDescriptor.empty(), "invalid GDS descriptor contract");
|
||||
const auto gds = Pm4::GdsAddress();
|
||||
guestMemory.AddWritable(gds, Pm4::GdsBytes, true);
|
||||
item.allocations.push_back(allocations.size());
|
||||
allocations.push_back({gds, Pm4::GdsBytes, true, nullptr, ShaderRecompiler::DescriptorRole::ShaderData, true});
|
||||
} else {
|
||||
Require(binding.count == 1, "shader data and flattened SRT descriptors must not be arrays");
|
||||
Require(!binding.guestDescriptor.empty(), "empty shader data descriptor");
|
||||
|
||||
@@ -453,7 +453,7 @@ Module Inspect(const CompiledShader& compiled, const State& state, const VkPhysi
|
||||
Require(binding->kind == ShaderRecompiler::DescriptorKind::StorageBuffer, "SPIR-V descriptor type disagrees with recompiler binding metadata");
|
||||
Require(!binding->readOnly, "read-only descriptor metadata is unsupported because the recompiler emits no NonWritable decoration");
|
||||
const bool array = binding->role == ShaderRecompiler::DescriptorRole::GuestBuffers;
|
||||
Require(array || (shader.bdaAbiVersion == ShaderRecompiler::BdaAbi::Version && (binding->role == ShaderRecompiler::DescriptorRole::BdaPagetable || binding->role == ShaderRecompiler::DescriptorRole::FaultBuffer)) || binding->role == ShaderRecompiler::DescriptorRole::ShaderData || binding->role == ShaderRecompiler::DescriptorRole::FlattenedSrt, "SPIR-V descriptor role is unsupported");
|
||||
Require(array || (shader.bdaAbiVersion == ShaderRecompiler::BdaAbi::Version && (binding->role == ShaderRecompiler::DescriptorRole::BdaPagetable || binding->role == ShaderRecompiler::DescriptorRole::FaultBuffer)) || binding->role == ShaderRecompiler::DescriptorRole::ShaderData || binding->role == ShaderRecompiler::DescriptorRole::FlattenedSrt || binding->role == ShaderRecompiler::DescriptorRole::Gds, "SPIR-V descriptor role is unsupported");
|
||||
auto blockId = typeId;
|
||||
if (array) {
|
||||
Require(type.size() == 4 && (type[0] & 0xffffu) == spv::OpTypeArray, "guest buffer descriptors must be declared as a descriptor array");
|
||||
|
||||
@@ -1281,7 +1281,7 @@ void resourceTests() {
|
||||
expectSingleFailure(changed([](auto& binding) { binding.role = Role::GuestImages; binding.kind = Kind::SampledImage; }), "guest texture descriptor must contain 8 dwords");
|
||||
expectSingleFailure(changed([](auto& binding) { binding.role = Role::GuestImages; binding.kind = Kind::StorageImage; }), "guest storage image descriptors must contain 8 dwords");
|
||||
expectSingleFailure(changed([](auto& binding) { binding.role = Role::GuestSamplers; binding.kind = Kind::Sampler; }), "shader sampler descriptors exceed per-stage limits");
|
||||
expectSingleFailure(changed([](auto& binding) { binding.role = Role::Gds; binding.guestDescriptor.clear(); }), "unsupported descriptor role Gds");
|
||||
expectSingleFailure(changed([](auto& binding) { binding.role = Role::Gds; }), "invalid GDS descriptor contract");
|
||||
expectSingleFailure(changed([](auto& binding) { binding.role = Role::BdaPagetable; binding.guestDescriptor.clear(); }), "BDA table and fault descriptors");
|
||||
expectSingleFailure(changed([](auto& binding) { binding.role = Role::FaultBuffer; binding.guestDescriptor.clear(); }), "BDA table and fault descriptors");
|
||||
expectSingleFailure(changed([](auto& binding) { binding.kind = Kind::UniformBuffer; }), "unsupported descriptor kind UniformBuffer");
|
||||
|
||||
Reference in New Issue
Block a user