Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
c69c8d9
fix(agc-driver): keep CPU stores made after a deferred DCC key store …
ee6aba54 Oct 14, 2026
fddbe7b
fix(agc-driver): keep a key store guarded behind the shadow publish t…
ee6aba54 Oct 14, 2026
878a189
fix(agc-driver): keep a key store guarded behind the key clears recor…
ee6aba54 Oct 14, 2026
960c01a
fix(agc-driver): leave the key blocks the CPU wrote after the record …
ee6aba54 Oct 15, 2026
ef11faf
fix(agc-driver): put the libs directory on PATH for agc_guarded_key_s…
ee6aba54 Oct 15, 2026
e91007b
test(agc): count a skipped agc_guarded_key_store as skipped
ee6aba54 Oct 17, 2026
408305a
docs(agc-driver): list the key store guard switches in the environmen…
ee6aba54 Oct 20, 2026
2094193
Merge branch 'fix/agc-changed-copy-back' into fix/agc-key-store-guard
ee6aba54 Oct 21, 2026
e9f8be6
fix(agc-driver): address a guarded key store's target from the start …
ee6aba54 Oct 21, 2026
d8c1459
docs(agc-driver): record that a guarded key store leaves whole CPU-wr…
ee6aba54 Oct 21, 2026
cebec8a
Merge main into fix/agc-key-store-guard
oneandonlydean Oct 24, 2026
49a2c11
test(agc-driver): expect CPU-written key blocks to be kept only when …
ee6aba54 Oct 25, 2026
ca51ffc
Merge fix/agc-changed-copy-back into fix/agc-key-store-guard
ee6aba54 Oct 25, 2026
5852a6f
Merge fix/agc-changed-copy-back into fix/agc-key-store-guard
ee6aba54 Oct 25, 2026
13d0941
test(agc-driver): count a guarded key store run without a case as ski…
ee6aba54 Oct 25, 2026
5118a91
Merge fix/agc-changed-copy-back into fix/agc-key-store-guard
ee6aba54 Oct 31, 2026
3962482
Merge branch 'fix/agc-changed-copy-back' into fix/agc-key-store-guard
ee6aba54 Oct 31, 2026
9a00b0c
Merge commit 'f6232fcfda70644495273f6b8dd88cf0603463d6' into HEAD
oneandonlydean Nov 3, 2026
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
14 changes: 14 additions & 0 deletions core/libs/prx/libSceAgcDriver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ add_custom_command(
VERBATIM
)

set(agcStagingCopyBackShaderSource ${CMAKE_CURRENT_SOURCE_DIR}/Graphics/shaders/StagingCopyBack.comp)
set(agcStagingCopyBackShaderSpv ${agcTextureDetileGeneratedDir}/StagingCopyBack.spv)
set(agcStagingCopyBackShaderHeader ${agcTextureDetileGeneratedDir}/StagingCopyBack_spv.h)
add_custom_command(
OUTPUT "${agcStagingCopyBackShaderHeader}"
COMMAND ${CMAKE_COMMAND} -E make_directory "${agcTextureDetileGeneratedDir}"
COMMAND "$<TARGET_FILE:glslang-standalone>" -V --target-env vulkan1.1 -o "${agcStagingCopyBackShaderSpv}" "${agcStagingCopyBackShaderSource}"
COMMAND ${CMAKE_COMMAND} -DINPUT=${agcStagingCopyBackShaderSpv} -DOUTPUT=${agcStagingCopyBackShaderHeader}
-DSYMBOL=STAGING_COPY_BACK_SPV -P "${CMAKE_CURRENT_SOURCE_DIR}/embed_spirv.cmake"
DEPENDS "${agcStagingCopyBackShaderSource}" glslang-standalone
VERBATIM
)

set(agcCmaskClearShaderSource ${CMAKE_CURRENT_SOURCE_DIR}/Graphics/shaders/CmaskClear.comp)
set(agcCmaskClearShaderSpv ${agcTextureDetileGeneratedDir}/CmaskClear.spv)
set(agcCmaskClearShaderHeader ${agcTextureDetileGeneratedDir}/CmaskClear_spv.h)
Expand Down Expand Up @@ -403,6 +416,7 @@ set(agcDriverSources
${agcMultisampleUploadHeader}
${agcCmaskClearShaderHeader}
${agcMeshArgumentsShaderHeader}
${agcStagingCopyBackShaderHeader}
${agcTextureDetileImageShaderHeader}
${agcTextureRepackShaderHeader}
${agcDispatchArgumentsShaderHeader}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void Write(std::uint64_t address, std::span<const std::byte> source, std::size_t
void WriteChanged(std::uint64_t address, std::span<const std::byte> current, std::span<const std::byte> original);
void WriteChanged(std::uint64_t address, std::span<const std::byte> current, std::span<const std::byte> original, std::uint32_t byteStoreLimit);
std::uint32_t ChangedByteStoreLimit();
void WriteChangedKeepingGuest(std::uint64_t address, std::span<const std::byte> current, std::span<const std::byte> original);

// Write tracking. The guest arena is reserved with page write watching, so CollectWrites learns which
// pages of a range the CPU wrote since they were last collected, stamps their 64 KiB blocks with a new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void Driver::flushBetweenPackets(std::uint32_t queue, std::uint32_t header, bool
lastReport = reportNow;
const auto stores = Graphics::Recorder::StoreCounts();
const auto failed = [](TrySite site) { return static_cast<unsigned long long>(triesFailed[site].load()); };
AgcDriver::ProfilePrint_nid_no_patch("[labels] batch flushes between packets: %llu at the next packet, %llu by deadline, %llu by size cap (%llu well overdue, waited for the mutex); %llu boundary reaps; record tries skipped (self-locking next) %llu; failed tries: label %llu, flush %llu, reap %llu, poll %llu, idle %llu, capture %llu, poll-record %llu; at the packet's own lock: %llu label groups (%.0f ms), %llu submits (%.0f ms incl. queue 0's reaps), left to it by a failed try: %llu groups, %llu submits; after a capture: %llu groups recorded by a try, %llu packets redone for a queued label over the capture, %llu for captured memory that changed before recording; %llu suspend points; stores: %llu in %llu runs (%llu joined, %llu replaced, %llu WAW barriers, %llu joins refused; %llu runs recorded at submit, %llu in place before a writer or reader); DCC key stores: %llu queued in %llu runs (%llu before a writer, %llu joined a queued range); queued labels: %llu noted (%llu over a recorded entry), %llu same-queue wait hits, %llu groups recorded from a poll loop, %llu from the flush hook (%llu hook accesses, %llu inside a completion skipped)\n", static_cast<unsigned long long>(packetFlushes.load()), static_cast<unsigned long long>(deadlineFlushes.load()), static_cast<unsigned long long>(capFlushes.load()), static_cast<unsigned long long>(overdueLocks.load()), static_cast<unsigned long long>(boundaryReaps.load()), static_cast<unsigned long long>(recordTriesSkipped.load()), failed(TryLabel), failed(TryFlush), failed(TryReap), failed(TryPoll), failed(TryIdle), failed(TryCapture), failed(TryPollRecord), static_cast<unsigned long long>(packetLockRecords.load()), packetLockRecordUs.load() / 1000.0, static_cast<unsigned long long>(packetLockSubmits.load()), packetLockSubmitUs.load() / 1000.0, static_cast<unsigned long long>(packetLockDeferred.load()), static_cast<unsigned long long>(packetSubmitDeferred.load()), static_cast<unsigned long long>(captureTryRecords.load()), static_cast<unsigned long long>(captureRetries.load()), static_cast<unsigned long long>(captureChangeRetries.load()), static_cast<unsigned long long>(suspendPoints.load()), static_cast<unsigned long long>(stores.stores), static_cast<unsigned long long>(stores.runs), static_cast<unsigned long long>(stores.joined), static_cast<unsigned long long>(stores.replaced), static_cast<unsigned long long>(stores.wawBarriers), static_cast<unsigned long long>(stores.joinsRefused), static_cast<unsigned long long>(stores.runsAtSubmit), static_cast<unsigned long long>(stores.runsForced), static_cast<unsigned long long>(stores.keyStores), static_cast<unsigned long long>(stores.keyStoreRuns), static_cast<unsigned long long>(stores.keyStoreRunsForWriter), static_cast<unsigned long long>(stores.keyStoresJoined), static_cast<unsigned long long>(stores.queuedNoted), static_cast<unsigned long long>(stores.queuedOverRecorded), static_cast<unsigned long long>(stores.queuedHits), static_cast<unsigned long long>(pollLabelRecords.load()), static_cast<unsigned long long>(hookLabelRecords.load()), static_cast<unsigned long long>(stores.queuedHookRecords), static_cast<unsigned long long>(stores.queuedHookInCompletion));
AgcDriver::ProfilePrint_nid_no_patch("[labels] batch flushes between packets: %llu at the next packet, %llu by deadline, %llu by size cap (%llu well overdue, waited for the mutex); %llu boundary reaps; record tries skipped (self-locking next) %llu; failed tries: label %llu, flush %llu, reap %llu, poll %llu, idle %llu, capture %llu, poll-record %llu; at the packet's own lock: %llu label groups (%.0f ms), %llu submits (%.0f ms incl. queue 0's reaps), left to it by a failed try: %llu groups, %llu submits; after a capture: %llu groups recorded by a try, %llu packets redone for a queued label over the capture, %llu for captured memory that changed before recording; %llu suspend points; stores: %llu in %llu runs (%llu joined, %llu replaced, %llu WAW barriers, %llu joins refused; %llu runs recorded at submit, %llu in place before a writer or reader); DCC key stores: %llu queued in %llu runs (%llu before a writer, %llu joined a queued range, %llu guarded, %llu key blocks left to a later CPU store); queued labels: %llu noted (%llu over a recorded entry), %llu same-queue wait hits, %llu groups recorded from a poll loop, %llu from the flush hook (%llu hook accesses, %llu inside a completion skipped)\n", static_cast<unsigned long long>(packetFlushes.load()), static_cast<unsigned long long>(deadlineFlushes.load()), static_cast<unsigned long long>(capFlushes.load()), static_cast<unsigned long long>(overdueLocks.load()), static_cast<unsigned long long>(boundaryReaps.load()), static_cast<unsigned long long>(recordTriesSkipped.load()), failed(TryLabel), failed(TryFlush), failed(TryReap), failed(TryPoll), failed(TryIdle), failed(TryCapture), failed(TryPollRecord), static_cast<unsigned long long>(packetLockRecords.load()), packetLockRecordUs.load() / 1000.0, static_cast<unsigned long long>(packetLockSubmits.load()), packetLockSubmitUs.load() / 1000.0, static_cast<unsigned long long>(packetLockDeferred.load()), static_cast<unsigned long long>(packetSubmitDeferred.load()), static_cast<unsigned long long>(captureTryRecords.load()), static_cast<unsigned long long>(captureRetries.load()), static_cast<unsigned long long>(captureChangeRetries.load()), static_cast<unsigned long long>(suspendPoints.load()), static_cast<unsigned long long>(stores.stores), static_cast<unsigned long long>(stores.runs), static_cast<unsigned long long>(stores.joined), static_cast<unsigned long long>(stores.replaced), static_cast<unsigned long long>(stores.wawBarriers), static_cast<unsigned long long>(stores.joinsRefused), static_cast<unsigned long long>(stores.runsAtSubmit), static_cast<unsigned long long>(stores.runsForced), static_cast<unsigned long long>(stores.keyStores), static_cast<unsigned long long>(stores.keyStoreRuns), static_cast<unsigned long long>(stores.keyStoreRunsForWriter), static_cast<unsigned long long>(stores.keyStoresJoined), static_cast<unsigned long long>(stores.keyStoresGuarded), static_cast<unsigned long long>(stores.keyBlocksLeftToCpu), static_cast<unsigned long long>(stores.queuedNoted), static_cast<unsigned long long>(stores.queuedOverRecorded), static_cast<unsigned long long>(stores.queuedHits), static_cast<unsigned long long>(pollLabelRecords.load()), static_cast<unsigned long long>(hookLabelRecords.load()), static_cast<unsigned long long>(stores.queuedHookRecords), static_cast<unsigned long long>(stores.queuedHookInCompletion));
}

}
16 changes: 13 additions & 3 deletions core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3024,7 +3024,7 @@ void WriteChanged(std::uint64_t address, std::span<const std::byte> current, std
WriteChanged(address, current, original, ChangedByteStoreLimit());
}

void WriteChanged(std::uint64_t address, std::span<const std::byte> current, std::span<const std::byte> original, std::uint32_t byteStoreLimit) {
static void writeChanged(std::uint64_t address, std::span<const std::byte> current, std::span<const std::byte> original, std::uint32_t byteStoreLimit, bool keepGuest) {
require(current.size() == original.size(), "write-back snapshot sizes differ");
BreakProofs();
if (current.empty()) return;
Expand All @@ -3044,7 +3044,9 @@ void WriteChanged(std::uint64_t address, std::span<const std::byte> current, std
std::size_t at = 0;
for (; at + 16 <= size; at += 16) {
const auto bytes = _mm_loadu_si128(reinterpret_cast<const __m128i*>(current.data() + at));
const auto same = _mm_cmpeq_epi8(bytes, _mm_loadu_si128(reinterpret_cast<const __m128i*>(original.data() + at)));
const auto uploaded = _mm_loadu_si128(reinterpret_cast<const __m128i*>(original.data() + at));
auto same = _mm_cmpeq_epi8(bytes, uploaded);
if (keepGuest) same = _mm_or_si128(same, _mm_xor_si128(_mm_cmpeq_epi8(_mm_loadu_si128(reinterpret_cast<const __m128i*>(destination + at)), uploaded), _mm_set1_epi8(-1)));
const auto equal = static_cast<std::uint32_t>(_mm_movemask_epi8(same));
if (equal == 0xffffu) continue;
const auto changed = ~equal & 0xffffu;
Expand All @@ -3063,7 +3065,7 @@ void WriteChanged(std::uint64_t address, std::span<const std::byte> current, std
}
if (masked) _mm_sfence();
for (; at < size; ++at) {
if (current[at] == original[at]) continue;
if (current[at] == original[at] || (keepGuest && destination[at] != original[at])) continue;
destination[at] = current[at];
firstChanged = std::min(firstChanged, at);
lastChanged = std::max(lastChanged, at + 1);
Expand All @@ -3072,6 +3074,14 @@ void WriteChanged(std::uint64_t address, std::span<const std::byte> current, std
});
}

void WriteChanged(std::uint64_t address, std::span<const std::byte> current, std::span<const std::byte> original, std::uint32_t byteStoreLimit) {
writeChanged(address, current, original, byteStoreLimit, false);
}

void WriteChangedKeepingGuest(std::uint64_t address, std::span<const std::byte> current, std::span<const std::byte> original) {
writeChanged(address, current, original, ChangedByteStoreLimit(), true);
}

void Write(std::uint64_t address, std::span<const std::byte> source, std::size_t alignment) {
if (source.empty()) return;
// A store, as in WriteChanged.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ inline VkDeviceSize HostImportOffset(const HostImport& import, std::uint64_t add
return address - import.base + import.lead;
}

struct HostImportStart {
std::uint64_t guest;
VkDeviceAddress device;
};

inline HostImportStart HostImportBufferStart(const HostImport& import) {
return {import.base - import.lead, import.address - import.lead};
}

inline VkDeviceAddress HostImportDeviceAddress(const HostImport& import, std::uint64_t address) {
return HostImportBufferStart(import).device + HostImportOffset(import, address);
}

enum class ImportWatch : std::uint8_t { Watch, Unwatch };

struct ImportProbe {
Expand Down Expand Up @@ -467,7 +480,9 @@ class GuestBufferMemory {
bool gpuCopy = false;
VkBuffer copySource = VK_NULL_HANDLE;
std::uint64_t copySourceBase = 0;
VkDeviceAddress copySourceAddress = 0;
bool copiedBack = false;
std::shared_ptr<Buffer> shadowCopy;
// An element the shader updates atomically lies inside (AddWritable's `atomic`).
bool atomic = false;
bool swept = false;
Expand Down
15 changes: 12 additions & 3 deletions core/libs/prx/libSceAgcDriver/Graphics/include/Recorder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Recorder {
// that know their ranges). `seed` (kept) supplies an
// unaligned head or tail; [begin, end) is the guest range. Debug aid: APS5_DCC_KEYS_EACH=1
// records every store at once, as before (QueueKeyStore then records and returns).
void QueueKeyStore(VkBuffer buffer, VkDeviceSize first, VkDeviceSize last, std::shared_ptr<void> seed, std::uint64_t begin, std::uint64_t end);
void QueueKeyStore(VkBuffer buffer, VkDeviceSize first, VkDeviceSize last, std::shared_ptr<void> seed, std::uint64_t begin, std::uint64_t end, VkDeviceAddress target = 0);
bool HasQueuedKeyStores() const { return open != nullptr && !open->keyStores.empty(); }
bool QueuedKeyStoreOverlaps(std::uint64_t address, std::size_t bytes) const;
// Whether `overlaps(begin, end)` holds for a queued store's range.
Expand Down Expand Up @@ -149,7 +149,8 @@ class Recorder {
void NotePendingFill(std::uint64_t address, std::size_t bytes, std::uint8_t value);
void NotePendingWordFill(std::uint64_t address, std::size_t bytes, std::uint32_t word);
// Notes several [begin, end) ranges and publishes the snapshot once (a dispatch writes many buffers).
void NotePendingWrites(std::span<const std::pair<std::uint64_t, std::uint64_t>> ranges);
static constexpr int ShadowPublishWrite = -2;
void NotePendingWrites(std::span<const std::pair<std::uint64_t, std::uint64_t>> ranges, int value = -1);
bool PendingWriteOverlaps(std::uint64_t address, std::size_t bytes) const;
// Whether no unfinished batch writes the range any more: no open-batch overlap, and every
// overlapping in-flight batch's fence has signaled (its in-place GPU stores are final in host
Expand Down Expand Up @@ -244,6 +245,7 @@ class Recorder {
void CountSamples();
bool CountingSamples() const { return countingSamples; }
bool RecordMeshArguments(VkCommandBuffer commands, VkDeviceAddress record, VkDeviceAddress arguments, std::span<const std::uint32_t, 7> rules);
bool RecordChangedCopy(VkCommandBuffer commands, VkDeviceAddress result, VkDeviceAddress snapshot, VkDeviceAddress target, std::uint32_t dwords, VkDeviceAddress counter = 0);
std::uint64_t SamplesTotal();
bool DumpSamples(VkDeviceAddress target, std::uint64_t address = 0);
void NoteSampledDraw(VkCommandBuffer commands);
Expand Down Expand Up @@ -493,7 +495,7 @@ class Recorder {
std::uint64_t stores, runs, joined, replaced, wawBarriers, joinsRefused, queuedNoted, queuedOverRecorded, queuedHits, queuedHookRecords, queuedHookInCompletion;
// DCC key stores queued, the runs that recorded them (at Submit, or before a writer of a
// queued range), and the queued stores a duplicate range joined.
std::uint64_t keyStores, keyStoreRuns, keyStoreRunsForWriter, keyStoresJoined;
std::uint64_t keyStores, keyStoreRuns, keyStoreRunsForWriter, keyStoresJoined, keyStoresGuarded, keyBlocksLeftToCpu;
// Store runs recorded at Submit, and in place before a later writer or reader of a queued
// store's bytes (FlushStores).
std::uint64_t runsAtSubmit, runsForced;
Expand Down Expand Up @@ -724,6 +726,9 @@ class Recorder {
std::shared_ptr<void> seed;
std::uint64_t begin;
std::uint64_t end;
std::shared_ptr<Buffer> snapshot;
VkDeviceAddress target = 0;
std::uint64_t cpuGeneration = 0;
};
std::vector<KeyStore> keyStores;
std::vector<std::uint64_t> drawTraceTokens;
Expand Down Expand Up @@ -770,6 +775,10 @@ class Recorder {
int meshArgumentState = 0;
VkPipelineLayout meshArgumentLayout = VK_NULL_HANDLE;
VkPipeline meshArgumentPipeline = VK_NULL_HANDLE;
int changedCopyState = 0;
std::shared_ptr<Buffer> onesBuffer;
VkPipelineLayout changedCopyLayout = VK_NULL_HANDLE;
VkPipeline changedCopyPipeline = VK_NULL_HANDLE;
int sampleCounterState = 0;
std::unique_ptr<Buffer> sampleCounter;
VkPipelineLayout sampleLayout = VK_NULL_HANDLE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#version 450
#extension GL_EXT_buffer_reference : require
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require

layout(local_size_x = 64) in;
layout(buffer_reference, std430, buffer_reference_align = 4) buffer Words { uint words[]; };
layout(push_constant) uniform Parameters {
Words result;
Words snapshot;
Words target;
Words counter;
uint count;
uint always;
} parameters;

uint differingBytes(uint difference) {
uint folded = difference | (difference >> 1u);
folded |= folded >> 2u;
folded |= folded >> 4u;
return (folded & 0x01010101u) * 0xffu;
}

void main() {
uint index = gl_GlobalInvocationID.x;
if (index >= parameters.count) return;
uint value = parameters.result.words[index];
uint before = parameters.snapshot.words[index];
uint held = parameters.target.words[index];
uint changed = differingBytes(value ^ before);
uint kept = differingBytes(held ^ before);
uint stored = parameters.always != 0u ? 0xffffffffu : changed & ~kept;
if (stored != 0u) parameters.target.words[index] = (held & ~stored) | (value & stored);
if (uint64_t(parameters.counter) != 0ul && (changed & kept) != 0u) atomicAdd(parameters.counter.words[0], 1u);
}
2 changes: 1 addition & 1 deletion core/libs/prx/libSceAgcDriver/Graphics/src/DccMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ bool StoreUncompressedOnGpu(const Context& context, Recorder& recorder, std::uin
if (AnyShadowedOverlaps(begin, count)) PublishShadow(begin, count, PublishScope::PartialUnits, PublishReason::Keys);
// Queued on the open batch and recorded with the batch's other key stores as one run (or before
// a later command writing the keys; see Recorder::QueueKeyStore).
recorder.QueueKeyStore(import->buffer, first, last, std::move(seed), begin, begin + count);
recorder.QueueKeyStore(import->buffer, first, last, std::move(seed), begin, begin + count, import->address != 0 ? HostImportDeviceAddress(*import, begin) : 0);
recorder.NotePendingFill(begin, count, 0xff);
GuestMemory::MarkWritten(begin, count);
// After the note: an unlocked memo lookup drops an entry the snapshot does not cover yet. The
Expand Down
Loading
Loading