Skip to content

Commit b1f16d7

Browse files
committed
Sync with "simplex"
1 parent 6f1a294 commit b1f16d7

23 files changed

+612
-281
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ echo 1 - Bistro (interior)
110110
echo 2 - Bistro (exterior)
111111
echo 3 - Shader balls
112112
echo 4 - Kitchen
113+
echo 5 - Claire
113114
:CHOOSE_SCENE
114115
set /P M=Choose scene [1-4]:
115116
if %M%==1 (
@@ -128,6 +129,10 @@ echo 4 - Kitchen
128129
set SCENE=Kitchen\Kitchen.gltf
129130
goto RUN
130131
)
132+
if %M%==5 (
133+
set SCENE=Claire\Claire.gltf
134+
goto RUN
135+
)
131136
goto CHOOSE_SCENE
132137

133138
:RUN

Shaders/Composition.cs.hlsl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ NRI_FORMAT("unknown") NRI_RESOURCE( RWTexture2D<float4>, gOut_ComposedSpec_ViewZ
2525
void main( int2 pixelPos : SV_DispatchThreadId )
2626
{
2727
float2 pixelUv = float2( pixelPos + 0.5 ) * gInvRectSize;
28-
float2 sampleUv = pixelUv + gJitter;
2928

3029
// Do not generate NANs for unused threads
3130
if( pixelUv.x > 1.0 || pixelUv.y > 1.0 )
@@ -72,7 +71,7 @@ void main( int2 pixelPos : SV_DispatchThreadId )
7271
float4 baseColorMetalness = gIn_BaseColor_Metalness[ pixelPos ];
7372
BRDF::ConvertBaseColorMetalnessToAlbedoRf0( baseColorMetalness.xyz, baseColorMetalness.w, albedo, Rf0 );
7473

75-
float3 Xv = Geometry::ReconstructViewPosition( sampleUv, gCameraFrustum, viewZ, gOrthoMode );
74+
float3 Xv = Geometry::ReconstructViewPosition( pixelUv, gCameraFrustum, viewZ, gOrthoMode );
7675
float3 V = gOrthoMode == 0 ? normalize( Geometry::RotateVector( gViewToWorld, 0 - Xv ) ) : gViewDirection.xyz;
7776

7877
// Sample NRD outputs
@@ -97,11 +96,8 @@ void main( int2 pixelPos : SV_DispatchThreadId )
9796

9897
if( gResolve && pixelUv.x >= gSeparator )
9998
{
100-
// ( Optional ) replace "roughness" with "roughnessAA"
101-
roughness = NRD_SG_ExtractRoughnessAA( specSg );
102-
10399
// Regain macro-details
104-
diff.xyz = NRD_SG_ResolveDiffuse( diffSg, N ); // or NRD_SH_ResolveDiffuse( diffSg, N )
100+
diff.xyz = NRD_SG_ResolveDiffuse( diffSg, N, V, roughness ); // or NRD_SH_ResolveDiffuse( diffSg, N )
105101
spec.xyz = NRD_SG_ResolveSpecular( specSg, N, V, roughness );
106102

107103
// Regain micro-details & jittering // TODO: preload N and Z into SMEM
@@ -115,7 +111,7 @@ void main( int2 pixelPos : SV_DispatchThreadId )
115111
float Zn = gIn_ViewZ[ pixelPos + int2( 0, 1 ) ];
116112
float Zs = gIn_ViewZ[ pixelPos + int2( 0, -1 ) ];
117113

118-
float2 scale = NRD_SG_ReJitter( diffSg, specSg, Rf0, V, roughness, viewZ, Ze, Zw, Zn, Zs, N, Ne, Nw, Nn, Ns );
114+
float2 scale = NRD_SG_ReJitter( diffSg, specSg, V, roughness, viewZ, Ze, Zw, Zn, Zs, N, Ne, Nw, Nn, Ns );
119115

120116
diff.xyz *= scale.x;
121117
spec.xyz *= scale.y;

Shaders/ConfidenceBlur.cs.hlsl

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// © 2024 NVIDIA Corporation
2+
3+
#include "Include/Shared.hlsli"
4+
5+
struct PushConstants
6+
{
7+
int step;
8+
};
9+
10+
NRI_ROOT_CONSTANTS( PushConstants, g_PushConstants, 1, SET_ROOT );
11+
12+
// Inputs
13+
NRI_RESOURCE( Texture2D<float4>, gIn_Gradient, t, 0, SET_OTHER );
14+
15+
// Outputs
16+
NRI_FORMAT("unknown") NRI_RESOURCE( RWTexture2D<float4>, gOut_Gradient, u, 0, SET_OTHER );
17+
18+
float2 GetGeometryWeightParams( float3 Nv, float3 Xv )
19+
{
20+
const float planeDistSensitivity = 0.02;
21+
22+
float frustumSize = gRectSize.x * gUnproject * lerp( abs( Xv.z ), 1.0, abs( gOrthoMode ) );
23+
float norm = planeDistSensitivity * frustumSize;
24+
float a = 1.0 / norm;
25+
float b = dot( Nv, Xv ) * a;
26+
27+
return float2( a, -b );
28+
}
29+
30+
#define ComputeNonExponentialWeight( x, px, py ) \
31+
Math::SmoothStep( 1.0, 0.0, abs( ( x ) * px + py ) )
32+
33+
[numthreads( 16, 16, 1 )]
34+
void main( uint2 pixelPos : SV_DispatchThreadId )
35+
{
36+
float2 pixelUv = ( pixelPos + 0.5 ) * gInvSharcRenderSize;
37+
38+
float4 data0 = gIn_Gradient[ pixelPos ];
39+
float z0 = data0.w / FP16_VIEWZ_SCALE;
40+
bool isLastPass = g_PushConstants.step == 5;
41+
42+
if( abs( z0 ) > INF )
43+
{
44+
gOut_Gradient[ pixelPos ] = float4( isLastPass, data0.yzw );
45+
return;
46+
}
47+
48+
float3 Xv0 = Geometry::ReconstructViewPosition( pixelUv, gCameraFrustum, z0, gOrthoMode );
49+
float3 Nv0 = Packing::DecodeUnitVector( data0.yz );
50+
float2 geometryWeightParams = GetGeometryWeightParams( Nv0, Xv0 );
51+
float gradient = data0.x;
52+
float sum = 1.0;
53+
54+
[unroll]
55+
for( int i = -2; i <= 2; i++ )
56+
{
57+
[unroll]
58+
for( int j = -2; j <= 2; j++ )
59+
{
60+
if( i == 0 && j == 0 )
61+
continue;
62+
63+
int2 pos = pixelPos + int2( i, j ) * g_PushConstants.step;
64+
float2 uv = ( pos + 0.5 ) * gInvSharcRenderSize;
65+
66+
float4 data = gIn_Gradient.SampleLevel( gNearestClamp, uv, 0 );
67+
68+
// Gaussian weight
69+
float d = length( int2( i, j ) ) / 2.0;
70+
float w = exp( -2.0 * d * d );
71+
72+
// Plane distance weight
73+
float z = data.w / FP16_VIEWZ_SCALE;
74+
float3 Xv = Geometry::ReconstructViewPosition( uv, gCameraFrustum, z, gOrthoMode );
75+
float NoX = dot( Nv0, Xv );
76+
w *= ComputeNonExponentialWeight( NoX, geometryWeightParams.x, geometryWeightParams.y );
77+
78+
// Normal weight
79+
float3 Nv = Packing::DecodeUnitVector( data.yz );
80+
float NoN = saturate( dot( Nv0, Nv ) );
81+
w *= NoN * NoN;
82+
83+
// Accumulate
84+
gradient += data.x * w;
85+
sum += w;
86+
}
87+
}
88+
89+
gradient /= sum;
90+
91+
if( isLastPass )
92+
{
93+
// Last pass converts "gradient" to "history confidence"
94+
gradient = Color::HdrToLinear_Uncharted( gradient * gExposure ).x; // or normalize to the blurred final image or SHARC cache
95+
gradient *= 1.0 - ( Sequence::Bayer4x4( pixelPos, gFrameIndex ) - 0.5 ) * 1.0; // optional dithering
96+
gradient = 1.0 - Color::ToSrgb( saturate( gradient ) ).x;
97+
}
98+
99+
gOut_Gradient[ pixelPos ] = float4( gradient, data0.yzw );
100+
}

Shaders/DlssBefore.cs.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void main( uint2 pixelPos : SV_DispatchThreadId )
5050
float NoV = abs( dot( N, V ) );
5151
float3 Fenv = gIndirectSpecular * BRDF::EnvironmentTerm_Rtg( Rf0, NoV, roughness );
5252

53-
float scaleHitDistance = gDenoiserType == DENOISER_RELAX ? 1.0 : _REBLUR_GetHitDistanceNormalization( viewZ, gHitDistParams, roughness );
53+
float scaleHitDistance = gDenoiserType == DENOISER_RELAX ? 1.0 : _REBLUR_GetHitDistanceNormalization( viewZ, gHitDistSettings, roughness );
5454
float specHitDistance = gIn_Spec[ pixelPos ].w * scaleHitDistance;
5555

5656
bool isSky = abs( viewZ ) == INF;

Shaders/Final.cs.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void main( uint2 pixelPos : SV_DispatchThreadId )
1818
return;
1919

2020
// Noisy input
21-
float3 input = gIn_PreAA.SampleLevel( gNearestSampler, pixelUv * gRectSize * gInvRenderSize, 0 ).xyz;
21+
float3 input = gIn_PreAA.SampleLevel( gNearestClamp, pixelUv * gRectSize * gInvRenderSize, 0 ).xyz;
2222

2323
input = ApplyTonemap( input );
2424
if( gIsSrgb )
@@ -47,7 +47,7 @@ void main( uint2 pixelPos : SV_DispatchThreadId )
4747
// Validation layer
4848
if( gValidation )
4949
{
50-
float4 validation = gIn_Validation.SampleLevel( gNearestSampler, pixelUv, 0 );
50+
float4 validation = gIn_Validation.SampleLevel( gNearestClamp, pixelUv, 0 );
5151
result = lerp( result, validation.xyz, validation.w );
5252
}
5353

Shaders/Include/RaytracingShared.hlsli

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,13 @@ MaterialProps GetMaterialProps( GeometryProps geometryProps )
448448
float3 N = gUseNormalMap ? Geometry::TransformLocalNormal( packedNormal, geometryProps.T, geometryProps.N ) : geometryProps.N;
449449
float3 T = geometryProps.T.xyz;
450450

451+
float3 Nlocal = Geometry::UnpackLocalNormal( packedNormal );
452+
Nlocal.xy *= float( gUseNormalMap );
453+
451454
// Estimate curvature
452455
float viewZ = Geometry::AffineTransform( gWorldToView, geometryProps.X ).z;
453456
float pixelSize = gUnproject * lerp( abs( viewZ ), 1.0, abs( gOrthoMode ) );
454-
float localCurvature = length( Geometry::UnpackLocalNormal( packedNormal ).xy ) * float( gUseNormalMap );
455-
localCurvature /= pixelSize;
457+
float localCurvature = length( Nlocal.xy ) / pixelSize;
456458

457459
// Emission
458460
coords = GetSamplingCoords( baseTexture + 3, geometryProps.uv, geometryProps.mip, MIP_VISIBILITY );
@@ -464,10 +466,12 @@ MaterialProps GetMaterialProps( GeometryProps geometryProps )
464466
if( geometryProps.Has( FLAG_FORCED_EMISSION ) )
465467
{
466468
Lemi = geometryProps.GetForcedEmissionColor( );
469+
Lemi *= gEmissionIntensityCubes;
470+
467471
baseColor = 0.0;
468472
}
469-
470-
Lemi *= gEmissionIntensity;
473+
else
474+
Lemi *= gEmissionIntensityLights;
471475

472476
// Material overrides
473477
[flatten]
@@ -511,6 +515,9 @@ MaterialProps GetMaterialProps( GeometryProps geometryProps )
511515
metalness = lerp( metalness, 0.0, emissionLevel );
512516
roughness = lerp( roughness, 1.0, emissionLevel );
513517

518+
// TODO: roughness AA
519+
520+
// Output
514521
props.Lemi = Lemi;
515522
props.N = N;
516523
props.T = T;
@@ -519,6 +526,10 @@ MaterialProps GetMaterialProps( GeometryProps geometryProps )
519526
props.metalness = metalness;
520527
props.curvature = geometryProps.curvature + localCurvature;
521528

529+
#if USE_WHITE_FURNACE
530+
props.baseColor = 1.0;
531+
#endif
532+
522533
return props;
523534
}
524535

@@ -527,7 +538,7 @@ MaterialProps GetMaterialProps( GeometryProps geometryProps )
527538
#define SHADOW 0x02
528539
#define SSS 0x04
529540

530-
float3 GetLighting( GeometryProps geometryProps, MaterialProps materialProps, uint flags, out float3 Xshadow )
541+
float3 GetLighting( GeometryProps geometryProps, MaterialProps materialProps, compiletime uint flags, out float3 Xshadow )
531542
{
532543
float3 lighting = 0.0;
533544

@@ -730,6 +741,7 @@ float3 GenerateRayAndUpdateThroughput( inout GeometryProps geometryProps, inout
730741
emissiveHitNum++;
731742

732743
// Save either the first ray or the last ray hitting an emissive
744+
// TODO: the selection should be probabilistic and based on the intensity percentage across all hit candidates, currently emission intensity is uniform, so all candidates are equally "good"
733745
if( isEmissiveHit || sampleIndex == 0 )
734746
rayLocal = candidateRayLocal;
735747

@@ -900,7 +912,7 @@ float ReprojectIrradiance( bool isPrevFrame, bool isRefraction, Texture2D<float3
900912
float2 uv = Geometry::GetScreenUv( isPrevFrame ? gWorldToClipPrev : gWorldToClip, geometryProps.X, true ) - gJitter;
901913

902914
float2 rescale = ( isPrevFrame ? gRectSizePrev : gRectSize ) * gInvRenderSize;
903-
float4 data = texSpecViewZ.SampleLevel( gNearestSampler, uv * rescale, 0 );
915+
float4 data = texSpecViewZ.SampleLevel( gNearestClamp, uv * rescale, 0 );
904916
float prevViewZ = abs( data.w ) / FP16_VIEWZ_SCALE;
905917

906918
// Initial state
@@ -950,7 +962,7 @@ float ReprojectIrradiance( bool isPrevFrame, bool isRefraction, Texture2D<float3
950962
weight *= gPrevFrameConfidence; // see C++ code for details
951963

952964
// Read data
953-
Ldiff = texDiff.SampleLevel( gNearestSampler, uv * rescale, 0 );
965+
Ldiff = texDiff.SampleLevel( gNearestClamp, uv * rescale, 0 );
954966
Lspec = data.xyz;
955967

956968
// Avoid NANs

Shaders/Include/Shared.hlsli

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define USE_SHARC_DITHERING 1
2222
#define USE_TRANSLUCENCY 1 // translucent foliage
2323
#define USE_MOVING_EMISSION_FIX 1 // fixes a dark tail, left by an animated emissive object
24+
#define USE_IS_FOR_ALL_BOUNCES 1 // slower, but better lighting for 2nd+ bounces
2425

2526
// Default = 0
2627
#define USE_SANITIZATION 0 // NRD sample is NAN/INF free
@@ -36,6 +37,8 @@
3637
#define USE_SHARC_DEBUG 0 // 1 - show cache, 2 - show grid (NRD sample recompile required)
3738
#define USE_TAA_DEBUG 0 // 1 - show weight
3839
#define USE_BIAS_FIX 0 // fixes negligible hair and specular bias
40+
#define USE_AO_FOR_LAST_BOUNCE 0 // apply a simple AO estimation to SHARC data for the last bounce
41+
#define USE_WHITE_FURNACE 0 // energy conservation test
3942
#define USE_CAMERA_ATTACHED_REFLECTION_TEST 0 // test special treatment for reflections of objects attached to the camera
4043
#define USE_RUSSIAN_ROULETTE 0 // bad practice for real-time denoising
4144

@@ -125,14 +128,15 @@
125128
#define SHARC_MATERIAL_DEMODULATION 1
126129
#define SHARC_USE_FP16 0
127130
#define SHARC_RADIANCE_SCALE 100.0 // matches max emission intensity range ( must be > SUN_INTENSITY )
131+
#define SHARC_RESAMPLING_DEPTH_MIN 1
128132

129133
// Blue noise
130134
#define BLUE_NOISE_SPATIAL_DIM 128 // see StaticTexture::ScramblingRanking
131135
#define BLUE_NOISE_TEMPORAL_DIM 4 // good values: 4-8 for shadows, 8-16 for occlusion, 8-32 for lighting
132136

133137
// Other
134138
#define FP16_MAX 65504.0
135-
#define INF 1e5
139+
#define INF 1e5 // IMPORTANT: INF * FP16_VIEWZ_SCALE < FP16_MAX!
136140
#define LINEAR_BLOCK_SIZE 256
137141
#define FP16_VIEWZ_SCALE 0.125 // TODO: tuned for meters, needs to be scaled down for cm and mm
138142
#define MAX_MIP_LEVEL 11.0
@@ -238,10 +242,11 @@ NRI_RESOURCE( cbuffer, GlobalConstants, b, 0, SET_ROOT )
238242
float4x4 gViewToWorld;
239243
float4x4 gViewToClip;
240244
float4x4 gWorldToView;
241-
float4x4 gWorldToViewPrev;
242245
float4x4 gWorldToClip;
246+
float4x4 gWorldToViewPrev;
243247
float4x4 gWorldToClipPrev;
244-
float4 gHitDistParams;
248+
float4x4 gViewToWorldPrev;
249+
float4 gHitDistSettings;
245250
float4 gCameraFrustum;
246251
float4 gSunBasisX;
247252
float4 gSunBasisY;
@@ -260,7 +265,9 @@ NRI_RESOURCE( cbuffer, GlobalConstants, b, 0, SET_ROOT )
260265
float2 gRectSizePrev;
261266
float2 gInvSharcRenderSize;
262267
float2 gJitter;
263-
float gEmissionIntensity;
268+
float2 gJitterPrev;
269+
float gEmissionIntensityLights;
270+
float gEmissionIntensityCubes;
264271
float gNearZ;
265272
float gSeparator;
266273
float gRoughnessOverride;
@@ -332,9 +339,8 @@ NRI_RESOURCE( cbuffer, MorphMeshUpdatePrimitivesConstants, b, 0, SET_ROOT )
332339
NRI_RESOURCE( SamplerState, gLinearMipmapLinearSampler, s, 0, SET_ROOT );
333340
NRI_RESOURCE( SamplerState, gLinearMipmapNearestSampler, s, 1, SET_ROOT );
334341
NRI_RESOURCE( SamplerState, gNearestMipmapNearestSampler, s, 2, SET_ROOT );
335-
336-
#define gLinearSampler gLinearMipmapLinearSampler
337-
#define gNearestSampler gNearestMipmapNearestSampler
342+
NRI_RESOURCE( SamplerState, gLinearClamp, s, 3, SET_ROOT );
343+
NRI_RESOURCE( SamplerState, gNearestClamp, s, 4, SET_ROOT );
338344

339345
//=============================================================================================
340346
// MISC
@@ -498,7 +504,11 @@ float3 GetSunIntensity( float3 v )
498504

499505
sunColor *= Math::SmoothStep( -0.01, 0.05, gSunDirection.z );
500506

507+
#if USE_WHITE_FURNACE
508+
return 0.0;
509+
#else
501510
return Color::FromGamma( sunColor ) * SUN_INTENSITY;
511+
#endif
502512
}
503513

504514
float3 GetSkyIntensity( float3 v )
@@ -515,7 +525,11 @@ float3 GetSkyIntensity( float3 v )
515525
float ground = 0.5 + 0.5 * Math::SmoothStep( -1.0, 0.0, v.z );
516526
skyColor *= ground;
517527

528+
#if USE_WHITE_FURNACE
529+
return 1.0;
530+
#else
518531
return Color::FromGamma( skyColor ) * SKY_INTENSITY + GetSunIntensity( v );
532+
#endif
519533
}
520534

521535
#endif

Shaders/Shaders.cfg

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
DlssBefore.cs.hlsl -T cs
2-
DlssAfter.cs.hlsl -T cs
3-
Composition.cs.hlsl -T cs
41
MorphMeshUpdatePrimitives.cs.hlsl -T cs
52
MorphMeshUpdateVertices.cs.hlsl -T cs
6-
SharcResolve.cs.hlsl -T cs
73
SharcUpdate.cs.hlsl -T cs
8-
Taa.cs.hlsl -T cs
4+
SharcResolve.cs.hlsl -T cs
5+
ConfidenceBlur.cs.hlsl -T cs
96
TraceOpaque.cs.hlsl -T cs
7+
Composition.cs.hlsl -T cs
108
TraceTransparent.cs.hlsl -T cs
9+
Taa.cs.hlsl -T cs
10+
DlssBefore.cs.hlsl -T cs
11+
DlssAfter.cs.hlsl -T cs
1112
Final.cs.hlsl -T cs

0 commit comments

Comments
 (0)