118 lines
2.7 KiB
C++
118 lines
2.7 KiB
C++
// vulkan_guide.h : Include file for standard system include files,
|
|
// or project specific include files.
|
|
|
|
#pragma once
|
|
|
|
#include <vk_types.h>
|
|
#include <vk_descriptors.h>
|
|
|
|
struct DeletionQueue {
|
|
std::deque<std::function<void()>> deletors;
|
|
|
|
void push_function(std::function<void()>&& function) {
|
|
deletors.push_back(function);
|
|
}
|
|
|
|
void flush() {
|
|
// reverse itererate the deletion queue
|
|
for(auto it = deletors.rbegin(); it != deletors.rend(); it++) {
|
|
(*it)();
|
|
}
|
|
|
|
deletors.clear();
|
|
}
|
|
};
|
|
|
|
struct FrameData {
|
|
VkCommandPool _commandPool;
|
|
VkCommandBuffer _mainCommandBuffer;
|
|
VkSemaphore _swapchainSemaphore;
|
|
VkSemaphore _renderSemaphore;
|
|
VkFence _renderFence;
|
|
DeletionQueue _deletionQueue;
|
|
};
|
|
|
|
constexpr unsigned int FRAME_OVERLAP=2;
|
|
|
|
class VulkanEngine {
|
|
public:
|
|
VkFence _immFence;
|
|
VkCommandBuffer _immCommandBuffer;
|
|
VkCommandPool _immCommandPool;
|
|
VkPipeline _gradientPipeline;
|
|
VkPipelineLayout _gradientPipelineLayout;
|
|
|
|
DescriptorAllocator globalDescriptorAllocator;
|
|
VkDescriptorSet _drawImageDescriptors;
|
|
VkDescriptorSetLayout _drawImageDescriptorLayout;
|
|
|
|
// device selection
|
|
VkInstance _instance;
|
|
VkDebugUtilsMessengerEXT _debug_messenger;
|
|
VkPhysicalDevice _chosenGPU;
|
|
VkDevice _device;
|
|
VkSurfaceKHR _surface;
|
|
|
|
// swapchain
|
|
VkSwapchainKHR _swapchain;
|
|
VkFormat _swapchainImageFormat;
|
|
|
|
std::vector<VkImage> _swapchainImages;
|
|
std::vector<VkImageView> _swapchainImageViews;
|
|
VkExtent2D _swapchainExtent;
|
|
|
|
// VMA stuff
|
|
VmaAllocator _allocator;
|
|
|
|
// frames/command buffer
|
|
unsigned int _frameNumber = 0;
|
|
FrameData _frames[FRAME_OVERLAP];
|
|
|
|
FrameData& get_current_frame() {
|
|
return _frames[_frameNumber % FRAME_OVERLAP];
|
|
}
|
|
|
|
VkQueue _graphicsQueue;
|
|
uint32_t _graphicsQueueFamily;
|
|
|
|
// draw resources
|
|
AllocatedImage _drawImage;
|
|
VkExtent2D _drawExtent;
|
|
|
|
// internal data
|
|
bool _isInitialized{ false };
|
|
VkExtent2D _windowExtent{ 1700 , 900 };
|
|
struct SDL_Window* _window{ nullptr };
|
|
DeletionQueue _mainDeletionQueue;
|
|
|
|
static VulkanEngine& Get();
|
|
|
|
//initializes everything in the engine
|
|
void init();
|
|
|
|
//shuts down the engine
|
|
void cleanup();
|
|
|
|
//draw loop
|
|
void draw();
|
|
|
|
//run main loop
|
|
void run();
|
|
|
|
void immediate_submit(std::function<void(VkCommandBuffer cmd)>&& function);
|
|
|
|
private:
|
|
void init_vulkan();
|
|
void init_swapchain();
|
|
void init_commands();
|
|
void init_sync_structures();
|
|
void init_descriptors();
|
|
void init_pipelines();
|
|
void init_background_pipelines();
|
|
void init_imgui();
|
|
|
|
void create_swapchain(uint32_t width, uint32_t height);
|
|
void destroy_swapchain();
|
|
void draw_background(VkCommandBuffer cmd);
|
|
void draw_imgui(VkCommandBuffer cmd, VkImageView targetImageView);
|
|
};
|