# shader coding: calculate screen coordinates of fragment

Good morning, I'm new to shader coding and trying to implement some visual effects code in shaders using billboards. (Yes, I couldn't have picked anything harder to start with, but I'm lucky that way)

Setup: I have rendered the full screen z depth to an array of floats in a previous pass. In the fragment shader I need the scene depth where the rendered fragment is displayed (to see if it's occluded). I can use tex2d() to get the depth value if I have the screen coordinates of the point being rendered in the fragment shader.

Question: In the fragment shader how do you calculate the screen coordinates of the pixel (in the range 0-1.0)?

Is the position passed to the fragment shader a pixel offset? If so, I guess it would be: float2( position.x / screen-width, position.y / screen-height )

Thanks for any help/

CG (as your tags indicate) supports the WPOS semantic in some profiles, for example this one. An input parameter bound to the WPOS semantic will get the window position (with the origin in the lower-left) of the fragment.
Other languages, like GLSL, have similar predefined uniforms or inputs (gl_FragCoord for example), although the specifics of their value ranges may vary.