learn-vulkan/vk_engine.h

139 lines
3 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 ComputePushConstants {
glm::vec4 data1;
glm::vec4 data2;
glm::vec4 data3;
glm::vec4 data4;
};
struct ComputeEffect {
const char *name;
VkPipeline pipeline;
VkPipelineLayout layout;
ComputePushConstants data;
};
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;
// imgui shader stuff
std::vector<ComputeEffect> backgroundEffects;
int currentBackgroundEffect{0};
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);
void render_imgui();
};