Compute shaders on images, confused

Started by
2 comments, last by fries 5 years, 1 month ago

I'm a bit overwhelmed learning this stuff, but i was looking at a piece of code and was super confused how the compute shader was executing over the data.

On the CPU side they had:
 


	public void render()
	{
		shader.bind();
		bitReversedSSBO.bindBufferBase(1);
		shader.updateUniforms(N);
		glBindImageTexture(0, texture.getHandle(), 0, false, 0, GL_WRITE_ONLY, GL_RGBA32F);
		glDispatchCompute(log_2_N,N/16,1);	
	}
	

Then the compute shader in simple terms has:
 


void main(void)
{
	vec2 x = gl_GlobalInvocationID.xy;
    imageStore(texture, ivec2(x),vec4(0,1,0.25,1) );
}

So how does the compute shader know how many invocation Ids to run for this image? Does it just instinctively know that it has to do it for the full image dimensions ? But if that is true what if you had multiple images ? Does it just then go up to what ever the largest image is... but that would then cause accessing invalid pixels on smaller images...

What if you only wanted the top quarter of the image to change... i am very confused how the Global ID is working here to coincide with image pixels.

In short is the vector2 ranging from [0,0] to [imageWidth-1, imageHeight-1] ? If not how are they informing the compute shader how many pixels to process on the image?

Hope you can clear up my confusions here, thanks.

Advertisement

That's determined by parameters in the glDispatchCompute() call :)

.:vinterberg:.

The work group size will be defined in the compute shader itself, something like this:


layout (local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

Then the dispatch call from C/C++ has this prototype:


void glDispatchCompute(GLuint num_groups_x, GLuint num_groups_y, GLuint num_groups_z);

So when you call the dispatch function, it will make 'num_groups_x * num_groups_y * num_groups_z' groups, and the size of the group (number of threads) is 'local_size_x * local_size_y * local_size_z'.

This topic is closed to new replies.

Advertisement