Skip to content

Commit

Permalink
v4.2.1:
Browse files Browse the repository at this point in the history
HIGHLIGHTS:
- bugfixes & improvements

DETAILS:
- NRD: fixed regression previously introduced in projection matrix decomposition
- REBLUR: fixed touching of data outside of rectangle, set by Dynamic Resolution Scaling
- Cmake: "NRD_Shaders" target doesn't get created if "NRD_USE_PRECOMPILED_SHADERS" is not set
- updated MathLib
  • Loading branch information
dzhdanNV committed May 30, 2023
1 parent 40906c5 commit 0ad8056
Show file tree
Hide file tree
Showing 24 changed files with 169 additions and 148 deletions.
156 changes: 80 additions & 76 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@ endif ()

message ("NRD shaders output path: '${NRD_SHADERS_PATH}'")

if (NRD_USE_PRECOMPILED_SHADERS)
target_include_directories (${PROJECT_NAME} PRIVATE "${NRD_SHADERS_PATH}")
target_compile_definitions (${PROJECT_NAME} PRIVATE NRD_USE_PRECOMPILED_SHADERS)
endif ()

target_include_directories (${PROJECT_NAME} PUBLIC "Include")
target_include_directories (${PROJECT_NAME} PRIVATE "External")
target_compile_definitions (${PROJECT_NAME} PRIVATE ${COMPILE_DEFINITIONS})
Expand All @@ -138,76 +133,85 @@ if (NOT TARGET ShaderMake)
endif ()

# Shaders
file (GLOB_RECURSE SHADERS "Shaders/*.hlsl" "Shaders/*.hlsli" "External/MathLib/*.hlsli")
set_source_files_properties (${SHADERS} PROPERTIES VS_TOOL_OVERRIDE "None")
if (NRD_USE_PRECOMPILED_SHADERS)
target_include_directories (${PROJECT_NAME} PRIVATE "${NRD_SHADERS_PATH}")
target_compile_definitions (${PROJECT_NAME} PRIVATE NRD_USE_PRECOMPILED_SHADERS)

if (WIN32)
add_custom_target (${PROJECT_NAME}_Shaders ALL
COMMAND ShaderMake --useAPI
--header ${NRD_SHADER_BINARIES} --flatten --stripReflection --compiler "${DXC_PATH}"
--sourceDir "Shaders/Source"
--allResourcesBound
-p DXIL --WX
-c Shaders.cfg
-o "${NRD_SHADERS_PATH}"
-I "External/MathLib"
-I "Shaders/Include"
-I "Shaders/Resources"
-D NRD_NORMAL_ENCODING=${NRD_NORMAL_ENCODING}
-D NRD_ROUGHNESS_ENCODING=${NRD_ROUGHNESS_ENCODING}
-D NRD_INTERNAL
COMMAND ShaderMake --useAPI
--header ${NRD_SHADER_BINARIES} --flatten --stripReflection --compiler "${DXC_SPIRV_PATH}"
--sourceDir "Shaders/Source"
--allResourcesBound
-p SPIRV --WX
-c Shaders.cfg
-o "${NRD_SHADERS_PATH}"
-I "External/MathLib"
-I "Shaders/Include"
-I "Shaders/Resources"
-D NRD_NORMAL_ENCODING=${NRD_NORMAL_ENCODING}
-D NRD_ROUGHNESS_ENCODING=${NRD_ROUGHNESS_ENCODING}
-D NRD_INTERNAL
COMMAND ShaderMake --useAPI
--header ${NRD_SHADER_BINARIES} --flatten --stripReflection --compiler "${FXC_PATH}"
--sourceDir "Shaders/Source"
--allResourcesBound
-p DXBC --WX
-c Shaders.cfg
-o "${NRD_SHADERS_PATH}"
-I "External/MathLib"
-I "Shaders/Include"
-I "Shaders/Resources"
-D NRD_NORMAL_ENCODING=${NRD_NORMAL_ENCODING}
-D NRD_ROUGHNESS_ENCODING=${NRD_ROUGHNESS_ENCODING}
-D NRD_INTERNAL
DEPENDS ShaderMake
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM
SOURCES ${SHADERS}
)
else ()
add_custom_target (${PROJECT_NAME}_Shaders ALL
COMMAND ShaderMake
--header ${NRD_SHADER_BINARIES} --flatten --stripReflection --compiler "${DXC_SPIRV_PATH}"
--sourceDir "Shaders/Source"
--allResourcesBound
-p SPIRV --WX
-c Shaders.cfg
-o "${NRD_SHADERS_PATH}"
-I "External/MathLib"
-I "Shaders/Include"
-I "Shaders/Resources"
-D NRD_NORMAL_ENCODING=${NRD_NORMAL_ENCODING}
-D NRD_ROUGHNESS_ENCODING=${NRD_ROUGHNESS_ENCODING}
-D NRD_INTERNAL
DEPENDS ShaderMake
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM
SOURCES ${SHADERS}
)
endif ()
file (GLOB_RECURSE SHADERS "Shaders/*.hlsl" "Shaders/*.hlsli" "External/MathLib/*.hlsli")
set_source_files_properties (${SHADERS} PROPERTIES VS_TOOL_OVERRIDE "None")

if (WIN32)
add_custom_target (${PROJECT_NAME}_Shaders ALL
COMMAND ShaderMake --useAPI
--header ${NRD_SHADER_BINARIES} --flatten --stripReflection --compiler "${DXC_PATH}"
--sourceDir "Shaders/Source"
--allResourcesBound
-p DXIL --WX
-c Shaders.cfg
-o "${NRD_SHADERS_PATH}"
-I "External/MathLib"
-I "Shaders/Include"
-I "Shaders/Resources"
-D NRD_NORMAL_ENCODING=${NRD_NORMAL_ENCODING}
-D NRD_ROUGHNESS_ENCODING=${NRD_ROUGHNESS_ENCODING}
-D NRD_INTERNAL
COMMAND ShaderMake --useAPI
--header ${NRD_SHADER_BINARIES} --flatten --stripReflection --compiler "${DXC_SPIRV_PATH}"
--sourceDir "Shaders/Source"
--allResourcesBound
--sRegShift 100
--tRegShift 200
--bRegShift 300
--uRegShift 400
-p SPIRV --WX
-c Shaders.cfg
-o "${NRD_SHADERS_PATH}"
-I "External/MathLib"
-I "Shaders/Include"
-I "Shaders/Resources"
-D NRD_NORMAL_ENCODING=${NRD_NORMAL_ENCODING}
-D NRD_ROUGHNESS_ENCODING=${NRD_ROUGHNESS_ENCODING}
-D NRD_INTERNAL
COMMAND ShaderMake --useAPI
--header ${NRD_SHADER_BINARIES} --flatten --stripReflection --compiler "${FXC_PATH}"
--sourceDir "Shaders/Source"
--allResourcesBound
-p DXBC --WX
-c Shaders.cfg
-o "${NRD_SHADERS_PATH}"
-I "External/MathLib"
-I "Shaders/Include"
-I "Shaders/Resources"
-D NRD_NORMAL_ENCODING=${NRD_NORMAL_ENCODING}
-D NRD_ROUGHNESS_ENCODING=${NRD_ROUGHNESS_ENCODING}
-D NRD_INTERNAL
DEPENDS ShaderMake
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM
SOURCES ${SHADERS}
)
else ()
add_custom_target (${PROJECT_NAME}_Shaders ALL
COMMAND ShaderMake
--header ${NRD_SHADER_BINARIES} --flatten --stripReflection --compiler "${DXC_SPIRV_PATH}"
--sourceDir "Shaders/Source"
--allResourcesBound
-p SPIRV --WX
-c Shaders.cfg
-o "${NRD_SHADERS_PATH}"
-I "External/MathLib"
-I "Shaders/Include"
-I "Shaders/Resources"
-D NRD_NORMAL_ENCODING=${NRD_NORMAL_ENCODING}
-D NRD_ROUGHNESS_ENCODING=${NRD_ROUGHNESS_ENCODING}
-D NRD_INTERNAL
DEPENDS ShaderMake
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM
SOURCES ${SHADERS}
)
endif ()

set_property (TARGET ${PROJECT_NAME}_Shaders PROPERTY FOLDER ${PROJECT_NAME})
add_dependencies (${PROJECT_NAME} ${PROJECT_NAME}_Shaders)
set_property (TARGET ${PROJECT_NAME}_Shaders PROPERTY FOLDER ${PROJECT_NAME})
add_dependencies (${PROJECT_NAME} ${PROJECT_NAME}_Shaders)
endif ()
2 changes: 1 addition & 1 deletion External/MathLib
4 changes: 2 additions & 2 deletions Include/NRD.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.

#define NRD_VERSION_MAJOR 4
#define NRD_VERSION_MINOR 2
#define NRD_VERSION_BUILD 0
#define NRD_VERSION_DATE "23 May 2023"
#define NRD_VERSION_BUILD 1
#define NRD_VERSION_DATE "25 May 2023"

#if defined(_MSC_VER)
#define NRD_CALL __fastcall
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NVIDIA REAL-TIME DENOISERS v4.2.0 (NRD)
# NVIDIA REAL-TIME DENOISERS v4.2.1 (NRD)

[![Build NRD SDK](https://github.com/NVIDIAGameWorks/RayTracingDenoiser/actions/workflows/build.yml/badge.svg)](https://github.com/NVIDIAGameWorks/RayTracingDenoiser/actions/workflows/build.yml)

Expand Down
2 changes: 1 addition & 1 deletion Resources/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ Versioning rules:

#define VERSION_MAJOR 4
#define VERSION_MINOR 2
#define VERSION_BUILD 0
#define VERSION_BUILD 1

#define VERSION_STRING STR(VERSION_MAJOR.VERSION_MINOR.VERSION_BUILD encoding=NRD_NORMAL_ENCODING.NRD_ROUGHNESS_ENCODING)
22 changes: 13 additions & 9 deletions Shaders/Include/Common.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ float GetGaussianWeight( float r )

// Upsampling

// TODO: used only for history resampling. This is why "gRectSizePrev" is hardcoded
#define _BicubicFilterNoCornersWithFallbackToBilinearFilterWithCustomWeights_Init \
/* Catmul-Rom with 12 taps ( excluding corners ) */ \
float2 centerPos = floor( samplePos - 0.5 ) + 0.5; \
Expand All @@ -463,11 +464,12 @@ float GetGaussianWeight( float r )
w4 = useBicubic ? w4 : 0.0; \
float sum = dot( w, 1.0 ) + w4; \
/* Texture coordinates */ \
float2 uv0 = centerPos + ( useBicubic ? float2( tc.x, -1.0 ) : float2( 0, 0 ) ); \
float2 uv1 = centerPos + ( useBicubic ? float2( -1.0, tc.y ) : float2( 1, 0 ) ); \
float2 uv2 = centerPos + ( useBicubic ? float2( tc.x, tc.y ) : float2( 0, 1 ) ); \
float2 uv3 = centerPos + ( useBicubic ? float2( 2.0, tc.y ) : float2( 1, 1 ) ); \
float2 uv4 = centerPos + ( useBicubic ? float2( tc.x, 2.0 ) : f ); // can be used to get a free bilinear sample after some massaging
float2 uv0 = min( centerPos + ( useBicubic ? float2( tc.x, -1.0 ) : float2( 0, 0 ) ), gRectSizePrev - 1.0 ); \
float2 uv1 = min( centerPos + ( useBicubic ? float2( -1.0, tc.y ) : float2( 1, 0 ) ), gRectSizePrev - 1.0 ); \
float2 uv2 = min( centerPos + ( useBicubic ? float2( tc.x, tc.y ) : float2( 0, 1 ) ), gRectSizePrev - 1.0 ); \
float2 uv3 = min( centerPos + ( useBicubic ? float2( 2.0, tc.y ) : float2( 1, 1 ) ), gRectSizePrev - 1.0 ); \
float2 uv4 = min( centerPos + ( useBicubic ? float2( tc.x, 2.0 ) : f ), gRectSizePrev - 1.0 ); \
float4 bilinearTaps

/*
IMPORTANT:
Expand All @@ -488,10 +490,12 @@ IMPORTANT:

#define _BilinearFilterWithCustomWeights_Color( color, tex ) \
/* Sampling */ \
color = tex.SampleLevel( gNearestClamp, centerPos * invTextureSize, 0 ) * bilinearCustomWeights.x; \
color += tex.SampleLevel( gNearestClamp, centerPos * invTextureSize, 0, int2( 1, 0 ) ) * bilinearCustomWeights.y; \
color += tex.SampleLevel( gNearestClamp, centerPos * invTextureSize, 0, int2( 0, 1 ) ) * bilinearCustomWeights.z; \
color += tex.SampleLevel( gNearestClamp, centerPos * invTextureSize, 0, int2( 1, 1 ) ) * bilinearCustomWeights.w; \
bilinearTaps = centerPos.xyxy + float4( 0.0, 0.0, 1.0, 1.0 ); \
bilinearTaps.zw = min( bilinearTaps.zw, gRectSizePrev - 1.0 ); \
color = tex.SampleLevel( gNearestClamp, bilinearTaps.xy * invTextureSize, 0 ) * bilinearCustomWeights.x; \
color += tex.SampleLevel( gNearestClamp, bilinearTaps.zy * invTextureSize, 0 ) * bilinearCustomWeights.y; \
color += tex.SampleLevel( gNearestClamp, bilinearTaps.xw * invTextureSize, 0 ) * bilinearCustomWeights.z; \
color += tex.SampleLevel( gNearestClamp, bilinearTaps.zw * invTextureSize, 0 ) * bilinearCustomWeights.w; \
/* Normalize similarly to "STL::Filtering::ApplyBilinearCustomWeights()" */ \
sum = dot( bilinearCustomWeights, 1.0 ); \
color = sum < 0.0001 ? 0 : color * rcp( sum );
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
w *= GetCombinedWeight( geometryWeightParams, Nv, Xvs, normalWeightParams, N, Ns );
w *= lerp( minHitDistWeight, 1.0, GetHitDistanceWeight( hitDistanceWeightParams, ExtractHitDist( s ) ) );

// Get rid of potentially bad values outside of the screen ( important for checkerboard )
w = IsInScreen( uv ) ? w : 0.0;
s = w ? s : 0.0;
// Get rid of potentially bad values outside of the screen
w = ( IsInScreen( uv ) && !isnan( w ) ) ? w : 0.0;
s = w != 0.0 ? s : 0.0;

// Accumulate
sum += w;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
#endif
#endif

// Get rid of potentially bad values outside of the screen ( important for checkerboard )
w = IsInScreen( uv ) ? w : 0.0;
s = w ? s : 0.0;
// Get rid of potentially bad values outside of the screen
w = ( IsInScreen( uv ) && !isnan( w ) ) ? w : 0.0;
s = w != 0.0 ? s : 0.0;

#if( REBLUR_SPATIAL_MODE == REBLUR_PRE_BLUR )
// Min hit distance for tracking
Expand Down
2 changes: 1 addition & 1 deletion Shaders/Include/REBLUR/REBLUR_DiffuseSpecular_Blur.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ NRD_EXPORT void NRD_CS_MAIN( int2 threadPos : SV_GroupThreadId, int2 pixelPos :

// Tile-based early out
float isSky = gIn_Tiles[ pixelPos >> 4 ];
if( isSky != 0.0 )
if( isSky != 0.0 || pixelPos.x >= gRectSize.x || pixelPos.y >= gRectSize.y )
return;

// Early out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ NRD_EXPORT void NRD_CS_MAIN( uint2 pixelPos : SV_DispatchThreadId )
{
// Tile-based early out
float isSky = gIn_Tiles[ pixelPos >> 4 ];
if( isSky != 0.0 )
if( isSky != 0.0 && !gIsRectChanged )
return;

// TODO: is it possible to introduce "CopyResource" in NRD API?
Expand Down
46 changes: 27 additions & 19 deletions Shaders/Include/REBLUR/REBLUR_DiffuseSpecular_HistoryFix.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ NRD_EXPORT void NRD_CS_MAIN( int2 threadPos : SV_GroupThreadId, int2 pixelPos :
PRELOAD_INTO_SMEM_WITH_TILE_CHECK;

// Tile-based early out
if( isSky != 0.0 )
if( isSky != 0.0 || pixelPos.x >= gRectSize.x || pixelPos.y >= gRectSize.y )
return;

// Early out
Expand Down Expand Up @@ -149,19 +149,23 @@ NRD_EXPORT void NRD_CS_MAIN( int2 threadPos : SV_GroupThreadId, int2 pixelPos :
float angle = STL::Math::AcosApprox( cosa );

// Accumulate
float wd = IsInScreen( uv );
wd *= CompareMaterials( materialID, materialIDs, gDiffMaterialMask );
wd *= _ComputeWeight( NoX, diffGeometryWeightParams.x, diffGeometryWeightParams.y );
wd *= _ComputeExponentialWeight( angle, diffNormalWeightParam, 0.0 );
float w = IsInScreen( uv );
w *= CompareMaterials( materialID, materialIDs, gDiffMaterialMask );
w *= _ComputeWeight( NoX, diffGeometryWeightParams.x, diffGeometryWeightParams.y );
w *= _ComputeExponentialWeight( angle, diffNormalWeightParam, 0.0 );

REBLUR_TYPE d = gIn_Diff.SampleLevel( gNearestClamp, uvScaled, 0 );
REBLUR_TYPE s = gIn_Diff.SampleLevel( gNearestClamp, uvScaled, 0 );

diff += d * wd;
sumd += wd;
// Get rid of potentially bad values outside of the screen
w = IsInScreen( uv ) ? w : 0.0; // no "!isnan" because "s" is not used for "w" calculations
s = w != 0.0 ? s : 0.0;

diff += s * w;
sumd += w;

#ifdef REBLUR_SH
float4 dh = gIn_DiffSh.SampleLevel( gNearestClamp, uvScaled, 0 );
diffSh += dh * wd;
float4 sh = gIn_DiffSh.SampleLevel( gNearestClamp, uvScaled, 0 );
diffSh += sh * w;
#endif
}
}
Expand Down Expand Up @@ -329,25 +333,29 @@ NRD_EXPORT void NRD_CS_MAIN( int2 threadPos : SV_GroupThreadId, int2 pixelPos :
float angle = STL::Math::AcosApprox( cosa );

// Accumulate
float ws = IsInScreen( uv );
ws *= CompareMaterials( materialID, materialIDs, gSpecMaterialMask );
ws *= _ComputeWeight( NoX, specGeometryWeightParams.x, specGeometryWeightParams.y );
ws *= _ComputeExponentialWeight( angle, specNormalWeightParam, 0.0 );
ws *= _ComputeExponentialWeight( Ns.w * Ns.w, specRoughnessWeightParamsSq.x, specRoughnessWeightParamsSq.y );
float w = IsInScreen( uv );
w *= CompareMaterials( materialID, materialIDs, gSpecMaterialMask );
w *= _ComputeWeight( NoX, specGeometryWeightParams.x, specGeometryWeightParams.y );
w *= _ComputeExponentialWeight( angle, specNormalWeightParam, 0.0 );
w *= _ComputeExponentialWeight( Ns.w * Ns.w, specRoughnessWeightParamsSq.x, specRoughnessWeightParamsSq.y );

REBLUR_TYPE s = gIn_Spec.SampleLevel( gNearestClamp, uvScaled, 0 );

// TODO: ideally "diffuseness at hit" needed...
// TODO: for roughness closer to REBLUR_HISTORY_FIX_BUMPED_ROUGHNESS "saturate( hitDistNormAtCenter - ExtractHitDist( s ) )" could be used.
// It allows bleeding of background to foreground, but not vice versa ( doesn't suit for 0 roughness )
ws *= saturate( 1.0 - hitDistWeightScale * abs( ExtractHitDist( s ) - hitDistNormAtCenter ) / ( max( ExtractHitDist( s ), hitDistNormAtCenter ) + NRD_EPS ) );
w *= saturate( 1.0 - hitDistWeightScale * abs( ExtractHitDist( s ) - hitDistNormAtCenter ) / ( max( ExtractHitDist( s ), hitDistNormAtCenter ) + NRD_EPS ) );

// Get rid of potentially bad values outside of the screen
w = ( IsInScreen( uv ) && !isnan( w ) ) ? w : 0.0;
s = w != 0.0 ? s : 0.0;

spec += s * ws;
sums += ws;
spec += s * w;
sums += w;

#ifdef REBLUR_SH
float4 sh = gIn_SpecSh.SampleLevel( gNearestClamp, uvScaled, 0 );
specSh += sh * ws;
specSh += sh * w;
#endif
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ NRD_EXPORT void NRD_CS_MAIN( int2 threadPos : SV_GroupThreadId, int2 pixelPos :
PRELOAD_INTO_SMEM_WITH_TILE_CHECK;

// Tile-based early out
if( isSky != 0.0 )
if( isSky != 0.0 || pixelPos.x >= gRectSize.x || pixelPos.y >= gRectSize.y )
return;

// Early out
Expand Down
4 changes: 2 additions & 2 deletions Shaders/Include/REBLUR/REBLUR_DiffuseSpecular_PostBlur.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ NRD_EXPORT void NRD_CS_MAIN( int2 threadPos : SV_GroupThreadId, int2 pixelPos :

// Tile-based early out
float isSky = gIn_Tiles[ pixelPos >> 4 ];
if( isSky != 0.0 )
if( isSky != 0.0 || pixelPos.x >= gRectSize.x || pixelPos.y >= gRectSize.y )
{
// ~0 normal is needed to allow bilinear filter in TA ( 0 can't be used due to "division by zero" in "UnpackNormalRoughness" )
gOut_Normal_Roughness[ pixelPos ] = PackNormalRoughness( 1.0 / 255.0 );
Expand All @@ -26,7 +26,7 @@ NRD_EXPORT void NRD_CS_MAIN( int2 threadPos : SV_GroupThreadId, int2 pixelPos :

// Early out
float viewZ = UnpackViewZ( gIn_ViewZ[ pixelPos ] );
if( viewZ > gDenoisingRange )
if( viewZ > gDenoisingRange || pixelPos.x >= gRectSize.x || pixelPos.y >= gRectSize.y )
{
// ~0 normal is needed to allow bilinear filter in TA ( 0 can't be used due to "division by zero" in "UnpackNormalRoughness" )
gOut_Normal_Roughness[ pixelPos ] = PackNormalRoughness( 1.0 / 255.0 );
Expand Down
Loading

0 comments on commit 0ad8056

Please sign in to comment.