Compare commits

..

17 Commits

Author SHA1 Message Date
Pasha Bibko
bca88e51ed Added VulkanHandle 2025-11-26 21:28:04 +00:00
Pasha Bibko
0b15b55520 Create README.md 2025-11-24 20:23:10 +00:00
Pasha Bibko
ed9d01010a Added mesh type and updated colors 2025-11-23 18:05:59 +00:00
Pasha Bibko
02fe285a9d Moved main.cpp 2025-11-22 15:59:07 +00:00
Pasha Bibko
c12325d30c Fixed Push constant setup errros 2025-11-22 15:55:00 +00:00
Pasha Bibko
201f5896ce Fixed some errors caused on cleanup 2025-11-22 15:48:15 +00:00
Pasha Bibko
d34bb57402 Added Vulkan debug layer 2025-11-21 23:54:22 +00:00
Pasha Bibko
dea1ffee49 Added Debug detection 2025-11-21 23:13:31 +00:00
Pasha Bibko
2eb42da357 Added custom colors 2025-11-21 23:04:51 +00:00
Pasha Bibko
ff7f3e86ee Added multi triangle support 2025-11-19 23:00:51 +00:00
Pasha Bibko
36fbbefe55 Removed compiled shaders from repo 2025-11-19 20:34:25 +00:00
Pasha Bibko
0f08088f39 Added shaders to build process 2025-11-19 20:33:36 +00:00
Pasha Bibko
c3924b05c1 Hid Vulkan helper structs 2025-11-17 15:14:09 +00:00
Pasha Bibko
cfce4b3069 Added resize + fullscreen support 2025-11-16 22:03:14 +00:00
Pasha Bibko
edcf13d62a Moved vulkan files 2025-11-16 21:20:43 +00:00
Pasha Bibko
5dc0974d50 Added FPS counter
Also removed multi window support
2025-11-16 21:19:17 +00:00
Pasha Bibko
4c1226f658 Added comments about Vulkan init 2025-11-16 18:15:55 +00:00
23 changed files with 935 additions and 323 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
# Compiled shader output #
*.spv

7
.idea/dictionaries/project.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="project">
<words>
<w>spirv</w>
</words>
</dictionary>
</component>

23
.idea/editor.xml generated
View File

@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="BackendCodeEditorSettings">
<option name="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=6EF66A4B_002D3E31_002D529B_002D8F75_002D540403659512_002Fd_003Asrc_002Fd_003Amanagers_002Fd_003Avulkan_002Ff_003AVulkanManagerRender_002Ecpp/@EntryIndexedValue" value="ForceIncluded" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CDeclarationWithImplicitIntType/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CommentTypo/@EntryIndexedValue" value="DO_NOT_SHOW" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=ConstevalIfIsAlwaysConstant/@EntryIndexedValue" value="WARNING" type="string" />
@@ -131,8 +132,8 @@
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNoDiscardExpression/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNodiscardFunctionWithoutReturnValue/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNonExceptionSafeResourceAcquisition/@EntryIndexedValue" value="HINT" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNonExplicitConversionOperator/@EntryIndexedValue" value="HINT" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNonExplicitConvertingConstructor/@EntryIndexedValue" value="HINT" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNonExplicitConversionOperator/@EntryIndexedValue" value="DO_NOT_SHOW" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNonExplicitConvertingConstructor/@EntryIndexedValue" value="DO_NOT_SHOW" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNonInlineFunctionDefinitionInHeaderFile/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNonInlineVariableDefinitionInHeaderFile/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNotAllPathsReturnValue/@EntryIndexedValue" value="WARNING" type="string" />
@@ -252,5 +253,23 @@
<option name="/Default/CodeStyle/CodeFormatting/CppCodeStyle/OverridingFunctionStyle/@EntryValue" value="VirtualAndOverride" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppCodeStyle/UseAutoCommonCase/@EntryValue" value="Never" type="string" />
<option name="/Default/CodeStyle/CppIncludeDirective/SortIncludeDirectives/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=0AFB7787612DF743B09AD9412E48D4CC/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;7&quot; Title=&quot;Local variables&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;local variable&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;aaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=0B82708A1BA7774EB13D27F245698A56/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;1&quot; Title=&quot;Classes and structs&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;__interface&quot; /&gt;&lt;type Name=&quot;class&quot; /&gt;&lt;type Name=&quot;struct&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;AaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=1350D079A82E0740947E85445B5AF47C/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;6&quot; Title=&quot;Parameters&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;function parameter&quot; /&gt;&lt;type Name=&quot;lambda parameter&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;aaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=1CBDD658AEE8EA4382EE1F914B5B3314/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;18&quot; Title=&quot;Namespaces&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;namespace&quot; /&gt;&lt;type Name=&quot;namespace alias&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;AaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=2229A2BE1AA9214483A51F028530E042/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;4&quot; Title=&quot;Unions&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;union&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;AaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=2B232F1067F0324F8FF4B9D63ACECDB2/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;16&quot; Title=&quot;Other constants&quot;&gt;&lt;Descriptor Static=&quot;True&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;True&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;class field&quot; /&gt;&lt;type Name=&quot;local variable&quot; /&gt;&lt;type Name=&quot;struct field&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;AA_BB&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=4203BE6F332C5149B409B4D5F7197E54/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;15&quot; Title=&quot;Enumerators&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;scoped enumerator&quot; /&gt;&lt;type Name=&quot;unscoped enumerator&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;AA_BB&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=5653BA7B6222F349B94149A2A53C35B8/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;14&quot; Title=&quot;Union members&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;union member&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;m_&quot; Suffix=&quot;&quot; Style=&quot;AaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=72514D5DF422D442B71A277F97B72887/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;8&quot; Title=&quot;Global variables&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;global variable&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;AA_BB&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=821F3C5CF47D5640AD3511BCBADE17C4/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;9&quot; Title=&quot;Lambdas&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;lambda&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;aaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=8F69F48E2532F54CBAA0039D4557825E/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;10&quot; Title=&quot;Global functions&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;global function&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;AaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=A42D627FA43B054F91D1E1C4281D8896/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;2&quot; Title=&quot;Concepts&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;concept&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;AaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=A4FAA2257682A94F8C2C93E123FAFC7A/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;19&quot; Title=&quot;Typedefs&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;type alias&quot; /&gt;&lt;type Name=&quot;typedef&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;AaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=A7EBF16DA3BDCB42A0B710704BC8A053/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;3&quot; Title=&quot;Enums&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;enum&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;AaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=B6E900853D6D05429D8C57765B2E546A/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;11&quot; Title=&quot;Class and struct methods&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;member function&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;AaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=B82A063F0DDD98498A70D8D7EBB97F8D/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;12&quot; Title=&quot;Class and struct fields&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;class field&quot; /&gt;&lt;type Name=&quot;struct field&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;m_&quot; Suffix=&quot;&quot; Style=&quot;AaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=BBE8AA08E662BF409B2CB08EC597C493/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;13&quot; Title=&quot;Class and struct public fields&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;Indeterminate&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;PUBLIC&quot;&gt;&lt;type Name=&quot;class field&quot; /&gt;&lt;type Name=&quot;struct field&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;m_&quot; Suffix=&quot;&quot; Style=&quot;AaBb&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=BF0D1AE66D64FE4FAF613448A12051A0/@EntryIndexedValue" value="&lt;NamingElement Priority=&quot;17&quot; Title=&quot;Global constants&quot;&gt;&lt;Descriptor Static=&quot;Indeterminate&quot; Constexpr=&quot;Indeterminate&quot; Const=&quot;True&quot; Volatile=&quot;Indeterminate&quot; Accessibility=&quot;NOT_APPLICABLE&quot;&gt;&lt;type Name=&quot;global variable&quot; /&gt;&lt;/Descriptor&gt;&lt;Policy Inspect=&quot;True&quot; WarnAboutPrefixesAndSuffixes=&quot;False&quot; Prefix=&quot;&quot; Suffix=&quot;&quot; Style=&quot;AA_BB&quot; /&gt;&lt;/NamingElement&gt;" type="string" />
</component>
</project>

View File

@@ -0,0 +1,7 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="CppNonExplicitConversionOperator" enabled="false" level="HINT" enabled_by_default="false" />
<inspection_tool class="CppNonExplicitConvertingConstructor" enabled="false" level="HINT" enabled_by_default="false" />
</profile>
</component>

1
.idea/vcs.xml generated
View File

@@ -3,5 +3,6 @@
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/cmake-build-debug/_deps/glfw-src" vcs="Git" />
<mapping directory="$PROJECT_DIR$/cmake-build-release/_deps/glfw-src" vcs="Git" />
</component>
</project>

View File

@@ -27,18 +27,45 @@ FetchContent_Declare(
FetchContent_MakeAvailable(glfw)
# Compiles the shaders #
set(SHADER_DIR "${CMAKE_SOURCE_DIR}/shaders")
file(GLOB SHADERS
"${SHADER_DIR}/*.frag"
"${SHADER_DIR}/*.vert"
)
foreach (SHADER ${SHADERS})
get_filename_component(FILENAME_WE ${SHADER} NAME_WE)
set(SPIRV_FILE "${SHADER_DIR}/${FILENAME_WE}.spv")
add_custom_command(
OUTPUT ${SPIRV_FILE}
COMMAND ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE} -V ${SHADER} -o ${SPIRV_FILE}
DEPENDS ${SHADER}
COMMENT "Compiling shader ${FILENAME}"
)
list(APPEND SPIRV_BINARY_FILES ${SPIRV_FILE})
endforeach()
add_custom_target(CompileShaders ALL DEPENDS ${SPIRV_BINARY_FILES})
# Creates the output binary
project(VulkanRenderer LANGUAGES CXX)
add_executable(VulkanRenderer
main.cpp
src/main.cpp
src/managers/GLFWManager.h
src/managers/GLFWManager.cpp
src/managers/VulkanManagerInit.cpp
src/managers/VulkanManagerRender.cpp
src/managers/VulkanManager.h
src/managers/vulkan/VulkanManagerInit.cpp
src/managers/vulkan/VulkanManagerRender.cpp
src/managers/vulkan/VulkanManager.h
src/VulkanRenderer.h
src/RendererTypes.h
src/Mesh.h
)
add_dependencies(VulkanRenderer CompileShaders)
target_precompile_headers(VulkanRenderer PRIVATE src/VulkanRenderer.h)
# Links the libraries to the binary #

23
README.md Normal file
View File

@@ -0,0 +1,23 @@
# Vulkan Renderer
This project is a small Vulkan renderer written in C++ (and GLSL) with CMake as the build process.
It currently can render basic triangles in 2D space with a given colour.
### Build
```
mkdir build
cd build
cmake ..
cmake --build .
```
This will work on most operating systems and compile all C++ and GLSL code to their required binary formats.
Then all you will need to do is run the outputted binary to see the example scene.
### Planed Features
- Texture Support
- 3D space
- Free camera
- Mesh (.obj) loading

View File

@@ -1,47 +0,0 @@
#include "src/VulkanRenderer.h"
#include "src/managers/GLFWManager.h"
#include "src/managers/VulkanManager.h"
namespace PB::Renderer
{
void CleanupAllAndExit(const int code)
{
if (!GLFWManager::Cleanup())
std::exit(EXIT_FAILURE);
if (!VulkanManager::Cleanup())
std::exit(EXIT_FAILURE);
std::exit(code);
}
}
int main()
{
using namespace PB::Renderer; // Project namespace
/* Initializes GLFW and creates window */
if (!GLFWManager::Init())
return -1;
GLFWwindow* window = GLFWManager::CreateWindow(800, 600, "Vulkan window");
if (!window)
return -1;
/* Runs Vulkan initialisation functions */
if (!VulkanManager::Init(window))
CleanupAllAndExit(EXIT_FAILURE);
/* Polls window events whilst it is still open */
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
if (!VulkanManager::RenderPass())
CleanupAllAndExit(EXIT_FAILURE);
}
/* Cleans up GLFW and Vulkan */
CleanupAllAndExit(EXIT_SUCCESS);
}

View File

@@ -1,7 +1,11 @@
#version 450
layout(location = 0) out vec4 outColor;
layout(push_constant) uniform PC {
vec4 color;
} pc;
void main()
{
outColor = vec4(1.0, 0.0, 0.0, 1.0); // red
outColor = pc.color;
}

Binary file not shown.

Binary file not shown.

View File

@@ -1,13 +1,8 @@
#version 450
vec2 positions[3] = vec2[]
(
vec2(0.0, -0.5),
vec2(0.5, 0.5),
vec2(-0.5, 0.5)
);
layout(location = 0) in vec2 inPosition;
void main()
{
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
gl_Position = vec4(inPosition, 0.0, 1.0);
}

11
src/Mesh.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#include <vector>
namespace PB::Renderer
{
struct Mesh
{
std::vector<float> vertices;
std::vector<int> indices;
};
}

34
src/RendererTypes.h Normal file
View File

@@ -0,0 +1,34 @@
#pragma once
#include <cstdint>
namespace PB::Renderer
{
struct Color
{
float r = 0.0f, g = 0.0f, b = 0.0f, a = 0.0f;
};
namespace Colors
{
constexpr Color white = {1.0f, 1.0f, 1.0f, 1.0f};
constexpr Color red = {1.0f, 0.0f, 0.0f, 1.0f};
constexpr Color green = {0.0f, 1.0f, 0.0f, 1.0f};
constexpr Color blue = {0.0f, 0.0f, 1.0f, 1.0f};
constexpr Color yellow = {1.0f, 1.0f, 0.0f, 1.0f};
constexpr Color cyan = {0.0f, 1.0f, 1.0f, 1.0f};
constexpr Color magenta = {1.0f, 0.0f, 1.0f, 1.0f};
}
class VulkanHandle
{
public:
VulkanHandle(void* _handle) : handle(_handle) {}
template<typename Ty> operator Ty() { return static_cast<Ty>(handle); }
void* get() const { return handle; }
private:
void* handle = nullptr;
};
}

View File

@@ -1,5 +1,25 @@
#pragma once
#ifndef NDEBUG
#define PB_DEBUG
namespace PB::Renderer
{
constexpr bool DEBUG_MODE = true;
constexpr const char* MODE_NAME = "DEBUG";
}
#else
#define PB_RELEASE
namespace PB::Renderer
{
constexpr bool DEBUG_MODE = false;
constexpr const char* MODE_NAME = "RELEASE";
}
#endif
/* Includes general project .h files */
#include "RendererTypes.h"
/* Includes dependencies */
#include <vulkan/vulkan.h>
@@ -9,9 +29,10 @@
/* Commonly used C++ STD files */
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <optional>
#include <fstream>
#include <vector>
#include <set>
#include <vector>

79
src/main.cpp Normal file
View File

@@ -0,0 +1,79 @@
#include "Mesh.h"
#include "VulkanRenderer.h"
#include "managers/GLFWManager.h"
#include "managers/vulkan/VulkanManager.h"
namespace PB::Renderer
{
void CleanupAllAndExit(const int code)
{
if (!GLFWManager::Cleanup())
std::exit(EXIT_FAILURE);
if (!VulkanManager::Cleanup())
std::exit(EXIT_FAILURE);
std::exit(code);
}
}
int main()
{
using namespace PB::Renderer; // Project namespace
/* Initializes GLFW and creates a window */
GLFWwindow* window = GLFWManager::Init(800, 600, "Vulkan window");
if (window == nullptr)
return -1;
/* Runs Vulkan initialisation functions */
if (!VulkanManager::Init(window))
CleanupAllAndExit(EXIT_FAILURE);
/* Adds runtime objects */
{
VulkanManager::CreateNewRenderObject
(
Colors::red, Mesh
{
.vertices =
{
0.0f, -0.5f,
0.5f, 0.9f,
-0.5f, 0.5f
},
.indices = { 0, 1, 2 }
}
);
VulkanManager::CreateNewRenderObject
(
Colors::yellow, Mesh
{
.vertices =
{
-0.9f, 0.0f,
-0.9f, -0.9f,
0.0f, -0.9f
},
.indices = { 0, 1, 2 }
}
);
}
/* Polls window events whilst it is still open */
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
GLFWManager::UpdateWindowTitleFPSInfo();
if (VulkanManager::RenderPass(window) == false)
CleanupAllAndExit(EXIT_FAILURE);
}
/* Cleans up GLFW and Vulkan */
CleanupAllAndExit(EXIT_SUCCESS);
}

View File

@@ -2,53 +2,111 @@
namespace PB::Renderer
{
std::vector<GLFWwindow*> GLFWManager::s_Windows;
GLFWwindow* GLFWManager::s_Window = nullptr;
bool GLFWManager::Init()
int GLFWManager::s_FramesInTheLastSecond = 0;
int GLFWManager::s_FpsCounter = 0;
double GLFWManager::s_LastDeltaUpdateTime = 0.0;
double GLFWManager::s_LastFPSUpdateTime = 0.0;
std::string GLFWManager::s_BaseWindowTitle = {};
bool GLFWManager::s_Fullscreen = false;
int GLFWManager::s_WindowedWidth = 800;
int GLFWManager::s_WindowedHeight = 600;
int GLFWManager::s_WindowedPosX = 50;
int GLFWManager::s_WindowedPosY = 50;
GLFWwindow* GLFWManager::Init(const int width, const int height, const char* title)
{
const bool success = glfwInit();
if (!success)
{
std::cout << "PB::Renderer::GLFWManager::Init(): glfwInit() failed" << std::endl;
return nullptr;
}
/* Stops GLFW from creating openGL contexts */
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
return success;
}
bool GLFWManager::Cleanup()
{
/* Closes all windows */
for (GLFWwindow* window : s_Windows)
glfwDestroyWindow(window);
glfwTerminate();
return true; // Cannot fail (yet)
}
GLFWwindow* GLFWManager::CreateWindow(int width, int height, const char* title)
{
/* Checks for valid window size before creation */
if (width < 0 || height < 0)
{
std::cout << "PB::Renderer::GLFWManager::CreateWindow(): width or height must be < 0" << std::endl;
return nullptr;
}
GLFWwindow* window = glfwCreateWindow(width, height, title, nullptr, nullptr);
/* Stores the window and returns it to the user */
s_Windows.push_back(window);
return window;
/* Creates the window */
s_Window = glfwCreateWindow(width, height, title, nullptr, nullptr);
s_BaseWindowTitle = title;
/* Adds callbacks to the window and returns it */
glfwSetKeyCallback(s_Window, KeyCallback);
return s_Window;
}
GLFWwindow* GLFWManager::GetWindow(int index)
bool GLFWManager::Cleanup()
{
/* Checks input is within the bounds before fetching */
if (index >= s_Windows.size() || index < 0)
if (s_Window != nullptr)
glfwDestroyWindow(s_Window);
glfwTerminate();
return true; // Cannot fail (yet)
}
void GLFWManager::UpdateWindowTitleFPSInfo()
{
const double currentTime = glfwGetTime();
s_FramesInTheLastSecond++;
if (currentTime - s_LastFPSUpdateTime > 1.0f)
{
std::cout << "PB::Renderer::GLFWManager::GetWindow(): index out of bounds" << std::endl;
return nullptr;
s_FpsCounter = s_FramesInTheLastSecond / (currentTime - s_LastFPSUpdateTime);
s_FramesInTheLastSecond = 0;
s_LastFPSUpdateTime = currentTime;
}
return s_Windows[index];
const double deltaTime = currentTime - s_LastDeltaUpdateTime;
s_LastDeltaUpdateTime = currentTime;
const std::string fullTitle = s_BaseWindowTitle + '('
+ std::to_string(s_FpsCounter) + " FPS | "
+ std::to_string(deltaTime) + " ms)";
glfwSetWindowTitle(s_Window, fullTitle.c_str());
}
void GLFWManager::KeyCallback(GLFWwindow* window, const int key, const int scancode, const int action, const int mode)
{
if (action != GLFW_PRESS)
return;
switch (key)
{
case GLFW_KEY_F11:
{
s_Fullscreen = !s_Fullscreen;
if (s_Fullscreen)
{
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* videoMode = glfwGetVideoMode(monitor);
glfwGetWindowPos(window, &s_WindowedPosX, &s_WindowedPosY);
glfwGetWindowSize(window, &s_WindowedWidth, &s_WindowedHeight);
glfwSetWindowMonitor(window, monitor, 0, 0, videoMode->width, videoMode->height, videoMode->refreshRate);
return;
}
glfwSetWindowMonitor(window, nullptr, s_WindowedPosX, s_WindowedPosY, s_WindowedWidth, s_WindowedHeight, 0);
return;
}
default:
return;
}
}
}

View File

@@ -6,14 +6,31 @@ namespace PB::Renderer
{
class GLFWManager
{
private:
static std::vector<GLFWwindow*> s_Windows;
public:
static bool Init();
static GLFWwindow* Init(int width, int height, const char* title = "Unnamed window");
__forceinline static GLFWwindow* GetWindow() { return s_Window; }
static bool Cleanup();
static GLFWwindow* CreateWindow(int width, int height, const char* title = "Unnamed window");
static GLFWwindow* GetWindow(int index = 0);
static __forceinline void PollEvents() { glfwPollEvents(); }
static void UpdateWindowTitleFPSInfo();
private:
static GLFWwindow* s_Window;
static int s_FramesInTheLastSecond;
static int s_FpsCounter;
static double s_LastDeltaUpdateTime;
static double s_LastFPSUpdateTime;
static std::string s_BaseWindowTitle;
static bool s_Fullscreen;
static int s_WindowedWidth;
static int s_WindowedHeight;
static int s_WindowedPosX;
static int s_WindowedPosY;
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mode);
};
}

View File

@@ -1,94 +0,0 @@
#pragma once
#include "../VulkanRenderer.h"
namespace PB::Renderer
{
struct QueueFamilyIndices
{
static constexpr uint32_t UNDEFINED_UINT32_VALUE = 0xFFFFFFFF;
uint32_t graphicsFamily = UNDEFINED_UINT32_VALUE;
uint32_t presentFamily = UNDEFINED_UINT32_VALUE;
[[nodiscard]] bool Complete() const
{
return
graphicsFamily != UNDEFINED_UINT32_VALUE &&
presentFamily != UNDEFINED_UINT32_VALUE;
}
};
struct SwapChainSupportDetails
{
VkSurfaceCapabilitiesKHR capabilities;
std::vector<VkSurfaceFormatKHR> formats;
std::vector<VkPresentModeKHR> presentModes;
};
class VulkanManager
{
public:
/* Static class so (de)constructors have been deleted to stop accidental creation */
VulkanManager() = delete;
~VulkanManager() = delete;
static bool Init(GLFWwindow* window);
static bool Cleanup();
static bool CreateInstance();
static bool CreateSurface(GLFWwindow* window);
static bool PickPhysicalDevice();
static bool CreateLogicalDevice();
static bool CreateSwapChain(GLFWwindow* window);
static bool CreateImageViews();
static bool CreateRenderPass();
static bool CreateFramebuffer();
static bool CreateGraphicsPipeline();
static bool CreateCommandBuffers();
static bool CreateSemaphores();
static bool RenderPass();
private:
static bool IsDeviceSuitable(const VkPhysicalDevice& device);
static QueueFamilyIndices FindQueueFamilies(const VkPhysicalDevice& device);
static bool CheckDeviceExtensionSupport(const VkPhysicalDevice& device);
static SwapChainSupportDetails QuerySwapChainSupport();
static VkSurfaceFormatKHR ChooseSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
static VkPresentModeKHR ChoosePresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes);
static VkExtent2D ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, GLFWwindow* window);
static VkShaderModule CreateShaderModule(const std::string& filename);
static VkInstance s_Instance;
static VkSurfaceKHR s_Surface;
static VkPhysicalDevice s_PhysicalDevice;
static QueueFamilyIndices s_QueueIndices;
static VkDevice s_Device;
static VkQueue s_GraphicsQueue;
static VkQueue s_PresentQueue;
static VkSwapchainKHR s_SwapChain;
static std::vector<VkImage> s_SwapChainImages;
static std::vector<VkImageView> s_SwapChainImageViews;
static VkFormat s_SwapChainImageFormat;
static VkExtent2D s_SwapChainExtent;
static VkRenderPass s_RenderPass;
static std::vector<VkFramebuffer> s_Framebuffers;
static VkPipelineLayout s_PipelineLayout;
static VkPipeline s_RenderPipeline;
static VkCommandPool s_CommandPool;
static std::vector<VkCommandBuffer> s_CommandBuffers;
static VkSemaphore s_ImageAvailableSemaphore;
static VkSemaphore s_RenderFinishedSemaphore;
};
}

View File

@@ -1,76 +0,0 @@
#include "VulkanManager.h"
namespace PB::Renderer
{
bool VulkanManager::RenderPass()
{
uint32_t imageIndex;
VkResult result = vkAcquireNextImageKHR(
s_Device,
s_SwapChain,
UINT64_MAX,
s_ImageAvailableSemaphore,
VK_NULL_HANDLE,
&imageIndex
);
if (result == VK_ERROR_OUT_OF_DATE_KHR)
{
return false;
}
if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR)
{
std::cout << "PB::Renderer::VulkanManager::RenderPass(): Failed to acquire swap chain image!" << std::endl;
return false;
}
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
const VkSemaphore waitSemaphores[] = { s_ImageAvailableSemaphore };
constexpr VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &s_CommandBuffers[imageIndex];
const VkSemaphore signalSemaphores[] = { s_RenderFinishedSemaphore };
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;
if (vkQueueSubmit(s_GraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::RenderPass(): Failed to submit draw command buffer!" << std::endl;
return false;
}
VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
const VkSwapchainKHR swapChains[] = { s_SwapChain };
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex;
result = vkQueuePresentKHR(s_PresentQueue, &presentInfo);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
{
return false;
}
if (result != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::RenderPass(): Failed to present swap chain image!" << std::endl;
return false;
}
vkQueueWaitIdle(s_PresentQueue);
return true;
}
}

View File

@@ -0,0 +1,201 @@
#pragma once
#include "../../VulkanRenderer.h"
namespace PB::Renderer
{
struct Mesh;
class VulkanManager
{
public:
// === Public functions === //
/* Static class so (de)constructors have been deleted to stop accidental creation */
VulkanManager() = delete;
~VulkanManager() = delete;
/* Shorthand for calling all the initialisation functions, returns false if any fail */
static bool Init(GLFWwindow* window);
/* Frees ALL allocated Vulkan resources */
static bool Cleanup();
/*
Creates the global Vulkan instance, which connects the application to Vulkan.
Defines the application info and imports the needed (GLFW) extensions.
*/
static bool CreateInstance();
/*
Ties a surface to the window to present images. Abstracts platform specific code.
Required for creating a swap chain.
*/
static bool CreateSurface(GLFWwindow* window);
/*
Scans for all available GPUs and picks the first one that supports the necessary capabilities.
Only looks for discrete (non-integrated) GPUs and uses that for all rendering.
*/
static bool PickPhysicalDevice();
/*
Creates a logical device which handles the commands from Vulkan to hardware instructions.
Also requests the queues needed.
*/
static bool CreateLogicalDevice();
/*
Creates a swap chain with suitable image formats, present modes and image dimensions.
The swap chain is the series of images that are displayed to the screen.
*/
static bool CreateSwapChain(GLFWwindow* window);
/*
Each swap chain image requires a view, which describes how shaders and
the pipeline interact with the image data.
*/
static bool CreateImageViews();
/*
Defines the attachments (such as color and depth) and defines how they are rendered.
Also defines the subpasses and dependencies between them.
*/
static bool CreateRenderPass();
/*
Creates a framebuffer for each swap chain image, pairing the render pass with the relevant image.
Each framebuffer is a specific set of attachments used drawing a render pass instance.
*/
static bool CreateFramebuffer();
/*
Assembles shaders, fixed function state and creates the render pipeline.
Also checks that the render pass is compatible.
*/
static bool CreateGraphicsPipeline();
/*
Allocates a command pool and command buffers.
The buffers represent the exact instructions the GPU will execute.
*/
static bool CreateCommandBuffers();
/*
Creates sync between coordinate operations between different parts of the rendering.
Required to stop race conditions between frames.
*/
static bool CreateSemaphores();
/*
Draws a frame to the screen using the command buffers and sync objects.
*/
static bool RenderPass(GLFWwindow* window);
static void CreateNewRenderObject(Color color, const Mesh& mesh);
private:
// === Internal helper structs === //
struct RenderObject
{
VkBuffer VertexBuffer{};
VkDeviceMemory VertexBufferMemory{};
VkBuffer IndexBuffer{};
VkDeviceMemory IndexBufferMemory{};
Color DrawColor;
uint32_t IndexCount{};
};
struct QueueFamilyIndices
{
static constexpr uint32_t UNDEFINED_UINT32_VALUE = 0xFFFFFFFF;
uint32_t graphicsFamily = UNDEFINED_UINT32_VALUE;
uint32_t presentFamily = UNDEFINED_UINT32_VALUE;
[[nodiscard]] bool Complete() const
{
return
graphicsFamily != UNDEFINED_UINT32_VALUE &&
presentFamily != UNDEFINED_UINT32_VALUE;
}
};
struct SwapChainSupportDetails
{
VkSurfaceCapabilitiesKHR capabilities;
std::vector<VkSurfaceFormatKHR> formats;
std::vector<VkPresentModeKHR> presentModes;
};
struct BufferCreationInfo
{
VkDeviceSize size;
VkBufferUsageFlags usage;
VkMemoryPropertyFlags properties;
};
// === Vulkan init helpers === //
static bool IsDeviceSuitable(VulkanHandle device);
static QueueFamilyIndices FindQueueFamilies(VulkanHandle device);
static bool CheckDeviceExtensionSupport(VulkanHandle device);
static SwapChainSupportDetails QuerySwapChainSupport();
static VkSurfaceFormatKHR ChooseSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
static VkPresentModeKHR ChoosePresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes);
static VkExtent2D ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, GLFWwindow* window);
static VkShaderModule CreateShaderModule(const std::string& filename);
// === Vulkan render helpers === //
static VkResult RenderPassInternal();
static void RecreateSwapChain(GLFWwindow* window);
static void CleanupSwapChain();
static VkResult RecordCommandBuffer(uint32_t imageIndex);
static uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
static void CreateBuffer(VkBuffer& buffer, VkDeviceMemory& memory, const BufferCreationInfo& info);
// === Custom resources === //
static std::vector<RenderObject> s_RenderObjects;
// === Vulkan resources === //
static VkInstance s_Instance;
static VkSurfaceKHR s_Surface;
static VkPhysicalDevice s_PhysicalDevice;
static QueueFamilyIndices s_QueueIndices;
static VkDevice s_Device;
static VkQueue s_GraphicsQueue;
static VkQueue s_PresentQueue;
static VkSwapchainKHR s_SwapChain;
static std::vector<VkImage> s_SwapChainImages;
static std::vector<VkImageView> s_SwapChainImageViews;
static VkFormat s_SwapChainImageFormat;
static VkExtent2D s_SwapChainExtent;
static VkRenderPass s_RenderPass;
static std::vector<VkFramebuffer> s_Framebuffers;
static VkPipelineLayout s_PipelineLayout;
static VkPipeline s_RenderPipeline;
static VkCommandPool s_CommandPool;
static std::vector<VkCommandBuffer> s_CommandBuffers;
static VkSemaphore s_ImageAvailableSemaphore;
static VkSemaphore s_RenderFinishedSemaphore;
};
}

View File

@@ -1,12 +1,36 @@
#include "VulkanManager.h"
#ifdef PB_DEBUG
static VKAPI_ATTR VkBool32 VKAPI_CALL VulkanErrorDebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT, VkDebugUtilsMessageTypeFlagsEXT, const VkDebugUtilsMessengerCallbackDataEXT* data, void*)
{
static std::set<int32_t> seenErrors;
if (seenErrors.contains(data->messageIdNumber))
return VK_FALSE;
seenErrors.insert(data->messageIdNumber);
std::cout << "\nVULKAN ERROR [" << data->pMessageIdName << "]: \n" << data->pMessage << "\n" << std::endl;
return VK_FALSE;
}
static VkDebugUtilsMessengerEXT gDebugMessenger;
#endif // PB_DEBUG
namespace PB::Renderer
{
// === Custom resources === //
std::vector<VulkanManager::RenderObject> VulkanManager::s_RenderObjects = {};
// === Vulkan Resources === //
VkInstance VulkanManager::s_Instance = VK_NULL_HANDLE;
VkSurfaceKHR VulkanManager::s_Surface = VK_NULL_HANDLE;
VkPhysicalDevice VulkanManager::s_PhysicalDevice = VK_NULL_HANDLE;
QueueFamilyIndices VulkanManager::s_QueueIndices;
VulkanManager::QueueFamilyIndices VulkanManager::s_QueueIndices;
VkDevice VulkanManager::s_Device = VK_NULL_HANDLE;
@@ -33,6 +57,8 @@ namespace PB::Renderer
bool VulkanManager::Init(GLFWwindow* window)
{
std::cout << "Initializing Vulkan with [" << MODE_NAME << "] profile" << std::endl;
return
CreateInstance() &&
CreateSurface(window) &&
@@ -49,6 +75,15 @@ namespace PB::Renderer
bool VulkanManager::Cleanup()
{
for (const RenderObject& object : s_RenderObjects)
{
vkDestroyBuffer(s_Device, object.IndexBuffer, nullptr);
vkDestroyBuffer(s_Device, object.VertexBuffer, nullptr);
vkFreeMemory(s_Device, object.IndexBufferMemory, nullptr);
vkFreeMemory(s_Device, object.VertexBufferMemory, nullptr);
}
if (s_Device != VK_NULL_HANDLE)
vkDeviceWaitIdle(s_Device);
@@ -85,6 +120,16 @@ namespace PB::Renderer
if (s_Surface != VK_NULL_HANDLE)
vkDestroySurfaceKHR(s_Instance, s_Surface, nullptr);
#ifdef PB_DEBUG
if (gDebugMessenger != VK_NULL_HANDLE)
{
if (const auto func = reinterpret_cast<PFN_vkDestroyDebugUtilsMessengerEXT>(vkGetInstanceProcAddr(s_Instance, "vkDestroyDebugUtilsMessengerEXT")); func != nullptr)
func(s_Instance, gDebugMessenger, nullptr);
}
#endif // PB_DEBUG
if (s_Instance != VK_NULL_HANDLE)
vkDestroyInstance(s_Instance, nullptr);
@@ -102,27 +147,68 @@ namespace PB::Renderer
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_3;
VkInstanceCreateInfo createInfo;
VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pNext = nullptr;
createInfo.pApplicationInfo = &appInfo;
createInfo.enabledLayerCount = 0;
createInfo.ppEnabledLayerNames = nullptr;
/* Imports GLFW extensions */
if constexpr (DEBUG_MODE)
{
static const std::vector LAYERS =
{
"VK_LAYER_KHRONOS_validation",
};
createInfo.enabledLayerCount = LAYERS.size();
createInfo.ppEnabledLayerNames = LAYERS.data();
}
else
{
createInfo.enabledLayerCount = 0;
createInfo.ppEnabledLayerNames = nullptr;
}
/* Imports extensions */
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
createInfo.enabledExtensionCount = glfwExtensionCount;
createInfo.ppEnabledExtensionNames = glfwExtensions;
std::vector extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
if constexpr (DEBUG_MODE)
{
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
}
createInfo.enabledExtensionCount = extensions.size();
createInfo.ppEnabledExtensionNames = extensions.data();
/* Creates the Vulkan instance */
VkInstance instance;
if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS)
if (const VkResult result = vkCreateInstance(&createInfo, nullptr, &instance); result != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::Init(): Could not create Vulkan instance" << std::endl;
std::cout << "PB::Renderer::VulkanManager::Init(): Could not create Vulkan instance: [" << result << "]" << std::endl;
return false;
}
/* Adds a debug messenger */
#ifdef PB_DEBUG
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
debugCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
debugCreateInfo.messageSeverity =
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
debugCreateInfo.messageType =
VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
debugCreateInfo.pfnUserCallback = VulkanErrorDebugCallback;
if (const auto func = reinterpret_cast<PFN_vkCreateDebugUtilsMessengerEXT>(vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT")); func != nullptr)
func(instance, &debugCreateInfo, nullptr, &gDebugMessenger);
#endif // PB_DEBUG
s_Instance = instance;
return true;
}
@@ -173,7 +259,7 @@ namespace PB::Renderer
return false;
}
bool VulkanManager::IsDeviceSuitable(const VkPhysicalDevice& device)
bool VulkanManager::IsDeviceSuitable(VulkanHandle device)
{
const QueueFamilyIndices indices = FindQueueFamilies(device);
@@ -195,7 +281,7 @@ namespace PB::Renderer
return indices.Complete() && swapChainAdequate && discreteGPU;
}
QueueFamilyIndices VulkanManager::FindQueueFamilies(const VkPhysicalDevice& device)
VulkanManager::QueueFamilyIndices VulkanManager::FindQueueFamilies(VulkanHandle device)
{
QueueFamilyIndices indices;
@@ -230,7 +316,7 @@ namespace PB::Renderer
return indices;
}
bool VulkanManager::CheckDeviceExtensionSupport(const VkPhysicalDevice& device)
bool VulkanManager::CheckDeviceExtensionSupport(VulkanHandle device)
{
const std::vector REQUIRED_EXTENSIONS = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
@@ -361,7 +447,7 @@ namespace PB::Renderer
return true;
}
SwapChainSupportDetails VulkanManager::QuerySwapChainSupport()
VulkanManager::SwapChainSupportDetails VulkanManager::QuerySwapChainSupport()
{
SwapChainSupportDetails details;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(s_PhysicalDevice, s_Surface, &details.capabilities);
@@ -555,10 +641,30 @@ namespace PB::Renderer
VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
struct Vertex { float pos[2]; };
// Binding description
VkVertexInputBindingDescription binding{};
binding.binding = 0;
binding.stride = sizeof(Vertex);
binding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
// Attribute description
VkVertexInputAttributeDescription attribute{};
attribute.location = 0; // matches layout(location = 0)
attribute.binding = 0;
attribute.format = VK_FORMAT_R32G32_SFLOAT; // vec2
attribute.offset = offsetof(Vertex, pos);
VkVertexInputBindingDescription bindingDescriptions[] = { binding };
VkVertexInputAttributeDescription attributeDescriptions[] = { attribute };
VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexInputInfo.vertexBindingDescriptionCount = 0;
vertexInputInfo.vertexAttributeDescriptionCount = 0;
vertexInputInfo.vertexBindingDescriptionCount = 1;
vertexInputInfo.pVertexBindingDescriptions = bindingDescriptions;
vertexInputInfo.vertexAttributeDescriptionCount = 1;
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions;
VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
@@ -568,13 +674,13 @@ namespace PB::Renderer
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = static_cast<float>(s_SwapChainExtent.width);
viewport.width = static_cast<float>(s_SwapChainExtent.width);
viewport.height = static_cast<float>(s_SwapChainExtent.height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor{};
scissor.offset = {0,0};
scissor.offset = { 0, 0 };
scissor.extent = s_SwapChainExtent;
VkPipelineViewportStateCreateInfo viewportState{};
@@ -600,10 +706,11 @@ namespace PB::Renderer
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT
| VK_COLOR_COMPONENT_G_BIT
| VK_COLOR_COMPONENT_B_BIT
| VK_COLOR_COMPONENT_A_BIT;
colorBlendAttachment.colorWriteMask =
VK_COLOR_COMPONENT_R_BIT
| VK_COLOR_COMPONENT_G_BIT
| VK_COLOR_COMPONENT_B_BIT
| VK_COLOR_COMPONENT_A_BIT;
colorBlendAttachment.blendEnable = VK_FALSE;
VkPipelineColorBlendStateCreateInfo colorBlending{};
@@ -612,12 +719,23 @@ namespace PB::Renderer
colorBlending.attachmentCount = 1;
colorBlending.pAttachments = &colorBlendAttachment;
VkPushConstantRange range{};
range.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
range.offset = 0;
range.size = sizeof(Color); // TEMPORARY
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 0;
pipelineLayoutInfo.pNext = nullptr;
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &range;
if (vkCreatePipelineLayout(s_Device, &pipelineLayoutInfo, nullptr, &s_PipelineLayout) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateGraphicsPipeline(): Could not make pipeline layout" << std::endl;
std::cout << "PB::VulkanManager::CreateGraphicsPipeline(): Failed to create pipeline layout\n";
return false;
}
@@ -625,19 +743,21 @@ namespace PB::Renderer
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.stageCount = 2;
pipelineInfo.pStages = shaderStages;
pipelineInfo.pVertexInputState = &vertexInputInfo;
pipelineInfo.pInputAssemblyState = &inputAssembly;
pipelineInfo.pViewportState = &viewportState;
pipelineInfo.pRasterizationState = &rasterizer;
pipelineInfo.pMultisampleState = &multisampling;
pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.pMultisampleState = &multisampling;
pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.layout = s_PipelineLayout;
pipelineInfo.renderPass = s_RenderPass;
pipelineInfo.subpass = 0;
if (vkCreateGraphicsPipelines(s_Device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &s_RenderPipeline) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateGraphicsPipeline(): Could not make graphics pipeline" << std::endl;
std::cout << "PB::VulkanManager::CreateGraphicsPipeline(): Failed to create graphics pipeline\n";
return false;
}
@@ -703,39 +823,6 @@ namespace PB::Renderer
return false;
}
for (size_t i = 0; i < s_CommandBuffers.size(); i++)
{
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
vkBeginCommandBuffer(s_CommandBuffers[i], &beginInfo);
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = s_RenderPass;
renderPassInfo.framebuffer = s_Framebuffers[i];
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = s_SwapChainExtent;
VkClearValue clearColor = {0.0f, 0.0f, 0.0f, 1.0f};
renderPassInfo.clearValueCount = 1;
renderPassInfo.pClearValues = &clearColor;
vkCmdBeginRenderPass(s_CommandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(s_CommandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, s_RenderPipeline);
vkCmdDraw(s_CommandBuffers[i], 3, 1, 0, 0);
vkCmdEndRenderPass(s_CommandBuffers[i]);
if (vkEndCommandBuffer(s_CommandBuffers[i]) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateCommandBuffers(): Could not end command buffer" << std::endl;
return false;
}
}
return true;
}

View File

@@ -0,0 +1,236 @@
#include "VulkanManager.h"
#include "../../Mesh.h"
#define CHECK_RESULT(res) if (res != VK_SUCCESS) { return res; }
namespace PB::Renderer
{
bool VulkanManager::RenderPass(GLFWwindow* window)
{
switch (const VkResult result = RenderPassInternal())
{
case VK_ERROR_OUT_OF_DATE_KHR:
case VK_SUBOPTIMAL_KHR:
RecreateSwapChain(window);
return true;
default:
return result == VK_SUCCESS;
}
}
void VulkanManager::CreateNewRenderObject(const Color color, const Mesh& mesh)
{
RenderObject obj{};
obj.IndexCount = mesh.indices.size();
obj.DrawColor = color;
BufferCreationInfo vertexCreationInfo{};
vertexCreationInfo.size = mesh.vertices.size() * sizeof(float);;
vertexCreationInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
vertexCreationInfo.properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
CreateBuffer(obj.VertexBuffer, obj.VertexBufferMemory, vertexCreationInfo);
void* data;
vkMapMemory(s_Device, obj.VertexBufferMemory, 0, mesh.vertices.size() * sizeof(float), 0, &data);
std::memcpy(data, mesh.vertices.data(), mesh.vertices.size() * sizeof(float));
vkUnmapMemory(s_Device, obj.VertexBufferMemory);
BufferCreationInfo indexCreationInfo{};
indexCreationInfo.size = mesh.indices.size() * sizeof(uint32_t);
indexCreationInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
indexCreationInfo.properties = VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
CreateBuffer(obj.IndexBuffer, obj.IndexBufferMemory, indexCreationInfo);
vkMapMemory(s_Device, obj.IndexBufferMemory, 0, mesh.indices.size() * sizeof(uint32_t), 0, &data);
std::memcpy(data, mesh.indices.data(), mesh.indices.size() * sizeof(uint32_t));
vkUnmapMemory(s_Device, obj.IndexBufferMemory);
s_RenderObjects.push_back(obj);
}
void VulkanManager::RecreateSwapChain(GLFWwindow* window)
{
int width, height;
glfwGetFramebufferSize(window, &width, &height);
/* Pauses if minimised */
while (width == 0 || height == 0)
{
glfwGetFramebufferSize(window, &width, &height);
glfwWaitEvents();
}
/* Waits until device is idle before recreating swap chain */
vkDeviceWaitIdle(s_Device);
CleanupSwapChain();
CreateSwapChain(window);
CreateImageViews();
CreateRenderPass();
CreateFramebuffer();
CreateGraphicsPipeline();
CreateCommandBuffers();
CreateSemaphores();
}
void VulkanManager::CleanupSwapChain()
{
for (const VkFramebuffer& framebuffer : s_Framebuffers)
{
vkDestroyFramebuffer(s_Device, framebuffer, nullptr);
}
s_Framebuffers.clear();
vkFreeCommandBuffers(s_Device, s_CommandPool, static_cast<uint32_t>(s_CommandBuffers.size()), s_CommandBuffers.data());
vkDestroyPipeline(s_Device, s_RenderPipeline, nullptr);
vkDestroyPipelineLayout(s_Device, s_PipelineLayout, nullptr);
vkDestroyRenderPass(s_Device, s_RenderPass, nullptr);
for (const VkImageView& view : s_SwapChainImageViews)
{
vkDestroyImageView(s_Device, view, nullptr);
}
vkDestroySwapchainKHR(s_Device, s_SwapChain, nullptr);
}
VkResult VulkanManager::RecordCommandBuffer(const uint32_t imageIndex)
{
VkResult result = VK_SUCCESS;
const VkCommandBuffer& cmd = s_CommandBuffers[imageIndex];
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = 0;
beginInfo.pInheritanceInfo = nullptr;
result = vkBeginCommandBuffer(cmd, &beginInfo);
CHECK_RESULT(result);
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = s_RenderPass;
renderPassInfo.framebuffer = s_Framebuffers[imageIndex];
renderPassInfo.renderArea.offset = { 0, 0 };
renderPassInfo.renderArea.extent = s_SwapChainExtent;
VkClearValue clearValue[2];
clearValue[0].color = { 0.0f, 0.0f, 0.0f, 1.0f };
clearValue[1].depthStencil = { 1.0f, 0 };
renderPassInfo.clearValueCount = 2;
renderPassInfo.pClearValues = clearValue;
vkCmdBeginRenderPass(cmd, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, s_RenderPipeline);
for (const auto& renderObject : s_RenderObjects)
{
VkBuffer vertexBuffer = { renderObject.VertexBuffer };
constexpr VkDeviceSize offsets[] = { 0 };
vkCmdBindVertexBuffers(cmd, 0, 1, &vertexBuffer, offsets);
vkCmdBindIndexBuffer(cmd, renderObject.IndexBuffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdPushConstants(cmd, s_PipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(Color), &renderObject.DrawColor);
vkCmdDrawIndexed(cmd, renderObject.IndexCount, 1, 0, 0, 0);
}
vkCmdEndRenderPass(cmd);
result = vkEndCommandBuffer(cmd);
return result;
}
VkResult VulkanManager::RenderPassInternal()
{
uint32_t imageIndex;
VkResult result = vkAcquireNextImageKHR(
s_Device,
s_SwapChain,
UINT64_MAX,
s_ImageAvailableSemaphore,
VK_NULL_HANDLE,
&imageIndex
);
CHECK_RESULT(result);
result = RecordCommandBuffer(imageIndex);
CHECK_RESULT(result);
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
const VkSemaphore waitSemaphores[] = { s_ImageAvailableSemaphore };
constexpr VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &s_CommandBuffers[imageIndex];
const VkSemaphore signalSemaphores[] = { s_RenderFinishedSemaphore };
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;
result = vkQueueSubmit(s_GraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
CHECK_RESULT(result);
VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
const VkSwapchainKHR swapChains[] = { s_SwapChain };
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex;
result = vkQueuePresentKHR(s_PresentQueue, &presentInfo);
CHECK_RESULT(result);
vkQueueWaitIdle(s_PresentQueue);
return VK_SUCCESS;
}
uint32_t VulkanManager::FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
{
VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(s_PhysicalDevice, &memProperties);
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++)
{
bool typeSupported = (typeFilter & (1 << i)) != 0;
bool hasProperties = (memProperties.memoryTypes[i].propertyFlags & properties) == properties;
if (typeSupported && hasProperties)
return i;
}
return -1;
}
void VulkanManager::CreateBuffer(VkBuffer& buffer, VkDeviceMemory& memory, const BufferCreationInfo& info)
{
VkBufferCreateInfo bufferInfo{};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = info.size;
bufferInfo.usage = info.usage;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
vkCreateBuffer(s_Device, &bufferInfo, nullptr, &buffer);
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(s_Device, buffer, &memRequirements);
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = FindMemoryType(memRequirements.memoryTypeBits, info.properties);
vkAllocateMemory(s_Device, &allocInfo, nullptr, &memory);
vkBindBufferMemory(s_Device, buffer, memory, 0);
}
}