Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/libs/prx/libSceAgcDriver/Execution/include/Pm4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ bool AccessesMemory(std::uint32_t header);
// Whether an ACQUIRE_MEM packet asks only for GPU cache actions (no CPU-visible memory
// synchronization): such a packet needs a pipeline barrier, not a device drain.
bool UsesGpuCacheBarrier(std::span<const std::uint32_t> packet);
bool ReleaseWritesBackL2(std::span<const std::uint32_t> packet);
bool IsTagMarker(std::span<const std::uint32_t> packet);
inline bool Predicated(std::uint32_t header) { return !FillerPacket(header) && (header & 1u) != 0; }
bool PredicationPasses(const QueueState& queue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void Driver::resolveGroupAhead(const Submission& submission, const QueueState& l
const auto count = Pm4::PacketWords(header);
if (limit + count > commands.size()) break;
const auto opcode = (header >> 8u) & 0xffu;
if (header == FlipPacketHeader || header == RenderingWaitPacketHeader || opcode == 0x22 || opcode == 0x3f || opcode == 0x3c || opcode == 0x93) break;
if (header == FlipPacketHeader || header == RenderingWaitPacketHeader || opcode == 0x22 || opcode == 0x3f || opcode == 0x3c || opcode == 0x93 || Pm4::ReleaseWritesBackL2(std::span(commands).subspan(limit, count))) break;
if (opcode == 0x15 || opcode == 0x16) hasFurtherDispatch = true;
limit += count;
}
Expand Down Expand Up @@ -259,7 +259,7 @@ void Driver::resolveGroupAhead(const Submission& submission, const QueueState& l
const auto packet = std::span(commands).subspan(cursor, count);
const auto opcode = (header >> 8u) & 0xffu;
const auto next = cursor + count;
if (header == FlipPacketHeader || header == RenderingWaitPacketHeader || opcode == 0x22 || (Pm4::Predicated(header) && scratch.predication.operation != 0)) break;
if (header == FlipPacketHeader || header == RenderingWaitPacketHeader || opcode == 0x22 || Pm4::ReleaseWritesBackL2(packet) || (Pm4::Predicated(header) && scratch.predication.operation != 0)) break;
if (opcode == 0x3c || opcode == 0x93) {
if (!Pm4::WaitSatisfiedUnchecked(packet) && !WrittenByRun(packet, labels)) break;
} else if (opcode == 0x15 || opcode == 0x16) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ bool Driver::preparePacketMemory(const Submission& submission, QueueState& queue
wroteOnGpu = false;

bool orderedAlready = false;
const bool releaseWriteBack = Pm4::ReleaseWritesBackL2(packet);
if (releaseWriteBack) {
GuestMemory::TagGpuLockSite(GuestMemory::GpuLockSite::Flush);
std::lock_guard gpuLock(GuestMemory::GpuMutex());
const auto localDevice = device.Load();
recordDeferredLabels(localDevice.get(), submission.queue);
if (localDevice != nullptr) Graphics::StorageTexture::StoreBeforeRelease(localDevice->Device());
}

endOfPipeInterrupt = interrupt.has_value();
interruptDeferred = false;
Expand All @@ -73,7 +81,7 @@ bool Driver::preparePacketMemory(const Submission& submission, QueueState& queue
std::lock_guard gpuLock(GuestMemory::GpuMutex());
const auto localDevice = device.Load();
recordDeferredLabels(localDevice.get(), submission.queue);
bool workOpen = Graphics::Recorder::RecordedWorkSinceSubmit() != 0;
bool workOpen = releaseWriteBack || Graphics::Recorder::RecordedWorkSinceSubmit() != 0;
int reason = localDevice != nullptr ? 0 : 1;
if (label.has_value()) {
const auto bytes = label->Bytes();
Expand Down Expand Up @@ -114,7 +122,7 @@ bool Driver::preparePacketMemory(const Submission& submission, QueueState& queue
if (!drainAll && !endOfPipeInterrupt && (opcode == 0x49 || opcode == 0x37)) {
if (const auto label = Pm4::DecodeLabelWrite(packet)) {
const auto bytes = label->Bytes();
if (DeferLabels() && bytes.size() <= DeferredLabel::Capacity && bytes.size() % 4 == 0 && label->address % 4 == 0) {
if (!releaseWriteBack && DeferLabels() && bytes.size() <= DeferredLabel::Capacity && bytes.size() % 4 == 0 && label->address % 4 == 0) {

auto& deferred = deferredLabels();
if (deferred.labels.empty()) deferred.since = std::chrono::steady_clock::now();
Expand Down Expand Up @@ -484,7 +492,7 @@ void Driver::commitStore(std::uint32_t queue, std::uint64_t address, std::span<c

bool Driver::labelPacketQueues(std::span<const std::uint32_t> packet, std::uint32_t opcode) {
static const bool ordered = std::getenv("APS5_PIPELINE_DRAIN_LABELS") == nullptr;
if (!ordered || !DeferLabels() || (opcode != 0x49 && opcode != 0x37) || !deferredLabels().labels.empty()) return false;
if (!ordered || !DeferLabels() || (opcode != 0x49 && opcode != 0x37) || !deferredLabels().labels.empty() || Pm4::ReleaseWritesBackL2(packet)) return false;
if (Pm4::DecodeLabelWrite(packet).has_value()) return true;
return opcode == 0x49 && ((packet[2] >> 29u) == 0 || (packet[3] | (static_cast<std::uint64_t>(packet[4]) << 32u)) == 0);
}
Expand Down
12 changes: 12 additions & 0 deletions core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,14 @@ void Validate(std::span<const std::uint32_t> packet, std::uint32_t queue) {
const auto interruptSelect = (packet[2] >> 24u) & 7u;
require(interruptSelect == 0 || interruptSelect == 3, "an interrupting GCN RELEASE_MEM has no interrupt context dword");
}
if (ReleaseWritesBackL2(packet)) {
require((packet[1] & 0x00cf0000u) == 0, "RELEASE_MEM GL2 write-back scope, discard or sequence is not implemented");
const auto eventType = packet[1] & 0x3fu;
const auto eventIndex = (packet[1] >> 8u) & 0xfu;
const bool endOfPipe = eventIndex == 5u && (eventType == 0x04u || eventType == 0x14u || eventType == 0x28u || eventType == 0x29u || eventType == 0x2bu || eventType == 0x2du);
const bool computeDone = queue != 0 && eventIndex == 6u && eventType == 0x2fu;
require(endOfPipe || computeDone, "RELEASE_MEM GL2 write-back event/index is not implemented");
}
const auto dataSelect = packet[2] >> 29u;
require(dataSelect != 4 || ShaderRecompiler::GcnGuestShaders(), "RELEASE_MEM of the 64-bit system counter (DATA_SEL 4) is not implemented");
require(dataSelect <= 5, "reserved RELEASE_MEM DATA_SEL");
Expand Down Expand Up @@ -730,6 +738,10 @@ std::uint32_t ReadCondition(std::span<const std::uint32_t> packet) {
return value;
}

bool ReleaseWritesBackL2(std::span<const std::uint32_t> packet) {
return packet.size() >= 2 && ((packet[0] >> 8u) & 0xffu) == 0x49u && (packet[1] & 0x00200000u) != 0;
}

std::optional<LabelWrite> DecodeLabelWrite(std::span<const std::uint32_t> packet) {
const auto opcode = (packet[0] >> 8u) & 0xffu;
if (opcode == 0x49 && packet.size() >= 7) {
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ class StorageTexture : public std::enable_shared_from_this<StorageTexture> {
// overlapping pending image, as before).
static bool FlushPending(std::uint64_t address, std::size_t bytes, const StorageTexture* except = nullptr, const char* reason = "memory access", PublishScope scope = PublishScope::Whole, bool* published = nullptr, bool waited = false, VkDevice device = VK_NULL_HANDLE);
static void FlushAllPending(const char* reason, VkDevice device = VK_NULL_HANDLE);
static void StoreBeforeRelease(VkDevice device);
static bool FlushAllPendingUntil(const char* reason, const std::function<bool()>& stop, VkDevice device = VK_NULL_HANDLE);
static bool WaitForFlushing(std::uint64_t address, std::size_t bytes, const StorageTexture* except = nullptr);
static void SaveBeforeUnwatch(std::uint64_t address, std::size_t bytes);
Expand Down
29 changes: 29 additions & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5226,8 +5226,10 @@ bool StorageTexture::FlushPending(std::uint64_t address, std::size_t bytes, cons
return scope == PublishScope::None || !AnyShadowedOverlaps(address, bytes);
};
if (nothingToStore()) return false;
const bool requirePublication = reason != nullptr && std::strcmp(reason, "release") == 0;
if (AgcDriver::DriverDetail::DrawPipeline::HeldReads(address, bytes)) {
queuedReadHeld.fetch_add(1, std::memory_order_relaxed);
Require(!requirePublication, "RELEASE_MEM GL2 write-back with queued draw reads is not implemented");
return false;
}
std::unique_lock<GuestMemory::GpuMutexType> drawnHold;
Expand All @@ -5250,6 +5252,11 @@ bool StorageTexture::FlushPending(std::uint64_t address, std::size_t bytes, cons
auto& pending = Pending();
std::lock_guard lock(pending.mutex);
const auto* exempt = Refreshing::Get();
if (requirePublication) {
Require(std::none_of(pending.flushing.begin(), pending.flushing.end(), [&](const StorageTexture* texture) {
return texture != except && texture != exempt && (device == VK_NULL_HANDLE || texture->context.device == device) && texture->overlaps(address, bytes);
}), "RELEASE_MEM GL2 write-back during another image publication is not implemented");
}
for (auto it = pending.textures.MayOverlap(address, bytes) ? pending.textures.begin() : pending.textures.end(); it != pending.textures.end();) {
auto* texture = *it;
if (texture != except && texture != exempt && (device == VK_NULL_HANDLE || texture->context.device == device) && texture->overlaps(address, bytes)) {
Expand Down Expand Up @@ -5757,6 +5764,28 @@ void StorageTexture::FlushAllPending(const char* reason, VkDevice device) {
static_cast<void>(FlushPending(0, std::numeric_limits<std::size_t>::max(), nullptr, reason, PublishScope::Whole, nullptr, false, device));
}

void StorageTexture::StoreBeforeRelease(VkDevice device) {
GuestMemory::AssertGpuLockHeld("StorageTexture::StoreBeforeRelease");
constexpr auto bytes = std::numeric_limits<std::size_t>::max();
bool imagesPending = false;
{
auto& pending = Pending();
std::lock_guard lock(pending.mutex);
const auto sameDevice = [device](const StorageTexture* texture) { return device == VK_NULL_HANDLE || texture->context.device == device; };
Require(std::none_of(pending.flushing.begin(), pending.flushing.end(), sameDevice), "RELEASE_MEM GL2 write-back during another image publication is not implemented");
imagesPending = std::any_of(pending.textures.begin(), pending.textures.end(), sameDevice);
}
if (!imagesPending && !DepthPlanesStale() && !AnyShadowedOverlaps(0, bytes)) return;
Require(!AgcDriver::DriverDetail::DrawPipeline::HeldReads(0, bytes), "RELEASE_MEM GL2 write-back with queued draw reads is not implemented");
static const bool gpuOrdered = std::getenv("APS5_NO_FLUSH_GPU_ORDERED") == nullptr;
struct OrderedScope {
bool previous;
explicit OrderedScope(bool on) : previous(GpuOrderedFlush()) { GpuOrderedFlush() = previous || on; }
~OrderedScope() { GpuOrderedFlush() = previous; }
} ordered{gpuOrdered};
static_cast<void>(FlushPending(0, bytes, nullptr, "release", PublishScope::Whole, nullptr, false, device));
}

bool StorageTexture::FlushAllPendingUntil(const char* reason, const std::function<bool()>& stop, VkDevice device) {
std::optional<PublishOnCpuScope> onCpu;
if (reason != nullptr && std::strcmp(reason, "idle") == 0) onCpu.emplace();
Expand Down
42 changes: 42 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/Pm4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,47 @@ void testEventWrite() {
}
}

void testReleaseMemWriteback() {
const auto release = [](std::uint32_t control) { return makePacket(0x49, {control, 1u << 29u, 0x1000, 0, 1, 0, 0}); };
for (const auto control : {0x00200514u, 0x00300514u, 0x0030c514u, 0x0630c528u}) {
const auto packet = release(control);
check(AgcDriver::Pm4::ReleaseWritesBackL2(packet), "RELEASE_MEM lost an explicit GL2 write-back");
for (const auto queue : {0u, 0x20u}) AgcDriver::Pm4::Validate(packet, queue);
}
for (const auto control : {0x514u, 0x528u, 0x0000c514u, 0x00100514u}) {
const auto packet = release(control);
AgcDriver::Pm4::Validate(packet, 0);
check(!AgcDriver::Pm4::ReleaseWritesBackL2(packet), "RELEASE_MEM without GL2 write-back requested publication");
}
const auto computeDone = release(0x0020062fu);
AgcDriver::Pm4::Validate(computeDone, 0x20);
check(AgcDriver::Pm4::ReleaseWritesBackL2(computeDone), "a compute CS_DONE release lost GL2 write-back");
for (const auto event : {0x04u, 0x14u, 0x28u, 0x29u, 0x2bu, 0x2du}) {
for (const auto queue : {0u, 0x20u}) AgcDriver::Pm4::Validate(release(0x00200500u | event), queue);
}
expectFailure([&] { AgcDriver::Pm4::Validate(computeDone, 0); }, "RELEASE_MEM GL2 write-back event/index");
for (const auto control : {0x0020052fu, 0x00200630u, 0x00200414u, 0x00200614u, 0x00200510u, 0x00200014u}) {
for (const auto queue : {0u, 0x20u}) {
expectFailure([&] { AgcDriver::Pm4::Validate(release(control), queue); }, "RELEASE_MEM GL2 write-back event/index");
}
}
for (const auto control : {0u, 2u << 24u, 3u << 24u, 4u << 24u}) {
const auto packet = makePacket(0x49, {0x00300514, control, 0, 0, 0, 0, 0});
AgcDriver::Pm4::Validate(packet, 0);
check(AgcDriver::Pm4::ReleaseWritesBackL2(packet), "a release without label data lost GL2 write-back");
}
for (const auto flags : {0x10000u, 0x20000u, 0x40000u, 0x60000u, 0x80000u, 0x400000u, 0x800000u, 0xc00000u}) {
const auto packet = release(0x00200514u | flags);
for (const auto queue : {0u, 0x20u}) {
expectFailure([&] { AgcDriver::Pm4::Validate(packet, queue); }, "RELEASE_MEM GL2 write-back scope");
}
}
check(!AgcDriver::Pm4::ReleaseWritesBackL2({}), "an empty packet requested GL2 write-back");
const auto packet = release(0x00200514u);
check(!AgcDriver::Pm4::ReleaseWritesBackL2(std::span<const std::uint32_t>(packet).first(1)), "a header without release control requested GL2 write-back");
check(!AgcDriver::Pm4::ReleaseWritesBackL2(makePacket(0x37, {0x00200514, 0x1000, 0, 1})), "WRITE_DATA was classified as a GL2 write-back release");
}

void testAcquireMem() {
const auto captured = makePacket(0x58, {0x02007fc0, 0, 0, 0, 0, 10, 0x200});
AgcDriver::Pm4::Validate(captured, 0);
Expand Down Expand Up @@ -2868,6 +2909,7 @@ int main(int argc, char** argv) {
testGcnReleaseMemSystemCounter();
testGcnCalledBufferPacketForms();
testAcquireMem();
testReleaseMemWriteback();
testConditionalWrite();
testConditionalWriteSubmission();
testConditionalWriteRegisters();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
target_link_libraries(agc_driver_storage_completion_reuse_tests PRIVATE libkernel)

if(BUILD_TESTING)
set_tests_properties(agc_driver_storage_completion_reuse PROPERTIES TIMEOUT 30)
add_test(NAME agc_driver_storage_completion_reuse_writeback COMMAND agc_driver_storage_completion_reuse_tests medium compared writeback)
set_tests_properties(agc_driver_storage_completion_reuse_writeback PROPERTIES
ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" TIMEOUT 30)
if(WIN32)
set_tests_properties(agc_driver_storage_completion_reuse_writeback PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
foreach(reuseSize IN ITEMS small medium large)
foreach(reuseMemory IN ITEMS compared watched)
foreach(reuseImport IN ITEMS allowed disabled)
if(reuseSize STREQUAL "medium" AND reuseMemory STREQUAL "compared" AND reuseImport STREQUAL "allowed")
continue()
endif()
set(reuseTest "agc_driver_storage_completion_reuse_${reuseSize}_${reuseMemory}")
set(reuseEnvironment "ANYPS5_NO_SHADER_CACHE=1")
if(reuseImport STREQUAL "disabled")
string(APPEND reuseTest "_no_import")
list(APPEND reuseEnvironment "APS5_NO_HOST_IMPORT=1")
endif()
add_test(NAME ${reuseTest} COMMAND agc_driver_storage_completion_reuse_tests ${reuseSize} ${reuseMemory})
set_tests_properties(${reuseTest} PROPERTIES
ENVIRONMENT "${reuseEnvironment}" SKIP_RETURN_CODE 77
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" TIMEOUT 30)
if(WIN32)
set_tests_properties(${reuseTest} PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
endforeach()
endforeach()
endforeach()
foreach(reuseRoute IN ITEMS label64 timestamp cache-only interrupt conditional no-interrupt compute)
string(REPLACE "-" "_" reuseSuffix "${reuseRoute}")
set(reuseTest "agc_driver_storage_completion_reuse_${reuseSuffix}")
add_test(NAME ${reuseTest} COMMAND agc_driver_storage_completion_reuse_tests medium compared explicit ${reuseRoute})
set_tests_properties(${reuseTest} PROPERTIES
ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" TIMEOUT 30)
if(WIN32)
set_tests_properties(${reuseTest} PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
endforeach()
foreach(reuseExecution IN ITEMS deferred immediate dispatch_items)
set(reuseTest "agc_driver_storage_completion_reuse_${reuseExecution}")
set(reuseEnvironment "ANYPS5_NO_SHADER_CACHE=1")
set(reuseRoute "no-interrupt")
if(reuseExecution STREQUAL "dispatch_items")
set(reuseRoute "label32")
list(APPEND reuseEnvironment "APS5_DRAW_PIPELINE=1" "APS5_PIPELINED_DRAWS=8" "APS5_PIPELINE_DISPATCH=1")
else()
list(APPEND reuseEnvironment "APS5_PIPELINED_DRAWS=0")
if(reuseExecution STREQUAL "immediate")
list(APPEND reuseEnvironment "APS5_LABEL_LOCK_EACH=1")
endif()
endif()
add_test(NAME ${reuseTest} COMMAND agc_driver_storage_completion_reuse_tests medium compared explicit ${reuseRoute})
set_tests_properties(${reuseTest} PROPERTIES
ENVIRONMENT "${reuseEnvironment}" SKIP_RETURN_CODE 77
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" TIMEOUT 30)
if(WIN32)
set_tests_properties(${reuseTest} PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
endforeach()
add_test(NAME agc_driver_storage_completion_reuse_consumer_ahead COMMAND agc_driver_storage_completion_reuse_tests medium compared explicit consumer)
set_tests_properties(agc_driver_storage_completion_reuse_consumer_ahead PROPERTIES
ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_GROUP_CAPTURE_ALL_QUEUES=1;APS5_PIPELINED_DRAWS=0" SKIP_RETURN_CODE 77
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" TIMEOUT 30)
if(WIN32)
set_tests_properties(agc_driver_storage_completion_reuse_consumer_ahead PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
endif()
Loading
Loading