Confused About gl_FragCoord use with Textures

I don’t understand what value range glFragCoord returns when accessing a texture.

I’m writing a color bayer demosaicing shader for raw digital camera photos, and I need to get the actual original image pixel location (based on the texture size) to determine whether I’ve accessed a red, green or blue pixel in the bayer pattern.

Example: The source texture is 2048 pixels wide and 1536 pixels high, so I need to know where the current fragment is located relative to this scale.

To be overly redundant, I want to get the x value in the range of 0-2047 and the y value in the range of 0-1535.

By the way, I’m using standard sample2D uniforms, NOT sampler2DRect.

I appreciate your help.

gl_FragCoord doesn’t “return” anything when accessing a texture. It is a built-in available in the fragment shader that tells you the coordinates of the currently processed fragment. These coordinates are screen space coordinates thus depend on your viewport size. It’s a fixed number inside a single fragment shader invocation, no texture fetch changes it’s value.
Thus gl_FragCoord.x’s range is [0…w), gl_FragCoord.y’s range is [0…h) where w and h are the width and height of your viewport, respectively.

Thanks aqnuep. Let me further define my application. The fragment shader is rendering to an FBO, not a window. So assume these conditions:

My source texture is 2048 wide and 1536 high.
My FBO is 1024 wide by 768 high.
When rendering to the FBO, The original texture is being randomly scaled down with glScale() and glTranslate() to fit into the smaller FBO resolution.

If I understand you correctly, gl_FragCoord.x is going to be 0-1023 and gl_FragCoord.y will be 0-767 in my example, because that is the fragment position relative to the FBO. Is that correct?

So it is correct that the only way I can get the original texture coordinate of the source image is to make my FBO identical in resolution (without scaling or translation ) to the source texture resolution?

Is there a better way I can determine, either in a Vertex or Fragment shader the X/Y position of the current fragment?

Just one other thing: I read in another post that gl_FragCoord actually contains the value range of 0.5,05. to (w+0.5,y+0.5). In my example that would be x range of 0.5-1023.5 and y range of 0.5-767.5. Could this be true?

it’s 0.5 to (including) w - 1.0 + 0.5
so a 16x16 fbo will see gl_FragCoord.x = 0.5, and gl_FragCoord.x = 15.5

Is there a better way I can determine, either in a Vertex or Fragment shader the X/Y position of the current fragment?

Relearn what a “fragment” and “texture-coordinate” is, you’re mixing-up the terms constantly, introducing confusion.

What you need seems to be: when drawing a fullscreen quad (that does not move), to compute for a given destination fragment coordinate (a read-only value provided by gl_FragCoord) , a source texture-coordinate.


vec2 texcoord = ((gl_FragCoord.xy - adjust.xy) / scale.xy) / textureSize(sampler0); 

vec4 color = texture2D(sampler0, texcoord);

Everyone else does the more sane approach to transform some constant geometry that also has a constant texcoord attrib, by a matrix in the vertex-shader, and pass the texcoord as a varying.

[QUOTE=Ilian Dinev;1239031]it’s 0.5 to (including) w - 1.0 + 0.5
so a 16x16 fbo will see gl_FragCoord.x = 0.5, and gl_FragCoord.x = 15.5[/QUOTE]
Yes, I forgot about the half pixel offset.

what is adjust.xy and scale.xy ?