// // Created by timo on 03.04.25. // #ifndef VULKAN_APP_H #define VULKAN_APP_H #define GLFW_INCLUDE_VULKAN #include #include #include #include #include #include #include #include #include #include #include #include namespace vapp{ const VkPresentModeKHR WISHED_PRESENT_MODE = VK_PRESENT_MODE_MAILBOX_KHR; typedef struct _QueueFamilyIndices{ std::optional graphicsFamily; std::optional presentFamily; bool isComplete(){ return graphicsFamily.has_value(); } } QueueFamilyIndices; typedef struct{ VkSurfaceCapabilitiesKHR capabilities; std::vector formats; std::vector presentModes; } SwapChainSupportDetails; static std::vector readFile(const std::string &filename); class Vulkan{ private: GLFWwindow *window; uint32_t _width, _height; VkInstance instance; VkDebugUtilsMessengerEXT debugMessenger; VkSurfaceKHR surface; VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; VkDevice device; VkQueue graphicsQueue; VkQueue presentQueue; VkSwapchainKHR swapChain; std::vector swapChainImages; VkFormat swapChainImageFormat; VkExtent2D swapChainExtent; std::vector swapChainImageViews; VkRenderPass renderPass; VkPipelineLayout pipelineLayout; VkPipeline graphicsPipeline; VkCommandPool commandPool; VkCommandBuffer commandBuffer; VkSemaphore imageAvailableSemaphore, renderFinishedSemaphore; VkFence inFlightFence; std::vector swapChainFramebuffers; bool checkValidationLayerSupport(); std::vector getRequiredExtensions(); void createInstance(); void setupDebugMessenger(); void pickPhysicalDevice(); void createLogicalDevice(); QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device); bool checkDeviceExtensionSupport(VkPhysicalDevice device); bool isDeviceSuitable(VkPhysicalDevice device); void createSurface(); SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device); VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector& availableFormats); VkPresentModeKHR chooseSwapPresentMode(const std::vector availablePresentModes); VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities); void createSwapChain(); void createImageViews(); void createGraphicsPipeline(); VkShaderModule createShaderModule(const std::vector &code); void createRenderPass(); void createFrameBuffers(); void createCommandPool(); void createCommandBuffer(); void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex); void createSyncObjects(); void drawFrame(); void initWindow(); void initVulkan(); void mainLoop(); void cleanup(); public: #ifdef DEBUG const bool enableValidationLayers = true; #else const bool enableValidationLayers = false; #endif void run(const uint32_t width = 800, const uint32_t height = 600); }; } // vapp #endif //VULKAN_APP_H