godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.14k stars 21.19k forks source link

Creating Detail Albedo texture in GDScript causes infinite textures being created. #56004

Open Gyrth opened 2 years ago

Gyrth commented 2 years ago

Godot version

v3.4-stable

System information

Manjaro Linux, GLES3, NVIDIA Geforce GTX 1060 495.44

Issue description

In my script I create a detail texture in the _ready function for bullet holes. It works flawlessly but I noticed the object id of the image object increase steadily. So this might be a memory leak, or needless swapping of textures in the engine.
Peek 2021-12-17 00-33

Steps to reproduce

  1. Open reproduction project.
  2. Run project.
  3. Click on the object in Material slot 0.
  4. Click the object in Detail Albedo.
  5. See the object id of the Image increase.

Minimal reproduction project

BugReport.zip

Zylann commented 2 years ago

I think this is actually a side-effect of remote inspector, I have seen this in the past. It is trying to access image but for some reason I think it re-creates the object client-side just to provide it, but in reality it is not cached. Similarly if you try to access image in script it might also create a new image each time because it does not actually store such object. Only the data inside is stored. In fact image is not even a documented property, I think it only exists because of serialization purposes.

You can also see some of that in master, the data is cached as a Ref<Image> only in some conditions in editor: https://github.com/godotengine/godot/blob/ed395c6b99915809347a87b0d65220c256d6ec3f/servers/rendering/renderer_rd/renderer_storage_rd.cpp#L1032-L1040

Then in all the other cases, it gets the data probably from the graphics card or whatever cache if any, but because it gets it as raw bytes, it has to wrap it inside an Image object: https://github.com/godotengine/godot/blob/ed395c6b99915809347a87b0d65220c256d6ec3f/servers/rendering/renderer_rd/renderer_storage_rd.cpp#L1041-L1044 Which explains why you may get a different instance of an Image object each time.

Gyrth commented 2 years ago

Thank you for checking it out. So is this considered a bug worth fixing?

Zylann commented 2 years ago

I'm not sure it is even a bug, it looks like it works as intended, as confusing as it looks. I wonder why it shows there in the first place. Also I have a vague memory that this was raised before, but I can't find the issue if any.

Some other slightly related stuff from the past: https://github.com/godotengine/godot/issues/18801#issuecomment-388607918