Skip to content

Compute Texture Demo is Brittle + Proposed Fix #1152

Open
@EnlightenedOne

Description

@EnlightenedOne

Which demo project is affected:
water_plane

OS/device including version:
Godot v4.4.dev.mono (e9504b662) - Windows 10.0.19045 - Multi-window, 1 monitor - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3070 Laptop GPU (NVIDIA; 31.0.15.3742) - 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz (16 threads)

Issue description:
The demo code is too brittle to use as reference material. A scene change or scene reload cleans up the texture but does not rebind it. If you add another blank scene to the demo, open the main scene and look at the pretty texture, change tab to the new scene then reopen the main scene the texture will be blank and you wont see any ripples.

I traced the issue to the Texture2DRD not surviving the scene change/reload event. You can fix it by doing the following:

func _ready():
	next_texture = 0;
	t = 0.0
... Replace the get texture with:
			# Set our texture object.
			material.set_shader_parameter("effect_texture", texture);

At the end of the exit tree request ready refire so that the scene will rebuild the freed textures:

func _exit_tree():
...
	request_ready()

Finally make the new Texture2DRD object in _initialize_compute_code:

func _initialize_compute_code(init_with_texture_size):
	# As this becomes part of our normal frame rendering,
	# we use our main rendering device here.
	rd = RenderingServer.get_rendering_device()
	
	# Create a new texture
	texture = Texture2DRD.new()

With these changes I can smoothly reload a scene and not have the compute shader fail.

Another QoL side note, include RenderingDevice.TEXTURE_USAGE_CAN_COPY_FROM_BIT on the tf usage bits so you can preview the texture in the editor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions