代码改变世界

SV_POSITION

2019-08-17 14:36  kk20161206  阅读(1385)  评论(0)    收藏  举报

SV_POSITION semantic has different meaning when used in vertex and pixel shader respectively. It is a position in clip space in vertex shader and screen space position in pixel shader. So in your pixel shader i.pos.x is in range from 0 to screenWidth and i.pos.y is in range from 0 to screenHeight. SV_POSITION in pixel shader is an replacement for VPOS semantic from the old days. It will only work under DX10 and up or equivalent OpenGL.

 

There it says that ( on the left) after the perspective divide , you get "Normalized Device Coordinates - real" ( and by real I suppose that they are referring to screen size in pixels) , but according to this (section 4.1.10) and this you only get the "real" coordinates after the viewport transform, so in the normalized device coordinates you still have the homogeneous form. Is that correct?

 

// Used for vertex factory shaders which need to use the resolved view
float4 SvPositionToResolvedScreenPosition(float4 SvPosition)
{
	float2 PixelPos = SvPosition.xy - ResolvedView.ViewRectMin.xy;	

	// NDC (NormalizedDeviceCoordinates, after the perspective divide)
	float3 NDCPos = float3( (PixelPos * ResolvedView.ViewSizeAndInvSize.zw - 0.5f) * float2(2, -2), SvPosition.z);

	// SvPosition.w: so .w has the SceneDepth, some mobile code and the DepthFade material expression wants that
	return float4(NDCPos.xyz, 1) * SvPosition.w;
}  

 

上述代码来自ue4Common.ush文件中。结合上面查到的资料,感觉上面代码应该这样解释:在这里Pixel Shader中,SvPosition的含义是屏幕空间坐标。(屏幕空间坐标 - viewRectMin)*zw……也就是代码的前两句是进行viewport空间转换,然后第三句是齐次坐标除法。按照平时的习惯,先透视除法,得到ndc坐标,然后进行viewport转换;这个只是把透视除法的顺序改变了一下。

最上面两条来自链接:https://forum.unity.com/threads/regarding-the-sv_position-input-in-the-fragment-shader.390553/