Skip to content

Commit

Permalink
NRD updated to v4.9.1:
Browse files Browse the repository at this point in the history
- OCCLUSION: better settings
- OCCLUSION: added blue noise usage to compensate the lack of temporal stabilization
- UI: various improvements
- reworked post processing pipeline
- quality of life improvements
- fixed several minor bugs
- updated deps
  • Loading branch information
dzhdanNV committed Aug 14, 2024
1 parent d00cb72 commit b5677a7
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 336 deletions.
2 changes: 1 addition & 1 deletion External/NRIFramework
6 changes: 3 additions & 3 deletions Shaders.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ SharcClear.cs.hlsl -T cs
SharcHashCopy.cs.hlsl -T cs
SharcResolve.cs.hlsl -T cs
SharcUpdate.cs.hlsl -T cs
Temporal.cs.hlsl -T cs
Taa.cs.hlsl -T cs
TraceOpaque.cs.hlsl -T cs
TraceTransparent.cs.hlsl -T cs
Upsample.cs.hlsl -T cs
UpsampleNis.cs.hlsl -T cs
Final.cs.hlsl -T cs
Nis.cs.hlsl -T cs
14 changes: 3 additions & 11 deletions Shaders/DlssAfter.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,10 @@ void main( uint2 pixelPos : SV_DispatchThreadId )

float3 color = gOut_Image[ pixelPos ];

// Tonemap
bool isSrgb = gIsSrgb && ( gOnScreen == SHOW_FINAL || gOnScreen == SHOW_BASE_COLOR );
color = ApplyTonemap( color );

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

float rnd = Rng::Hash::GetFloat( );
color += ( rnd * 2.0 - 1.0 ) / 127.0;

// Conversion
if( gIsSrgb && ( gOnScreen == SHOW_FINAL || gOnScreen == SHOW_BASE_COLOR ) )
color = Color::ToSrgb( color );
if( isSrgb )
color = Color::ToSrgb( saturate( color ) );

// Output
gOut_Image[ pixelPos ] = color;
Expand Down
12 changes: 1 addition & 11 deletions Shaders/DlssBefore.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.

#include "Include/Shared.hlsli"

NRI_RESOURCE( Texture2D<float4>, gIn_ComposedLighting_ViewZ, t, 0, 1 );

NRI_RESOURCE( RWTexture2D<float>, gOut_ViewZ, u, 0, 1 );
NRI_RESOURCE( RWTexture2D<float3>, gInOut_Mv, u, 1, 1 );
NRI_RESOURCE( RWTexture2D<float3>, gOut_FinalImage, u, 2, 1 );

[numthreads( 16, 16, 1 )]
void main( uint2 pixelPos : SV_DispatchThreadId )
Expand All @@ -36,19 +33,12 @@ void main( uint2 pixelPos : SV_DispatchThreadId )
}

// Patch MV, because 2D MVs needed
float3 mv = gInOut_Mv[ pixelPos ];
if( gIsWorldSpaceMotionEnabled )
{
float3 mv = gInOut_Mv[ pixelPos ];
float3 Xprev = Geometry::AffineTransform( gViewToWorld, Xv ) + mv;
float2 pixelUvPrev = Geometry::GetScreenUv( gWorldToClipPrev, Xprev );
mv.xy = ( pixelUvPrev - pixelUv ) * gRenderSize;
gInOut_Mv[ pixelPos ] = mv;
}

// Apply exposure
float3 Lsum = gIn_ComposedLighting_ViewZ[ pixelPos ].xyz;
Lsum = ApplyExposure( Lsum );

// Output
gOut_FinalImage[ pixelPos ] = Lsum;
}
38 changes: 21 additions & 17 deletions Shaders/Upsample.cs.hlsl → Shaders/Final.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.

#include "Include/Shared.hlsli"

NRI_RESOURCE( Texture2D<float4>, gIn_Image, t, 0, 1 );
NRI_RESOURCE( Texture2D<float4>, gIn_Validation, t, 1, 1 );
NRI_RESOURCE( Texture2D<float4>, gIn_PostAA, t, 0, 1 );
NRI_RESOURCE( Texture2D<float4>, gIn_PreAA, t, 1, 1 );
NRI_RESOURCE( Texture2D<float4>, gIn_Validation, t, 2, 1 );

NRI_RESOURCE( RWTexture2D<float3>, gOut_Image, u, 0, 1 );
NRI_RESOURCE( RWTexture2D<float3>, gOut_Final, u, 0, 1 );

[numthreads( 16, 16, 1 )]
void main( uint2 pixelPos : SV_DispatchThreadId )
Expand All @@ -24,23 +25,26 @@ void main( uint2 pixelPos : SV_DispatchThreadId )
if( pixelUv.x > 1.0 || pixelUv.y > 1.0 )
return;

// DLSS upscales render resolution to output resolution
float2 rectSize = gRectSize;
float2 invRenderSize = gInvRenderSize;
if( gSR || gRR )
{
rectSize = gOutputSize;
invRenderSize = gInvOutputSize;
}

// Upsampling
float2 pixelPosScaled = pixelUv * rectSize;
float3 upsampled = BicubicFilterNoCorners( gIn_Image, gLinearSampler, pixelPosScaled, invRenderSize, 0.66 ).xyz;
float3 upsampled = BicubicFilterNoCorners( gIn_PostAA, gLinearSampler, pixelUv * gOutputSize, gInvOutputSize, 0.66 ).xyz;

// Noisy input
float3 input = gIn_PreAA.SampleLevel( gNearestSampler, pixelUv * gRectSize * gInvRenderSize, 0 ).xyz;

bool isSrgb = gIsSrgb && ( gOnScreen == SHOW_FINAL || gOnScreen == SHOW_BASE_COLOR );
input = ApplyTonemap( input );
if( isSrgb )
input = Color::ToSrgb( saturate( input ) );

// Split screen - noisy input / denoised output
float3 input = gIn_Image.SampleLevel( gNearestSampler, pixelPosScaled * invRenderSize, 0 ).xyz;
float3 result = pixelUv.x < gSeparator ? input : upsampled;

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

float rnd = Rng::Hash::GetFloat( );
result += ( rnd - 0.5 ) / ( isSrgb ? 256.0 : 1024.0 );

// Split screen - vertical line
float verticalLine = saturate( 1.0 - abs( pixelUv.x - gSeparator ) * gWindowSize.x / 3.5 );
verticalLine = saturate( verticalLine / 0.5 );
Expand All @@ -52,10 +56,10 @@ void main( uint2 pixelPos : SV_DispatchThreadId )
// Validation layer
if( gValidation )
{
float4 validation = gIn_Validation.SampleLevel( gLinearSampler, pixelUv, 0 );
float4 validation = gIn_Validation.SampleLevel( gNearestSampler, pixelUv, 0 );
result = lerp( result, validation.xyz, validation.w );
}

// Output
gOut_Image[ pixelPos ] = result;
gOut_Final[ pixelPos ] = result;
}
4 changes: 0 additions & 4 deletions Shaders/Include/RaytracingShared.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -579,10 +579,6 @@ float ReprojectIrradiance(
float weight = 1.0;
float2 pixelUv = float2( pixelPos + 0.5 ) * gInvRectSize;

// Ignore undenoised regions ( split screen mode is active )
weight *= float( pixelUv.x > gSeparator );
weight *= float( uv.x > gSeparator );

// Relaxed checks for refractions
float viewZ = abs( Geometry::AffineTransform( isPrevFrame ? gWorldToViewPrev : gWorldToView, geometryProps.X ).z );
float err = ( viewZ - prevViewZ ) * Math::PositiveRcp( max( viewZ, prevViewZ ) );
Expand Down
17 changes: 16 additions & 1 deletion Shaders/Include/Shared.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
// SH - SH (spherical harmonics or spherical gaussian) denoisers
// OCCLUSION - OCCLUSION (ambient or specular occlusion only) denoisers
// DIRECTIONAL_OCCLUSION - DIRECTIONAL_OCCLUSION (ambient occlusion in SH mode) denoisers
#define NRD_MODE NORMAL // NORMAL, SH, OCCLUSION, DIRECTIONAL_OCCLUSION
#define NRD_MODE OCCLUSION // NORMAL, SH, OCCLUSION, DIRECTIONAL_OCCLUSION
#define SIGMA_TRANSLUCENT 1

// Default = 1
#define USE_IMPORTANCE_SAMPLING 1
#define USE_PSR 1 // allow primary surface replacement
#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)

// Default = 0
#define USE_SANITIZATION 0 // NRD sample is NAN/INF free
Expand Down Expand Up @@ -129,10 +130,22 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
#define SHARC_NORMAL_DITHER 0.003
#define SHARC_POS_DITHER 0.001

#define BLUE_NOISE_SPATIAL_DIM 128 // see StaticTexture::ScramblingRanking
#define BLUE_NOISE_TEMPORAL_DIM 4 // good values: 4-8 for shadows, 8-16 for occlusion, 8-32 for lighting

#define MORPH_MAX_ACTIVE_TARGETS_NUM 8u
#define MORPH_ELEMENTS_PER_ROW_NUM 4
#define MORPH_ROWS_NUM ( MORPH_MAX_ACTIVE_TARGETS_NUM / MORPH_ELEMENTS_PER_ROW_NUM )

#define NIS_SCALER 1
#define NIS_HDR_MODE 0
#define NIS_BLOCK_WIDTH 32
#define NIS_BLOCK_HEIGHT 32
#define NIS_THREAD_GROUP_SIZE 128
#define NIS_USE_HALF_PRECISION 1
#define NIS_HLSL 1
#define NIS_VIEWPORT_SUPPORT 0

// Instance flags
#define FLAG_FIRST_BIT 25 // this + number of flags must be <= 32
#define NON_FLAG_MASK ( ( 1 << FLAG_FIRST_BIT ) - 1 )
Expand Down Expand Up @@ -493,6 +506,8 @@ float GetCircleOfConfusion( float distance ) // diameter
#define SKY_INTENSITY 1.0
#define SUN_INTENSITY 10.0

// TODO: add dither, use USE_FP11 = true

float3 GetSunIntensity( float3 v )
{
float b = dot( v, gSunDirection.xyz );
Expand Down
21 changes: 12 additions & 9 deletions Shaders/UpsampleNis.cs.hlsl → Shaders/Nis.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,26 @@ NRI_RESOURCE( RWTexture2D<float4>, gOut_Image, u, 0, 1 );
#pragma warning( disable : 3203 )

// Main
#define NIS_SCALER 1
#define NIS_HDR_MODE 0
#define NIS_BLOCK_WIDTH 32
#define NIS_BLOCK_HEIGHT 32
#define NIS_THREAD_GROUP_SIZE 128
#define NIS_USE_HALF_PRECISION 1
#define NIS_HLSL 1
#define NIS_VIEWPORT_SUPPORT 0

#if( NRI_SHADER_MODEL >= 62 )
#define NIS_HLSL_6_2 1
#endif

#include "NVIDIAImageScaling/NIS/NIS_Scaler.h"

#if( USE_NIS == 1 )

[numthreads( NIS_THREAD_GROUP_SIZE, 1, 1 )]
void main( uint2 blockId : SV_GroupID, uint threadId : SV_GroupThreadID )
{
NVScaler( blockId, threadId );
}

#else

[numthreads( NIS_BLOCK_WIDTH, NIS_BLOCK_HEIGHT, 1 )]
void main( int2 pixelPos : SV_DispatchThreadId )
{
gOut_Image[ pixelPos ] = gIn_Image[ pixelPos ];
}

#endif
22 changes: 4 additions & 18 deletions Shaders/Temporal.cs.hlsl → Shaders/Taa.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void Preload( uint2 sharedPos, int2 globalPos )
globalPos = clamp( globalPos, 0, gRectSize - 1.0 );

float4 color_viewZ = gIn_ComposedLighting_ViewZ[ globalPos ];
color_viewZ.xyz = ApplyExposure( color_viewZ.xyz );
color_viewZ.xyz = ApplyTonemap( color_viewZ.xyz );
color_viewZ.w = abs( color_viewZ.w ) * Math::Sign( gNearZ ) / FP16_VIEWZ_SCALE;

Expand Down Expand Up @@ -128,6 +127,7 @@ void main( int2 threadPos : SV_GroupThreadId, int2 pixelPos : SV_DispatchThreadI

history.xyz = max( history.xyz, 0.0 ); // yes, not "saturate"

// Remove transfer
bool isSrgb = gIsSrgb && ( gOnScreen == SHOW_FINAL || gOnScreen == SHOW_BASE_COLOR );
if( isSrgb )
history.xyz = Color::FromSrgb( history.xyz );
Expand All @@ -138,7 +138,7 @@ void main( int2 threadPos : SV_GroupThreadId, int2 pixelPos : SV_DispatchThreadI

// Disocclusion #1
bool isInScreen = all( saturate( pixelUvPrev ) == pixelUvPrev );
mixRate = ( !isInScreen || pixelUv.x < gSeparator ) ? 1.0 : mixRate;
mixRate = !isInScreen ? 1.0 : mixRate;

// Disocclusion #2
float3 clampedHistory = Color::ClampAabb( m1, sigma, history.xyz );
Expand All @@ -158,24 +158,10 @@ void main( int2 threadPos : SV_GroupThreadId, int2 pixelPos : SV_DispatchThreadI
// Final mix
float3 result = lerp( clampedHistory, input, mixRate );

// Split screen - vertical line
float verticalLine = saturate( 1.0 - abs( pixelUv.x - gSeparator ) * gRectSize.x / 3.5 );
verticalLine = saturate( verticalLine / 0.5 );
verticalLine *= float( gSeparator != 0.0 );
verticalLine *= float( gRenderSize.x == gRectSize.x );

const float3 nvColor = float3( 118.0, 185.0, 0.0 ) / 255.0;
result = lerp( result, nvColor * verticalLine, verticalLine );

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

float rnd = Rng::Hash::GetFloat( );
result += ( rnd * 2.0 - 1.0 ) / 1023.0;

// Output
// Apply transfer
if( isSrgb )
result = Color::ToSrgb( result );

// Output
gOut_Result[ pixelPos ] = float4( result, mixRate );
}
Loading

0 comments on commit b5677a7

Please sign in to comment.