/compute-shader-plus

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

Primary LanguageGDScriptMIT LicenseMIT

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 (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()

Demo

I've made a demo showing how to use this plugin. It's a slime mold simulation, similar to Sebastian Lague's Slime Simulation. It performs multiple passes of compute shaders on a texture every frame. Hopefully it helps in understanding how to use the plugin.

Planned Additions

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 where it doesn't work properly, not because the plugin itself is broken, but because of limitations of Vulkan/Godot. For example, the format RGBA8 doesn't work as the format of images passed to compute shaders, and it would be helpful to clarify that somewhere.

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: