Skip to content
This repository has been archived by the owner on Feb 11, 2024. It is now read-only.

Commit

Permalink
SSAO optimisations and depth prepass fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
GameWin221 committed Jul 20, 2023
1 parent 7cbae51 commit eb1c93e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 44 deletions.
Binary file modified Shaders/Depth.spv
Binary file not shown.
3 changes: 2 additions & 1 deletion Shaders/Depth.vert
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ layout(push_constant) uniform ModelMatrixID {

void main()
{
gl_Position = camera.projView * modelMatrix[modelMatrixID] * vec4(vPosition, 1.0);
vec4 vWorldSpace = modelMatrix[modelMatrixID] * vec4(vPosition, 1.0);
gl_Position = camera.projView * vWorldSpace;
}
13 changes: 8 additions & 5 deletions Shaders/SSAO.frag
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ float LinearDepth(float d)
}
void main()
{
vec2 noiseScale = vec2(params.screenWidth/params.noiseScale, params.screenHeight/params.noiseScale);
vec2 noiseScale = vec2(params.screenWidth, params.screenHeight) / params.noiseScale;

vec3 viewPos = GetViewPosition(fTexcoord);
vec3 viewNormal = normalize(cross(dFdx(viewPos.xyz), dFdy(viewPos.xyz)));
Expand All @@ -92,12 +92,15 @@ void main()
vec3 samplePos = TBN * kernels[i];
samplePos = viewPos + samplePos * params.radius;

vec4 offset = vec4(samplePos, 1.0);
offset = camera.proj * offset;
offset.xy /= offset.w;
// Faster and shorter form of:
//vec4 offset = camera.proj * vec4(samplePos, 1.0);
//offset.xyz /= offset.w;

float sampleDepth = GetViewZ(offset.xy * 0.5 + 0.5);
vec2 offsetXY = vec2(camera.proj[0][0] * samplePos.x, camera.proj[1][1] * samplePos.y);
offsetXY /= camera.proj[2][3] * samplePos.z;

float sampleDepth = GetViewZ(offsetXY * 0.5 + 0.5);

float rangeCheck = smoothstep(0.0, 1.0, params.radius / abs(viewPos.z - sampleDepth));
occlusion += (sampleDepth >= samplePos.z + params.bias ? 1.0 : 0.0) * rangeCheck * params.multiplier;
}
Expand Down
Binary file modified Shaders/SSAO.spv
Binary file not shown.
15 changes: 4 additions & 11 deletions Source/Core/Eruption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ void Eruption::Update()
if (m_Input->IsKey(en::Key::LShift) && m_Input->IsKey(en::Key::R, en::InputState::Pressed))
m_Renderer->ReloadBackend();

else if (m_Input->IsKey(en::Key::Y, en::InputState::Pressed))
{
m_Renderer->SetVSyncEnabled(false);
//m_Renderer->m_PostProcessParams.antialiasingMode = en::Renderer::AntialiasingMode::None;
}

double deltaTime = m_Renderer->GetFrameTime();

static float targetYaw = m_Camera->m_Yaw;
Expand Down Expand Up @@ -103,9 +97,9 @@ void Eruption::Render()
m_Renderer->Render();
}


void Eruption::CreateExampleScene()
{
/*
m_AssetManager->LoadMesh("SkullModel", "Models/Skull/Skull.gltf");
m_AssetManager->LoadTexture("SkullAlbedo", "Models/Skull/SkullAlbedo.jpg");
Expand All @@ -117,9 +111,9 @@ void Eruption::CreateExampleScene()
m_AssetManager->GetMesh("SkullModel")->m_SubMeshes[0].SetMaterial(m_AssetManager->GetMaterial("SkullMaterial"));
m_AssetManager->LoadMesh("Sponza", "Models/Sponza/Sponza.gltf");

*/
m_ExampleScene = en::MakeHandle<en::Scene>();

/*
auto m_Sponza = m_ExampleScene->CreateSceneObject("Sponza", m_AssetManager->GetMesh("Sponza"));
m_Sponza->SetScale(glm::vec3(1.2f));
Expand All @@ -130,7 +124,7 @@ void Eruption::CreateExampleScene()
auto m_Skull = m_ExampleScene->CreateSceneObject("SkullModel", m_AssetManager->GetMesh("SkullModel"));
m_Skull->SetPosition(glm::vec3(0, 0.5f, -0.3));
m_Skull->SetRotation(glm::vec3(45.0f, 90.0f, 0.0f));

*/



Expand Down Expand Up @@ -160,7 +154,6 @@ void Eruption::CreateExampleScene()
m_Renderer->BindScene(m_ExampleScene);
}


void Eruption::Run()
{
Init();
Expand Down
2 changes: 0 additions & 2 deletions Source/Editor/UIPanels/SettingsPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ namespace en
ImGui::DragFloat("SSAO Bias", &ssao.bias, 0.05f, 0.0f, 1.0f), "%.2f", ImGuiSliderFlags_AlwaysClamp;
ImGui::DragFloat("SSAO Radius", &ssao.radius, 0.1f, 0.0f, 5.0f, "%.1f", ImGuiSliderFlags_AlwaysClamp);
ImGui::DragFloat("SSAO Multiplier", &ssao.multiplier, 0.1f, 0.0f, 5.0f, "%.1f", ImGuiSliderFlags_AlwaysClamp);
ImGui::DragFloat("ssao_noise_scale", &ssao._noiseScale, 0.1f, 1.0f, 8.0f, "%.1f", ImGuiSliderFlags_AlwaysClamp);
ImGui::DragInt("ssao_samples", &s, 1.0f, 1.0f, 64.0f, "%.1f", ImGuiSliderFlags_AlwaysClamp);

break;

Expand Down
14 changes: 7 additions & 7 deletions Source/Renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,16 @@ namespace en
switch (m_Settings.ambientOcclusionQuality)
{
case QualityLevel::Low:
m_Settings.ambientOcclusion._samples = 16U;
m_Settings.ambientOcclusion._noiseScale = 4.0f;
m_Settings.ambientOcclusion._samples = 8U;
m_Settings.ambientOcclusion._noiseScale = 2.0f;
break;
case QualityLevel::Medium:
m_Settings.ambientOcclusion._samples = 32U;
m_Settings.ambientOcclusion._noiseScale = 4.0f;
m_Settings.ambientOcclusion._samples = 16U;
m_Settings.ambientOcclusion._noiseScale = 2.0f;
break;
case QualityLevel::High:
m_Settings.ambientOcclusion._samples = 32U;
m_Settings.ambientOcclusion._noiseScale = 2.0f;
m_Settings.ambientOcclusion._samples = 16U;
m_Settings.ambientOcclusion._noiseScale = 1.0f;
break;
case QualityLevel::Ultra:
m_Settings.ambientOcclusion._samples = 32U;
Expand Down Expand Up @@ -1239,7 +1239,7 @@ namespace en
.enableDepthWrite = !m_Settings.depthPrePass,
.blendEnable = false,

.compareOp = m_Settings.depthPrePass ? VK_COMPARE_OP_LESS_OR_EQUAL : VK_COMPARE_OP_LESS,
.compareOp = m_Settings.depthPrePass ? VK_COMPARE_OP_EQUAL : VK_COMPARE_OP_LESS,
.polygonMode = VK_POLYGON_MODE_FILL,
};

Expand Down
8 changes: 4 additions & 4 deletions Source/Renderer/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ namespace en
float screenWidth = 1920.0f;
float screenHeight = 1080.0f;

float radius = 0.85f;
float radius = 0.75f;
float bias = 0.025f;
float multiplier = 1.2f;
float multiplier = 1.0f;

uint32_t _samples = 64U;
float _noiseScale = 4.0f;
uint32_t _samples = 16U;
float _noiseScale = 2.0f;
};

void SetVSyncEnabled(const bool enabled);
Expand Down
4 changes: 2 additions & 2 deletions Source/Renderer/Swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace en
.imageColorSpace = surfaceFormat.colorSpace,
.imageExtent = extent,
.imageArrayLayers = 1U,
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
.preTransform = swapChainSupport.capabilities.currentTransform,
.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
.presentMode = presentMode,
Expand Down Expand Up @@ -80,7 +80,7 @@ namespace en
{
UseContext();

for (auto& imageView : m_ImageViews)
for (const auto& imageView : m_ImageViews)
vkDestroyImageView(ctx.m_LogicalDevice, imageView, nullptr);

vkDestroySwapchainKHR(ctx.m_LogicalDevice, m_Swapchain, nullptr);
Expand Down
24 changes: 12 additions & 12 deletions imgui.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Window][DockSpace Demo]
Pos=0,33
Size=2560,1336
Size=1920,1047
Collapsed=0

[Window][Debug##Default]
Expand All @@ -9,14 +9,14 @@ Size=400,400
Collapsed=0

[Window][Asset Manager]
Pos=273,732
Size=1400,348
Pos=273,1021
Size=2287,348
Collapsed=0
DockId=0x00000008,0

[Window][Camera Properties]
Pos=0,909
Size=271,460
Pos=0,720
Size=271,360
Collapsed=0
DockId=0x00000004,0

Expand All @@ -26,19 +26,19 @@ Size=405,428
Collapsed=0

[Window][Debug Menu]
Pos=1683,398
Pos=1387,230
Size=322,194
Collapsed=0

[Window][Scene Hierarchy]
Pos=0,33
Size=271,874
Size=271,685
Collapsed=0
DockId=0x00000003,0

[Window][Inspector]
Pos=2315,33
Size=245,1336
Pos=1675,33
Size=245,1047
Collapsed=0
DockId=0x00000006,0

Expand All @@ -58,12 +58,12 @@ Size=409,172
Collapsed=0

[Window][Settings]
Pos=1621,648
Size=500,449
Pos=1441,546
Size=500,504
Collapsed=0

[Docking][Data]
DockSpace ID=0x1315676B Window=0x4647B76E Pos=0,33 Size=2560,1336 Split=X
DockSpace ID=0x1315676B Window=0x4647B76E Pos=0,33 Size=1920,1047 Split=X
DockNode ID=0x00000005 Parent=0x1315676B SizeRef=1673,1047 Split=X
DockNode ID=0x00000001 Parent=0x00000005 SizeRef=271,1047 Split=Y Selected=0x2E9237F7
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=499,496 Selected=0x2E9237F7
Expand Down

0 comments on commit eb1c93e

Please sign in to comment.