Repository navigation
Conversation
|
I confirmed a recoverable allocation failure in the current-head path. The pool stores readback buffers by exact size. When a request misses its size bucket and A bounded fix is to reclaim the idle readback buffers only for Local candidate verification
Candidate diffdiff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs
@@
fn take(&mut self, device: usize, queue_type: i32, size: u64) -> (Option<S>, Option<B>) {
@@
(set, buffer)
}
+
+ fn drain_buffers(&mut self) -> BTreeMap<(usize, u64), Vec<B>> {
+ self.buffer_bytes = 0;
+ std::mem::take(&mut self.buffers)
+ }
@@
static DOWNLOAD_POOL: Mutex<DownloadPool<(DownloadCommands, ID3D12Device), (ID3D12Resource, ID3D12Device)>> =
Mutex::new(DownloadPool::new());
+fn reclaim_idle_download_buffers<S, B>(pool: &Mutex<DownloadPool<S, B>>) {
+ let buffers = {
+ let mut pool = pool.lock().unwrap();
+ pool.drain_buffers()
+ };
+ drop(buffers);
+}
+
+fn retry_after_oom<T, E>(
+ mut create: impl FnMut() -> Result<T, E>,
+ is_oom: impl Fn(&E) -> bool,
+ reclaim: impl FnOnce(),
+) -> Result<T, E> {
+ match create() {
+ Err(error) if is_oom(&error) => {
+ reclaim();
+ create()
+ }
+ result => result,
+ }
+}
@@
Some((readback, _)) => readback,
None => {
let mut readback = None;
- dev.CreateCommittedResource(
- &D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_READBACK, ..Default::default() },
- D3D12_HEAP_FLAG_NONE,
- &D3D12_RESOURCE_DESC {
- Dimension: D3D12_RESOURCE_DIMENSION_BUFFER,
- Width: readback_size,
- Height: 1,
- DepthOrArraySize: 1,
- MipLevels: 1,
- SampleDesc: windows::Win32::Graphics::Dxgi::Common::DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
- Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
- ..Default::default()
- },
- D3D12_RESOURCE_STATE_COPY_DEST,
- None,
- &mut readback,
- ).map_err(|e| e.to_string())?;
+ retry_after_oom(
+ || {
+ readback = None;
+ dev.CreateCommittedResource(
+ &D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_READBACK, ..Default::default() },
+ D3D12_HEAP_FLAG_NONE,
+ &D3D12_RESOURCE_DESC {
+ Dimension: D3D12_RESOURCE_DIMENSION_BUFFER,
+ Width: readback_size,
+ Height: 1,
+ DepthOrArraySize: 1,
+ MipLevels: 1,
+ SampleDesc: windows::Win32::Graphics::Dxgi::Common::DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
+ Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
+ ..Default::default()
+ },
+ D3D12_RESOURCE_STATE_COPY_DEST,
+ None,
+ &mut readback,
+ )
+ },
+ |e| e.code() == windows::core::HRESULT(crate::util::E_OUTOFMEMORY),
+ || reclaim_idle_download_buffers(&DOWNLOAD_POOL),
+ ).map_err(|e| e.to_string())?;
readback.ok_or("no readback resource")?
}
@@
fn download_pool_caps_idle_sets_and_readback_bytes_independently() {
@@
assert_eq!(pool.buffer_bytes, 2 * size);
}
+ #[test]
+ fn download_pool_releases_other_size_buckets_before_retrying_out_of_memory() {
+ struct ReclaimProbe {
+ pool: std::sync::Weak<Mutex<DownloadPool<(), ReclaimProbe>>>,
+ released: Arc<AtomicBool>,
+ lock_free: Arc<AtomicBool>,
+ }
+ impl Drop for ReclaimProbe {
+ fn drop(&mut self) {
+ let lock_free = self.pool.upgrade().is_some_and(|pool| pool.try_lock().is_ok());
+ self.lock_free.store(lock_free, Ordering::SeqCst);
+ self.released.store(true, Ordering::SeqCst);
+ }
+ }
+
+ let pool: Arc<Mutex<DownloadPool<(), ReclaimProbe>>> = Arc::new(Mutex::new(DownloadPool::new()));
+ let released = Arc::new(AtomicBool::new(false));
+ let lock_free = Arc::new(AtomicBool::new(false));
+ let (set, buffer) = pool.lock().unwrap().put(
+ 1,
+ 0,
+ DOWNLOAD_POOL_BYTES,
+ None,
+ ReclaimProbe { pool: Arc::downgrade(&pool), released: Arc::clone(&released), lock_free: Arc::clone(&lock_free) },
+ );
+ assert!(set.is_none());
+ assert!(buffer.is_none());
+ let mut attempts = 0;
+ let result = retry_after_oom(
+ || {
+ attempts += 1;
+ if released.load(Ordering::SeqCst) { Ok(42) } else { Err(crate::util::E_OUTOFMEMORY) }
+ },
+ |error| *error == crate::util::E_OUTOFMEMORY,
+ || reclaim_idle_download_buffers(&pool),
+ );
+ assert_eq!(result, Ok(42));
+ assert_eq!(attempts, 2);
+ assert!(released.load(Ordering::SeqCst));
+ assert!(lock_free.load(Ordering::SeqCst), "COM buffers must be released after dropping the pool lock");
+ let pool = pool.lock().unwrap();
+ assert!(pool.buffers.is_empty());
+ assert_eq!(pool.buffer_bytes, 0);
+ }
+
+ #[test]
+ fn download_pool_does_not_reclaim_or_retry_non_out_of_memory_errors() {
+ let pool = Mutex::new(DownloadPool::<(), i32>::new());
+ assert_eq!(pool.lock().unwrap().put(1, 0, 0x10000, None, 7), (None, None));
+ let mut attempts = 0;
+ let mut reclaimed = false;
+ let result: Result<(), _> = retry_after_oom(
+ || {
+ attempts += 1;
+ Err(crate::util::E_INVALIDARG)
+ },
+ |error| *error == crate::util::E_OUTOFMEMORY,
+ || {
+ reclaimed = true;
+ reclaim_idle_download_buffers(&pool);
+ },
+ );
+ assert_eq!(result, Err(crate::util::E_INVALIDARG));
+ assert_eq!(attempts, 1);
+ assert!(!reclaimed);
+ assert_eq!(pool.lock().unwrap().buffer_bytes, 0x10000);
+ }
+
#[test]
fn download_plans_use_writer_capable_queue_types() {AI use: Codex (GPT-6.2 Sol) prepared and tested this candidate; GPT-6.2 Astra independently reviewed the reclaim, lock, and retry path. |
3e00f2b to
58815f7
Compare
|
Thanks, that is a real hole: with up to 1 GiB idle in other size buckets, an allocation that would have succeeded before the pool can now fail and leave the texture's CPU copy stale. Taken in 58815f7 (folded into the PR's commit): on Rerun on the new head (RTX 5070 Ti): AI use: Claude Code, Claude Fable 5.2 wrote the change and this comment and ran the tests. |
|
@BBareth I tested completed texture-download reuse on my RTX 3080 10 GB (driver 616.92). I added one test fixture on top of After fence value 1, all 4,096 texels read back as An independent repeat in a fresh process reproduced both values and the same object reuse. Both runs passed with zero skips. This confirms the reused buffer returned fresh contents on this GPU. I also ran the CPU retry test with its stand-in allocator. A real D3D12 out-of-memory failure remains untested. I used Codex GPT-6.2 Sol to add the fixture and run the first test. Codex GPT-6.1 Terra independently reviewed and repeated it. Codex GPT-6.2 Astra drafted this comment. I reviewed and approved this comment before posting. |
|
merge-check claim |
|
merge-check
No difference above the run-to-run spread (the two A runs differ by 157 CPU-s). This covers the boot and the loading screen only, not the world, where the description measured the gain; I do not know how many texture downloads this phase makes, so it shows no cost, not the absence of the gain. |
|
Test report for the intermittent failure noted in the merge check above (
AI use: Claude Code, Claude Fable 5.2 ran the tests and wrote this report. |
|
merge-check AI-written and posted by Foniass without a human check of each point. |
|
No open or merged PR pools texture-download command objects or readback buffers (#3389, #3549, #3550 touch the same download path for other reasons). AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human. |
|
On the triage note above: the description now states the memory cost (idle readback buffers, up to the 1 GiB limit, stay committed for the life of the process and are released only when a new readback buffer runs out of memory). The two misplaced comments (above AI use: Claude Code, Claude Fable 5.2 wrote this comment. |
bmjask
left a comment
There was a problem hiding this comment.
Review of 58815f7c: 3 finding(s). 3 are inline.
These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
| .map(|()| readback) | ||
| }; | ||
| // The idle buffers are dropped after the pool's lock is released. | ||
| let reclaim = || { |
There was a problem hiding this comment.
The pool's up to 1 GiB of committed readback memory is only released when XCL's own readback CreateCommittedResource returns E_OUTOFMEMORY. The title's allocations, and XCL's CreateCommandAllocator/CreateCommandList/CreateFence failures, never reclaim it. A title that streams many texture downloads fills the pool to ~1 GiB of READBACK commit (the PR's smoke runs show peak commit +1.1-1.4 GB). When the title's CreateCommittedResource/CreateHeap later hits the commit limit, it gets E_OUTOFMEMORY while 1 GiB of idle XCL readback memory sits in DOWNLOAD_POOL, so the title fails or crashes where main had the memory free. The buffer side also never evicts and matches sizes exactly (~3712), so once the cap fills with sizes no longer requested, new sizes get no reuse but the stale 1 GiB stays. An idle-time or LRU trim, and releasing the pool on any E_OUTOFMEMORY XCL sees, would bound this.
There was a problem hiding this comment.
Addressed in 60f0498. A readback buffer that no download took for 2 s is now released (DOWNLOAD_POOL_IDLE_US, checked a few times a second as download fences complete; test download_pool_releases_buffers_idle_past_the_limit). What stays committed is the buffers of sizes downloads asked for in the last 2 s. It is no longer up to 1 GiB held for the rest of the run. The out-of-memory retry stays for XCL's own readback creation. I did not hook the release into the title's allocations, because the idle release already bounds the held memory to what is in use. Smoke on this head: peak commit 32252 MB against main 078358a's 32465 MB (blocks in the description).
AI use: Claude Code, Claude Opus 5.5 wrote this reply.
| commands.fence_value = value; | ||
| (commands, device.clone()) | ||
| }); | ||
| let rejected = DOWNLOAD_POOL.lock().unwrap().put(key, queue_type, readback_size, set, (readback, device)); |
There was a problem hiding this comment.
Not certain about this one: idle entries keep a strong device reference and are never evicted by device, so after device removal or recreation the old device, its command sets and up to 1 GiB of buffers stay pinned and use up the global caps. If device A is removed and the title recreates B, DOWNLOAD_POOL still holds A's sets and buffers, which take() never returns for B's key. sets.len() stays at 1024 and buffer_bytes near the cap, so put() rejects every B entry: B gets no pooling and A's memory is never released. Dropping a device's entries when it's removed, or when a different device starts using the pool, would fix it.
There was a problem hiding this comment.
Checked: this does not happen in XCL. A process has one D3D12 device (device_raw()). When it is removed, stop_if_device_removed (real.rs) logs the reason and ends the process with TerminateProcess, so there is no device B that a stale device A's entries could crowd out. The entries are still keyed by device, so a second device would get no wrong objects, only fewer pooled ones. No change made.
AI use: Claude Code, Claude Opus 5.5 wrote this reply.
| let readback = readback.ok_or("no readback resource")?; | ||
| let readback_size = download_buffer_size(bytes).ok_or("readback buffer size overflow")?; | ||
| let (pooled_set, pooled_buffer) = DOWNLOAD_POOL.lock().unwrap().take(dev.as_raw() as usize, queue_type.0, readback_size); | ||
| let (commands, readback) = own_work(|| { |
There was a problem hiding this comment.
Not certain about this one: object creation in prepare_texture_download is now wrapped in own_work, so the allocator, command list and readback buffer silently stop being adopted (is_own_object), a behaviour change the PR doesn't mention or test. On main, prepare_downloads runs outside own_work, so the CreateCommandAllocator/CreateCommandList/CreateCommittedResource hooks adopted these objects. For example, the adopted list's Close hook logs failures with what was recorded. Now a Close failure on the download list isn't logged by that hook, and any tracking the hooks did for these objects stops.
There was a problem hiding this comment.
Right that these objects are no longer adopted. I checked what adoption did for them. The one effect that mattered is the list's Close hook logging a failure, and 60f0498 now logs a failed Close of the download list itself (D3D12.X: texture download list Close → …). The rest of the hooks' tracking is for title objects. These are XCL's own, never handed to the title, and released by XCL, so there is nothing for it to track. The description now says so.
AI use: Claude Code, Claude Opus 5.5 wrote this reply.
|
@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any. |
…wnloads Texture download preparation repeatedly creates the same D3D12 objects during streaming. Reuse each allocator, command list and private fence by device and queue type. Reuse committed readback buffers separately by device and 64 KiB-rounded size. Return objects only after their private fence confirms GPU completion. Cap idle storage at 1,024 command sets and 1 GiB of readback buffers. Each idle entry owns a device reference, so its raw device address cannot be reused while the entry is cached. A readback buffer no download took for 2 s is released (checked a few times a second when fences complete), so sizes no longer asked for do not keep up to 1 GiB committed for the rest of the run. The download list is XCL's own object, so no Close hook logs a failure: Close logs it here. Assisted-by: Codex (GPT-6) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
58815f7 to
60f0498
Compare
|
Pushed 60f0498 (rebased on main 078358a; merges cleanly into 395405e).
AI use: Claude Code, Claude Opus 5.5 wrote this comment. |
|
Follow-up review of In short: The PR reuses the allocator, command list, fence and readback buffer of each texture download. All three points from the triage of
A failed Medium
Nit
Checked and fine
Written by Claude Code (Claude Opus 5.5) and posted by Foniass without a human check of each point. |
A GPU-written title texture is copied back into the title's memory through a readback buffer, a command allocator, a command list and a fence.
prepare_texture_downloadcreated all four for every download and released them when the download was published. A title that has hundreds of such downloads every second pays for that on the thread that signals its queues.This keeps the objects of a finished download and hands them to the next one.
What is in it
crates/xcl/src/hle/d3d12x/title_textures.rs:DownloadPoolholds idle command sets (allocator, list, fence) by device and list type, and idle readback buffers by device and size. Sizes are rounded up to 64 KiB. Limits: 1024 sets and 1 GiB of buffers; anything over is released as before.DOWNLOAD_POOL_IDLE_US) is released. This is checked a few times a second as download fences complete. The pool no longer keeps up to 1 GiB committed for the rest of the run.E_OUTOFMEMORY), the idle buffers are released, outside the pool's lock, and the allocation is tried once more (retry_after_reclaim). Other errors are returned as before.Drop for PendingDownloadreturns the objects once the download's fence has completed; an incomplete one is released, not pooled. A pooled fence continues from its last value and is retired before the value could wrap.Closeof the download list is logged. It is XCL's own list, so no hook logs it.crates/xcl/src/hle/d3d12x/real.rs:create_download_fencemakes the pool's fences without entering them in the title fence registry. They are XCL's own; the title never sees them.download_pool_rounds_readback_sizesdownload_pool_keys_sets_by_queue_type_and_buffers_by_sizedownload_pool_caps_idle_sets_and_readback_bytes_independentlydownload_pool_releases_buffers_idle_past_the_limitdownload_pool_gives_up_its_idle_buffers_for_one_retry_after_out_of_memoryTested
cargo build --workspace, thencargo test --workspace --no-fail-fast -- --test-threads=4on bbcef6c6 (on main 078358a): 3011 passed, 0 failed, 52 ignored.SKIPPED: 22 no wave64, 2 no heap windows, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 driver cache.Regression run
Each run follows one warm-up run of the same build; the A button is pressed once at 70 s.
Not tested
AI use
🤖 Generated with Claude Code