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 (ideally in the render thread):
var image := Image.create(image_size.x, image_size.y, false, Image.FORMAT_RGBAF)
image.fill(Color.BLACK)
var compute_shader := ComputeHelper.create("res://compute-shader.glsl")
var input_texture := ImageUniform.create(image)
var output_texture := SharedImageUniform.create(input_texture)
compute_shader.add_uniform_array([input_texture, output_texture])
var work_groups := Vector3i(image_size.x, image_size.y, 1)
compute_shader.run(work_groups)
ComputeHelper.sync()
image = output_texture.get_image()
I've made a few sample projects that use this plugin:
There's a few things I'd like to add to this plugin eventually:
- A new LinkedArrayUniform class. Because arrays are passed by reference, it should be possible to have a class that automatically reads from and updates a given array without the user having to call functions like get_data() or update().
- A more optimized use of uniform sets. From examples I've seen, I know there are times where uniforms and uniform sets can be reused, but I haven't done enough testing to know exactly when, or how I'd want to implement that in this plugin.
- Proper descriptions and warnings. Most functions and classes would benefit from having descriptions. As I've been testing this plugin, I've also found a few edge cases that don't seem to have an obvious fix. For example, the format RGBA8 doesn't work as the format of images passed to compute shaders, and I need a good place to clarify stuff like that.
For more information on compute shaders in Godot 4, here are some useful resources:
- Official Compute Texture Demo Project
- "Godot 4 - Conway's Game Of Life (Full Lesson)" Video Tutorial (Uses C#)
- "Everything About Textures in Compute Shaders!" Article
- "How to use Compute Shaders in Godot 4" Video
And while you're here, here's some similar plugins you might want to look at: