Screen coordinates in shader

How can i calculate screen position of a vertex
in vertex shader?[HLSL, GLSL]

In GLSL i used gl_FragCoord.xy , but i got very poor speed!

Umm… screen position of a vertex in vertex shader AND gl_FragCoord confuse me.

Anyway: screen pos is postTransformVertexCoord.xy/postTransformVertexCoord.w. +some scaling and remaping as you need it.

Your solution look’s not right!

I will tell what i’am doing in details:
Scene is draws into two passes:

First pass i draw into a texture, for each pixel i get some value, as a result I obtain the following:
Image
And in the second pass I need to read values of the pixel i’am rendering.

Thanks, i have done it!

Just like 1024, I’m also skeptical about the use of gl_FragCoord in a vertex shader. On the other hand, I had the same problem and I fixed it by using the following:

screenCoord = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position = screenCoord;
screenCoord /= screenCoord.w; // perspective divide
screenCoord.x = (screenCoord.x+1.)*fViewport[2]/2. + fViewport[0]; // viewport transformation
screenCoord.y = (screenCoord.y+1.)*fViewport[3]/2. + fViewport[1]; // viewport transformation

Unfortunately, the viewport isn’t directly accessible from the shader and you need to pass the parameters. It’s just redoing what the vertex postprocessing would do.

Originally posted by SKoder:
[b]How can i calculate screen position of a vertex
in vertex shader?[HLSL, GLSL]

In GLSL i used gl_FragCoord.xy , but i got very poor speed![/b]
Starting with Catalyst 4.8 referencing the fragment position with a non-zero polygon offset resulted in software rendering. I contacted ATI about the problem and got them to admit that using x,y, or w should not require software rendering. Only z is a problem. A bug fix request was entered.

vince
I don’t understand what is a viewport transformation!

What values should i put in fViewport

Also I have tryed another way:

In vertex shader:

vec4 texpos = pos * gl_ModelViewProjectionMatrix;

and in pixel shader:

vec4 color = texture2DProj(TexSample, texpos);

but I get next:
Image