Skip to content

Commit defd4cd

Browse files
CameraAttribs: added scene near/far z and depth
1 parent 0137cb5 commit defd4cd

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

Hydrogent/src/Tasks/HnBeginFrameTask.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -534,20 +534,9 @@ void HnBeginFrameTask::UpdateFrameConstants(IDeviceContext* pCtx,
534534

535535
float fNearPlaneZ, fFarPlaneZ;
536536
ProjMatrix.GetNearFarClipPlanes(fNearPlaneZ, fFarPlaneZ, pDevice->GetDeviceInfo().NDC.MinZ == -1);
537-
if (m_Params.UseReverseDepth)
538-
{
539-
CamAttribs.fNearPlaneZ = fFarPlaneZ;
540-
CamAttribs.fFarPlaneZ = fNearPlaneZ;
541-
CamAttribs.fNearPlaneDepth = 1.f;
542-
CamAttribs.fFarPlaneDepth = 0.f;
543-
}
544-
else
545-
{
546-
CamAttribs.fNearPlaneZ = fNearPlaneZ;
547-
CamAttribs.fFarPlaneZ = fFarPlaneZ;
548-
CamAttribs.fNearPlaneDepth = 0.f;
549-
CamAttribs.fFarPlaneDepth = 1.f;
550-
}
537+
VERIFY_EXPR((!m_Params.UseReverseDepth && (fNearPlaneZ <= fFarPlaneZ)) ||
538+
(m_Params.UseReverseDepth && (fNearPlaneZ >= fFarPlaneZ)));
539+
CamAttribs.SetClipPlanes(fNearPlaneZ, fFarPlaneZ);
551540

552541
if (CamAttribs.mView != PrevCamera.mView)
553542
{

Shaders/Common/public/BasicStructures.fxh

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,18 @@ struct CameraAttribs
9090
float fFarPlaneZ; // fNearPlaneZ < fFarPlaneZ
9191
float fNearPlaneDepth;
9292
float fFarPlaneDepth;
93-
93+
94+
// Tight scene bounds
95+
float fSceneNearZ;
96+
float fSceneFarZ; // fSceneNearZ < fSceneFarZ
97+
float fSceneNearDepth;
98+
float fSceneFarDepth;
99+
94100
float fHandness; // +1.0 for right-handed coordinate system, -1.0 for left-handed
95101
uint uiFrameIndex;
96102
float Padding0;
97103
float Padding1;
98-
104+
99105
// Distance to the point of focus
100106
float fFocusDistance DEFAULT_VALUE(10.0f);
101107
// Ratio of the aperture (known as f-stop or f-number)
@@ -104,14 +110,14 @@ struct CameraAttribs
104110
float fFocalLength DEFAULT_VALUE(50.0f);
105111
// Sensor width in mm
106112
float fSensorWidth DEFAULT_VALUE(36.0f);
107-
113+
108114
// Sensor height in mm
109115
float fSensorHeight DEFAULT_VALUE(24.0f);
110116
// Exposure adjustment as a log base-2 value.
111117
float fExposure DEFAULT_VALUE(0.0f);
112118
// TAA jitter
113119
float2 f2Jitter;
114-
120+
115121
float4x4 mView;
116122
float4x4 mProj;
117123
float4x4 mViewProj;
@@ -120,6 +126,26 @@ struct CameraAttribs
120126
float4x4 mViewProjInv;
121127

122128
float4 f4ExtraData[5]; // Any appliation-specific data
129+
130+
#ifdef __cplusplus
131+
// Set the near and far clip planes z and depth values.
132+
//
133+
// fNearZ > fFarZ means that the depth buffer is reversed.
134+
void SetClipPlanes(float fNearZ, float fFarZ)
135+
{
136+
bool UseReverseDepth = fNearZ > fFarZ;
137+
138+
fNearPlaneZ = UseReverseDepth ? fFarZ : fNearZ;
139+
fFarPlaneZ = UseReverseDepth ? fNearZ : fFarZ;
140+
fNearPlaneDepth = UseReverseDepth ? 1.f : 0.f;
141+
fFarPlaneDepth = UseReverseDepth ? 0.f : 1.f;
142+
143+
fSceneNearZ = fNearPlaneZ;
144+
fSceneFarZ = fFarPlaneZ;
145+
fSceneNearDepth = fNearPlaneDepth;
146+
fSceneFarDepth = fFarPlaneDepth;
147+
}
148+
#endif
123149
};
124150
#ifdef CHECK_STRUCT_ALIGNMENT
125151
CHECK_STRUCT_ALIGNMENT(CameraAttribs);

Shaders/PBR/private/RenderPBR.psh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,10 @@ PSOutput main(in VSOutput VSOut,
601601
}
602602
# elif (DEBUG_VIEW == DEBUG_VIEW_SCENE_DEPTH)
603603
{
604-
float CameraZ = DepthToCameraZ(VSOut.ClipPos.z, g_Frame.Camera.mProj);
605-
CameraZ = (CameraZ - g_Frame.Camera.fNearPlaneZ) / (g_Frame.Camera.fFarPlaneZ - g_Frame.Camera.fNearPlaneZ);
606-
OutColor.rgb = float3(CameraZ, CameraZ, CameraZ);
604+
float CameraZ = DepthToCameraZ(VSOut.ClipPos.z, g_Frame.Camera.mProj);
605+
float SceneZRange = max(g_Frame.Camera.fSceneFarZ - g_Frame.Camera.fSceneNearZ, 1e-6);
606+
float RelSceneZ = saturate((CameraZ - g_Frame.Camera.fSceneNearZ) / SceneZRange);
607+
OutColor.rgb = float3(RelSceneZ, RelSceneZ, RelSceneZ);
607608
}
608609
# elif (DEBUG_VIEW != DEBUG_VIEW_NONE)
609610
{

0 commit comments

Comments
 (0)