metal: per-encoder residency (VICTORY_METAL_RESIDENCY_SETS=0) names all 30k+ heap allocations one-by-one with Read|Write on every pass #382

Closed
opened 2026-11-27 15:27:38 +00:00 by Sheffy007 · 2 comments
Sheffy007 commented 2026-11-27 15:27:38 +00:00 (Migrated from github.com)
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T15:27:38Z.


Refs #79, #8, #258

Problem

When MTLResidencySet is off (VICTORY_METAL_RESIDENCY_SETS=0, and the default on 16–24 GB Macs), world frames spend 12 to 24 s per frame in per-encoder useResource overhead even though total GPU shader execution is ~1.6 s over 120+ s (#79):

  1. Unscoped full-heap naming on bounded passes (crates/victory-metal/src/gfx.rs use_heap_encoder): Every compute and render encoder unconditionally walks Heap::alloc_counts to name all ~30,000–68,000 heap allocations, even when the pass's translated shaders do not index ResourceDescriptorHeap dynamically (!heap) and only access a few fixed-offset Root::Table entries (or no resource-heap entries at all). In #79's measurement, ~29,600 of ~30,300 heap textures are never in any bound table.
  2. One-by-one useResource calls over a HashMap: allocations.values().for_each(|(_, r)| use_res(r)) issues 30,000–68,000 separate Objective-C useResource: calls per encoder instead of submitting cached contiguous slices via Metal's batch useResources:count:usage:.
  3. MTLResourceUsage::Read | MTLResourceUsage::Write on heap.buf and read-only textures: bind_root_compute and bind_root_render mark &heap.buf and all ~30,000 read-only sampled textures (!MTLTextureUsage::ShaderWrite) as Write on every pass, forcing IOGPUFamily hazard tracking (when !set_on_queue) to serialize false write-after-write dependencies across the entire heap between encoders.

Plan

  1. Exact DXIL createHandleFromHeap / annotateHandle analysis in Binding::of (crates/victory-gpu/src/native/list.rs): Inspect the emitted DXIL bitcode alongside HostShader::user_data (keeping warm shader caches valid without touching victory-shader) to classify each stage's resource-heap accesses as either exact bounded entries (Table(slot, offset, writes), pool-offset Pairs(writes), Const(index, writes), or none) or FullHeap { writes } (conservative fallback for bindless ResourceDescriptorHeap, dynamic/per-lane table offsets, and raw untranslated DXIL).
  2. Pass HeapAccess on Dispatch / Draw (crates/victory-backend, crates/victory-gpu/src/native/host.rs): Resolve each command's bounded heap entry ranges (or FullHeap { writes }) against the bound tables and view pairs.
  3. Scoped & batched useResources with accurate Read vs Read | Write usage (crates/victory-metal/src/gfx.rs):
    • Split Heap allocations into cached contiguous read_only vs shader_writable (MTLBuffer or MTLTextureUsage::ShaderWrite) slices for FullHeap passes, naming &heap.buf and read-only textures as Read and writable allocations as Read | Write only when an active command in the encoder can write to the heap.
    • For bounded passes, merge the encoder's accessed heap entry intervals and submit only their deduplicated Heap::refs allocations via batched useResources.
    • Leave MTLResidencySet mode (residency_sets == true) untouched.
  4. Tests: Metal GPU execution tests with with_residency_sets(false) covering bounded table passes over heaps populated with thousands of unrelated textures, read-after-write UAV hazard ordering across passes, Lower::memory / root_views pair ranges, TypeTextureBuffer views, dynamic/bindless FullHeap shaders, and stage scoping.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6649385585 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/382). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T15:27:38Z. --- Refs #79, #8, #258 ## Problem When `MTLResidencySet` is off (`VICTORY_METAL_RESIDENCY_SETS=0`, and the default on 16–24 GB Macs), world frames spend 12 to 24 s per frame in per-encoder `useResource` overhead even though total GPU shader execution is ~1.6 s over 120+ s (#79): 1. **Unscoped full-heap naming on bounded passes (`crates/victory-metal/src/gfx.rs` `use_heap_encoder`):** Every compute and render encoder unconditionally walks `Heap::alloc_counts` to name all ~30,000–68,000 heap allocations, even when the pass's translated shaders do not index `ResourceDescriptorHeap` dynamically (`!heap`) and only access a few fixed-offset `Root::Table` entries (or no resource-heap entries at all). In #79's measurement, ~29,600 of ~30,300 heap textures are never in any bound table. 2. **One-by-one `useResource` calls over a `HashMap`:** `allocations.values().for_each(|(_, r)| use_res(r))` issues 30,000–68,000 separate Objective-C `useResource:` calls per encoder instead of submitting cached contiguous slices via Metal's batch `useResources:count:usage:`. 3. **`MTLResourceUsage::Read | MTLResourceUsage::Write` on `heap.buf` and read-only textures:** `bind_root_compute` and `bind_root_render` mark `&heap.buf` and all ~30,000 read-only sampled textures (`!MTLTextureUsage::ShaderWrite`) as `Write` on every pass, forcing `IOGPUFamily` hazard tracking (when `!set_on_queue`) to serialize false write-after-write dependencies across the entire heap between encoders. ## Plan 1. **Exact DXIL `createHandleFromHeap` / `annotateHandle` analysis in `Binding::of` (`crates/victory-gpu/src/native/list.rs`):** Inspect the emitted DXIL bitcode alongside `HostShader::user_data` (keeping warm shader caches valid without touching `victory-shader`) to classify each stage's resource-heap accesses as either exact bounded entries (`Table(slot, offset, writes)`, pool-offset `Pairs(writes)`, `Const(index, writes)`, or none) or `FullHeap { writes }` (conservative fallback for bindless `ResourceDescriptorHeap`, dynamic/per-lane table offsets, and raw untranslated DXIL). 2. **Pass `HeapAccess` on `Dispatch` / `Draw` (`crates/victory-backend`, `crates/victory-gpu/src/native/host.rs`):** Resolve each command's bounded heap entry ranges (or `FullHeap { writes }`) against the bound tables and view pairs. 3. **Scoped & batched `useResources` with accurate `Read` vs `Read | Write` usage (`crates/victory-metal/src/gfx.rs`):** - Split `Heap` allocations into cached contiguous `read_only` vs `shader_writable` (`MTLBuffer` or `MTLTextureUsage::ShaderWrite`) slices for `FullHeap` passes, naming `&heap.buf` and read-only textures as `Read` and writable allocations as `Read | Write` only when an active command in the encoder can write to the heap. - For bounded passes, merge the encoder's accessed heap entry intervals and submit only their deduplicated `Heap::refs` allocations via batched `useResources`. - Leave `MTLResidencySet` mode (`residency_sets == true`) untouched. 4. **Tests:** Metal GPU execution tests with `with_residency_sets(false)` covering bounded table passes over heaps populated with thousands of unrelated textures, read-after-write UAV hazard ordering across passes, `Lower::memory` / `root_views` pair ranges, `TypeTextureBuffer` views, dynamic/bindless `FullHeap` shaders, and stage scoping.
Sheffy007 commented 2026-11-27 15:27:39 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T15:27:39Z.


/claim

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7222084730 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/382#issuecomment-7222084730). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T15:27:39Z. --- /claim
github-actions[bot] commented 2026-11-27 15:27:50 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-27T15:27:50Z.


Claimed by @Sheffy007. Open a draft pull request with Refs #382 in its description within 24 hours, or the claim expires.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7222088457 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/382#issuecomment-7222088457). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-27T15:27:50Z. --- Claimed by @Sheffy007. Open a draft pull request with `Refs #382` in its description within 24 hours, or the claim expires.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS#382
No description provided.