Skip to content

Commit b85ff0e

Browse files
committed
-Dev: SSAO pre-pass added (raytraced and classic). Still no blurr
1 parent 2c2c644 commit b85ff0e

29 files changed

+760
-133
lines changed

examples/renderer-app/application.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void Application::init(Systems::RendererSettings settings) {
1818
std::placeholders::_3,
1919
std::placeholders::_4));
2020

21-
m_renderer = new Systems::ForwardRenderer(m_window, ShadowResolution::MEDIUM, settings);
21+
m_renderer = new Systems::DeferredRenderer(m_window, ShadowResolution::MEDIUM, settings);
2222

2323
setup();
2424

include/engine/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ typedef enum ColorFormatTypeFlagBits
289289
SRGB_32F = VK_FORMAT_R32G32B32_SFLOAT,
290290
SRGBA_16F = VK_FORMAT_R16G16B16A16_SFLOAT, // HDR precission 16
291291
SRGBA_32F = VK_FORMAT_R32G32B32A32_SFLOAT, // HDR precission 32
292+
R_8U = VK_FORMAT_R8_UNORM,
293+
RG_8U = VK_FORMAT_R8G8_UNORM,
294+
RGB_8U = VK_FORMAT_R8G8B8_UNORM,
292295
RGBA_8U = VK_FORMAT_R8G8B8A8_UNORM,
293296
DEPTH_16F = VK_FORMAT_D16_UNORM,
294297
DEPTH_32F = VK_FORMAT_D32_SFLOAT

include/engine/core/passes/composition_pass.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ enum class OutputBuffer
2424
NORMAL = 2,
2525
POSITION = 3,
2626
MATERIAL = 4,
27-
EMISSIVE = 5,
28-
SSR = 6,
29-
SSAO = 7
27+
SSAO = 5,
28+
EMISSIVE = 6,
29+
SSR = 7,
3030
};
3131
struct SSRSettings {
3232
uint32_t maxSteps = 64;
@@ -53,6 +53,7 @@ class CompositionPass : public GraphicPass
5353

5454
struct Settings {
5555
OutputBuffer outputBuffer = OutputBuffer::LIGHTING;
56+
int enableAO = 1;
5657
SSRSettings ssr = {};
5758
};
5859
Settings m_settings = {};
@@ -83,6 +84,12 @@ class CompositionPass : public GraphicPass
8384
inline OutputBuffer get_output_buffer() const {
8485
return m_settings.outputBuffer;
8586
};
87+
inline bool enable_AO() const {
88+
return m_settings.enableAO;
89+
}
90+
inline void enable_AO(bool op) {
91+
m_settings.enableAO = op;
92+
}
8693

8794
void setup_attachments(std::vector<Graphics::Attachment>& attachments,
8895
std::vector<Graphics::SubPassDependency>& dependencies);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
This file is part of Vulkan-Engine, a simple to use Vulkan based 3D library
3+
4+
MIT License
5+
6+
Copyright (c) 2023 Antonio Espinosa Garcia
7+
8+
*/
9+
#ifndef PRECOMPOSITION_PASS_H
10+
#define PRECOMPOSITION_PASS_H
11+
#include <engine/core/passes/pass.h>
12+
#include <engine/core/resource_manager.h>
13+
#include <random>
14+
15+
VULKAN_ENGINE_NAMESPACE_BEGIN
16+
namespace Core {
17+
18+
typedef enum class AmbientOcclusionType
19+
{
20+
SSAO = 0,
21+
RTAO = 1, // Raytraced AO
22+
} AOType;
23+
struct SSAOSettings {
24+
float radius = 0.75;
25+
float bias = 0.0;
26+
uint32_t samples = 16;
27+
AOType type = AOType::RTAO;
28+
uint32_t enabled = 1;
29+
};
30+
/*
31+
Pre-composition pass called before the Composition (Lighting) pass in a deferred framework. This pass computes
32+
SSAO and other lighting effects, such as blurring raytraced shadows by bilinear filering them, in order to be used in
33+
future lighting passes.
34+
*/
35+
class PreCompositionPass : public GraphicPass
36+
{
37+
Mesh* m_vignette;
38+
/*Descriptors*/
39+
struct FrameDescriptors {
40+
Graphics::DescriptorSet globalDescritor;
41+
};
42+
std::vector<FrameDescriptors> m_descriptors;
43+
/*Resources*/
44+
const size_t MAX_KERNEL_MEMBERS = 64;
45+
Graphics::Buffer m_kernelBuffer;
46+
bool m_updateSamplesKernel = true;
47+
Graphics::Image m_blurredSSAO;
48+
/*Settings*/
49+
SSAOSettings m_AO = {};
50+
51+
void create_samples_kernel();
52+
53+
public:
54+
PreCompositionPass(Graphics::Device* ctx, VkExtent2D extent, uint32_t framebufferCount, Mesh* vignette)
55+
: BasePass(ctx, extent, framebufferCount, 1, false, "PRE-COMPOSITION")
56+
, m_vignette(vignette) {
57+
}
58+
59+
inline void set_SSAO_settings(SSAOSettings settings) {
60+
if (settings.samples != m_AO.samples)
61+
m_updateSamplesKernel = true;
62+
m_AO = settings;
63+
};
64+
inline SSAOSettings get_SSAO_settings() const {
65+
return m_AO;
66+
};
67+
68+
void setup_attachments(std::vector<Graphics::Attachment>& attachments,
69+
std::vector<Graphics::SubPassDependency>& dependencies);
70+
71+
void setup_uniforms(std::vector<Graphics::Frame>& frames);
72+
73+
void setup_shader_passes();
74+
75+
void render(Graphics::Frame& currentFrame, Scene* const scene, uint32_t presentImageIndex = 0);
76+
77+
void connect_to_previous_images(std::vector<Graphics::Image> images);
78+
79+
void update_uniforms(uint32_t frameIndex, Scene* const scene);
80+
81+
void cleanup();
82+
};
83+
} // namespace Core
84+
VULKAN_ENGINE_NAMESPACE_END
85+
86+
#endif

include/engine/systems/renderers/deferred.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <engine/core/passes/composition_pass.h>
66
#include <engine/core/passes/geometry_pass.h>
77
#include <engine/core/passes/postprocess_pass.h>
8+
#include <engine/core/passes/precomposition_pass.h>
89
#include <engine/core/passes/variance_shadow_pass.h>
910

1011
#include <engine/systems/renderers/renderer.h>
@@ -20,13 +21,13 @@ class DeferredRenderer : public BaseRenderer
2021
{
2122
enum RendererPasses
2223
{
23-
SHADOW_PASS = 0,
24-
GEOMETRY_PASS = 1,
25-
COMPOSITION_PASS = 2,
26-
// SSR_PASS = 3,
27-
BLOOM_PASS = 3,
28-
TONEMAPPIN_PASS = 4,
29-
FXAA_PASS = 5,
24+
SHADOW_PASS = 0,
25+
GEOMETRY_PASS = 1,
26+
PRECOMPOSITION_PASS = 2,
27+
COMPOSITION_PASS = 3,
28+
BLOOM_PASS = 4,
29+
TONEMAPPIN_PASS = 5,
30+
FXAA_PASS = 6,
3031
};
3132

3233
ShadowResolution m_shadowQuality = ShadowResolution::MEDIUM;
@@ -72,12 +73,24 @@ class DeferredRenderer : public BaseRenderer
7273
inline Core::SSRSettings get_SSR_settings() const {
7374
return static_cast<Core::CompositionPass*>(m_passes[COMPOSITION_PASS])->get_SSR_settings();
7475
};
76+
inline void set_SSAO_settings(Core::SSAOSettings settings) {
77+
static_cast<Core::CompositionPass*>(m_passes[COMPOSITION_PASS])->enable_AO(settings.enabled);
78+
static_cast<Core::PreCompositionPass*>(m_passes[PRECOMPOSITION_PASS])->set_SSAO_settings(settings);
79+
};
80+
inline Core::SSAOSettings get_SSAO_settings() const {
81+
return static_cast<Core::PreCompositionPass*>(m_passes[PRECOMPOSITION_PASS])->get_SSAO_settings();
82+
};
7583
inline void set_shading_output(Core::OutputBuffer output) {
7684
static_cast<Core::CompositionPass*>(m_passes[COMPOSITION_PASS])->set_output_buffer(output);
7785
}
7886
inline Core::OutputBuffer get_shading_output() const {
7987
return static_cast<Core::CompositionPass*>(m_passes[COMPOSITION_PASS])->get_output_buffer();
8088
}
89+
inline void set_clearcolor(Vec4 c) {
90+
BaseRenderer::set_clearcolor(c);
91+
m_passes[GEOMETRY_PASS]->set_attachment_clear_value(
92+
{m_settings.clearColor.r, m_settings.clearColor.g, m_settings.clearColor.b, m_settings.clearColor.a}, 2);
93+
}
8194

8295
protected:
8396
virtual void on_before_render(Core::Scene* const scene);

include/engine/systems/renderers/renderer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ class BaseRenderer
8585
inline void set_settings(RendererSettings settings) {
8686
m_settings = settings;
8787
}
88-
inline void set_clearcolor(Vec4 c) {
89-
m_settings.clearColor = c;
90-
}
9188
inline void set_antialiasing(MSAASamples msaa) {
9289
m_settings.samplesMSAA = msaa;
9390
}
@@ -111,6 +108,9 @@ class BaseRenderer
111108
if (m_initialized)
112109
m_updateFramebuffers = true;
113110
}
111+
virtual inline void set_clearcolor(Vec4 c) {
112+
m_settings.clearColor = c;
113+
}
114114

115115
#pragma endregion
116116
#pragma region Public Functions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#shader compute
2+
#version 460
3+
4+
layout(set = 0, binding = 7) uniform sampler2D InputImage;
5+
layout(set = 0, binding = 8) uniform image2D OutputImage;
6+
7+
8+
layout(local_size_x = 16, local_size_y = 16) in;
9+
10+
void main() {
11+
// Get the image coordinates of the current thread
12+
ivec2 pixelCoord = ivec2(gl_GlobalInvocationID.xy);
13+
14+
// // Read the R channel from the input image
15+
// float rValue = texture(uInputImage, pixelCoord).r;
16+
17+
// // Write the value into the G channel of the output image
18+
// vec4 color = texture(uOutputImage, pixelCoord); // Load existing data in output image
19+
// color.g = rValue; // Store R value into G channel
20+
21+
// Store the updated color back into the output image
22+
imageStore(OutputImage, pixelCoord, vec4(1.0));
23+
}

resources/shaders/deferred/composition.glsl

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ void main() {
2626
#include utils.glsl
2727
#include shadow_mapping.glsl
2828
#include fresnel.glsl
29-
#include ssao.glsl
3029
#include IBL.glsl
31-
#include reindhart.glsl
3230
#include BRDFs/schlick_smith_BRDF.glsl
3331
#include BRDFs/marschner_BSDF.glsl
32+
#include warp.glsl
3433
#include raytracing.glsl
3534
#include hashing.glsl
3635
#include SSR.glsl
3736

37+
3838
//INPUT
3939
layout(location = 0) in vec2 v_uv;
4040

@@ -53,13 +53,25 @@ layout(set = 1, binding = 1) uniform sampler2D normalBuffer;
5353
layout(set = 1, binding = 2) uniform sampler2D colorBuffer;
5454
layout(set = 1, binding = 3) uniform sampler2D materialBuffer;
5555
layout(set = 1, binding = 4) uniform sampler2D emissionBuffer;
56-
layout(set = 1, binding = 5) uniform sampler2D prevBuffer;
56+
layout(set = 1, binding = 5) uniform sampler2D preCompositionBuffer;
57+
58+
layout(set = 1, binding = 6) uniform sampler2D prevBuffer;
59+
5760
//SETTINGS
5861
layout(push_constant) uniform Settings {
5962
uint bufferOutput;
63+
uint enableAO;
6064
SSR ssr;
6165
} settings;
6266

67+
// DEBUGGING QUERIES
68+
#define LIGHTING_MODE settings.bufferOutput == 0
69+
#define ALBEDO_OUTPUT settings.bufferOutput == 1
70+
#define NORMALS_OUTPUT settings.bufferOutput == 2
71+
#define POSITION_OUTPUT settings.bufferOutput == 3
72+
#define MATERIAL_OUTPUT settings.bufferOutput == 4
73+
#define SSAO_OUTPUT settings.bufferOutput == 5
74+
6375
//SURFACE PROPERTIES
6476
vec3 g_pos;
6577
float g_depth;
@@ -71,6 +83,7 @@ vec3 g_emission;
7183
int g_isReflective;
7284
vec4 g_temp;
7385

86+
7487
void main()
7588
{
7689

@@ -89,10 +102,18 @@ void main()
89102
g_emission = emissionFresnelThreshold.rgb;
90103
g_isReflective = int(emissionFresnelThreshold.w);
91104
g_temp = vec4(0.0);
105+
///////////////////////////////////
106+
// PRE-COMPUTED DATA
107+
///////////////////////////////////
108+
vec2 preComputedData = texture(preCompositionBuffer,v_uv).rg;
109+
float SSAO = preComputedData.r;
110+
float rtShadow = preComputedData.g;
111+
92112

93113
vec3 color = vec3(0.0);
94114
vec3 reflectedColor = vec3(0.0);
95-
if(settings.bufferOutput == 0){
115+
116+
if(LIGHTING_MODE){
96117
//////////////////////////////////////
97118
// IF LIT MATERIAL
98119
//////////////////////////////////////
@@ -137,6 +158,9 @@ void main()
137158
lighting *= computeShadow(shadowMap, scene.lights[i], i, modelPos);
138159
if(scene.lights[i].shadowType == 1) //VSM
139160
lighting *= computeVarianceShadow(shadowMap, scene.lights[i], i, modelPos);
161+
// if(scene.lights[i].shadowType == 2) //Raytraced
162+
// lighting *= texture(preCompositionBuffer,v_uv).g;
163+
140164
if(scene.lights[i].shadowType == 2) //Raytraced
141165
lighting *= computeRaytracedShadow(
142166
TLAS,
@@ -167,7 +191,7 @@ void main()
167191
}else{
168192
ambient = (scene.ambientIntensity * scene.ambientColor) * brdf.albedo;
169193
}
170-
ambient *= brdf.ao;
194+
ambient *= settings.enableAO == 1 ? (brdf.ao * SSAO) : brdf.ao;
171195
//SSR ________________________________
172196
vec3 fresnel = fresnelSchlick(max(dot(g_normal, normalize(g_pos)), 0.0), brdf.F0);
173197
if(settings.ssr.enabled == 1 && g_isReflective == 1){
@@ -226,7 +250,7 @@ void main()
226250
color = g_albedo;
227251
}
228252

229-
253+
//Fog ________________________________
230254
if(scene.enableFog){
231255
float f = computeFog(g_depth);
232256
color = f * color + (1 - f) * scene.fogColor.rgb;
@@ -236,21 +260,21 @@ void main()
236260

237261
}
238262
else{ //DEBUG MODE
239-
if(settings.bufferOutput == 1) //Albedo
263+
if(ALBEDO_OUTPUT)
240264
outColor = vec4(g_albedo,1.0);
241-
if(settings.bufferOutput == 2) //Normals
242-
outColor = vec4(g_normal,1.0);
243-
if(settings.bufferOutput == 3) //Position
244-
outColor = vec4(g_pos,1.0);
245-
if(settings.bufferOutput == 4) //Material
265+
if(NORMALS_OUTPUT)
266+
outColor = vec4((camera.invView * vec4(g_normal.xyz, 0.0)).xyz,1.0);
267+
if(POSITION_OUTPUT)
268+
outColor = vec4((camera.invView * vec4(g_pos.xyz, 1.0)).xyz,1.0);
269+
if(MATERIAL_OUTPUT)
246270
outColor = g_material;
247-
if(settings.bufferOutput == 5) //SSR
248-
outColor = vec4(reflectedColor,1.0);
249-
}
271+
if(SSAO_OUTPUT)
272+
outColor = vec4(SSAO,SSAO,SSAO,1.0);
273+
}
250274

251275
// check whether result is higher than some threshold, if so, output as bloom threshold color
252276
float brightness = dot(color, vec3(0.2126, 0.7152, 0.0722));
253-
if(brightness > 1.0 && settings.bufferOutput == 0)
277+
if(brightness > 1.0 && LIGHTING_MODE)
254278
outBrightColor = vec4(color, 1.0);
255279
else
256280
outBrightColor = vec4(0.0, 0.0, 0.0, 1.0);
File renamed without changes.

0 commit comments

Comments
 (0)