Skip to content

Commit d6efc93

Browse files
committed
WIP: AO plugged with other input
1 parent 2bd899b commit d6efc93

2 files changed

Lines changed: 121 additions & 8 deletions

File tree

Core/Source/Lux/Renderer/SceneRenderer.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,9 @@ namespace Lux {
12061206
aoRenderPassSpec.Pipeline = Pipeline::Create(aoPipelineSpec);
12071207
m_AOCompositePass = RenderPass::Create(aoRenderPassSpec);
12081208
m_AOCompositePass->SetInput("u_GTAOTex", m_GTAOFinalImage);
1209+
m_AOCompositePass->SetInput("u_Depth", m_PreDepthPass->GetDepthOutput());
1210+
m_AOCompositePass->SetInput("u_Normal", m_GeometryPass->GetOutput(1));
1211+
m_AOCompositePass->SetInput("Camera", m_UBSCamera);
12091212
m_AOCompositePass->SetInput("r_DefaultSampler", Renderer::GetDefaultSampler());
12101213
m_AOCompositePass->SetInput("r_PointSampler", Renderer::GetPointSampler());
12111214
m_AOCompositePass->SetInput("r_LinearSampler", Renderer::GetClampSampler());
@@ -1229,6 +1232,9 @@ namespace Lux {
12291232
aoDebugRenderPassSpec.Pipeline = Pipeline::Create(aoDebugPipelineSpec);
12301233
m_AODebugPass = RenderPass::Create(aoDebugRenderPassSpec);
12311234
m_AODebugPass->SetInput("u_GTAOTex", m_GTAOFinalImage);
1235+
m_AODebugPass->SetInput("u_Depth", m_PreDepthPass->GetDepthOutput());
1236+
m_AODebugPass->SetInput("u_Normal", m_GeometryPass->GetOutput(1));
1237+
m_AODebugPass->SetInput("Camera", m_UBSCamera);
12321238
m_AODebugPass->SetInput("r_DefaultSampler", Renderer::GetDefaultSampler());
12331239
m_AODebugPass->SetInput("r_PointSampler", Renderer::GetPointSampler());
12341240
m_AODebugPass->SetInput("r_LinearSampler", Renderer::GetClampSampler());
@@ -1961,9 +1967,17 @@ namespace Lux {
19611967

19621968
m_GTAOFinalImage = (m_Options.GTAODenoisePasses % 2 != 0) ? m_GTAODenoiseImage : m_GTAOOutputImage;
19631969
if (m_AOCompositePass)
1970+
{
19641971
m_AOCompositePass->SetInput("u_GTAOTex", m_GTAOFinalImage);
1972+
m_AOCompositePass->SetInput("u_Depth", m_PreDepthPass->GetDepthOutput());
1973+
m_AOCompositePass->SetInput("u_Normal", m_GeometryPass->GetOutput(1));
1974+
}
19651975
if (m_AODebugPass)
1976+
{
19661977
m_AODebugPass->SetInput("u_GTAOTex", m_GTAOFinalImage);
1978+
m_AODebugPass->SetInput("u_Depth", m_PreDepthPass->GetDepthOutput());
1979+
m_AODebugPass->SetInput("u_Normal", m_GeometryPass->GetOutput(1));
1980+
}
19671981
if (m_SSRPass && m_SSRPass->IsInputValid("u_GTAOTex"))
19681982
m_SSRPass->SetInput("u_GTAOTex", m_GTAOFinalImage);
19691983
if (m_GTAOTemporalPass)
@@ -2634,9 +2648,11 @@ namespace Lux {
26342648

26352649
std::vector<RenderGraph::ResourceHandle> aoFinalOutputs = { gtaoOutput, gtaoDenoise, gtaoHistoryA, gtaoHistoryB };
26362650
std::vector<RenderGraph::ResourceHandle> aoCompositeReads = geometryOutputs;
2651+
appendResources(aoCompositeReads, preDepthOutputs);
26372652
appendResources(aoCompositeReads, aoFinalOutputs);
26382653
addPass("AO Composite", aoCompositeReads, addRenderPassResources("AO Composite", m_AOCompositePass));
2639-
addPass("AO Debug", aoFinalOutputs, addRenderPassResources("AO Debug", m_AODebugPass));
2654+
std::vector<RenderGraph::ResourceHandle> aoDebugReads = aoCompositeReads;
2655+
addPass("AO Debug", aoDebugReads, addRenderPassResources("AO Debug", m_AODebugPass));
26402656

26412657
std::vector<RenderGraph::ResourceHandle> preConvolutionOutputs;
26422658
preConvolutionOutputs.push_back(m_PreConvolutedTexture.Texture ? addTexture("Pre-Convoluted Scene", m_PreConvolutedTexture.Texture->GetImage()) : RenderGraph::InvalidResource);
@@ -4653,7 +4669,11 @@ namespace Lux {
46534669
{
46544670
m_GTAOFinalImage = m_GTAOOutputImage;
46554671
if (m_AOCompositePass)
4672+
{
46564673
m_AOCompositePass->SetInput("u_GTAOTex", m_GTAOFinalImage);
4674+
m_AOCompositePass->SetInput("u_Depth", m_PreDepthPass->GetDepthOutput());
4675+
m_AOCompositePass->SetInput("u_Normal", m_GeometryPass->GetOutput(1));
4676+
}
46574677
if (m_SSRPass && m_SSRPass->IsInputValid("u_GTAOTex"))
46584678
m_SSRPass->SetInput("u_GTAOTex", m_GTAOFinalImage);
46594679
return;
@@ -4677,7 +4697,11 @@ namespace Lux {
46774697

46784698
m_GTAOFinalImage = (denoisePasses % 2u) != 0u ? m_GTAODenoiseImage : m_GTAOOutputImage;
46794699
if (m_AOCompositePass)
4700+
{
46804701
m_AOCompositePass->SetInput("u_GTAOTex", m_GTAOFinalImage);
4702+
m_AOCompositePass->SetInput("u_Depth", m_PreDepthPass->GetDepthOutput());
4703+
m_AOCompositePass->SetInput("u_Normal", m_GeometryPass->GetOutput(1));
4704+
}
46814705
if (m_SSRPass && m_SSRPass->IsInputValid("u_GTAOTex"))
46824706
m_SSRPass->SetInput("u_GTAOTex", m_GTAOFinalImage);
46834707

@@ -4719,7 +4743,11 @@ namespace Lux {
47194743
m_GTAOHistoryIndex = writeIndex;
47204744
m_GTAOFinalImage = historyOutput;
47214745
if (m_AOCompositePass)
4746+
{
47224747
m_AOCompositePass->SetInput("u_GTAOTex", m_GTAOFinalImage);
4748+
m_AOCompositePass->SetInput("u_Depth", m_PreDepthPass->GetDepthOutput());
4749+
m_AOCompositePass->SetInput("u_Normal", m_GeometryPass->GetOutput(1));
4750+
}
47234751
if (m_SSRPass && m_SSRPass->IsInputValid("u_GTAOTex"))
47244752
m_SSRPass->SetInput("u_GTAOTex", m_GTAOFinalImage);
47254753
}
@@ -4744,6 +4772,8 @@ namespace Lux {
47444772
return;
47454773

47464774
m_AODebugPass->SetInput("u_GTAOTex", m_GTAOFinalImage);
4775+
m_AODebugPass->SetInput("u_Depth", m_PreDepthPass->GetDepthOutput());
4776+
m_AODebugPass->SetInput("u_Normal", m_GeometryPass->GetOutput(1));
47474777

47484778
BeginProfiledGPU("AODebug");
47494779
Renderer::BeginRenderPass(m_CommandBuffer, m_AODebugPass);

Editor/Resources/Shaders/PostProcessing/AO-Composite.glsl

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void main()
1414
#version 450 core
1515
#pragma stage : frag
1616

17+
#include <Buffers.glslh>
1718
#include <GTAO.slh>
1819
#include <Samplers.glslh>
1920

@@ -22,25 +23,107 @@ void main()
2223

2324
#if ENABLED_GTAO
2425
layout(set = 1, binding = 0) uniform utexture2D u_GTAOTex;
26+
layout(set = 1, binding = 1) uniform texture2D u_Depth;
27+
layout(set = 1, binding = 2) uniform texture2D u_Normal;
2528
#endif
2629

2730
#if ENABLED_HBAO
28-
layout(binding = 1) uniform texture2D u_HBAOTex;
31+
layout(set = 1, binding = 3) uniform texture2D u_HBAOTex;
2932
#endif
3033

3134
layout(location = 0) out vec4 o_Occlusion;
3235
layout(location = 0) in vec2 vs_TexCoord;
3336

34-
void main()
35-
{
36-
float occlusion = 1.0f;
3737
#if ENABLED_GTAO
38+
float LinearizeDepth(float screenDepth)
39+
{
40+
float depthLinearizeMul = u_Camera.DepthUnpackConsts.x;
41+
float depthLinearizeAdd = u_Camera.DepthUnpackConsts.y;
42+
return depthLinearizeMul / (depthLinearizeAdd - screenDepth);
43+
}
44+
45+
float ReadDepth(vec2 uv)
46+
{
47+
return LinearizeDepth(texture(sampler2D(u_Depth, r_PointSampler), uv).r);
48+
}
49+
50+
vec3 ReadNormal(vec2 uv)
51+
{
52+
vec3 normal = texture(sampler2D(u_Normal, r_PointSampler), uv).xyz;
53+
float normalLength = length(normal);
54+
if (normalLength < 0.0001)
55+
return vec3(0.0, 0.0, 1.0);
56+
57+
return normal / normalLength;
58+
}
59+
60+
float DecodeGTAO(uint packedValue)
61+
{
3862
#if __HZ_GTAO_COMPUTE_BENT_NORMALS
39-
float ao = (texture(usampler2D(u_GTAOTex, r_LinearSampler), vs_TexCoord).x >> 24) / 255.f;
63+
return float(packedValue >> 24u) / 255.0;
4064
#else
41-
float ao = texture(usampler2D(u_GTAOTex, r_LinearSampler), vs_TexCoord).x / 255.f;
65+
return float(packedValue) / 255.0;
4266
#endif
43-
occlusion = min(ao * XE_GTAO_OCCLUSION_TERM_SCALE, 1.0f);
67+
}
68+
69+
float FetchGTAO(ivec2 texel)
70+
{
71+
ivec2 aoSize = textureSize(usampler2D(u_GTAOTex, r_PointSampler), 0);
72+
texel = clamp(texel, ivec2(0), max(aoSize - ivec2(1), ivec2(0)));
73+
return DecodeGTAO(texelFetch(usampler2D(u_GTAOTex, r_PointSampler), texel, 0).x);
74+
}
75+
76+
float UpscaleGTAO(vec2 uv)
77+
{
78+
ivec2 aoSize = textureSize(usampler2D(u_GTAOTex, r_PointSampler), 0);
79+
ivec2 depthSize = textureSize(sampler2D(u_Depth, r_PointSampler), 0);
80+
if (aoSize.x >= depthSize.x && aoSize.y >= depthSize.y)
81+
return FetchGTAO(ivec2(clamp(uv * vec2(aoSize), vec2(0.0), vec2(aoSize - ivec2(1)))));
82+
83+
vec2 aoTexel = uv * vec2(aoSize) - vec2(0.5);
84+
ivec2 baseTexel = ivec2(floor(aoTexel));
85+
float centerDepth = ReadDepth(uv);
86+
vec3 centerNormal = ReadNormal(uv);
87+
88+
float weightedAO = 0.0;
89+
float totalWeight = 0.0;
90+
91+
for (int y = -1; y <= 2; y++)
92+
{
93+
for (int x = -1; x <= 2; x++)
94+
{
95+
ivec2 sampleTexel = baseTexel + ivec2(x, y);
96+
ivec2 clampedTexel = clamp(sampleTexel, ivec2(0), max(aoSize - ivec2(1), ivec2(0)));
97+
vec2 sampleUV = (vec2(clampedTexel) + vec2(0.5)) / vec2(aoSize);
98+
99+
float sampleDepth = ReadDepth(sampleUV);
100+
vec3 sampleNormal = ReadNormal(sampleUV);
101+
vec2 spatialOffset = (vec2(sampleTexel) + vec2(0.5)) - aoTexel;
102+
103+
float relativeDepthDelta = abs(sampleDepth - centerDepth) / max(abs(centerDepth), 1.0);
104+
float depthWeight = exp(-relativeDepthDelta / 0.035);
105+
float normalWeight = pow(clamp(dot(centerNormal, sampleNormal), 0.0, 1.0), 24.0);
106+
float spatialWeight = exp(-dot(spatialOffset, spatialOffset) * 0.55);
107+
float weight = depthWeight * normalWeight * spatialWeight;
108+
109+
weightedAO += FetchGTAO(clampedTexel) * weight;
110+
totalWeight += weight;
111+
}
112+
}
113+
114+
if (totalWeight <= 0.0001)
115+
return FetchGTAO(ivec2(round(aoTexel)));
116+
117+
return weightedAO / totalWeight;
118+
}
119+
#endif
120+
121+
void main()
122+
{
123+
float occlusion = 1.0f;
124+
#if ENABLED_GTAO
125+
float ao = UpscaleGTAO(vs_TexCoord);
126+
occlusion = min(ao * XE_GTAO_OCCLUSION_TERM_SCALE, 1.0f);
44127
#endif
45128
#if ENABLED_HBAO
46129
occlusion *= SampleLinear(u_HBAOTex, vs_TexCoord).x;

0 commit comments

Comments
 (0)