Skip to content

Commit

Permalink
NRD updated to v4.3.4:
Browse files Browse the repository at this point in the history
- exposed optional USE_LOAD macro switch in "Shared.hlsli"
- fixed "1-Deploy.sh" script
  • Loading branch information
dzhdanNV committed Oct 24, 2023
1 parent dff6212 commit 47823d4
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 56 deletions.
1 change: 0 additions & 1 deletion 1-Deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
git submodule update --init --recursive

chmod +x "External/Packman/packman.sh"
chmod +x "External/NRIFramework/External/NRI/External/Packman/packman.sh"
chmod +x "2-Build.sh"
chmod +x "3-Prepare NRD SDK.sh"
chmod +x "4-Clean.sh"
Expand Down
69 changes: 41 additions & 28 deletions Shaders/Include/RaytracingShared.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ NRI_RESOURCE( Texture2D<float4>, gIn_Textures[], t, 5, 2 );

#define TEX_SAMPLER gLinearMipmapLinearSampler

#if( USE_LOAD == 1 )
#define SAMPLE( coords ) Load( int3( coords ) )
#else
#define SAMPLE( coords ) SampleLevel( TEX_SAMPLER, coords.xy, coords.z )
#endif

#include "HairBRDF.hlsli"

//====================================================================================================================================
Expand Down Expand Up @@ -128,30 +134,35 @@ float2 GetConeAngleFromRoughness( float mip, float roughness )
return GetConeAngleFromAngularRadius( mip, coneAngle );
}

/*
Returns:
.x - for visibility (emission, shadow)
We must avoid using lower mips because it can lead to significant increase in AHS invocations. Mips lower than 128x128 are skipped!
.y - for sampling (normals...)
Negative MIP bias is applied
.z - for sharp sampling
Negative MIP bias is applied (can be more negative...)
*/
float3 GetRealMip( uint textureIndex, float mip )
float3 GetSamplingCoords( uint textureIndex, float2 uv, float mip, int mode )
{
float w, h;
gIn_Textures[ textureIndex ].GetDimensions( w, h ); // TODO: if I only had it as a constant...
float2 texSize;
gIn_Textures[ NonUniformResourceIndex( textureIndex ) ].GetDimensions( texSize.x, texSize.y ); // TODO: if I only had it as a constant...

// Recalculate for the current texture
float mipNum = log2( max( texSize.x, texSize.y ) );
mip += mipNum - MAX_MIP_LEVEL;
if( mode == MIP_VISIBILITY )
{
// We must avoid using lower mips because it can lead to significant increase in AHS invocations. Mips lower than 128x128 are skipped!
mip = min( mip, mipNum - 7.0 );
}
else
mip += gCameraOrigin_gMipBias.w * ( mode == MIP_LESS_SHARP ? 0.5 : 1.0 );
mip = clamp( mip, 0.0, mipNum - 1.0 );

// Taking into account real dimensions of the current texture
float mipNum = log2( w );
float realMip = mip + mipNum - MAX_MIP_LEVEL;
#if( USE_LOAD == 1 )
mip = round( mip );
#endif

float3 mips;
mips.x = min( realMip, mipNum - 7.0 );
mips.y = realMip + gCameraOrigin_gMipBias.w * 0.5;
mips.z = realMip + gCameraOrigin_gMipBias.w;
texSize *= exp2( -mip );

return max( mips, 0.0 );
// Uv coordinates
#if( USE_LOAD == 1 )
uv = frac( uv ) * texSize;
#endif

return float3( uv, mip );
}

MaterialProps GetMaterialProps( GeometryProps geometryProps )
Expand All @@ -169,31 +180,33 @@ MaterialProps GetMaterialProps( GeometryProps geometryProps )
}

uint baseTexture = geometryProps.GetBaseTexture( );
float3 mips = GetRealMip( baseTexture, geometryProps.mip );

InstanceData instanceData = gIn_InstanceData[ geometryProps.instanceIndex ];

// Base color
float4 color = gIn_Textures[ NonUniformResourceIndex( baseTexture ) ].SampleLevel( TEX_SAMPLER, geometryProps.uv, mips.z );
float3 coords = GetSamplingCoords( baseTexture, geometryProps.uv, geometryProps.mip, MIP_SHARP );
float4 color = gIn_Textures[ NonUniformResourceIndex( baseTexture ) ].SAMPLE( coords );
color.xyz *= instanceData.baseColorAndMetalnessScale.xyz;
color.xyz *= geometryProps.IsTransparent( ) ? 1.0 : STL::Math::PositiveRcp( color.w ); // Correct handling of BC1 with pre-multiplied alpha
float3 baseColor = saturate( color.xyz );

// Roughness and metalness
float3 materialProps = gIn_Textures[ NonUniformResourceIndex( baseTexture + 1 ) ].SampleLevel( TEX_SAMPLER, geometryProps.uv, mips.z ).xyz;
coords = GetSamplingCoords( baseTexture + 1, geometryProps.uv, geometryProps.mip, MIP_SHARP );
float3 materialProps = gIn_Textures[ NonUniformResourceIndex( baseTexture + 1 ) ].SAMPLE( coords ).xyz;
float roughness = saturate( materialProps.y * instanceData.emissionAndRoughnessScale.w );
float metalness = saturate( materialProps.z * instanceData.baseColorAndMetalnessScale.w );

// Normal
float2 packedNormal = gIn_Textures[ NonUniformResourceIndex( baseTexture + 2 ) ].SampleLevel( TEX_SAMPLER, geometryProps.uv, mips.y ).xy;
coords = GetSamplingCoords( baseTexture + 2, geometryProps.uv, geometryProps.mip, MIP_LESS_SHARP );
float2 packedNormal = gIn_Textures[ NonUniformResourceIndex( baseTexture + 2 ) ].SAMPLE( coords ).xy;
float3 N = gUseNormalMap ? STL::Geometry::TransformLocalNormal( packedNormal, geometryProps.T, geometryProps.N ) : geometryProps.N;
float3 T = geometryProps.T.xyz;

// Estimate curvature
float curvature = length( STL::Geometry::UnpackLocalNormal( packedNormal ).xy ) * float( gUseNormalMap );

// Emission
float3 Lemi = gIn_Textures[ NonUniformResourceIndex( baseTexture + 3 ) ].SampleLevel( TEX_SAMPLER, geometryProps.uv, mips.x ).xyz;
coords = GetSamplingCoords( baseTexture + 3, geometryProps.uv, geometryProps.mip, MIP_VISIBILITY );
float3 Lemi = gIn_Textures[ NonUniformResourceIndex( baseTexture + 3 ) ].SAMPLE( coords ).xyz;
Lemi *= instanceData.emissionAndRoughnessScale.xyz;
Lemi *= ( baseColor + 0.01 ) / ( max( baseColor, max( baseColor, baseColor ) ) + 0.01 );

Expand Down Expand Up @@ -514,8 +527,8 @@ float ReprojectIrradiance(
\
/* Alpha test */ \
uint baseTexture = ( instanceData.textureOffsetAndFlags & NON_FLAG_MASK ) + 0; \
float3 mips = GetRealMip( baseTexture, mip ); \
float alpha = gIn_Textures[ baseTexture ].SampleLevel( TEX_SAMPLER, uv, mips.x ).w; \
float3 coords = GetSamplingCoords( baseTexture, uv, mip, MIP_VISIBILITY ); \
float alpha = gIn_Textures[ baseTexture ].SAMPLE( coords ).w; \
\
if( alpha > 0.5 ) \
rayQuery.CommitNonOpaqueTriangleHit( ); \
Expand Down
26 changes: 17 additions & 9 deletions Shaders/Include/Shared.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
//=============================================================================================

// Fused or separate denoising selection
// 0 - DIFFUSE and SPECULAR
// 1 - DIFFUSE_SPECULAR
// 0 - DIFFUSE and SPECULAR
// 1 - DIFFUSE_SPECULAR
#define NRD_COMBINED 1

// NORMAL - common (non specialized) denoisers
// OCCLUSION - OCCLUSION (ambient or specular occlusion only) denoisers
// SH - SH (spherical harmonics or spherical gaussian) denoisers
// DIRECTIONAL_OCCLUSION - DIRECTIONAL_OCCLUSION (ambient occlusion in SH mode) denoisers
// NORMAL - common (non specialized) denoisers
// OCCLUSION - OCCLUSION (ambient or specular occlusion only) denoisers
// SH - SH (spherical harmonics or spherical gaussian) denoisers
// DIRECTIONAL_OCCLUSION - DIRECTIONAL_OCCLUSION (ambient occlusion in SH mode) denoisers
#define NRD_MODE NORMAL // NORMAL, OCCLUSION, SH, DIRECTIONAL_OCCLUSION

// Default = 1
Expand All @@ -38,6 +38,7 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
#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 )
#define USE_LOAD 0 // Load vs SampleLevel

#define THROUGHPUT_THRESHOLD 0.001
#define PSR_THROUGHPUT_THRESHOLD 0.0 // TODO: even small throughput can produce a bright spot if incoming radiance is huge
Expand Down Expand Up @@ -100,6 +101,11 @@ license agreement from NVIDIA CORPORATION is strictly prohibited.
#define MATERIAL_ID_PSR 2
#define MATERIAL_ID_HAIR 3

// Mip mode
#define MIP_VISIBILITY 0 // for visibility: emission, shadow and alpha mask
#define MIP_LESS_SHARP 1 // for normal
#define MIP_SHARP 2 // for albedo and roughness

// Other
#define FP16_MAX 65504.0
#define INF 1e5
Expand Down Expand Up @@ -320,8 +326,10 @@ NRI_RESOURCE( cbuffer, MorphMeshUpdatePrimitivesConstants, b, 0, 3 )

NRI_RESOURCE( SamplerState, gLinearMipmapLinearSampler, s, 0, 0 );
NRI_RESOURCE( SamplerState, gLinearMipmapNearestSampler, s, 1, 0 );
NRI_RESOURCE( SamplerState, gLinearSampler, s, 2, 0 );
NRI_RESOURCE( SamplerState, gNearestSampler, s, 3, 0 );
NRI_RESOURCE( SamplerState, gNearestMipmapNearestSampler, s, 2, 0 );

#define gLinearSampler gLinearMipmapLinearSampler
#define gNearestSampler gNearestMipmapNearestSampler

//=============================================================================================
// MISC
Expand Down Expand Up @@ -382,7 +390,7 @@ float3 ApplyExposure( float3 Lsum, bool applyToneMap )
return Lsum;
}

float3 BicubicFilterNoCorners( Texture2D<float3> tex, SamplerState samp, float2 samplePos, float2 invTextureSize, compiletime const float sharpness )
float3 BicubicFilterNoCorners( Texture2D<float3> tex, SamplerState samp, float2 samplePos, float2 invTextureSize, float sharpness )
{
float2 centerPos = floor( samplePos - 0.5 ) + 0.5;
float2 f = samplePos - centerPos;
Expand Down
24 changes: 7 additions & 17 deletions Source/NRDSample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ enum class Descriptor : uint32_t

LinearMipmapLinear_Sampler,
LinearMipmapNearest_Sampler,
Linear_Sampler,
Nearest_Sampler,
NearestMipmapNearest_Sampler,

InstanceData_Buffer,
MorphMeshIndices_Buffer,
Expand Down Expand Up @@ -2296,7 +2295,7 @@ void Sample::CreatePipelineLayoutAndDescriptorPool()
const nri::DescriptorRangeDesc descriptorRanges0[] =
{
{ 0, 1, nri::DescriptorType::CONSTANT_BUFFER, nri::ShaderStage::COMPUTE },
{ 0, 4, nri::DescriptorType::SAMPLER, nri::ShaderStage::COMPUTE },
{ 0, 3, nri::DescriptorType::SAMPLER, nri::ShaderStage::COMPUTE },
};

const nri::DescriptorRangeDesc descriptorRanges1[] =
Expand Down Expand Up @@ -2901,21 +2900,13 @@ void Sample::CreateSamplers()
m_Descriptors.push_back(descriptor);
}

{ // Descriptor::Linear_Sampler
{ // Descriptor::NearestMipmapNearest_Sampler
nri::SamplerDesc samplerDesc = {};
samplerDesc.addressModes = {nri::AddressMode::CLAMP_TO_EDGE, nri::AddressMode::CLAMP_TO_EDGE};
samplerDesc.minification = nri::Filter::LINEAR;
samplerDesc.magnification = nri::Filter::LINEAR;

NRI_ABORT_ON_FAILURE( NRI.CreateSampler(*m_Device, samplerDesc, descriptor) );
m_Descriptors.push_back(descriptor);
}

{ // Descriptor::Nearest_Sampler
nri::SamplerDesc samplerDesc = {};
samplerDesc.addressModes = {nri::AddressMode::CLAMP_TO_EDGE, nri::AddressMode::CLAMP_TO_EDGE};
samplerDesc.addressModes = {nri::AddressMode::REPEAT, nri::AddressMode::REPEAT};
samplerDesc.minification = nri::Filter::NEAREST;
samplerDesc.magnification = nri::Filter::NEAREST;
samplerDesc.mip = nri::Filter::NEAREST;
samplerDesc.mipMax = 16.0f;

NRI_ABORT_ON_FAILURE( NRI.CreateSampler(*m_Device, samplerDesc, descriptor) );
m_Descriptors.push_back(descriptor);
Expand Down Expand Up @@ -3155,8 +3146,7 @@ void Sample::CreateDescriptorSets()
{
Get(Descriptor::LinearMipmapLinear_Sampler),
Get(Descriptor::LinearMipmapNearest_Sampler),
Get(Descriptor::Linear_Sampler),
Get(Descriptor::Nearest_Sampler),
Get(Descriptor::NearestMipmapNearest_Sampler),
};

for (Frame& frame : m_Frames)
Expand Down

0 comments on commit 47823d4

Please sign in to comment.