Skip to content

Commit b4fd935

Browse files
Coordinate Grid: support inverse depth
1 parent 90f7174 commit b4fd935

File tree

5 files changed

+38
-29
lines changed

5 files changed

+38
-29
lines changed

Components/interface/CoordinateGridRenderer.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Diligent Graphics LLC
2+
* Copyright 2024-2025 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,14 +53,13 @@ class CoordinateGridRenderer
5353
enum FEATURE_FLAGS : Uint32
5454
{
5555
FEATURE_FLAG_NONE = 0u,
56-
FEATURE_FLAG_REVERSED_DEPTH = 1u << 0u,
57-
FEATURE_FLAG_CONVERT_TO_SRGB = 1u << 1u,
58-
FEATURE_FLAG_RENDER_PLANE_YZ = 1u << 2u,
59-
FEATURE_FLAG_RENDER_PLANE_XZ = 1u << 3u,
60-
FEATURE_FLAG_RENDER_PLANE_XY = 1u << 4u,
61-
FEATURE_FLAG_RENDER_AXIS_X = 1u << 5u,
62-
FEATURE_FLAG_RENDER_AXIS_Y = 1u << 6u,
63-
FEATURE_FLAG_RENDER_AXIS_Z = 1u << 7u,
56+
FEATURE_FLAG_CONVERT_TO_SRGB = 1u << 0u,
57+
FEATURE_FLAG_RENDER_PLANE_YZ = 1u << 1u,
58+
FEATURE_FLAG_RENDER_PLANE_XZ = 1u << 2u,
59+
FEATURE_FLAG_RENDER_PLANE_XY = 1u << 3u,
60+
FEATURE_FLAG_RENDER_AXIS_X = 1u << 4u,
61+
FEATURE_FLAG_RENDER_AXIS_Y = 1u << 5u,
62+
FEATURE_FLAG_RENDER_AXIS_Z = 1u << 6u,
6463
};
6564

6665
struct RenderAttributes

Components/src/CoordinateGridRenderer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Diligent Graphics LLC
2+
* Copyright 2024-2025 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -220,7 +220,6 @@ bool CoordinateGridRenderer::UpdateUI(HLSL::CoordinateGridAttribs& Attribs, Coor
220220

221221
void CoordinateGridRenderer::AddShaderMacros(FEATURE_FLAGS FeatureFlags, ShaderMacroHelper& Macros)
222222
{
223-
Macros.Add("COORDINATE_GRID_INVERTED_DEPTH", (FeatureFlags & FEATURE_FLAG_REVERSED_DEPTH) != 0);
224223
Macros.Add("COORDINATE_GRID_CONVERT_OUTPUT_TO_SRGB", (FeatureFlags & FEATURE_FLAG_CONVERT_TO_SRGB) != 0);
225224

226225
Macros.Add("COORDINATE_GRID_AXIS_X", (FeatureFlags & FEATURE_FLAG_RENDER_AXIS_X) != 0);

Hydrogent/src/Tasks/HnBeginFrameTask.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,8 @@ void HnBeginFrameTask::UpdateFrameConstants(IDeviceContext* pCtx,
548548
CamAttribs.f2Jitter = Jitter;
549549
CamAttribs.fFStop = m_pCamera->GetFStop();
550550

551-
const float MetersPerUnit = RenderParam ? RenderParam->GetConfig().MetersPerUnit : 0.01f;
551+
const HnRenderParam::Configuration& RenderConfig = RenderParam->GetConfig();
552+
const float MetersPerUnit = RenderConfig.MetersPerUnit;
552553

553554
// USD camera properties are measured in scene units, but Diligent camera expects them in world units.
554555
CamAttribs.fFocusDistance = m_pCamera->GetFocusDistance() * MetersPerUnit;
@@ -574,7 +575,22 @@ void HnBeginFrameTask::UpdateFrameConstants(IDeviceContext* pCtx,
574575
CamAttribs.fFocalLength = m_pCamera->GetFocalLength() * MillimetersPerUnit;
575576
CamAttribs.fExposure = m_pCamera->GetExposure();
576577

577-
ProjMatrix.GetNearFarClipPlanes(CamAttribs.fNearPlaneZ, CamAttribs.fFarPlaneZ, pDevice->GetDeviceInfo().NDC.MinZ == -1);
578+
float fNearPlaneZ, fFarPlaneZ;
579+
ProjMatrix.GetNearFarClipPlanes(fNearPlaneZ, fFarPlaneZ, pDevice->GetDeviceInfo().NDC.MinZ == -1);
580+
if (RenderConfig.UseReverseDepth)
581+
{
582+
CamAttribs.fNearPlaneZ = fFarPlaneZ;
583+
CamAttribs.fFarPlaneZ = fNearPlaneZ;
584+
CamAttribs.fNearPlaneDepth = 1.f;
585+
CamAttribs.fFarPlaneDepth = 0.f;
586+
}
587+
else
588+
{
589+
CamAttribs.fNearPlaneZ = fNearPlaneZ;
590+
CamAttribs.fFarPlaneZ = fFarPlaneZ;
591+
CamAttribs.fNearPlaneDepth = 0.f;
592+
CamAttribs.fFarPlaneDepth = 1.f;
593+
}
578594

579595
if (CamAttribs.mView != PrevCamera.mView)
580596
{

Shaders/Common/public/BasicStructures.fxh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,13 @@ struct CameraAttribs
8888

8989
float fNearPlaneZ;
9090
float fFarPlaneZ; // fNearPlaneZ < fFarPlaneZ
91+
float fNearPlaneDepth;
92+
float fFarPlaneDepth;
93+
9194
float fHandness; // +1.0 for right-handed coordinate system, -1.0 for left-handed
9295
uint uiFrameIndex;
96+
float Padding0;
97+
float Padding1;
9398

9499
// Distance to the point of focus
95100
float fFocusDistance DEFAULT_VALUE(10.0f);

Shaders/Common/public/CoordinateGrid.fxh

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@
44
#include "CoordinateGridStructures.fxh"
55
#include "ShaderUtilities.fxh"
66

7-
#if COORDINATE_GRID_INVERTED_DEPTH
8-
#define DepthNearPlane 1.0
9-
#define DepthFarPlane 0.0
10-
#define DepthMin max
11-
#define DepthCompare(x, y) ((x)>(y))
12-
#else
13-
#define DepthNearPlane 0.0
14-
#define DepthFarPlane 1.0
15-
#define DepthMin min
16-
#define DepthCompare(x, y) ((x)<(y))
17-
#endif // COORDINATE_GRID_INVERTED_DEPTH
18-
197
struct Ray
208
{
219
float3 Origin;
@@ -25,11 +13,13 @@ struct Ray
2513
Ray CreateCameraRay(float2 NormalizedXY,
2614
float4x4 CameraProj,
2715
float4x4 CameraViewProjInv,
28-
float3 CameraPosition)
16+
float3 CameraPosition,
17+
float NearPlaneDepth,
18+
float FarPlaneDepth)
2919
{
3020
Ray CameraRay;
31-
float4 RayStart = mul(float4(NormalizedXY, DepthToNormalizedDeviceZ(DepthNearPlane), 1.0f), CameraViewProjInv);
32-
float4 RayEnd = mul(float4(NormalizedXY, DepthToNormalizedDeviceZ(DepthFarPlane), 1.0f), CameraViewProjInv);
21+
float4 RayStart = mul(float4(NormalizedXY, DepthToNormalizedDeviceZ(NearPlaneDepth), 1.0f), CameraViewProjInv);
22+
float4 RayEnd = mul(float4(NormalizedXY, DepthToNormalizedDeviceZ(FarPlaneDepth), 1.0f), CameraViewProjInv);
3323

3424
RayStart.xyz /= RayStart.w;
3525
RayEnd.xyz /= RayEnd.w;
@@ -171,7 +161,7 @@ float4 ComputeCoordinateGrid(in float2 f2NormalizedXY,
171161
in float MaxDepth,
172162
in CoordinateGridAttribs GridAttribs)
173163
{
174-
Ray RayWS = CreateCameraRay(f2NormalizedXY, Camera.mProj, Camera.mViewProjInv, Camera.f4Position.xyz);
164+
Ray RayWS = CreateCameraRay(f2NormalizedXY, Camera.mProj, Camera.mViewProjInv, Camera.f4Position.xyz, Camera.fNearPlaneDepth, Camera.fFarPlaneDepth);
175165

176166
float3 Positions[3];
177167
float PlaneAlpha[3];

0 commit comments

Comments
 (0)