How to pass a Texture2D to a CompositorEffect shader?

Godot Version

Windows 10, v4.3.stable.official [77dcf97d8]

Question

Does anyone know how the Compositor works?

I’m trying to read a Texture2D in a Compositor shader, a simple noise texture, but this data size error keeps happening.

E 0:00:01:0598   effect_compositor.gd:118 @ create_channel_texture(): Data for slice index 0 (mapped to layer 0) differs in size (supplied: 1398100) than what is required by the format (1048576).
  <C++ Error>    Condition "(uint32_t)p_data[i].size() != required_size" is true. Returning: RID()
  <C++ Source>   servers/rendering/rendering_device.cpp:722 @ texture_create()
  <Stack Trace>  effect_compositor.gd:118 @ create_channel_texture()
                 effect_compositor.gd:208 @ _render_callback()

Based in some tutorials on the matter, I am correctly converting the texture to the right format, but looks like it’s not working. My function for creating the RID textures:

...

var device: RenderingDevice

func create_channel_texture(texture: Texture2D) -> RID:
	if not device:
		return RID()

	var image: Image = texture.get_image()
	image.convert(Image.FORMAT_RF)

	var format: RDTextureFormat = RDTextureFormat.new()
	format.width = image.get_width()
	format.height = image.get_height()
	format.format = RenderingDevice.DATA_FORMAT_R32_SFLOAT
	format.usage_bits = RenderingDevice.TEXTURE_USAGE_SAMPLING_BIT

	return device.texture_create(format, RDTextureView.new(), [image.get_data()])

...

I need help so bad, documentation simply do not exist on this.

For any person looking for it

There is a very comprehensive blog post I founded on Textures and Compute Shaders in Godot 4 made by @leopeltola on it, this helped.

But for my error, I use Image.clear_mipmaps(), I don’t needed.

Make sure that the minimaps parameters in the Image and RDTextureFormat are the same too!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.