From 1f314a6087b276ad8b2680ecb18f43dfe77df595 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Fri, 31 Jan 2025 00:31:01 +0100 Subject: Make gpu module --- hk21/game/main.cpp | 86 +++++------------------------------------------------- 1 file changed, 7 insertions(+), 79 deletions(-) (limited to 'hk21/game/main.cpp') diff --git a/hk21/game/main.cpp b/hk21/game/main.cpp index 03bc77b..2a9414e 100644 --- a/hk21/game/main.cpp +++ b/hk21/game/main.cpp @@ -1,14 +1,9 @@ -#include -#include #include #include #include -#include -#include "hk21/vulkan_loader/api.hpp" - -#define VK_ALLOCATOR nullptr +#include "hk21/game/gpu.hpp" int SDL_main(int /* argc */, char* /* argv */[]) { @@ -17,82 +12,14 @@ int SDL_main(int /* argc */, char* /* argv */[]) SDL_ShowWindow(window); - asl::buffer instance_extensions; - asl::buffer device_extensions; - asl::buffer layers; - - { - uint32_t count = 0; - const char* const* extensions = SDL_Vulkan_GetInstanceExtensions(&count); - for (uint32_t i = 0; i < count; ++i) - { - instance_extensions.push(extensions[i]); // NOLINT(*-pointer-arithmetic) - } - } - - layers.push("VK_LAYER_KHRONOS_validation"); - layers.push("VK_LAYER_LUNARG_monitor"); - - instance_extensions.push(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); - - device_extensions.push(VK_KHR_SWAPCHAIN_EXTENSION_NAME); - - auto vkGetInstanceProcAddr = asl::bit_cast(SDL_Vulkan_GetVkGetInstanceProcAddr()); - auto status = vulkan_loader::load_global(vkGetInstanceProcAddr); - if (!status.ok()) - { - ASL_LOG_ERROR("Couldn't load global Vulkan functions: {}", status); - return 1; - } - - uint32_t version{}; - vkEnumerateInstanceVersion(&version); - - uint32_t vk_major = VK_API_VERSION_MAJOR(version); // NOLINT - uint32_t vk_minor = VK_API_VERSION_MINOR(version); // NOLINT - - if (vk_major != 1 || vk_minor < 3) + auto gpu_opt = gpu::init(window); + if (!gpu_opt.ok()) { - ASL_LOG_ERROR("Incompatible Vulkan version: {}.{}", vk_major, vk_minor); + ASL_LOG_ERROR("Couldn't initialize GPU: {}", gpu_opt); return 1; } + auto gpu = ASL_MOVE(gpu_opt).value(); - VkApplicationInfo app_info{ - .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, - .pNext = nullptr, - .pApplicationName = "HK-21", - .applicationVersion = 0, - .pEngineName = "Custom", - .engineVersion = 0, - .apiVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), // NOLINT - }; - - VkInstanceCreateInfo instance_create_info{ - .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, - .pNext = nullptr, - .flags = 0, - .pApplicationInfo = &app_info, - .enabledLayerCount = static_cast(layers.size()), - .ppEnabledLayerNames = layers.data(), - .enabledExtensionCount = static_cast(instance_extensions.size()), - .ppEnabledExtensionNames = instance_extensions.data(), - }; - - VkInstance instance{}; - VkResult res = vkCreateInstance(&instance_create_info, VK_ALLOCATOR, &instance); - if (res != VK_SUCCESS) - { - ASL_LOG_ERROR("Couldn't create Vulkan instance: {}", res); - return 1; - } - - status = vulkan_loader::load_instance(vkGetInstanceProcAddr, instance); - if (!status.ok()) - { - ASL_LOG_ERROR("Couldn't load instance Vulkan functions: {}", status); - return 1; - } - bool running = true; while (running) { @@ -108,7 +35,8 @@ int SDL_main(int /* argc */, char* /* argv */[]) SDL_Delay(16); } - vkDestroyInstance(instance, VK_ALLOCATOR); + gpu->destroy(); + SDL_DestroyWindow(window); SDL_Quit(); -- cgit