[ad_1]
There’s this Q&A: SV_POSITION in pixel shader, however the reply is incomplete so this query is asking for the entire reply.
What are SV_POSITION.z
and SV_POSITION.w
in a pixel shader and the way are they calculated?
Word: I am not on the lookout for generalities. I am on the lookout for the precise math. Given clip house coordinates p1, p2, p3 for a triangle, a viewport setting, a depth vary setting, then, given any fragment coordinate, what’s the math to compute SV_POSITION.z
and SV_POSITION.w
?
In OpenGL that is spelled out for gl_FragCoord
which appears just like SV_POSITION
however the values in DirectX 12 are completely different sufficient that it isn’t clear how z
and w
are calculated and I’ve had no luck discovering a spec.
The OpenGL math appears to be
-
clip => ndc
which is simply
ndc = clip.xyz/.clip.w
-
ndc => window
which is successfully
window.xy = (ndc.xy * 0.5 + 0.5) * viewport_range + viewport_x window.z = (ndc.z * 0.5 + 0.5) * depth_range + depth_near
Then, for every fragment:
-
compute a barycentric coordinate for fragcoord.xy relative to the second window triangle (so no z)
-
gl_FragCoord.z
= the three window.z values * the barycentric coordinate (ie, linear iterpolation) -
gl_FragCoord.w
= 1 / (the clipspace.w values perspective-interpolated by the barycentric coordinates)
the place perspective-interpolation is outlined as
f = a(fa)/wa + b(fb)/wb + c(fc)/ wc
----------------------------------
a/wa + b/wb + c/wc
the place a, b, c are the barycentric coordinates, wa, wb, wc are the clipspace.w coordinates, and fa, fb, fc are the three values being interpolated. On this case IIUC they’re the identical clipspace.w coords
Doing that math appears to compute the proper gl_FragCoord.zw
values. What’s the equal math for SV_POSITION.zw
?
Word: On DirectX window.z
can be ndc.z * depth_range + depth_near
I feel however simply making that adjustments does not match the SV_POSITION.zw
values I get from rendering.
[ad_2]