godot: `RenderingDevice` cannot release texture resource when window is minimized
Tested versions
4.3.stable.mono
System information
Godot v4.3.stable.mono - Windows 10.0.22631 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4070 (NVIDIA; 31.0.15.3161) - AMD Ryzen 9 7900X 12-Core Processor (24 Threads)
Issue description
When the game window is minimized, the texture created by RenderingDevice.create_texture() won’t be freed by calling RenderingDevice.free_rid().
when the game window is not minimized
when the game window is minimized after a while
BTW, the Memory inspected by Editor Debugger grows much slower than inspected by Windows Task Manager, when this bug is triggered.
not minimized
minimized after a while
Steps to reproduce
extends Node2D
var rd : RenderingDevice;
var texture : RID;
func _ready() -> void:
rd = RenderingServer.get_rendering_device()
func _process(delta: float) -> void:
#RenderingServer.call_on_render_thread(create_texture);
create_texture()
func create_texture() -> void:
if texture.is_valid():
rd.free_rid(texture)
var format = RDTextureFormat.new()
format.width = 1024
format.height = 1024
format.depth = 1
format.format = RenderingDevice.DATA_FORMAT_R8G8B8A8_UNORM
format.array_layers = 1
format.mipmaps = 1
format.usage_bits = RenderingDevice.TEXTURE_USAGE_SAMPLING_BIT | RenderingDevice.TEXTURE_USAGE_CAN_UPDATE_BIT | RenderingDevice.TEXTURE_USAGE_STORAGE_BIT
texture = rd.texture_create(format, RDTextureView.new(), [])
- This script will create a texture pre frame and release the texture created last frame.
- Attach it to a node and run the scene.
- Minimize the game window (not editor). (just hide this window below other windows won’t trigger this bug)
Minimal reproduction project (MRP)
See ‘Steps to reproduce’
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (14 by maintainers)
Commits related to this issue
- Keep processing Graphics if there are pending operations Fixes #90017 Fixes #90030 Fixes #98044 This PR makes the following changes: # Force processing of GPU commands for frame_count frames The v... — committed to darksylinc/godot by darksylinc 2 years ago
- Keep processing Graphics if there are pending operations Fixes #90017 Fixes #90030 Fixes #98044 This PR makes the following changes: # Force processing of GPU commands for frame_count frames The v... — committed to tGautot/godot by darksylinc 2 years ago
- Keep processing Graphics if there are pending operations Fixes #90017 Fixes #90030 Fixes #98044 This PR makes the following changes: # Force processing of GPU commands for frame_count frames The v... — committed to tGautot/godot by darksylinc 2 years ago
- Keep processing Graphics if there are pending operations Fixes #90017 Fixes #90030 Fixes #98044 This PR makes the following changes: # Force processing of GPU commands for frame_count frames The v... — committed to JackErb/godot by darksylinc 2 years ago
Adding public methods to certain classes is usually done with extreme care because these classes are exposed to users either via GDScript or Extensions and require documentation. It also presents a commitment to support this new method in future versions and we must make sure to not break compatibility if possible.
If we come to situations like this where there’s no other solution, we can discuss it and add the method, but it’s not usually something that we should think of as the first solution if possible.
As for free pending resources, it’s unlikely we’d ever want to expose that. That’s an internal implementation detail that end users should not concern themselves with, as from their perspective, resources are deleted when they call free().
When the free_rid() method is called the resources are not released immediately, but are queued to be freed in the next frame, but when the window is minimized the gpu cycle is paused which means it doesn’t process frames anymore, this causes resources to be queued but never released. And when the window is opened up again it starts to process the frames and clears out the queue.
As a test, I added this inside add_frame_delay method in os.cpp,
It solved this problem, #90030 and also #90017, but I’m not sure what would be the proper implementation because _flush_and_stall_for_all_frames is a private method (I changed it to public for testing) and calling it every frame might be problematic.
Resources are not freed until there’s a guarantee the GPU is no longer executing the frame. It only knows that if it has to wait for the fence, which happens when it wants to begin drawing on that frame in the queue. Therefore destruction of resources is delayed until that frame begins recording.
Calling _flush_and_stall_for_all_frames() does indeed fix most of the issue as the trick the method does is wait on the current frame to finish executing and begins the next one.
We’re indeed lacking a way to force this. There’s other methods such as
buffer_get_data()that will also trigger it, but we don’t have a method publicly exposed for it.sync()is the closest we have, but the use ofsubmit()andsync()is reserved for Local Devices only.It’d seem the only way to go about it is making the sync() method accepted or to add a different method altogether.