Skip to content

Conversation

@RT2Code
Copy link
Contributor

@RT2Code RT2Code commented Oct 10, 2025

This PR follows up on #8963 (comment). It reuses the upload buffer whenever possible, recreating it only when necessary. It also leaves it mapped until destruction to avoid the overhead of repeated Map()/Unmap() calls. This is both safe and a recommended best practice:

Resources on CPU-accessible heaps can be persistently mapped, meaning Map can be called once, immediately after resource creation. Unmap never needs to be called, but the address returned from Map must no longer be used after the last reference to the resource is released. When using persistent map, the application must ensure the CPU finishes writing data into memory before the GPU executes a command list that reads or writes the memory.

It is safe and recommended to keep buffers persistently mapped if they need mapping, to avoid the overhead of calling Map()/Unmap() many times.

I benchmarked it with the following code, and verified that the texture was reuploaded every frame:

    io.Fonts->TexMinWidth = 64;
    io.Fonts->TexMinHeight = 64;
    io.Fonts->TexMaxWidth = 256;
    io.Fonts->TexMaxHeight = 256;
    
    int i = 0;
    
    [main_loop]
    
    ImGui::PushFont(ImGui::GetFont(), (float)(10 + (i++ % 10))); // Lord have mercy on this atrocity
   
    [...]
    
    ImGui::PopFont();

Before:
before_release

After:
after_release

I know this benchmark is unrealistic, and in real use the performance difference is likely negligible since the texture doesn’t change that often. But it's still more efficient, and since it doesn’t really make the code more complex, I think this is a worthwhile improvement.

@RT2Code RT2Code changed the title Backends: D3d12: Reuse upload buffer and grow it only when necessary Backends: Dx12: Reuse upload buffer and grow it only when necessary Oct 11, 2025
@RT2Code RT2Code changed the title Backends: Dx12: Reuse upload buffer and grow it only when necessary Backends: DX12: Reuse upload buffer and grow it only when necessary Oct 11, 2025
ocornut pushed a commit that referenced this pull request Oct 13, 2025
@ocornut
Copy link
Owner

ocornut commented Oct 13, 2025

Thanks a lot! This makes sense!
Merged as 9be4f15 ( I have renamed the fields to be more fully qualified, m_UploadSize -> m_UploadBufferSize etc.)

@ocornut ocornut closed this Oct 13, 2025
@RT2Code RT2Code deleted the upload-buffers branch October 13, 2025 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants