In HLSL pixel shader , why is SV_POSITION different to other semantics?

In my HLSL pixel shader, SV_POSITION seems to have different values to any other semantic I use. I don't understand why this is. Can you please explain it?

For example, I am using a triangle with the following coordinates:

(0.0f, 0.5f)
(0.5f, -0.5f)
(-0.5f, -0.5f)

The w and z values are 0 and 1, respectively.

This is the pixel shader.

struct VS_IN
{
    float4 pos : POSITION;
};

struct PS_IN
{
    float4 pos : SV_POSITION;
    float4 k : LOLIMASEMANTIC;
};

PS_IN VS( VS_IN input )
{
    PS_IN output = (PS_IN)0;
    output.pos = input.pos;
    output.k = input.pos;
    return output;
}

float4 PS( PS_IN input ) : SV_Target
{
    // screenshot 1
    return input.pos;

    // screenshot 2
    return input.k;
}

technique10 Render
{
    pass P0
    {
        SetGeometryShader( 0 );
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetPixelShader( CompileShader( ps_4_0, PS() ) );
    }
}

Screenshot 1

Screenshot 2

When I use the first statement (result is first screenshot), the one that uses the SV_POSITIONsemantic, the result is completely unexpected and is yellow, whereas using any other semantic will produce the expected result. Why is this?

directx hlsl fragment-shader

shareimprove this question

edited Nov 26 '12 at 9:58

Laurent Couvidou

8,23623254

asked Nov 25 '12 at 12:09

tina nyaa

133116

add a comment

1 Answer

activeoldestvotes

11

 

SV_Position gives you the position in screen coordinates, not in a [0,1] range though but basically in pixel coordinates. The range will correspond to the D3D11_VIEWPORT you set, possibly something along the lines of:

D3D11_VIEWPORT viewport = {0};
viewport.Width = 1280;
viewport.Height = 720;

So in order to get a [0,1] range again, for the colors, you could do:

return float4(input.pos.r/1280, input.pos.g/720, 0, 1);

shareimprove this answer

edited May 18 '13 at 2:59

answered Nov 25 '12 at 16:30

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值