In my 2D game, I render tiles. I want to do lighting without needing an extra buffer the size of the screen.

Either in glsl or hlsl, what could I feed to the shader each time I render a bitmap so that it knows what pixel on the scren it will fall on. That way I can do per pixel lighting.

Currently I only get the UV coord relative to the texture, not the screen.


Using ps 2.0

  • \$\begingroup\$ Does gl_FragCoord not work for you? \$\endgroup\$ Aug 19, 2012 at 17:51

3 Answers 3


If it's ps3+, use vPos in assembly or VPOS/SV_POSITION in HLSL.

If you don't want to use ps3+, then you need to pass, in another texcoord, the position before it gets mangled into clipspace.


struct output{
   float4 Position : SV_POSITION;
   float4 Fake_Position : TEXCOORD0;
VShader(youstruct input)
    a = input.Position * World
    b = a * View;
    c = b * Projection;

    output.Position = c;
    output.fake_Position = b;

In HLSL you can use VPOS or SV_Position to get the screen space position in the pixel shader.

  • 1
    \$\begingroup\$ Unfortunately I need a ps_2.0 solution. \$\endgroup\$
    – jmasterx
    Aug 19, 2012 at 17:54
  • 2
    \$\begingroup\$ If you're rendering a quad it's possible - you just need to work out at draw call time the required transform from UV space to screen space and pass that to the shader in a constant. \$\endgroup\$
    – Adam
    Aug 19, 2012 at 17:58

In the vertex shader, you could augment the output so that it contains a texture coordinate. That coordinate should be the vertex's world position, projected into screen space by world and project matrices. This would be identical to what the rasterizer does for its own purposes, except you can use it in your pixel shader.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .