Skip to content

feat(kyty): create large block-compressed textures without their top mips under memory pressure - #312

Open
Ryongen wants to merge 1 commit into
simnJS:AnyKytyfrom
Ryongen:feat/partial-bc-textures
Open

Ryongen wants to merge 1 commit into
simnJS:AnyKytyfrom
Ryongen:feat/partial-bc-textures

Conversation

@Ryongen

@Ryongen Ryongen commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What

On 8 GB cards the first scene of PPSA38500 pins 2.5-5.3 GB of textures through the bindless heap (heap_refs), ~93% of the bytes in textures >= 512 px; the texture GC cannot free them and VRAM overflows (guard kill or device loss). This is a last resort for cards too small for the scene; the heap fix itself belongs to the #168 rework.

  • Trigger: once device-local usage passes the VK_EXT_memory_budget budget minus the reserve the caches keep below it (GetTotalMemoryBudget: an eighth, at most 1 GiB), BC 2D textures >= 512 px with >= 3 mips drop mip 0; past halfway from there to the budget, mips 0-1. KYTY_PARTIAL_TEXTURE_MB overrides the start (0 = off). A 12-24 GB card that is not short never drops a mip.
  • No CPU waits: CreatePartialImage queues its binds; CommandScheduler::QueueSubmit flushes every queued bind in one vkQueueBindSparse that signals a timeline semaphore, and that submit and later ones wait on it on the GPU.
  • Omitted levels are never reached: sampled views that hold a resident mip clamp minLod. Every other use first binds memory for the omitted mips and uploads them from guest memory (TextureCache::MakeResident): a sampled view of omitted mips only, storage and attachment views, CopyImage/CopyImageMip (source and destination), clears, BeginColorRendering, and downloads/write-back. Image::Download, CopyImage, CopyImageWithBuffer, CopyMip, Resolve and FindView stop on a partial image that reaches them anyway. The device also needs residencyNonResidentStrict.
  • Lifetime: an image keeps its top mips unbound until something needs them, even after the pressure is gone; documented in the THIRD_PARTY_KYTY.md row.
  • KYTY_PARTIAL_TEXTURE_MB registered in Settings.cpp.
  • New test kyty_shim_partial_textures: a 2048 px BC1 texture with 12 mips created partially resident; a view of every mip keeps it partial, a view of mip 0 only backs it and its contents match a fully backed control; a copy from a partial texture matches the same copy from a fully backed control. The shared TextureCacheTestAccess moved to a header (both tests link into libSceAgcDriver).

Tested

Windows 10, RTX 2070 SUPER 617.42. ctest -R "kyty_shim_(partial_textures|image_write_back_alias)" -V with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, layer 1.4.363: on AnyKyty (90a4845) image_write_back_alias passes; on this branch both pass. Each log shows the loader inserting VK_LAYER_KHRONOS_validation, neither has was not found but was requested, and neither has a VUID- line. First version, same scene of PPSA38500: 769-3073 textures made partial per run, 134 MiB bound instead of 452 MiB up to 567 MiB instead of 3747 MiB, peak VRAM 6.66 GB vs a guard kill at 7.9 GB. The scene was not re-run with this revision.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 2070 SUPER, 617.42, Windows 10 22H2
  • AI-assisted: yes

@simnJS

simnJS commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Thanks for the numbers. Leaving the top mips of large BC textures unbacked is a reasonable last resort for cards that cannot hold the working set, and we would like to have it. It needs some changes first:

  1. CI: check_kyty_settings.py fails because KYTY_PARTIAL_TEXTURE_MB is not in the list of read names in core/libs/prx/libSceAgcDriver/KytyShim/src/Settings.cpp.
  2. Commit message: it names the title by its ID. Commit messages, file and branch names and code in this repository never name a game or a title ID. Please reword it, for example "in the first 3D scene of a title". The PR text can stay as it is.
  3. Trigger: half of the largest device-local heap starts dropping mips at 8 GB on a 16 GB card, where the same scene fits at about 13 GB. Please trigger on real pressure instead: the heap budget from VK_EXT_memory_budget, minus the reserve the allocator already keeps below it, so a card with 12 to 24 GB never loses a mip unless it is actually short.
  4. Stalls: every partial image does its own vkQueueBindSparse on the main queue and waits on a fence. On several drivers, a sparse bind on a busy queue waits for the work already submitted, so each of the 769 to 3073 images can cost a frame-sized stall while textures stream in. Please batch the binds and have the next submit wait on a semaphore signalled by the bind, instead of waiting on the CPU. At the very least, measure the time spent in that wait.
  5. Non-resident levels must be unreachable on every path, not only uploads and sampled views:
    • a view whose levels are all below first_resident_level (a view of mip 0 only, for example) gets minLod = its last level, which is still not resident;
    • image-to-image copies, blits, clears and readback or write-back to guest memory do not go through Image::Upload;
    • reads of unbound memory are only defined as zero when residencyNonResidentStrict is set.
      Please either require residencyNonResidentStrict and skip the unbound levels in those paths, or show that none of them can reach a partial image.
  6. Recovery: an image keeps its top mips missing for its whole life, even after the pressure is gone. That is fine for a first version, but please say so in the docs row.

On why these textures are pinned in the first place: the heap resolves every entry of a bindless table, read or not, and each resolved entry holds its image (heap_refs), which the GC skips. That is what makes VRAM grow. The fix for that belongs in the heap itself: resolve only the entries in use, and let the GC evict heap-held images that no recent draw used. It is tracked with the heap rework from #168. This PR stays useful after that, for cards that are simply too small for the scene.

@simnJS simnJS added enhancement New feature or request area: gpu-memory Memory tracking, write faults, buffer and texture caches, write-backs hard Deep change or open question: comment on the issue before starting needs: measurement Needs numbers from a run: in-run A/B, profile or counters labels Nov 26, 2026

@simnJS simnJS left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please register KYTY_PARTIAL_TEXTURE_MB in Settings.cpp and use a generic title reference in the commit body. A view limited to an omitted mip still selects that mip, and Image::CopyImage transfers from level zero. Please handle views and transfers that touch omitted levels, using fully backed images where residency cannot be guaranteed. Cover both cases under Vulkan validation with a fully backed control. Use current device-local budget and allocator headroom for the default, document the lifetime of omitted levels, and measure or batch the per-image fence waits.

…mips under memory pressure

Past the device-local budget less the cache reserve, large BC textures are sparse-resident without mip 0 (or 0-1); any use beyond sampling backs and uploads those mips first. On an 8 GB card this saved 2-3 GB of VRAM in the first 3D scene of a title.
@Ryongen
Ryongen force-pushed the feat/partial-bc-textures branch from bbba3c8 to 9a2d4ff Compare November 26, 2026 17:27

@simnJS simnJS left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new omitted-only view and copy cases cover the earlier examples. A shader can also use OpImageFetch at mip 0 through a sampled view that spans all mips. Vulkan returns zero or poison for integer fetches below the view’s minLod. Please make the mip resident before that path can read it and compare a shader fetch against the fully backed control. Offline CI reports the fixture’s skip as unexpected. Please report a specific optional sparse capability limitation when it applies and fail other Vulkan initialization errors. Please also rerun the memory-pressure comparison with the default threshold and batched binds on this head.

@Ryongen

Ryongen commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Before pushing the next revision I measured the fetch rule in the scene, and it changes the picture, so I would like your view on the direction first.

What the local revision does (not pushed yet):

  • ImageResource::fetched is set when a resource has an ImageRead (the OpImageFetch path); size queries and gathers do not set it. It goes into the texture desc and the heap profile key, so a root that fetches gets its own heap entries, and FindTexture backs and uploads the omitted mips before it hands out a view for such a root. Sampling roots keep the clamped view.
  • New fixture case: a compute shader fetches 64 mip-0 texels through a full-chain sampled view of a partial texture and matches the fully backed control; with the backing disabled it fails on exactly that comparison. The fixture now skips only with skipped, the device has no ... for the missing sparse capability (lavapipe: no VK_EXT_image_view_min_lod) and fails on any other initialization error.
  • All 107 kyty_shim_* tests pass under VK_LAYER_KHRONOS_validation 1.4.363 on the RTX 2070 SUPER, no VUID-.

Scene data (the same first 3D scene of the title, RTX 2070 SUPER 8 GB, default threshold, batched binds, a temporary counter per promotion reason):

  • About 1281 BC textures are created partial in the load burst (227 MiB bound instead of 1815 MiB).
  • Within 90 s, 897 of them are backed again: 895 because a fetching root reaches them, 2 for a view of omitted mips only. Nothing else (copies, clears, downloads, storage, targets) promoted any.
  • The late backing lands in the burst itself. Each run ended at about 1.5 min with TextureCache: could not back the omitted levels, because no allocator had room at that point (device use 7.98 GB). Stopping is the only safe answer there, since the alternative is to fetch from unbound memory.

The cause is granularity: fetched is known per root, and the bindless tables here are shared, so one ImageRead anywhere through a table backs every BC texture in it, including the ones that are only sampled.

Two directions I can see:

  1. Keep fetches from ever reading below the resident mip in the shader: the heap slot (and the plain binding) carries the first resident level, and the emitted OpImageFetch reads max(lod, first) at coordinates shifted right by the difference. Fetch then behaves like sampling does now (one mip coarser up close), nothing is backed again, and the omitted levels stay unreachable. It touches the SPIR-V emitter and the heap slot format.
  2. Keep the per-root rule, and stop making a texture partial once a fetching root has reached its address range, so the late backing does not happen. Simpler, but in this scene it keeps most of the savings out of reach.

I lean to 1. Which would you rather have in this PR?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: gpu-memory Memory tracking, write faults, buffer and texture caches, write-backs enhancement New feature or request hard Deep change or open question: comment on the issue before starting needs: measurement Needs numbers from a run: in-run A/B, profile or counters

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants