Skip to content

Commit 65cef1a

Browse files
authored
Merge pull request #15 from rfdickerson/depth-prepass
Depth prepass
2 parents 48fef6e + f4e080e commit 65cef1a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2457
-813
lines changed

.clang-format

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
1-
# .clang-format
1+
---
2+
Language: Cpp
23
BasedOnStyle: LLVM
34
IndentWidth: 4
45
TabWidth: 4
56
UseTab: Never
6-
7-
# Don’t indent contents of namespaces
8-
NamespaceIndentation: None
9-
10-
# Align consecutive assignments
11-
AlignConsecutiveAssignments: true
12-
13-
# Optionally align declarations too (if you like the visual symmetry)
14-
AlignConsecutiveDeclarations: true
15-
16-
# Keep parameters and arguments aligned (can be visually helpful)
7+
ColumnLimit: 120 # No forced wrapping; useful for long VulkanHpp calls
8+
AccessModifierOffset: -4
179
AlignAfterOpenBracket: Align
18-
19-
# Optional: control brace placement if you like a specific style
20-
BreakBeforeBraces: Attach
21-
22-
# Optional: max line length
23-
ColumnLimit: 100
10+
AlignConsecutiveAssignments: false
11+
AlignConsecutiveDeclarations: false
12+
AllowShortBlocksOnASingleLine: false
13+
AllowShortFunctionsOnASingleLine: None
14+
AllowShortIfStatementsOnASingleLine: Never
15+
AllowShortLoopsOnASingleLine: false
16+
AlwaysBreakTemplateDeclarations: Yes
17+
BinPackArguments: false
18+
BinPackParameters: false
19+
BreakBeforeBinaryOperators: NonAssignment
20+
BreakBeforeBraces: Custom
21+
BraceWrapping:
22+
AfterClass: true
23+
AfterControlStatement: true
24+
AfterEnum: true
25+
AfterFunction: true
26+
AfterNamespace: true
27+
AfterStruct: true
28+
AfterUnion: true
29+
BeforeCatch: true
30+
BeforeElse: true
31+
IndentBraces: false
32+
SplitEmptyFunction: false
33+
SplitEmptyRecord: false
34+
Cpp11BracedListStyle: true
35+
DerivePointerAlignment: false
36+
NamespaceIndentation: None
37+
PointerAlignment: Left
38+
SortIncludes: true
39+
IncludeBlocks: Preserve

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ build/
22
cmake-build-debug/
33
vcpkg_installed/
44
.idea
5-
resources/
5+
resources/
6+
*.fbx
7+
workspace/

CMakeLists.txt

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ find_package(spdlog CONFIG REQUIRED)
1212
find_package(VulkanMemoryAllocator CONFIG REQUIRED)
1313
find_package(glm CONFIG REQUIRED)
1414
find_package(imgui CONFIG REQUIRED)
15+
find_package(assimp CONFIG REQUIRED)
1516

16-
add_executable(Reactor src/core/main.cpp
17+
add_library(ReactorLib STATIC
1718
src/vulkan/VulkanContext.cpp
1819
src/vulkan/VulkanContext.hpp
1920
src/pch.hpp
@@ -55,9 +56,39 @@ add_executable(Reactor src/core/main.cpp
5556
src/core/OrbitController.cpp
5657
src/core/OrbitController.hpp
5758
src/core/Application.cpp
58-
src/core/Application.hpp)
59+
src/core/Application.hpp
60+
src/vulkan/Vertex.hpp
61+
src/vulkan/ImageUtils.hpp
62+
src/vulkan/ImageUtils.cpp
63+
src/vulkan/Mesh.cpp
64+
src/vulkan/Mesh.hpp
65+
src/vulkan/MeshGenerators.hpp
66+
src/vulkan/MeshGenerators.cpp
67+
src/core/ModelIO.hpp
68+
src/core/ModelIO.cpp
69+
src/vulkan/ShadowMapping.hpp
70+
src/vulkan/ShadowMapping.cpp
71+
)
5972

60-
target_link_libraries(Reactor PRIVATE Vulkan::Vulkan Vulkan::Headers glfw fmt::fmt spdlog::spdlog GPUOpen::VulkanMemoryAllocator glm::glm)
61-
target_link_libraries(Reactor PRIVATE imgui::imgui)
73+
target_compile_definitions(ReactorLib PUBLIC GLM_ENABLE_EXPERIMENTAL)
74+
target_compile_definitions(ReactorLib PUBLIC GLM_FORCE_DEPTH_ZERO_TO_ONE)
6275

63-
target_precompile_headers(Reactor PRIVATE src/pch.hpp)
76+
target_link_libraries(ReactorLib PUBLIC
77+
Vulkan::Vulkan
78+
Vulkan::Headers
79+
glfw
80+
fmt::fmt
81+
spdlog::spdlog
82+
GPUOpen::VulkanMemoryAllocator
83+
glm::glm
84+
imgui::imgui
85+
assimp::assimp
86+
)
87+
88+
target_precompile_headers(ReactorLib PRIVATE src/pch.hpp)
89+
90+
add_executable(Editor src/core/main.cpp)
91+
target_link_libraries(Editor PRIVATE ReactorLib)
92+
93+
add_executable(BuildModel src/tools/ModelBuilder.cpp)
94+
target_link_libraries(BuildModel PRIVATE ReactorLib)

shaders/composite.frag

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,27 @@ layout(location = 0) out vec4 outColor;
55

66
layout(binding = 0) uniform sampler2D uInputImage;
77

8+
// Binding 2: Multisampled depth texture from geometry pass
9+
layout(binding = 2) uniform sampler2DMS uDepthImage;
10+
811
layout(set = 0, binding = 1) uniform CompositeParams {
9-
float uExposure; // Default: 1.0
10-
float uContrast; // Default: 1.0
11-
float uSaturation; // Default: 1.0
12-
float uVignetteIntensity; // Default: 0.5
13-
float uVignetteFalloff; // Default 0.5
12+
float uExposure;// Default: 1.0
13+
float uContrast;// Default: 1.0
14+
float uSaturation;// Default: 1.0
15+
float uVignetteIntensity;// Default: 0.5
16+
float uVignetteFalloff;// Default 0.5
17+
float uFogDensity;
1418
};
1519

20+
// add camera projection uniforms to the UBO or pass them in another way
21+
const float Z_NEAR = 0.1;
22+
const float Z_FAR = 100.0;
23+
24+
float linearizeDepth(float depth) {
25+
float z_n = 2.0 * depth - 1.0;
26+
return (2.0 * Z_NEAR * Z_FAR) / (Z_FAR + Z_NEAR - z_n * (Z_FAR - Z_NEAR));
27+
}
28+
1629
// ACES Filmic Tone Mapping Curve
1730
vec3 tonemapACES(vec3 x) {
1831
const float a = 2.51;
@@ -24,6 +37,21 @@ vec3 tonemapACES(vec3 x) {
2437
}
2538

2639
void main() {
40+
41+
ivec2 texelCoord = ivec2(gl_FragCoord.xy);
42+
43+
float depth = texelFetch(uDepthImage, texelCoord, 0).r;
44+
45+
// --- Fog Calculation ---
46+
vec3 fogColor = vec3(0.5, 0.6, 0.7);// A cool, hazy blue
47+
float fogStart = 0.5;
48+
float fogEnd = 120.0;
49+
float viewDistance = linearizeDepth(depth);
50+
// Calculate fog amount (0.0 = no fog, 1.0 = full fog)
51+
float fogFactor = 1.0 - exp(-viewDistance * uFogDensity);
52+
// float fogFactor = uFogDensity;
53+
// ----------------------
54+
2755
// Sample the HDR input image
2856
vec3 hdrColor = texture(uInputImage, fragUV).rgb;
2957

@@ -43,5 +71,8 @@ void main() {
4371
vec3 grayscale = vec3(dot(finalColor, vec3(0.299, 0.587, 0.114)));
4472
finalColor = mix(grayscale, finalColor, uSaturation);
4573

74+
// Mix the final scene color with the fog color
75+
finalColor = mix(finalColor, fogColor, fogFactor);
76+
4677
outColor = vec4(finalColor, 1.0);
4778
}

shaders/triangle.frag

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,55 @@
11
#version 450
22

3-
layout(location = 0) in vec3 vColor;
3+
layout(location = 0) in vec3 inWorldPos;
4+
layout(location = 1) in vec3 inNormal;
5+
layout(location = 2) in vec4 inLightSpacePos;
6+
7+
layout(binding = 1) uniform LightUBO {
8+
vec4 lightDirection;
9+
vec4 lightColor;
10+
float lightIntensity;
11+
} ubo;
12+
13+
layout(binding = 2) uniform sampler2DShadow shadowMap;
14+
15+
// Final output color
416
layout(location = 0) out vec4 outColor;
517

18+
float calculateShadow()
19+
{
20+
// Perform perspective divide
21+
vec3 projCoords = inLightSpacePos.xyz / inLightSpacePos.w;
22+
23+
// Convert from [-1, 1] to [0, 1] texture coordinates
24+
projCoords.xy = projCoords.xy * 0.5 + 0.5;
25+
26+
// Get the closest depth from the shadow map (from light's perspective)
27+
// The 'z' coordinate of projCoords is the current fragment's depth from the light
28+
// texture() for sampler2DShadow performs the depth comparison automatically
29+
float shadow = texture(shadowMap, projCoords);
30+
31+
return shadow;
32+
}
33+
634
void main() {
7-
outColor = vec4(vColor, 1.0);
35+
vec3 objectColor = vec3(0.8, 0.8, 0.8);
36+
37+
vec3 ambient = objectColor * 0.1;
38+
39+
// 2. Calculate the diffuse (directional) light component.
40+
vec3 normal = normalize(inNormal);
41+
vec3 lightDir = normalize(-ubo.lightDirection.xyz);
42+
float diffuseFactor = max(dot(normal, lightDir), 0.0);
43+
vec3 diffuse = objectColor * ubo.lightColor.rgb * ubo.lightIntensity * diffuseFactor;
44+
45+
// 3. Get the shadow factor (1.0 for lit, 0.0 for shadowed).
46+
float shadow = calculateShadow();
47+
48+
// 4. Combine the components for the final color.
49+
// The final color is the ambient light, plus the direct (diffuse) light,
50+
// which IS blocked by shadows.
51+
vec3 finalColor = ambient + (diffuse * shadow);
52+
53+
outColor = vec4(finalColor, 1.0);
54+
855
}

shaders/triangle.vert

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,29 @@
11
#version 450
22

3-
layout(set = 0, binding = 0) uniform UBO {
3+
layout(location = 0) in vec3 inPosition;
4+
layout(location = 1) in vec3 inNormal;
5+
layout(location = 2) in vec3 inColor;
6+
layout(location = 3) in vec2 inTexCoord;
7+
8+
layout(location = 0) out vec3 outWorldPos;
9+
layout(location = 1) out vec3 outNormal;
10+
layout(location = 2) out vec4 outLightSpacePos;
11+
12+
layout(binding = 0) uniform SceneUBO {
413
mat4 view;
514
mat4 projection;
6-
};
15+
mat4 lightSpaceMatrix;
16+
} ubo;
717

8-
layout(location = 0) out vec3 vColor;
18+
layout(push_constant) uniform PushConstants {
19+
mat4 model;
20+
} push;
921

1022
void main() {
11-
// Hardcoded cube made of 12 triangles (36 vertices)
12-
vec3 positions[36] = vec3[](
13-
// Front face
14-
vec3(-0.5, -0.5, 0.5),
15-
vec3( 0.5, -0.5, 0.5),
16-
vec3( 0.5, 0.5, 0.5),
17-
vec3(-0.5, -0.5, 0.5),
18-
vec3( 0.5, 0.5, 0.5),
19-
vec3(-0.5, 0.5, 0.5),
20-
21-
// Back face
22-
vec3(-0.5, -0.5, -0.5),
23-
vec3( 0.5, 0.5, -0.5),
24-
vec3( 0.5, -0.5, -0.5),
25-
vec3(-0.5, -0.5, -0.5),
26-
vec3(-0.5, 0.5, -0.5),
27-
vec3( 0.5, 0.5, -0.5),
28-
29-
// Left face
30-
vec3(-0.5, -0.5, -0.5),
31-
vec3(-0.5, -0.5, 0.5),
32-
vec3(-0.5, 0.5, 0.5),
33-
vec3(-0.5, -0.5, -0.5),
34-
vec3(-0.5, 0.5, 0.5),
35-
vec3(-0.5, 0.5, -0.5),
36-
37-
// Right face
38-
vec3( 0.5, -0.5, -0.5),
39-
vec3( 0.5, 0.5, 0.5),
40-
vec3( 0.5, -0.5, 0.5),
41-
vec3( 0.5, -0.5, -0.5),
42-
vec3( 0.5, 0.5, -0.5),
43-
vec3( 0.5, 0.5, 0.5),
44-
45-
// Top face
46-
vec3(-0.5, 0.5, -0.5),
47-
vec3(-0.5, 0.5, 0.5),
48-
vec3( 0.5, 0.5, 0.5),
49-
vec3(-0.5, 0.5, -0.5),
50-
vec3( 0.5, 0.5, 0.5),
51-
vec3( 0.5, 0.5, -0.5),
52-
53-
// Bottom face
54-
vec3(-0.5, -0.5, -0.5),
55-
vec3( 0.5, -0.5, 0.5),
56-
vec3(-0.5, -0.5, 0.5),
57-
vec3(-0.5, -0.5, -0.5),
58-
vec3( 0.5, -0.5, -0.5),
59-
vec3( 0.5, -0.5, 0.5)
60-
);
61-
62-
vec3 colors[6] = vec3[](
63-
vec3(1.0, 0.0, 0.0),
64-
vec3(0.0, 1.0, 0.0),
65-
vec3(0.0, 0.0, 1.0),
66-
vec3(1.0, 1.0, 0.0),
67-
vec3(1.0, 0.0, 1.0),
68-
vec3(0.0, 1.0, 1.0)
69-
);
23+
vec4 worldPos = push.model * vec4(inPosition, 1.0);
24+
gl_Position = ubo.projection * ubo.view * worldPos;
7025

71-
int face = gl_VertexIndex / 6; // Each face has 6 vertices
72-
gl_Position = projection * view * vec4(positions[gl_VertexIndex], 1.0);
73-
vColor = colors[face];
26+
outWorldPos = worldPos.xyz;
27+
outNormal = normalize(mat3(push.model) * inNormal);
28+
outLightSpacePos = ubo.lightSpaceMatrix * worldPos;
7429
}

src/core/Application.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void Application::initialize() {
1414
m_window = std::make_unique<Window>(1280, 720, "Reactor", *m_eventManager);
1515
m_camera = std::make_unique<Camera>();
1616
m_orbitController = std::make_unique<OrbitController>(*m_camera);
17+
m_orbitController->setSimCityView(20.0f, 45.0f);
1718

1819
// subscribe controller to input events.
1920
m_eventManager->subscribe(EventType::MouseMoved, m_orbitController.get());
@@ -33,9 +34,9 @@ void Application::initialize() {
3334
m_renderer = std::make_unique<VulkanRenderer>(config, *m_window, *m_camera);
3435
}
3536

36-
void Application::run() {
37+
void Application::run() const {
3738
while (!m_window->shouldClose()) {
38-
m_window->pollEvents();
39+
Window::pollEvents();
3940
m_renderer->drawFrame();
4041
}
4142
}

src/core/Application.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ class Application {
1414
Application();
1515
~Application();
1616

17-
void run();
17+
// Prevent copying
18+
Application(const Application&) = delete;
19+
Application& operator=(const Application&) = delete;
20+
21+
// Allow moving if needed
22+
Application(Application&&) = default;
23+
Application& operator=(Application&&) = default;
24+
25+
// Runs the main application loop. Returns program exit code.
26+
void run() const;
1827

1928
private:
2029
void initialize();

0 commit comments

Comments
 (0)