Glsl tutorial vincent houze

I’m leaving a question because there’s something
I don’t understand while watching the Vincent House GLSL lecture(2020).
At the beginning of his lecture
He used this code to convert a compute shader into like a Fragment shader.

layout (local_size_x = 8, local_size_y = 8) in;
void main()

vec2 uv = (gl_GlobalInvocationID.xy+vec2(.5))*uTDOutputInfo.res.xy;

vec4 color;
color = texture(sTD2DInputs[0],uv);
imageStore(mTDComputeOutputs[0], ivec2(gl_GlobalInvocationID.xy), TDOutputSwizzle(color));

And I don’t understand this code

vec2 uv = (gl_GlobalInvocationID.xy+vec2(.5))*uTDOutputInfo.res.xy;

He said when you use texture you need to sample the center of the pixel and so you need to add (.5)

So I don’t know why I have to add 0.5
And I don’t know why I should multiply without dividing.

and sorry about my bad english!

Hey, so the quick answer first. uTDOutputInfo.res.xy holds (1.0 / width, 1.0 / height). We store it that way since multiply is much faster than a divide, so we do the division on the CPU once and that way you can just multiple on the GPU much more quickly.

For the texture coordinates. Think about a 2x1 grid of pixels. Maybe draw it. The left edge of the left pixel is u = 0, the right edge of the right pixel is u=1. The spot between the two pixels is u=0.5. So if you want to sample the middle of the left pixel, you need to sample at u=0.25. 0.25 being 1/2 a pixel width in U space.

In the above code it’s dealing with indices, 0, 1, 2, 3 coming from gl_GlobalInvocationID. Those will yield the left edge of each pixel, unless you add 0.5 to them (before normalizing them). By adding 0.5 you are landing in the middle of the pixel instead.

Hope this helps!


Continuing the discussion from Glsl tutorial vincent houze:

thank you very much malcolm!!