From 5ca1ac9035be8cd534f527021dd6263459798865 Mon Sep 17 00:00:00 2001 From: JonnyDevp Date: Thu, 24 Apr 2025 13:57:26 +0300 Subject: [PATCH 1/4] Reorganize VulkanEngine class structure for better readability --- src/include/graphics/vulkan/vk_engine.h | 161 +++++++++++++++--------- 1 file changed, 105 insertions(+), 56 deletions(-) diff --git a/src/include/graphics/vulkan/vk_engine.h b/src/include/graphics/vulkan/vk_engine.h index 3124bfc7..6025cee3 100644 --- a/src/include/graphics/vulkan/vk_engine.h +++ b/src/include/graphics/vulkan/vk_engine.h @@ -16,13 +16,17 @@ #include #include +#include "vk_command_buffers.h" #include "vk_descriptors.h" #include "vk_types.h" +<<<<<<< HEAD #include "pipelines.h" #include "ComputePipeline.h" #include "vk_command_buffers.h" +======= +>>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) class Camera; class VulkanEngine; @@ -30,10 +34,55 @@ struct DrawContext; struct LoadedGLTF; struct MeshAsset; +<<<<<<< HEAD constexpr unsigned int FRAME_OVERLAP = 2; +======= +// Maximum number of frames that can be processed concurrently +constexpr unsigned int FRAME_OVERLAP = 2; + +/** + * GLTFMetallic_Roughness - Structure for handling PBR materials from GLTF + * models + */ +struct GLTFMetallic_Roughness { + MaterialPipeline opaquePipeline; + MaterialPipeline transparentPipeline; + + VkDescriptorSetLayout materialLayout; + + struct MaterialConstants { + glm::vec4 colorFactors; + glm::vec4 metal_rough_factors; + // padding, we need it anyway for uniform buffers + glm::vec4 extra[14]; + }; + + struct MaterialResources { + AllocatedImage colorImage; + VkSampler colorSampler; + AllocatedImage metalRoughImage; + VkSampler metalRoughSampler; + VkBuffer dataBuffer; + uint32_t dataBufferOffset; + }; + + DescriptorWriter writer; + void build_pipelines(VulkanEngine* engine); + void clear_resources(VkDevice device); + + MaterialInstance write_material( + VkDevice device, MaterialPass pass, + const MaterialResources& resources, + DescriptorAllocatorGrowable& descriptorAllocator); +}; +>>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) + +/** + * DeletionQueue - Helper structure for managing resource cleanup + */ struct DeletionQueue { std::deque> deletors; @@ -94,38 +143,63 @@ struct DrawContext { std::vector OpaqueSurfaces; }; +/** + * VulkanEngine - Main renderer class + */ class VulkanEngine { public: +<<<<<<< HEAD Pipelines pipelines; CommandBuffers command_buffers; +======= + // PUBLIC METHODS + static VulkanEngine& Get(); + + void init(struct SDL_Window* window); + void cleanup(); + void draw(); + void update(); + void update_scene(); +>>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) int64_t registerMesh(const std::string& filePath); void unregisterMesh(int64_t id); - void setMeshTransform(int64_t id, glm::mat4 mat); - std::unordered_map> meshes; + FrameData& get_current_frame() { + return _frames[_frameNumber % FRAME_OVERLAP]; + }; - std::unordered_map transforms; + GPUMeshBuffers uploadMesh(std::span indices, + std::span vertices); - std::unordered_map> loadedScenes; + 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; - Camera* mainCamera; + AllocatedBuffer create_buffer(size_t allocSize, VkBufferUsageFlags usage, + VmaMemoryUsage memoryUsage) const; - DrawContext mainDrawContext; + // PUBLIC FIELDS + CommandBuffers command_buffers; + + // Asset management + std::unordered_map> meshes; + std::unordered_map transforms; + std::unordered_map> loadedScenes; std::unordered_map> loadedNodes; - void update_scene(); + Camera* mainCamera; + DrawContext mainDrawContext; FrameData _frames[FRAME_OVERLAP]; - - FrameData& get_current_frame() { - return _frames[_frameNumber % FRAME_OVERLAP]; - }; - VkQueue _graphicsQueue; uint32_t _graphicsQueueFamily; @@ -133,38 +207,23 @@ class VulkanEngine { unsigned int _frameNumber{0}; bool stop_rendering{false}; VkExtent2D _windowExtent{2560, 1440}; - struct SDL_Window* _window{nullptr}; - static VulkanEngine& Get(); - - // initializes everything in the engine - void init(struct SDL_Window* window); - - // shuts down the engine - void cleanup(); - - // draw loop - void draw(); - - // run main loop - void update(); - - 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 + // Vulkan instance and device + VkInstance _instance; + VkDebugUtilsMessengerEXT _debug_messenger; + VkPhysicalDevice _chosenGPU; + VkDevice _device; + VkSurfaceKHR _surface; + // Swapchain VkSwapchainKHR _swapchain; VkFormat _swapchainImageFormat; - std::vector _swapchainImages; std::vector _swapchainImageViews; VkExtent2D _swapchainExtent; DeletionQueue _mainDeletionQueue; - VmaAllocator _allocator; AllocatedImage _drawImage; @@ -173,37 +232,30 @@ class VulkanEngine { float renderScale = 1.f; DescriptorAllocatorGrowable globalDescriptorAllocator; - VkDescriptorSet _drawImageDescriptors; VkDescriptorSetLayout _drawImageDescriptorLayout; +<<<<<<< HEAD // immediate submit structures +======= + VkPipeline _gradientPipeline; + VkPipelineLayout _gradientPipelineLayout; + +>>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) VkFence _immFence; VkCommandBuffer _immCommandBuffer; VkCommandPool _immCommandPool; GPUMeshBuffers rectangle; - - GPUMeshBuffers uploadMesh(std::span indices, - std::span vertices); - std::vector> testMeshes; bool resize_requested; GPUSceneData sceneData; - VkDescriptorSetLayout _gpuSceneDataDescriptorLayout; - 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 textures and resources AllocatedImage _whiteImage; AllocatedImage _blackImage; AllocatedImage _greyImage; @@ -217,10 +269,8 @@ class VulkanEngine { MaterialInstance defaultData; GLTFMetallic_Roughness metalRoughMaterial; - AllocatedBuffer create_buffer(size_t allocSize, VkBufferUsageFlags usage, - VmaMemoryUsage memoryUsage) const; - private: + // Debug callback for validation layers static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, @@ -237,20 +287,19 @@ class VulkanEngine { void draw_background(VkCommandBuffer cmd) const; void init_descriptors(); - void init_pipelines(); void init_imgui(); +<<<<<<< HEAD +======= + void init_triangle_pipeline(); +>>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) void draw_imgui(VkCommandBuffer cmd, VkImageView targetImageView) const; - void draw_geometry(VkCommandBuffer cmd); void destroy_buffer(const AllocatedBuffer& buffer) const; - void resize_swapchain(); - void init_mesh_pipeline(); - void init_default_data(); }; \ No newline at end of file From f64a36f1bfd8111c482e97c24ff1d44ee39c3774 Mon Sep 17 00:00:00 2001 From: JonnyDevp Date: Thu, 24 Apr 2025 15:06:38 +0300 Subject: [PATCH 2/4] Reorganize VulkanEngine class structure: move public members to private, fix GLTFMetallic_Roughness redefinition --- src/include/graphics/vulkan/vk_engine.h | 163 ++++++++++++++++++++---- 1 file changed, 141 insertions(+), 22 deletions(-) diff --git a/src/include/graphics/vulkan/vk_engine.h b/src/include/graphics/vulkan/vk_engine.h index 6025cee3..de3657e2 100644 --- a/src/include/graphics/vulkan/vk_engine.h +++ b/src/include/graphics/vulkan/vk_engine.h @@ -16,24 +16,29 @@ #include #include +#include "ComputePipeline.h" +#include "pipelines.h" // Contains GLTFMetallic_Roughness definition #include "vk_command_buffers.h" #include "vk_descriptors.h" #include "vk_types.h" <<<<<<< HEAD +<<<<<<< HEAD #include "pipelines.h" #include "ComputePipeline.h" +======= +>>>>>>> 14bd247 (Reorganize VulkanEngine class structure: move public members to private, fix GLTFMetallic_Roughness redefinition) #include "vk_command_buffers.h" ======= >>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) class Camera; -class VulkanEngine; struct DrawContext; struct LoadedGLTF; struct MeshAsset; +<<<<<<< HEAD <<<<<<< HEAD constexpr unsigned int FRAME_OVERLAP = 2; @@ -83,6 +88,10 @@ struct GLTFMetallic_Roughness { /** * DeletionQueue - Helper structure for managing resource cleanup */ +======= +constexpr unsigned int FRAME_OVERLAP = 2; + +>>>>>>> 14bd247 (Reorganize VulkanEngine class structure: move public members to private, fix GLTFMetallic_Roughness redefinition) struct DeletionQueue { std::deque> deletors; @@ -143,9 +152,6 @@ struct DrawContext { std::vector OpaqueSurfaces; }; -/** - * VulkanEngine - Main renderer class - */ class VulkanEngine { public: <<<<<<< HEAD @@ -155,39 +161,52 @@ class VulkanEngine { CommandBuffers command_buffers; ======= // PUBLIC METHODS + + // Singleton accessor static VulkanEngine& Get(); + // Main lifecycle methods void init(struct SDL_Window* window); void cleanup(); void draw(); void update(); +<<<<<<< HEAD void update_scene(); >>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) +======= +>>>>>>> 14bd247 (Reorganize VulkanEngine class structure: move public members to private, fix GLTFMetallic_Roughness redefinition) + + // Current frame access + FrameData& get_current_frame() { + return _frames[_frameNumber % FRAME_OVERLAP]; + } + // Mesh management methods int64_t registerMesh(const std::string& filePath); void unregisterMesh(int64_t id); void setMeshTransform(int64_t id, glm::mat4 mat); - FrameData& get_current_frame() { - return _frames[_frameNumber % FRAME_OVERLAP]; - }; - - GPUMeshBuffers uploadMesh(std::span indices, - std::span vertices); + // Scene management methods + void update_scene(); + // Resource creation methods (can be called from other classes) 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; - AllocatedBuffer create_buffer(size_t allocSize, VkBufferUsageFlags usage, VmaMemoryUsage memoryUsage) const; + // Resource management methods + void destroy_image(const AllocatedImage& img) const; + GPUMeshBuffers uploadMesh(std::span indices, + std::span vertices); + // PUBLIC FIELDS +<<<<<<< HEAD CommandBuffers command_buffers; // Asset management @@ -269,37 +288,137 @@ class VulkanEngine { MaterialInstance defaultData; GLTFMetallic_Roughness metalRoughMaterial; +======= + + // Display settings + VkExtent2D _windowExtent{2560, 1440}; + float renderScale = 1.f; + bool resize_requested; + + // Main interaction objects with other systems + Camera* mainCamera; + struct SDL_Window* _window{nullptr}; + + // State flags + bool _isInitialized{false}; + bool stop_rendering{false}; + + // Core components + Pipelines pipelines; + CommandBuffers command_buffers; + +>>>>>>> 14bd247 (Reorganize VulkanEngine class structure: move public members to private, fix GLTFMetallic_Roughness redefinition) private: - // Debug callback for validation layers + // PRIVATE METHODS + + // Vulkan debug callback function static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData); + // Internal initialization methods 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(); <<<<<<< HEAD +<<<<<<< HEAD ======= void init_triangle_pipeline(); >>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) +======= + void init_mesh_pipeline(); + void init_default_data(); +>>>>>>> 14bd247 (Reorganize VulkanEngine class structure: move public members to private, fix GLTFMetallic_Roughness redefinition) + // Rendering methods + void draw_background(VkCommandBuffer cmd) const; void draw_imgui(VkCommandBuffer cmd, VkImageView targetImageView) const; void draw_geometry(VkCommandBuffer cmd); - void destroy_buffer(const AllocatedBuffer& buffer) const; + // Swapchain management methods + void create_swapchain(uint32_t width, uint32_t height); + void destroy_swapchain(); void resize_swapchain(); - void init_mesh_pipeline(); - void init_default_data(); + + // Memory management method + void destroy_buffer(const AllocatedBuffer& buffer) const; + + // PRIVATE FIELDS + + // Frame data + FrameData _frames[FRAME_OVERLAP]; + unsigned int _frameNumber{0}; + + // Vulkan instances and devices + VkInstance _instance; + VkDebugUtilsMessengerEXT _debug_messenger; + VkPhysicalDevice _chosenGPU; + VkDevice _device; + VkSurfaceKHR _surface; + + // Queues and queue families + VkQueue _graphicsQueue; + uint32_t _graphicsQueueFamily; + + // Swapchain and its resources + VkSwapchainKHR _swapchain; + VkFormat _swapchainImageFormat; + std::vector _swapchainImages; + std::vector _swapchainImageViews; + VkExtent2D _swapchainExtent; + VkExtent2D _drawExtent; + + // Memory and object management + VmaAllocator _allocator; + DeletionQueue _mainDeletionQueue; + + // Image resources + AllocatedImage _drawImage; + AllocatedImage _depthImage; + AllocatedImage _whiteImage; + AllocatedImage _blackImage; + AllocatedImage _greyImage; + AllocatedImage _errorCheckerboardImage; + + // Samplers + VkSampler _defaultSamplerLinear; + VkSampler _defaultSamplerNearest; + + // Descriptors + VkDescriptorSet _drawImageDescriptors; + VkDescriptorSetLayout _drawImageDescriptorLayout; + VkDescriptorSetLayout _gpuSceneDataDescriptorLayout; + VkDescriptorSetLayout _singleImageDescriptorLayout; + DescriptorAllocatorGrowable globalDescriptorAllocator; + + // Pipeline objects + VkPipeline _gradientPipeline; + VkPipelineLayout _gradientPipelineLayout; + + // Immediate command submission structures + VkFence _immFence; + VkCommandBuffer _immCommandBuffer; + VkCommandPool _immCommandPool; + + // Scene and object data + GPUSceneData sceneData; + GPUMeshBuffers rectangle; + DrawContext mainDrawContext; + + // Materials + MaterialInstance defaultData; + GLTFMetallic_Roughness metalRoughMaterial; + + // Collections of loaded objects + std::unordered_map> meshes; + std::unordered_map transforms; + std::unordered_map> loadedScenes; + std::unordered_map> loadedNodes; + std::vector> testMeshes; }; \ No newline at end of file From 2ff6f9d05e1d162c1e59e947d5f82fef6674e3d9 Mon Sep 17 00:00:00 2001 From: JonnyDevp Date: Thu, 15 May 2025 11:39:31 +0300 Subject: [PATCH 3/4] Reorganize VulkanEngine class members and fix build issues --- src/include/graphics/vulkan/vk_engine.h | 253 +++++------------------- 1 file changed, 48 insertions(+), 205 deletions(-) diff --git a/src/include/graphics/vulkan/vk_engine.h b/src/include/graphics/vulkan/vk_engine.h index de3657e2..689b98fd 100644 --- a/src/include/graphics/vulkan/vk_engine.h +++ b/src/include/graphics/vulkan/vk_engine.h @@ -16,82 +16,29 @@ #include #include +// Core includes for the Vulkan engine #include "ComputePipeline.h" #include "pipelines.h" // Contains GLTFMetallic_Roughness definition #include "vk_command_buffers.h" #include "vk_descriptors.h" #include "vk_types.h" -<<<<<<< HEAD -<<<<<<< HEAD - -#include "pipelines.h" -#include "ComputePipeline.h" -======= ->>>>>>> 14bd247 (Reorganize VulkanEngine class structure: move public members to private, fix GLTFMetallic_Roughness redefinition) - -#include "vk_command_buffers.h" -======= ->>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) +// Forward declarations class Camera; +// Note: VulkanEngine is the class we are defining, no forward declaration +// needed here struct DrawContext; struct LoadedGLTF; struct MeshAsset; -<<<<<<< HEAD -<<<<<<< HEAD - -constexpr unsigned int FRAME_OVERLAP = 2; - -======= // Maximum number of frames that can be processed concurrently constexpr unsigned int FRAME_OVERLAP = 2; -/** - * GLTFMetallic_Roughness - Structure for handling PBR materials from GLTF - * models - */ -struct GLTFMetallic_Roughness { - MaterialPipeline opaquePipeline; - MaterialPipeline transparentPipeline; - - VkDescriptorSetLayout materialLayout; - - struct MaterialConstants { - glm::vec4 colorFactors; - glm::vec4 metal_rough_factors; - // padding, we need it anyway for uniform buffers - glm::vec4 extra[14]; - }; - - struct MaterialResources { - AllocatedImage colorImage; - VkSampler colorSampler; - AllocatedImage metalRoughImage; - VkSampler metalRoughSampler; - VkBuffer dataBuffer; - uint32_t dataBufferOffset; - }; - - DescriptorWriter writer; - - void build_pipelines(VulkanEngine* engine); - void clear_resources(VkDevice device); - - MaterialInstance write_material( - VkDevice device, MaterialPass pass, - const MaterialResources& resources, - DescriptorAllocatorGrowable& descriptorAllocator); -}; ->>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) +// Note: GLTFMetallic_Roughness struct definition is likely in pipelines.h /** * DeletionQueue - Helper structure for managing resource cleanup */ -======= -constexpr unsigned int FRAME_OVERLAP = 2; - ->>>>>>> 14bd247 (Reorganize VulkanEngine class structure: move public members to private, fix GLTFMetallic_Roughness redefinition) struct DeletionQueue { std::deque> deletors; @@ -100,9 +47,9 @@ struct DeletionQueue { } void flush() { - // reverse iterate the deletion queue to execute all the functions + // Reverse iterate the deletion queue to execute all the functions for (auto& deletor : std::ranges::reverse_view(deletors)) { - deletor(); // call functors + deletor(); // Call functors } deletors.clear(); @@ -152,45 +99,36 @@ struct DrawContext { std::vector OpaqueSurfaces; }; +/** + * VulkanEngine - Main renderer class + */ class VulkanEngine { public: -<<<<<<< HEAD - - Pipelines pipelines; - - CommandBuffers command_buffers; -======= // PUBLIC METHODS - // Singleton accessor + // Static Methods static VulkanEngine& Get(); - // Main lifecycle methods + // Initialization and Cleanup void init(struct SDL_Window* window); void cleanup(); - void draw(); + + // Main Loop void update(); -<<<<<<< HEAD - void update_scene(); ->>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) -======= ->>>>>>> 14bd247 (Reorganize VulkanEngine class structure: move public members to private, fix GLTFMetallic_Roughness redefinition) + void draw(); // Current frame access - FrameData& get_current_frame() { - return _frames[_frameNumber % FRAME_OVERLAP]; - } + FrameData& get_current_frame(); - // Mesh management methods + // Core Functionality - Scene and Mesh Management int64_t registerMesh(const std::string& filePath); - void unregisterMesh(int64_t id); void setMeshTransform(int64_t id, glm::mat4 mat); - - // Scene management methods void update_scene(); + GPUMeshBuffers uploadMesh(std::span indices, + std::span vertices); - // Resource creation methods (can be called from other classes) + // Core Functionality - Resource Creation AllocatedImage create_image(VkExtent3D size, VkFormat format, VkImageUsageFlags usage, bool mipmapped = false) const; @@ -200,114 +138,44 @@ class VulkanEngine { AllocatedBuffer create_buffer(size_t allocSize, VkBufferUsageFlags usage, VmaMemoryUsage memoryUsage) const; - // Resource management methods + // Resource management methods (public destruction for images) void destroy_image(const AllocatedImage& img) const; - GPUMeshBuffers uploadMesh(std::span indices, - std::span vertices); // PUBLIC FIELDS -<<<<<<< HEAD - CommandBuffers command_buffers; - - // Asset management - std::unordered_map> meshes; - std::unordered_map transforms; - std::unordered_map> loadedScenes; - std::unordered_map> loadedNodes; - Camera* mainCamera; - DrawContext mainDrawContext; - - FrameData _frames[FRAME_OVERLAP]; - VkQueue _graphicsQueue; - uint32_t _graphicsQueueFamily; - - bool _isInitialized{false}; - unsigned int _frameNumber{0}; - bool stop_rendering{false}; + // Display settings VkExtent2D _windowExtent{2560, 1440}; - struct SDL_Window* _window{nullptr}; - - // Vulkan instance and device - VkInstance _instance; - VkDebugUtilsMessengerEXT _debug_messenger; - VkPhysicalDevice _chosenGPU; - VkDevice _device; - VkSurfaceKHR _surface; - - // Swapchain - VkSwapchainKHR _swapchain; - VkFormat _swapchainImageFormat; - std::vector _swapchainImages; - std::vector _swapchainImageViews; - VkExtent2D _swapchainExtent; - - DeletionQueue _mainDeletionQueue; - VmaAllocator _allocator; - - AllocatedImage _drawImage; - AllocatedImage _depthImage; - VkExtent2D _drawExtent; float renderScale = 1.f; + bool resize_requested; + struct SDL_Window* _window{nullptr}; // Interaction object + // Core components (Managers and shared Vulkan objects) + Pipelines pipelines; + CommandBuffers command_buffers; DescriptorAllocatorGrowable globalDescriptorAllocator; - VkDescriptorSet _drawImageDescriptors; - VkDescriptorSetLayout _drawImageDescriptorLayout; -<<<<<<< HEAD - // immediate submit structures -======= - VkPipeline _gradientPipeline; - VkPipelineLayout _gradientPipelineLayout; - ->>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) + // Immediate command submission structures VkFence _immFence; VkCommandBuffer _immCommandBuffer; VkCommandPool _immCommandPool; - - GPUMeshBuffers rectangle; - std::vector> testMeshes; - - bool resize_requested; - + // Scene and object data GPUSceneData sceneData; - VkDescriptorSetLayout _gpuSceneDataDescriptorLayout; - - // Default textures and resources - AllocatedImage _whiteImage; - AllocatedImage _blackImage; - AllocatedImage _greyImage; - AllocatedImage _errorCheckerboardImage; - - VkSampler _defaultSamplerLinear; - VkSampler _defaultSamplerNearest; - - VkDescriptorSetLayout _singleImageDescriptorLayout; + GPUMeshBuffers rectangle; + DrawContext mainDrawContext; + Camera* mainCamera; // Interaction object + // Materials (Struct definition is elsewhere, likely pipelines.h) MaterialInstance defaultData; GLTFMetallic_Roughness metalRoughMaterial; -======= - - // Display settings - VkExtent2D _windowExtent{2560, 1440}; - float renderScale = 1.f; - bool resize_requested; - - // Main interaction objects with other systems - Camera* mainCamera; - struct SDL_Window* _window{nullptr}; - - // State flags - bool _isInitialized{false}; - bool stop_rendering{false}; - - // Core components - Pipelines pipelines; - CommandBuffers command_buffers; + // Collections of loaded objects (Asset management) + std::unordered_map> meshes; + std::unordered_map transforms; + std::unordered_map> loadedScenes; + std::unordered_map> loadedNodes; + std::vector> testMeshes; ->>>>>>> 14bd247 (Reorganize VulkanEngine class structure: move public members to private, fix GLTFMetallic_Roughness redefinition) private: // PRIVATE METHODS @@ -325,16 +193,9 @@ class VulkanEngine { void init_descriptors(); void init_pipelines(); void init_imgui(); -<<<<<<< HEAD -<<<<<<< HEAD - -======= - void init_triangle_pipeline(); ->>>>>>> e10dd8c (Reorganize VulkanEngine class structure for better readability) -======= + void init_triangle_pipeline(); // Added from e10dd8c void init_mesh_pipeline(); void init_default_data(); ->>>>>>> 14bd247 (Reorganize VulkanEngine class structure: move public members to private, fix GLTFMetallic_Roughness redefinition) // Rendering methods void draw_background(VkCommandBuffer cmd) const; @@ -346,11 +207,15 @@ class VulkanEngine { void destroy_swapchain(); void resize_swapchain(); - // Memory management method + // Memory management method (private destruction for buffers) void destroy_buffer(const AllocatedBuffer& buffer) const; // PRIVATE FIELDS + // State flags + bool _isInitialized{false}; + bool stop_rendering{false}; + // Frame data FrameData _frames[FRAME_OVERLAP]; unsigned int _frameNumber{0}; @@ -378,7 +243,7 @@ class VulkanEngine { VmaAllocator _allocator; DeletionQueue _mainDeletionQueue; - // Image resources + // Image resources (private images/textures) AllocatedImage _drawImage; AllocatedImage _depthImage; AllocatedImage _whiteImage; @@ -390,35 +255,13 @@ class VulkanEngine { VkSampler _defaultSamplerLinear; VkSampler _defaultSamplerNearest; - // Descriptors + // Descriptors (private descriptor sets/layouts) VkDescriptorSet _drawImageDescriptors; VkDescriptorSetLayout _drawImageDescriptorLayout; VkDescriptorSetLayout _gpuSceneDataDescriptorLayout; VkDescriptorSetLayout _singleImageDescriptorLayout; - DescriptorAllocatorGrowable globalDescriptorAllocator; - // Pipeline objects + // Pipeline objects (private pipelines/layouts) VkPipeline _gradientPipeline; VkPipelineLayout _gradientPipelineLayout; - - // Immediate command submission structures - VkFence _immFence; - VkCommandBuffer _immCommandBuffer; - VkCommandPool _immCommandPool; - - // Scene and object data - GPUSceneData sceneData; - GPUMeshBuffers rectangle; - DrawContext mainDrawContext; - - // Materials - MaterialInstance defaultData; - GLTFMetallic_Roughness metalRoughMaterial; - - // Collections of loaded objects - std::unordered_map> meshes; - std::unordered_map transforms; - std::unordered_map> loadedScenes; - std::unordered_map> loadedNodes; - std::vector> testMeshes; }; \ No newline at end of file From c547230fb22ca83610be5eef576eff0f0bef1fe6 Mon Sep 17 00:00:00 2001 From: JonnyDevp Date: Thu, 15 May 2025 15:26:48 +0300 Subject: [PATCH 4/4] Some upd --- src/include/graphics/vulkan/vk_engine.h | 219 +++++++++++------------- 1 file changed, 104 insertions(+), 115 deletions(-) diff --git a/src/include/graphics/vulkan/vk_engine.h b/src/include/graphics/vulkan/vk_engine.h index 689b98fd..6fe251bd 100644 --- a/src/include/graphics/vulkan/vk_engine.h +++ b/src/include/graphics/vulkan/vk_engine.h @@ -16,29 +16,20 @@ #include #include -// Core includes for the Vulkan engine #include "ComputePipeline.h" -#include "pipelines.h" // Contains GLTFMetallic_Roughness definition +#include "pipelines.h" #include "vk_command_buffers.h" #include "vk_descriptors.h" #include "vk_types.h" -// Forward declarations class Camera; -// Note: VulkanEngine is the class we are defining, no forward declaration -// needed here +class VulkanEngine; struct DrawContext; struct LoadedGLTF; struct MeshAsset; -// Maximum number of frames that can be processed concurrently constexpr unsigned int FRAME_OVERLAP = 2; -// Note: GLTFMetallic_Roughness struct definition is likely in pipelines.h - -/** - * DeletionQueue - Helper structure for managing resource cleanup - */ struct DeletionQueue { std::deque> deletors; @@ -47,9 +38,9 @@ struct DeletionQueue { } void flush() { - // Reverse iterate the deletion queue to execute all the functions + // reverse iterate the deletion queue to execute all the functions for (auto& deletor : std::ranges::reverse_view(deletors)) { - deletor(); // Call functors + deletor(); // call functors } deletors.clear(); @@ -99,169 +90,167 @@ struct DrawContext { std::vector OpaqueSurfaces; }; -/** - * VulkanEngine - Main renderer class - */ class VulkanEngine { public: + // ------------------------------------------------- // PUBLIC METHODS + // ------------------------------------------------- - // Static Methods + // Static access method static VulkanEngine& Get(); - // Initialization and Cleanup + // Core engine methods void init(struct SDL_Window* window); void cleanup(); - - // Main Loop - void update(); void draw(); + void update(); + void update_scene(); - // Current frame access - FrameData& get_current_frame(); - - // Core Functionality - Scene and Mesh Management + // Resource management methods int64_t registerMesh(const std::string& filePath); void unregisterMesh(int64_t id); void setMeshTransform(int64_t id, glm::mat4 mat); - 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; - // Core Functionality - Resource Creation + // 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; - AllocatedBuffer create_buffer(size_t allocSize, VkBufferUsageFlags usage, - VmaMemoryUsage memoryUsage) const; - - // Resource management methods (public destruction for images) void destroy_image(const AllocatedImage& img) const; - // PUBLIC FIELDS + // Frame management + FrameData& get_current_frame() { + return _frames[_frameNumber % FRAME_OVERLAP]; + } + + // ------------------------------------------------- + // PUBLIC MEMBER VARIABLES + // ------------------------------------------------- - // Display settings + // Engine state + bool _isInitialized{false}; + unsigned int _frameNumber{0}; + bool stop_rendering{false}; VkExtent2D _windowExtent{2560, 1440}; - float renderScale = 1.f; + struct SDL_Window* _window{nullptr}; bool resize_requested; - struct SDL_Window* _window{nullptr}; // Interaction object - // Core components (Managers and shared Vulkan objects) + // Render components Pipelines pipelines; CommandBuffers command_buffers; - DescriptorAllocatorGrowable globalDescriptorAllocator; - - // Immediate command submission structures - VkFence _immFence; - VkCommandBuffer _immCommandBuffer; - VkCommandPool _immCommandPool; - - // Scene and object data - GPUSceneData sceneData; - GPUMeshBuffers rectangle; + Camera* mainCamera; DrawContext mainDrawContext; - Camera* mainCamera; // Interaction object - - // Materials (Struct definition is elsewhere, likely pipelines.h) - MaterialInstance defaultData; - GLTFMetallic_Roughness metalRoughMaterial; + GPUSceneData sceneData; + float renderScale = 1.f; - // Collections of loaded objects (Asset management) + // Resource collections std::unordered_map> meshes; std::unordered_map transforms; std::unordered_map> loadedScenes; std::unordered_map> loadedNodes; std::vector> testMeshes; -private: - // PRIVATE METHODS - - // Vulkan debug callback function - static VKAPI_ATTR VkBool32 VKAPI_CALL - debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, - VkDebugUtilsMessageTypeFlagsEXT messageType, - const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, - void* pUserData); - - // Internal initialization methods - void init_vulkan(); - void init_swapchain(); - void init_sync_structures(); - void init_descriptors(); - void init_pipelines(); - void init_imgui(); - void init_triangle_pipeline(); // Added from e10dd8c - void init_mesh_pipeline(); - void init_default_data(); - - // Rendering methods - void draw_background(VkCommandBuffer cmd) const; - void draw_imgui(VkCommandBuffer cmd, VkImageView targetImageView) const; - void draw_geometry(VkCommandBuffer cmd); - - // Swapchain management methods - void create_swapchain(uint32_t width, uint32_t height); - void destroy_swapchain(); - void resize_swapchain(); - - // Memory management method (private destruction for buffers) - void destroy_buffer(const AllocatedBuffer& buffer) const; - - // PRIVATE FIELDS - - // State flags - bool _isInitialized{false}; - bool stop_rendering{false}; - - // Frame data + // Frame management FrameData _frames[FRAME_OVERLAP]; - unsigned int _frameNumber{0}; - // Vulkan instances and devices - VkInstance _instance; - VkDebugUtilsMessengerEXT _debug_messenger; - VkPhysicalDevice _chosenGPU; - VkDevice _device; - VkSurfaceKHR _surface; + // 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 - // Queues and queue families + // Queue info VkQueue _graphicsQueue; uint32_t _graphicsQueueFamily; - // Swapchain and its resources + // Memory allocator + VmaAllocator _allocator; + + // Swapchain VkSwapchainKHR _swapchain; VkFormat _swapchainImageFormat; std::vector _swapchainImages; std::vector _swapchainImageViews; VkExtent2D _swapchainExtent; - VkExtent2D _drawExtent; - // Memory and object management - VmaAllocator _allocator; + // Deletion queue DeletionQueue _mainDeletionQueue; - // Image resources (private images/textures) + // Rendering resources AllocatedImage _drawImage; AllocatedImage _depthImage; + VkExtent2D _drawExtent; + + // Immediate submission resources + VkFence _immFence; + VkCommandBuffer _immCommandBuffer; + VkCommandPool _immCommandPool; + + // Geometry resources + GPUMeshBuffers rectangle; + + // Descriptor resources + DescriptorAllocatorGrowable globalDescriptorAllocator; + VkDescriptorSet _drawImageDescriptors; + VkDescriptorSetLayout _drawImageDescriptorLayout; + VkDescriptorSetLayout _gpuSceneDataDescriptorLayout; + VkDescriptorSetLayout _singleImageDescriptorLayout; + + // Default texture resources AllocatedImage _whiteImage; AllocatedImage _blackImage; AllocatedImage _greyImage; AllocatedImage _errorCheckerboardImage; - // Samplers + // Default sampler resources VkSampler _defaultSamplerLinear; VkSampler _defaultSamplerNearest; - // Descriptors (private descriptor sets/layouts) - VkDescriptorSet _drawImageDescriptors; - VkDescriptorSetLayout _drawImageDescriptorLayout; - VkDescriptorSetLayout _gpuSceneDataDescriptorLayout; - VkDescriptorSetLayout _singleImageDescriptorLayout; + // Material resources + MaterialInstance defaultData; + GLTFMetallic_Roughness metalRoughMaterial; + +private: + // ------------------------------------------------- + // PRIVATE METHODS + // ------------------------------------------------- - // Pipeline objects (private pipelines/layouts) - VkPipeline _gradientPipeline; - VkPipelineLayout _gradientPipelineLayout; + // Vulkan initialization helpers + void init_vulkan(); + void init_swapchain(); + void init_sync_structures(); + 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(); + + // 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; + + // Debug callback + static VKAPI_ATTR VkBool32 VKAPI_CALL + debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, + VkDebugUtilsMessageTypeFlagsEXT messageType, + const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, + void* pUserData); }; \ No newline at end of file