Imagine I have two uncompressed, 2D textures A and B with identical sizes and formats. I bind A to texture unit 0, and B to a framebuffer object.

If I draw a full-size polygon in that framebuffer with this pixel shader:

uniform sampler2D iChannel0;
out vec4 fragColor;
void main()
   fragColor = texelFetch(iChannel0, gl_FragCoord.xy);

Is this guaranteed to make a perfect copy of texture A?

ie. Will gl_FragCoord.xy give good numbers that map every input texel to every output texel? Will there be any filtering or clamping applied to the texels?

Edit: It should be

fragColor = texelFetch(iChannel0, ivec2(gl_FragCoord.xy), 0);

1 Answer 1


You should take a look at the layout qualifier identifiers (see here). The usual range is [0.5, 1023.5] for a viewport size of 1024, So it is the Pixel center. But when casting it to ivecX to hand it over to the texelfetch command the Pixel positions is correct. Texelfetch returns you the raw data, without filtering, because the input position is in integer form.

But you need other states to be set correctly like viewport size, depth test, stancil test, blend mode, culling...

There is the third parameter missing at the texelfetch command in your shader. It is the level of detail (mipmap level) this should be 0.

  • $\begingroup$ I assumed it would be vanilla 2D textures.... $\endgroup$ May 9 at 21:26
  • $\begingroup$ Yes, it works for 2D textures.... Your texelFetch position would then be an ivec2 datatype $\endgroup$
    – Thomas
    May 10 at 7:18
  • $\begingroup$ Or was it an indication of the viewport range? $\endgroup$
    – Thomas
    May 10 at 7:20
  • $\begingroup$ Yes, it seems to work... $\endgroup$ May 12 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.