Skip to content

Repository files navigation

Compute Shader Plus

This Godot 4 plugin adds in a ComputeHelper class that keeps track of compute shaders and their uniforms.

Here's a simple example of a shader that reads and then writes to a texture, making it grayscale (ideally in the render thread). You can find a more thorough explanation in docs/tutorial.md:

varimage:=Image.create(image_size.x, image_size.y, false, Image.FORMAT_RGBAF)
image.fill(Color.RED)
varcompute_shader:=ComputeHelper.create("res://compute-shader.glsl")
varinput_texture:=ImageUniform.create(image)
varoutput_texture:=SharedImageUniform.create(input_texture)
compute_shader.add_uniform_array([input_texture, output_texture])
varwork_groups:=Vector3i(image_size.x, image_size.y, 1)
compute_shader.run(work_groups)
ComputeHelper.sync()
image=output_texture.get_image()

Corresponding shader file:

#[compute]
#version450layout(set =0, binding =0, rgba32f) readonlyuniformimage2D input_texture;
layout(set =0, binding =1, rgba32f) writeonlyrestrictuniformimage2D output_texture;
layout(local_size_x =1, local_size_y =1, local_size_z =1) in;
void main() {
ivec2 id =ivec2(gl_GlobalInvocationID.xy);
vec4 color = imageLoad(input_texture, id);
vec3 grayscale =vec3((color.r + color.g + color.b) /3.0);
imageStore(output_texture, id, vec4(grayscale, 1.0));
}

Demos

I've made a few sample projects that use this plugin:

Planned Additions

There's a few things I'd like to add to this plugin eventually:

  • Basic shader reflection features, such as the ability to get the names of all of the uniforms in a shader, and the ability to set a shader's uniform based off of its name.
  • More thorough documentation. Currently, there is only a basic tutorial, but I'd like to add some other examples, such as using the plugin in a compositor effect, and using it with other RenderingDevice functions.

Other Resources

For more information on compute shaders in Godot 4, here are some useful resources:

And while you're here, here's some similar plugins you might want to look at:

About

A Godot 4 plugin allowing for easy use of compute shaders.

Topics

Resources

Stars

144 stars

Watchers

5 watching

Forks

Releases

Contributors

Languages