Skip to content

Commit

Permalink
Fix SSAO broken MSAA support
Browse files Browse the repository at this point in the history
Fixes #476

Depth Downsampling needed a special shader to handle MSAA.

SSAO_HS_ps shader was needing a special shader to handle MSAA due to
gBuf_normals being MSAA. Rather than adding a special variant via ifdef,
just disable explicit_resolve since the explicit MSAA surface is not
needed in the sample.

Reworked SSAO compositor to be applied in two stages, so that MSAA can
be handled properly
  • Loading branch information
darksylinc committed Nov 10, 2024
1 parent b262400 commit 93c3add
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 19 deletions.
5 changes: 4 additions & 1 deletion Samples/2.0/Tutorials/Tutorial_SSAO/Tutorial_SSAO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ namespace Demo
{
Ogre::CompositorManager2 *compositorManager = mRoot->getCompositorManager2();
return compositorManager->addWorkspace( mSceneManager, mRenderWindow->getTexture(), mCamera,
"SSAOWorkspace", true );
mRenderWindow->getSampleDescription().isMultisample()
? "SSAOWorkspaceMSAA"
: "SSAOWorkspace",
true );
}

void setupResources() override
Expand Down
68 changes: 68 additions & 0 deletions Samples/Media/2.0/scripts/materials/Common/DepthUtils.material
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,71 @@ material Ogre/Depth/DownscaleMax
}
}
}


fragment_program Ogre/Depth/DownscaleMax_Subsample0_ps_GLSL glsl
{
source DepthDownscaleMax_Subsample0_ps.glsl
default_params
{
param_named depthTexture int 0
}
}

fragment_program Ogre/Depth/DownscaleMax_Subsample0_ps_VK glslvk
{
source DepthDownscaleMax_Subsample0_ps.glsl
}

fragment_program Ogre/Depth/DownscaleMax_Subsample0_ps_HLSL hlsl
{
source DepthDownscaleMax_Subsample0_ps.hlsl
entry_point main
target ps_5_0 ps_4_0 ps_4_0_level_9_1 ps_4_0_level_9_3
}

fragment_program Ogre/Depth/DownscaleMax_Subsample0_ps_Metal metal
{
source DepthDownscaleMax_Subsample0_ps.metal
shader_reflection_pair_hint Ogre/Compositor/Quad_vs
}

fragment_program Ogre/Depth/DownscaleMax_Subsample0_ps unified
{
delegate Ogre/Depth/DownscaleMax_Subsample0_ps_GLSL
delegate Ogre/Depth/DownscaleMax_Subsample0_ps_VK
delegate Ogre/Depth/DownscaleMax_Subsample0_ps_HLSL
delegate Ogre/Depth/DownscaleMax_Subsample0_ps_Metal
}

// Downscales resolution of input depth texture by half (w/2 x h/2)
// using a max filter (max depth of all 4 neighbours), MSAA version that reads only subsample 0.
material Ogre/Depth/DownscaleMax_Subsample0
{
technique
{
pass
{
depth_check on
depth_write on

depth_func always_pass

cull_hardware none

vertex_program_ref Ogre/Compositor/Quad_vs
{
}

fragment_program_ref Ogre/Depth/DownscaleMax_Subsample0_ps
{
}

texture_unit depthTexture
{
filtering none
tex_address_mode clamp
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#version ogre_glsl_ver_330

vulkan_layout( ogre_t0 ) uniform texture2DMS depthTexture;

in vec4 gl_FragCoord;
//out float gl_FragDepth;

void main()
{
float fDepth0 = texelFetch( depthTexture, ivec2(gl_FragCoord.xy * 2.0), 0 ).x;
float fDepth1 = texelFetch( depthTexture, ivec2(gl_FragCoord.xy * 2.0) + ivec2( 0, 1 ), 0 ).x;
float fDepth2 = texelFetch( depthTexture, ivec2(gl_FragCoord.xy * 2.0) + ivec2( 1, 0 ), 0 ).x;
float fDepth3 = texelFetch( depthTexture, ivec2(gl_FragCoord.xy * 2.0) + ivec2( 1, 1 ), 0 ).x;

//gl_FragDepth = texelFetch( depthTexture, ivec2(gl_FragCoord.xy * 2.0), 0 ).x;
gl_FragDepth = max( max( fDepth0, fDepth1 ), max( fDepth2, fDepth3 ) );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
struct PS_INPUT
{
float2 uv0 : TEXCOORD0;
};

Texture2DMS<float> depthTexture : register(t0);

float main
(
PS_INPUT inPs,
float4 gl_FragCoord : SV_Position
) : SV_Depth
{
float fDepth0 = depthTexture.Load( int2(gl_FragCoord.xy * 2.0), 0 ).x;
float fDepth1 = depthTexture.Load( int2(gl_FragCoord.xy * 2.0) + int2( 0, 1 ), 0 ).x;
float fDepth2 = depthTexture.Load( int2(gl_FragCoord.xy * 2.0) + int2( 1, 0 ), 0 ).x;
float fDepth3 = depthTexture.Load( int2(gl_FragCoord.xy * 2.0) + int2( 1, 1 ), 0 ).x;

//return depthTexture.Load( int2(gl_FragCoord.xy * 2.0), 0 ) ).x;
return max( max( fDepth0, fDepth1 ), max( fDepth2, fDepth3 ) );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <metal_stdlib>
using namespace metal;

struct PS_INPUT
{
float2 uv0;
};

struct PS_OUTPUT
{
float depth [[depth(any)]];
};

fragment PS_OUTPUT main_metal
(
PS_INPUT inPs [[stage_in]],

depth2d_ms<float, access::read> depthTexture [[texture(0)]],

float4 gl_FragCoord [[position]]
)
{
uint2 iFragCoord = (uint2)(gl_FragCoord.xy * 2.0);
float fDepth0 = depthTexture.read( iFragCoord, 0 );
float fDepth1 = depthTexture.read( iFragCoord + uint2( 0, 1 ), 0 );
float fDepth2 = depthTexture.read( iFragCoord + uint2( 1, 0 ), 0 );
float fDepth3 = depthTexture.read( iFragCoord + uint2( 1, 1 ), 0 );

PS_OUTPUT outPs;
//outPs.depth =depthTexture.read( uint2(gl_FragCoord.xy * 2.0) ).x;
outPs.depth = max( max( fDepth0, fDepth1 ), max( fDepth2, fDepth3 ) );
return outPs;
}
104 changes: 86 additions & 18 deletions Samples/Media/2.0/scripts/materials/Tutorial_SSAO/SSAO_HS.compositor
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
compositor_node SSAO_RenderNode
compositor_node SSAO_MainRender
{
in 0 rt_renderwindow

texture RT0 target_width target_height PFG_RGBA8_UNORM_SRGB msaa_auto
texture gBufferNormals target_width target_height PFG_R10G10B10A2_UNORM msaa_auto explicit_resolve

texture depthTexture target_width target_height PFG_D32_FLOAT msaa_auto
texture depthTextureCopy target_width_scaled 0.5 target_height_scaled 0.5 PFG_D32_FLOAT
texture gBufferNormals target_width target_height PFG_R10G10B10A2_UNORM msaa_auto

texture ssaoTexture target_width_scaled 0.5 target_height_scaled 0.5 PFG_R16_FLOAT depth_pool 0

texture blurTextureHorizontal target_width target_height PFG_R16_FLOAT depth_pool 0
texture blurTextureVertical target_width target_height PFG_R16_FLOAT depth_pool 0
texture depthTexture target_width target_height PFG_D32_FLOAT msaa_auto

rtv RT0
{
Expand All @@ -35,17 +27,63 @@ compositor_node SSAO_RenderNode
gen_normals_gbuffer true
}
}


out 0 RT0
out 1 gBufferNormals
out 2 depthTexture
}

compositor_node SSAO_DepthDownsampler
{
in 0 depthTexture

texture depthTextureCopy target_width_scaled 0.5 target_height_scaled 0.5 PFG_D32_FLOAT

target depthTextureCopy
{
pass render_quad
{
load { all dont_care }
material Ogre/Depth/DownscaleMax
input 0 depthTexture
input 0 depthTexture
}
}

out 0 depthTextureCopy
}

compositor_node SSAO_DepthDownsamplerMSAA
{
in 0 depthTexture

texture depthTextureCopy target_width_scaled 0.5 target_height_scaled 0.5 PFG_D32_FLOAT

target depthTextureCopy
{
pass render_quad
{
load { all dont_care }
material Ogre/Depth/DownscaleMax_Subsample0
input 0 depthTexture
}
}

out 0 depthTextureCopy
}

compositor_node SSAO_GenerationSSAO_and_Composite
{
in 0 RT0
in 1 gBufferNormals
in 2 depthTextureCopy

in 3 rt_renderwindow

texture ssaoTexture target_width_scaled 0.5 target_height_scaled 0.5 PFG_R16_FLOAT depth_pool 0

texture blurTextureHorizontal target_width target_height PFG_R16_FLOAT depth_pool 0
texture blurTextureVertical target_width target_height PFG_R16_FLOAT depth_pool 0

target ssaoTexture
{
pass render_quad
Expand All @@ -56,13 +94,13 @@ compositor_node SSAO_RenderNode
clear_colour 1 1 1 1
}
material SSAO/HS
input 0 depthTextureCopy
input 0 depthTextureCopy
input 1 gBufferNormals

quad_normals camera_far_corners_view_space
}
}

target blurTextureHorizontal
{
pass render_quad
Expand All @@ -73,7 +111,7 @@ compositor_node SSAO_RenderNode
input 1 depthTextureCopy
}
}

target blurTextureVertical
{
pass render_quad
Expand All @@ -94,7 +132,7 @@ compositor_node SSAO_RenderNode
input 0 blurTextureVertical
input 1 RT0
}

pass render_scene
{
lod_update_list off
Expand All @@ -109,5 +147,35 @@ compositor_node SSAO_RenderNode

workspace SSAOWorkspace
{
connect_output SSAO_RenderNode 0
/**
SSAO_MainRender SSAO_GenerationSSAO_and_Composite
0 ---------------------------------> 0
1 ---------------------------------> 1
SSAO_DepthDownsampler
2 ----> 0 -----------------------> 2

connect_output ------------------------> 3
*/
connect SSAO_MainRender 2 SSAO_DepthDownsampler 0
connect SSAO_MainRender 0 1 SSAO_GenerationSSAO_and_Composite 0 1
connect SSAO_DepthDownsampler 0 SSAO_GenerationSSAO_and_Composite 2
connect_output SSAO_GenerationSSAO_and_Composite 3
}


workspace SSAOWorkspaceMSAA
{
/**
SSAO_MainRender SSAO_GenerationSSAO_and_Composite
0 ---------------------------------> 0
1 ---------------------------------> 1
SSAO_DepthDownsamplerMSAA
2 ----> 0 -----------------------> 2

connect_output ------------------------> 3
*/
connect SSAO_MainRender 2 SSAO_DepthDownsamplerMSAA 0
connect SSAO_MainRender 0 1 SSAO_GenerationSSAO_and_Composite 0 1
connect SSAO_DepthDownsamplerMSAA 0 SSAO_GenerationSSAO_and_Composite 2
connect_output SSAO_GenerationSSAO_and_Composite 3
}

0 comments on commit 93c3add

Please sign in to comment.