diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp index fa552ac52484..13dbe1fd9f46 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/GuestBufferMemory.hpp @@ -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; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp index 532110460583..d2ec1c3234b0 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp @@ -290,6 +290,7 @@ class ShaderResources { struct ProofReport { ProofPath path = ProofPath::Fast; ProofFailure failure = ProofFailure::None; + bool audited = false; }; bool Revalidate(std::span shaders, ProofReport* report = nullptr); bool Revalidate(const CompiledShader& shader, ProofReport* report = nullptr) { return Revalidate(std::span(&shader, 1), report); } @@ -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> writeOnlyRanges; bool drawBuild = false; std::uint64_t dispatchThreads = 0; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp index 074681e607a6..7783e14e6d25 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp @@ -128,6 +128,8 @@ struct GuestBufferMemory::AddressSpace { namespace { +std::atomic importSetGeneration{1}; + struct RetiredImport; struct AddressSpaceCache { @@ -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(settle.waitForFences(device, 1, &batch.fence, VK_TRUE, 10'000'000'000ull)); @@ -378,7 +389,7 @@ void retireImport(VkDevice device, HostImports& state, std::mapsecond, [&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(state.device, state.destroyBuffer, state.freeMemory, state.imports.extract(it)); #ifdef _WIN32 @@ -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; } @@ -1892,7 +1904,7 @@ void refreshImports(const Context& context, HostImports& state, const GuestAlloc state.destroyBuffer = context.Function("vkDestroyBuffer"); state.freeMemory = context.Function("vkFreeMemory"); state.refreshedGeneration = 0; - ++state.epoch; + bumpEpoch(state); } if (state.destroyBuffer == nullptr) state.destroyBuffer = context.Function("vkDestroyBuffer"); if (state.freeMemory == nullptr) state.freeMemory = context.Function("vkFreeMemory"); @@ -3055,7 +3067,7 @@ void ClearHostImports(VkDevice device) { state.reportNoted = 0; state.device = VK_NULL_HANDLE; state.refreshedGeneration = 0; - ++state.epoch; + bumpEpoch(state); } namespace { @@ -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::max() - address) return false; auto& state = Imports(); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp index d6d84cd4aa5f..b4b85441bbea 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp @@ -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(text != nullptr ? std::strtoul(text, nullptr, 10) : 8ul); + }(); + return budget; +} + +bool ImportAuditAdmitted() { + static std::atomic slot{-1}; + static std::atomic 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 @@ -3306,6 +3330,7 @@ bool ShaderResources::Revalidate(std::span 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; @@ -3411,12 +3436,15 @@ bool ShaderResources::Revalidate(std::span 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()) { diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/SampledCacheBudget.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/SampledCacheBudget.cpp index 46e3184ea625..5d4bf067502f 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/SampledCacheBudget.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/SampledCacheBudget.cpp @@ -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(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"); diff --git a/docs/dev/ENVIRONMENT.md b/docs/dev/ENVIRONMENT.md index dd75c7f25f79..04bb735416c3 100644 --- a/docs/dev/ENVIRONMENT.md +++ b/docs/dev/ENVIRONMENT.md @@ -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` |