Skip to content

Commit

Permalink
fix: clean up ssgi blur and upsample, change presets (doodlum#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pentalimbed authored Dec 12, 2024
1 parent b898542 commit e179b04
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 31 deletions.
22 changes: 13 additions & 9 deletions features/Screen Space GI/Shaders/ScreenSpaceGI/blur.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Common/FrameBuffer.hlsli"
#include "Common/GBuffer.hlsli"
#include "Common/Math.hlsli"
#include "Common/Random.hlsli"
#include "Common/VR.hlsli"
#include "ScreenSpaceGI/common.hlsli"

Expand Down Expand Up @@ -76,6 +77,12 @@ float2x3 getKernelBasis(float3 D, float3 N, float roughness = 1.0, float anisoFa
}

// TODO: spinning blur
float2x2 getRotationMatrix(float noise)
{
float2 sin_cos;
sincos(noise * Math::PI * 2, sin_cos.y, sin_cos.x);
return float2x2(sin_cos.x, sin_cos.y, -sin_cos.y, sin_cos.x);
}

[numthreads(8, 8, 1)] void main(const uint2 dtid
: SV_DispatchThreadID) {
Expand All @@ -99,7 +106,7 @@ float2x3 getKernelBasis(float3 D, float3 N, float roughness = 1.0, float anisoFa
const float2 pixelDirRBViewspaceSizeAtCenterZ = depth.xx * (eyeIndex == 0 ? NDCToViewMul.xy : NDCToViewMul.zw) * RCP_OUT_FRAME_DIM;
const float worldRadius = radius * pixelDirRBViewspaceSizeAtCenterZ.x;
float2x3 TvBv = getKernelBasis(normal, normal); // D = N
float halfAngle = Math::HALF_PI * .5f;
float halfAngle = Math::HALF_PI;

TvBv[0] *= worldRadius;
TvBv[1] *= worldRadius;
Expand Down Expand Up @@ -139,10 +146,10 @@ float2x3 getKernelBasis(float3 D, float3 N, float roughness = 1.0, float anisoFa
float2 uvSample = Stereo::ConvertToStereoUV(screenPosSample.xy, eyeIndex);
uvSample = (floor(uvSample * OUT_FRAME_DIM) + 0.5) * RCP_OUT_FRAME_DIM; // Snap to the pixel centre

float depthSample = srcDepth.SampleLevel(samplerLinearClamp, uvSample * frameScale, 0);
float depthSample = srcDepth.SampleLevel(samplerPointClamp, uvSample * frameScale, RES_MIP);
float3 posSample = ScreenToViewPosition(screenPosSample.xy, depthSample, eyeIndex);

float4 normalRoughnessSample = srcNormalRoughness.SampleLevel(samplerLinearClamp, uvSample * frameScale, 0);
float4 normalRoughnessSample = srcNormalRoughness.SampleLevel(samplerPointClamp, uvSample * frameScale, 0);
float3 normalSample = GBuffer::DecodeNormal(normalRoughnessSample.xy);

// geometry weight
Expand All @@ -151,13 +158,10 @@ float2x3 getKernelBasis(float3 D, float3 N, float roughness = 1.0, float anisoFa
w *= 1 - saturate(FastMath::acosFast4(saturate(dot(normalSample, normal))) / halfAngle);

if (w > 1e-8) {
float4 ySample = srcIlY.SampleLevel(samplerLinearClamp, uvSample * OUT_FRAME_SCALE, 0);
float2 coCgSample = srcIlCoCg.SampleLevel(samplerLinearClamp, uvSample * OUT_FRAME_SCALE, 0);

ySum += ySample * w;
coCgSum += coCgSample * w;
ySum += srcIlY.SampleLevel(samplerPointClamp, uvSample * OUT_FRAME_SCALE, 0) * w;
coCgSum += srcIlCoCg.SampleLevel(samplerPointClamp, uvSample * OUT_FRAME_SCALE, 0) * w;
#if defined(TEMPORAL_DENOISER)
fSum += srcAccumFrames.SampleLevel(samplerLinearClamp, uvSample * OUT_FRAME_SCALE, 0) * w;
fSum += srcAccumFrames.SampleLevel(samplerPointClamp, uvSample * OUT_FRAME_SCALE, 0) * w;
#endif
wSum += w;
}
Expand Down
7 changes: 5 additions & 2 deletions features/Screen Space GI/Shaders/ScreenSpaceGI/common.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,21 @@ SamplerState samplerLinearClamp : register(s1);
// texCoord - texture coordinate

#ifdef HALF_RES
# define READ_DEPTH(tex, px) tex.Load(int3(px, 1))
# define RES_MIP 1
# define READ_DEPTH(tex, px) tex.Load(int3(px, RES_MIP))
# define FULLRES_LOAD(tex, px, texCoord, samp) tex.SampleLevel(samp, texCoord, 0)
# define OUT_FRAME_DIM (FrameDim * 0.5)
# define RCP_OUT_FRAME_DIM (RcpFrameDim * 2)
# define OUT_FRAME_SCALE (frameScale * 0.5)
#elif defined(QUARTER_RES)
# define READ_DEPTH(tex, px) tex.Load(int3(px, 2))
# define RES_MIP 2
# define READ_DEPTH(tex, px) tex.Load(int3(px, RES_MIP))
# define FULLRES_LOAD(tex, px, texCoord, samp) tex.SampleLevel(samp, texCoord, 0)
# define OUT_FRAME_DIM (FrameDim * 0.25)
# define RCP_OUT_FRAME_DIM (RcpFrameDim * 4)
# define OUT_FRAME_SCALE (frameScale * 0.25)
#else
# define RES_MIP 0
# define READ_DEPTH(tex, px) tex[px]
# define FULLRES_LOAD(tex, px, texCoord, samp) tex[px]
# define OUT_FRAME_DIM FrameDim
Expand Down
7 changes: 3 additions & 4 deletions features/Screen Space GI/Shaders/ScreenSpaceGI/gi.cs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,12 @@ void CalculateGI(
if (frontBackMult > 0.f) {
float3 sampleHorizonVecWS = normalize(mul(FrameBuffer::CameraViewInverse[eyeIndex], half4(sampleHorizonVec, 0)).xyz);

float3 sampleRadiance = srcRadiance.SampleLevel(samplerPointClamp, sampleUV * OUT_FRAME_SCALE, mipLevel).rgb * frontBackMult * giBoost;
float3 sampleRadiance = srcRadiance.SampleLevel(samplerPointClamp, sampleUV * OUT_FRAME_SCALE, mipLevel).rgb * frontBackMult * giBoost * countbits(validBits) * 0.03125;
sampleRadiance = max(sampleRadiance, 0);
float3 sampleRadianceYCoCg = Color::RGBToYCoCg(sampleRadiance);

float bitmaskWeight = countbits(validBits) * 0.03125;
radianceY += sampleRadianceYCoCg.r * SphericalHarmonics::Evaluate(sampleHorizonVecWS) * bitmaskWeight;
radianceCoCg += sampleRadianceYCoCg.gb * bitmaskWeight;
radianceY += sampleRadianceYCoCg.r * SphericalHarmonics::Evaluate(sampleHorizonVecWS);
radianceCoCg += sampleRadianceYCoCg.gb;
}
}
#endif // GI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ RWTexture2D<half2> outIlCoCg : register(u2);
int2 px11 = px00 + int2(1, 1);

float4 d = float4(
srcDepth.Load(int3(px00, 1)),
srcDepth.Load(int3(px01, 1)),
srcDepth.Load(int3(px10, 1)),
srcDepth.Load(int3(px11, 1)));
srcDepth.Load(int3(px00, RES_MIP)),
srcDepth.Load(int3(px01, RES_MIP)),
srcDepth.Load(int3(px10, RES_MIP)),
srcDepth.Load(int3(px11, RES_MIP)));

// note: edge-detection
float mind = min4(d);
Expand Down
20 changes: 12 additions & 8 deletions src/Features/ScreenSpaceGI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,43 +79,47 @@ void ScreenSpaceGI::DrawSettings()

ImGui::TableNextColumn();
if (ImGui::Button("Low", { -1, 0 })) {
settings.NumSlices = 2;
settings.NumSteps = 4;
settings.NumSlices = 10;
settings.NumSteps = 12;
settings.ResolutionMode = 2;
settings.EnableGI = true;
recompileFlag = true;
}
if (auto _tt = Util::HoverTooltipWrapper())
ImGui::Text("2 Slices, 4 Steps");
ImGui::Text("Quarter res and blurry.");

ImGui::TableNextColumn();
if (ImGui::Button("Medium", { -1, 0 })) {
settings.NumSlices = 3;
settings.NumSteps = 6;
settings.NumSlices = 5;
settings.NumSteps = 8;
settings.ResolutionMode = 1;
settings.EnableGI = true;
recompileFlag = true;
}
if (auto _tt = Util::HoverTooltipWrapper())
ImGui::Text("3 Slices, 6 Steps");
ImGui::Text("Half res and somewhat stable.");

ImGui::TableNextColumn();
if (ImGui::Button("High", { -1, 0 })) {
settings.NumSlices = 4;
settings.NumSteps = 8;
settings.ResolutionMode = 0;
settings.EnableGI = true;
recompileFlag = true;
}
if (auto _tt = Util::HoverTooltipWrapper())
ImGui::Text("4 Slices, 8 Steps");
ImGui::Text("Full res and clean.");

ImGui::TableNextColumn();
if (ImGui::Button("Ultra", { -1, 0 })) {
settings.NumSlices = 6;
settings.NumSteps = 10;
settings.ResolutionMode = 0;
settings.EnableGI = true;
recompileFlag = true;
}
if (auto _tt = Util::HoverTooltipWrapper())
ImGui::Text("6 Slices, 10 Steps");
ImGui::Text("Reference mode.");

ImGui::EndTable();
}
Expand Down
8 changes: 4 additions & 4 deletions src/Features/ScreenSpaceGI.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ struct ScreenSpaceGI : Feature
bool Enabled = true;
bool EnableGI = true;
// performance/quality
uint NumSlices = 2;
uint NumSteps = 4;
uint NumSlices = 5;
uint NumSteps = 8;
int ResolutionMode = 1; // 0-full, 1-half, 2-quarter
// visual
float MinScreenRadius = 0.01f;
Expand All @@ -70,8 +70,8 @@ struct ScreenSpaceGI : Feature
bool EnableBlur = true;
float DepthDisocclusion = .1f;
float NormalDisocclusion = .1f;
uint MaxAccumFrames = 32;
float BlurRadius = 15.f;
uint MaxAccumFrames = 16;
float BlurRadius = 5.f;
float DistanceNormalisation = 2.f;
} settings;

Expand Down

0 comments on commit e179b04

Please sign in to comment.