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 @@ -132,6 +132,7 @@ bool ImportMappedRanges(const Context& context, const GuestAllocations::Mapped&
// made outside the device lock (a sampled texture's path, a dispatch's pre-sync); the path taken
// re-checks with HostImportFor when it binds the import.
bool HostImportCovers(const Context& context, std::uint64_t address, std::size_t bytes);
std::uint64_t HostImportSetGeneration();
struct RegisteredPiece {
std::uint64_t address;
std::uint64_t bytes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ class ShaderResources {
struct ProofReport {
ProofPath path = ProofPath::Fast;
ProofFailure failure = ProofFailure::None;
bool audited = false;
};
bool Revalidate(std::span<const CompiledShader> shaders, ProofReport* report = nullptr);
bool Revalidate(const CompiledShader& shader, ProofReport* report = nullptr) { return Revalidate(std::span<const CompiledShader>(&shader, 1), report); }
Expand Down Expand Up @@ -598,6 +599,7 @@ class ShaderResources {
// notes (counted in MarkGpuWrites for the [buffers] line).
std::size_t readOnlyBuffers = 0;
std::chrono::steady_clock::time_point lastFullWalk = std::chrono::steady_clock::now();
std::uint64_t walkImportGeneration = HostImportSetGeneration();
std::vector<std::pair<std::uint64_t, std::uint64_t>> writeOnlyRanges;
bool drawBuild = false;
std::uint64_t dispatchThreads = 0;
Expand Down
22 changes: 19 additions & 3 deletions core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ struct GuestBufferMemory::AddressSpace {

namespace {

std::atomic<std::uint64_t> importSetGeneration{1};

struct RetiredImport;

struct AddressSpaceCache {
Expand Down Expand Up @@ -284,6 +286,15 @@ HostImports& Imports() {
return imports;
}

void importSetChanged() {
importSetGeneration.fetch_add(1, std::memory_order_release);
}

void bumpEpoch(HostImports& state) {
++state.epoch;
importSetChanged();
}

#ifdef _WIN32
void waitSettleBatch(const ImportSettle& settle, VkDevice device, const SettleBatch& batch) {
if (batch.busy && settle.waitForFences != nullptr) static_cast<void>(settle.waitForFences(device, 1, &batch.fence, VK_TRUE, 10'000'000'000ull));
Expand Down Expand Up @@ -378,7 +389,7 @@ void retireImport(VkDevice device, HostImports& state, std::map<std::uint64_t, H
// readable registered range (the import retires because its registration vanished or changed
// size); the holder below outlives the batch that copies them.
RetireShadow(device, it->second, [&lease, joined = it->second.joined](std::uint64_t begin, std::uint64_t end) { return runCovers(lease, begin, end) || (joined && joinedCovers(lease, begin, end)); });
++state.epoch;
bumpEpoch(state);
state.liveBytes -= it->second.pinnedBytes;
auto holder = std::make_shared<RetiredImport>(state.device, state.destroyBuffer, state.freeMemory, state.imports.extract(it));
#ifdef _WIN32
Expand Down Expand Up @@ -1847,6 +1858,7 @@ const HostImport* importRunAt(const Context& context, HostImports& state, Import
}
#endif
const auto inserted = state.imports.emplace(base, entry);
importSetChanged();
owner.armed = false;
return &inserted.first->second;
}
Expand Down Expand Up @@ -1892,7 +1904,7 @@ void refreshImports(const Context& context, HostImports& state, const GuestAlloc
state.destroyBuffer = context.Function<PFN_vkDestroyBuffer>("vkDestroyBuffer");
state.freeMemory = context.Function<PFN_vkFreeMemory>("vkFreeMemory");
state.refreshedGeneration = 0;
++state.epoch;
bumpEpoch(state);
}
if (state.destroyBuffer == nullptr) state.destroyBuffer = context.Function<PFN_vkDestroyBuffer>("vkDestroyBuffer");
if (state.freeMemory == nullptr) state.freeMemory = context.Function<PFN_vkFreeMemory>("vkFreeMemory");
Expand Down Expand Up @@ -3055,7 +3067,7 @@ void ClearHostImports(VkDevice device) {
state.reportNoted = 0;
state.device = VK_NULL_HANDLE;
state.refreshedGeneration = 0;
++state.epoch;
bumpEpoch(state);
}

namespace {
Expand Down Expand Up @@ -3480,6 +3492,10 @@ std::uint64_t RegisteredReadableEnd(std::uint64_t address) {
return range != nullptr ? range->address + range->bytes : 0;
}

std::uint64_t HostImportSetGeneration() {
return importSetGeneration.load(std::memory_order_acquire);
}

bool HostImportCovers(const Context& context, std::uint64_t address, std::size_t bytes) {
if (context.hostImportAlignment == 0 || bytes == 0 || bytes > std::numeric_limits<std::uint64_t>::max() - address) return false;
auto& state = Imports();
Expand Down
32 changes: 30 additions & 2 deletions core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2889,11 +2889,35 @@ std::chrono::steady_clock::duration TemplateAuditInterval() {
return interval;
}

bool AuditEveryInterval() {
static const bool always = std::getenv("APS5_TEMPLATE_AUDIT_ALWAYS") != nullptr;
return always;
}

bool TemplateAuditDue(std::chrono::steady_clock::time_point last) {
const auto interval = TemplateAuditInterval();
return interval.count() != 0 && std::chrono::steady_clock::now() - last >= interval;
}

std::uint32_t ImportAuditBudget() {
static const auto budget = [] {
const char* text = std::getenv("APS5_TEMPLATE_AUDIT_BUDGET");
return static_cast<std::uint32_t>(text != nullptr ? std::strtoul(text, nullptr, 10) : 8ul);
}();
return budget;
}

bool ImportAuditAdmitted() {
static std::atomic<std::int64_t> slot{-1};
static std::atomic<std::uint32_t> admitted{0};
const auto budget = ImportAuditBudget();
if (budget == 0) return true;
const auto now = std::chrono::steady_clock::now().time_since_epoch() / std::chrono::milliseconds(4);
auto seen = slot.load(std::memory_order_relaxed);
if (seen != now && slot.compare_exchange_strong(seen, now, std::memory_order_relaxed)) admitted.store(0, std::memory_order_relaxed);
return admitted.fetch_add(1, std::memory_order_relaxed) < budget;
}

}

// Turns the lookups' records into this object's per-texture validation records (see
Expand Down Expand Up @@ -3306,6 +3330,7 @@ bool ShaderResources::Revalidate(std::span<const CompiledShader> shaders, ProofR
// by stage, binding by binding, so the walk repeats that order.
const auto fullWalk = [&] {
lastFullWalk = std::chrono::steady_clock::now();
walkImportGeneration = HostImportSetGeneration();
lookupLog().clear();
BuildLookups lookups;
std::size_t textureIndex = 0;
Expand Down Expand Up @@ -3411,12 +3436,15 @@ bool ShaderResources::Revalidate(std::span<const CompiledShader> shaders, ProofR
}
if (fallback != OwnRefreshFallback::Count) countOwnRefreshFallback(fallback);
}
if (fast && TemplateAuditDue(lastFullWalk)) {
if (fast && TemplateAuditDue(lastFullWalk) && (AuditEveryInterval() || (walkImportGeneration != HostImportSetGeneration() && ImportAuditAdmitted()))) {
fast = false;
ownRefreshed = false;
reason = FastFail::Audit;
}
if (report != nullptr) report->path = !fast ? ProofPath::Full : ownRefreshed ? ProofPath::OwnRefreshed : ProofPath::Fast;
if (report != nullptr) {
report->path = !fast ? ProofPath::Full : ownRefreshed ? ProofPath::OwnRefreshed : ProofPath::Fast;
report->audited = !fast && reason == FastFail::Audit;
}
if (!fast) {
countFullWalk(reason);
if (!fullWalk()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,26 @@ bool lookupChangedTests(Context context) {
Require(first->Revalidate(shader, &report) && report.path == ShaderResources::ProofPath::Fast, "the lookup-changed template did not take the fast proof");
std::this_thread::sleep_for(std::chrono::milliseconds(300));
Require(first->Revalidate(shader, &report), "the audit refused an unchanged template");
Require(report.path == ShaderResources::ProofPath::Full, "a proof past the audit interval did not take the full walk");
Require(first->SampledTextures().front() == bound, "the audit changed the bound texture");
std::this_thread::sleep_for(std::chrono::milliseconds(300));
Require(first->Revalidate(shader, &report) && report.path == ShaderResources::ProofPath::Fast && !report.audited, "a proof past the audit interval took the full walk although no host import changed since the last one");
#ifdef _WIN32
Require(GuestArena::GuestArenaAllocate_nid_postfix(SurfaceBytes, 65536) != nullptr, "cannot reserve the gap before the unrelated surface");
#endif
WatchedSurface unrelated(context);
std::memset(reinterpret_cast<void*>(unrelated.Address()), 0x33, SurfaceBytes);
std::this_thread::sleep_for(std::chrono::milliseconds(300));
Require(first->Revalidate(shader, &report) && report.path == ShaderResources::ProofPath::Fast && !report.audited, "a guest write outside the template's surfaces made a proof past the audit interval take the full walk although no host import changed");
const auto beforeUnrelated = HostImportSetGeneration();
if (HostImportFor(context, unrelated.Address(), SurfaceBytes) == nullptr) {
Require(std::getenv("ANYPS5_REQUIRE_VULKAN") == nullptr, "the host import of the unrelated surface was refused");
std::puts("skipped, host imports are unavailable");
return false;
}
Require(HostImportSetGeneration() != beforeUnrelated, "adding a host import did not move the import set generation");
std::this_thread::sleep_for(std::chrono::milliseconds(300));
Require(first->Revalidate(shader, &report) && report.audited && report.path == ShaderResources::ProofPath::Full, "a proof past the audit interval after a host import was added did not audit the template");
Require(first->SampledTextures().front() == bound, "the audit after an unrelated host import changed the bound texture");
Require(first->Revalidate(shader, &report) && report.path == ShaderResources::ProofPath::Fast, "the proof right after an audit did not take the fast proof");
if (HostImportFor(context, surface.Address(), SurfaceBytes) == nullptr) {
Require(std::getenv("ANYPS5_REQUIRE_VULKAN") == nullptr, "the host import of the watched surface was refused");
Expand Down
4 changes: 3 additions & 1 deletion docs/dev/ENVIRONMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ The code reads environment names through `getenv`, `IsEnvironmentSet`, `kibSetti
| `APS5_SYNC_SAMPLE_DUMPS` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp` | | takes the CPU path for occlusion-counter sample dumps, as `APS5_NO_GPU_SAMPLE_DUMPS` does |
| `APS5_SYNC_SHADER_PREPARE` | presence | `core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cpp` | | prepares registered shaders on the registering thread instead of in the background |
| `APS5_SYNC_TEXTURE_UPLOAD` | presence | `core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp` | | restores the waiting batch for every texture upload |
| `APS5_TEMPLATE_AUDIT_MS` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp` | 1000 | milliseconds after which a cached shader-resource object takes the full walk instead of the fast proof; `0` turns the audit off |
| `APS5_TEMPLATE_AUDIT_ALWAYS` | flag | `core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp` | unset | the audit of `APS5_TEMPLATE_AUDIT_MS` runs at every interval, also when no host import was added or retired since the object's last full walk |
| `APS5_TEMPLATE_AUDIT_BUDGET` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp` | 8 | audits that a host import change may start per 4 ms across all cached shader-resource objects; the others keep the fast proof and audit at a later use, so the walks after one import are spread over frames; `0` removes the limit |
| `APS5_TEMPLATE_AUDIT_MS` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp` | 1000 | milliseconds after which a cached shader-resource object takes the full walk instead of the fast proof, when a host import was added or retired since its last full walk; `0` turns the audit off |
| `APS5_TEXTURE_CACHE_MIB` | integer | `core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp` | 0 | forced byte budget in MiB for the storage and sampled texture caches; `0` selects the budget automatically |
| `APS5_THREAD_NICE` | integer | `core/libs/prx/libSceAgcDriver/Execution/src/ThreadPriority.cpp` | -10 | nice value the raised queue workers and presenter ask rtkit for (clamped to -20..19) |
| `APS5_THREAD_PRIORITY` | string | `core/libs/prx/libSceAgcDriver/Execution/src/ThreadPriority.cpp` | off | raises the queue workers and presenter above the title's threads: `off`, `high` or `rt` |
Expand Down
Loading