Skip to content

Commit

Permalink
NRI updated to v1.162 (WIP):
Browse files Browse the repository at this point in the history
- updated deps
  • Loading branch information
dzhdanNV committed Jan 24, 2025
1 parent b520f26 commit 3671220
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 212 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ else ()
endif ()

if (NGX_D_LIB AND NGX_LIB AND DLSS_SR_DLL)
target_include_directories (${PROJECT_NAME} PRIVATE "External/NRIFramework/External/NRI/External/vulkan/include")
target_include_directories (${PROJECT_NAME} PRIVATE "$ENV{VULKAN_SDK}/include")
target_link_libraries (${PROJECT_NAME} PRIVATE debug ${NGX_D_LIB})
target_link_libraries (${PROJECT_NAME} PRIVATE optimized ${NGX_LIB})

Expand Down
2 changes: 1 addition & 1 deletion External/NRD
Submodule NRD updated from 073fe9 to c55976
2 changes: 1 addition & 1 deletion External/NRIFramework
2 changes: 1 addition & 1 deletion External/SHARC
Submodule SHARC updated from 14b0d5 to 072ee4
37 changes: 21 additions & 16 deletions Shaders/Composition.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -254,44 +254,49 @@ void main( int2 pixelPos : SV_DispatchThreadId )

// SHARC debug
#if( 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;
HashGridParameters hashGridParams;
hashGridParams.cameraPosition = gCameraGlobalPos.xyz;
hashGridParams.sceneScale = SHARC_SCENE_SCALE;
hashGridParams.logarithmBase = SHARC_GRID_LOGARITHM_BASE;
hashGridParams.levelBias = SHARC_GRID_LEVEL_BIAS;

#if( USE_SHARC_DEBUG == 1 )
SharcState sharcState;
sharcState.gridParameters = gridParameters;
sharcState.hashMapData.capacity = SHARC_CAPACITY;
sharcState.hashMapData.hashEntriesBuffer = gInOut_SharcHashEntriesBuffer;
sharcState.voxelDataBuffer = gInOut_SharcVoxelDataBuffer;

float3 Xglobal = GetGlobalPos( X );

// Dithered output
#if 0
Rng::Hash::Initialize( pixelPos, gFrameIndex );

uint level = GetGridLevel( Xglobal, gridParameters );
float voxelSize = GetVoxelSize( level, gridParameters );
uint level = HashGridGetLevel( Xglobal, hashGridParams );
float voxelSize = HashGridGetVoxelSize( level, hashGridParams );
float3x3 mBasis = Geometry::GetBasis( N );
float2 rnd = ( Rng::Hash::GetFloat2( ) - 0.5 ) * voxelSize * USE_SHARC_DITHERING;
Xglobal += mBasis[ 0 ] * rnd.x + mBasis[ 1 ] * rnd.y;
#endif

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

bool isValid = SharcGetCachedRadiance( sharcState, sharcHitData, Ldiff, true );
HashMapData hashMapData;
hashMapData.capacity = SHARC_CAPACITY;
hashMapData.hashEntriesBuffer = gInOut_SharcHashEntriesBuffer;

SharcParameters sharcParams;
sharcParams.gridParameters = hashGridParams;
sharcParams.hashMapData = hashMapData;
sharcParams.enableAntiFireflyFilter = SHARC_ANTI_FIREFLY;
sharcParams.voxelDataBuffer = gInOut_SharcVoxelDataBuffer;
sharcParams.voxelDataBufferPrev = gInOut_SharcVoxelDataBufferPrev;

bool isValid = SharcGetCachedRadiance( sharcParams, sharcHitData, Ldiff, true );

// Highlight invalid cells
#if 0
Ldiff = isValid ? Ldiff : float3( 1.0, 0.0, 0.0 );
#endif
#elif( USE_SHARC_DEBUG == 2 )
Ldiff = HashGridDebugColoredHash( GetGlobalPos( X ), gridParameters );
Ldiff = HashGridDebugColoredHash( GetGlobalPos( X ), hashGridParams );
#endif

Lspec *= float( USE_SHARC_DEBUG == 0 );
Expand Down
7 changes: 5 additions & 2 deletions Shaders/Include/Shared.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
#define USE_SIMULATED_FIREFLY_TEST 0 // "anti-firefly" debugging
#define USE_CAMERA_ATTACHED_REFLECTION_TEST 0 // test special treatment for reflections of objects attached to the camera
#define USE_RUSSIAN_ROULETTE 0 // bad practice for real-time denoising
#define USE_DRS_STRESS_TEST 0 // test for verifying that NRD doesn't touch data outside of DRS rectangle
#define USE_INF_STRESS_TEST 0 // test for verifying that NRD doesn't touch data outside of denoising range
#define USE_DRS_STRESS_TEST 0 // NRD must not touch GARBAGE data outside of DRS rectangle
#define USE_INF_STRESS_TEST 0 // NRD must not touch GARBAGE data outside of denoising range
#define USE_ANOTHER_COBALT 0 // another cobalt variant
#define USE_PUDDLES 0 // add puddles
#define USE_RANDOMIZED_ROUGHNESS 0 // randomize roughness ( a common case in games )
Expand Down Expand Up @@ -128,6 +128,8 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
#define SHARC_DOWNSCALE 5
#define SHARC_NORMAL_DITHER 0.003
#define SHARC_POS_DITHER 0.001
#define SHARC_ANTI_FIREFLY true
#define SHARC_STALE_FRAME_NUM_MIN 32 // new version uses 8 by default, old value offers more stability in voxels with low number of samples ( critical for glass )

// Blue noise
#define BLUE_NOISE_SPATIAL_DIM 128 // see StaticTexture::ScramblingRanking
Expand All @@ -153,6 +155,7 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
#define LEAF_THICKNESS 0.001 // TODO: viewZ dependent?
#define STRAND_THICKNESS 80e-6f
#define TAA_HISTORY_SHARPNESS 0.5
#define GARBAGE sqrt( -1.0 ) // sqrt( -1.0 ) or -log( 0.0 ) or 32768.0

#define MORPH_MAX_ACTIVE_TARGETS_NUM 8u
#define MORPH_ELEMENTS_PER_ROW_NUM 4
Expand Down
25 changes: 19 additions & 6 deletions Shaders/SharcResolve.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,28 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
[numthreads( LINEAR_BLOCK_SIZE, 1, 1 )]
void main( uint threadIndex : SV_DispatchThreadID )
{
GridParameters gridParameters = ( GridParameters )0;
gridParameters.cameraPosition = gCameraGlobalPos.xyz;
gridParameters.cameraPositionPrev = gCameraGlobalPosPrev.xyz;
gridParameters.sceneScale = SHARC_SCENE_SCALE;
gridParameters.logarithmBase = SHARC_GRID_LOGARITHM_BASE;
HashGridParameters hashGridParams;
hashGridParams.cameraPosition = gCameraGlobalPos.xyz;
hashGridParams.sceneScale = SHARC_SCENE_SCALE;
hashGridParams.logarithmBase = SHARC_GRID_LOGARITHM_BASE;
hashGridParams.levelBias = SHARC_GRID_LEVEL_BIAS;

HashMapData hashMapData;
hashMapData.capacity = SHARC_CAPACITY;
hashMapData.hashEntriesBuffer = gInOut_SharcHashEntriesBuffer;

SharcResolveEntry( threadIndex, gridParameters, hashMapData, gInOut_SharcHashCopyOffsetBuffer, gInOut_SharcVoxelDataBuffer, gInOut_SharcVoxelDataBufferPrev, gSharcMaxAccumulatedFrameNum, SHARC_STALE_FRAME_NUM_MIN );
SharcParameters sharcParams;
sharcParams.gridParameters = hashGridParams;
sharcParams.hashMapData = hashMapData;
sharcParams.enableAntiFireflyFilter = SHARC_ANTI_FIREFLY;
sharcParams.voxelDataBuffer = gInOut_SharcVoxelDataBuffer;
sharcParams.voxelDataBufferPrev = gInOut_SharcVoxelDataBufferPrev;

SharcResolveParameters sharcResolveParameters;
sharcResolveParameters.cameraPositionPrev = gCameraGlobalPosPrev.xyz;
sharcResolveParameters.accumulationFrameNum = gSharcMaxAccumulatedFrameNum;
sharcResolveParameters.staleFrameNumMax = SHARC_STALE_FRAME_NUM_MIN;
sharcResolveParameters.enableAntiFireflyFilter = SHARC_ANTI_FIREFLY;

SharcResolveEntry( threadIndex, sharcParams, sharcResolveParameters, gInOut_SharcHashCopyOffsetBuffer );
}
117 changes: 63 additions & 54 deletions Shaders/SharcUpdate.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,75 +34,47 @@ float3 GetAmbientBRDF( GeometryProps geometryProps, MaterialProps materialProps,
return ambBRDF;
}

[numthreads( 16, 16, 1 )]
void main( uint2 pixelPos : SV_DispatchThreadId )
void Trace( GeometryProps geometryProps )
{
/*
TODO: modify SHARC to support:
- material de-modulation
- 2 levels of detail: fine and coarse ( large voxels )
- firefly suppression
- anti-lag
- dynamic "sceneScale"
- auto "sceneScale" adjustment to guarantee desired number of samples in voxels on average
*/

// Initialize RNG
Rng::Hash::Initialize( pixelPos, gFrameIndex );

// Sample position
float2 sampleUv = ( pixelPos + 0.5 + gJitter * gRectSize ) * SHARC_DOWNSCALE * gInvRectSize;

// SHARC state
GridParameters gridParameters = ( GridParameters )0;
gridParameters.cameraPosition = gCameraGlobalPos.xyz;
gridParameters.cameraPositionPrev = gCameraGlobalPosPrev.xyz;
gridParameters.sceneScale = SHARC_SCENE_SCALE;
gridParameters.logarithmBase = SHARC_GRID_LOGARITHM_BASE;
HashGridParameters hashGridParams;
hashGridParams.cameraPosition = gCameraGlobalPos.xyz;
hashGridParams.sceneScale = SHARC_SCENE_SCALE;
hashGridParams.logarithmBase = SHARC_GRID_LOGARITHM_BASE;
hashGridParams.levelBias = SHARC_GRID_LEVEL_BIAS;

HashMapData hashMapData;
hashMapData.capacity = SHARC_CAPACITY;
hashMapData.hashEntriesBuffer = gInOut_SharcHashEntriesBuffer;

SharcParameters sharcParams;
sharcParams.gridParameters = hashGridParams;
sharcParams.hashMapData = hashMapData;
sharcParams.enableAntiFireflyFilter = SHARC_ANTI_FIREFLY;
sharcParams.voxelDataBuffer = gInOut_SharcVoxelDataBuffer;
sharcParams.voxelDataBufferPrev = gInOut_SharcVoxelDataBufferPrev;

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

SharcInit( sharcState );

// Primary ray
float3 Xv = Geometry::ReconstructViewPosition( sampleUv, gCameraFrustum, gNearZ, gOrthoMode );

float3 cameraRayOrigin = Geometry::AffineTransform( gViewToWorld, Xv );
float3 cameraRayDirection = gOrthoMode == 0.0 ? normalize( Geometry::RotateVector( gViewToWorld, Xv ) ) : -gViewDirection.xyz;

// Force some portion of rays to be absolutely random to keep cache alive behind the camera
if( Rng::Hash::GetFloat( ) < 0.2 )
cameraRayDirection = normalize( Rng::Hash::GetFloat4( ).xyz - 0.5 );

// Cast ray
GeometryProps geometryProps = CastRay( cameraRayOrigin, cameraRayDirection, 0.0, INF, GetConeAngleFromAngularRadius( 0.0, gTanPixelAngularRadius * SHARC_DOWNSCALE ), gWorldTlas, FLAG_NON_TRANSPARENT, 0 );
MaterialProps materialProps = GetMaterialProps( geometryProps, USE_SHARC_V_DEPENDENT == 0 );

if( geometryProps.IsSky( ) )
return;

// Compute lighting at hit point
float3 L = GetShadowedLighting( geometryProps, materialProps );

// Update SHARC cache
{
SharcHitData sharcHitData = ( SharcHitData )0;
float3 L = GetShadowedLighting( geometryProps, materialProps );

SharcHitData sharcHitData;
sharcHitData.positionWorld = GetGlobalPos( geometryProps.X ) + ( Rng::Hash::GetFloat4( ).xyz - 0.5 ) * SHARC_POS_DITHER;
sharcHitData.normalWorld = normalize( geometryProps.N + ( Rng::Hash::GetFloat4( ).xyz - 0.5 ) * SHARC_NORMAL_DITHER );

SharcSetThroughput( sharcState, 1.0 );
if( !SharcUpdateHit( sharcState, sharcHitData, L, 1.0 ) )
if( !SharcUpdateHit( sharcParams, sharcState, sharcHitData, L, 1.0 ) )
return;
}

// Secondary rays
[loop]
for( uint i = 1; i <= 4 && !geometryProps.IsSky( ); i++ )
for( uint bounce = 1; bounce <= SHARC_PROPOGATION_DEPTH && !geometryProps.IsSky( ); bounce++ )
{
//=============================================================================================================================================================
// Origin point
Expand Down Expand Up @@ -133,7 +105,7 @@ void main( uint2 pixelPos : SV_DispatchThreadId )
// If IS is enabled, generate up to PT_IMPORTANCE_SAMPLES_NUM rays depending on roughness
// If IS is disabled, there is no need to generate up to PT_IMPORTANCE_SAMPLES_NUM rays for specular because VNDF v3 doesn't produce rays pointing inside the surface
uint maxSamplesNum = 0;
if( i == 1 && gDisableShadowsAndEnableImportanceSampling ) // TODO: use IS in each bounce?
if( bounce == 1 && gDisableShadowsAndEnableImportanceSampling ) // TODO: use IS in each bounce?
maxSamplesNum = PT_IMPORTANCE_SAMPLES_NUM * ( isDiffuse ? 1.0 : materialProps.roughness );
maxSamplesNum = max( maxSamplesNum, 1 );

Expand Down Expand Up @@ -250,15 +222,52 @@ void main( uint2 pixelPos : SV_DispatchThreadId )
float3 L = GetShadowedLighting( geometryProps, materialProps );

{ // Update SHARC cache
SharcHitData sharcHitData = ( SharcHitData )0;
SharcHitData sharcHitData;
sharcHitData.positionWorld = GetGlobalPos( geometryProps.X ) + ( Rng::Hash::GetFloat4( ).xyz - 0.5 ) * SHARC_POS_DITHER;
sharcHitData.normalWorld = normalize( geometryProps.N + ( Rng::Hash::GetFloat4( ).xyz - 0.5 ) * SHARC_NORMAL_DITHER );

SharcSetThroughput( sharcState, throughput );
if( geometryProps.IsSky( ) )
SharcUpdateMiss( sharcState, L );
else if( !SharcUpdateHit( sharcState, sharcHitData, L, Rng::Hash::GetFloat( ) ) )
SharcUpdateMiss( sharcParams, sharcState, L );
else if( !SharcUpdateHit( sharcParams, sharcState, sharcHitData, L, Rng::Hash::GetFloat( ) ) )
break;
}
}
}

[numthreads( 16, 16, 1 )]
void main( uint2 pixelPos : SV_DispatchThreadId )
{
/*
TODO: modify SHARC to support:
- material de-modulation
- 2 levels of detail: fine and coarse ( large voxels )
- firefly suppression
- anti-lag
- dynamic "sceneScale"
- auto "sceneScale" adjustment to guarantee desired number of samples in voxels on average
*/

// Initialize RNG
Rng::Hash::Initialize( pixelPos, gFrameIndex );

// Sample position
float2 sampleUv = ( pixelPos + 0.5 + gJitter * gRectSize ) * SHARC_DOWNSCALE * gInvRectSize;

// Primary ray
float3 Xv = Geometry::ReconstructViewPosition( sampleUv, gCameraFrustum, gNearZ, gOrthoMode );

float3 cameraRayOrigin = Geometry::AffineTransform( gViewToWorld, Xv );
float3 cameraRayDirection = gOrthoMode == 0.0 ? normalize( Geometry::RotateVector( gViewToWorld, Xv ) ) : -gViewDirection.xyz;

// Force some portion of rays to be absolutely random to keep cache alive behind the camera
if( Rng::Hash::GetFloat( ) < 0.2 )
cameraRayDirection = normalize( Rng::Hash::GetFloat4( ).xyz - 0.5 );

// Cast ray
GeometryProps geometryProps = CastRay( cameraRayOrigin, cameraRayDirection, 0.0, INF, GetConeAngleFromAngularRadius( 0.0, gTanPixelAngularRadius * SHARC_DOWNSCALE ), gWorldTlas, FLAG_NON_TRANSPARENT, 0 );

// Opaque path
if( !geometryProps.IsSky( ) )
Trace( geometryProps ); // looping this for 4-8 iterations helps to improve cache quality, but it's expensive
}
Loading

0 comments on commit 3671220

Please sign in to comment.