Skip to content

Commit

Permalink
feat: sharper ssgi blur (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pentalimbed authored Aug 8, 2024
1 parent de93278 commit 22dffac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
28 changes: 18 additions & 10 deletions features/Screen Space GI/Shaders/ScreenSpaceGI/blur.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#include "../Common/VR.hlsli"
#include "common.hlsli"

#ifdef SPECULAR_BLUR
# undef TEMPORAL_DENOISER
#endif

Texture2D<float4> srcGI : register(t0); // maybe half-res
Texture2D<unorm float> srcAccumFrames : register(t1); // maybe half-res
Texture2D<half> srcDepth : register(t2);
Expand All @@ -31,6 +27,11 @@ static const float3 g_Poisson8[8] = {
float3(+0.1564120, -0.8198990, +0.8346850)
};

float GaussianWeight(float r)
{
return exp(-0.66 * r * r);
}

// http://marc-b-reynolds.github.io/quaternions/2016/07/06/Orthonormal.html
float3x3 getBasis(float3 N)
{
Expand Down Expand Up @@ -98,23 +99,26 @@ float2x3 getKernelBasis(float3 D, float3 N, float roughness = 1.0, float anisoFa
const float worldRadius = radius * pixelDirRBViewspaceSizeAtCenterZ.x;
#ifdef SPECULAR_BLUR
float2x3 TvBv = getKernelBasis(getSpecularDominantDirection(normal, -normalize(pos), roughness), normal, roughness);
const float halfAngle = specularLobeHalfAngle(roughness);
float halfAngle = specularLobeHalfAngle(roughness);
#else
float2x3 TvBv = getKernelBasis(normal, normal); // D = N
const float halfAngle = fsl_HALF_PI;
float halfAngle = fsl_HALF_PI * .5f;
#endif
TvBv[0] *= worldRadius;
TvBv[1] *= worldRadius;
#ifdef TEMPORAL_DENOISER
halfAngle *= 1.01 - sqrt(accumFrames / (float)MaxAccumFrames);
#endif

float4 gi = srcGI[dtid];

float4 sum = gi;
#ifdef TEMPORAL_DENOISER
#if defined(TEMPORAL_DENOISER) && !defined(SPECULAR_BLUR)
float fsum = accumFrames;
#endif
float wsum = 1;
for (uint i = 0; i < numSamples; i++) {
float w = g_Poisson8[i].z;
float w = GaussianWeight(g_Poisson8[i].z);

float2 poissonOffset = g_Poisson8[i].xy;

Expand Down Expand Up @@ -155,16 +159,20 @@ float2x3 getKernelBasis(float3 D, float3 N, float roughness = 1.0, float anisoFa
// roughness weight
w *= abs(roughness - roughnessSample) / (roughness * roughness * 0.99 + 0.01);
#endif
#ifdef TEMPORAL_DENOISER
// luminance weight
w *= exp(-dot(abs(gi - giSample), 1) / (sqrt(accumFrames / (float)MaxAccumFrames) + 1e-8));
#endif

sum += giSample * w;
#ifdef TEMPORAL_DENOISER
#if defined(TEMPORAL_DENOISER) && !defined(SPECULAR_BLUR)
fsum += srcAccumFrames.SampleLevel(samplerLinearClamp, uvSample * OUT_FRAME_SCALE, 0) * w;
#endif
wsum += w;
}

outGI[dtid] = sum / wsum;
#ifdef TEMPORAL_DENOISER
#if defined(TEMPORAL_DENOISER) && !defined(SPECULAR_BLUR)
outAccumFrames[dtid] = fsum / wsum;
#endif
}
4 changes: 4 additions & 0 deletions features/Screen Space GI/Shaders/ScreenSpaceGI/gi.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ void CalculateGI(

uint2 bitsRangeGI = uint2(round(angleRangeGI.x * 32u), round((angleRangeGI.y - angleRangeGI.x) * 32u));
uint maskedBitsGI = ((1 << bitsRangeGI.y) - 1) << bitsRangeGI.x;

uint checkGI = maskedBitsGI & ~bitmaskGI;
# ifdef GI_SPECULAR
checkGI = checkGI || (maskedBitsGISpecular & ~bitsRangeGISpecular);
# endif
# else
bool checkGI = shc > horizonCos;
# endif
Expand Down
2 changes: 1 addition & 1 deletion src/Features/ScreenSpaceGI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ void ScreenSpaceGI::DrawSSGI(Texture2D* srcPrevAmbient)
if (doSpecular) {
resetViews();
srvs.at(0) = texGISpecular[inputGITexIdx]->srv.get();
srvs.at(1) = nullptr;
srvs.at(1) = texAccumFrames[lastFrameAccumTexIdx]->srv.get();
srvs.at(2) = texWorkingDepth->srv.get();
srvs.at(3) = rts[NORMALROUGHNESS].SRV;

Expand Down

0 comments on commit 22dffac

Please sign in to comment.