stories/engine/logicaldevice.h

92 lines
2.8 KiB
C++

#pragma once
#include <vector>
#include <string>
#include <vulkan/vulkan.h>
#include "utils.h"
#include "physicaldevice.h"
#include "surface.h"
namespace Engine {
class SwapChain;
class LogicalDevice {
public:
LogicalDevice(
PhysicalDevice* physicalDevice,
Surface* surface,
const std::vector<const char*>& extensions,
const std::vector<const char*> layers
);
~LogicalDevice();
void createSwapChain();
void clearSwapChain();
void createGraphicsPipeline(const std::string& vertexShaderPath, const std::string& fragmentShaderPath);
void recreateSwapChain();
void setResized();
void drawFrame();
VkResult waitIdle();
VkResult waitForFence(const VkFence& fence);
VkResult resetFence(const VkFence& fence);
VkExtent2D chooseSwapExtent() const;
VkResult createVkSwapChain(const VkExtent2D& extent, VkSwapchainKHR& out);
void destroyVkSwapChain(VkSwapchainKHR& swapChain);
VkResult getVkSwapChainImages(VkSwapchainKHR& swapChain, std::vector<VkImage>& out);
VkResult createVkImageView(const VkImage& image, VkImageView& out);
void destroyVkImageView(VkImageView& view);
VkResult createVkFrameBuffer(const VkImageView& imageView, const VkExtent2D& extent, VkFramebuffer& out);
void destroyVkFrameBuffer(VkFramebuffer& buffer);
private:
VkShaderModule createShaderModule(const std::string& path);
void destroyShaderModule(VkShaderModule shaderModule);
void createDevice(
VkPhysicalDevice physicalDevice,
const QueueFamilyIndices& indices,
const std::vector<const char*>& extensions,
const std::vector<const char*> layers
);
void createQueues(uint32_t graphicsFamilyIndex, uint32_t presenFamilyIndex);
void createCommandPool(uint32_t queueFamilyIndex);
void createCommandBuffers();
void createSyncObjects();
void createRenderPass(VkFormat format);
VkResult queueSubmitGraphics(
const VkSemaphore& wait,
const VkCommandBuffer& buffer,
const VkSemaphore& signal,
const VkFence& fence
);
VkResult queuePresent(const VkSemaphore& signal, uint32_t index);
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
private:
PhysicalDevice* phys;
VkDevice vk;
VkQueue graphicsQueue;
VkQueue presentQueue;
VkRenderPass renderPass;
VkPipelineLayout pipelineLayout;
VkPipeline graphicsPipeline;
VkCommandPool commandPool;
std::vector<VkCommandBuffer> commandBuffers;
std::vector<VkSemaphore> imageAvailableSemaphores;
std::vector<VkSemaphore> renderFinishedSemaphores;
std::vector<VkFence> inFlightFences;
uint32_t currentFrame;
bool framebufferResized;
bool hasPipeline;
Surface* surface;
VkSurfaceFormatKHR surfaceFormat;
SwapChain* swapChain;
};
}