Skip to content

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

License

Notifications You must be signed in to change notification settings

DevPoodle/compute-shader-plus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

var image := Image.create(image_size.x, image_size.y, false, Image.FORMAT_RGBAF)
image.fill(Color.RED)

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

Corresponding shader file:

#[compute]
#version 450

layout(set = 0, binding = 0, rgba32f) readonly uniform image2D input_texture;
layout(set = 0, binding = 1, rgba32f) writeonly restrict uniform image2D 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:

  • 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().
  • The ability to use push constants for compute shaders.

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

License

Stars

Watchers

Forks