From 145932bf598a3fb4476ed63d9ceffefe02f5bf0c Mon Sep 17 00:00:00 2001 From: Lucas Daniel Duarte Cabral <177052392+Lucas-blip-png@users.noreply.github.com> Date: Sat, 3 Oct 2026 19:21:45 -0400 Subject: [PATCH] texture_cache: scan only the first page in FindImageFromRange (#5240) * texture_cache: scan only the first page in FindImageFromRange FindImageFromRange only accepts images whose guest address equals the requested address, and every image is registered in all of its pages, so the candidates are always found in the first page. Walking every page and image of a large texel buffer range cost ~35-45 us per call and showed up as the top per-draw cost in inFamous Second Son. Also skip the lock in RegionManager::ForEachModifiedRange when no bit of the requested type is set; the caller would be a no-op in that case. inFamous Second Son (Salmon Bay dock): 23.2 -> 30.7 FPS. * texture_cache: add ForEachImageWithAddress, drop region_manager change Look up only the bucket of the first page and only images with the exact address, as suggested in review. The lockless early out in ForEachModifiedRange is removed since ChangeRegionState updates the state bits before the tracker. * texture_cache: drop redundant address check and comment Image addresses are 256-byte aligned, so a matching key already implies the same address. --- src/video_core/texture_cache/texture_cache.cpp | 5 +---- src/video_core/texture_cache/texture_cache.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/video_core/texture_cache/texture_cache.cpp b/src/video_core/texture_cache/texture_cache.cpp index a07d912ea..bd41a3bc5 100644 --- a/src/video_core/texture_cache/texture_cache.cpp +++ b/src/video_core/texture_cache/texture_cache.cpp @@ -595,10 +595,7 @@ ImageId TextureCache::FindImage(ImageDesc& desc, bool exact_fmt) { ImageId TextureCache::FindImageFromRange(VAddr address, size_t size, bool ensure_valid) { SmallVector image_ids; - ForEachImageInRegion(address, size, [&](ImageId image_id, Image& image) { - if (image.info.guest_address != address) { - return; - } + ForEachImageWithAddress(address, [&](ImageId image_id, Image& image) { if (ensure_valid && !image.SafeToDownload()) { return; } diff --git a/src/video_core/texture_cache/texture_cache.h b/src/video_core/texture_cache/texture_cache.h index b3c50b06d..b20145475 100644 --- a/src/video_core/texture_cache/texture_cache.h +++ b/src/video_core/texture_cache/texture_cache.h @@ -277,6 +277,20 @@ public: }); } + template + void ForEachImageWithAddress(VAddr cpu_addr, Func&& func) { + const auto it = page_table.find(cpu_addr >> Traits::PAGE_BITS); + if (it == nullptr) { + return; + } + const u32 key = u32(cpu_addr >> 8); + for (const auto& entry : it->entries) { + if (entry.key == key) { + func(entry.id, slot_images[entry.id]); + } + } + } + private: /// Iterate over all page indices in a range template