Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions samples/performance/msaa/msaa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,19 @@ bool MSAASample::prepare(const vkb::ApplicationOptions &options)
auto &camera_node = vkb::add_free_camera(get_scene(), "main_camera", get_render_context().get_surface_extent());
camera = dynamic_cast<vkb::sg::PerspectiveCamera *>(&camera_node.get_component<vkb::sg::Camera>());

vkb::ShaderSource scene_vs("base.vert");
vkb::ShaderSource scene_fs("base.frag");
vkb::ShaderSource scene_vs{"base.vert"};
vkb::ShaderSource scene_fs{"base.frag"};
auto scene_subpass = std::make_unique<vkb::ForwardSubpass>(get_render_context(), std::move(scene_vs), std::move(scene_fs), get_scene(), *camera);
scene_pipeline = std::make_unique<vkb::RenderPipeline>();
scene_pipeline->add_subpass(std::move(scene_subpass));

vkb::ShaderSource postprocessing_vs("postprocessing/postprocessing.vert");
postprocessing_pipeline = std::make_unique<vkb::PostProcessingPipeline>(get_render_context(), std::move(postprocessing_vs));
postprocessing_pipeline = std::make_unique<vkb::PostProcessingPipeline>(get_render_context(), vkb::ShaderSource{"postprocessing/postprocessing.vert"});
postprocessing_pipeline->add_pass()
.add_subpass(vkb::ShaderSource("postprocessing/outline.frag"));
.add_subpass(vkb::ShaderSource{"postprocessing/outline.frag"});

ms_depth_postprocessing_pipeline = std::make_unique<vkb::PostProcessingPipeline>(get_render_context(), vkb::ShaderSource{"postprocessing/postprocessing.vert"});
ms_depth_postprocessing_pipeline->add_pass()
.add_subpass(vkb::ShaderSource{"postprocessing/outline_ms_depth.frag"});

update_pipelines();

Expand Down Expand Up @@ -587,7 +590,10 @@ void MSAASample::postprocessing(vkb::CommandBuffer &command_buffer, vkb::RenderT

glm::vec4 near_far = {camera->get_far_plane(), camera->get_near_plane(), -1.0f, -1.0f};

auto &postprocessing_pass = postprocessing_pipeline->get_pass(0);
// Select the currently active pipeline
auto &pipeline = multisampled_depth ? ms_depth_postprocessing_pipeline : postprocessing_pipeline;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is what replaces existing behavior


auto &postprocessing_pass = pipeline->get_pass(0);
postprocessing_pass.set_uniform_data(near_far);

auto &postprocessing_subpass = postprocessing_pass.get_subpass(0);
Expand All @@ -596,17 +602,14 @@ void MSAASample::postprocessing(vkb::CommandBuffer &command_buffer, vkb::RenderT
postprocessing_subpass.unbind_sampled_image("ms_depth_sampler");

postprocessing_subpass.get_fs_variant().clear();
if (multisampled_depth)
{
postprocessing_subpass.get_fs_variant().add_define("MS_DEPTH");
}

postprocessing_subpass
.bind_sampled_image(depth_sampler_name, {depth_attachment, nullptr, nullptr, depth_writeback_resolve_supported && resolve_depth_on_writeback})
.bind_sampled_image("color_sampler", i_color_resolve);

// Second render pass
// NOTE: Color and depth attachments are automatically transitioned to be bound as textures
postprocessing_pipeline->draw(command_buffer, render_target);
pipeline->draw(command_buffer, render_target);

if (has_gui())
{
Expand Down
7 changes: 7 additions & 0 deletions samples/performance/msaa/msaa.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ class MSAASample : public vkb::VulkanSample<vkb::BindingType::C>
*/
std::unique_ptr<vkb::PostProcessingPipeline> postprocessing_pipeline{};

/**
* @brief Postprocessing pipeline using multi-sampled depth
* Read in the output color and depth attachments from the
* scene subpass and use them to apply a screen-based effect
*/
std::unique_ptr<vkb::PostProcessingPipeline> ms_depth_postprocessing_pipeline{};

/**
* @brief Update MSAA options and accordingly set the load/store
* attachment operations for the renderpasses
Expand Down
14 changes: 1 addition & 13 deletions shaders/postprocessing/outline.frag
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#version 450
/* Copyright (c) 2020, Arm Limited and Contributors
/* Copyright (c) 2020-2024, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -18,11 +18,7 @@

precision highp float;

#ifdef MS_DEPTH
layout(set = 0, binding = 1) uniform sampler2DMS ms_depth_sampler;
#else
layout(set = 0, binding = 1) uniform sampler2D depth_sampler;
#endif
layout(set = 0, binding = 2) uniform sampler2D color_sampler;

layout(location = 0) in vec2 in_uv;
Expand All @@ -42,11 +38,7 @@ float linearizeDepth(float depth, float near, float far)
float getDepth(ivec2 offset)
{
float depth;
#ifdef MS_DEPTH
depth = texelFetch(ms_depth_sampler, ivec2(gl_FragCoord.xy) + offset, 0).r;
#else
depth = texelFetch(depth_sampler, ivec2(gl_FragCoord.xy) + offset, 0).r;
#endif
return linearizeDepth(depth, postprocessing_uniform.near_far.x, postprocessing_uniform.near_far.y);
}

Expand All @@ -62,9 +54,5 @@ void main(void)
outline += depth - getDepth(ivec2(thickness, 0));
outline += depth - getDepth(ivec2(0, -thickness));

#ifdef OUTLINE_ONLY
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This define is never set anywhere so i removed it

o_color.rgb = vec3(outline);
#else
o_color.rgb = mix(color.rgb, outline_color, clamp(outline, 0.0, 1.0));
#endif
}
58 changes: 58 additions & 0 deletions shaders/postprocessing/outline_ms_depth.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#version 450
/* Copyright (c) 2020-2024, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

precision highp float;

layout(set = 0, binding = 1) uniform sampler2DMS ms_depth_sampler;
layout(set = 0, binding = 2) uniform sampler2D color_sampler;

layout(location = 0) in vec2 in_uv;

layout(location = 0) out vec4 o_color;

layout(set = 0, binding = 0) uniform PostprocessingUniform
{
vec2 near_far;
} postprocessing_uniform;

float linearizeDepth(float depth, float near, float far)
{
return near * far / (far + depth * (near - far));
}

float getDepth(ivec2 offset)
{
float depth;
depth = texelFetch(ms_depth_sampler, ivec2(gl_FragCoord.xy) + offset, 0).r;
return linearizeDepth(depth, postprocessing_uniform.near_far.x, postprocessing_uniform.near_far.y);
}

void main(void)
{
vec4 color = texture(color_sampler, in_uv);
float depth = getDepth(ivec2(0, 0));

vec3 outline_color = vec3(0.0, 0.0, 0.0);
int thickness = 2;
float outline = depth - getDepth(ivec2(-thickness, 0));
outline += depth - getDepth(ivec2(0, thickness));
outline += depth - getDepth(ivec2(thickness, 0));
outline += depth - getDepth(ivec2(0, -thickness));

o_color.rgb = mix(color.rgb, outline_color, clamp(outline, 0.0, 1.0));
}