123 lines
3.2 KiB
C++
123 lines
3.2 KiB
C++
//
|
|
// Created by timo on 03.04.25.
|
|
//
|
|
|
|
#ifndef VULKAN_APP_H
|
|
#define VULKAN_APP_H
|
|
|
|
#define GLFW_INCLUDE_VULKAN
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <cstdint>
|
|
#include <vector>
|
|
#include <optional>
|
|
#include <set>
|
|
#include <limits>
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
|
|
namespace vapp{
|
|
|
|
const VkPresentModeKHR WISHED_PRESENT_MODE = VK_PRESENT_MODE_MAILBOX_KHR;
|
|
|
|
typedef struct _QueueFamilyIndices{
|
|
std::optional<uint32_t> graphicsFamily;
|
|
std::optional<uint32_t> presentFamily;
|
|
|
|
bool isComplete(){
|
|
return graphicsFamily.has_value();
|
|
}
|
|
} QueueFamilyIndices;
|
|
|
|
typedef struct{
|
|
VkSurfaceCapabilitiesKHR capabilities;
|
|
std::vector<VkSurfaceFormatKHR> formats;
|
|
std::vector<VkPresentModeKHR> presentModes;
|
|
} SwapChainSupportDetails;
|
|
|
|
static std::vector<char> readFile(const std::string &filename);
|
|
|
|
class Vulkan{
|
|
private:
|
|
GLFWwindow *window;
|
|
uint32_t _width, _height;
|
|
|
|
VkInstance instance;
|
|
VkDebugUtilsMessengerEXT debugMessenger;
|
|
|
|
VkSurfaceKHR surface;
|
|
|
|
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
|
VkDevice device;
|
|
|
|
VkQueue graphicsQueue;
|
|
VkQueue presentQueue;
|
|
|
|
VkSwapchainKHR swapChain;
|
|
std::vector<VkImage> swapChainImages;
|
|
VkFormat swapChainImageFormat;
|
|
VkExtent2D swapChainExtent;
|
|
|
|
std::vector<VkImageView> swapChainImageViews;
|
|
|
|
VkRenderPass renderPass;
|
|
VkPipelineLayout pipelineLayout;
|
|
|
|
VkPipeline graphicsPipeline;
|
|
|
|
VkCommandPool commandPool;
|
|
VkCommandBuffer commandBuffer;
|
|
|
|
VkSemaphore imageAvailableSemaphore, renderFinishedSemaphore;
|
|
VkFence inFlightFence;
|
|
|
|
std::vector<VkFramebuffer> swapChainFramebuffers;
|
|
|
|
bool checkValidationLayerSupport();
|
|
std::vector<const char*> getRequiredExtensions();
|
|
|
|
void createInstance();
|
|
void setupDebugMessenger();
|
|
void pickPhysicalDevice();
|
|
void createLogicalDevice();
|
|
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
|
|
bool checkDeviceExtensionSupport(VkPhysicalDevice device);
|
|
bool isDeviceSuitable(VkPhysicalDevice device);
|
|
void createSurface();
|
|
SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device);
|
|
VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
|
|
VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR> availablePresentModes);
|
|
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
|
|
void createSwapChain();
|
|
void createImageViews();
|
|
void createGraphicsPipeline();
|
|
VkShaderModule createShaderModule(const std::vector<char> &code);
|
|
void createRenderPass();
|
|
void createFrameBuffers();
|
|
void createCommandPool();
|
|
void createCommandBuffer();
|
|
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
|
|
void createSyncObjects();
|
|
void drawFrame();
|
|
void initWindow();
|
|
void initVulkan();
|
|
void mainLoop();
|
|
void cleanup();
|
|
|
|
public:
|
|
#ifdef DEBUG
|
|
const bool enableValidationLayers = true;
|
|
#else
|
|
const bool enableValidationLayers = false;
|
|
#endif
|
|
|
|
void run(const uint32_t width = 800, const uint32_t height = 600);
|
|
};
|
|
} // vapp
|
|
|
|
#endif //VULKAN_APP_H
|