Skip to content
Merged
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 @@ -6,6 +6,7 @@
#include "prx/libSceAgcDriver/Graphics/include/State.hpp"
#include <array>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <span>
Expand All @@ -24,6 +25,8 @@ DepthSampleLocations DepthSurfaceSampleLocations(const Context& context, const D
enum class DepthPlaneScope { Overlapped, Whole };
std::vector<std::pair<std::uint64_t, std::uint64_t>> FlushDepthPlanes(std::uint64_t address, std::size_t bytes, DepthPlaneScope scope = DepthPlaneScope::Overlapped);
bool DepthPlanesStale();
std::uint64_t NextRecordOrder();
std::uint64_t KeepNewerDepthUnits(std::uint64_t address, std::size_t bytes, std::uint64_t recorded, const std::function<std::uint64_t()>& stamp);
struct DepthPlaneStoreStatistics {
std::uint64_t stores = 0;
std::uint64_t units = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ class GuestBufferMemory {
bool uploaded = false;
bool committed = false;
mutable std::uint64_t directWritesGeneration = 0;
mutable std::uint64_t directWritesOrder = 0;
};

}
Expand Down
3 changes: 3 additions & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ class StorageTexture : public std::enable_shared_from_this<StorageTexture> {
// Whether an image with results pending, or one a FlushPending is storing right now, overlaps
// any of the [begin, end) ranges: one acquisition of the registry's mutex.
static bool AnyPendingOverlaps(std::span<const std::pair<std::uint64_t, std::uint64_t>> ranges);
static bool AnyPendingInside(std::uint64_t address, std::size_t bytes, const StorageTexture* except);
static bool AnyLiveOverlaps(std::span<const std::pair<std::uint64_t, std::uint64_t>> ranges);
static std::uint64_t StampKeepingNewerResults(std::uint64_t address, std::size_t bytes, std::uint64_t recorded, const std::function<std::uint64_t()>& stamp);
// Moves with every change of the pending registry (an image marked dirty, stored, flushed or
Expand Down Expand Up @@ -521,6 +522,7 @@ class StorageTexture : public std::enable_shared_from_this<StorageTexture> {
void writeBackLayers(const std::vector<bool>& layers);
void releaseReadTraps(bool sharingPages);
void storeTrappedOverlaps(const char* reason);
void storeLayers(const std::vector<bool>& layers);
bool unchangedSinceBaseline(std::uint64_t from, std::uint64_t to) const;
bool cpuLeftUnchanged(std::uint32_t layer, std::uint64_t from, std::uint64_t to, bool edge) const;
void saveBeforeUnwatch();
Expand Down Expand Up @@ -754,6 +756,7 @@ class StorageTexture : public std::enable_shared_from_this<StorageTexture> {
std::uint64_t trackedLayerBytes = 0;
std::vector<std::uint64_t> layerGeneration;
std::vector<std::uint64_t> layerPendingSince;
std::vector<std::uint64_t> layerRecorded;
std::vector<std::uint64_t> completionKept;
UnitFlags layerPending;
std::vector<bool> layerBehind;
Expand Down
70 changes: 69 additions & 1 deletion core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,8 @@ class DepthSurface {
return mirror;
}

void SizeUnits(std::uint64_t planeBytes) {
void SizeUnits(std::uint64_t bytesOfPlane) {
planeBytes = bytesOfPlane;
static const bool whole = std::getenv("APS5_DEPTH_PLANE_WHOLE_STORE") != nullptr;
const std::uint64_t bytes = !whole && target.address % 65536u == 0 ? std::uint64_t{65536} : std::max<std::uint64_t>(planeBytes, 1u);
const auto count = static_cast<std::size_t>(std::max<std::uint64_t>((planeBytes + bytes - 1u) / bytes, 1u));
Expand All @@ -1217,6 +1218,7 @@ class DepthSurface {
staleUnits.assign(count, false);
staleCount = 0;
unitGeneration.assign(count, 0);
unitDrawn.assign(count, 0);
}

bool AnyStale() const {
Expand All @@ -1233,6 +1235,10 @@ class DepthSurface {
staleCount = staleUnits.size();
}

void NoteDrawn(std::uint64_t order) {
unitDrawn.assign(staleUnits.size(), order);
}

void ClearStale() {
staleUnits.assign(staleUnits.size(), false);
staleCount = 0;
Expand Down Expand Up @@ -1312,7 +1318,20 @@ class DepthSurface {
return changed;
}

void StorePendingImages(const GuestTextureResource& resource) {
const auto bytes = planeBytes != 0 ? planeBytes : DescribeSurface(resource).guestBytes;
if (!StorageTexture::AnyPendingInside(resource.baseAddress, static_cast<std::size_t>(bytes), mirrorPlane.storage.get())) return;
struct Syncing {
bool& flag;
bool previous;
~Syncing() { flag = previous; }
} guard{syncing, syncing};
syncing = true;
static_cast<void>(StorageTexture::FlushPending(resource.baseAddress, static_cast<std::size_t>(bytes), mirrorPlane.storage.get(), "depth plane sync", PublishScope::None));
}

void SyncFromMemory(const GuestTextureResource& resource) {
StorePendingImages(resource);
std::uint64_t generation = 0;
const auto changed = WrittenUnits(resource, generation);
if ((pendingClear & VK_IMAGE_ASPECT_DEPTH_BIT) != 0 || !anyUnit(changed)) {
Expand Down Expand Up @@ -1395,7 +1414,9 @@ class DepthSurface {
std::uint64_t depthMark = 0;
std::uint64_t stencilMark = 0;
std::vector<std::uint64_t> unitGeneration;
std::vector<std::uint64_t> unitDrawn;
std::uint64_t unitBytes = 0;
std::uint64_t planeBytes = 0;
bool syncing = false;
std::uint64_t scannedEpoch = 0;

Expand Down Expand Up @@ -2866,6 +2887,7 @@ VkImageView DepthSurfaceViewWithStencilWriteIntent(const Context& context, const
const auto planeBytes = DescribeSurface(mirrorResource(target)).guestBytes;
surface->NoteDepthWork();
surface->MarkAllStale(planeBytes);
surface->NoteDrawn(NextRecordOrder());
noteStalePlane(plane, plane + planeBytes);
}
if (stencilWritten || created || stencilRebound) surface->MarkStencilStale();
Expand Down Expand Up @@ -2904,6 +2926,52 @@ bool DepthPlanesStale() {
return planes.begin.load(std::memory_order_acquire) < planes.end.load(std::memory_order_acquire);
}

std::uint64_t NextRecordOrder() {
static std::atomic<std::uint64_t> order{0};
return order.fetch_add(1, std::memory_order_relaxed) + 1u;
}

std::uint64_t KeepNewerDepthUnits(std::uint64_t address, std::size_t bytes, std::uint64_t recorded, const std::function<std::uint64_t()>& stamp) {
const auto end = bytes > std::numeric_limits<std::uint64_t>::max() - address ? std::numeric_limits<std::uint64_t>::max() : address + bytes;
if (recorded == 0 || bytes == 0 || !overlapsStalePlane(address, end)) return stamp();
struct Kept {
DepthSurface* surface;
std::size_t unit;
std::uint64_t from;
std::size_t bytes;
std::uint64_t since;
};
std::vector<Kept> kept;
{
std::lock_guard gpu(GuestMemory::GpuMutex());
std::lock_guard lock(surfacesMutex());
for (const auto& surface : surfaces()) {
if (!surface->AnyStale() || surface->retired || !mirrorable(surface->target) || surface->unitBytes == 0 || surface->planeBytes == 0) continue;
const auto plane = surface->target.address;
const auto planeBytes = surface->planeBytes;
if (plane >= end || address >= plane + planeBytes) continue;
const auto units = surface->StaleUnitsIn(planeBytes, address, end);
for (std::size_t unit = 0; unit < units.size() && unit < surface->unitDrawn.size() && unit < surface->unitGeneration.size(); ++unit) {
if (!units[unit] || surface->unitDrawn[unit] <= recorded || surface->unitGeneration[unit] == 0) continue;
const auto from = plane + unit * surface->unitBytes;
const auto length = static_cast<std::size_t>(std::min<std::uint64_t>(surface->unitBytes, planeBytes - unit * surface->unitBytes));
if (GuestMemory::UnchangedSince(from, length, surface->unitGeneration[unit])) kept.push_back({surface.get(), unit, from, length, surface->unitGeneration[unit]});
}
}
}
const auto generation = stamp();
if (generation == 0 || kept.empty()) return generation;
std::lock_guard gpu(GuestMemory::GpuMutex());
std::lock_guard lock(surfacesMutex());
for (const auto& entry : kept) {
if (std::none_of(surfaces().begin(), surfaces().end(), [&](const auto& surface) { return surface.get() == entry.surface; })) continue;
auto& surface = *entry.surface;
if (entry.unit >= surface.staleUnits.size() || entry.unit >= surface.unitGeneration.size() || !surface.staleUnits[entry.unit] || surface.unitGeneration[entry.unit] != entry.since) continue;
if (GuestMemory::UnchangedSinceCollected(entry.from, entry.bytes, entry.since)) surface.unitGeneration[entry.unit] = std::max(entry.since, generation);
}
return generation;
}

std::vector<std::pair<std::uint64_t, std::uint64_t>> FlushDepthPlanes(std::uint64_t address, std::size_t bytes, DepthPlaneScope scope) {
static thread_local bool flushing = false;
const auto end = bytes > std::numeric_limits<std::uint64_t>::max() - address ? std::numeric_limits<std::uint64_t>::max() : address + bytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6744,11 +6744,14 @@ void GuestBufferMemory::MarkStagedWrites() const {
}

void GuestBufferMemory::MarkDirectWrites() const {
std::uint64_t order = 0;
for (const auto& [begin, end] : writes) {
const auto* found = owner(begin, end);
if (found == nullptr) continue;
const auto& region = *found;
if (region.direct != nullptr || (region.gpuCopy && region.copiedBack)) {
if (order == 0) order = NextRecordOrder();
directWritesOrder = order;
const auto generation = GuestMemory::MarkPossiblyWritten(begin, static_cast<std::size_t>(end - begin));
if (generation != 0) directWritesGeneration = std::max(directWritesGeneration, generation);
}
Expand Down Expand Up @@ -6857,7 +6860,7 @@ void GuestBufferMemory::WriteBack() {
if (region.direct == nullptr && !(region.gpuCopy && (region.copiedBack || region.deviceLocal)) && !(region.mirror != nullptr && !region.mirror->writable)) Recorder::NoteWrittenBack(begin, static_cast<std::size_t>(end - begin));
if (region.direct != nullptr) {
// The GPU wrote imported guest memory directly; page write watching did not see it.
StorageTexture::StampKeepingNewerResults(begin, static_cast<std::size_t>(end - begin), directWritesGeneration, [&] { return GuestMemory::MarkPossiblyWritten(begin, static_cast<std::size_t>(end - begin)); });
StorageTexture::StampKeepingNewerResults(begin, static_cast<std::size_t>(end - begin), directWritesGeneration, [&] { return KeepNewerDepthUnits(begin, static_cast<std::size_t>(end - begin), directWritesOrder, [&] { return GuestMemory::MarkPossiblyWritten(begin, static_cast<std::size_t>(end - begin)); }); });
continue;
}
if (region.mirror != nullptr) {
Expand Down Expand Up @@ -6901,7 +6904,7 @@ void GuestBufferMemory::WriteBack() {
if (region.copiedBack) {
// The GPU copied the written sub-range back into the import; page write watching
// did not see it.
StorageTexture::StampKeepingNewerResults(begin, static_cast<std::size_t>(end - begin), directWritesGeneration, [&] { return GuestMemory::MarkPossiblyWritten(begin, static_cast<std::size_t>(end - begin)); });
StorageTexture::StampKeepingNewerResults(begin, static_cast<std::size_t>(end - begin), directWritesGeneration, [&] { return KeepNewerDepthUnits(begin, static_cast<std::size_t>(end - begin), directWritesOrder, [&] { return GuestMemory::MarkPossiblyWritten(begin, static_cast<std::size_t>(end - begin)); }); });
continue;
}
static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr;
Expand Down
33 changes: 33 additions & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,7 @@ StorageTexture::StorageTexture(const Context& context, TextureDetiler& detiler,
layerPendingSince.assign(trackedLayers, 0);
unwatchHash.assign(trackedLayers, {});
pendingMiss.assign(trackedLayers, BaselineMiss::None);
layerRecorded.assign(trackedLayers, 0);
layerPending.assign(trackedLayers, false);
layerOriginal.assign(trackedLayers, true);
savedTicket.assign(trackedLayers, 0);
Expand Down Expand Up @@ -4114,6 +4115,8 @@ std::uint64_t StorageTexture::OccasionalLabelStores() {
void StorageTexture::markLayersPending(std::uint32_t first, std::uint32_t count) {
CaptureTrace::Log("image-write image=%llx first=%u count=%u generation=%llu", static_cast<unsigned long long>(descriptor.baseAddress), first, count, static_cast<unsigned long long>(generation));
++writes;
const auto recorded = NextRecordOrder();
for (std::uint32_t layer = first; layer < first + count && layer < layerRecorded.size(); ++layer) layerRecorded[layer] = recorded;
static const bool eager = std::getenv("APS5_EAGER_WRITEBACK") != nullptr || std::getenv("APS5_NO_TEXTURE_CACHE") != nullptr;
if (const auto flip = flipSerial.load(std::memory_order_relaxed); writes == 1u || flip != writtenFlip) {
writtenOccasionally = writes == 1u || flip - writtenFlip > OccasionalWriteFlips;
Expand Down Expand Up @@ -5321,6 +5324,14 @@ std::uint64_t StorageTexture::StampKeepingNewerResults(std::uint64_t address, st
return generation;
}

bool StorageTexture::AnyPendingInside(std::uint64_t address, std::size_t bytes, const StorageTexture* except) {
auto& pending = Pending();
if (pending.flushingCount.load(std::memory_order_acquire) == 0 && pending.textures.SurelyClear(address, bytes)) return false;
std::lock_guard lock(pending.mutex);
if (pending.textures.MayOverlap(address, bytes) && std::any_of(pending.textures.begin(), pending.textures.end(), [&](const StorageTexture* texture) { return texture != except && texture->overlaps(address, bytes) && texture->pendingUnitInside(address, bytes); })) return true;
return std::any_of(pending.flushing.begin(), pending.flushing.end(), [&](const StorageTexture* texture) { return texture != except && texture->overlaps(address, bytes); });
}

bool StorageTexture::AnyPendingOverlaps(std::span<const std::pair<std::uint64_t, std::uint64_t>> ranges) {
if (ranges.empty()) return false;
auto& pending = Pending();
Expand Down Expand Up @@ -6497,6 +6508,28 @@ bool StorageTexture::cpuLeftUnchanged(std::uint32_t layer, std::uint64_t from, s
}

void StorageTexture::writeBackLayers(const std::vector<bool>& layers) {
std::uint64_t recorded = 0;
bool ordered = true;
std::uint64_t first = descriptor.baseAddress + guestBytes;
std::uint64_t last = descriptor.baseAddress;
for (std::uint32_t layer = 0; layer < trackedLayers; ++layer) {
if (!layers[layer]) continue;
ordered = ordered && layer < layerRecorded.size() && layerRecorded[layer] != 0;
if (layer < layerRecorded.size()) recorded = std::max(recorded, layerRecorded[layer]);
first = std::min(first, layerBegin(layer));
last = std::max(last, layerBegin(layer) + layerBytes(layer));
}
if (!ordered || last <= first) {
storeLayers(layers);
return;
}
static_cast<void>(KeepNewerDepthUnits(first, static_cast<std::size_t>(last - first), recorded, [&] {
storeLayers(layers);
return GuestMemory::TrackerGeneration();
}));
}

void StorageTexture::storeLayers(const std::vector<bool>& layers) {
CaptureTrace::Log("writeback image=%llx generation=%llu reason=%s units=%zu", static_cast<unsigned long long>(descriptor.baseAddress), static_cast<unsigned long long>(generation), flushReason, static_cast<std::size_t>(std::count(layers.begin(), layers.end(), true)));
static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr;
PhaseTimer timer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
if(BUILD_TESTING)
add_test(NAME agc_driver_depth_plane_sampling_evicting COMMAND agc_driver_depth_plane_sampling_tests --evicting)
set_tests_properties(agc_driver_depth_plane_sampling_evicting PROPERTIES ENVIRONMENT "APS5_DEPTH_PLANE_COPY_MIB=1" SKIP_RETURN_CODE 77 TIMEOUT 120)
add_test(NAME agc_driver_depth_plane_sampling_unimported COMMAND agc_driver_depth_plane_sampling_tests --require-write-watch)
set_tests_properties(agc_driver_depth_plane_sampling_unimported PROPERTIES ENVIRONMENT "APS5_NO_HOST_IMPORT=1" SKIP_RETURN_CODE 77 TIMEOUT 120)
if(WIN32)
set_tests_properties(agc_driver_depth_plane_sampling_evicting PROPERTIES ENVIRONMENT_MODIFICATION
set_tests_properties(agc_driver_depth_plane_sampling_evicting agc_driver_depth_plane_sampling_unimported PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
endif()
Loading
Loading