Compare commits
12 Commits
c3924b05c1
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bca88e51ed | ||
|
|
0b15b55520 | ||
|
|
ed9d01010a | ||
|
|
02fe285a9d | ||
|
|
c12325d30c | ||
|
|
201f5896ce | ||
|
|
d34bb57402 | ||
|
|
dea1ffee49 | ||
|
|
2eb42da357 | ||
|
|
ff7f3e86ee | ||
|
|
36fbbefe55 | ||
|
|
0f08088f39 |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
# Compiled shader output #
|
||||
*.spv
|
||||
7
.idea/dictionaries/project.xml
generated
Normal file
7
.idea/dictionaries/project.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
<component name="ProjectDictionaryState">
|
||||
<dictionary name="project">
|
||||
<words>
|
||||
<w>spirv</w>
|
||||
</words>
|
||||
</dictionary>
|
||||
</component>
|
||||
22
.idea/editor.xml
generated
22
.idea/editor.xml
generated
@@ -132,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" />
|
||||
@@ -253,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="<NamingElement Priority="7" Title="Local variables"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="local variable" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="aaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=0B82708A1BA7774EB13D27F245698A56/@EntryIndexedValue" value="<NamingElement Priority="1" Title="Classes and structs"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="__interface" /><type Name="class" /><type Name="struct" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=1350D079A82E0740947E85445B5AF47C/@EntryIndexedValue" value="<NamingElement Priority="6" Title="Parameters"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="function parameter" /><type Name="lambda parameter" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="aaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=1CBDD658AEE8EA4382EE1F914B5B3314/@EntryIndexedValue" value="<NamingElement Priority="18" Title="Namespaces"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="namespace" /><type Name="namespace alias" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=2229A2BE1AA9214483A51F028530E042/@EntryIndexedValue" value="<NamingElement Priority="4" Title="Unions"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="union" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=2B232F1067F0324F8FF4B9D63ACECDB2/@EntryIndexedValue" value="<NamingElement Priority="16" Title="Other constants"><Descriptor Static="True" Constexpr="Indeterminate" Const="True" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="class field" /><type Name="local variable" /><type Name="struct field" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AA_BB" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=4203BE6F332C5149B409B4D5F7197E54/@EntryIndexedValue" value="<NamingElement Priority="15" Title="Enumerators"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="scoped enumerator" /><type Name="unscoped enumerator" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AA_BB" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=5653BA7B6222F349B94149A2A53C35B8/@EntryIndexedValue" value="<NamingElement Priority="14" Title="Union members"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="union member" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="m_" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=72514D5DF422D442B71A277F97B72887/@EntryIndexedValue" value="<NamingElement Priority="8" Title="Global variables"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="global variable" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AA_BB" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=821F3C5CF47D5640AD3511BCBADE17C4/@EntryIndexedValue" value="<NamingElement Priority="9" Title="Lambdas"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="lambda" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="aaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=8F69F48E2532F54CBAA0039D4557825E/@EntryIndexedValue" value="<NamingElement Priority="10" Title="Global functions"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="global function" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=A42D627FA43B054F91D1E1C4281D8896/@EntryIndexedValue" value="<NamingElement Priority="2" Title="Concepts"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="concept" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=A4FAA2257682A94F8C2C93E123FAFC7A/@EntryIndexedValue" value="<NamingElement Priority="19" Title="Typedefs"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="type alias" /><type Name="typedef" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=A7EBF16DA3BDCB42A0B710704BC8A053/@EntryIndexedValue" value="<NamingElement Priority="3" Title="Enums"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="enum" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=B6E900853D6D05429D8C57765B2E546A/@EntryIndexedValue" value="<NamingElement Priority="11" Title="Class and struct methods"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="member function" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=B82A063F0DDD98498A70D8D7EBB97F8D/@EntryIndexedValue" value="<NamingElement Priority="12" Title="Class and struct fields"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="class field" /><type Name="struct field" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="m_" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=BBE8AA08E662BF409B2CB08EC597C493/@EntryIndexedValue" value="<NamingElement Priority="13" Title="Class and struct public fields"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="PUBLIC"><type Name="class field" /><type Name="struct field" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="m_" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNamingOptions/Rules/=BF0D1AE66D64FE4FAF613448A12051A0/@EntryIndexedValue" value="<NamingElement Priority="17" Title="Global constants"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="True" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="global variable" /></Descriptor><Policy Inspect="True" WarnAboutPrefixesAndSuffixes="False" Prefix="" Suffix="" Style="AA_BB" /></NamingElement>" type="string" />
|
||||
</component>
|
||||
</project>
|
||||
7
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
7
.idea/inspectionProfiles/Project_Default.xml
generated
Normal 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
1
.idea/vcs.xml
generated
@@ -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>
|
||||
@@ -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/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
23
README.md
Normal 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
|
||||
45
main.cpp
45
main.cpp
@@ -1,45 +0,0 @@
|
||||
#include "src/VulkanRenderer.h"
|
||||
|
||||
#include "src/managers/GLFWManager.h"
|
||||
#include "src/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)
|
||||
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();
|
||||
GLFWManager::UpdateWindowTitleFPSInfo();
|
||||
|
||||
if (!VulkanManager::RenderPass(window))
|
||||
CleanupAllAndExit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Cleans up GLFW and Vulkan */
|
||||
CleanupAllAndExit(EXIT_SUCCESS);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
BIN
shaders/frag.spv
BIN
shaders/frag.spv
Binary file not shown.
BIN
shaders/vert.spv
BIN
shaders/vert.spv
Binary file not shown.
@@ -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
11
src/Mesh.h
Normal 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
34
src/RendererTypes.h
Normal 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;
|
||||
};
|
||||
}
|
||||
@@ -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
79
src/main.cpp
Normal 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);
|
||||
}
|
||||
@@ -4,9 +4,13 @@
|
||||
|
||||
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;
|
||||
@@ -88,9 +92,24 @@ namespace PB::Renderer
|
||||
*/
|
||||
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;
|
||||
@@ -113,11 +132,18 @@ namespace PB::Renderer
|
||||
std::vector<VkPresentModeKHR> presentModes;
|
||||
};
|
||||
|
||||
struct BufferCreationInfo
|
||||
{
|
||||
VkDeviceSize size;
|
||||
VkBufferUsageFlags usage;
|
||||
VkMemoryPropertyFlags properties;
|
||||
};
|
||||
|
||||
// === Vulkan init helpers === //
|
||||
|
||||
static bool IsDeviceSuitable(const VkPhysicalDevice& device);
|
||||
static QueueFamilyIndices FindQueueFamilies(const VkPhysicalDevice& device);
|
||||
static bool CheckDeviceExtensionSupport(const VkPhysicalDevice& device);
|
||||
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);
|
||||
@@ -132,6 +158,14 @@ namespace PB::Renderer
|
||||
|
||||
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 === //
|
||||
|
||||
|
||||
@@ -1,7 +1,31 @@
|
||||
#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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
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 GLFW extensions */
|
||||
/* 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;
|
||||
}
|
||||
|
||||
VulkanManager::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,
|
||||
@@ -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;
|
||||
@@ -600,7 +706,8 @@ namespace PB::Renderer
|
||||
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
||||
|
||||
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
|
||||
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT
|
||||
colorBlendAttachment.colorWriteMask =
|
||||
VK_COLOR_COMPONENT_R_BIT
|
||||
| VK_COLOR_COMPONENT_G_BIT
|
||||
| VK_COLOR_COMPONENT_B_BIT
|
||||
| VK_COLOR_COMPONENT_A_BIT;
|
||||
@@ -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 = ⦥
|
||||
|
||||
|
||||
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.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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "VulkanManager.h"
|
||||
#include "../../Mesh.h"
|
||||
|
||||
#define CHECK_RESULT(res) if (res != VK_SUCCESS) { return res; }
|
||||
|
||||
@@ -18,6 +19,36 @@ namespace PB::Renderer
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -64,6 +95,54 @@ namespace PB::Renderer
|
||||
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;
|
||||
@@ -77,6 +156,9 @@ namespace PB::Renderer
|
||||
);
|
||||
CHECK_RESULT(result);
|
||||
|
||||
result = RecordCommandBuffer(imageIndex);
|
||||
CHECK_RESULT(result);
|
||||
|
||||
VkSubmitInfo submitInfo{};
|
||||
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||
|
||||
@@ -112,4 +194,43 @@ namespace PB::Renderer
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user