startup-runtime: every non-aliased placed buffer keeps its own committed buffer for the process, and re-placing the same address adds another #3474

Open
opened 2026-11-29 16:27:50 +00:00 by Leyukaka · 3 comments
Leyukaka commented 2026-11-29 16:27:50 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T16:27:50Z.


Commit

main at 36914379. Line numbers below are in src/startup_stubs/d3d12_xs_device.cpp at that commit.

What the code does

  • device_place_resource_x (device slot 133) aliases guest memory only when the offset in the allocation is 64 KiB aligned, the range is covered and the allocation is MEM_PRIVATE (:5933-5936). Otherwise, or when CreatePlacedResource fails, it creates a committed default-heap buffer per call (:5970-5975, logged not aliased). Nothing looks for an earlier buffer at the same address.
  • release() (:1068-1094) removes the placement from the index (retire_placement) and ends its budget use, but keeps the desktop object: "The wrappers and their desktop objects stay alive for the process" (:1045).
  • The one exception, X2W_D3D12_RELEASE_RETIRED=1, is off by default and excludes buffers: retire_classify returns RetireNever::buffer (:889) and publish_placed_buffer sets the same reason (:5767).

D3D12 sizes a committed buffer in 64 KiB units, so each non-aliased placement holds at least 64 KiB until the process exits, whatever its width, and placing the same address again after a Release allocates a second one.

Measured

One long local run of the application (about 20 min), slot 133 not aliased placements, sizes rounded up to 64 KiB:

Process time Non-aliased placed buffers Size
75 s 3,368 1.06 GiB
319 s 9,566 1.46 GiB
928 s 13,576 1.72 GiB
1,237 s 18,969 2.07 GiB

The count did not level off. At the last sample 14,826 of the buffers are 8 bytes wide (0.9 GiB at 64 KiB each), and about 4,000 were created at an address that an earlier placement had already used. Aliased placements stayed at 56. Shorter runs ended near 3,975 buffers (1.1 GiB).

#2379 covers the same retention for committed textures; its retirement slices leave buffers out, and the reason counter there reported them as never-class:buffer (8,178 buffers / 0.55 GB in one run, 5,163 / 339 MiB in another).

Proposal

Aliased placements are not touched.

  1. Reuse. When a placement with the same address, width and flags arrives and the earlier wrapper for it has had its final Release, hand out the earlier committed buffer instead of creating one. To settle in the PR: the reused buffer keeps its previous contents and resource state, where a new one starts zeroed in initial_state.
  2. Release. Free the committed fallback after the final guest Release, through the existing retire path (retire_resource / drain_retired_resources, :6238, :6280): the release-time fence of every queue has passed, no list or submission holds it, and no tracked descriptor names it (retire_view_refs). The rule from #2379 stays: a live descriptor owns the resource until its slot is overwritten or its heap is destroyed. Today retire_hold is taken for copies and texture binds only; a list that resolved a placed buffer by address (vertex, index, root and memory-SRV paths) takes no hold. A buffer would enter the class only where each such use records a hold, and stay retained otherwise. Opt-in, under X2W_D3D12_RELEASE_RETIRED.
  3. Later, optionally: pack small placements into a shared buffer.

Test: a synthetic fixture that places and releases the same small unaligned buffer N times and checks that the number of live committed fallbacks stays bounded, with controls that a buffer named by a recorded or in-flight list, or by a live descriptor, is still readable after its Release, and a mutant that frees early.

Not covered: non-aliased placements that are never released, and the wrapper storage, which stays.

I'm working on this.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6681177815 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/3474). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-29T16:27:50Z. --- ### Commit `main` at `36914379`. Line numbers below are in `src/startup_stubs/d3d12_xs_device.cpp` at that commit. ### What the code does - `device_place_resource_x` (device slot 133) aliases guest memory only when the offset in the allocation is 64 KiB aligned, the range is covered and the allocation is `MEM_PRIVATE` (`:5933-5936`). Otherwise, or when `CreatePlacedResource` fails, it creates a committed default-heap buffer per call (`:5970-5975`, logged `not aliased`). Nothing looks for an earlier buffer at the same address. - `release()` (`:1068-1094`) removes the placement from the index (`retire_placement`) and ends its budget use, but keeps the desktop object: "The wrappers and their desktop objects stay alive for the process" (`:1045`). - The one exception, `X2W_D3D12_RELEASE_RETIRED=1`, is off by default and excludes buffers: `retire_classify` returns `RetireNever::buffer` (`:889`) and `publish_placed_buffer` sets the same reason (`:5767`). D3D12 sizes a committed buffer in 64 KiB units, so each non-aliased placement holds at least 64 KiB until the process exits, whatever its width, and placing the same address again after a Release allocates a second one. ### Measured One long local run of the application (about 20 min), slot 133 `not aliased` placements, sizes rounded up to 64 KiB: | Process time | Non-aliased placed buffers | Size | |---:|---:|---:| | 75 s | 3,368 | 1.06 GiB | | 319 s | 9,566 | 1.46 GiB | | 928 s | 13,576 | 1.72 GiB | | 1,237 s | 18,969 | 2.07 GiB | The count did not level off. At the last sample 14,826 of the buffers are 8 bytes wide (0.9 GiB at 64 KiB each), and about 4,000 were created at an address that an earlier placement had already used. Aliased placements stayed at 56. Shorter runs ended near 3,975 buffers (1.1 GiB). #2379 covers the same retention for committed textures; its retirement slices leave buffers out, and the reason counter there reported them as `never-class:buffer` (8,178 buffers / 0.55 GB in one run, 5,163 / 339 MiB in another). ### Proposal Aliased placements are not touched. 1. **Reuse.** When a placement with the same address, width and flags arrives and the earlier wrapper for it has had its final Release, hand out the earlier committed buffer instead of creating one. To settle in the PR: the reused buffer keeps its previous contents and resource state, where a new one starts zeroed in `initial_state`. 2. **Release.** Free the committed fallback after the final guest Release, through the existing retire path (`retire_resource` / `drain_retired_resources`, `:6238`, `:6280`): the release-time fence of every queue has passed, no list or submission holds it, and no tracked descriptor names it (`retire_view_refs`). The rule from #2379 stays: a live descriptor owns the resource until its slot is overwritten or its heap is destroyed. Today `retire_hold` is taken for copies and texture binds only; a list that resolved a placed buffer by address (vertex, index, root and memory-SRV paths) takes no hold. A buffer would enter the class only where each such use records a hold, and stay retained otherwise. Opt-in, under `X2W_D3D12_RELEASE_RETIRED`. 3. Later, optionally: pack small placements into a shared buffer. Test: a synthetic fixture that places and releases the same small unaligned buffer N times and checks that the number of live committed fallbacks stays bounded, with controls that a buffer named by a recorded or in-flight list, or by a live descriptor, is still readable after its Release, and a mutant that frees early. Not covered: non-aliased placements that are never released, and the wrapper storage, which stays. I'm working on this.
lukaslw-lab commented 2026-11-29 16:57:28 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-29T16:57:28Z.


Same class here, and one data point for proposal 2 before it is built.

On a 360 s startup run I had 9,182 copied 64 KiB placed buffers (0.60 GB by count), close to your 319 s row. I prototyped your step 2 locally: these buffers joined the retire class at final Release, waited for list holds, release fences and retire_view_refs, and were freed by the drain. Result: 19,527 freed over two runs, 0 late reads, no device removal, but no measurable memory change:

Two A/B pairs, 360 s, same build freed DXGI local Δ title commit Δ
pair 1 9,071 −0.06 GB −0.08 GiB
pair 2 10,456 +0.07 GB +0.44 GiB

A fresh buffer freed the same way drops DXGI local at once, so the long-released ones are probably already evicted, and count × 64 KiB may overstate what the process pays. Logging QueryVideoMemoryInfo local / non-local and the process commit next to the count at your 928 s and 1,237 s points would show it: if they grow with the count, step 1 (reuse) is the part that pays off.

On an older main I also listed the readers that bind a copied buffer without a retire_hold (the ExecuteIndirect index view, root CBV/SRV at draw, vertex/index views, copies). I can re-check them on current main and post the list with line numbers.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7259307640 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/3474#issuecomment-7259307640). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-29T16:57:28Z. --- Same class here, and one data point for proposal 2 before it is built. On a 360 s startup run I had 9,182 copied 64 KiB placed buffers (0.60 GB by count), close to your 319 s row. I prototyped your step 2 locally: these buffers joined the retire class at final Release, waited for list holds, release fences and `retire_view_refs`, and were freed by the drain. Result: 19,527 freed over two runs, 0 late reads, no device removal, but no measurable memory change: | Two A/B pairs, 360 s, same build | freed | DXGI local Δ | title commit Δ | |---|---|---|---| | pair 1 | 9,071 | −0.06 GB | −0.08 GiB | | pair 2 | 10,456 | +0.07 GB | +0.44 GiB | A fresh buffer freed the same way drops DXGI local at once, so the long-released ones are probably already evicted, and count × 64 KiB may overstate what the process pays. Logging `QueryVideoMemoryInfo` local / non-local and the process commit next to the count at your 928 s and 1,237 s points would show it: if they grow with the count, step 1 (reuse) is the part that pays off. On an older `main` I also listed the readers that bind a copied buffer without a `retire_hold` (the ExecuteIndirect index view, root CBV/SRV at draw, vertex/index views, copies). I can re-check them on current `main` and post the list with line numbers.
Leyukaka commented 2026-11-29 17:24:42 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T17:24:42Z.


Reuse (step 1): I prototyped it on a branch, not opened as a PR. Our own check of it estimated that it would reuse few buffers: every shim use of a copied fallback goes through placed_buffer_range first, and after that the buffer is not eligible.

A later long local run with the budget line on: local video memory went over budget from about 166 s. At the end textures were about 72% of it, about 6.5 GiB of them retained after the application released them (#2379's area). The 64 KiB placed-buffer fallbacks were about 401 MiB (6,360 retained).

So the placed buffers are a small share of it, which fits your A/B, and I will not open the reuse PR.

A QueryVideoMemoryInfo local / non-local and process commit line next to the buffer count is useful. I will add it as a diagnostic PR (Refs #3474) unless you prefer to do it.

Yes to the re-check of the readers that bind a copied buffer without a retire_hold on current main; please post the list with line numbers here.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7259669628 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/3474#issuecomment-7259669628). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-29T17:24:42Z. --- Reuse (step 1): I prototyped it on a branch, not opened as a PR. Our own check of it estimated that it would reuse few buffers: every shim use of a copied fallback goes through `placed_buffer_range` first, and after that the buffer is not eligible. A later long local run with the budget line on: local video memory went over budget from about 166 s. At the end textures were about 72% of it, about 6.5 GiB of them retained after the application released them (#2379's area). The 64 KiB placed-buffer fallbacks were about 401 MiB (6,360 retained). So the placed buffers are a small share of it, which fits your A/B, and I will not open the reuse PR. A `QueryVideoMemoryInfo` local / non-local and process commit line next to the buffer count is useful. I will add it as a diagnostic PR (Refs #3474) unless you prefer to do it. Yes to the re-check of the readers that bind a copied buffer without a `retire_hold` on current `main`; please post the list with line numbers here.
lukaslw-lab commented 2026-11-30 00:41:03 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-30T00:41:03Z.


Readers of a copied placed buffer (Placement::copied_buffer, found through placed_buffer_range) on main 2ff74a6a, by reading. These are what a copied-buffer retire class would have to hold.

No hold (neither retire_hold nor retain_with_recording):

reader copied buffer taken at bound at
compute root SRV (srv … root) compute_root_srv_buffer, d3d12_compute_dispatch.inl:653-660 (when list_copied_gpu_range) :845; the only holds there are textures (:899) and tokens (:1104)
table buffer inputs, draw and compute indirect_table_buffer, d3d12_draw.inl:2692-2697 d3d12_draw.inl:3482 (buffer_view); d3d12_compute_dispatch.inl:1029, :1235
ExecuteIndirect index view list_execute_indirect, d3d12_xs_device.cpp:4114-4121 (any placement, no alias check) :4165 (IASetIndexBuffer)
ExecuteIndirect argument buffer d3d12_xs_device.cpp:4093-4099 forwarded or translated Dispatch from :4099
memory SRV descriptor (slot 70) placed_as_buffer, d3d12_memory_srv.inl:666-680 :690; a descriptor, so it outlives any one recording: it needs a view ref, not a list hold
acceleration-structure build input resolve_placed_build, d3d12_xs_device.cpp:8365-8373 build at the caller

Already held: fixed vertex views (d3d12_draw.inl:2953), the draw index and table aliases (:3296, :3550, aliased buffers only), slot-57 mesh buffers (native_mesh57.inl:193), and copies (list_copy.inl:282-283).

Go ahead with the QueryVideoMemoryInfo diagnostic PR.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7265487244 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/3474#issuecomment-7265487244). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-30T00:41:03Z. --- Readers of a copied placed buffer (`Placement::copied_buffer`, found through `placed_buffer_range`) on `main` `2ff74a6a`, by reading. These are what a copied-buffer retire class would have to hold. **No hold** (neither `retire_hold` nor `retain_with_recording`): | reader | copied buffer taken at | bound at | |---|---|---| | compute root SRV (`srv … root`) | `compute_root_srv_buffer`, `d3d12_compute_dispatch.inl:653-660` (when `list_copied_gpu_range`) | `:845`; the only holds there are textures (`:899`) and tokens (`:1104`) | | table buffer inputs, draw and compute | `indirect_table_buffer`, `d3d12_draw.inl:2692-2697` | `d3d12_draw.inl:3482` (`buffer_view`); `d3d12_compute_dispatch.inl:1029`, `:1235` | | ExecuteIndirect index view | `list_execute_indirect`, `d3d12_xs_device.cpp:4114-4121` (any placement, no alias check) | `:4165` (`IASetIndexBuffer`) | | ExecuteIndirect argument buffer | `d3d12_xs_device.cpp:4093-4099` | forwarded or translated Dispatch from `:4099` | | memory SRV descriptor (slot 70) | `placed_as_buffer`, `d3d12_memory_srv.inl:666-680` | `:690`; a descriptor, so it outlives any one recording: it needs a view ref, not a list hold | | acceleration-structure build input | `resolve_placed_build`, `d3d12_xs_device.cpp:8365-8373` | build at the caller | **Already held:** fixed vertex views (`d3d12_draw.inl:2953`), the draw index and table aliases (`:3296`, `:3550`, aliased buffers only), slot-57 mesh buffers (`native_mesh57.inl:193`), and copies (`list_copy.inl:282-283`). Go ahead with the `QueryVideoMemoryInfo` diagnostic PR.
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
R0Y00/Xbox2Windows#3474
No description provided.