Please explain UnityPixelSnap in builtin shader

inline float4 UnityPixelSnap (float4 pos)
{
float2 hpc = _ScreenParams.xy * 0.5f;
float2 pixelPos = round ((pos.xy / pos.w) * hpc);
pos.xy = pixelPos / hpc * pos.w;
return pos;
}
What I understand:

  • Input is vertex clip space coordinate.
  • Output is vertex clip space coordinates which is integer after tranform to screen space.

What I am confused about:

The screen space coordinate to which input vertex is transformed:

float2 pixelPos = (pos.xy / pos.w + 1) * 0.5 * _ScreenParams.xy

So to round screen space coordinate, It should be

float roundedPixelPos = round((pos.xy / pos.w + 1) * 0.5 * _ScreenParams.xy)

But in code it is:

round ((pos.xy / pos.w) * 0.5 * _ScreenParams.xy);

So why?

This is old, but I am pretty sure it is because this function outputs a modified clipspace position and NOT a screen space position. This modifies your clipspace position, that is the input, and outputs a clipspace position that will be directly lined up with the screen’s pixels. Therefor no need for the extra math of calculating the modified screenspace position. The GPU will do that when it rasterizes the clipspace position.