Created Vulkan Instance (VkInstance)

This commit is contained in:
Pasha Bibko
2025-11-15 17:59:22 +00:00
parent e133929166
commit 2c6446aca9
8 changed files with 104 additions and 6 deletions

5
.idea/codeStyles/codeStyleConfig.xml generated Normal file
View File

@@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

8
.idea/editor.xml generated
View File

@@ -244,5 +244,13 @@
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=IfStdIsConstantEvaluatedCanBeReplaced/@EntryIndexedValue" value="SUGGESTION" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=StdIsConstantEvaluatedWillAlwaysEvaluateToConstant/@EntryIndexedValue" value="WARNING" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=StringLiteralTypo/@EntryIndexedValue" value="DO_NOT_SHOW" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppCodeStyle/BracesInForStatement/@EntryValue" value="Required" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppCodeStyle/BracesInIfStatement/@EntryValue" value="Required" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppCodeStyle/BracesInWhileStatement/@EntryValue" value="Required" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppCodeStyle/BracesRedundant/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppCodeStyle/OverridingDestructorStyle/@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/CppIncludeDirective/SortIncludeDirectives/@EntryValue" value="true" type="bool" />
</component>
</project>

View File

@@ -31,8 +31,10 @@ FetchContent_MakeAvailable(glfw)
project(VulkanRenderer LANGUAGES CXX)
add_executable(VulkanRenderer
main.cpp
src/GLFWManager.h
src/GLFWManager.cpp
src/managers/GLFWManager.h
src/managers/GLFWManager.cpp
src/managers/VulkanManager.cpp
src/managers/VulkanManager.h
)
# Links the libraries to the binary #

View File

@@ -1,4 +1,5 @@
#include "src/GLFWManager.h"
#include "src/managers/GLFWManager.h"
#include "src/managers/VulkanManager.h"
int main()
{
@@ -12,13 +13,20 @@ int main()
if (!window)
return -1;
/* Creates the Vulkan instance */
if (std::optional<VkInstance> instance = VulkanManager::Init(); !instance)
{
GLFWManager::Cleanup();
return -1;
}
/* Polls window events whilst it is still open */
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
}
/* Cleans up GLFW */
if (!GLFWManager::Cleanup())
/* Cleans up GLFW and Vulkan */
if (!GLFWManager::Cleanup() || !VulkanManager::Cleanup())
return -1;
}

View File

@@ -14,7 +14,6 @@ namespace PB::Renderer
/* Stops GLFW from creating openGL contexts */
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
return success;
}

View File

@@ -0,0 +1,59 @@
#include "VulkanManager.h"
#include <iostream>
#include "GLFW/glfw3.h"
namespace PB::Renderer
{
std::optional<VkInstance> VulkanManager::s_Instance = 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;
}
bool VulkanManager::Cleanup()
{
if (s_Instance != std::nullopt)
vkDestroyInstance(s_Instance.value(), nullptr);
s_Instance = std::nullopt;
return true;
}
}

View File

@@ -0,0 +1,17 @@
#pragma once
#include <optional>
#include <vulkan/vulkan.h>
namespace PB::Renderer
{
class VulkanManager
{
private:
static std::optional<VkInstance> s_Instance;
public:
static std::optional<VkInstance> Init();
static bool Cleanup();
};
}