<NME> SSAOPass.cpp
<BEF> /*******************************
Copyright (c) 2016-2022 Grégoire Angerand

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**********************************/

#include "SSAOPass.h"
#include "DownsamplePass.h"

#include <yave/framegraph/FrameGraph.h>
#include <yave/framegraph/FrameGraphPass.h>
#include <yave/framegraph/FrameGraphFrameResources.h>
#include <yave/graphics/commands/CmdBufferRecorder.h>
#include <yave/graphics/shaders/ComputeProgram.h>
#include <yave/graphics/device/DeviceResources.h>

namespace yave {

struct MiniAOParams {
    float inv_thickness[12];
    float weights[12];
};

struct UpsampleParams {
    float step_size;
    float noise_weight;
    float blur_tolerance;
    float upsample_tolerance;
};

static float compute_tan_half_fov(const GBufferPass& gbuffer) {
    const Camera& camera = gbuffer.scene_pass.scene_view.camera();
    return 1.0f / camera.proj_matrix()[0][0];
}

static MiniAOParams compute_ao_params(float tan_half_fov, usize screen_size_x) {
    const float sample_thickness[12] = {
        std::sqrt(1.0f - 0.2f * 0.2f),
        std::sqrt(1.0f - 0.4f * 0.4f),
        std::sqrt(1.0f - 0.6f * 0.6f),
        std::sqrt(1.0f - 0.8f * 0.8f),
        std::sqrt(1.0f - 0.2f * 0.2f - 0.2f * 0.2f),
        std::sqrt(1.0f - 0.2f * 0.2f - 0.4f * 0.4f),
        std::sqrt(1.0f - 0.2f * 0.2f - 0.6f * 0.6f),
        std::sqrt(1.0f - 0.2f * 0.2f - 0.8f * 0.8f),
        std::sqrt(1.0f - 0.4f * 0.4f - 0.4f * 0.4f),
        std::sqrt(1.0f - 0.4f * 0.4f - 0.6f * 0.6f),
        std::sqrt(1.0f - 0.4f * 0.4f - 0.8f * 0.8f),
        std::sqrt(1.0f - 0.6f * 0.6f - 0.6f * 0.6f)
    };



    const float screenspace_diameter = 10.0f;
    Y_TODO(Do we need to multiply by 2 here?)
    const float thickness_multiplier = /* 2.0f * */ 2.0f * tan_half_fov * screenspace_diameter / screen_size_x;
    const float inv_range = 1.0f / thickness_multiplier;

    MiniAOParams params = {};

    for(usize i = 0; i != 12; ++i) {
        params.inv_thickness[i] = inv_range / sample_thickness[i];
    }

    params.weights[0]  = 4.0f * sample_thickness[0];     // Axial
    params.weights[1]  = 4.0f * sample_thickness[1];     // Axial
    params.weights[2]  = 4.0f * sample_thickness[2];     // Axial
    params.weights[3]  = 4.0f * sample_thickness[3];     // Axial
    params.weights[4]  = 4.0f * sample_thickness[4];     // Diagonal
    params.weights[5]  = 8.0f * sample_thickness[5];     // L-shaped
    params.weights[6]  = 8.0f * sample_thickness[6];     // L-shaped
    params.weights[7]  = 8.0f * sample_thickness[7];     // L-shaped
    params.weights[8]  = 4.0f * sample_thickness[8];     // Diagonal
    params.weights[9]  = 8.0f * sample_thickness[9];     // L-shaped
    params.weights[10] = 8.0f * sample_thickness[10];    // L-shaped
    params.weights[11] = 4.0f * sample_thickness[11];    // Diagonal

    // SAMPLE_EXHAUSTIVELY ?
    // https://github.com/microsoft/DirectX-Graphics-Samples/blob/72b1ff9830286950960116264b414ea3c842ff47/MiniEngine/Core/SSAO.cpp#L202
    {
        params.weights[0] = 0.0f;
        params.weights[2] = 0.0f;
        params.weights[5] = 0.0f;
        params.weights[7] = 0.0f;
        params.weights[9] = 0.0f;
    }

    float total = 0.0f;
    for(usize i = 0; i != 12; ++i) {
        total += params.weights[i];
    }

    for(usize i = 0; i != 12; ++i) {
        params.weights[i] /= total;
    }

    return params;
}

static FrameGraphImageId compute_linear_depth(FrameGraph& framegraph, const GBufferPass& gbuffer, const math::Vec2ui& size) {
    static constexpr ImageFormat depth_format = VK_FORMAT_R32_SFLOAT;

    FrameGraphPassBuilder builder = framegraph.add_pass("Depth linearization pass");

    const auto linear_depth = builder.declare_image(depth_format, size);

    builder.add_uniform_input(gbuffer.depth, 0, PipelineStage::ComputeBit);
    builder.add_uniform_input(gbuffer.scene_pass.camera_buffer, 0, PipelineStage::ComputeBit);
    builder.add_storage_output(linear_depth, 0, PipelineStage::ComputeBit);
    builder.set_render_func([=](CmdBufferRecorder& recorder, const FrameGraphPass* self) {
        const auto& program = device_resources()[DeviceResources::LinearizeDepthProgram];
        recorder.dispatch_size(program, size, {self->descriptor_sets()[0]});
    });

    return linear_depth;
}

static FrameGraphMutableImageId upsample_mini_ao(FrameGraph& framegraph,
                                     u32 final_size_x,
                                     const math::Vec2ui& output_size,
                                     const SSAOSettings& settings,
                                     FrameGraphImageId hi_depth,
                                     FrameGraphImageId lo_depth,
                                     FrameGraphImageId hi_ao,
                                     FrameGraphImageId lo_ao = FrameGraphImageId()) {

    const bool merge = lo_ao.is_valid();
    const ImageFormat format = framegraph.image_format(hi_ao);
    //const math::Vec2ui hi_size = framegraph.image_size(hi_depth);
    const math::Vec2ui lo_size = framegraph.image_size(lo_depth);

    const float step_size = final_size_x / float(lo_size.x());
    float blur_tolerance = 1.0f - std::pow(10.0f, -settings.blur_tolerance) * step_size;
    blur_tolerance *= blur_tolerance;
    const float upsample_tolerance = std::pow(10.0f, -settings.upsample_tolerance);
    const float noise_filter_weight = 1.0f / (std::pow(10.0f, -settings.noise_filter_tolerance) + upsample_tolerance);

    FrameGraphPassBuilder builder = framegraph.add_pass("SSAO upsample pass");

    const auto upsampled = builder.declare_image(format, output_size);

    const Texture& white = *device_resources()[DeviceResources::WhiteTexture];
    builder.add_uniform_input_with_default(lo_ao, Descriptor(white), 0, PipelineStage::ComputeBit);
    builder.add_uniform_input(hi_ao, 0, PipelineStage::ComputeBit);
    builder.add_uniform_input(lo_depth, 0, PipelineStage::ComputeBit);
    builder.add_uniform_input(hi_depth, 0, PipelineStage::ComputeBit);
    builder.add_inline_input(InlineDescriptor(UpsampleParams{step_size, noise_filter_weight, blur_tolerance, upsample_tolerance}), 0);
    builder.add_storage_output(upsampled, 0, PipelineStage::ComputeBit);
    builder.set_render_func([=](CmdBufferRecorder& recorder, const FrameGraphPass* self) {
        const auto& program = device_resources()[merge ? DeviceResources::SSAOUpsampleMergeProgram : DeviceResources::SSAOUpsampleProgram];
        recorder.dispatch_size(program, lo_size + math::Vec2ui(2), {self->descriptor_sets()[0]});
    });

    return upsampled;
}

static FrameGraphImageId compute_mini_ao(FrameGraph& framegraph, FrameGraphImageId linear_depth, float tan_half_fov) {
    static constexpr ImageFormat format = VK_FORMAT_R8_UNORM;
    const math::Vec2ui size = framegraph.image_size(linear_depth);

    FrameGraphPassBuilder builder = framegraph.add_pass("SSAO pass");

    const auto ao = builder.declare_image(format, size);

    builder.add_uniform_input(linear_depth, 0, PipelineStage::ComputeBit);
    builder.add_inline_input(InlineDescriptor(compute_ao_params(tan_half_fov, size.x())), 0);
    builder.add_storage_output(ao, 0, PipelineStage::ComputeBit);
    builder.set_render_func([=](CmdBufferRecorder& recorder, const FrameGraphPass* self) {
        const auto& program = device_resources()[DeviceResources::SSAOProgram];
        recorder.dispatch_size(program, size, {self->descriptor_sets()[0]});
    });

    return ao;
}

SSAOPass SSAOPass::create(FrameGraph& framegraph, const GBufferPass& gbuffer, const SSAOSettings& settings) {
    const auto region = framegraph.region("SSAO");

    const math::Vec2ui size = framegraph.image_size(gbuffer.depth);

    FrameGraphImageId ao;
    switch(settings.method) {
    return upsampled;
}

Y_TODO(Clamp depth reads to avoid leaks)

SSAOPass SSAOPass::create(FrameGraph& framegraph, const GBufferPass& gbuffer, const SSAOSettings& settings) {
    const auto region = framegraph.region("SSAO");
                const FrameGraphImageId hi = compute_mini_ao(framegraph, downsample.mips[i], tan_half_fov);
                ao = upsample_mini_ao(framegraph, size.x(), output_size, settings, downsample.mips[i - 1], downsample.mips[i], hi, ao);
            }
        } break;

        default:
        break;
    }
            const DownsamplePass downsample = DownsamplePass::create(framegraph, linear_depth, settings.level_count, DownsamplePass::Filter::BestMatch);
            const float tan_half_fov = compute_tan_half_fov(gbuffer);
            for(usize i = settings.level_count - 1; i > 0; --i) {
                // interleave(framegraph, downsample.mips[i])
                const math::Vec2ui output_size = framegraph.image_size(downsample.mips[i - 1]);
                const FrameGraphImageId hi = compute_ao(framegraph, downsample.mips[i], tan_half_fov);
                ao = upsample_ao(framegraph, size.x(), output_size, settings, downsample.mips[i - 1], downsample.mips[i], hi, ao);
}


    return output;
}
#endif

}

<MSG> Added sampler to framegraph input
Changed uniform input to use clamp by default

<DFF> @@ -199,7 +199,7 @@ static FrameGraphImageId upsample_ao(FrameGraph& framegraph,
     return upsampled;
 }
 
-Y_TODO(Clamp depth reads to avoid leaks)
+Y_TODO(Generates line artifacts when size is not divisible by 8)
 
 SSAOPass SSAOPass::create(FrameGraph& framegraph, const GBufferPass& gbuffer, const SSAOSettings& settings) {
     const auto region = framegraph.region("SSAO");
@@ -214,7 +214,6 @@ SSAOPass SSAOPass::create(FrameGraph& framegraph, const GBufferPass& gbuffer, co
             const DownsamplePass downsample = DownsamplePass::create(framegraph, linear_depth, settings.level_count, DownsamplePass::Filter::BestMatch);
             const float tan_half_fov = compute_tan_half_fov(gbuffer);
             for(usize i = settings.level_count - 1; i > 0; --i) {
-                // interleave(framegraph, downsample.mips[i])
                 const math::Vec2ui output_size = framegraph.image_size(downsample.mips[i - 1]);
                 const FrameGraphImageId hi = compute_ao(framegraph, downsample.mips[i], tan_half_fov);
                 ao = upsample_ao(framegraph, size.x(), output_size, settings, downsample.mips[i - 1], downsample.mips[i], hi, ao);
@@ -250,6 +249,41 @@ static FrameGraphImageId interleave(FrameGraph& framegraph, FrameGraphImageId im
 
     return output;
 }
+
+static FrameGraphImageId upsample_ao(FrameGraph& framegraph, const math::Vec2ui& output_size, FrameGraphImageId filter, FrameGraphImageId ao) {
+    struct Params {
+        math::Vec4 weights[9];
+    } params = {};
+
+
+    const float sigma = 2.0f;
+    const usize kernel_size = (9 - 1) / 2;
+    for(usize i = 0; i != kernel_size; ++i) {
+        const float x = float(i);
+        const float w = 0.39894f * std::exp(-0.5f * x * x / (sigma * sigma)) / sigma;
+        params.weights[kernel_size + i] = params.weights[kernel_size - i] = w;
+    }
+
+    const ImageFormat format = framegraph.image_format(ao);
+
+    FrameGraphPassBuilder builder = framegraph.add_pass("TEST pass");
+
+    const auto output = builder.declare_image(format, output_size);
+    const auto params_buffer = builder.declare_typed_buffer<Params>();
+
+    builder.add_uniform_input(ao, 0, PipelineStage::ComputeBit);
+    builder.add_uniform_input(filter, 0, PipelineStage::ComputeBit);
+    builder.add_storage_output(output, 0, PipelineStage::ComputeBit);
+    builder.add_uniform_input(params_buffer, 0, PipelineStage::ComputeBit);
+    builder.map_update(params_buffer);
+    builder.set_render_func([=](CmdBufferRecorder& recorder, const FrameGraphPass* self) {
+        self->resources().mapped_buffer(params_buffer)[0] = params;
+        const auto& program = device_resources(recorder.device()).program_from_file("bilateral.comp");
+        recorder.dispatch_size(program, output_size * 2, {self->descriptor_sets()[0]});
+    });
+
+    return output;
+}
 #endif
 
 }

Added sampler to framegraph input Changed uniform input to use clamp by default