# How to get pixel coordinates between 0,1 from SV_POSITION [duplicate]

I am new to HLSL and Directx. I was trying to write a simple gradient Shader by modifying one of Microsoft samples.

1.I can not understand why SV_POSITION is float4? If it is pixel coordinates shouldn't it have only x and y ? What are the 4 parts? I think SV_POSITION is not between 0,1 and the maximum val is bufferWidth,bufferHeight ... then how can I get the position between 0,1?

1. The first code does not work because distance between float2 and float4 is impossible. While the second one works I get a warning. How can I avoid the warning message because I believe something is wrong!

Fist code (Of course it should not work):

float4 VS( float4 Pos : POSITION ) : SV_POSITION
{

return Pos;
}

float4 PS(   float4 Pos : SV_POSITION) : SV_Target
{
float distfromcenter=distance(float2(0.5f, 0.5f), Pos);
float4 rColor = lerp(float4(0,0,0,1),float4(1,1,1,1), saturate(distfromcenter));
return rColor;
}


The second one :

float4 VS( float4 Pos : POSITION ) : SV_POSITION
{

return Pos;
}

float4 PS(   float2 Pos : TEXTCOORD  ) : SV_Target
{
float distfromcenter=distance(float2(0.5f, 0.5f), Pos);
float4 rColor = lerp(float4(0,0,0,1),float4(1,1,1,1), saturate(distfromcenter));
return rColor;
}


Why do I get this warning ? How can I compile this Shader using SV_POSITION to avoid the warning message ?

D3D11 ERROR: ID3D11DeviceContext::Draw: Vertex Shader - Pixel Shader linkage error: Signatures between stages are incompatible. The input stage requires Semantic/Index (TEXTCOORD,0) as input, but it is not provided by the output stage. [ EXECUTION ERROR #342: DEVICE_SHADER_LINKAGE_SEMANTICNAME_NOT_FOUND]

• It is an array/structure of 4 elements as an optimization. It is the same as why some APIs represent vectors as arrays of 4 floats even if the last element is not used for anything. Maybe somebody can post a link to a good explanation of why. I could not find one in my bookmarks. – Hatoru Hansou Jan 2 '14 at 12:03
• BTW, of course the second one doesn't work because the pixel shader expects an input called TEXTCOORD and the vertex shader does not provide that (which is exactly what the warning is telling you). But that has nothing to do with SV_POSITION. – Nathan Reed Jan 2 '14 at 16:54
• @HatoruHansou: The GPU only works with registers of 4-component vectors. "Libraries" use four components because the fourth component is used for something: gamedev.stackexchange.com/questions/17987/… – Sean Middleditch Jan 3 '14 at 2:09
• @Nathan Reed So in order to avoid the warning I must extract x,y position from SV_POSITION but the problem is that the values in SV_POSITION are not in [0,1] ! They are [0, bufferWidth] x [0, bufferHeight] range ! How can I map that into [0f,1f] ? – user999792 Jan 3 '14 at 20:30
• @user999792 Uhh...divide by bufferWidth and bufferHeight. – Nathan Reed Jan 3 '14 at 21:12

I'm new too in directx and not very good in english so i havent understand your question at all but...

Try to change the pixel shader like this:

float4 PS(float4 Pos:SV_POSITION):SV_Target //pos contain x,y,z,w but you only need x,y
{                             //remember PS is called for each pixel and so it must now the pixel-screen position
pixelPosX=Pos[0]; pixelPosY=Pos[1];//theese are the pixel coordinates in the window start from top-left corner and are integer ones(ex. 100-200, 0-0, window_width, window_height)
...
}


If the backbuffer width_height are different from window ones, i dont know if the max Pos[0] value( and Pos[1] too) is the window width or the bck buffer one.

Hope that it works.

Look the comment.

#define MAX_X 640
#define MAX_Y 400

float4 PS(float4 Pos:SV_POSITION):SV_TARGET
{
int x=Pos[0]/MAX_X;
.....
}

• This wont work ! The SV_POSITION values are between Width and height not [0,1] while the code is intended to work with pixel position between [0,1] – user999792 Jan 3 '14 at 20:28
• You can obtin pixel position between [0,1] just with pos.x/max_x and pos.y/max_y. Es. pos.x=120; max_x=640; pos.x in float= 120/640=0.19; – Liuka Jan 4 '14 at 11:15
• I knew ... But max_x should be sent to shader with cbuffer ? – user999792 Jan 4 '14 at 16:51
• Yes, or define in it if the buffer's height-width is constant – Liuka Jan 5 '14 at 11:07