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.

| improve this answer | |
  • \$\begingroup\$ Gah. Of course the profile I'm using is all "to-be-written" \$\endgroup\$ – Jay Dec 3 '12 at 18:29
  • \$\begingroup\$ Woot! The documentation on wpos is pretty thin. I passed in the inverse of the screen size to the shader to calculate the offset into the texture array like this: float Zs = tex2D( DepthTexture, float2( scr.x * iViewWidth, 1 - ( scr.y * iViewHeight ) ) ); \$\endgroup\$ – Jay Dec 4 '12 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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