diff --git a/src/include/graphics/vulkan/vk_engine.h b/src/include/graphics/vulkan/vk_engine.h index 3124bfc7..6fe251bd 100644 --- a/src/include/graphics/vulkan/vk_engine.h +++ b/src/include/graphics/vulkan/vk_engine.h @@ -16,24 +16,20 @@ #include #include +#include "ComputePipeline.h" +#include "pipelines.h" +#include "vk_command_buffers.h" #include "vk_descriptors.h" #include "vk_types.h" -#include "pipelines.h" -#include "ComputePipeline.h" - -#include "vk_command_buffers.h" - class Camera; class VulkanEngine; struct DrawContext; struct LoadedGLTF; struct MeshAsset; - constexpr unsigned int FRAME_OVERLAP = 2; - struct DeletionQueue { std::deque> deletors; @@ -96,161 +92,165 @@ struct DrawContext { class VulkanEngine { public: + // ------------------------------------------------- + // PUBLIC METHODS + // ------------------------------------------------- - Pipelines pipelines; + // Static access method + static VulkanEngine& Get(); - CommandBuffers command_buffers; + // Core engine methods + void init(struct SDL_Window* window); + void cleanup(); + void draw(); + void update(); + void update_scene(); + // Resource management methods int64_t registerMesh(const std::string& filePath); - void unregisterMesh(int64_t id); - void setMeshTransform(int64_t id, glm::mat4 mat); - std::unordered_map> meshes; - - std::unordered_map transforms; - - std::unordered_map> loadedScenes; - - Camera* mainCamera; - - DrawContext mainDrawContext; - std::unordered_map> loadedNodes; - - void update_scene(); + // Buffer creation methods + GPUMeshBuffers uploadMesh(std::span indices, + std::span vertices); + AllocatedBuffer create_buffer(size_t allocSize, VkBufferUsageFlags usage, + VmaMemoryUsage memoryUsage) const; - FrameData _frames[FRAME_OVERLAP]; + // Image creation methods + AllocatedImage create_image(VkExtent3D size, VkFormat format, + VkImageUsageFlags usage, + bool mipmapped = false) const; + AllocatedImage create_image(const void* data, VkExtent3D size, + VkFormat format, VkImageUsageFlags usage, + bool mipmapped = false) const; + void destroy_image(const AllocatedImage& img) const; + // Frame management FrameData& get_current_frame() { return _frames[_frameNumber % FRAME_OVERLAP]; - }; + } - VkQueue _graphicsQueue; - uint32_t _graphicsQueueFamily; + // ------------------------------------------------- + // PUBLIC MEMBER VARIABLES + // ------------------------------------------------- + // Engine state bool _isInitialized{false}; unsigned int _frameNumber{0}; bool stop_rendering{false}; VkExtent2D _windowExtent{2560, 1440}; - struct SDL_Window* _window{nullptr}; + bool resize_requested; - static VulkanEngine& Get(); - - // initializes everything in the engine - void init(struct SDL_Window* window); - - // shuts down the engine - void cleanup(); + // Render components + Pipelines pipelines; + CommandBuffers command_buffers; + Camera* mainCamera; + DrawContext mainDrawContext; + GPUSceneData sceneData; + float renderScale = 1.f; - // draw loop - void draw(); + // Resource collections + std::unordered_map> meshes; + std::unordered_map transforms; + std::unordered_map> loadedScenes; + std::unordered_map> loadedNodes; + std::vector> testMeshes; - // run main loop - void update(); + // Frame management + FrameData _frames[FRAME_OVERLAP]; + // Vulkan core objects VkInstance _instance; // Vulkan library handle VkDebugUtilsMessengerEXT _debug_messenger; // Vulkan debug output handle VkPhysicalDevice _chosenGPU; // GPU chosen as the default device VkDevice _device; // Vulkan device for commands VkSurfaceKHR _surface; // Vulkan window surface + // Queue info + VkQueue _graphicsQueue; + uint32_t _graphicsQueueFamily; + + // Memory allocator + VmaAllocator _allocator; + + // Swapchain VkSwapchainKHR _swapchain; VkFormat _swapchainImageFormat; - std::vector _swapchainImages; std::vector _swapchainImageViews; VkExtent2D _swapchainExtent; + // Deletion queue DeletionQueue _mainDeletionQueue; - VmaAllocator _allocator; - + // Rendering resources AllocatedImage _drawImage; AllocatedImage _depthImage; VkExtent2D _drawExtent; - float renderScale = 1.f; - - DescriptorAllocatorGrowable globalDescriptorAllocator; - - VkDescriptorSet _drawImageDescriptors; - VkDescriptorSetLayout _drawImageDescriptorLayout; - // immediate submit structures + // Immediate submission resources VkFence _immFence; VkCommandBuffer _immCommandBuffer; VkCommandPool _immCommandPool; - + // Geometry resources GPUMeshBuffers rectangle; - GPUMeshBuffers uploadMesh(std::span indices, - std::span vertices); - - std::vector> testMeshes; - - bool resize_requested; - - GPUSceneData sceneData; - + // Descriptor resources + DescriptorAllocatorGrowable globalDescriptorAllocator; + VkDescriptorSet _drawImageDescriptors; + VkDescriptorSetLayout _drawImageDescriptorLayout; VkDescriptorSetLayout _gpuSceneDataDescriptorLayout; + VkDescriptorSetLayout _singleImageDescriptorLayout; - AllocatedImage create_image(VkExtent3D size, VkFormat format, - VkImageUsageFlags usage, - bool mipmapped = false) const; - AllocatedImage create_image(const void* data, VkExtent3D size, - VkFormat format, VkImageUsageFlags usage, - bool mipmapped = false) const; - void destroy_image(const AllocatedImage& img) const; - + // Default texture resources AllocatedImage _whiteImage; AllocatedImage _blackImage; AllocatedImage _greyImage; AllocatedImage _errorCheckerboardImage; + // Default sampler resources VkSampler _defaultSamplerLinear; VkSampler _defaultSamplerNearest; - VkDescriptorSetLayout _singleImageDescriptorLayout; - + // Material resources MaterialInstance defaultData; GLTFMetallic_Roughness metalRoughMaterial; - AllocatedBuffer create_buffer(size_t allocSize, VkBufferUsageFlags usage, - VmaMemoryUsage memoryUsage) const; - private: - static VKAPI_ATTR VkBool32 VKAPI_CALL - debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, - VkDebugUtilsMessageTypeFlagsEXT messageType, - const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, - void* pUserData); + // ------------------------------------------------- + // PRIVATE METHODS + // ------------------------------------------------- + // Vulkan initialization helpers void init_vulkan(); void init_swapchain(); void init_sync_structures(); - - void create_swapchain(uint32_t width, uint32_t height); - void destroy_swapchain(); - - void draw_background(VkCommandBuffer cmd) const; - void init_descriptors(); - void init_pipelines(); void init_imgui(); + void init_mesh_pipeline(); + void init_default_data(); + // Swapchain management + void create_swapchain(uint32_t width, uint32_t height); + void destroy_swapchain(); + void resize_swapchain(); - void draw_imgui(VkCommandBuffer cmd, VkImageView targetImageView) const; - + // Rendering helpers + void draw_background(VkCommandBuffer cmd) const; void draw_geometry(VkCommandBuffer cmd); + void draw_imgui(VkCommandBuffer cmd, VkImageView targetImageView) const; + // Resource management void destroy_buffer(const AllocatedBuffer& buffer) const; - void resize_swapchain(); - - void init_mesh_pipeline(); - - void init_default_data(); + // Debug callback + static VKAPI_ATTR VkBool32 VKAPI_CALL + debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, + VkDebugUtilsMessageTypeFlagsEXT messageType, + const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, + void* pUserData); }; \ No newline at end of file