Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp"
#include <cstddef>
#include <cstdint>
#include <optional>
#include <span>

namespace AgcDriver::Graphics {
Expand All @@ -33,6 +34,11 @@ bool KeysServeSurface(std::uint64_t followedDcc, DccKeys uploaded, DccKeys fille
DccKeys ReadDccKeys(std::uint64_t metaAddress, std::uint64_t surfaceBytes);
bool IsDccClear(DccKeys keys);
DccKeys CurrentDccKeys(std::uint64_t metaAddress, std::uint64_t surfaceBytes);
// The keys recorded work still pending on the GPU leaves over the surface's metadata, when the newest
// recorded writer of the whole range is a key store the driver noted (NoteKeysFillOnGpu: the memo
// CurrentDccKeys answers from without a wait); nullopt when nothing writes the range or the newest
// writer is anything else. Under GuestMemory::GpuMutex only; never waits.
std::optional<DccKeys> KnownPendingDccKeys(std::uint64_t metaAddress, std::uint64_t surfaceBytes);
// Stores "uncompressed" keys over the surface's metadata on the CPU (a guest memory write: it waits
// for recorded GPU work that writes the keys first).
void MarkDccUncompressed(std::uint64_t metaAddress, std::uint64_t surfaceBytes);
Expand Down
6 changes: 4 additions & 2 deletions core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,11 @@ class StorageTexture : public std::enable_shared_from_this<StorageTexture> {
// the image dirty like a shader write: the guest bytes follow through the deferred write-back,
// the range is stamped written so every other reader of it (other images over the memory,
// captures, snapshots) takes the bytes through the flush hook, and this image alone is current.
// False, naming why in `refusal`, when the fill must be stored as before.
// False, naming why in `refusal`, when the fill must be stored as before. `pendingKeysKnown`: the
// caller proved what the recorded work still writing the keys leaves (KnownPendingDccKeys), and
// that it lets the image stand for the texels, so a pending key writer alone does not refuse.
static constexpr std::uint32_t WholeImage = ~0u;
bool FillClear(std::span<const std::uint32_t, 4> pattern, std::uint32_t layer, const char*& refusal);
bool FillClear(std::span<const std::uint32_t, 4> pattern, std::uint32_t layer, const char*& refusal, bool pendingKeysKnown = false);
// The cached, live image whose surface is exactly [address, address + bytes), if any (the newest
// of several: see ClassifyFill).
static std::shared_ptr<StorageTexture> FindLive(std::uint64_t address, std::uint64_t bytes);
Expand Down
8 changes: 8 additions & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/src/DccMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,14 @@ DccKeys CurrentDccKeys(std::uint64_t metaAddress, std::uint64_t surfaceBytes) {
return currentDccKeys(metaAddress, surfaceBytes, memoized);
}

std::optional<DccKeys> KnownPendingDccKeys(std::uint64_t metaAddress, std::uint64_t surfaceBytes) {
const auto count = static_cast<std::size_t>(surfaceBytes / KeyBytes);
if (metaAddress == 0 || count == 0 || !GuestMemory::GpuMutex().HeldByThisThread()) return std::nullopt;
auto* recorder = Recorder::Active();
if (recorder == nullptr || !recorder->PendingWriteOverlaps(metaAddress, count)) return std::nullopt;
return PendingStoreKeys(*recorder, metaAddress, count);
}

bool IsDccClear(DccKeys keys) {
return keys == DccKeys::Clear0000 || keys == DccKeys::Clear0001 || keys == DccKeys::Clear1110 || keys == DccKeys::Clear1111 || keys == DccKeys::ClearRegister;
}
Expand Down
17 changes: 14 additions & 3 deletions core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ std::array<std::byte, 16> clearTexel(const ColorTarget& color, DccKeys keys) {
return texel;
}

bool clearToTexel(StorageTexture& image, const std::array<std::byte, 16>& texel, std::uint32_t elementBytes, const char*& refusal) {
bool clearToTexel(StorageTexture& image, const std::array<std::byte, 16>& texel, std::uint32_t elementBytes, const char*& refusal, bool pendingKeysKnown = false) {
std::array<std::byte, 16> repeated{};
for (std::size_t offset = 0; offset + elementBytes <= repeated.size(); offset += elementBytes) std::memcpy(repeated.data() + offset, texel.data(), elementBytes);
std::array<std::uint32_t, 4> pattern{};
std::memcpy(pattern.data(), repeated.data(), repeated.size());
return image.FillClear(std::span<const std::uint32_t, 4>(pattern), StorageTexture::WholeImage, refusal);
return image.FillClear(std::span<const std::uint32_t, 4>(pattern), StorageTexture::WholeImage, refusal, pendingKeysKnown);
}

void storeClearTexels(const Context& context, const ColorTarget& color, const std::array<std::byte, 16>& texel) {
Expand All @@ -110,7 +110,18 @@ void materializeRegisterClear(const Context& context, const ColorTarget& color,
if (ProvedCurrentDccKeys(color.dccAddress, color.bytes, resident.TargetKeyProof()) != DccKeys::ClearRegister) return;
const auto texel = clearTexel(color, DccKeys::ClearRegister);
const char* refusal = nullptr;
bool cleared = clearToTexel(resident, texel, color.elementBytes, refusal);
// Keys still written by recorded work make FillClear refuse, and the fallback below waits for that
// work. The wait buys nothing when the newest recorded writer of the keys is a key store the
// driver noted with the register clear code (the memo ProvedCurrentDccKeys just answered from):
// the keys the GPU will hold are then known, and they are the code this clear stands for. The
// image clear and the "uncompressed" key store that follows are recorded into the same queue
// behind that store, so they land after it, the order the wait gave. Any other pending writer
// (a title kernel, a partial or wider write, a shadowed range) leaves the keys unknown and keeps
// the wait, and so do keys in memory (still from before the store) that FillClear's own scan
// refuses. APS5_NO_REGISTER_CLEAR_KNOWN_KEYS=1 always waits.
static const bool knownKeys = std::getenv("APS5_NO_REGISTER_CLEAR_KNOWN_KEYS") == nullptr;
const bool pendingKeysKnown = knownKeys && KnownPendingDccKeys(color.dccAddress, color.bytes) == DccKeys::ClearRegister;
bool cleared = clearToTexel(resident, texel, color.elementBytes, refusal, pendingKeysKnown);
static const bool syncKeys = std::getenv("APS5_NO_REGISTER_CLEAR_SYNC") == nullptr;
const auto keyBytes = static_cast<std::size_t>(color.bytes / 256);
if (!cleared && syncKeys && keyBytes != 0) {
Expand Down
6 changes: 3 additions & 3 deletions core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3199,7 +3199,7 @@ bool StorageTexture::clearByKeysFill(DccKeys keys, std::uint8_t key) {
return true;
}

bool StorageTexture::FillClear(std::span<const std::uint32_t, 4> pattern, std::uint32_t layer, const char*& refusal) {
bool StorageTexture::FillClear(std::span<const std::uint32_t, 4> pattern, std::uint32_t layer, const char*& refusal, bool pendingKeysKnown) {
// Debug aid (APS5_TRACE_FILL_COVER=1): the first layer covers refused, with the surface, the
// pattern and the generation the refusal was memoized at (ClassifyFill's layerRefusedGeneration).
static const bool trace = std::getenv("APS5_TRACE_FILL_COVER") != nullptr;
Expand Down Expand Up @@ -3228,8 +3228,8 @@ bool StorageTexture::FillClear(std::span<const std::uint32_t, 4> pattern, std::u
if (descriptor.dccAddress != 0) {
// Under a clear code reads see that value whatever the texels hold, and keys a recorded
// kernel still writes are not in the bytes yet: only keys that read as uncompressed now let
// the image stand for the texels.
if (Recorder::SnapshotWriteOverlaps(descriptor.dccAddress, static_cast<std::size_t>(guestBytes / 256))) {
// the image stand for the texels (unless the caller proved what those keys will be).
if (!pendingKeysKnown && Recorder::SnapshotWriteOverlaps(descriptor.dccAddress, static_cast<std::size_t>(guestBytes / 256))) {
refusal = "keys pending";
return false;
}
Expand Down
27 changes: 27 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/Recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3447,8 +3447,13 @@ void pendingKeyStoreTests(const Device& device, Recorder& recorder) {
recorder.NotePendingWrite(address, keyCount);
NoteKeysFillOnGpu(address, keyCount, DccKeys::Clear0001);
Require(CurrentDccKeys(address, surfaceBytes) == DccKeys::Clear0001 && recorder.PendingWriteOverlaps(address, keyCount), "the keys of a pending fill did not read as its keys without a wait");
Require(KnownPendingDccKeys(address, surfaceBytes) == DccKeys::Clear0001 && recorder.PendingWriteOverlaps(address, keyCount), "the keys of a pending fill were not known without a wait");
Require(KnownPendingDccKeys(address + 16, surfaceBytes / 2) == DccKeys::Clear0001, "a key range inside a pending fill was not known");
Require(!KnownPendingDccKeys(address, 2 * surfaceBytes).has_value() && recorder.PendingWriteOverlaps(address, keyCount), "a range wider than the pending fill was known, or waited for");
recorder.NotePendingWrite(address, 2 * keyCount);
Require(!KnownPendingDccKeys(address, surfaceBytes).has_value() && recorder.PendingWriteOverlaps(address, keyCount), "the keys under a later writer over a pending fill were known");
Require(CurrentDccKeys(address, surfaceBytes) == DccKeys::Uncompressed && !recorder.PendingWriteOverlaps(address, keyCount), "a later wider writer over a pending fill was not waited for");
Require(!KnownPendingDccKeys(address, surfaceBytes).has_value(), "keys nothing writes were known as pending keys");
recorder.Sync();
}

Expand Down Expand Up @@ -3565,6 +3570,28 @@ void metadataPassTests(const Device& device, Recorder& recorder) {
const auto keyed = StorageTexture::FindPending(address, surfaceBytes);
Require(keyed != nullptr && keyed != unkeyed && keyed->Descriptor().dccAddress == color.dccAddress, "a pass over a resident image under other keys did not remake it under the target's keys");
Require(keysUncompressed() && memoryHolds({0xff, 0xff, 0xff, 0xff}), "a pass over a remade resident image did not store the 1111 value");
// A register clear over keys that a noted key store still writes (materializeRegisterClear with
// the keys known): the image clear is recorded behind the store without waiting for it, and the
// uncompressed store recorded after both leaves the clear word in memory and uncompressed keys.
{
const auto* import = HostImportFor(base, address, bytes);
Require(import != nullptr, "the metadata pass block lost its import");
const auto commands = recorder.Commands();
context.Function<PFN_vkCmdFillBuffer>("vkCmdFillBuffer")(commands, import->buffer, color.dccAddress - import->base, keyCount, 0x20202020u);
RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT | VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_HOST_READ_BIT);
recorder.NotePendingWrite(color.dccAddress, keyCount);
NoteKeysFillOnGpu(color.dccAddress, keyCount, DccKeys::ClearRegister);
}
Require(KnownPendingDccKeys(color.dccAddress, surfaceBytes) == DccKeys::ClearRegister, "the keys of a pending register clear store were not known");
const std::array<std::uint32_t, 4> clearPattern{0x80402010u, 0x80402010u, 0x80402010u, 0x80402010u};
const char* refusal = nullptr;
Require(!keyed->FillClear(clearPattern, StorageTexture::WholeImage, refusal) && refusal != nullptr && std::strcmp(refusal, "keys pending") == 0, "a clear over pending keys was not refused without the proof");
refusal = nullptr;
Require(keyed->FillClear(clearPattern, StorageTexture::WholeImage, refusal, true), "a clear over known pending keys was refused");
Require(recorder.PendingWriteOverlaps(color.dccAddress, keyCount) && keys[0] == 0xff, "the clear over known pending keys waited for the key store");
MarkDccUncompressed(context, color.dccAddress, surfaceBytes);
Require(memoryHolds({0x10, 0x20, 0x40, 0x80}), "the clear recorded behind a pending key store did not store CB_COLOR_CLEAR_WORD");
Require(std::all_of(keys, keys + keyCount, [](std::uint8_t key) { return key == 0xff; }), "the keys after a clear behind a pending key store are not uncompressed");
}

// The data word positions of a dispatch-cache variant (Driver.cpp's data-only hits): leaves
Expand Down