Skip to content

Commit 2a2fc2d

Browse files
committed
Several improvements:
- SHARC: switched to "SHARC_SEPARATE_EMISSIVE" to minimize lags - PT: removed recent "materialID" hack - PT: fixed "materialID" type (no functional changes) - updated deps
1 parent f27cbbf commit 2a2fc2d

7 files changed

+36
-30
lines changed

Shaders/Composition.cs.hlsl

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ void main( int2 pixelPos : SV_DispatchThreadId )
269269
SharcHitData sharcHitData;
270270
sharcHitData.positionWorld = Xglobal;
271271
sharcHitData.normalWorld = N;
272+
sharcHitData.emissive = Lemi;
272273

273274
HashMapData hashMapData;
274275
hashMapData.capacity = SHARC_CAPACITY;

Shaders/Include/RaytracingShared.hlsli

+7-3
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,10 @@ bool IsPsrAllowed( MaterialProps materialProps )
545545
return gPSR && materialProps.roughness < 0.044 && ( materialProps.metalness > 0.941 || Color::Luminance( materialProps.baseColor ) < 0.005 ); // TODO: tweaked for some content?
546546
}
547547

548-
float3 GetShadowedLighting( GeometryProps geometryProps, MaterialProps materialProps, bool softShadows = true )
548+
#define SKIP_SOFT_SHADOWS 0x1
549+
#define SKIP_EMISSIVE 0x2
550+
551+
float3 GetShadowedLighting( GeometryProps geometryProps, MaterialProps materialProps, uint flags = 0 )
549552
{
550553
const uint instanceInclusionMask = FLAG_NON_TRANSPARENT; // Default shadow rays must ignore transparency // TODO: what about translucency?
551554
const uint rayFlags = 0;
@@ -557,7 +560,7 @@ float3 GetShadowedLighting( GeometryProps geometryProps, MaterialProps materialP
557560
float2 rnd = Rng::Hash::GetFloat2( );
558561
rnd = ImportanceSampling::Cosine::GetRay( rnd ).xy;
559562
rnd *= gTanSunAngularRadius;
560-
rnd *= float( softShadows );
563+
rnd *= float( ( flags & SKIP_SOFT_SHADOWS ) == 0 );
561564

562565
float3 sunDirection = normalize( gSunBasisX.xyz * rnd.x + gSunBasisY.xyz * rnd.y + gSunDirection.xyz );
563566

@@ -566,7 +569,8 @@ float3 GetShadowedLighting( GeometryProps geometryProps, MaterialProps materialP
566569
L *= CastVisibilityRay_AnyHit( Xoffset, sunDirection, 0.0, INF, mipAndCone, gWorldTlas, instanceInclusionMask, rayFlags );
567570
}
568571

569-
L += materialProps.Lemi;
572+
if( ( flags & SKIP_EMISSIVE ) == 0 )
573+
L += materialProps.Lemi;
570574

571575
return NRD_MODE < OCCLUSION ? L : 0.0;
572576
}

Shaders/Include/Shared.hlsli

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,15 @@
114114
#define PT_EVIL_TWIN_LOBE_TOLERANCE 0.005 // normalized %
115115
#define PT_GLASS_MIN_F 0.05 // adds a bit of stability and bias
116116

117-
// Spatial HAsh-ased Radiance Cache
117+
// Spatial HAsh-ased Radiance Cache ( SHARC )
118118
#define SHARC_CAPACITY ( 1 << 22 )
119119
#define SHARC_SCENE_SCALE 50.0
120120
#define SHARC_DOWNSCALE 5
121121
#define SHARC_NORMAL_DITHER 0.003
122122
#define SHARC_POS_DITHER 0.001
123123
#define SHARC_ANTI_FIREFLY true
124124
#define SHARC_STALE_FRAME_NUM_MIN 32 // new version uses 8 by default, old value offers more stability in voxels with low number of samples ( critical for glass )
125+
#define SHARC_SEPARATE_EMISSIVE 1
125126

126127
// Blue noise
127128
#define BLUE_NOISE_SPATIAL_DIM 128 // see StaticTexture::ScramblingRanking

Shaders/SharcUpdate.cs.hlsl

+17-13
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,16 @@ void Trace( GeometryProps geometryProps )
5151

5252
MaterialProps materialProps = GetMaterialProps( geometryProps, USE_SHARC_V_DEPENDENT == 0 );
5353

54-
// Update SHARC cache
54+
// Update SHARC cache ( this ia always a hit )
5555
{
56-
float3 L = GetShadowedLighting( geometryProps, materialProps );
57-
5856
SharcHitData sharcHitData;
5957
sharcHitData.positionWorld = GetGlobalPos( geometryProps.X ) + ( Rng::Hash::GetFloat4( ).xyz - 0.5 ) * SHARC_POS_DITHER;
6058
sharcHitData.normalWorld = normalize( geometryProps.N + ( Rng::Hash::GetFloat4( ).xyz - 0.5 ) * SHARC_NORMAL_DITHER );
59+
sharcHitData.emissive = materialProps.Lemi;
6160

6261
SharcSetThroughput( sharcState, 1.0 );
62+
63+
float3 L = GetShadowedLighting( geometryProps, materialProps, SKIP_EMISSIVE );
6364
if( !SharcUpdateHit( sharcParams, sharcState, sharcHitData, L, 1.0 ) )
6465
return;
6566
}
@@ -210,19 +211,22 @@ void Trace( GeometryProps geometryProps )
210211
materialProps = GetMaterialProps( geometryProps, USE_SHARC_V_DEPENDENT == 0 );
211212
}
212213

213-
// Compute lighting at hit point
214-
float3 L = GetShadowedLighting( geometryProps, materialProps );
215-
216214
{ // Update SHARC cache
217-
SharcHitData sharcHitData;
218-
sharcHitData.positionWorld = GetGlobalPos( geometryProps.X ) + ( Rng::Hash::GetFloat4( ).xyz - 0.5 ) * SHARC_POS_DITHER;
219-
sharcHitData.normalWorld = normalize( geometryProps.N + ( Rng::Hash::GetFloat4( ).xyz - 0.5 ) * SHARC_NORMAL_DITHER );
220-
221215
SharcSetThroughput( sharcState, throughput );
216+
222217
if( geometryProps.IsSky( ) )
223-
SharcUpdateMiss( sharcParams, sharcState, L );
224-
else if( !SharcUpdateHit( sharcParams, sharcState, sharcHitData, L, Rng::Hash::GetFloat( ) ) )
225-
break;
218+
SharcUpdateMiss( sharcParams, sharcState, materialProps.Lemi );
219+
else
220+
{
221+
SharcHitData sharcHitData;
222+
sharcHitData.positionWorld = GetGlobalPos( geometryProps.X ) + ( Rng::Hash::GetFloat4( ).xyz - 0.5 ) * SHARC_POS_DITHER;
223+
sharcHitData.normalWorld = normalize( geometryProps.N + ( Rng::Hash::GetFloat4( ).xyz - 0.5 ) * SHARC_NORMAL_DITHER );
224+
sharcHitData.emissive = materialProps.Lemi;
225+
226+
float3 L = GetShadowedLighting( geometryProps, materialProps, SKIP_EMISSIVE );
227+
if( !SharcUpdateHit( sharcParams, sharcState, sharcHitData, L, Rng::Hash::GetFloat( ) ) )
228+
break;
229+
}
226230
}
227231
}
228232
}

Shaders/TraceOpaque.cs.hlsl

+6-10
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,11 @@ float4 GetRadianceFromPreviousFrame( GeometryProps geometryProps, MaterialProps
7878
return NRD_MODE < OCCLUSION ? float4( prevLsum * saturate( prevFrameWeight / 0.001 ), prevFrameWeight ) : 0.0;
7979
}
8080

81-
uint GetMaterialID( GeometryProps geometryProps, MaterialProps materialProps )
81+
float GetMaterialID( GeometryProps geometryProps, MaterialProps materialProps )
8282
{
8383
bool isHair = geometryProps.Has( FLAG_HAIR );
8484
bool isMetal = materialProps.metalness > 0.5;
8585

86-
// TODO: it's a hack improving behavior on border between non-metallic and metallic surfaces
87-
// It can add bias, because diffuse processing can touch metal pixels with 0 diffuse.
88-
// It's good for contact shadowing, but undesired in some other cases.
89-
isMetal = isMetal && materialProps.roughness < 0.3;
90-
9186
return isHair ? MATERIAL_ID_HAIR : ( isMetal ? MATERIAL_ID_METAL : MATERIAL_ID_DEFAULT );
9287
}
9388

@@ -227,7 +222,7 @@ TraceOpaqueResult TraceOpaque( inout TraceOpaqueDesc desc )
227222
{
228223
// Update materials, direct lighting and emission
229224
float3 psrNormal = float3( 0, 0, 1 );
230-
uint materialID = GetMaterialID( geometryProps, materialProps );
225+
float materialID = GetMaterialID( geometryProps, materialProps );
231226

232227
if( !geometryProps.IsSky( ) )
233228
{
@@ -257,6 +252,7 @@ TraceOpaqueResult TraceOpaque( inout TraceOpaqueDesc desc )
257252
SharcHitData sharcHitData;
258253
sharcHitData.positionWorld = Xglobal;
259254
sharcHitData.normalWorld = geometryProps.N;
255+
sharcHitData.emissive = materialProps.Lemi;
260256

261257
HashMapData hashMapData;
262258
hashMapData.capacity = SHARC_CAPACITY;
@@ -590,7 +586,7 @@ TraceOpaqueResult TraceOpaque( inout TraceOpaqueDesc desc )
590586
// Lighting
591587
//=============================================================================================================================================================
592588

593-
float4 Lcached = 0;
589+
float4 Lcached = float4( materialProps.Lemi, 0.0 );
594590
if( !geometryProps.IsSky( ) )
595591
{
596592
// L1 cache - reproject previous frame, carefully treating specular
@@ -615,6 +611,7 @@ TraceOpaqueResult TraceOpaque( inout TraceOpaqueDesc desc )
615611
SharcHitData sharcHitData;
616612
sharcHitData.positionWorld = Xglobal;
617613
sharcHitData.normalWorld = geometryProps.N;
614+
sharcHitData.emissive = materialProps.Lemi;
618615

619616
HashMapData hashMapData;
620617
hashMapData.capacity = SHARC_CAPACITY;
@@ -643,7 +640,6 @@ TraceOpaqueResult TraceOpaque( inout TraceOpaqueDesc desc )
643640
Lcached.xyz = bounce < desc.bounceNum ? L : max( Lcached.xyz, L );
644641
}
645642
}
646-
Lcached.xyz = max( Lcached.xyz, materialProps.Lemi );
647643

648644
//=============================================================================================================================================================
649645
// Other
@@ -862,7 +858,7 @@ void main( uint2 pixelPos : SV_DispatchThreadId )
862858
}
863859

864860
// G-buffer
865-
uint materialID = GetMaterialID( geometryProps0, materialProps0 );
861+
float materialID = GetMaterialID( geometryProps0, materialProps0 );
866862
#if( USE_SIMULATED_MATERIAL_ID_TEST == 1 )
867863
materialID = frac( geometryProps0.X ).x < 0.05 ? MATERIAL_ID_HAIR : materialID;
868864
#endif

Shaders/TraceTransparent.cs.hlsl

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ float3 TraceTransparent( TraceTransparentDesc desc )
108108
MaterialProps materialProps = GetMaterialProps( geometryProps );
109109

110110
// Lighting
111-
float4 Lcached = 0;
111+
float4 Lcached = float4( materialProps.Lemi, 0.0 );
112112
if( !geometryProps.IsSky( ) )
113113
{
114114
// L1 cache - reproject previous frame, carefully treating specular
@@ -135,6 +135,7 @@ float3 TraceTransparent( TraceTransparentDesc desc )
135135
SharcHitData sharcHitData;
136136
sharcHitData.positionWorld = Xglobal;
137137
sharcHitData.normalWorld = geometryProps.N;
138+
sharcHitData.emissive = materialProps.Lemi;
138139

139140
HashMapData hashMapData;
140141
hashMapData.capacity = SHARC_CAPACITY;
@@ -162,7 +163,6 @@ float3 TraceTransparent( TraceTransparentDesc desc )
162163
Lcached.xyz = max( Lcached.xyz, L );
163164
}
164165
}
165-
Lcached.xyz = max( Lcached.xyz, materialProps.Lemi );
166166

167167
// Output
168168
return Lcached.xyz * pathThroughput;

0 commit comments

Comments
 (0)