Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 91 additions & 91 deletions src/include/graphics/vulkan/vk_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@
#include <vulkan/vk_platform.h>
#include <vulkan/vulkan_core.h>

#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<std::function<void()>> deletors;

Expand Down Expand Up @@ -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<int64_t, std::shared_ptr<LoadedGLTF>> meshes;

std::unordered_map<int64_t, glm::mat4> transforms;

std::unordered_map<std::string, std::shared_ptr<LoadedGLTF>> loadedScenes;

Camera* mainCamera;

DrawContext mainDrawContext;
std::unordered_map<std::string, std::shared_ptr<ENode>> loadedNodes;

void update_scene();
// Buffer creation methods
GPUMeshBuffers uploadMesh(std::span<uint32_t> indices,
std::span<Vertex> 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<int64_t, std::shared_ptr<LoadedGLTF>> meshes;
std::unordered_map<int64_t, glm::mat4> transforms;
std::unordered_map<std::string, std::shared_ptr<LoadedGLTF>> loadedScenes;
std::unordered_map<std::string, std::shared_ptr<ENode>> loadedNodes;
std::vector<std::shared_ptr<MeshAsset>> 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<VkImage> _swapchainImages;
std::vector<VkImageView> _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<uint32_t> indices,
std::span<Vertex> vertices);

std::vector<std::shared_ptr<MeshAsset>> 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);
};
Loading