-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Give Resource
s/RefCounted
s a proper way to react to NOTIFICATION_PREDELETE
#11774
Comments
Are you sure this is needed? These high-level types like |
If we had a higher level API for compute shaders that would automatically manage resources, then I'd say this is not needed. Given the precedents, that is likely how this would be resolved. I've also wished for some kind of notification that I could use after an object has been freed to release associated resources. Now thinking about it... Perhaps we can have a I know this is not about
Addendum: I have posted a proof of concept at #7441 (comment) - I made using a |
It does seem to free the texture itself, but it does not affect e.g. the shader and pipeline instances that are needed to run the compute process (although I think the latter gets freed when the former is). I don't think Godot has a way of detecting those, so you would have to take care of them manually. |
Describe the project you are working on
A volumetric sky renderer using ray marching. An efficient way to implement this is by computing both the sky and the clouds in compute shaders that output to one or multiple look-up textures (represented by
TextureRD2D
s), which are then combined and sent to a sky shader for rendering. This is necessary because Godot's sky shader does not allow for certain performance tricks like staggered rendering using interpolation. An example on how it can be done is found here.Describe the problem or limitation you are having in your project
Godot's philosophy of composition implies that abstract
Node
s andResource
s should have as little cross-dependence as possible. It is therefore reasonable to have all the code relevant for setting up and running a compute shader be local to theTextureRD
(e.g.TextureRD2D
) that it is supposed to output to.However, working with compute shaders requires that the user correctly frees all the relevant
RID
s at the end of theTextureRD
's lifetime to avoid memory leaks. But whileResource
s can initialize theseRID
s throughRenderingDevice
at the beginning (e.g. using_ready()
), they can't free them at the end because listening toNOTIFICATION_PREDELETE
does not work for any objects inheriting fromRefCounted
. While this behavior was initially reported as a bug, judging from the comments by @reduz et al. it seemed to be intentional or at leastWONTFIX
at the time.But a lot of time has passed since that report and
TextureRD
is now a thing, which means that any suchResource
has to be permanently associated with aNode
object that manually frees the RIDs on its behalf. This ultimately unnecessary cross-dependence does not only contradict Godot's aforementioned philosophy, but it is also highly error-prone, especially sinceNode
-Resource
relationships can often change during refactoring.Describe the feature / enhancement and how it helps to overcome the problem or limitation
The aforementioned "bug" should be fixed, and the usage of
NOTIFICATION_PREDELETE
to define a destructor should be better documented (especially in the context ofTextureRD
). This would solve the aforementioned issues and inform people new to usingTextureRD
s about the practice.Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
N/A
If this enhancement will not be used often, can it be worked around with a few lines of script?
Yes, but doing so would violate Godot's core principles and could easily lead to memory leaks in large codebases.
Is there a reason why this should be core and not an add-on in the asset library?
The proposal depends on fixing a "bug" in core.
Addendum: It should be noted that one can work around this "bug", e.g. by not invoking any functions defined in the custom
Resource
/RefCounted
(accessing user-defined variables is fine though). However this is undefined behavior and not guaranteed to be possible indefinitely.The text was updated successfully, but these errors were encountered: