Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Confused About gl_FragCoord use with Textures

  1. #1
    Intern Contributor
    Join Date
    Sep 2007
    Location
    Southern California
    Posts
    60

    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.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Dec 2007
    Location
    Hungary
    Posts
    985
    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.
    Disclaimer: This is my personal profile. Whatever I write here is my personal opinion and none of my statements or speculations are anyhow related to my employer and as such should not be treated as accurate or valid and in no case should those be considered to represent the opinions of my employer.
    Technical Blog: http://www.rastergrid.com/blog/

  3. #3
    Intern Contributor
    Join Date
    Sep 2007
    Location
    Southern California
    Posts
    60
    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?
    Last edited by Rennie Johnson; 06-19-2012 at 10:57 AM. Reason: One More Question

  4. #4
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,290
    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.

    Code :
    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.

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Dec 2007
    Location
    Hungary
    Posts
    985
    Quote Originally Posted by Ilian Dinev View Post
    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
    Yes, I forgot about the half pixel offset.
    Disclaimer: This is my personal profile. Whatever I write here is my personal opinion and none of my statements or speculations are anyhow related to my employer and as such should not be treated as accurate or valid and in no case should those be considered to represent the opinions of my employer.
    Technical Blog: http://www.rastergrid.com/blog/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •