Skip to content

Commit

Permalink
v4.2.2:
Browse files Browse the repository at this point in the history
HIGHLIGHTS:
- various improvements & bug fixes

DETAILS:
- NRD: added optional "NRD_SG_Rotate"
- NRD: added missing sanitization of direction in SH packing functions
- NRD: added explicit control on which types of shaders (DXBC, DXIL and/or SPIRV) to compile and embed into the binary
- REBLUR: added more checks to avoid accessing potential NANs if a pixel is outside of denoising range
- REBLUR: fixed anti-firefly with enabled dynamic resolution scaling
- SIGMA: minor bug fix
- updated dependencies
- updated README
  • Loading branch information
dzhdanNV committed Jul 4, 2023
1 parent 3c881ae commit c20d25a
Show file tree
Hide file tree
Showing 18 changed files with 862 additions and 677 deletions.
129 changes: 66 additions & 63 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
cmake_minimum_required (VERSION 3.15)

# Options
option (NRD_DISABLE_SHADER_COMPILATION "Disable shader compilation" OFF)
option (NRD_USE_PRECOMPILED_SHADERS "Use precompiled (embedded) shaders" ON)
option (NRD_STATIC_LIBRARY "Build static library" OFF)
if (WIN32)
set (IS_WIN ON)
else ()
set (IS_WIN OFF)
endif ()

# Windows specific
if(WIN32)
option (NRD_PRECOMPILE_SPIRV_SHADERS "Precompile SPIRV shaders" ON)
option (NRD_PRECOMPILE_D3D_SHADERS "Precompile DXIL shaders" ON)
endif()
option (NRD_STATIC_LIBRARY "Build static library" OFF)
option (NRD_EMBEDS_SPIRV_SHADERS "NRD embeds SPIRV shaders" ON)
option (NRD_EMBEDS_DXIL_SHADERS "NRD embeds DXIL shaders" ${IS_WIN})
option (NRD_EMBEDS_DXBC_SHADERS "NRD embeds DXBC shaders" ${IS_WIN})
option (NRD_DISABLE_SHADER_COMPILATION "Disable shader compilation" OFF)

# Is submodule?
if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
set(IS_SUBMODULE OFF)
else()
set(IS_SUBMODULE ON)
endif()
set (IS_SUBMODULE OFF)
else ()
set (IS_SUBMODULE ON)
endif ()

# Cached
if (NOT IS_SUBMODULE)
set (CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "")
endif()
endif ()

set (NRD_DXC_CUSTOM_PATH "custom/path/to/dxc" CACHE STRING "Custom DXC to use if Vulkan SDK is not installed")
set (NRD_SHADERS_PATH "" CACHE STRING "Shader output path override")
Expand Down Expand Up @@ -73,13 +75,20 @@ endif ()
# Compile definitions
set (COMPILE_DEFINITIONS NRD_NORMAL_ENCODING=${NRD_NORMAL_ENCODING} NRD_ROUGHNESS_ENCODING=${NRD_ROUGHNESS_ENCODING})

if (NRD_EMBEDS_SPIRV_SHADERS)
set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRD_EMBEDS_SPIRV_SHADERS)
endif ()

if (NRD_EMBEDS_DXIL_SHADERS)
set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRD_EMBEDS_DXIL_SHADERS)
endif ()

if (NRD_EMBEDS_DXBC_SHADERS)
set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRD_EMBEDS_DXBC_SHADERS)
endif ()

if (WIN32)
if (NRD_PRECOMPILE_SPIRV_SHADERS AND NOT NRD_PRECOMPILE_D3D_SHADERS)
set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRD_ONLY_SPIRV_SHADERS_AVAILABLE=1)
endif ()
set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} WIN32_LEAN_AND_MEAN NOMINMAX _CRT_SECURE_NO_WARNINGS _UNICODE UNICODE _ENFORCE_MATCHING_ALLOCATORS=0)
else ()
set (COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} NRD_ONLY_SPIRV_SHADERS_AVAILABLE=1)
endif ()

# External/MathLib
Expand Down Expand Up @@ -135,28 +144,27 @@ set_target_properties (${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${GL
set_target_properties (${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${GLOBAL_BIN_OUTPUT_PATH}/$<CONFIG>")
message ("NRD output path: '${GLOBAL_BIN_OUTPUT_PATH}'")

# External/ShaderMake
if (NOT TARGET ShaderMake)
set (SHADERMAKE_BIN_OUTPUT_PATH ${GLOBAL_BIN_OUTPUT_PATH} CACHE STRING "")
add_subdirectory (External/ShaderMake)
endif ()

# Shaders
if (NRD_USE_PRECOMPILED_SHADERS)
if (NOT NRD_DISABLE_SHADER_COMPILATION)
target_include_directories (${PROJECT_NAME} PRIVATE "${NRD_SHADERS_PATH}")
target_compile_definitions (${PROJECT_NAME} PRIVATE NRD_USE_PRECOMPILED_SHADERS)

file (GLOB_RECURSE SHADERS "Shaders/*.hlsl" "Shaders/*.hlsli" "External/MathLib/*.hlsli")
set_source_files_properties (${SHADERS} PROPERTIES VS_TOOL_OVERRIDE "None")

# Arguements for ShaderMake
set(SHADERMAKE_GENERAL_ARGS
--WX
--header ${NRD_SHADER_BINARIES}

# External/ShaderMake
if (NOT TARGET ShaderMake)
set (SHADERMAKE_BIN_OUTPUT_PATH ${GLOBAL_BIN_OUTPUT_PATH} CACHE STRING "")
add_subdirectory (External/ShaderMake)
endif ()

# ShaderMake general arguments
set (SHADERMAKE_GENERAL_ARGS
--header ${NRD_SHADER_BINARIES}
--flatten
--stripReflection
--stripReflection
--sourceDir "Shaders/Source"
--allResourcesBound
--WX
-c Shaders.cfg
-o "${NRD_SHADERS_PATH}"
-I "External/MathLib"
Expand All @@ -166,40 +174,35 @@ if (NRD_USE_PRECOMPILED_SHADERS)
-D NRD_ROUGHNESS_ENCODING=${NRD_ROUGHNESS_ENCODING}
-D NRD_INTERNAL
)
if (WIN32)
# Arguements for ShaderMake depending on which backend is used
set(SHADERMAKE_COMMANDS "")
if(NRD_PRECOMPILE_D3D_SHADERS) # DXIL Commands
set(SHADERMAKE_COMMANDS ${SHADERMAKE_COMMANDS} COMMAND ShaderMake --useAPI -p DXIL --compiler "${DXC_PATH}" ${SHADERMAKE_GENERAL_ARGS})
endif()
if(NRD_PRECOMPILE_D3D_SHADERS) # DXBC Commands
set(SHADERMAKE_COMMANDS ${SHADERMAKE_COMMANDS} COMMAND ShaderMake --useAPI -p DXBC --compiler "${FXC_PATH}" ${SHADERMAKE_GENERAL_ARGS})
endif()
if(NRD_PRECOMPILE_SPIRV_SHADERS)
set(SHADERMAKE_COMMANDS ${SHADERMAKE_COMMANDS} COMMAND ShaderMake --useAPI -p SPIRV --compiler "${DXC_SPIRV_PATH}" ${SHADERMAKE_GENERAL_ARGS}
--sRegShift 100
--tRegShift 200
--bRegShift 300
--uRegShift 400
)
endif()

# Add the target with the commands
add_custom_target (${PROJECT_NAME}_Shaders ALL ${SHADERMAKE_COMMANDS}
DEPENDS ShaderMake
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM
SOURCES ${SHADERS})
else ()
add_custom_target (${PROJECT_NAME}_Shaders ALL
COMMAND ShaderMake -p SPIRV --compiler "${DXC_SPIRV_PATH}" ${SHADERMAKE_GENERAL_ARGS}
DEPENDS ShaderMake
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM
SOURCES ${SHADERS}

# ShaderMake commands for each shader code container
set (SHADERMAKE_COMMANDS "")

if (NRD_EMBEDS_DXBC_SHADERS)
set (SHADERMAKE_COMMANDS ${SHADERMAKE_COMMANDS} COMMAND ShaderMake -p DXBC --compiler "${FXC_PATH}" --useAPI ${SHADERMAKE_GENERAL_ARGS})
endif ()

if (NRD_EMBEDS_DXIL_SHADERS)
set (SHADERMAKE_COMMANDS ${SHADERMAKE_COMMANDS} COMMAND ShaderMake -p DXIL --compiler "${DXC_PATH}" --useAPI ${SHADERMAKE_GENERAL_ARGS})
endif ()

if (NRD_EMBEDS_SPIRV_SHADERS)
set (SHADERMAKE_COMMANDS ${SHADERMAKE_COMMANDS} COMMAND ShaderMake -p SPIRV --compiler "${DXC_SPIRV_PATH}" ${SHADERMAKE_GENERAL_ARGS}
--sRegShift 100
--tRegShift 200
--bRegShift 300
--uRegShift 400
)
endif ()

# Add the target with the commands
add_custom_target (${PROJECT_NAME}_Shaders ALL ${SHADERMAKE_COMMANDS}
DEPENDS ShaderMake
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM
SOURCES ${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
Submodule MathLib updated 4 files
+1 −1 External/sse2neon
+2 −2 MathLib.h
+44 −37 MathLib_d.h
+44 −37 MathLib_f.h
2 changes: 1 addition & 1 deletion External/ShaderMake
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 1
#define NRD_VERSION_DATE "25 May 2023"
#define NRD_VERSION_BUILD 2
#define NRD_VERSION_DATE "4 July 2023"

#if defined(_MSC_VER)
#define NRD_CALL __fastcall
Expand Down
2 changes: 1 addition & 1 deletion Include/NRDDescs.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ namespace nrd
OUT_DELTA_MV,

// (Optional) Debug output (RGBA8+), .w = transparency
// Written to if "InstanceCreationDesc::allowValidation = true" and "CommonSettings::enableValidation = true"
// Used if "CommonSettings::enableValidation = true"
OUT_VALIDATION,

//=============================================================================================================================
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NVIDIA REAL-TIME DENOISERS v4.2.1 (NRD)
# NVIDIA REAL-TIME DENOISERS v4.2.2 (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 Expand Up @@ -40,7 +40,7 @@ For diffuse and specular signals de-modulated irradiance (i.e. irradiance with "
- Install on
- Windows: latest *WindowsSDK* (22000+), *VulkanSDK* (1.3.216+)
- Linux (x86-64): latest *VulkanSDK*
- Linux (aarch64): find a precompiled binary for [*DXC*](https://github.com/microsoft/DirectXShaderCompiler) or disable shader compilation `NRD_DISABLE_SHADER_COMPILATION=OFF`
- Linux (aarch64): find a precompiled binary for [*DXC*](https://github.com/microsoft/DirectXShaderCompiler) or disable shader compilation `NRD_EMBEDS_SPIRV_SHADERS=OFF`
- Build (variant 1) - using *Git* and *CMake* explicitly
- Clone project and init submodules
- Generate and build the project using *CMake*
Expand All @@ -49,13 +49,15 @@ For diffuse and specular signals de-modulated irradiance (i.e. irradiance with "
- Run `2-Build`

CMake options:
- `NRD_DXC_CUSTOM_PATH = "custom/path/to/dxc"` - custom DXC to use if Vulkan SDK is not installed
- `NRD_SHADERS_PATH` - shader output path override
- `NRD_STATIC_LIBRARY` - build static library (OFF by default)
- `NRD_DXC_CUSTOM_PATH` - custom DXC to use if Vulkan SDK is not installed
- `NRD_NORMAL_ENCODING` - *normal* encoding for the entire library
- `NRD_ROUGHNESS_ENCODING` - *roughness* encoding for the entire library
- `NRD_DISABLE_SHADER_COMPILATION` - disable shader compilation (shaders can be compiled on another platform)
- `NRD_USE_PRECOMPILED_SHADERS` - use precompiled shaders (will be embedded into the library)
- `NRD_STATIC_LIBRARY` - build static library
- `NRD_EMBEDS_DXBC_SHADERS` - NRD compiles and embeds DXBC shaders (ON by default on Windows)
- `NRD_EMBEDS_DXIL_SHADERS` - NRD compiles and embeds DXIL shaders (ON by default on Windows)
- `NRD_EMBEDS_SPIRV_SHADERS` - NRD compiles and embeds SPIRV shaders (ON by default)
- `NRD_DISABLE_SHADER_COMPILATION` - disable shader compilation on the NRD side, NRD assumes that shaders are already compiled externally and have been put into `NRD_SHADERS_PATH` folder

`NRD_NORMAL_ENCODING` and `NRD_ROUGHNESS_ENCODING` can be defined only *once* during project deployment. These settings are dumped in `NRDEncoding.hlsli` file, which needs to be included on the application side prior `NRD.hlsli` inclusion to deliver encoding settings matching *NRD* settings. `LibraryDesc` includes encoding settings too. It can be used to verify that the library meets the application expectations.

Expand Down Expand Up @@ -247,6 +249,8 @@ else
}
```

Re-jittering math with minorly modified inputs can also be used with RESTIR produced sampling without involving SH denoisers. You only need to get light direction in the current pixel from RESTIR. Despite that RESTIR produces noisy light selections, its low variations can be easily handled by DLSS or other upscaling techs.

# VALIDATION LAYER

![Validation](Images/Validation.png)
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 1
#define VERSION_BUILD 2

#define VERSION_STRING STR(VERSION_MAJOR.VERSION_MINOR.VERSION_BUILD encoding=NRD_NORMAL_ENCODING.NRD_ROUGHNESS_ENCODING)
6 changes: 6 additions & 0 deletions Shaders/Include/NRD.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ float4 REBLUR_FrontEnd_PackSh( float3 radiance, float normHitDist, float3 direct
{
radiance = any( isnan( radiance ) | isinf( radiance ) ) ? 0 : clamp( radiance, 0, NRD_FP16_MAX );
normHitDist = ( isnan( normHitDist ) | isinf( normHitDist ) ) ? 0 : saturate( normHitDist );
direction = any( isnan( direction ) | isinf( direction ) ) ? 0 : clamp( direction, -NRD_FP16_MAX, NRD_FP16_MAX );
}

// "0" is reserved to mark "no data" samples, skipped due to probabilistic sampling
Expand Down Expand Up @@ -852,6 +853,11 @@ float NRD_SG_ExtractRoughnessAA( NRD_SG sg )
return sg.sharpness;
}

void NRD_SG_Rotate( inout NRD_SG sg, float3x3 rotation )
{
sg.c1 = mul( rotation, sg.c1 );
}

float3 NRD_SG_ResolveDiffuse( NRD_SG sg, float3 N )
{
// https://therealmjp.github.io/posts/sg-series-part-3-diffuse-lighting-from-an-sg-light-source/
Expand Down
10 changes: 9 additions & 1 deletion Shaders/Include/REBLUR/REBLUR_Common_DiffuseSpatialFilter.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ 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
// Get rid of potential NANs outside of rendering rectangle or denoising range
w = ( IsInScreen( uv ) && !isnan( w ) ) ? w : 0.0;
s = w != 0.0 ? s : 0.0;

Expand Down Expand Up @@ -174,12 +174,20 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
REBLUR_TYPE s0 = gIn_Diff[ checkerboardPos.xy ];
REBLUR_TYPE s1 = gIn_Diff[ checkerboardPos.zy ];

// Get rid of potential NANs outside of rendering rectangle or denoising range
s0 = wc.x != 0.0 ? s0 : 0;
s1 = wc.y != 0.0 ? s1 : 0;

diff = s0 * wc.x + s1 * wc.y;

#ifdef REBLUR_SH
float4 sh0 = gIn_DiffSh[ checkerboardPos.xy ];
float4 sh1 = gIn_DiffSh[ checkerboardPos.zy ];

// Get rid of potential NANs outside of rendering rectangle or denoising range
sh0 = wc.x != 0.0 ? sh0 : 0;
sh1 = wc.y != 0.0 ? sh1 : 0;

diffSh = sh0 * wc.x + sh1 * wc.y;
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
#endif
#endif

// Get rid of potentially bad values outside of the screen
// Get rid of potential NANs outside of rendering rectangle or denoising range
w = ( IsInScreen( uv ) && !isnan( w ) ) ? w : 0.0;
s = w != 0.0 ? s : 0.0;

Expand Down Expand Up @@ -222,12 +222,20 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
REBLUR_TYPE s0 = gIn_Spec[ checkerboardPos.xy ];
REBLUR_TYPE s1 = gIn_Spec[ checkerboardPos.zy ];

// Get rid of potential NANs outside of rendering rectangle or denoising range
s0 = wc.x != 0.0 ? s0 : 0;
s1 = wc.y != 0.0 ? s1 : 0;

spec = s0 * wc.x + s1 * wc.y;

#ifdef REBLUR_SH
float4 sh0 = gIn_SpecSh[ checkerboardPos.xy ];
float4 sh1 = gIn_SpecSh[ checkerboardPos.zy ];

// Get rid of potential NANs outside of rendering rectangle or denoising range
sh0 = wc.x != 0.0 ? sh0 : 0;
sh1 = wc.y != 0.0 ? sh1 : 0;

specSh = sh0 * wc.x + sh1 * wc.y;
#endif
}
Expand Down
14 changes: 10 additions & 4 deletions Shaders/Include/REBLUR/REBLUR_DiffuseSpecular_HistoryFix.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ NRD_EXPORT void NRD_CS_MAIN( int2 threadPos : SV_GroupThreadId, int2 pixelPos :

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

// Get rid of potentially bad values outside of the screen
// Get rid of potential NANs outside of rendering rectangle or denoising range
w = IsInScreen( uv ) ? w : 0.0; // no "!isnan" because "s" is not used for "w" calculations
s = w != 0.0 ? s : 0.0;

Expand Down Expand Up @@ -218,7 +218,10 @@ NRD_EXPORT void NRD_CS_MAIN( int2 threadPos : SV_GroupThreadId, int2 pixelPos :
if( abs( i ) <= 1 && abs( j ) <= 1 )
continue;

float d = gIn_DiffFast.SampleLevel( gNearestClamp, pixelUv + int2( i, j ) * gInvRectSize, 0 ).x;
float2 uv = pixelUv + float2( i, j ) * gInvRectSize;
float2 uvScaled = uv * gResolutionScale;

float d = gIn_DiffFast.SampleLevel( gNearestClamp, uvScaled, 0 ).x;
m1 += d;
m2 += d * d;
}
Expand Down Expand Up @@ -346,7 +349,7 @@ NRD_EXPORT void NRD_CS_MAIN( int2 threadPos : SV_GroupThreadId, int2 pixelPos :
// It allows bleeding of background to foreground, but not vice versa ( doesn't suit for 0 roughness )
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
// Get rid of potential NANs outside of rendering rectangle or denoising range
w = ( IsInScreen( uv ) && !isnan( w ) ) ? w : 0.0;
s = w != 0.0 ? s : 0.0;

Expand Down Expand Up @@ -408,7 +411,10 @@ NRD_EXPORT void NRD_CS_MAIN( int2 threadPos : SV_GroupThreadId, int2 pixelPos :
if( abs( i ) <= 1 && abs( j ) <= 1 )
continue;

float s = gIn_SpecFast.SampleLevel( gNearestClamp, pixelUv + int2( i, j ) * gInvRectSize, 0 ).x;
float2 uv = pixelUv + float2( i, j ) * gInvRectSize;
float2 uvScaled = uv * gResolutionScale;

float s = gIn_SpecFast.SampleLevel( gNearestClamp, uvScaled, 0 ).x;
m1 += s;
m2 += s * s;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ void Preload( uint2 sharedPos, int2 globalPos )
#endif
#endif

// Get rid of potential NANs outside of rendering rectangle or denoising range
hitDist = viewZ > gDenoisingRange ? 0 : hitDist;

s_HitDist_ViewZ[ sharedPos.y ][ sharedPos.x ] = float3( hitDist, viewZ );
}

Expand Down
Loading

0 comments on commit c20d25a

Please sign in to comment.