|
|
|
|
|
|
// From deferred or compute shader |
|
|
|
// depth must be the depth from the raw depth buffer. This allow to handle all kind of depth automatically with the inverse view projection matrix. |
|
|
|
// For information. In Unity Depth is always in range 0..1 (even on OpenGL) but can be reversed. |
|
|
|
void UpdatePositionInput(float depth, float4x4 invViewProjectionMatrix, float4x4 ViewProjectionMatrix, inout PositionInputs posInput) |
|
|
|
// It may be necessary to flip the Y axis as the origin of the screen-space coordinate system |
|
|
|
// of Direct3D is at the top left corner of the screen, with the Y axis pointing downwards. |
|
|
|
void UpdatePositionInput(float depth, float4x4 invViewProjectionMatrix, float4x4 ViewProjectionMatrix, |
|
|
|
inout PositionInputs posInput, bool flipY = false) |
|
|
|
// TODO: Do we need to flip Y axis here on OGL ? |
|
|
|
posInput.positionCS = float4(posInput.positionSS.xy * 2.0 - 1.0, depth, 1.0); |
|
|
|
float4 hpositionWS = mul(invViewProjectionMatrix, posInput.positionCS); |
|
|
|
float2 screenSpacePos; |
|
|
|
screenSpacePos.x = posInput.positionSS.x; |
|
|
|
screenSpacePos.y = flipY ? 1.0 - posInput.positionSS.y : posInput.positionSS.y; |
|
|
|
|
|
|
|
posInput.positionCS = float4(screenSpacePos * 2.0 - 1.0, depth, 1.0); |
|
|
|
float4 hpositionWS = mul(invViewProjectionMatrix, posInput.positionCS); |
|
|
|
posInput.positionWS = hpositionWS.xyz / hpositionWS.w; |
|
|
|
|
|
|
|
// The compiler should optimize this (less expensive than reconstruct depth VS from depth buffer) |
|
|
|