Compare commits

...

2 Commits

Author SHA1 Message Date
Pasha Bibko
973223c651 Made Vulkan Surface 2025-11-15 18:24:07 +00:00
Pasha Bibko
2c6446aca9 Created Vulkan Instance (VkInstance) 2025-11-15 17:59:22 +00:00
9 changed files with 156 additions and 12 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/=IfStdIsConstantEvaluatedCanBeReplaced/@EntryIndexedValue" value="SUGGESTION" type="string" />
<option name="/Default/CodeInspection/Highlighting/InspectionSeverities/=StdIsConstantEvaluatedWillAlwaysEvaluateToConstant/@EntryIndexedValue" value="WARNING" 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/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> </component>
</project> </project>

View File

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

View File

@@ -1,4 +1,21 @@
#include "src/GLFWManager.h" #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() int main()
{ {
@@ -12,13 +29,25 @@ int main()
if (!window) if (!window)
return -1; 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 */ /* Polls window events whilst it is still open */
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
glfwPollEvents(); glfwPollEvents();
} }
/* Cleans up GLFW */ /* Cleans up GLFW and Vulkan */
if (!GLFWManager::Cleanup()) CleanupAllAndExit();
return -1;
} }

14
src/VulkanRenderer.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
/* Includes dependencies */
#include <vulkan/vulkan.h>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
/* Commonly used C++ STD files */
#include <iostream>
#include <optional>
#include <vector>

View File

@@ -1,7 +1,5 @@
#include "GLFWManager.h" #include "GLFWManager.h"
#include <iostream>
namespace PB::Renderer namespace PB::Renderer
{ {
std::vector<GLFWwindow*> GLFWManager::s_Windows; std::vector<GLFWwindow*> GLFWManager::s_Windows;
@@ -14,7 +12,6 @@ namespace PB::Renderer
/* 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; return success;
} }

View File

@@ -1,8 +1,6 @@
#pragma once #pragma once
#include "GLFW/glfw3.h" #include "../VulkanRenderer.h"
#include <vector>
namespace PB::Renderer namespace PB::Renderer
{ {

View File

@@ -0,0 +1,71 @@
#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

@@ -0,0 +1,19 @@
#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);
};
}