Compare commits

..

27 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
Pasha Bibko
46d5e41e29 Removed uneeded std::optional uses 2025-11-16 17:51:24 +00:00
Pasha Bibko
e887b2b393 Cleaned up code 2025-11-16 17:25:03 +00:00
Pasha Bibko
f4e77e30a8 Added shaders 2025-11-16 17:06:35 +00:00
Pasha Bibko
038396338e Finished basic Vulkan setup 2025-11-16 17:06:24 +00:00
Pasha Bibko
8ca07fc723 Final Vulkan Init 2025-11-15 23:48:57 +00:00
Pasha Bibko
c3aad0ebd1 Create image views and render passes 2025-11-15 22:54:04 +00:00
Pasha Bibko
b2f33a76fb Added swapchain 2025-11-15 21:54:02 +00:00
Pasha Bibko
9754629059 Setup device and queue 2025-11-15 20:35:35 +00:00
Pasha Bibko
12cdd4e515 Added pick device support 2025-11-15 19:19:43 +00:00
Pasha Bibko
2eb357b2ed Made VulkanRenderer.h a PCH 2025-11-15 18:25:49 +00:00
21 changed files with 1653 additions and 180 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"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="BackendCodeEditorSettings"> <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/=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/=CommentTypo/@EntryIndexedValue" value="DO_NOT_SHOW" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=ConstevalIfIsAlwaysConstant/@EntryIndexedValue" value="WARNING" 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/=CppNoDiscardExpression/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNodiscardFunctionWithoutReturnValue/@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/=CppNonExceptionSafeResourceAcquisition/@EntryIndexedValue" value="HINT" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNonExplicitConversionOperator/@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="HINT" 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/=CppNonInlineFunctionDefinitionInHeaderFile/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=CppNonInlineVariableDefinitionInHeaderFile/@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" /> <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/OverridingFunctionStyle/@EntryValue" value="VirtualAndOverride" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppCodeStyle/UseAutoCommonCase/@EntryValue" value="Never" 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/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> </component>
</project> </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"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/cmake-build-debug/_deps/glfw-src" 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> </component>
</project> </project>

View File

@@ -27,17 +27,47 @@ FetchContent_Declare(
FetchContent_MakeAvailable(glfw) 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 # Creates the output binary
project(VulkanRenderer LANGUAGES CXX) project(VulkanRenderer LANGUAGES CXX)
add_executable(VulkanRenderer add_executable(VulkanRenderer
main.cpp src/main.cpp
src/managers/GLFWManager.h src/managers/GLFWManager.h
src/managers/GLFWManager.cpp src/managers/GLFWManager.cpp
src/managers/VulkanManager.cpp src/managers/vulkan/VulkanManagerInit.cpp
src/managers/VulkanManager.h src/managers/vulkan/VulkanManagerRender.cpp
src/managers/vulkan/VulkanManager.h
src/VulkanRenderer.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 # # Links the libraries to the binary #
target_link_libraries(VulkanRenderer PRIVATE target_link_libraries(VulkanRenderer PRIVATE
Vulkan::Vulkan Vulkan::Vulkan

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,53 +0,0 @@
#include "src/VulkanRenderer.h"
#include "src/managers/GLFWManager.h"
#include "src/managers/VulkanManager.h"
namespace PB::Renderer
{
void CleanupAllAndExit()
{
if (!GLFWManager::Cleanup())
std::exit(EXIT_FAILURE);
if (VulkanManager::Cleanup())
std::exit(EXIT_FAILURE);
std::exit(EXIT_SUCCESS);
}
}
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;
/* Creates the Vulkan instance and surface */
if (std::optional<VkInstance> instance = VulkanManager::Init(); !instance)
{
GLFWManager::Cleanup();
return -1;
}
if (std::optional<VkSurfaceKHR> surface = VulkanManager::CreateSurface(window); !surface)
{
CleanupAllAndExit();
return -1;
}
/* Polls window events whilst it is still open */
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
}
/* Cleans up GLFW and Vulkan */
CleanupAllAndExit();
}

11
shaders/frag.frag Normal file
View File

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

8
shaders/vert.vert Normal file
View File

@@ -0,0 +1,8 @@
#version 450
layout(location = 0) in vec2 inPosition;
void main()
{
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 #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 */ /* Includes dependencies */
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
@@ -9,6 +29,10 @@
/* Commonly used C++ STD files */ /* Commonly used C++ STD files */
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream> #include <iostream>
#include <optional> #include <optional>
#include <set>
#include <vector> #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 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(); const bool success = glfwInit();
if (!success) if (!success)
{
std::cout << "PB::Renderer::GLFWManager::Init(): glfwInit() failed" << std::endl; std::cout << "PB::Renderer::GLFWManager::Init(): glfwInit() failed" << std::endl;
return nullptr;
}
/* Stops GLFW from creating openGL contexts */ /* Stops GLFW from creating openGL contexts */
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); 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 */ /* Checks for valid window size before creation */
if (width < 0 || height < 0) if (width < 0 || height < 0)
{ {
std::cout << "PB::Renderer::GLFWManager::CreateWindow(): width or height must be < 0" << std::endl; std::cout << "PB::Renderer::GLFWManager::CreateWindow(): width or height must be < 0" << std::endl;
return nullptr; return nullptr;
} }
GLFWwindow* window = glfwCreateWindow(width, height, title, nullptr, nullptr);
/* Stores the window and returns it to the user */ /* Creates the window */
s_Windows.push_back(window); s_Window = glfwCreateWindow(width, height, title, nullptr, nullptr);
return window; 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 (s_Window != nullptr)
if (index >= s_Windows.size() || index < 0) glfwDestroyWindow(s_Window);
{
std::cout << "PB::Renderer::GLFWManager::GetWindow(): index out of bounds" << std::endl; glfwTerminate();
return nullptr; return true; // Cannot fail (yet)
} }
return s_Windows[index]; void GLFWManager::UpdateWindowTitleFPSInfo()
{
const double currentTime = glfwGetTime();
s_FramesInTheLastSecond++;
if (currentTime - s_LastFPSUpdateTime > 1.0f)
{
s_FpsCounter = s_FramesInTheLastSecond / (currentTime - s_LastFPSUpdateTime);
s_FramesInTheLastSecond = 0;
s_LastFPSUpdateTime = currentTime;
}
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 class GLFWManager
{ {
private:
static std::vector<GLFWwindow*> s_Windows;
public: 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 bool Cleanup();
static GLFWwindow* CreateWindow(int width, int height, const char* title = "Unnamed window"); static __forceinline void PollEvents() { glfwPollEvents(); }
static GLFWwindow* GetWindow(int index = 0); 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,71 +0,0 @@
#include "VulkanManager.h"
namespace PB::Renderer
{
std::optional<VkInstance> VulkanManager::s_Instance = std::nullopt;
std::optional<VkSurfaceKHR> VulkanManager::s_Surface = std::nullopt;
std::optional<VkInstance> VulkanManager::Init()
{
/* Stops multi initialisation */
if (s_Instance != std::nullopt)
return s_Instance;
VkApplicationInfo appInfo;
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pNext = nullptr;
appInfo.pApplicationName = "VulkanRenderer";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = "VulkanRendererEngine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_3;
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 */
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
createInfo.enabledExtensionCount = glfwExtensionCount;
createInfo.ppEnabledExtensionNames = glfwExtensions;
/* Creates the Vulkan instance */
VkInstance instance;
if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::Init(): Could not create Vulkan instance" << std::endl;
return std::nullopt;
}
s_Instance = instance;
return instance;
}
std::optional<VkSurfaceKHR> VulkanManager::CreateSurface(GLFWwindow* window)
{
VkSurfaceKHR surface;
if (glfwCreateWindowSurface(s_Instance.value(), window, nullptr, &surface) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateSurface(): Failed to create Vulkan Surface" << std::endl;
return std::nullopt;
}
return surface;
}
bool VulkanManager::Cleanup()
{
if (s_Surface != std::nullopt)
vkDestroySurfaceKHR(s_Instance.value(), s_Surface.value(), nullptr);
if (s_Instance != std::nullopt)
vkDestroyInstance(s_Instance.value(), nullptr);
s_Instance = std::nullopt;
return true;
}
}

View File

@@ -1,19 +0,0 @@
#pragma once
#include "../VulkanRenderer.h"
namespace PB::Renderer
{
class VulkanManager
{
private:
static std::optional<VkInstance> s_Instance;
static std::optional<VkSurfaceKHR> s_Surface;
public:
static std::optional<VkInstance> Init();
static bool Cleanup();
static std::optional<VkSurfaceKHR> CreateSurface(GLFWwindow* window);
};
}

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

@@ -0,0 +1,848 @@
#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;
VulkanManager::QueueFamilyIndices VulkanManager::s_QueueIndices;
VkDevice VulkanManager::s_Device = VK_NULL_HANDLE;
VkQueue VulkanManager::s_GraphicsQueue = VK_NULL_HANDLE;
VkQueue VulkanManager::s_PresentQueue = VK_NULL_HANDLE;
VkSwapchainKHR VulkanManager::s_SwapChain = VK_NULL_HANDLE;
std::vector<VkImage> VulkanManager::s_SwapChainImages;
std::vector<VkImageView> VulkanManager::s_SwapChainImageViews;
VkFormat VulkanManager::s_SwapChainImageFormat = {};
VkExtent2D VulkanManager::s_SwapChainExtent;
VkRenderPass VulkanManager::s_RenderPass = VK_NULL_HANDLE;
std::vector<VkFramebuffer> VulkanManager::s_Framebuffers;
VkPipelineLayout VulkanManager::s_PipelineLayout = {};
VkPipeline VulkanManager::s_RenderPipeline = {};
VkCommandPool VulkanManager::s_CommandPool = VK_NULL_HANDLE;
std::vector<VkCommandBuffer> VulkanManager::s_CommandBuffers;
VkSemaphore VulkanManager::s_ImageAvailableSemaphore = VK_NULL_HANDLE;
VkSemaphore VulkanManager::s_RenderFinishedSemaphore = VK_NULL_HANDLE;
bool VulkanManager::Init(GLFWwindow* window)
{
std::cout << "Initializing Vulkan with [" << MODE_NAME << "] profile" << std::endl;
return
CreateInstance() &&
CreateSurface(window) &&
PickPhysicalDevice() &&
CreateLogicalDevice() &&
CreateSwapChain(window) &&
CreateImageViews() &&
CreateRenderPass() &&
CreateFramebuffer() &&
CreateGraphicsPipeline() &&
CreateCommandBuffers() &&
CreateSemaphores();
}
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);
if (s_ImageAvailableSemaphore != VK_NULL_HANDLE)
vkDestroySemaphore(s_Device, s_ImageAvailableSemaphore, nullptr);
if (s_RenderFinishedSemaphore != VK_NULL_HANDLE)
vkDestroySemaphore(s_Device, s_RenderFinishedSemaphore, nullptr);
if (s_CommandPool != VK_NULL_HANDLE)
vkDestroyCommandPool(s_Device, s_CommandPool, nullptr);
for (const VkFramebuffer& fb : s_Framebuffers)
vkDestroyFramebuffer(s_Device, fb, nullptr);
if (s_RenderPipeline != VK_NULL_HANDLE)
vkDestroyPipeline(s_Device, s_RenderPipeline, nullptr);
if (s_PipelineLayout != VK_NULL_HANDLE)
vkDestroyPipelineLayout(s_Device, s_PipelineLayout, nullptr);
if (s_RenderPass != VK_NULL_HANDLE)
vkDestroyRenderPass(s_Device, s_RenderPass, nullptr);
for (const VkImageView& view : s_SwapChainImageViews)
vkDestroyImageView(s_Device, view, nullptr);
if (s_SwapChain != VK_NULL_HANDLE)
vkDestroySwapchainKHR(s_Device, s_SwapChain, nullptr);
if (s_Device != VK_NULL_HANDLE)
vkDestroyDevice(s_Device, nullptr);
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);
return true;
}
bool VulkanManager::CreateInstance()
{
VkApplicationInfo appInfo;
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pNext = nullptr;
appInfo.pApplicationName = "VulkanRenderer";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = "VulkanRendererEngine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_3;
VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pNext = nullptr;
createInfo.pApplicationInfo = &appInfo;
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);
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 (const VkResult result = vkCreateInstance(&createInfo, nullptr, &instance); result != VK_SUCCESS)
{
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;
}
bool VulkanManager::CreateSurface(GLFWwindow* window)
{
VkSurfaceKHR surface;
if (glfwCreateWindowSurface(s_Instance, window, nullptr, &surface) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateSurface(): Failed to create Vulkan Surface" << std::endl;
return false;
}
s_Surface = surface;
return true;
}
bool VulkanManager::PickPhysicalDevice()
{
uint32_t deviceCount = 0;
vkEnumeratePhysicalDevices(s_Instance, &deviceCount, nullptr);
if (deviceCount == 0)
{
std::cout << "PB::Renderer::VulkanManager::PickPhysicalDevice(): No GPU with Vulkan support" << std::endl;
return false;
}
std::vector<VkPhysicalDevice> devices(deviceCount);
vkEnumeratePhysicalDevices(s_Instance, &deviceCount, devices.data());
for (const auto& device : devices)
{
if (IsDeviceSuitable(device))
{
s_PhysicalDevice = device;
s_QueueIndices = FindQueueFamilies(device);
VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(device, &deviceProperties);
std::cout << "Selected GPU: "<< deviceProperties.deviceName << std::endl;
return true;
}
}
std::cout << "Failed to find a suitable GPU" << std::endl;
return false;
}
bool VulkanManager::IsDeviceSuitable(VulkanHandle device)
{
const QueueFamilyIndices indices = FindQueueFamilies(device);
if (const bool extensionsSupported = CheckDeviceExtensionSupport(device); !extensionsSupported)
return false;
VkSurfaceCapabilitiesKHR capabilities;
uint32_t formatCount, presentCount;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, s_Surface, &capabilities);
vkGetPhysicalDeviceSurfaceFormatsKHR(device, s_Surface, &formatCount, nullptr);
vkGetPhysicalDeviceSurfacePresentModesKHR(device, s_Surface, &presentCount, nullptr);
const bool swapChainAdequate = formatCount > 0 && presentCount > 0;
VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(device, &deviceProperties);
const bool discreteGPU = deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU;
return indices.Complete() && swapChainAdequate && discreteGPU;
}
VulkanManager::QueueFamilyIndices VulkanManager::FindQueueFamilies(VulkanHandle device)
{
QueueFamilyIndices indices;
uint32_t queueFamilyCount;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
int index = 0;
for (const auto& family : queueFamilies)
{
if (family.queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
indices.graphicsFamily = index;
}
VkBool32 presentSupport = false;
vkGetPhysicalDeviceSurfaceSupportKHR(device, index, s_Surface, &presentSupport);
if (presentSupport)
{
indices.presentFamily = index;
}
if (indices.Complete())
{
break;
}
index++;
}
return indices;
}
bool VulkanManager::CheckDeviceExtensionSupport(VulkanHandle device)
{
const std::vector REQUIRED_EXTENSIONS = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
};
uint32_t extensionCount;
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
std::vector<VkExtensionProperties> availableExtensions(extensionCount);
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
std::set<std::string> required(REQUIRED_EXTENSIONS.begin(), REQUIRED_EXTENSIONS.end());
for (const auto& [extensionName, specVersion] : availableExtensions)
{
required.erase(extensionName);
}
return required.empty();
}
bool VulkanManager::CreateLogicalDevice()
{
std::set uniqueQueueFamilies =
{
s_QueueIndices.graphicsFamily,
s_QueueIndices.presentFamily
};
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
float queuePriority = 1.0f;
for (uint32_t queueFamily : uniqueQueueFamilies)
{
VkDeviceQueueCreateInfo queueCreateInfo{};
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfo.queueFamilyIndex = queueFamily;
queueCreateInfo.queueCount = 1;
queueCreateInfo.pQueuePriorities = &queuePriority;
queueCreateInfos.push_back(queueCreateInfo);
}
VkPhysicalDeviceFeatures deviceFeatures{};
const std::vector extensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME
};
VkDeviceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
createInfo.pQueueCreateInfos = queueCreateInfos.data();
createInfo.pEnabledFeatures = &deviceFeatures;
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
createInfo.ppEnabledExtensionNames = extensions.data();
if (vkCreateDevice(s_PhysicalDevice, &createInfo, nullptr, &s_Device))
{
std::cout << "Failed to create logical device" << std::endl;
return false;
}
vkGetDeviceQueue(s_Device, s_QueueIndices.graphicsFamily, 0, &s_GraphicsQueue);
vkGetDeviceQueue(s_Device, s_QueueIndices.presentFamily, 0, &s_PresentQueue);
return true;
}
bool VulkanManager::CreateSwapChain(GLFWwindow* window)
{
auto [capabilities, formats, presentModes] = QuerySwapChainSupport();
auto [format, colorSpace] = ChooseSurfaceFormat(formats);
const VkPresentModeKHR presentMode = ChoosePresentMode(presentModes);
s_SwapChainExtent = ChooseSwapExtent(capabilities, window);
uint32_t imageCount = capabilities.minImageCount + 1;
if (capabilities.maxImageCount > 0 && imageCount > capabilities.maxImageCount)
imageCount = capabilities.maxImageCount;
VkSwapchainCreateInfoKHR createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.surface = s_Surface;
createInfo.minImageCount = imageCount;
createInfo.imageFormat = format;
createInfo.imageColorSpace = colorSpace;
createInfo.imageExtent = s_SwapChainExtent;
createInfo.imageArrayLayers = 1;
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
const uint32_t queueFamilyIndices[] =
{
s_QueueIndices.graphicsFamily,
s_QueueIndices.presentFamily
};
if (s_QueueIndices.graphicsFamily != s_QueueIndices.presentFamily)
{
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
createInfo.queueFamilyIndexCount = 2;
createInfo.pQueueFamilyIndices = queueFamilyIndices;
}
else
{
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
createInfo.queueFamilyIndexCount = 0;
createInfo.pQueueFamilyIndices = nullptr;
}
createInfo.preTransform = capabilities.currentTransform;
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
createInfo.presentMode = presentMode;
createInfo.clipped = VK_TRUE;
createInfo.oldSwapchain = VK_NULL_HANDLE;
if (vkCreateSwapchainKHR(s_Device, &createInfo, nullptr, &s_SwapChain) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateSwapChain(): Failed to create swap chain" << std::endl;
return false;
}
vkGetSwapchainImagesKHR(s_Device, s_SwapChain, &imageCount, nullptr);
s_SwapChainImages.resize(imageCount);
vkGetSwapchainImagesKHR(s_Device, s_SwapChain, &imageCount, s_SwapChainImages.data());
s_SwapChainImageFormat = format;
return true;
}
VulkanManager::SwapChainSupportDetails VulkanManager::QuerySwapChainSupport()
{
SwapChainSupportDetails details;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(s_PhysicalDevice, s_Surface, &details.capabilities);
uint32_t formatCount;
vkGetPhysicalDeviceSurfaceFormatsKHR(s_PhysicalDevice, s_Surface, &formatCount, nullptr);
details.formats.resize(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(s_PhysicalDevice, s_Surface, &formatCount, details.formats.data());
uint32_t presentModeCount;
vkGetPhysicalDeviceSurfacePresentModesKHR(s_PhysicalDevice, s_Surface, &presentModeCount, nullptr);
details.presentModes.resize(presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(s_PhysicalDevice, s_Surface, &presentModeCount, details.presentModes.data());
return details;
}
VkSurfaceFormatKHR VulkanManager::ChooseSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats)
{
for (const auto& format : availableFormats)
{
if (format.format == VK_FORMAT_B8G8R8A8_SRGB && format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
{
return format;
}
}
return availableFormats[0];
}
VkPresentModeKHR VulkanManager::ChoosePresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes)
{
for (const auto& mode : availablePresentModes)
{
if (mode == VK_PRESENT_MODE_MAILBOX_KHR)
{
return mode;
}
}
return VK_PRESENT_MODE_FIFO_KHR;
}
VkExtent2D VulkanManager::ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, GLFWwindow* window)
{
if (capabilities.currentExtent.width != UINT32_MAX)
{
return capabilities.currentExtent;
}
int width, height;
glfwGetFramebufferSize(window, &width, &height);
VkExtent2D actualExtent =
{
static_cast<uint32_t>(width),
static_cast<uint32_t>(height)
};
actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));
return actualExtent;
}
bool VulkanManager::CreateImageViews()
{
s_SwapChainImageViews.resize(s_SwapChainImages.size());
for (size_t i = 0; i < s_SwapChainImages.size(); i++)
{
VkImageViewCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.image = s_SwapChainImages[i];
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
createInfo.format = s_SwapChainImageFormat;
createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;
if (vkCreateImageView(s_Device, &createInfo, nullptr, &s_SwapChainImageViews[i]) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateImageView(): Failed to create swap chain image views" << std::endl;
return false;
}
}
return true;
}
bool VulkanManager::CreateRenderPass()
{
VkAttachmentDescription colorAttachment{};
colorAttachment.format = s_SwapChainImageFormat;
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
VkAttachmentReference colorAttachmentRef{};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkSubpassDescription subpass{};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorAttachmentRef;
VkSubpassDependency dependency{};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
VkRenderPassCreateInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = 1;
renderPassInfo.pAttachments = &colorAttachment;
renderPassInfo.subpassCount = 1;
renderPassInfo.pSubpasses = &subpass;
renderPassInfo.dependencyCount = 1;
renderPassInfo.pDependencies = &dependency;
if (VkResult result = vkCreateRenderPass(s_Device, &renderPassInfo, nullptr, &s_RenderPass); result != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateRenderPass(): Failed to create render pass, VkResult = " << result << std::endl;
return false;
}
return true;
}
bool VulkanManager::CreateFramebuffer()
{
s_Framebuffers.resize(s_SwapChainImageViews.size());
for (size_t index = 0; index < s_SwapChainImageViews.size(); index++)
{
const VkImageView attachments[] = {
s_SwapChainImageViews[index]
};
VkFramebufferCreateInfo framebufferInfo{};
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.renderPass = s_RenderPass;
framebufferInfo.attachmentCount = 1;
framebufferInfo.pAttachments = attachments;
framebufferInfo.width = s_SwapChainExtent.width;
framebufferInfo.height = s_SwapChainExtent.height;
framebufferInfo.layers = 1;
if (VkResult result = vkCreateFramebuffer(s_Device, &framebufferInfo, nullptr, &s_Framebuffers[index]); result != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateFramebuffers(): Failed to create framebuffers" << std::endl;
return false;
}
}
return true;
}
bool VulkanManager::CreateGraphicsPipeline()
{
VkShaderModule vertShaderModule = CreateShaderModule("../shaders/vert.spv");
VkShaderModule fragShaderModule = CreateShaderModule("../shaders/frag.spv");
VkPipelineShaderStageCreateInfo vertShaderStageInfo{};
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
vertShaderStageInfo.module = vertShaderModule;
vertShaderStageInfo.pName = "main";
VkPipelineShaderStageCreateInfo fragShaderStageInfo{};
fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
fragShaderStageInfo.module = fragShaderModule;
fragShaderStageInfo.pName = "main";
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 = 1;
vertexInputInfo.pVertexBindingDescriptions = bindingDescriptions;
vertexInputInfo.vertexAttributeDescriptionCount = 1;
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions;
VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
inputAssembly.primitiveRestartEnable = VK_FALSE;
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
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.extent = s_SwapChainExtent;
VkPipelineViewportStateCreateInfo viewportState{};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1;
viewportState.pViewports = &viewport;
viewportState.scissorCount = 1;
viewportState.pScissors = &scissor;
VkPipelineRasterizationStateCreateInfo rasterizer{};
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterizer.depthClampEnable = VK_FALSE;
rasterizer.rasterizerDiscardEnable = VK_FALSE;
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
rasterizer.lineWidth = 1.0f;
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
rasterizer.depthBiasEnable = VK_FALSE;
VkPipelineMultisampleStateCreateInfo multisampling{};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.sampleShadingEnable = VK_FALSE;
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.blendEnable = VK_FALSE;
VkPipelineColorBlendStateCreateInfo colorBlending{};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlending.logicOpEnable = VK_FALSE;
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::VulkanManager::CreateGraphicsPipeline(): Failed to create pipeline layout\n";
return false;
}
VkGraphicsPipelineCreateInfo pipelineInfo{};
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.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::VulkanManager::CreateGraphicsPipeline(): Failed to create graphics pipeline\n";
return false;
}
vkDestroyShaderModule(s_Device, vertShaderModule, nullptr);
vkDestroyShaderModule(s_Device, fragShaderModule, nullptr);
return true;
}
VkShaderModule VulkanManager::CreateShaderModule(const std::string& filename)
{
std::ifstream file(filename, std::ios::ate | std::ios::binary);
if (!file.is_open())
{
std::cout << "Failed to open shader file at '" << std::filesystem::absolute(filename) << "'" << std::endl;
return VK_NULL_HANDLE;
}
const size_t fileSize = file.tellg();
std::vector<char> buffer(fileSize);
file.seekg(0);
file.read(buffer.data(), static_cast<std::streamsize>(fileSize));
file.close();
VkShaderModuleCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = buffer.size();
createInfo.pCode = reinterpret_cast<const uint32_t*>(buffer.data());
VkShaderModule shaderModule;
if (vkCreateShaderModule(s_Device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS)
{
return VK_NULL_HANDLE;
}
return shaderModule;
}
bool VulkanManager::CreateCommandBuffers()
{
VkCommandPoolCreateInfo poolInfo{};
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
poolInfo.queueFamilyIndex = s_QueueIndices.graphicsFamily;
if (vkCreateCommandPool(s_Device, &poolInfo, nullptr, &s_CommandPool) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateCommandBuffers(): Could not create command pool" << std::endl;
return false;
}
s_CommandBuffers.resize(s_Framebuffers.size());
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.commandPool = s_CommandPool;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandBufferCount = static_cast<uint32_t>(s_CommandBuffers.size());
if (vkAllocateCommandBuffers(s_Device, &allocInfo, s_CommandBuffers.data()) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateCommandBuffers(): Could not allocate command buffers" << std::endl;
return false;
}
return true;
}
bool VulkanManager::CreateSemaphores()
{
VkSemaphoreCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
if (vkCreateSemaphore(s_Device, &createInfo, nullptr, &s_ImageAvailableSemaphore) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateSemaphores(): Could not create semaphore" << std::endl;
return false;
}
if (vkCreateSemaphore(s_Device, &createInfo, nullptr, &s_RenderFinishedSemaphore) != VK_SUCCESS)
{
std::cout << "PB::Renderer::VulkanManager::CreateSemaphores(): Could not create semaphore" << 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);
}
}