diff options
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/main.cpp | 13 | ||||
| -rw-r--r-- | src/window.cpp | 128 | ||||
| -rw-r--r-- | src/window.hpp | 22 |
4 files changed, 165 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a1e883..c367dcc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,8 @@ set(CMAKE_CXX_FLAGS_RELEASE "-O3 -s DNDEBUG") set(PKE_SOURCE_FILES src/main.cpp + src/window.hpp + src/window.cpp ) include(FetchContent) diff --git a/src/main.cpp b/src/main.cpp index b18b7c6..cf70271 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,21 @@ #include <cstdio> +#include <exception> + +#include "window.hpp" + +PKEWindowProperties windowProps{}; int main() { printf("PKE ENTERING\n"); + try { + CreateWindow(&windowProps); + } catch (const std::exception &exc) { + printf("EXCEPTION: %s\n", exc.what()); + } catch (...) { + printf("UNHANDLED EXCEPTION\n"); + } + DestroyWindow(); printf("PKE EXITING\n"); return 0; } diff --git a/src/window.cpp b/src/window.cpp new file mode 100644 index 0000000..b18e723 --- /dev/null +++ b/src/window.cpp @@ -0,0 +1,128 @@ + +#include <cstdio> +#include <vector> + +#include "window.hpp" + +#define NELEMS(x) (sizeof(x) / sizeof((x)[0])) + +GLFWwindow *window = nullptr; +VkInstance vkInstance = nullptr; +VkPhysicalDevice vkPhysicalDevice = nullptr; +VkSurfaceKHR vkSurfaceKHR = nullptr; + +const bool ENABLE_VALIDATION_LAYERS = true; + +VkBool32 UserDebugCallback( + VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, + VkDebugUtilsMessageTypeFlagsEXT messageType, + const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, void *pUserData) { + printf("Validation Layer: %s\n", pCallbackData->pMessage); + return VK_FALSE; +} + +void InitVulkan() { + + if (ENABLE_VALIDATION_LAYERS) { + unsigned int layerCount; + vkEnumerateInstanceLayerProperties(&layerCount, nullptr); + std::vector<VkLayerProperties> availableLayers(layerCount); + vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data()); + printf("Available Layers:\n"); + for (const auto &layerProperties : availableLayers) { + printf("\t%s\n", layerProperties.layerName); + } + } + + VkApplicationInfo appInfo{}; + appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; + appInfo.apiVersion = VK_API_VERSION_1_3; + appInfo.pApplicationName = "pikul"; + appInfo.pEngineName = "pikul"; + appInfo.applicationVersion = 1; + appInfo.engineVersion = 1; + + VkInstanceCreateInfo createInfo{}; + createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + createInfo.pApplicationInfo = &appInfo; + createInfo.enabledLayerCount = 0; + + std::vector<const char *> enabledLayerNames(1); + enabledLayerNames[0] = "VK_LAYER_KHRONOS_validation"; + printf("Requested Layers:\n"); + for (const char *s : enabledLayerNames) { + printf("\t%s\n", s); + } + VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo{}; + debugCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; + if (ENABLE_VALIDATION_LAYERS) { + createInfo.enabledLayerCount = enabledLayerNames.size(); + createInfo.ppEnabledLayerNames = enabledLayerNames.data(); + + debugCreateInfo.messageSeverity = + VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | + 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 = UserDebugCallback; + debugCreateInfo.pUserData = nullptr; + debugCreateInfo.pNext = nullptr; + createInfo.pNext = &debugCreateInfo; + } + + std::vector<const char *> allGlfwExtensions; + { + unsigned int extensionCount = 0; + const char **glfwExtensions = glfwGetRequiredInstanceExtensions(&extensionCount); + allGlfwExtensions.reserve(extensionCount + 2); + copy(&glfwExtensions[0], &glfwExtensions[extensionCount], back_inserter(allGlfwExtensions)); + + if (ENABLE_VALIDATION_LAYERS) { + allGlfwExtensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); + allGlfwExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); + } + createInfo.ppEnabledExtensionNames = allGlfwExtensions.data(); + createInfo.enabledExtensionCount = allGlfwExtensions.size(); + } + printf("Required Extensions:\n"); + for (const auto &ext : allGlfwExtensions) { + printf("\t%s\n", ext); + } + + if (ENABLE_VALIDATION_LAYERS) { + unsigned int extensionCount = 0; + vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr); + std::vector<VkExtensionProperties> extensions(extensionCount); + vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data()); + printf("Available Extensions:\n"); + for (const auto &extension : extensions) { + printf("\t%s\n", extension.extensionName); + } + } + + auto result = vkCreateInstance(&createInfo, nullptr, &vkInstance); + if (result != VK_SUCCESS) { + printf("Failed to create VkInstance! : %d\n", result); + throw result; + } + +} + +void CreateWindow(PKEWindowProperties *wp) { + if (vkInstance != nullptr) return; + glfwInit(); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + window = glfwCreateWindow(wp->width, wp->height, "Pikul", nullptr, nullptr); + InitVulkan(); +} + +void DestroyWindow() { + if (vkInstance != nullptr) return; + PFN_vkDestroyInstance(vkInstance); + glfwDestroyWindow(window); + glfwTerminate(); +} + diff --git a/src/window.hpp b/src/window.hpp new file mode 100644 index 0000000..4997327 --- /dev/null +++ b/src/window.hpp @@ -0,0 +1,22 @@ +#ifndef PKE_WINDOW_HPP +#define PKE_WINDOW_HPP + +#include <GLFW/glfw3.h> +#include <vulkan/vulkan.h> + +const unsigned int MAX_FRAMES_IN_FLIGHT = 3; + +extern GLFWwindow *glfwWindow; +extern VkInstance vkInstance; +extern VkPhysicalDevice vkPhysicalDevice; +extern VkSurfaceKHR vkSurfaceKHR; + +struct PKEWindowProperties { + unsigned int width = 1280; + unsigned int height = 720; +}; + +void CreateWindow(PKEWindowProperties *wp); +void DestroyWindow(); + +#endif /* PKE_WINDOW_HPP */ |
