Fix shaders for reverse z

This commit is contained in:
ExMatics HydrogenC
2024-06-04 09:30:26 +08:00
parent cde3e3d710
commit eab4a8c404
8 changed files with 10 additions and 10 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ void RenderView::Prepare(RenderContext& renderContext)
void RenderView::PrepareCache(const RenderContext& renderContext, float width, float height, const Float2& temporalAAJitter, const RenderView* mainView)
{
// The same format used by the Flax common shaders and postFx materials
ViewInfo = Float4(1.0f / Projection.M11, 1.0f / Projection.M22, Far / (Far - Near), (-Far * Near) / (Far - Near) / Far);
ViewInfo = Float4(1.0f / Projection.M11, 1.0f / Projection.M22, Near / (Far - Near), (Far * Near) / (Far - Near) / Far);
ScreenSize = Float4(width, height, 1.0f / width, 1.0f / height);
TemporalAAJitter.Z = TemporalAAJitter.X;
+2 -2
View File
@@ -277,8 +277,8 @@ void DepthOfFieldPass::Render(RenderContext& renderContext, GPUTexture*& frame,
cbData.BokehTargetSize.Y = static_cast<float>(bokehTargetHeight);
// TODO: use projection matrix instead of this far and near stuff?
cbData.ProjectionAB.X = farPlane / (farPlane - nearPlane);
cbData.ProjectionAB.Y = (-farPlane * nearPlane) / (farPlane - nearPlane);
cbData.ProjectionAB.X = nearPlane / (farPlane - nearPlane);
cbData.ProjectionAB.Y = (farPlane * nearPlane) / (farPlane - nearPlane);
auto cb = shader->GetCB(0);
context->UpdateCB(cb, &cbData);
+1 -1
View File
@@ -396,7 +396,7 @@ bool GBufferPass::IsDebugView(ViewMode mode)
void GBufferPass::SetInputs(const RenderView& view, GBufferData& gBuffer)
{
// GBuffer params:
// ViewInfo : x-1/Projection[0,0] y-1/Projection[1,1] z-(Far / (Far - Near) w-(-Far * Near) / (Far - Near) / Far)
// ViewInfo : x-1/Projection[0,0] y-1/Projection[1,1] z-(Near / (Far - Near)) w-((Far * Near) / (Far - Near))
// ScreenSize : x-Width y-Height z-1 / Width w-1 / Height
// ViewPos,ViewFar : x,y,z - world space view position w-Far
// InvViewMatrix : inverse view matrix (4 rows by 4 columns)
+1 -1
View File
@@ -135,7 +135,7 @@ SamplerComparisonState ShadowSamplerPCF : register(s5);
// Structure that contains information about GBuffer
struct GBufferData
{
float4 ViewInfo; // x-1/Projection[0,0], y-1/Projection[1,1], z-(Far / (Far - Near), w-(-Far * Near) / (Far - Near) / Far)
float4 ViewInfo; // x-1/Projection[0,0], y-1/Projection[1,1], z-(Near / (Far - Near)), w-((Far * Near) / (Far - Near) / Far)
float4 ScreenSize; // x-Width, y-Height, z-1/Width, w-1/Height
float3 ViewPos; // view position (in world space)
float ViewFar; // view far plane distance (in world space)
+1 -1
View File
@@ -96,7 +96,7 @@ Texture2D Input1 : register(t1);
// Converts z-buffer depth to linear view-space depth
float LinearDepth(in float zBufferDepth)
{
return ProjectionAB.y / (zBufferDepth - ProjectionAB.x);
return ProjectionAB.y / (zBufferDepth + ProjectionAB.x);
}
// Depth of Field depth blur generation (outputs linear depth + blur factor to R16G16 target)
+2 -2
View File
@@ -27,13 +27,13 @@ Texture2D GBuffer3 : register(t4);
// Linearize raw device depth
float LinearizeZ(GBufferData gBuffer, float depth)
{
return gBuffer.ViewInfo.w / ((1-depth) - gBuffer.ViewInfo.z);
return gBuffer.ViewInfo.w / (depth + gBuffer.ViewInfo.z);
}
// Convert linear depth to device depth
float LinearZ2DeviceDepth(GBufferData gBuffer, float linearDepth)
{
return 1 - ((gBuffer.ViewInfo.w / linearDepth) + gBuffer.ViewInfo.z);
return ((gBuffer.ViewInfo.w / linearDepth) - gBuffer.ViewInfo.z);
}
// Get view space position at given pixel coordinate with given device depth
+1 -1
View File
@@ -275,7 +275,7 @@ void PrepareDepthMip(const float4 inPos, int mipLevel, out float outD0, out floa
{
float4 depths = depthsArr[i];
float closest = min(min(depths.x, depths.y), min(depths.z, depths.w));
float closest = max(max(depths.x, depths.y), max(depths.z, depths.w));
CalculateRadiusParameters(abs(closest), 1.0, dummyUnused1, dummyUnused2, falloffCalcMulSq);
+1 -1
View File
@@ -98,7 +98,7 @@ float3 TraceScreenSpaceReflection(float2 uv, GBufferSample gBuffer, Texture2D de
{
// Sample depth buffer and calculate depth difference
currSample = SAMPLE_RT(depthBuffer, currOffset.xy).r;
depthDiff = currOffset.z - currSample;
depthDiff = currSample - currOffset.z;
// Check intersection
if (depthDiff >= 0)