vkback: reuse retired streaming images and cache format properties #190

Open
github-bot wants to merge 1 commit from github-bot/gh-190 into main AGit
Owner

@⁠Mhyster on GitHub · 2026-12-02 06:33 UTC · original
Mhyster:opt/imgmake-reuse-pool → main · +337 −31 · 5 files · 1 commits · open

What

Overlap with open PRs: #168 and #178 edit 5 lines of greenfield/vkback/src/imgmake.rs (in make_linear_in_place external-memory setup); #144 / #147 / #152 do not touch imgmake.rs or imgview.rs.

Addresses docs/TASKS.md ("Placed image creation in the streaming phase": "reuse images of the same description and size instead of creating new ones"):

  • Bounded streaming-image reuse pool (ImgPool in greenfield/vkback/src/imgmake.rs):
    • When retire() completes retired arena-backed streaming textures (self.live, i.mem == vk::DeviceMemory::null(), arena_of.contains_key(&i.image), !i.depth, desc.flags & 7 == 0, GPU timeline serial passed, all views destroyed, shown == 0), parks them in a bounded pool keyed by exact ImgReuseKey (ty, fmt, extent, mips, layers, samples, usage, flags, gtt) instead of calling vkDestroyImage + arena.free immediately.
    • Pool is strictly capped (MAX_POOL_IMAGES = 128, MAX_POOL_BYTES = 32 MiB, MAX_ENTRY_BYTES = 8 MiB), with FIFO eviction of oldest entries when full.
    • On hit in make_image, reuses the already-bound, already-GENERAL vk::Image directly — skipping vkCreateImage, vkGetImageMemoryRequirements, arena.alloc, vkBindImageMemory, and the UNDEFINED -> GENERAL layout transition barrier — while keeping self.arena.made(img.image, self.serial) (hold_new) and self.upload(id, &img, key) intact.
    • Pooled images remain tracked in arena_of and life (so over_line() accounts for their bytes), and are drained immediately by heap whenever arena_memory would otherwise wait or grow a new block (Arena::can_fit_ready), whenever residency::let_go_from runs under memory pressure, and on Drop for VulkanBackend.
  • Per-device immutable format-properties cache (ImgCache::fmt_props & linear_ext_ok):
    • Caches vkGetPhysicalDeviceFormatProperties per vk::Format across imgmake.rs (make_image, make_linear_in_place) and imgview.rs (view_for), and caches vkGetPhysicalDeviceImageFormatProperties2 external-handle support per (vk::Format, uav) in make_linear_in_place.
  • Moves retired-image destruction into VulkanBackend::retire_passed_images in imgmake.rs (shrinking lib.rs by 12 lines).

The reference

No direct vkd3d-proton counterpart (D3D12 placed textures alias heap memory directly; vkback creates optimal vk::Image copies per placed streaming texture in imgmake.rs / arena.rs, per docs/TASKS.md "Placed image creation in the streaming phase").

Evidence

Checks run (all passed on Linux x86_64)

  • Unit test imgmake::tests::img_pool_reuses_exact_key_respects_caps_and_drains_by_heap: tests ImgPool directly for oversized rejection (> MAX_ENTRY_BYTES), exact-key LIFO reuse, heap/extent mismatch non-reuse, FIFO eviction under MAX_POOL_BYTES / MAX_POOL_IMAGES, and heap-selective (Some(true) vs None) draining.
  • cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked: passed (victory-core 13, victory-formats 49 passed / 5 ignored, victory-shader 240 passed / 2 ignored, xtask 6).
  • (cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked): passed (frame 13, umem 22, vkback 169 passed / 3 ignored, policy 6/6 including file_size).
  • cargo clippy --all-targets --locked -- -D warnings: passed cleanly across root (-p victory-shader -p victory-formats -p victory-core -p xtask), greenfield, greenfield/wine, and greenfield/vkback/translate.

Checks NOT run (live game / GPU unavailable in this session)

The host GPU was not touched in this session:

  • NOT run: A B B A boat-scene performance measurement with VKB_LAT + greenfield/tools/frec/frames.py (fps, mean/p50/p90/p99/max, share > 33.3 ms).
  • NOT run: 60 s house-scene Vulkan validation run (docs/tools.md, "Vulkan validation run").
  • NOT run: Before and after screenshots.
  • NOT run: Cross-compiled Windows PE clippy (cargo xwin clippy ..., verified by GitHub Actions CI).

Checklist (docs/AGENTS.md)

  • One topic (rule 31); docs/TASKS.md not edited, the fixed item named above (rule 29)
  • "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits)
  • No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9)
  • Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b)
  • Every switch is listed, read in one place, and a bad value stops the start (rule 2)
  • Queues and other externally synchronized Vulkan objects are used under their lock (rule 35)
  • Every unsafe block has a // SAFETY: comment; no raw pointer around the borrow checker (rule 36)
  • Clippy clean; tests call the production code; TRANSLATOR_VERSION bumped if the translator's output changes (rule 27)
  • Comments say what is true now and name functions, not line numbers (rule 3)
  • Nothing from the title, and no pointers to where non-free data can be found (hard constraints)
**@⁠Mhyster** on GitHub · 2026-12-02 06:33 UTC · [original](https://github.com/vktory6/VKtory/pull/190) `Mhyster:opt/imgmake-reuse-pool` → `main` · +337 −31 · 5 files · 1 commits · open ## What Overlap with open PRs: `#168` and `#178` edit 5 lines of `greenfield/vkback/src/imgmake.rs` (in `make_linear_in_place` external-memory setup); `#144` / `#147` / `#152` do not touch `imgmake.rs` or `imgview.rs`. Addresses `docs/TASKS.md` ("Placed image creation in the streaming phase": "reuse images of the same description and size instead of creating new ones"): - **Bounded streaming-image reuse pool (`ImgPool` in `greenfield/vkback/src/imgmake.rs`)**: - When `retire()` completes retired arena-backed streaming textures (`self.live`, `i.mem == vk::DeviceMemory::null()`, `arena_of.contains_key(&i.image)`, `!i.depth`, `desc.flags & 7 == 0`, GPU timeline serial passed, all views destroyed, `shown == 0`), parks them in a bounded pool keyed by exact `ImgReuseKey` (`ty`, `fmt`, `extent`, `mips`, `layers`, `samples`, `usage`, `flags`, `gtt`) instead of calling `vkDestroyImage` + `arena.free` immediately. - Pool is strictly capped (`MAX_POOL_IMAGES = 128`, `MAX_POOL_BYTES = 32 MiB`, `MAX_ENTRY_BYTES = 8 MiB`), with FIFO eviction of oldest entries when full. - On hit in `make_image`, reuses the already-bound, already-`GENERAL` `vk::Image` directly — skipping `vkCreateImage`, `vkGetImageMemoryRequirements`, `arena.alloc`, `vkBindImageMemory`, and the `UNDEFINED -> GENERAL` layout transition barrier — while keeping `self.arena.made(img.image, self.serial)` (`hold_new`) and `self.upload(id, &img, key)` intact. - Pooled images remain tracked in `arena_of` and `life` (so `over_line()` accounts for their bytes), and are drained immediately by heap whenever `arena_memory` would otherwise wait or grow a new block (`Arena::can_fit_ready`), whenever `residency::let_go_from` runs under memory pressure, and on `Drop for VulkanBackend`. - **Per-device immutable format-properties cache (`ImgCache::fmt_props` & `linear_ext_ok`)**: - Caches `vkGetPhysicalDeviceFormatProperties` per `vk::Format` across `imgmake.rs` (`make_image`, `make_linear_in_place`) and `imgview.rs` (`view_for`), and caches `vkGetPhysicalDeviceImageFormatProperties2` external-handle support per `(vk::Format, uav)` in `make_linear_in_place`. - Moves retired-image destruction into `VulkanBackend::retire_passed_images` in `imgmake.rs` (shrinking `lib.rs` by 12 lines). ## The reference No direct vkd3d-proton counterpart (D3D12 placed textures alias heap memory directly; `vkback` creates optimal `vk::Image` copies per placed streaming texture in `imgmake.rs` / `arena.rs`, per `docs/TASKS.md` "Placed image creation in the streaming phase"). ## Evidence ### Checks run (all passed on Linux x86_64) - Unit test `imgmake::tests::img_pool_reuses_exact_key_respects_caps_and_drains_by_heap`: tests `ImgPool` directly for oversized rejection (`> MAX_ENTRY_BYTES`), exact-key LIFO reuse, heap/extent mismatch non-reuse, FIFO eviction under `MAX_POOL_BYTES` / `MAX_POOL_IMAGES`, and heap-selective (`Some(true)` vs `None`) draining. - `cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked`: passed (`victory-core` 13, `victory-formats` 49 passed / 5 ignored, `victory-shader` 240 passed / 2 ignored, `xtask` 6). - `(cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked)`: passed (`frame` 13, `umem` 22, `vkback` 169 passed / 3 ignored, `policy` 6/6 including `file_size`). - `cargo clippy --all-targets --locked -- -D warnings`: passed cleanly across root (`-p victory-shader -p victory-formats -p victory-core -p xtask`), `greenfield`, `greenfield/wine`, and `greenfield/vkback/translate`. ### Checks NOT run (live game / GPU unavailable in this session) The host GPU was not touched in this session: - **NOT run**: A B B A boat-scene performance measurement with `VKB_LAT` + `greenfield/tools/frec/frames.py` (fps, mean/p50/p90/p99/max, share > 33.3 ms). - **NOT run**: 60 s house-scene Vulkan validation run (`docs/tools.md`, "Vulkan validation run"). - **NOT run**: Before and after screenshots. - **NOT run**: Cross-compiled Windows PE clippy (`cargo xwin clippy ...`, verified by GitHub Actions CI). ## Checklist (docs/AGENTS.md) - [x] One topic (rule 31); `docs/TASKS.md` not edited, the fixed item named above (rule 29) - [x] "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits) - [x] No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9) - [x] Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b) - [x] Every switch is listed, read in one place, and a bad value stops the start (rule 2) - [x] Queues and other externally synchronized Vulkan objects are used under their lock (rule 35) - [x] Every `unsafe` block has a `// SAFETY:` comment; no raw pointer around the borrow checker (rule 36) - [x] Clippy clean; tests call the production code; `TRANSLATOR_VERSION` bumped if the translator's output changes (rule 27) - [x] Comments say what is true now and name functions, not line numbers (rule 3) - [x] Nothing from the title, and no pointers to where non-free data can be found (hard constraints)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 06:37:11 +00:00
Author
Owner

@⁠vktory6 on GitHub · 2026-12-02 15:28 UTC · original

Thanks, reusing retired streaming images is one of the anti-stutter items ("Placed image creation in the streaming phase"), and the logic mirrors main's. Please:

  • Split out the format-properties cache. It is small and mergeable on its own.
  • One clear path. The pool path now has a second copy of the clear block; keep one (rule 33).
  • Derive or justify the caps. The 128 / 32 MiB / 8 MiB caps are tuned constants (rule 9b).
  • Rebase onto #192 once it merges. #192 changes the signature of clear_compressed.
  • Measure. It needs an A B B A boat run (stream-in) and a picture check: a pooled image keeps stale contents if any fill is partial.
**@⁠vktory6** on GitHub · 2026-12-02 15:28 UTC · [original](https://github.com/vktory6/VKtory/pull/190#issuecomment-7350388683) Thanks, reusing retired streaming images is one of the anti-stutter items ("Placed image creation in the streaming phase"), and the logic mirrors main's. Please: - **Split out the format-properties cache.** It is small and mergeable on its own. - **One clear path.** The pool path now has a second copy of the clear block; keep one (rule 33). - **Derive or justify the caps.** The 128 / 32 MiB / 8 MiB caps are tuned constants (rule 9b). - **Rebase onto #192** once it merges. #192 changes the signature of `clear_compressed`. - **Measure.** It needs an A B B A boat run (stream-in) and a picture check: a pooled image keeps stale contents if any fill is partial.
Author
Owner

@⁠firefloc on GitHub · 2026-12-02 17:32 UTC · original

The pool test at 9182f44 checks bookkeeping with dummy handles. A readback regression would cover the ownership change: fill a texture with pattern A, retire it, verify that the next texture reuses the same VkImage, then partially fill it with pattern B. At the first consumer, compare every accessible mip and layer against a fresh-image control and check the expected texel values independently. Filled texels should contain B, and holes and unheld mips should have the clear value. Include a deferred first fill followed by a retry. The RGBA8 case can stand on its own; add a BC case with Img::base > 0 under Vulkan validation after #192. This tests that the shared clear path covers what the first fill leaves behind without erasing newly written texels.

I exercised the readback assertions with four simulated inputs. They accept clean reuse and reject a pool miss, a stale mip, and identically wrong output in both arms. Connecting that check to Vulkan readback remains open.

One detail for the boat benchmark: #226's 0.03 ms for vkCreateImage + memory is the total in one window with 22 new images. Upload and fill preparation took 0.37 ms in that window. Both came from serialized profiling on the house scene. I would report pool hits, creation time, first-fill time, and frame-time tails separately on the boat.

**@⁠firefloc** on GitHub · 2026-12-02 17:32 UTC · [original](https://github.com/vktory6/VKtory/pull/190#issuecomment-7353412264) The pool test at `9182f44` checks bookkeeping with dummy handles. A readback regression would cover the ownership change: fill a texture with pattern A, retire it, verify that the next texture reuses the same `VkImage`, then partially fill it with pattern B. At the first consumer, compare every accessible mip and layer against a fresh-image control and check the expected texel values independently. Filled texels should contain B, and holes and unheld mips should have the clear value. Include a deferred first fill followed by a retry. The RGBA8 case can stand on its own; add a BC case with `Img::base > 0` under Vulkan validation after #192. This tests that the shared clear path covers what the first fill leaves behind without erasing newly written texels. I exercised the readback assertions with four simulated inputs. They accept clean reuse and reject a pool miss, a stale mip, and identically wrong output in both arms. Connecting that check to Vulkan readback remains open. One detail for the boat benchmark: #226's 0.03 ms for `vkCreateImage + memory` is the total in one window with 22 new images. Upload and fill preparation took 0.37 ms in that window. Both came from serialized profiling on the house scene. I would report pool hits, creation time, first-fill time, and frame-time tails separately on the boat.
Author
Owner

@⁠firefloc on GitHub · 2026-12-02 18:50 UTC · original

The RGBA8 reuse/readback test now passes on 9182f44 on RX 7900 XTX / RADV. I verified the old pattern across all ten mips, retired the image, and checked that the pool returned the same VkImage. After the partial fill, the first readback matched the fresh-image control and the expected values: the two unfilled mips were zero and the other eight contained the new pattern. Final run: 1 passed, 0 failed.

The test uses a direct Vulkan copy for readback. This covers the RGBA8 fixture. The application Execute reader, Vulkan validation, and the BC nonzero-base case after #192 remain open.

**@⁠firefloc** on GitHub · 2026-12-02 18:50 UTC · [original](https://github.com/vktory6/VKtory/pull/190#issuecomment-7355241710) The RGBA8 reuse/readback test now passes on `9182f44` on RX 7900 XTX / RADV. I verified the old pattern across all ten mips, retired the image, and checked that the pool returned the same `VkImage`. After the partial fill, the first readback matched the fresh-image control and the expected values: the two unfilled mips were zero and the other eight contained the new pattern. Final run: 1 passed, 0 failed. The test uses a direct Vulkan copy for readback. This covers the RGBA8 fixture. The application `Execute` reader, Vulkan validation, and the BC nonzero-base case after #192 remain open.
This pull request has changes conflicting with the target branch.
  • greenfield/vkback/src/imgmake.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/190/head:github-bot/gh-190'
git switch 'github-bot/gh-190'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-190'
git switch 'github-bot/gh-190'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-190'
git switch 'github-bot/gh-190'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-190'
git switch 'main'
git merge --squash 'github-bot/gh-190'
git switch 'main'
git merge --ff-only 'github-bot/gh-190'
git switch 'main'
git merge 'github-bot/gh-190'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.