Godot Version
4.4.1stable, running on windows pc
Question
I’m trying to fill an empty Texture3D in a compute shader
Here is a minimal reproduction of the GDscript code i’m using:
extends Node
const IMAGE_FORMAT: Image.Format = Image.FORMAT_R8
@export_file("*.glsl") var shader_path: String
@export var resolution: int = 0
var resoult: Texture3D
func _ready() -> void:
resoult = compute(resolution)
func compute(res: int) -> Texture3D:
var rd: RenderingDevice = RenderingServer.create_local_rendering_device()
var shader_file: RDShaderFile = load(shader_path)
var shader_spirv: RDShaderSPIRV = shader_file.get_spirv()
var shader: RID = rd.shader_create_from_spirv(shader_spirv)
#OUTPUT TEXTURE
var tex_format := RDTextureFormat.new()
tex_format.format = RenderingDevice.DATA_FORMAT_R8_UNORM
tex_format.width = res
tex_format.height = res
tex_format.depth = res
tex_format.usage_bits = \
RenderingDevice.TEXTURE_USAGE_SAMPLING_BIT +\
RenderingDevice.TEXTURE_USAGE_STORAGE_BIT +\
RenderingDevice.TEXTURE_USAGE_CAN_COPY_FROM_BIT
tex_format.texture_type = RenderingDevice.TEXTURE_TYPE_3D
var texture: RID = rd.texture_create(tex_format, RDTextureView.new())
var uniform_out := RDUniform.new()
uniform_out.uniform_type = RenderingDevice.UNIFORM_TYPE_IMAGE
uniform_out.binding = 0
uniform_out.add_id(texture)
var uniform_set: RID = rd.uniform_set_create([uniform_out], shader, 0)
var pipeline: RID = rd.compute_pipeline_create(shader)
var compute_list: int = rd.compute_list_begin()
rd.compute_list_bind_compute_pipeline(compute_list, pipeline)
rd.compute_list_bind_uniform_set(compute_list, uniform_set, 0)
var workgroups: int = ceili(res/8.0)
rd.compute_list_dispatch(compute_list, workgroups, workgroups, workgroups)
rd.compute_list_end()
rd.submit()
rd.sync()
var data: Array[Image] = RenderingServer.texture_3d_get(texture)
print(rd.texture_is_valid(texture))
var tex := ImageTexture3D.new()
tex.create(IMAGE_FORMAT, res, res, res, false, data)
rd.free_rid(shader)
rd.free_rid(texture)
rd.free_rid(uniform_set)
rd.free_rid(pipeline)
rd.free()
return tex
And the compute shader:
#[compute]
#version 450
layout(local_size_x = 8, local_size_y = 8, local_size_z = 8) in;
layout(r8, binding = 0) restrict uniform image3D output_texture;
void main() {
imageStore(output_texture, ivec3(gl_GlobalInvocationID), vec4(0.5,0.0,0.0,0.0));
}
But when I run the project, it returns the following error:
E 0:00:01:140 node.gd:61 @ compute(): Parameter “tex” is null.
<C++ Source> servers/rendering/renderer_rd/storage_rd/texture_storage.cpp:1514 @ texture_3d_get()
node.gd:61 @ compute()
node.gd:11 @ _ready()
and a nothing is returned.
It seems like it’s impossible for it to retrieve the data from the texture, but I have no idea at all why it isn’t working
How can I fix this?
Thanks in advance