Skip to content

Commit cf17aa2

Browse files
committed
G-Buffer bandwidth packed into less attachments (GeometryPass)
1 parent 306bc70 commit cf17aa2

File tree

25 files changed

+451
-301
lines changed

25 files changed

+451
-301
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ endif()
109109
# Choose if building demos directory. User-Defined.
110110
option(BUILD_EXAMPLES "Build Examples Directory" ON)
111111
if(BUILD_EXAMPLES)
112-
add_subdirectory(${CMAKE_SOURCE_DIR}/examples)
112+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/examples)
113113
endif()
114114
# Choose if building demos directory. User-Defined.
115115
option(BUILD_TESTS "Build Tests Directory" ON)
116116
if(BUILD_TESTS)
117117
enable_testing()
118-
add_subdirectory(${CMAKE_SOURCE_DIR}/tests)
118+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tests)
119119
endif()
120120

121121
target_compile_definitions(VulkanEngine PUBLIC ENGINE_RESOURCES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/resources/")

include/engine/common.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,10 @@ typedef enum ColorFormatTypeFlagBits
308308
RGBA_8U = VK_FORMAT_R8G8B8A8_UNORM,
309309
R_32_UINT = VK_FORMAT_R32_UINT,
310310
DEPTH_16F = VK_FORMAT_D16_UNORM,
311-
DEPTH_32F = VK_FORMAT_D32_SFLOAT
312-
} ColorFormatType;
311+
DEPTH_32F = VK_FORMAT_D32_SFLOAT,
312+
RGB10A2 = VK_FORMAT_A2B10G10R10_UNORM_PACK32,
313+
}
314+
ColorFormatType;
313315
typedef enum MipmapModeFlagsBits
314316
{
315317
MIPMAP_NEAREST = 0x00000001,

include/engine/core/geometries/geometry.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ class Geometry
101101
void fill(Vec3* pos, Vec3* normal, Vec2* uv, Vec3* tangent, uint32_t vertNumber);
102102
void fill_voxel_array(std::vector<Graphics::Voxel> voxels);
103103
static Geometry* create_quad();
104-
static Geometry* create_cube();
104+
static Geometry* create_simple_cube(); //Not for shading geometry
105+
static Geometry* create_cube();
105106
static Geometry* create_cylinder(int segments = 1, float radius = 0.5, float height = 2.0);
106107
static void compute_tangents_gram_smidt(std::vector<Graphics::Vertex>& vertices,
107108
const std::vector<uint32_t>& indices);

include/engine/core/passes/bloom_pass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ A (first downsampled image).
3232
class BloomPass : public BaseGraphicPass
3333
{
3434
protected:
35-
ColorFormatType m_colorFormat = SRGBA_32F;
35+
ColorFormatType m_colorFormat = SRGBA_16F;
3636

3737
Graphics::DescriptorSet m_imageDescriptorSet;
3838

include/engine/core/passes/forward_pass.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class ForwardPass : public BaseGraphicPass
6767
void setup_out_attachments(std::vector<Graphics::AttachmentConfig>& attachments,
6868
std::vector<Graphics::SubPassDependency>& dependencies) override;
6969

70+
void create_framebuffer() override;
71+
7072
void setup_uniforms(std::vector<Graphics::Frame>& frames) override;
7173

7274
void setup_shader_passes() override;

include/engine/core/passes/geometry_pass.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,21 @@ class GeometryPass : public BaseGraphicPass
4343
4444
Output Attachments:
4545
-
46-
- Position buffer
47-
- Normal buffer
46+
- Normal + VelX buffer
4847
- Albedo buffer
4948
- Material buffer
50-
- Emmissive buffer
49+
- Emmissive + VelY buffer
5150
- Depth buffer
5251
*/
5352
GeometryPass(Graphics::Device* device,
54-
const PassLinkage<3, 6>& config,
53+
const PassLinkage<3, 5>& config,
5554
Extent2D extent,
5655
ColorFormatType colorFormat,
5756
ColorFormatType depthFormat)
5857
: BaseGraphicPass(device, extent, 1, 1, true, false, "GEOMETRY")
5958
, m_colorFormat(colorFormat)
6059
, m_depthFormat(depthFormat) {
61-
BasePass::store_attachments<3, 6>(config);
60+
BasePass::store_attachments<3, 5>(config);
6261
}
6362

6463
void setup_out_attachments(std::vector<Graphics::AttachmentConfig>& attachments,

include/engine/core/passes/sky_pass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class SkyPass : public BaseGraphicPass
6060
BasePass::store_attachments<0, 1>(config);
6161
}
6262

63-
void create_framebuffer();
63+
void create_framebuffer() override;
6464

6565
virtual void setup_out_attachments(std::vector<Graphics::AttachmentConfig>& attachments,
6666
std::vector<Graphics::SubPassDependency>& dependencies) override;

include/engine/core/scene/camera.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Camera : public Object3D
5151

5252
private:
5353
Mat4 m_view;
54+
5455
Mat4 m_proj;
5556

5657
Frustum m_frustrum;
@@ -63,6 +64,7 @@ class Camera : public Object3D
6364

6465
bool m_perspective{true};
6566
bool m_frustrumCulling{true};
67+
bool m_inverse_Z{false};
6668

6769
static int m_instanceCount;
6870

@@ -76,7 +78,6 @@ class Camera : public Object3D
7678
Camera::m_instanceCount++;
7779
}
7880

79-
8081
inline void set_field_of_view(float fov) {
8182
m_fov = fov;
8283
isDirty = true;
@@ -86,13 +87,22 @@ class Camera : public Object3D
8687
}
8788
inline void set_projection(int width, int height) {
8889
m_aspect = (float)width / (float)height;
89-
m_proj = math::perspectiveRH_ZO(math::radians(m_fov), m_aspect, m_near, m_far);
90-
// m_proj = math::ortho(0.0f,(float)width,0.0f,(float)height,m_near,m_far);
90+
if (m_perspective)
91+
m_proj = !m_inverse_Z ? math::perspectiveRH_ZO(math::radians(m_fov), m_aspect, m_near, m_far)
92+
: math::perspectiveRH_ZO(math::radians(m_fov), m_aspect, m_far, m_near);
93+
else
94+
m_proj = math::ortho(0.0f, (float)width, 0.0f, (float)height, m_near, m_far);
9195
m_proj[1][1] *= -1; // Because Vulkan
9296
}
9397
inline Mat4 get_projection() const {
9498
return m_proj;
9599
}
100+
inline bool inverse_Z() const {
101+
return m_inverse_Z;
102+
}
103+
inline void inverse_Z(bool op) {
104+
m_inverse_Z = op;
105+
}
96106
inline Mat4 get_view() {
97107
if (isDirty)
98108
{

include/engine/core/scene/skybox.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ class Skybox
5656
public:
5757
Skybox(TextureHDR* env)
5858
: m_env(env) {
59-
m_box = Geometry::create_cube();
59+
m_box = Geometry::create_simple_cube();
6060
}
6161
Skybox()
6262
: m_env(nullptr) {
63-
m_box = Geometry::create_cube();
63+
m_box = Geometry::create_simple_cube();
6464
}
6565
~Skybox() {
6666
delete m_box;

resources/shaders/deferred/composition.glsl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ layout(set = 0, binding = 6) uniform sampler2D samplerMap;
5151
layout(set = 0, binding = 7) uniform sampler2D brdfMap;
5252
layout(set = 0, binding = 8) uniform sampler3D voxelMap;
5353
//G-BUFFER
54-
layout(set = 1, binding = 0) uniform sampler2D positionBuffer;
54+
layout(set = 1, binding = 0) uniform sampler2D depthBuffer;
5555
layout(set = 1, binding = 1) uniform sampler2D normalBuffer;
5656
layout(set = 1, binding = 2) uniform sampler2D colorBuffer;
5757
layout(set = 1, binding = 3) uniform sampler2D materialBuffer;
5858
layout(set = 1, binding = 4) uniform sampler2D emissionBuffer;
59+
5960
layout(set = 1, binding = 5) uniform sampler2D preCompositionBuffer;
6061
//TEMPORAL
6162
layout(set = 1, binding = 6) uniform sampler2D prevBuffer;
@@ -104,9 +105,15 @@ void main() {
104105
//////////////////////////////////////
105106
// SETUP SURFACE
106107
//////////////////////////////////////
107-
vec4 positionData = texture(positionBuffer, v_uv);
108-
g_pos = positionData.rgb;
109-
g_depth = positionData.w;
108+
g_depth = texture(depthBuffer, v_uv).r;
109+
110+
// Build position from depth buffer
111+
vec2 ndc = v_uv * 2.0 - 1.0;
112+
vec4 clip = vec4(ndc, g_depth, 1.0);
113+
vec4 viewPos = camera.invProj * clip;
114+
viewPos /= viewPos.w;
115+
g_pos = viewPos.xyz;
116+
110117
g_normal = normalize(texture(normalBuffer, v_uv).rgb);
111118
vec4 colorData = texture(colorBuffer, v_uv);
112119
g_albedo = colorData.rgb;
@@ -209,7 +216,7 @@ void main() {
209216
if(settings.ssr.enabled == 1 && g_isReflective == 1) {
210217
vec3 modelPos = vec3(camera.invView * vec4(g_pos, 1.0));
211218
vec3 fresnel = fresnelSchlick(max(dot(g_normal, normalize(g_pos)), 0.0), brdf.F0);
212-
reflectedColor = performSSR(settings.ssr, g_pos, g_normal, modelPos, positionBuffer, prevBuffer, brdf.metalness, brdf.roughness, fresnel);
219+
reflectedColor = performSSR(settings.ssr, g_pos, g_normal, modelPos, depthBuffer, prevBuffer, brdf.metalness, brdf.roughness, fresnel);
213220
}
214221

215222
}
@@ -273,7 +280,7 @@ void main() {
273280

274281
//Fog ________________________________
275282
if(scene.enableFog) {
276-
float f = computeFog(g_depth);
283+
float f = computeFog(1.0-g_depth);
277284
color = f * color + (1 - f) * scene.fogColor.rgb;
278285
}
279286

0 commit comments

Comments
 (0)