Skip to content

Commit

Permalink
NRD updated to v4.4.3:
Browse files Browse the repository at this point in the history
- added HDR support
- updated deps
  • Loading branch information
dzhdanNV committed Jan 2, 2024
1 parent 4874083 commit 7fa4d2a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion External/NRD
4 changes: 2 additions & 2 deletions Shaders/DlssAfter.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ void main( uint2 pixelPos : SV_DispatchThreadId )
color = ApplyTonemap( color );

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

// Output
gOut_Image[ pixelPos ] = color;
Expand Down
12 changes: 7 additions & 5 deletions Shaders/Include/Shared.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ NRI_RESOURCE( cbuffer, GlobalConstants, b, 0, 0 )
float gFocalDistance;
float gFocalLength;
float gTAA;
float gHdrScale;
uint32_t gDenoiserType;
uint32_t gDisableShadowsAndEnableImportanceSampling; // TODO: remove - modify GetSunIntensity to return 0 if sun is below horizon
uint32_t gOnScreen;
Expand All @@ -261,6 +262,7 @@ NRI_RESOURCE( cbuffer, GlobalConstants, b, 0, 0 )
uint32_t gTrimLobe;
uint32_t gSR;
uint32_t gRR;
uint32_t gIsSrgb;

// Ambient
float gAmbientMaxAccumulatedFramesNum;
Expand Down Expand Up @@ -377,7 +379,7 @@ float3 ApplyTonemap( float3 Lsum )
{
#if( NRD_MODE != OCCLUSION && NRD_MODE != DIRECTIONAL_OCCLUSION )
if( gOnScreen == SHOW_FINAL )
Lsum = STL::Color::HdrToLinear_Uncharted( Lsum );
Lsum = gHdrScale * STL::Color::HdrToLinear_Uncharted( Lsum );
#else
Lsum = Lsum.xxx;
#endif
Expand Down Expand Up @@ -461,7 +463,7 @@ float GetCircleOfConfusion( float distance ) // diameter
//=============================================================================================

#define SKY_INTENSITY 1.0
#define SUN_INTENSITY 8.0
#define SUN_INTENSITY 10.0

float3 GetSunIntensity( float3 v, float3 sunDirection, float tanAngularRadius )
{
Expand All @@ -480,11 +482,11 @@ float3 GetSunIntensity( float3 v, float3 sunDirection, float tanAngularRadius )
sun += glow;

float3 sunColor = lerp( float3( 1.0, 0.6, 0.3 ), float3( 1.0, 0.9, 0.7 ), STL::Math::Sqrt01( sunDirection.z ) );
sunColor *= saturate( sun );
sunColor *= sun;

sunColor *= STL::Math::SmoothStep( -0.01, 0.05, sunDirection.z );

return STL::Color::GammaToLinear( sunColor ) * SUN_INTENSITY;
return STL::Color::FromGamma( sunColor ) * SUN_INTENSITY;
}

float3 GetSkyIntensity( float3 v, float3 sunDirection, float tanAngularRadius )
Expand All @@ -501,7 +503,7 @@ float3 GetSkyIntensity( float3 v, float3 sunDirection, float tanAngularRadius )
float ground = 0.5 + 0.5 * STL::Math::SmoothStep( -1.0, 0.0, v.z );
skyColor *= ground;

return STL::Color::GammaToLinear( saturate( skyColor ) ) * SKY_INTENSITY + GetSunIntensity( v, sunDirection, tanAngularRadius );
return STL::Color::FromGamma( skyColor ) * SKY_INTENSITY + GetSunIntensity( v, sunDirection, tanAngularRadius );
}

#endif
6 changes: 3 additions & 3 deletions Shaders/Temporal.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ void main( int2 threadPos : SV_GroupThreadId, int2 pixelPos : SV_DispatchThreadI
float2 pixelPosPrev = saturate( pixelUvPrev ) * gRectSizePrev;
float3 history = BicubicFilterNoCorners( gIn_History, gLinearSampler, pixelPosPrev, gInvRenderSize, TAA_HISTORY_SHARPNESS ).xyz;

bool isSrgb = gOnScreen == SHOW_FINAL || gOnScreen == SHOW_BASE_COLOR;
bool isSrgb = gIsSrgb && ( gOnScreen == SHOW_FINAL || gOnScreen == SHOW_BASE_COLOR );
if( isSrgb )
history = STL::Color::SrgbToLinear( history );
history = STL::Color::FromSrgb( history );

// History clamping
// IMPORTANT: must be done in linear space
Expand Down Expand Up @@ -150,7 +150,7 @@ void main( int2 threadPos : SV_GroupThreadId, int2 pixelPos : SV_DispatchThreadI

// Output
if( isSrgb )
result = STL::Color::LinearToSrgb( result );
result = STL::Color::ToSrgb( result );

gOut_Result[ pixelPos ] = result;
}
4 changes: 2 additions & 2 deletions Shaders/TraceOpaque.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ TraceOpaqueResult TraceOpaque( TraceOpaqueDesc desc )

float3 psrNormal = STL::Geometry::RotateVectorInverse( mirrorMatrix, materialProps.N );

gOut_BaseColor_Metalness[ desc.pixelPos ] = float4( STL::Color::LinearToSrgb( materialProps.baseColor ), materialProps.metalness );
gOut_BaseColor_Metalness[ desc.pixelPos ] = float4( STL::Color::ToSrgb( materialProps.baseColor ), materialProps.metalness );
gOut_Normal_Roughness[ desc.pixelPos ] = NRD_FrontEnd_PackNormalAndRoughness( psrNormal, materialProps.roughness, materialID );
}
else
Expand Down Expand Up @@ -811,7 +811,7 @@ void main( uint2 pixelPos : SV_DispatchThreadId )
#endif

gOut_Normal_Roughness[ pixelPos ] = NRD_FrontEnd_PackNormalAndRoughness( materialProps0.N, materialProps0.roughness, materialID );
gOut_BaseColor_Metalness[ pixelPos ] = float4( STL::Color::LinearToSrgb( materialProps0.baseColor ), materialProps0.metalness );
gOut_BaseColor_Metalness[ pixelPos ] = float4( STL::Color::ToSrgb( materialProps0.baseColor ), materialProps0.metalness );

// Debug
if( gOnScreen == SHOW_INSTANCE_INDEX )
Expand Down
27 changes: 19 additions & 8 deletions Source/NRDSample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ class Sample : public SampleBase
float m_MinResolutionScale = 0.5f;
float m_DofAperture = 0.0f;
float m_DofFocalDistance = 1.0f;
float m_SdrScale = 1.0f;
bool m_HasTransparent = false;
bool m_ShowUi = true;
bool m_ForceHistoryReset = false;
Expand All @@ -663,6 +664,7 @@ class Sample : public SampleBase
bool m_ShowValidationOverlay = false;
bool m_PositiveZ = true;
bool m_ReversedZ = false;
bool m_IsSrgb = false;
};

Sample::~Sample()
Expand Down Expand Up @@ -1712,7 +1714,7 @@ void Sample::PrepareFrame(uint32_t frameIndex)
sampleShaders +=
" --useAPI --binary --flatten --stripReflection --WX --colorize"
" -c Shaders.cfg -o _Shaders --sourceDir Shaders"
" -I Shaders -I External -I External/NGX -I External/NRD/External"
" -I Shaders -I External -I External/NGX -I External/NRD/External -I External/NRIFramework/Shaders"
" -D COMPILER_DXC -D NRD_NORMAL_ENCODING=" STRINGIFY(NRD_NORMAL_ENCODING) " -D NRD_ROUGHNESS_ENCODING=" STRINGIFY(NRD_ROUGHNESS_ENCODING);

nrdShaders +=
Expand Down Expand Up @@ -2272,16 +2274,16 @@ void Sample::GenerateAnimatedCubes()
nri::Format Sample::CreateSwapChain()
{
nri::SwapChainDesc swapChainDesc = {};
swapChainDesc.windowSystemType = GetWindowSystemType();
swapChainDesc.window = GetWindow();
swapChainDesc.commandQueue = m_CommandQueue;
swapChainDesc.format = nri::SwapChainFormat::BT709_G22_8BIT;
swapChainDesc.format = nri::SwapChainFormat::BT709_G10_16BIT; // or BT709_G22_10BIT
swapChainDesc.verticalSyncInterval = m_VsyncInterval;
swapChainDesc.width = (uint16_t)GetWindowResolution().x;
swapChainDesc.height = (uint16_t)GetWindowResolution().y;
swapChainDesc.textureNum = SWAP_CHAIN_TEXTURE_NUM;

NRI_ABORT_ON_FAILURE(NRI.CreateSwapChain(*m_Device, swapChainDesc, m_SwapChain));
m_IsSrgb = swapChainDesc.format != nri::SwapChainFormat::BT709_G10_16BIT;

uint32_t swapChainTextureNum = 0;
nri::Texture* const* swapChainTextures = NRI.GetSwapChainTextures(*m_SwapChain, swapChainTextureNum);
Expand Down Expand Up @@ -2970,6 +2972,8 @@ void Sample::CreateResources(nri::Format swapChainFormat)
nri::Format normalFormat = nri::Format::RGBA16_SNORM;
#endif

nri::Format taaFormat = m_IsSrgb ? nri::Format::R10_G10_B10_A2_UNORM : nri::Format::RGBA16_SFLOAT;

const uint16_t w = (uint16_t)m_RenderResolution.x;
const uint16_t h = (uint16_t)m_RenderResolution.y;
const uint64_t instanceDataSize = m_Scene.instances.size() * sizeof(InstanceData);
Expand Down Expand Up @@ -3045,9 +3049,9 @@ void Sample::CreateResources(nri::Format swapChainFormat)
nri::TextureUsageBits::SHADER_RESOURCE | nri::TextureUsageBits::SHADER_RESOURCE_STORAGE, nri::AccessBits::SHADER_RESOURCE_STORAGE);
CreateTexture(descriptorDescs, "Texture::ComposedSpec_ViewZ", nri::Format::RGBA16_SFLOAT, w, h, 1, 1,
nri::TextureUsageBits::SHADER_RESOURCE | nri::TextureUsageBits::SHADER_RESOURCE_STORAGE, nri::AccessBits::SHADER_RESOURCE_STORAGE);
CreateTexture(descriptorDescs, "Texture::TaaHistory", nri::Format::R10_G10_B10_A2_UNORM, w, h, 1, 1,
CreateTexture(descriptorDescs, "Texture::TaaHistory", taaFormat, w, h, 1, 1,
nri::TextureUsageBits::SHADER_RESOURCE | nri::TextureUsageBits::SHADER_RESOURCE_STORAGE, nri::AccessBits::SHADER_RESOURCE);
CreateTexture(descriptorDescs, "Texture::TaaHistoryPrev", nri::Format::R10_G10_B10_A2_UNORM, w, h, 1, 1,
CreateTexture(descriptorDescs, "Texture::TaaHistoryPrev", taaFormat, w, h, 1, 1,
nri::TextureUsageBits::SHADER_RESOURCE | nri::TextureUsageBits::SHADER_RESOURCE_STORAGE, nri::AccessBits::SHADER_RESOURCE_STORAGE);

#if( NRD_MODE == SH )
Expand Down Expand Up @@ -4103,6 +4107,11 @@ void Sample::UpdateConstantBuffer(uint32_t frameIndex, uint32_t maxAccumulatedFr
DecomposeProjection(STYLE_D3D, STYLE_D3D, m_Camera.state.mViewToClip, &flags, nullptr, nullptr, frustum.pv, project, nullptr);
float orthoMode = ( flags & PROJ_ORTHO ) == 0 ? 0.0f : -1.0f;

nri::DisplayDesc displayDesc = {};
NRI.GetDisplayDesc(*m_SwapChain, displayDesc);

m_SdrScale = displayDesc.sdrLuminance / 80.0f;

const uint32_t bufferedFrameIndex = frameIndex % BUFFERED_FRAME_MAX_NUM;
const uint64_t rangeOffset = m_Frames[bufferedFrameIndex].globalConstantBufferOffset;
nri::Buffer* globalConstants = Get(Buffer::GlobalConstants);
Expand Down Expand Up @@ -4150,6 +4159,7 @@ void Sample::UpdateConstantBuffer(uint32_t frameIndex, uint32_t maxAccumulatedFr
data->gFocalDistance = m_DofFocalDistance;
data->gFocalLength = ( 0.5f * ( 35.0f * 0.001f ) ) / Tan( DegToRad( m_Settings.camFov * 0.5f ) ); // for 35 mm sensor size (aka old-school 35 mm film)
data->gTAA = (m_Settings.denoiser != DENOISER_REFERENCE && m_Settings.TAA) ? taaAccumulationFactor : 0;
data->gHdrScale = displayDesc.isHDR ? 0.9f * displayDesc.maxLuminance / 80.0f : 1.0f;
data->gDenoiserType = (uint32_t)m_Settings.denoiser;
data->gDisableShadowsAndEnableImportanceSampling = (sunDirection.z < 0.0f && m_Settings.importanceSampling) ? 1 : 0;
data->gOnScreen = m_Settings.onScreen + ((NRD_MODE == OCCLUSION || NRD_MODE == DIRECTIONAL_OCCLUSION) ? 3 : 0); // preserve original mapping
Expand All @@ -4164,8 +4174,9 @@ void Sample::UpdateConstantBuffer(uint32_t frameIndex, uint32_t maxAccumulatedFr
data->gPSR = m_Settings.PSR && m_Settings.tracingMode != RESOLUTION_HALF;
data->gValidation = m_ShowValidationOverlay && m_Settings.denoiser != DENOISER_REFERENCE && m_Settings.separator != 1.0f;
data->gTrimLobe = m_Settings.specularLobeTrimming ? 1 : 0;
data->gSR = m_Settings.SR;
data->gRR = m_Settings.RR;
data->gSR = m_Settings.SR ? 1 : 0;
data->gRR = m_Settings.RR ? 1 : 0;
data->gIsSrgb = m_IsSrgb ? 1 : 0;

// Ambient
data->gAmbientMaxAccumulatedFramesNum = m_ForceHistoryReset ? 0 : float(maxAccumulatedFrameNum);
Expand Down Expand Up @@ -4964,7 +4975,7 @@ void Sample::RenderFrame(uint32_t frameIndex)
desc.colorNum = 1;

NRI.CmdBeginRendering(commandBuffer, desc);
RenderUserInterface(*m_Device, commandBuffer);
RenderUserInterface(*m_Device, commandBuffer, m_SdrScale, m_IsSrgb);
NRI.CmdEndRendering(commandBuffer);

const nri::TextureTransitionBarrierDesc afterTransitions = nri::TextureTransitionFromState(beforeTransitions, {nri::AccessBits::UNKNOWN, nri::TextureLayout::PRESENT});
Expand Down

0 comments on commit 7fa4d2a

Please sign in to comment.