1
\$\begingroup\$

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]
\$\endgroup\$

marked as duplicate by Nathan Reed, Seth Battin, bummzack, MichaelHouse Jan 7 '14 at 22:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ 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. \$\endgroup\$ – Hatoru Hansou Jan 2 '14 at 12:03
  • \$\begingroup\$ 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. \$\endgroup\$ – Nathan Reed Jan 2 '14 at 16:54
  • 1
    \$\begingroup\$ @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/… \$\endgroup\$ – Sean Middleditch Jan 3 '14 at 2:09
  • \$\begingroup\$ @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] ? \$\endgroup\$ – user999792 Jan 3 '14 at 20:30
  • \$\begingroup\$ @user999792 Uhh...divide by bufferWidth and bufferHeight. \$\endgroup\$ – Nathan Reed Jan 3 '14 at 21:12
0
\$\begingroup\$

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
Vertex shader....

float4 PS(float4 Pos:SV_POSITION):SV_TARGET
{
 int x=Pos[0]/MAX_X;
 .....
}
\$\endgroup\$
  • \$\begingroup\$ 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] \$\endgroup\$ – user999792 Jan 3 '14 at 20:28
  • \$\begingroup\$ 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; \$\endgroup\$ – Liuka Jan 4 '14 at 11:15
  • \$\begingroup\$ I knew ... But max_x should be sent to shader with cbuffer ? \$\endgroup\$ – user999792 Jan 4 '14 at 16:51
  • \$\begingroup\$ Yes, or define in it if the buffer's height-width is constant \$\endgroup\$ – Liuka Jan 5 '14 at 11:07

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