Skip to content

Commit

Permalink
NRD updated to v4.9.4:
Browse files Browse the repository at this point in the history
- various improvements and bug fixes
- updated deps
  • Loading branch information
dzhdanNV committed Sep 20, 2024
1 parent 07a705e commit 003f56a
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 178 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ if (WIN32)
else ()
find_library (NGX_D_LIB NAMES libnvsdk_ngx.a PATHS "External/NGX/lib/Linux_x86_64")
find_library (NGX_LIB NAMES libnvsdk_ngx.a PATHS "External/NGX/lib/Linux_x86_64")
find_file (DLSS_SR_DLL NAMES libnvidia-ngx-dlss.so.3.7.10 PATHS "External/NGX/lib/Linux_x86_64/rel")
find_file (DLSS_SR_DLL NAMES libnvidia-ngx-dlss.so.3.7.20 PATHS "External/NGX/lib/Linux_x86_64/rel")
endif ()

if (NGX_D_LIB AND NGX_LIB AND DLSS_SR_DLL)
Expand Down
2 changes: 1 addition & 1 deletion External/NGX
Submodule NGX updated 30 files
+40 −0 include/nvsdk_ngx.h
+10 −2 include/nvsdk_ngx_defs.h
+2 −2 include/nvsdk_ngx_helpers_vk.h
+ lib/Linux_x86_64/dev/libnvidia-ngx-dlss.so.3.7.20
+ lib/Linux_x86_64/libnvsdk_ngx.a
+ lib/Linux_x86_64/rel/libnvidia-ngx-dlss.so.3.7.20
+ lib/Windows_x86_64/dev/nvngx_dlss.dll
+ lib/Windows_x86_64/rel/nvngx_dlss.dll
+ lib/Windows_x86_64/vs2010/nvsdk_ngx_d.lib
+ lib/Windows_x86_64/vs2010/nvsdk_ngx_d_dbg.lib
+ lib/Windows_x86_64/vs2010/nvsdk_ngx_s.lib
+ lib/Windows_x86_64/vs2010/nvsdk_ngx_s_dbg.lib
+ lib/Windows_x86_64/vs2012/nvsdk_ngx_d.lib
+ lib/Windows_x86_64/vs2012/nvsdk_ngx_d_dbg.lib
+ lib/Windows_x86_64/vs2012/nvsdk_ngx_s.lib
+ lib/Windows_x86_64/vs2012/nvsdk_ngx_s_dbg.lib
+ lib/Windows_x86_64/vs2013/nvsdk_ngx_d.lib
+ lib/Windows_x86_64/vs2013/nvsdk_ngx_d_dbg.lib
+ lib/Windows_x86_64/vs2013/nvsdk_ngx_s.lib
+ lib/Windows_x86_64/vs2013/nvsdk_ngx_s_dbg.lib
+ lib/Windows_x86_64/x86_64/nvsdk_ngx_d.lib
+ lib/Windows_x86_64/x86_64/nvsdk_ngx_d_dbg.lib
+ lib/Windows_x86_64/x86_64/nvsdk_ngx_d_dbg_iterator0.lib
+ lib/Windows_x86_64/x86_64/nvsdk_ngx_d_dbg_iterator1.lib
+ lib/Windows_x86_64/x86_64/nvsdk_ngx_d_iterator1.lib
+ lib/Windows_x86_64/x86_64/nvsdk_ngx_s.lib
+ lib/Windows_x86_64/x86_64/nvsdk_ngx_s_dbg.lib
+ lib/Windows_x86_64/x86_64/nvsdk_ngx_s_dbg_iterator0.lib
+ lib/Windows_x86_64/x86_64/nvsdk_ngx_s_dbg_iterator1.lib
+ lib/Windows_x86_64/x86_64/nvsdk_ngx_s_iterator1.lib
2 changes: 1 addition & 1 deletion External/NRD
Submodule NRD updated from d27094 to ebf92a
2 changes: 1 addition & 1 deletion External/NRIFramework
34 changes: 22 additions & 12 deletions Shaders/Include/RaytracingShared.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ float ReprojectIrradiance(
bool isPrevFrame, bool isRefraction,
Texture2D<float3> texDiff, Texture2D<float4> texSpecViewZ,
GeometryProps geometryProps, uint2 pixelPos,
out float3 prevLdiff, out float3 prevLspec
out float3 Ldiff, out float3 Lspec
)
{
// Get UV and ignore back projection
Expand All @@ -577,9 +577,6 @@ float ReprojectIrradiance(
float4 data = texSpecViewZ.SampleLevel( gNearestSampler, uv * rescale, 0 );
float prevViewZ = abs( data.w ) / FP16_VIEWZ_SCALE;

prevLdiff = texDiff.SampleLevel( gNearestSampler, uv * rescale, 0 );
prevLspec = data.xyz;

// Initial state
float weight = 1.0;
float2 pixelUv = float2( pixelPos + 0.5 ) * gInvRectSize;
Expand All @@ -591,15 +588,15 @@ float ReprojectIrradiance(
if( isRefraction )
{
// Confidence - viewZ ( PSR makes prevViewZ further than the original primary surface )
weight *= Math::LinearStep( 0.03, 0.01, saturate( err ) );
weight *= Math::LinearStep( 0.01, 0.005, saturate( err ) );

// Fade-out on screen edges ( hard )
weight *= all( saturate( uv ) == uv );
}
else
{
// Confidence - viewZ
weight *= Math::LinearStep( 0.03, 0.01, abs( err ) );
weight *= Math::LinearStep( 0.01, 0.005, abs( err ) );

// Fade-out on screen edges ( soft )
float2 f = Math::LinearStep( 0.0, 0.1, uv ) * Math::LinearStep( 1.0, 0.9, uv );
Expand All @@ -619,17 +616,30 @@ float ReprojectIrradiance(
// Ignore sky
weight *= float( !geometryProps.IsSky( ) );

// Clear out bad values
// Use only if radiance is on the screen
weight *= float( gOnScreen < SHOW_AMBIENT_OCCLUSION );

// Add global confidence
if( isPrevFrame )
weight *= gPrevFrameConfidence; // see C++ code for details

// Read data
Ldiff = texDiff.SampleLevel( gNearestSampler, uv * rescale, 0 );
Lspec = data.xyz;

// Avoid NANs
[flatten]
if( any( isnan( prevLdiff ) | isinf( prevLdiff ) | isnan( prevLspec ) | isinf( prevLspec ) ) )
if( any( isnan( Ldiff ) | isinf( Ldiff ) | isnan( Lspec ) | isinf( Lspec ) ) || NRD_MODE >= OCCLUSION )
{
prevLdiff = 0;
prevLspec = 0;
Ldiff = 0;
Lspec = 0;
weight = 0;
}

// Use only if radiance is on the screen
weight *= float( gOnScreen < SHOW_AMBIENT_OCCLUSION );
// Avoid really bad reprojection
float f = saturate( weight / 0.001 );
Ldiff *= f;
Lspec *= f;

return weight;
}
4 changes: 2 additions & 2 deletions Shaders/Include/Shared.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
#define USE_SHARC_DITHERING 1 // must be in [0; 1] range
#define USE_TRANSLUCENCY 1 // translucent foliage
#define USE_NIS 1 // NIS filter (debug only)
#define USE_SHARC_V_DEPENDENT 1 // needed to get a full match with prev frame data // TODO: improve multi-bounce low-roughness case

// Default = 0
#define USE_SANITIZATION 0 // NRD sample is NAN/INF free
Expand All @@ -43,7 +44,6 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
#define USE_RANDOMIZED_ROUGHNESS 0 // randomize roughness ( a common case in games )
#define USE_LOAD 0 // Load vs SampleLevel
#define USE_SHARC_DEBUG 0 // 1 - show cache, 2 - show grid
#define USE_SHARC_V_DEPENDENT 1 // needed to get a full match with prev frame data // TODO: improve multi-bounce low-roughness case

//=============================================================================================
// CONSTANTS
Expand Down Expand Up @@ -271,7 +271,6 @@ NRI_RESOURCE( cbuffer, GlobalConstants, b, 0, SET_GLOBAL )
float gTanSunAngularRadius;
float gTanPixelAngularRadius;
float gDebug;
float gTransparent;
float gPrevFrameConfidence;
float gMinProbability;
float gUnproject;
Expand All @@ -283,6 +282,7 @@ NRI_RESOURCE( cbuffer, GlobalConstants, b, 0, SET_GLOBAL )
float gExposure;
float gMipBias;
float gOrthoMode;
uint32_t gTransparent;
uint32_t gSharcMaxAccumulatedFrameNum;
uint32_t gDenoiserType;
uint32_t gDisableShadowsAndEnableImportanceSampling; // TODO: remove - modify GetSunIntensity to return 0 if sun is below horizon
Expand Down
143 changes: 69 additions & 74 deletions Shaders/TraceOpaque.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ float4 GetRadianceFromPreviousFrame( GeometryProps geometryProps, MaterialProps
// Reproject previous frame
float3 prevLdiff, prevLspec;
float prevFrameWeight = ReprojectIrradiance( true, false, gIn_PrevComposedDiff, gIn_PrevComposedSpec_PrevViewZ, geometryProps, pixelPos, prevLdiff, prevLspec );
prevFrameWeight *= gPrevFrameConfidence; // see C++ code for details

// Estimate how strong lighting at hit depends on the view direction
float diffuseProbabilityBiased = EstimateDiffuseProbability( geometryProps, materialProps, true );
Expand All @@ -82,7 +81,7 @@ float4 GetRadianceFromPreviousFrame( GeometryProps geometryProps, MaterialProps
float b = Color::Luminance( prevLspec );
prevFrameWeight *= lerp( diffuseProbabilityBiased, 1.0, ( a + NRD_EPS ) / ( a + b + NRD_EPS ) );

// Return avoiding really bad reprojection
// Avoid really bad reprojection
return NRD_MODE < OCCLUSION ? float4( prevLsum * saturate( prevFrameWeight / 0.001 ), prevFrameWeight ) : 0.0;
}

Expand Down Expand Up @@ -236,41 +235,39 @@ TraceOpaqueResult TraceOpaque( inout TraceOpaqueDesc desc )
Lpsr = GetRadianceFromPreviousFrame( geometryProps, materialProps, desc.pixelPos, false );

// L2 cache - SHARC
if( gSHARC && NRD_MODE < OCCLUSION )
{
GridParameters gridParameters = ( GridParameters )0;
gridParameters.cameraPosition = gCameraGlobalPos.xyz;
gridParameters.cameraPositionPrev = gCameraGlobalPosPrev.xyz;
gridParameters.sceneScale = SHARC_SCENE_SCALE;
gridParameters.logarithmBase = SHARC_GRID_LOGARITHM_BASE;

float3 Xglobal = GetGlobalPos( geometryProps.X );
uint level = GetGridLevel( Xglobal, gridParameters );
float voxelSize = GetVoxelSize( level, gridParameters );
float smc = GetSpecMagicCurve( materialProps.roughness );

float3x3 mBasis = Geometry::GetBasis( geometryProps.N );
float2 rndScaled = ( Rng::Hash::GetFloat2( ) - 0.5 ) * voxelSize * USE_SHARC_DITHERING;
Xglobal += mBasis[ 0 ] * rndScaled.x + mBasis[ 1 ] * rndScaled.y;

SharcHitData sharcHitData = ( SharcHitData )0;
sharcHitData.positionWorld = Xglobal;
sharcHitData.normalWorld = geometryProps.N;

SharcState sharcState;
sharcState.gridParameters = gridParameters;
sharcState.hashMapData.capacity = SHARC_CAPACITY;
sharcState.hashMapData.hashEntriesBuffer = gInOut_SharcHashEntriesBuffer;
sharcState.voxelDataBuffer = gInOut_SharcVoxelDataBuffer;

bool isSharcAllowed = geometryProps.tmin > voxelSize; // voxel angular size is acceptable
isSharcAllowed = isSharcAllowed && Rng::Hash::GetFloat( ) > Lpsr.w; // probabilistically estimate the need
isSharcAllowed = isSharcAllowed && desc.bounceNum == 0; // allow only for the last bounce for PSR

float3 sharcRadiance;
if( isSharcAllowed && SharcGetCachedRadiance( sharcState, sharcHitData, sharcRadiance, false ) )
Lpsr = float4( sharcRadiance, 1.0 );
}
GridParameters gridParameters = ( GridParameters )0;
gridParameters.cameraPosition = gCameraGlobalPos.xyz;
gridParameters.cameraPositionPrev = gCameraGlobalPosPrev.xyz;
gridParameters.sceneScale = SHARC_SCENE_SCALE;
gridParameters.logarithmBase = SHARC_GRID_LOGARITHM_BASE;

float3 Xglobal = GetGlobalPos( geometryProps.X );
uint level = GetGridLevel( Xglobal, gridParameters );
float voxelSize = GetVoxelSize( level, gridParameters );
float smc = GetSpecMagicCurve( materialProps.roughness );

float3x3 mBasis = Geometry::GetBasis( geometryProps.N );
float2 rndScaled = ( Rng::Hash::GetFloat2( ) - 0.5 ) * voxelSize * USE_SHARC_DITHERING;
Xglobal += mBasis[ 0 ] * rndScaled.x + mBasis[ 1 ] * rndScaled.y;

SharcHitData sharcHitData = ( SharcHitData )0;
sharcHitData.positionWorld = Xglobal;
sharcHitData.normalWorld = geometryProps.N;

SharcState sharcState;
sharcState.gridParameters = gridParameters;
sharcState.hashMapData.capacity = SHARC_CAPACITY;
sharcState.hashMapData.hashEntriesBuffer = gInOut_SharcHashEntriesBuffer;
sharcState.voxelDataBuffer = gInOut_SharcVoxelDataBuffer;

bool isSharcAllowed = gSHARC && NRD_MODE < OCCLUSION; // trivial
isSharcAllowed &= geometryProps.tmin > voxelSize; // voxel angular size is acceptable
isSharcAllowed &= Rng::Hash::GetFloat( ) > Lpsr.w; // probabilistically estimate the need
isSharcAllowed &= desc.bounceNum == 0; // allow only for the last bounce for PSR

float3 sharcRadiance;
if( isSharcAllowed && SharcGetCachedRadiance( sharcState, sharcHitData, sharcRadiance, false ) )
Lpsr = float4( sharcRadiance, 1.0 );

// TODO: add a macro switch for old mode ( with coupled direct lighting )

Expand Down Expand Up @@ -582,47 +579,45 @@ TraceOpaqueResult TraceOpaque( inout TraceOpaqueDesc desc )
//=============================================================================================================================================================

float4 Lcached = 0;
if( !geometryProps.IsSky( ) && NRD_MODE < OCCLUSION )
if( !geometryProps.IsSky( ) )
{
// L1 cache - reproject previous frame, carefully treating specular
Lcached = GetRadianceFromPreviousFrame( geometryProps, materialProps, desc.pixelPos, false );

// L2 cache - SHARC
if( gSHARC && NRD_MODE < OCCLUSION )
{
GridParameters gridParameters = ( GridParameters )0;
gridParameters.cameraPosition = gCameraGlobalPos.xyz;
gridParameters.cameraPositionPrev = gCameraGlobalPosPrev.xyz;
gridParameters.sceneScale = SHARC_SCENE_SCALE;
gridParameters.logarithmBase = SHARC_GRID_LOGARITHM_BASE;

float3 Xglobal = GetGlobalPos( geometryProps.X );
uint level = GetGridLevel( Xglobal, gridParameters );
float voxelSize = GetVoxelSize( level, gridParameters );
float smc = GetSpecMagicCurve( materialProps.roughness );

float3x3 mBasis = Geometry::GetBasis( geometryProps.N );
float2 rndScaled = ( Rng::Hash::GetFloat2( ) - 0.5 ) * voxelSize * USE_SHARC_DITHERING;
Xglobal += mBasis[ 0 ] * rndScaled.x + mBasis[ 1 ] * rndScaled.y;

SharcHitData sharcHitData = ( SharcHitData )0;
sharcHitData.positionWorld = Xglobal;
sharcHitData.normalWorld = geometryProps.N;

SharcState sharcState;
sharcState.gridParameters = gridParameters;
sharcState.hashMapData.capacity = SHARC_CAPACITY;
sharcState.hashMapData.hashEntriesBuffer = gInOut_SharcHashEntriesBuffer;
sharcState.voxelDataBuffer = gInOut_SharcVoxelDataBuffer;

bool isSharcAllowed = geometryProps.tmin > voxelSize; // voxel angular size is acceptable
isSharcAllowed = isSharcAllowed && Rng::Hash::GetFloat( ) > Lcached.w; // probabilistically estimate the need
isSharcAllowed = isSharcAllowed && ( isDiffuse || Rng::Hash::GetFloat( ) < smc || bounce == desc.bounceNum ); // allowed for diffuse-like events or last bounce

float3 sharcRadiance;
if( isSharcAllowed && SharcGetCachedRadiance( sharcState, sharcHitData, sharcRadiance, false ) )
Lcached = float4( sharcRadiance, 1.0 );
}
GridParameters gridParameters = ( GridParameters )0;
gridParameters.cameraPosition = gCameraGlobalPos.xyz;
gridParameters.cameraPositionPrev = gCameraGlobalPosPrev.xyz;
gridParameters.sceneScale = SHARC_SCENE_SCALE;
gridParameters.logarithmBase = SHARC_GRID_LOGARITHM_BASE;

float3 Xglobal = GetGlobalPos( geometryProps.X );
uint level = GetGridLevel( Xglobal, gridParameters );
float voxelSize = GetVoxelSize( level, gridParameters );
float smc = GetSpecMagicCurve( materialProps.roughness );

float3x3 mBasis = Geometry::GetBasis( geometryProps.N );
float2 rndScaled = ( Rng::Hash::GetFloat2( ) - 0.5 ) * voxelSize * USE_SHARC_DITHERING;
Xglobal += mBasis[ 0 ] * rndScaled.x + mBasis[ 1 ] * rndScaled.y;

SharcHitData sharcHitData = ( SharcHitData )0;
sharcHitData.positionWorld = Xglobal;
sharcHitData.normalWorld = geometryProps.N;

SharcState sharcState;
sharcState.gridParameters = gridParameters;
sharcState.hashMapData.capacity = SHARC_CAPACITY;
sharcState.hashMapData.hashEntriesBuffer = gInOut_SharcHashEntriesBuffer;
sharcState.voxelDataBuffer = gInOut_SharcVoxelDataBuffer;

bool isSharcAllowed = gSHARC && NRD_MODE < OCCLUSION; // trivial
isSharcAllowed &= geometryProps.tmin > voxelSize; // voxel angular size is acceptable
isSharcAllowed &= Rng::Hash::GetFloat( ) > Lcached.w; // probabilistically estimate the need
isSharcAllowed &= isDiffuse || Rng::Hash::GetFloat( ) < smc || bounce == desc.bounceNum; // allowed for diffuse-like events or last bounce

float3 sharcRadiance;
if( isSharcAllowed && SharcGetCachedRadiance( sharcState, sharcHitData, sharcRadiance, false ) )
Lcached = float4( sharcRadiance, 1.0 );

// Cache miss - compute lighting, if not found in caches
if( Rng::Hash::GetFloat( ) > Lcached.w )
Expand Down Expand Up @@ -889,7 +884,7 @@ void main( uint2 pixelPos : SV_DispatchThreadId )

uint checkerboard = Sequence::CheckerBoard( pixelPos >> 2, 0 ) != 0;
float3 color = Rng::Hash::GetFloat4( ).xyz;
color *= checkerboard && !geometryProps0.Has( FLAG_STATIC ) ? 0.5 : 1.0;
color *= ( checkerboard && !geometryProps0.Has( FLAG_STATIC ) ) ? 0.5 : 1.0;

materialProps0.Ldirect = color;
}
Expand Down
Loading

0 comments on commit 003f56a

Please sign in to comment.