From 921832a9508ac9b9452e29c3814ff7d6886f2b47 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Sun, 24 May 2026 13:13:20 +0200 Subject: [PATCH] Added a descriptor set builder, changed some filepaths and added a basic server --- .clangd | 3 + src/Common/Logger.h | 40 ++++++++ src/Common/{timer.h => Timer.h} | 3 +- src/Common/{types.h => Types.h} | 13 ++- src/Engine/engine/callbacks.h | 2 +- src/Engine/engine/descriptors.h | 154 ++++++++++++++++++++++++++++-- src/Engine/engine/engine.h | 158 ++++++++++++++++++++++++------- src/Engine/engine/images.h | 2 +- src/Engine/engine/initializers.h | 2 +- src/Server/main.cpp | 7 ++ src/Server/server/init.h | 43 +++++++++ src/Server/server/server.h | 28 ++++++ 12 files changed, 407 insertions(+), 48 deletions(-) create mode 100644 src/Common/Logger.h rename src/Common/{timer.h => Timer.h} (82%) rename src/Common/{types.h => Types.h} (68%) create mode 100644 src/Server/server/init.h create mode 100644 src/Server/server/server.h diff --git a/.clangd b/.clangd index 95311f5..4cc5202 100644 --- a/.clangd +++ b/.clangd @@ -7,3 +7,6 @@ CompileFlags: - -DPIPELINES_IMPL - -DDESCRIPTORS_IMPL - -DTIMER_IMPL + - -DLOGGER_IMPL + - -DSERVER_INIT_IMPL + - -DSERVER_IMPL diff --git a/src/Common/Logger.h b/src/Common/Logger.h new file mode 100644 index 0000000..0b5b0a7 --- /dev/null +++ b/src/Common/Logger.h @@ -0,0 +1,40 @@ +#ifndef LOGGER_H +#define LOGGER_H + +// Custom includes + +class NetLogger { + public: + struct Server{ + int priority; + char* url[]; // Put the url at the end for bit optimization + }; + + void initNetLogger(Server server); + + void logNet(char* body[], char* head, Server server); + + void logNetAsync(); // Todo + private: + + +}; + +#endif // End of header + + +#ifdef LOGGER_IMPL +#ifndef LOGGER_IMPL_H +#define LOGGER_IMPL_H +// Implementation + +// Includes +#include // Todo : add it in the flake + +void NetLogger::initNetLogger(Server server) { + httplib::Client cli(server.url) + // Todo : add error handling +} + +#endif +#endif diff --git a/src/Common/timer.h b/src/Common/Timer.h similarity index 82% rename from src/Common/timer.h rename to src/Common/Timer.h index 1778d32..71f3fe8 100644 --- a/src/Common/timer.h +++ b/src/Common/Timer.h @@ -32,7 +32,8 @@ void RunTimer::startTimer(){ } int RunTimer::endTimer() { - auto elapsed = std::chrono::duration_cast(endTime - startTime); + endTime = Clock::now(); + auto elapsed = std::chrono::duration_cast(endTime - startTime); return elapsed.count(); } diff --git a/src/Common/types.h b/src/Common/Types.h similarity index 68% rename from src/Common/types.h rename to src/Common/Types.h index 038d64f..40b1377 100644 --- a/src/Common/types.h +++ b/src/Common/Types.h @@ -28,7 +28,18 @@ // Custom headers //#include "callbacks.h" -#include "timer.h" +#include "Timer.h" +#include "Logger.h" + +// Custom macro +#define VK_CHECK(x) \ + do { \ + VkResult err = x; \ + if (err) { \ + fmt::println("[Engine:Error] Vulkan error : {}", string_VkResult(err)); \ + abort(); \ + } \ + } while (0) \ // Custom types diff --git a/src/Engine/engine/callbacks.h b/src/Engine/engine/callbacks.h index cd861da..6d4e726 100644 --- a/src/Engine/engine/callbacks.h +++ b/src/Engine/engine/callbacks.h @@ -2,7 +2,7 @@ #define CALLBACK_H // Header // Other includes -#include "Common/types.h" +#include "Common/Types.h" class Callback { public: diff --git a/src/Engine/engine/descriptors.h b/src/Engine/engine/descriptors.h index 5ddfa8b..0e35d52 100644 --- a/src/Engine/engine/descriptors.h +++ b/src/Engine/engine/descriptors.h @@ -2,7 +2,11 @@ #define DESCRIPTORS_H // Header // Other includes -#include "Common/types.h" +#include "Common/Types.h" +#include +#include +#include +#include struct DescriptorLayoutBuilder { @@ -23,6 +27,35 @@ struct DescriptorLayoutBuilder { ); }; +struct DescriptorAllocator { + struct PoolSizeRatio { + VkDescriptorType type; + float ratio; + }; + + VkDescriptorPool pool; + + void initPool( + VkDevice device, + uint32_t maxSets, + std::span poolRatios + ); + + void clearDescriptors( + VkDevice device + ); + + void destroyPool( + VkDevice device + ); + + VkDescriptorSet allocate( + VkDevice device, + VkDescriptorSetLayout layout + ); + +}; + #endif #ifdef DESCRIPTORS_IMPL @@ -35,7 +68,7 @@ void DescriptorLayoutBuilder::addBinding( VkDescriptorType type ){ #ifdef DEBUG - fmt::println("Adding a descriptor set binding..."); + fmt::println("[DescriptorLayoutBuilder] Adding a descriptor set binding..."); #endif VkDescriptorSetLayoutBinding newBind { .binding = binding, @@ -44,26 +77,31 @@ void DescriptorLayoutBuilder::addBinding( }; bindings.push_back(newBind); + #ifdef DEBUG + fmt::println("[DescriptorLayoutBuilder] Descriptor set binding added !"); + #endif } void DescriptorLayoutBuilder::clear() { #ifdef DEBUG - fmt::println("Clearing a descriptor set..."); + fmt::println("[DescriptorLayoutBuilder] Clearing a descriptor set..."); #endif bindings.clear(); - + #ifdef DEBUG + fmt::println("[DescriptorLayoutBuilder] Descriptor set cleared !"); + #endif } VkDescriptorSetLayout DescriptorLayoutBuilder::build ( VkDevice device, VkShaderStageFlags shaderStages, - void* pNext = nullptr, - VkDescriptorSetLayoutCreateFlags flags = 0 + void* pNext, + VkDescriptorSetLayoutCreateFlags flags ) { #ifdef DEBUG - fmt::println("Building a descriptor set..."); + fmt::println("[DescriptorLayoutBuilder] Building a descriptor set..."); #endif for (auto& b : bindings) { @@ -79,7 +117,7 @@ VkDescriptorSetLayout DescriptorLayoutBuilder::build ( .pBindings = bindings.data(), }; - VkDescriptorSet set; + VkDescriptorSetLayout set; VK_CHECK( vkCreateDescriptorSetLayout( @@ -89,10 +127,106 @@ VkDescriptorSetLayout DescriptorLayoutBuilder::build ( &set ) ); - + #ifdef DEBUG + fmt::println("[DescriptorLayoutBuilder] Descriptor set built !"); + #endif return set; - } +void DescriptorAllocator::initPool( + VkDevice device, + uint32_t maxSets, + std::span poolRatios +) { + #ifdef DEBUG + fmt::println("[DescriptorAllocator] Initializing a new pool.."); + #endif + std::vector poolSizes; + + for (PoolSizeRatio ratio : poolRatios) { + poolSizes.push_back(VkDescriptorPoolSize { + .type = ratio.type, + .descriptorCount = uint32_t(ratio.ratio * maxSets) + }); + } + + VkDescriptorPoolCreateInfo poolInfo = { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, + .flags = 0, + .maxSets = maxSets, + .poolSizeCount = (uint32_t)poolSizes.size(), + .pPoolSizes = poolSizes.data() + }; + + vkCreateDescriptorPool( + device, + &poolInfo, + nullptr, + &pool + ); + #ifdef DEBUG + fmt::println("[DescriptorAllocator] Descriptor pool initialized..."); + #endif +} + +void DescriptorAllocator::clearDescriptors( + VkDevice device +) { + #ifdef DEBUG + fmt::println("[DescriptorAllocator] Clearing a descriptor set..."); + #endif + vkResetDescriptorPool( + device, + pool, + 0 + ); + #ifdef DEBUG + fmt::println("[DescriptorAllocator] Descriptor set cleared !"); + #endif +} + +void DescriptorAllocator::destroyPool( + VkDevice device +) { + #ifdef DEBUG + fmt::println("[DescriptorAllocator] Destroying a descriptor pool..."); + #endif + vkDestroyDescriptorPool( + device, + pool, + nullptr + ); + + #ifdef DEBUG + fmt::println("[DescriptorAllocator] Descriptor pool destroyed !"); + #endif +} + +VkDescriptorSet DescriptorAllocator::allocate( + VkDevice device, + VkDescriptorSetLayout layout +){ + VkDescriptorSetAllocateInfo allocInfo = { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, + .pNext = nullptr, + .descriptorPool = pool, + .descriptorSetCount = 1, + .pSetLayouts = &layout + }; + + VkDescriptorSet ds; + + VK_CHECK( + vkAllocateDescriptorSets( + device, + &allocInfo, + &ds + ) + ); + + return ds; +} + + #endif #endif diff --git a/src/Engine/engine/engine.h b/src/Engine/engine/engine.h index a5e4a2c..6f588f2 100644 --- a/src/Engine/engine/engine.h +++ b/src/Engine/engine/engine.h @@ -2,8 +2,10 @@ #define ENGINE_H // Normal header -#include "Common/types.h" +#include "descriptors.h" // Required for public interface custom allocator +#include "Common/Types.h" +#include struct FrameData { @@ -55,6 +57,11 @@ class Engine { AllocatedImage _drawImage; VkExtent2D _drawExtent; + // Descriptor Sets + DescriptorAllocator globalDescriptorAllocator; + VkDescriptorSet _drawImageDescriptors; + VkDescriptorSetLayout _drawImageDescriptorLayout; + void init(); @@ -79,6 +86,8 @@ class Engine { void drawBackground( VkCommandBuffer cmd ); + + void initDescriptors(); }; #endif @@ -91,16 +100,19 @@ class Engine { // Implementation defines #define IMAGES_IMPL #define INITIALIZERS_IMPL +#define DESCRIPTORS_IMPL #define CALLBACK_IMPL #define TIMER_IMPL +#define TYPES_IMPL #define VMA_IMPLEMENTATION // Custom includes #include "images.h" #include "initializers.h" #include "callbacks.h" -#include "Common/timer.h" -#include "Common/types.h" +#include "descriptors.h" +#include "Common/Types.h" +#include "Common/Timer.h" #include "VkBootstrap.h" #include "vk_mem_alloc.h" @@ -115,20 +127,24 @@ constexpr bool bUseValidationLayers = true; // Abort when there is an error // Todo : Give an error message or crash dump -using namespace std; -#define VK_CHECK(x) \ +//using namespace std; +//#define VK_CHECK(x) \ do { \ VkResult err = x; \ - if (err) { \ - fmt::println("Vulkan error : {}", string_VkResult(err)); \ - abort(); \ - } \ - } while (0) \ +// if (err) { \ +// fmt::println("[Engine:Error] Vulkan error : {}", string_VkResult(err)); \ +// abort(); \ +// } \ +// } while (0) \ + +//====================== +//=== Main Functions === +//====================== void Engine::init() { #ifdef DEBUG - fmt::println("Initializing the engine..."); - fmt::println("Current engine version {}", BUILD_ID); + fmt::println("[Engine:Init] Initializing the engine..."); + fmt::println("[Engine:Info] Current engine version {}", BUILD_ID); #endif glfwInit(); @@ -146,18 +162,18 @@ void Engine::init() { glfwSetKeyCallback(window, Callback::keyboardCallback); - _isInitialized = true; - initRender(); + _isInitialized = true; + #ifdef DEBUG - fmt::println("Engine Initialized !"); + fmt::println("[Engine:Init] Engine Initialized !"); #endif } void Engine::cleanup() { #ifdef DEBUG - fmt::println("Cleaning up..."); + fmt::println("[Engine:Cleanup] Cleaning up..."); #endif if (_isInitialized) { // Wait for the GPU to stop working @@ -209,7 +225,7 @@ void Engine::cleanup() { } #ifdef DEBUG - fmt::println("Engine cleaned up !"); + fmt::println("[Engine:Cleanup] Engine cleaned up !"); #endif } @@ -313,7 +329,7 @@ void Engine::draw() { void Engine::run() { #ifdef DEBUG - fmt::println("Running..."); + fmt::println("[Engine:Info] Running..."); RunTimer timer; timer.startTimer(); #endif @@ -337,10 +353,14 @@ void Engine::run() { draw(); } #ifdef DEBUG - fmt::println("Engine has finished running after {} seconds!", timer.endTimer()); + fmt::println("[Engine:Info] Engine has finished running after {} seconds!", timer.endTimer()); #endif } +//====================== +//=== Init Functions === +//====================== + void Engine::initRender() { initVulkan(); initSwapchain(); @@ -348,11 +368,12 @@ void Engine::initRender() { //initFramebuffers(); initCommands(); initSyncStructures(); + initDescriptors(); } void Engine::initVulkan() { #ifdef DEBUG - fmt::println("Initializing Vulkan..."); + fmt::println("[Engine:Init] Initializing Vulkan..."); #endif vkb::InstanceBuilder builder; @@ -371,7 +392,7 @@ void Engine::initVulkan() { // Create the surface if (glfwCreateWindowSurface(_instance, window, nullptr, & _surface) != VK_SUCCESS) { - throw std::runtime_error("Failed to create window surface !\nAborting..."); + throw std::runtime_error("[Engine:Error] Failed to create window surface !\nAborting..."); } // Features @@ -400,7 +421,7 @@ void Engine::initVulkan() { vkb::DeviceBuilder deviceBuilder { physicalDevice }; #ifdef DEBUG - fmt::println("Device {}", physicalDevice.name); + fmt::println("[Engine:Info] Device {}", physicalDevice.name); #endif vkb::Device vkbDevice { deviceBuilder.build().value() }; @@ -423,13 +444,13 @@ void Engine::initVulkan() { }); #ifdef DEBUG - fmt::println("Vulkan initialized !"); + fmt::println("[Engine:Init] Vulkan initialized !"); #endif } void Engine::initSwapchain() { #ifdef DEBUG - fmt::println("Initializing the swapchain..."); + fmt::println("[Engine:Init] Initializing the swapchain..."); #endif createSwapchain(_windowExtent.width, _windowExtent.height); @@ -507,13 +528,13 @@ void Engine::initSwapchain() { }); #ifdef DEBUG - fmt::println("Swapchain initialized !"); + fmt::println("[Engine:Init] Swapchain initialized !"); #endif } void Engine::initCommands() { #ifdef DEBUG - fmt::println("Initializing vulkan commands.."); + fmt::println("[Engine:Init] Initializing Vulkan commands.."); #endif VkCommandPoolCreateInfo commandPoolInfo = { @@ -544,13 +565,13 @@ void Engine::initCommands() { ); } #ifdef DEBUG - fmt::println("Vulan commands initialized !"); + fmt::println("[Engine:Init] Vulkan commands initialized !"); #endif } void Engine::initSyncStructures() { #ifdef DEBUG - fmt::println("Creating sync structures..."); + fmt::println("[Engine:Init] Creating sync structures..."); #endif VkFenceCreateInfo fenceCreateInfo = vkinit::fenceCreateInfo(VK_FENCE_CREATE_SIGNALED_BIT); @@ -564,16 +585,87 @@ void Engine::initSyncStructures() { } #ifdef DEBUG - fmt::println("Sync structures created !"); + fmt::println("[Engine:Init] Sync structures created !"); #endif } +void Engine::initDescriptors(){ + #ifdef DEBUG + fmt::println("[Engine:Init] Initializing descriptors..."); + #endif + + std::vector sizes = { + { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1 } + }; + + globalDescriptorAllocator.initPool( + _device, + 10, + sizes + ); + + { + DescriptorLayoutBuilder builder; + builder.addBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE); + _drawImageDescriptorLayout = builder.build( + _device, + VK_SHADER_STAGE_COMPUTE_BIT + ); + } + + _drawImageDescriptors = globalDescriptorAllocator.allocate( + _device, + _drawImageDescriptorLayout + ); + + VkDescriptorImageInfo imgInfo { + .imageView = _drawImage.imageView, + .imageLayout = VK_IMAGE_LAYOUT_GENERAL + }; + + VkWriteDescriptorSet drawImageWrite = { + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .pNext = nullptr, + .dstSet = _drawImageDescriptors, + .dstBinding = 0, + .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, + .pImageInfo = &imgInfo + }; + + vkUpdateDescriptorSets( + _device, + 1, + &drawImageWrite, + 0, + nullptr + ); + + _mainDeletionQueue.pushFunction([this]() { + globalDescriptorAllocator.destroyPool(_device); + + vkDestroyDescriptorSetLayout( + _device, + _drawImageDescriptorLayout, + nullptr + ); + }); + + #ifdef DEBUG + fmt::println("[Engine:Init] Descriptors initialized !"); + #endif +} + +//================================ +//=== Render-related Functions === +//================================ + void Engine::createSwapchain( uint32_t width, uint32_t height ) { #ifdef DEBUG - fmt::println("Creating the Swapchain..."); + fmt::println("[Engine:Init] Creating the Swapchain..."); #endif vkb::SwapchainBuilder swapchainBuilder { _choosenGPU, @@ -603,13 +695,13 @@ void Engine::createSwapchain( _swapchainImageViews = { vkbSwapchain.get_image_views().value() }; #ifdef DEBUG - fmt::println("Swapchain created with dimentions {} by {}", vkbSwapchain.extent.width, vkbSwapchain.extent.height); + fmt::println("[Engine:Info] Swapchain created with dimentions {} by {}", vkbSwapchain.extent.width, vkbSwapchain.extent.height); #endif } void Engine::destroySwapchain() { #ifdef DEBUG - fmt::println("Destroying the swapchain..."); + fmt::println("[Engine:Cleanup] Destroying the swapchain..."); #endif vkDestroySwapchainKHR(_device, _swapchain, nullptr); @@ -618,7 +710,7 @@ void Engine::destroySwapchain() { } #ifdef DEBUG - fmt::println("Swapchain destroyed !"); + fmt::println("[Engine:Cleanup]Swapchain destroyed !"); #endif } diff --git a/src/Engine/engine/images.h b/src/Engine/engine/images.h index 39ebcfc..21af43e 100644 --- a/src/Engine/engine/images.h +++ b/src/Engine/engine/images.h @@ -1,7 +1,7 @@ #ifndef IMAGES_H #define IMAGES_H // Normal header -#include "Common/types.h" +#include "Common/Types.h" namespace vkutil { void transitionImage( diff --git a/src/Engine/engine/initializers.h b/src/Engine/engine/initializers.h index 7aa37e7..517ae62 100644 --- a/src/Engine/engine/initializers.h +++ b/src/Engine/engine/initializers.h @@ -1,7 +1,7 @@ #ifndef INITIALIZERS_H #define INITIALIZERS_H // Normal header -#include "Common/types.h" +#include "Common/Types.h" namespace vkinit { VkCommandPoolCreateInfo commandPoolCreateInfo( diff --git a/src/Server/main.cpp b/src/Server/main.cpp index 4cce7f6..1c1fbf1 100644 --- a/src/Server/main.cpp +++ b/src/Server/main.cpp @@ -1,3 +1,10 @@ +#define SERVER_IMPL +#include "server/server.h" + int main() { + GameServer mainServer; + + mainServer.init(); + return 0; } diff --git a/src/Server/server/init.h b/src/Server/server/init.h new file mode 100644 index 0000000..3d7cb07 --- /dev/null +++ b/src/Server/server/init.h @@ -0,0 +1,43 @@ +#ifndef SERVER_INIT_H +#define SERVER_INIT_H +// Normal header + +class GameServerInit { + // This class groups all the code required for + // the server to initialize and start being + // usable + public: + bool init(); // Uses a bool to mark if init was sucessfull + + void cleanup(); + private: +}; + +#endif + +#ifdef SERVER_INIT_IMPL +#ifndef SERVER_INIT_IMPL_H +#define SERVER_INIT_IMPL_H +// Implementation + +#include "Common/Types.h" + +bool GameServerInit::init() { + // Create the logger server object, + // to send messages + // Try to compile but I don't think it'll work in the current state + NetLogger::Server logServer { + .priority = 1, + .url = "https://ntfy.alexdelcamp.fr" + // clangd gives an error, fix it using the corrext type ig + // Todo : read the server from a file and fallback to my own + }; + + NetLogger::initNetLogger(logServer); + + // Add error detection (no connectivity for exemple) + return true; // Everything went correctly +} + +#endif +#endif diff --git a/src/Server/server/server.h b/src/Server/server/server.h new file mode 100644 index 0000000..e3fc6e7 --- /dev/null +++ b/src/Server/server/server.h @@ -0,0 +1,28 @@ +#ifndef SERVER_H +#define SERVER_H +// Normal header + +class GameServer { + public: + void init(); // Wrapper to the init function in the init class + + void cleanup(); // Wrapper to the cleanup function in the init class + private: +}; + +#endif + +#ifdef SERVER_IMPL +#ifndef SERVER_IMPL_H +#define SERVER_IMPL_H +// Implementation + +#include "init.h" + +void GameServer::init(){ + //GameServerInit::init(); + +} + +#endif +#endif