From f334d723f43caacf0d47433c0f41253c20edc501 Mon Sep 17 00:00:00 2001 From: Jiga228 Date: Fri, 19 Sep 2025 20:01:00 +0700 Subject: [PATCH] Remove linux support. Add create logical device --- Core/Core/CoreInstance.cpp | 2 +- Core/Core/CoreInstance.h | 7 +- Core/Core/Linux.cpp | 64 ------- Core/Core/RenderEngine.cpp | 160 +++++++++++++----- Core/Core/RenderEngine.h | 22 ++- .../{Windows.cpp => Windows/WindowsCalls.cpp} | 4 +- 6 files changed, 144 insertions(+), 115 deletions(-) delete mode 100644 Core/Core/Linux.cpp rename Core/Core/{Windows.cpp => Windows/WindowsCalls.cpp} (95%) diff --git a/Core/Core/CoreInstance.cpp b/Core/Core/CoreInstance.cpp index db8f6f1..518705f 100644 --- a/Core/Core/CoreInstance.cpp +++ b/Core/Core/CoreInstance.cpp @@ -63,7 +63,7 @@ CoreInstance::CoreInstance() glfwTerminate(); throw std::runtime_error("Fail create window"); } - render_engine_ = new RenderEngine(window_); + render_engine_ = new RenderEngine(*this, window_); game_ = GameFactory(*this); if (game_ == nullptr) diff --git a/Core/Core/CoreInstance.h b/Core/Core/CoreInstance.h index 897d537..43ee59f 100644 --- a/Core/Core/CoreInstance.h +++ b/Core/Core/CoreInstance.h @@ -59,7 +59,9 @@ public: int getCountCPU() const { return countCPU_; } double getMemorySize() const { return memorySize_; } - + const std::string& getBaseWorldName() const { return main_config_.base_world; } + const std::string& getGameName() const { return main_config_.game_name; } + /** * @throws std::runtime_error if module isn't enabled * @throws std::runtime_error if module not found @@ -78,7 +80,4 @@ public: * @throws std::runtime_error module isn't contain QuitModule function */ void disableModule(const char* name) noexcept(false); - - const std::string& getBaseWorldName() const { return main_config_.base_world; } - const std::string& getGameName() const { return main_config_.game_name; } }; diff --git a/Core/Core/Linux.cpp b/Core/Core/Linux.cpp deleted file mode 100644 index 4e93d75..0000000 --- a/Core/Core/Linux.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#ifdef __linux__ - -#include "SystemCalls.h" - -#include -#include -#include - -#include -#include - -int System::getCountCPU() -{ - int num_cores = sysconf(_SC_NPROCESSORS_ONLN); - if(num_cores <= 0) - throw std::runtime_error("Fail get number of cores"); - - return num_cores; -} - -double System::getMemorySize() -{ - struct sysinfo info{}; - if(sysinfo(&info) == -1) - std::runtime_error("Fail get system info"); - return info.totalram / 1024 / 1024; -} - -void* System::InitModule(const char* ModuleName, CoreInstance& core) -{ - std::string fileName = "./lib"; - fileName += ModuleName; - fileName += ".so"; - - void* handler = dlopen(fileName.c_str(), RTLD_NOW); - if(handler == nullptr) - throw std::runtime_error("Module not found"); - - void(*init)(CoreInstance&) = reinterpret_cast(dlsym(handler, "InitModule")); - if(init == nullptr) - throw std::runtime_error("Module not contains InitModule"); - init(core); - - return handler; -} - -void System::StartModule(void* handler) -{ - void(*start)() = reinterpret_cast(dlsym(handler, "StartModule")); - if(start == nullptr) - throw std::runtime_error("Module not contins StartModule"); - start(); -} - -void System::QuitModule(void* handler) -{ - void(*quit)() = reinterpret_cast(dlsym(handler, "QuitModule")); - if(quit == nullptr) - throw std::runtime_error("Module not contains QuitModule"); - quit(); - dlclose(handler); -} - -#endif diff --git a/Core/Core/RenderEngine.cpp b/Core/Core/RenderEngine.cpp index 1e9a487..decf4fe 100644 --- a/Core/Core/RenderEngine.cpp +++ b/Core/Core/RenderEngine.cpp @@ -2,6 +2,8 @@ #include #include + +#include "CoreInstance.h" #include "Log/Log.h" #ifdef _DEBUG @@ -21,6 +23,11 @@ static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( return VK_FALSE; } +bool RenderEngine::QueueFamilyIndices::is_complete() +{ + return graphycs_family.has_value(); +} + VkResult RenderEngine::enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info) { PFN_vkCreateDebugUtilsMessengerEXT debug_creator = reinterpret_cast(vkGetInstanceProcAddr(instance_, "vkCreateDebugUtilsMessengerEXT")); @@ -48,54 +55,73 @@ bool RenderEngine::is_device_suitable(VkPhysicalDevice device) VkPhysicalDeviceFeatures device_features; vkGetPhysicalDeviceProperties(device, &device_properties); vkGetPhysicalDeviceFeatures(device, &device_features); + + QueueFamilyIndices indices = find_queue_family_indices(device); - return device_properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU && device_features.geometryShader; + return device_properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU + && device_features.geometryShader + && indices.is_complete(); } -RenderEngine::RenderEngine(GLFWwindow* window) : window_(window) -#ifdef _DEBUG - , debug_messenger_(nullptr) -#endif +RenderEngine::QueueFamilyIndices RenderEngine::find_queue_family_indices(VkPhysicalDevice device) { - { - std::vector extensions = get_required_extensions(); - std::vector layers = { -#ifdef _DEBUG - "VK_LAYER_KHRONOS_validation" -#endif - }; + QueueFamilyIndices queue_family_indices; + + uint32_t queueFamilyCount = 0; + vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr); + std::vector family_propertieses(queueFamilyCount); + vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, family_propertieses.data()); - VkApplicationInfo app_info{}; - app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; - app_info.apiVersion = VK_API_VERSION_1_0; - app_info.applicationVersion = VK_MAKE_VERSION(0, 0, 0); - app_info.pApplicationName = "UwU Engine"; - app_info.pEngineName = "UwU Engine"; - app_info.engineVersion = VK_MAKE_VERSION(0, 0, 0); - - - VkInstanceCreateInfo instance_create_info{}; - instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; - instance_create_info.pApplicationInfo = &app_info; - instance_create_info.enabledExtensionCount = extensions.size(); - instance_create_info.ppEnabledExtensionNames = extensions.data(); - instance_create_info.enabledLayerCount = layers.size(); - instance_create_info.ppEnabledLayerNames = layers.data(); -#ifdef _DEBUG - VkDebugUtilsMessengerCreateInfoEXT debug_messenger_create_info{}; - debug_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; - debug_messenger_create_info.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; - debug_messenger_create_info.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; - debug_messenger_create_info.pfnUserCallback = &debugCallback; - instance_create_info.pNext = &debug_messenger_create_info; -#endif - VK_CHECK(vkCreateInstance(&instance_create_info, nullptr, &instance_)); -#ifdef _DEBUG - VK_CHECK(enable_layer_validation(&debug_messenger_create_info)); -#endif + for (uint32_t i = 0; i < family_propertieses.size(); ++i) + { + if (family_propertieses[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) + queue_family_indices.graphycs_family = i; + if(queue_family_indices.is_complete()) + break; } - { + return queue_family_indices; +} + +void RenderEngine::create_instance() +{ + std::vector extensions = get_required_extensions(); + + VkApplicationInfo app_info{}; + app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; + app_info.apiVersion = VK_API_VERSION_1_0; + app_info.applicationVersion = VK_MAKE_VERSION(0, 0, 0); + app_info.pApplicationName = core_.getGameName().c_str(); + app_info.pEngineName = "UwU Engine"; + app_info.engineVersion = VK_MAKE_VERSION(0, 0, 0); + + + VkInstanceCreateInfo instance_create_info{}; + instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + instance_create_info.pApplicationInfo = &app_info; + instance_create_info.enabledExtensionCount = static_cast(extensions.size()); + instance_create_info.ppEnabledExtensionNames = extensions.data(); +#ifdef _DEBUG + std::vector layers_validation = { + "VK_LAYER_KHRONOS_validation" + }; + instance_create_info.enabledLayerCount = static_cast(layers_validation.size()); + instance_create_info.ppEnabledLayerNames = layers_validation.data(); + VkDebugUtilsMessengerCreateInfoEXT debug_messenger_create_info{}; + debug_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; + debug_messenger_create_info.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; + debug_messenger_create_info.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; + debug_messenger_create_info.pfnUserCallback = &debugCallback; + instance_create_info.pNext = &debug_messenger_create_info; +#endif + VK_CHECK(vkCreateInstance(&instance_create_info, nullptr, &instance_)); +#ifdef _DEBUG + VK_CHECK(enable_layer_validation(&debug_messenger_create_info)); +#endif +} + +void RenderEngine::pick_physical_device() +{ uint32_t device_count = 0; std::vector devices; VK_CHECK(vkEnumeratePhysicalDevices(instance_, &device_count, nullptr)); @@ -117,10 +143,60 @@ RenderEngine::RenderEngine(GLFWwindow* window) : window_(window) if (VK_NULL_HANDLE == physical_device_) throw std::runtime_error("No suitable device found"); } + +void RenderEngine::create_logical_device() +{ + QueueFamilyIndices indices = find_queue_family_indices(physical_device_); + + VkPhysicalDeviceFeatures device_features{}; + + float queue_priority = 1.0f; + VkDeviceQueueCreateInfo device_queue_create_info{}; + device_queue_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; + device_queue_create_info.queueFamilyIndex = indices.graphycs_family.value(); + device_queue_create_info.queueCount = 1; + device_queue_create_info.pQueuePriorities = &queue_priority; + + VkDeviceCreateInfo device_create_info{}; + device_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; + device_create_info.queueCreateInfoCount = 1; + device_create_info.pQueueCreateInfos = &device_queue_create_info; + device_create_info.pEnabledFeatures = &device_features; + device_create_info.enabledExtensionCount = 0; + device_create_info.ppEnabledExtensionNames = nullptr; + +#ifdef _DEBUG + std::vector layers_validation = { + "VK_LAYER_KHRONOS_validation" + }; + device_create_info.enabledLayerCount = static_cast(layers_validation.size()); + device_create_info.ppEnabledLayerNames = layers_validation.data(); +#else + device_create_info.enabledLayerCount = 0; + device_create_info.ppEnabledLayerNames = nullptr; + +#endif + VK_CHECK(vkCreateDevice(physical_device_, &device_create_info, nullptr, &device_)); + vkGetDeviceQueue(device_, indices.graphycs_family.value(), 0, &graphics_queue_); +} + +RenderEngine::RenderEngine(CoreInstance& core, GLFWwindow* window): +core_(core), +window_(window) +#ifdef _DEBUG +,debug_messenger_(nullptr) +#endif +{ + create_instance(); + pick_physical_device(); + create_logical_device(); } RenderEngine::~RenderEngine() { + if (device_ != VK_NULL_HANDLE) + vkDestroyDevice(device_, nullptr); + #ifdef _DEBUG if (debug_messenger_ != nullptr && instance_ != nullptr) { PFN_vkDestroyDebugUtilsMessengerEXT destroyer_messenger = reinterpret_cast(vkGetInstanceProcAddr(instance_, "vkDestroyDebugUtilsMessengerEXT")); @@ -128,7 +204,7 @@ RenderEngine::~RenderEngine() } #endif - if (instance_ != nullptr) + if (instance_ != VK_NULL_HANDLE) vkDestroyInstance(instance_, nullptr); if (window_ != nullptr) diff --git a/Core/Core/RenderEngine.h b/Core/Core/RenderEngine.h index 764330b..fe7476c 100644 --- a/Core/Core/RenderEngine.h +++ b/Core/Core/RenderEngine.h @@ -5,12 +5,24 @@ #include "GLFW/glfw3.h" #include +#include + +class CoreInstance; class RenderEngine { + struct QueueFamilyIndices + { + std::optional graphycs_family; + bool is_complete(); + }; + + CoreInstance& core_; GLFWwindow* window_; VkInstance instance_ = VK_NULL_HANDLE; VkPhysicalDevice physical_device_ = VK_NULL_HANDLE; + VkDevice device_ = VK_NULL_HANDLE; + VkQueue graphics_queue_ = VK_NULL_HANDLE; #ifdef _DEBUG VkResult enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info); @@ -18,9 +30,15 @@ class RenderEngine #endif static std::vector get_required_extensions(); - bool is_device_suitable(VkPhysicalDevice device); + static bool is_device_suitable(VkPhysicalDevice device); + static QueueFamilyIndices find_queue_family_indices(VkPhysicalDevice device); + + void create_instance(); + void pick_physical_device(); + void create_logical_device(); public: - RenderEngine(GLFWwindow* window); + // Can throw the exception + RenderEngine(CoreInstance& core, GLFWwindow* window); ~RenderEngine(); void start(); diff --git a/Core/Core/Windows.cpp b/Core/Core/Windows/WindowsCalls.cpp similarity index 95% rename from Core/Core/Windows.cpp rename to Core/Core/Windows/WindowsCalls.cpp index 41ab21c..ad05a22 100644 --- a/Core/Core/Windows.cpp +++ b/Core/Core/Windows/WindowsCalls.cpp @@ -1,6 +1,6 @@ #ifdef _WIN32 -#include "SystemCalls.h" +#include "../SystemCalls.h" #include @@ -38,7 +38,7 @@ double System::getMemorySize() { memStatus.dwLength = sizeof(memStatus); GlobalMemoryStatusEx(&memStatus); - return memStatus.ullTotalPhys / 1024 / 1024; + return static_cast(memStatus.ullTotalPhys) / 1024. / 1024.; } void* System::InitModule(const char* ModuleName, CoreCallBacks* callbacks) {