Skip to content

Commit

Permalink
ssao - some configuration exposed
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Oct 24, 2024
1 parent 140960f commit 9f1d6f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
18 changes: 12 additions & 6 deletions data/shaders/ssao_blit.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

cbuffer Data : register(b4) {
uint u_downscale;
float u_depth_diff_weight;
TextureHandle u_ssao_buf;
TextureHandle u_depthbuffer; // full-size
TextureHandle u_depthbuffer_small; // downscaled, if available
Expand Down Expand Up @@ -43,19 +44,24 @@ void main(uint3 thread_id : SV_DispatchThreadID) {

// compute weights as combination of depth differences and bilinear coefs
// depth difference added to bilinear coefs to fixed artifacts at depth discontinuities
float w00 = r00 - r00 * abs(d - d00);
float w10 = r10 - r10 * abs(d - d10);
float w01 = r01 - r01 * abs(d - d01);
float w11 = r11 - r11 * abs(d - d11);
float w00 = saturate(r00 - saturate(r00 * abs(d - d00) * u_depth_diff_weight));
float w10 = saturate(r10 - saturate(r10 * abs(d - d10) * u_depth_diff_weight));
float w01 = saturate(r01 - saturate(r01 * abs(d - d01) * u_depth_diff_weight));
float w11 = saturate(r11 - saturate(r11 * abs(d - d11) * u_depth_diff_weight));

ssao = ssao00 * w00
+ ssao10 * w10
+ ssao01 * w01
+ ssao11 * w11;

// normalize weight (because of depth differences)
float sum_w = w00 + w01 + w10 + w11;
ssao /= sum_w;
if (sum_w < 10e-6) {
// our depth is not close to any from the four sampled depths, just use the first one
ssao = ssao00;
} else {
// normalize weight (because of depth differences)
ssao = saturate(ssao / (sum_w + 10e-6));
}
} else {
// ssao is at full resolution, just sample it
float2 uv = (thread_id.xy + 0.5) * Global_rcp_framebuffer_size;
Expand Down
10 changes: 7 additions & 3 deletions src/renderer/postprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ struct SSAO : public RenderPlugin {
bool m_enabled = true;
u32 m_blur_iterations = 3;
u32 m_downscale = 1;
float m_depth_diff_weight = 2;
float m_radius = 0.4f;
float m_intensity = 1.f;

Expand All @@ -729,9 +730,10 @@ struct SSAO : public RenderPlugin {
void debugUI(Pipeline& pipeline) override {
if (ImGui::BeginMenu("SSAO")) {
ImGui::Checkbox("Enabled", &m_enabled);
ImGui::DragFloat("Radius", &m_radius);
ImGui::DragFloat("Intensity", &m_intensity);
ImGui::DragInt("Blur iterations", (i32*)&m_blur_iterations);
ImGui::DragFloat("Radius", &m_radius, 0.1f, FLT_MIN, FLT_MAX);
ImGui::DragFloat("Intensity", &m_intensity, 0.1f, FLT_MIN, FLT_MAX);
ImGui::DragFloat("Depth difference weight", &m_depth_diff_weight, 0.1f, FLT_MIN, FLT_MAX);
ImGui::DragInt("Blur iterations", (i32*)&m_blur_iterations, 1, 0, 50);
const char* downscale_values[] = { "Disabled", "2x", "4x" };
ImGui::TextUnformatted("Downscale");
for (const char*& v : downscale_values) {
Expand Down Expand Up @@ -861,12 +863,14 @@ struct SSAO : public RenderPlugin {

struct {
u32 downscale;
float depth_diff_weight;
gpu::BindlessHandle ssao_buf;
gpu::BindlessHandle depth_buffer;
gpu::BindlessHandle depth_buffer_small;
gpu::RWBindlessHandle gbufferB;
} udata2 = {
.downscale = m_downscale,
.depth_diff_weight = m_depth_diff_weight,
.ssao_buf = pipeline.toBindless(ssao_rb, stream),
.depth_buffer = pipeline.toBindless(gbuffer.DS, stream),
.depth_buffer_small = pipeline.toBindless(depth_buffer, stream),
Expand Down

0 comments on commit 9f1d6f8

Please sign in to comment.