Original Post
Used Reconstructing pixel 3D position from depth as a reference. Trying to reconstruct position as MJP proposed - using frustum corner and linear depth. No luck so far. This is what kind of problem I have now:
On the left is a direct output of WorldView transformation, on the right is a reconstructed view position, as you can see they don't match. Depth generation: Position reconstruction: corner_position is a top right corner of frustum. Was calculated this way: farH = 2 * tan(fov/2) * farClip farW = farH * aspect_ratio farX = farW/2 farY = farH/2 corner_position = (farX, farY, farClip)
On the left is a direct output of WorldView transformation, on the right is a reconstructed view position, as you can see they don't match. Depth generation:
Vertex shader
output.clip_pos_depth = mul(input.pos, WorldView);
Pixel shader
float d = input.clip_pos_depth.z;
float4 main_ps(in_ps input, float2 screenPos :VPOS) : COLOR
{
float2 coord = screenPos * pixel_size;
coord += pixel_size * 0.5;
float4 pos_depth = tex2D(positionDepth_sampler, coord);
float depth = pos_depth.a / farClippingPlane; // 1000 is a test value
float3 ViewDir;
ViewDir.x = lerp(-corner_position.x, corner_position.x, coord.x);
ViewDir.y = lerp(corner_position.y, -corner_position.y, coord.y);
ViewDir.z = corner_position.z;
float3 position = ViewDir * depth;
return float4(position.xyz,1);
}