102 lines
3.0 KiB
C++
102 lines
3.0 KiB
C++
#pragma once
|
|
|
|
#include "SDL_video.h"
|
|
#include "SDL_events.h"
|
|
|
|
#include <cstdint>
|
|
#include <vector>
|
|
#include <optional>
|
|
|
|
#include <vulkan/vulkan.h>
|
|
|
|
const std::vector<const char*> validationLayers = {
|
|
"VK_LAYER_KHRONOS_validation"
|
|
};
|
|
|
|
const std::vector<const char*> deviceExtensions = {
|
|
VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
|
};
|
|
|
|
#ifdef NDEBUG
|
|
const bool enableValidationLayers = false;
|
|
#else
|
|
const bool enableValidationLayers = true;
|
|
#endif
|
|
|
|
VkResult CreateDebugUtilsMessengerEXT(VkInstance, const VkDebugUtilsMessengerCreateInfoEXT*, const VkAllocationCallbacks*, VkDebugUtilsMessengerEXT*);
|
|
void DestroyDebugUtilsMessengerEXT(VkInstance, VkDebugUtilsMessengerEXT, const VkAllocationCallbacks*);
|
|
|
|
struct QueueFamilyIndices {
|
|
std::optional<uint32_t> graphicsFamily;
|
|
std::optional<uint32_t> presentFamily;
|
|
|
|
bool complete() {
|
|
return graphicsFamily.has_value() && presentFamily.has_value();
|
|
}
|
|
};
|
|
|
|
struct SwapChainSupportDetails {
|
|
VkSurfaceCapabilitiesKHR capabilities;
|
|
std::vector<VkSurfaceFormatKHR> formats;
|
|
std::vector<VkPresentModeKHR> presentModes;
|
|
};
|
|
|
|
class VulkanApp {
|
|
public:
|
|
VulkanApp(const uint32_t& _w, const uint32_t& _h) :
|
|
mWidth(_w), mHeight(_h), mWin(nullptr), mActive(false) {}
|
|
|
|
void init();
|
|
void loop();
|
|
void cleanup();
|
|
private:
|
|
// SDL2
|
|
SDL_Window *mWin;
|
|
SDL_Event mEvent;
|
|
|
|
// Not tied to library
|
|
const uint32_t mWidth;
|
|
const uint32_t mHeight;
|
|
bool mActive;
|
|
|
|
// Vulkan
|
|
VkInstance mInstance;
|
|
VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
|
|
VkDevice mLogicalDevice;
|
|
VkSurfaceKHR mSurface;
|
|
VkSwapchainKHR mSwapChain;
|
|
std::vector<VkImage> mSwapChainImages;
|
|
std::vector<VkImageView> mSwapChainImageViews;
|
|
VkFormat mSwapChainImageFormat;
|
|
VkExtent2D mSwapChainExtent;
|
|
|
|
VkQueue mGraphicsQueue;
|
|
VkQueue mPresentQueue;
|
|
|
|
VkDebugUtilsMessengerEXT mDebugMessenger;
|
|
|
|
void createInstance();
|
|
void selectPhysicalDevice();
|
|
void createLogicalDevice();
|
|
void createSwapChain();
|
|
void createImageViews();
|
|
void createGraphicsPipeline();
|
|
|
|
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice);
|
|
bool isDeviceSuitable(VkPhysicalDevice);
|
|
bool checkDeviceExtensionSupport(VkPhysicalDevice);
|
|
SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice);
|
|
VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>&);
|
|
VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR>&);
|
|
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR&);
|
|
// Validation layer stuff
|
|
void setupDebugMessenger();
|
|
bool checkValidationLayerSupport();
|
|
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
|
VkDebugUtilsMessageTypeFlagsEXT messageType,
|
|
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData) {
|
|
fprintf(stderr, "Validation Layer: %s \n", pCallbackData->pMessage);
|
|
|
|
return VK_FALSE;
|
|
}
|
|
}; |