Godot Version
4.2.2-stable
Question
I am trying to build a compute shader to retrieve pixel color values from a noise-generated image using GLSL and Godot 4.2. The image itself is a 3840 x 2160 and formatted in RG8. I would expect buffered result array to be of size 8,294,400 float values (only reading one channel). However, when I check the output, I only get 2,073,600 - exactly a quarter - back. I’m really new to working with GLSL and shaders in general, so I would be grateful for pointers and insight and what could be going on.
The shader looks as follows:
#[compute]
#version 450
// Instruct the GPU to use 8x8x1 = 64 local invocations per workgroup.
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
layout(rg8, binding = 0) restrict uniform readonly image2D heightmap;
layout(set=0, binding = 1, std430) restrict buffer ColorValues
{
float[] result;
}
colorValues;
void main() {
// from https://stackoverflow.com/questions/49335851/compute-shader-gl-globalinvocationid-and-local-size
const uint clusterSize = gl_WorkGroupSize.x * gl_WorkGroupSize.y * gl_WorkGroupSize.z;
const uvec3 linearizeInvocation = uvec3(1, clusterSize, clusterSize * clusterSize);
uint clusterId = int(dot(gl_GlobalInvocationID, linearizeInvocation));
// Grab the current pixel's position from the ID of this specific invocation ("thread").
ivec2 coords = ivec2(gl_GlobalInvocationID.xy);
vec4 pixel = imageLoad(heightmap, coords);
float channelValue = pixel.r;
colorValues.result[clusterId] = new_z;
}
And this is how I retrieve and format the image in Godot:
# read image from noise generated texture of size 3840x2160.
var _map_noise_image = _map_image.texture.noise.get_image(
_map_image.texture.get_width(),
_map_image.texture.get_height(), true)
# conversion to RG8
_map_noise_image.convert(Image.FORMAT_RG8)