gfx-rs / gfx

[maintenance mode] A low-overhead Vulkan-like GPU API for Rust.
http://gfx-rs.github.io/
Apache License 2.0
5.35k stars 547 forks source link

Memory leak in DX12 backend when updating texture each frame #2556

Open bgourlie opened 5 years ago

bgourlie commented 5 years ago

Short info header:

I have a WIP renderer for my NES emulator that I derived from the colour_uniform example. When running with the dx12 backend, it leaks ~22mb per second. I found a potentially related (closed) issue here.

You can reproduce using the following steps:

Memory leak can then be observed using the task manager. Also note that you won't see any meaningful output when running this ROM, however if you have a mario bros rom handy you will. There is no memory leak when running using the Vulkan backend.

msiglreith commented 5 years ago

Hey 👋 I'm currently failing to build it (running out of stack memory on my nightly compiler?) and haven't looked too closely on the code so far, but I assume this is most likely due to differences in the command buffer memory allocation behaviors between vulkan and dx12, which we need to address on our side. Btw, the colour-uniform example leaks memory on both backends due to not freeing nor recycling cmd buffers for some reason. The easiest way to avoid any issues is to reuse command buffers and periodically reset the command pool with resource release flag.

bgourlie commented 5 years ago

I was running into that last week but a rustup update fixed it. Also, I've added some commits that have made the branch referenced in the repro steps incompatible with the dx12 backend (actually an HLSL compatibility issue which I do not believe to be a bug, sampling non-float textures), so I've updated the repro steps to reference a particular commit. In hindsight I should have created a branch specifically for the repro!

kvark commented 5 years ago

the colour-uniform example leaks memory on both backends due to not freeing nor recycling cmd buffers for some reason

reason being - it used on gfx-hal recycling the command buffers in the wrong way (without fences and such), and this logic was recently removed for good.

bgourlie commented 5 years ago

I was able to fix the big DX12 memory leak and the much smaller Vulkan memory leak by pre-recording and reusing my command buffers.

I'm still very much a vulkan noob so I'm not sure if my issue (as well as the memory leak in the example) is due to incorrect usage or an actual bug in gfx-hal.