2024-09-25 23:03:56 -04:00
|
|
|
#define GLFW_INCLUDE_NONE
|
|
|
|
#define GLFW_INCLUDE_VULKAN
|
|
|
|
#include <GLFW/glfw3.h>
|
2024-09-28 21:55:26 -04:00
|
|
|
#include <algorithm>
|
2024-09-28 18:13:24 -04:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2024-09-28 21:55:26 -04:00
|
|
|
#include <fmt/format.h>
|
2024-09-28 18:13:24 -04:00
|
|
|
#include <iostream>
|
2024-09-28 21:55:26 -04:00
|
|
|
#include <limits>
|
2024-09-28 18:13:24 -04:00
|
|
|
#include <optional>
|
|
|
|
#include <set>
|
|
|
|
#include <span>
|
|
|
|
#include <stdexcept>
|
2024-09-28 21:55:26 -04:00
|
|
|
#include <vulkan/vulkan_core.h>
|
2024-09-25 23:03:56 -04:00
|
|
|
|
2024-09-28 19:38:13 -04:00
|
|
|
#include "util/magic_enum.hpp"
|
|
|
|
#include "util/types.h"
|
2024-09-25 23:03:56 -04:00
|
|
|
|
2024-09-28 21:55:26 -04:00
|
|
|
const u32 WIDTH = 800;
|
|
|
|
const u32 HEIGHT = 600;
|
2024-09-25 23:03:56 -04:00
|
|
|
|
2024-09-28 21:55:26 -04:00
|
|
|
const std::array<const char*, 1> deviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
|
2024-09-28 18:13:24 -04:00
|
|
|
const std::array<const char*, 1> validationLayers = { "VK_LAYER_KHRONOS_validation" };
|
2024-09-25 23:03:56 -04:00
|
|
|
|
2024-09-26 19:56:19 -04:00
|
|
|
#ifdef NDEBUG
|
2024-09-25 23:03:56 -04:00
|
|
|
const bool enableValidationLayers = false;
|
|
|
|
#else
|
|
|
|
const bool enableValidationLayers = true;
|
|
|
|
#endif
|
|
|
|
|
2024-09-28 14:54:39 -04:00
|
|
|
fn CreateDebugUtilsMessengerEXT(
|
|
|
|
VkInstance instance,
|
|
|
|
const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
|
|
|
|
const VkAllocationCallbacks* pAllocator,
|
|
|
|
VkDebugUtilsMessengerEXT* pDebugMessenger
|
|
|
|
) -> VkResult {
|
|
|
|
auto func = std::bit_cast<PFN_vkCreateDebugUtilsMessengerEXT>(
|
|
|
|
vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT")
|
|
|
|
);
|
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
if (func != nullptr)
|
|
|
|
return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
|
2024-09-28 14:54:39 -04:00
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
return VK_ERROR_EXTENSION_NOT_PRESENT;
|
2024-09-28 14:54:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn DestroyDebugUtilsMessengerEXT(
|
|
|
|
VkInstance instance,
|
|
|
|
VkDebugUtilsMessengerEXT debugMessenger,
|
|
|
|
const VkAllocationCallbacks* pAllocator
|
|
|
|
) -> void {
|
|
|
|
auto func = std::bit_cast<PFN_vkDestroyDebugUtilsMessengerEXT>(
|
|
|
|
vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT")
|
|
|
|
);
|
|
|
|
|
|
|
|
if (func != nullptr)
|
|
|
|
func(instance, debugMessenger, pAllocator);
|
|
|
|
}
|
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
struct QueueFamilyIndices {
|
2024-09-28 21:55:26 -04:00
|
|
|
std::optional<u32> graphics_family;
|
|
|
|
std::optional<u32> present_family;
|
2024-09-28 18:13:24 -04:00
|
|
|
|
|
|
|
fn isComplete() -> bool { return graphics_family.has_value() && present_family.has_value(); }
|
|
|
|
};
|
|
|
|
|
2024-09-28 21:55:26 -04:00
|
|
|
struct SwapChainSupportDetails {
|
|
|
|
VkSurfaceCapabilitiesKHR capabilities;
|
|
|
|
std::vector<VkSurfaceFormatKHR> formats;
|
|
|
|
std::vector<VkPresentModeKHR> present_modes;
|
|
|
|
};
|
|
|
|
|
2024-09-25 23:03:56 -04:00
|
|
|
class Application {
|
|
|
|
public:
|
|
|
|
fn run() -> void {
|
|
|
|
initWindow();
|
|
|
|
initVulkan();
|
|
|
|
mainLoop();
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-09-28 18:13:24 -04:00
|
|
|
GLFWwindow* mWindow;
|
|
|
|
|
2024-09-28 14:54:39 -04:00
|
|
|
VkInstance mInstance;
|
2024-09-28 18:13:24 -04:00
|
|
|
VkDebugUtilsMessengerEXT mDebugMessenger;
|
|
|
|
VkSurfaceKHR mSurface;
|
2024-09-25 23:03:56 -04:00
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
|
|
|
|
VkDevice mDevice;
|
2024-09-25 23:03:56 -04:00
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
VkQueue mGraphicsQueue;
|
|
|
|
VkQueue mPresentQueue;
|
2024-09-25 23:03:56 -04:00
|
|
|
|
2024-09-28 21:55:26 -04:00
|
|
|
VkSwapchainKHR mSwapChain;
|
|
|
|
std::vector<VkImage> mSwapChainImages;
|
|
|
|
VkFormat mSwapChainImageFormat;
|
|
|
|
VkExtent2D mSwapChainExtent;
|
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
fn initWindow() -> void {
|
2024-09-28 21:55:26 -04:00
|
|
|
if (glfwInit() == GLFW_FALSE)
|
|
|
|
throw std::runtime_error("Failed to initialize GLFW!");
|
2024-09-25 23:03:56 -04:00
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
|
|
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
2024-09-25 23:03:56 -04:00
|
|
|
|
2024-09-28 21:55:26 -04:00
|
|
|
if (mWindow = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); mWindow == nullptr)
|
|
|
|
throw std::runtime_error("Failed to create GLFW window!");
|
2024-09-25 23:03:56 -04:00
|
|
|
}
|
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
fn initVulkan() -> void {
|
|
|
|
createInstance();
|
|
|
|
setupDebugMessenger();
|
|
|
|
createSurface();
|
|
|
|
pickPhysicalDevice();
|
|
|
|
createLogicalDevice();
|
2024-09-28 21:55:26 -04:00
|
|
|
createSwapChain();
|
2024-09-25 23:03:56 -04:00
|
|
|
}
|
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
fn mainLoop() -> void {
|
|
|
|
while (!glfwWindowShouldClose(mWindow)) { glfwPollEvents(); }
|
2024-09-28 14:54:39 -04:00
|
|
|
}
|
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
fn cleanup() -> void {
|
2024-09-28 21:55:26 -04:00
|
|
|
vkDestroySwapchainKHR(mDevice, mSwapChain, nullptr);
|
2024-09-28 18:13:24 -04:00
|
|
|
vkDestroyDevice(mDevice, nullptr);
|
2024-09-28 14:54:39 -04:00
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
if (enableValidationLayers)
|
|
|
|
DestroyDebugUtilsMessengerEXT(mInstance, mDebugMessenger, nullptr);
|
2024-09-28 14:54:39 -04:00
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
vkDestroySurfaceKHR(mInstance, mSurface, nullptr);
|
|
|
|
vkDestroyInstance(mInstance, nullptr);
|
2024-09-28 14:54:39 -04:00
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
glfwDestroyWindow(mWindow);
|
2024-09-28 14:54:39 -04:00
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
glfwTerminate();
|
2024-09-28 14:54:39 -04:00
|
|
|
}
|
|
|
|
|
2024-09-25 23:03:56 -04:00
|
|
|
fn createInstance() -> void {
|
|
|
|
if (enableValidationLayers && !checkValidationLayerSupport())
|
2024-09-28 18:13:24 -04:00
|
|
|
throw std::runtime_error("validation layers requested, but not available!");
|
2024-09-25 23:03:56 -04:00
|
|
|
|
|
|
|
VkApplicationInfo appInfo {};
|
2024-09-28 18:13:24 -04:00
|
|
|
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
2024-09-25 23:03:56 -04:00
|
|
|
appInfo.pApplicationName = "Hello Triangle";
|
|
|
|
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
2024-09-28 18:13:24 -04:00
|
|
|
appInfo.pEngineName = "No Engine";
|
2024-09-25 23:03:56 -04:00
|
|
|
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
|
|
|
appInfo.apiVersion = VK_API_VERSION_1_0;
|
|
|
|
|
|
|
|
VkInstanceCreateInfo createInfo {};
|
2024-09-28 14:54:39 -04:00
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
|
|
createInfo.pApplicationInfo = &appInfo;
|
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
std::vector<const char*> extensions = getRequiredExtensions();
|
2024-09-26 19:56:19 -04:00
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo {};
|
2024-09-26 19:56:19 -04:00
|
|
|
if (enableValidationLayers) {
|
2024-09-28 21:55:26 -04:00
|
|
|
createInfo.enabledLayerCount = static_cast<u32>(validationLayers.size());
|
2024-09-26 19:56:19 -04:00
|
|
|
createInfo.ppEnabledLayerNames = validationLayers.data();
|
2024-09-28 14:54:39 -04:00
|
|
|
|
|
|
|
populateDebugMessengerCreateInfo(debugCreateInfo);
|
2024-09-28 18:13:24 -04:00
|
|
|
createInfo.pNext = static_cast<VkDebugUtilsMessengerCreateInfoEXT*>(&debugCreateInfo);
|
2024-09-26 19:56:19 -04:00
|
|
|
} else {
|
|
|
|
createInfo.enabledLayerCount = 0;
|
2024-09-25 23:03:56 -04:00
|
|
|
|
2024-09-28 14:54:39 -04:00
|
|
|
createInfo.pNext = nullptr;
|
|
|
|
}
|
2024-09-26 17:18:45 -04:00
|
|
|
|
2024-09-28 14:54:39 -04:00
|
|
|
extensions.emplace_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
|
2024-09-26 17:18:45 -04:00
|
|
|
|
|
|
|
createInfo.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
|
2024-09-28 18:13:24 -04:00
|
|
|
|
2024-09-28 14:54:39 -04:00
|
|
|
createInfo.enabledExtensionCount = static_cast<u32>(extensions.size());
|
|
|
|
createInfo.ppEnabledExtensionNames = extensions.data();
|
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
for (const char* extension : extensions) std::cout << extension << std::endl;
|
2024-09-25 23:03:56 -04:00
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
if (vkCreateInstance(&createInfo, nullptr, &mInstance) != VK_SUCCESS)
|
|
|
|
throw std::runtime_error("failed to create instance!");
|
2024-09-28 14:54:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static fn populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) -> void {
|
|
|
|
createInfo = {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
|
|
|
createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
|
|
|
|
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
|
|
|
|
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
|
|
|
createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
|
|
|
|
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
|
|
|
|
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
|
|
|
|
createInfo.pfnUserCallback = debugCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setupDebugMessenger() -> void {
|
|
|
|
if (!enableValidationLayers)
|
|
|
|
return;
|
|
|
|
|
|
|
|
VkDebugUtilsMessengerCreateInfoEXT createInfo;
|
|
|
|
populateDebugMessengerCreateInfo(createInfo);
|
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
if (CreateDebugUtilsMessengerEXT(mInstance, &createInfo, nullptr, &mDebugMessenger) != VK_SUCCESS)
|
|
|
|
throw std::runtime_error("failed to set up debug messenger!");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn createSurface() -> void {
|
2024-09-28 19:38:13 -04:00
|
|
|
if (VkResult result = glfwCreateWindowSurface(mInstance, mWindow, nullptr, &mSurface);
|
|
|
|
result != VK_SUCCESS)
|
|
|
|
throw std::runtime_error(
|
|
|
|
"Failed to create window surface! Error: " + string(magic_enum::enum_name(result))
|
|
|
|
);
|
2024-09-28 18:13:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn pickPhysicalDevice() -> void {
|
2024-09-28 21:55:26 -04:00
|
|
|
u32 deviceCount = 0;
|
2024-09-28 18:13:24 -04:00
|
|
|
vkEnumeratePhysicalDevices(mInstance, &deviceCount, nullptr);
|
|
|
|
|
|
|
|
if (deviceCount == 0)
|
|
|
|
throw std::runtime_error("failed to find GPUs with Vulkan support!");
|
|
|
|
|
|
|
|
std::vector<VkPhysicalDevice> devices(deviceCount);
|
|
|
|
vkEnumeratePhysicalDevices(mInstance, &deviceCount, devices.data());
|
|
|
|
|
|
|
|
for (const auto& device : devices)
|
|
|
|
if (isDeviceSuitable(device)) {
|
|
|
|
mPhysicalDevice = device;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPhysicalDevice == VK_NULL_HANDLE)
|
|
|
|
throw std::runtime_error("failed to find a suitable GPU!");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn createLogicalDevice() -> void {
|
|
|
|
QueueFamilyIndices indices = findQueueFamilies(mPhysicalDevice);
|
|
|
|
|
|
|
|
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
|
2024-09-28 21:55:26 -04:00
|
|
|
std::set<u32> uniqueQueueFamilies = { indices.graphics_family.value(), indices.present_family.value() };
|
2024-09-28 18:13:24 -04:00
|
|
|
|
2024-09-28 21:55:26 -04:00
|
|
|
f32 queuePriority = 1.0F;
|
|
|
|
for (u32 queueFamily : uniqueQueueFamilies) {
|
2024-09-28 18:13:24 -04:00
|
|
|
VkDeviceQueueCreateInfo queueCreateInfo {};
|
|
|
|
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
|
|
|
queueCreateInfo.queueFamilyIndex = queueFamily;
|
|
|
|
queueCreateInfo.queueCount = 1;
|
|
|
|
queueCreateInfo.pQueuePriorities = &queuePriority;
|
|
|
|
queueCreateInfos.push_back(queueCreateInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
VkPhysicalDeviceFeatures deviceFeatures {};
|
|
|
|
|
|
|
|
VkDeviceCreateInfo createInfo {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
|
|
|
|
2024-09-28 21:55:26 -04:00
|
|
|
createInfo.queueCreateInfoCount = static_cast<u32>(queueCreateInfos.size());
|
2024-09-28 18:13:24 -04:00
|
|
|
createInfo.pQueueCreateInfos = queueCreateInfos.data();
|
|
|
|
|
|
|
|
createInfo.pEnabledFeatures = &deviceFeatures;
|
|
|
|
|
2024-09-28 21:55:26 -04:00
|
|
|
createInfo.enabledExtensionCount = static_cast<u32>(deviceExtensions.size());
|
|
|
|
createInfo.ppEnabledExtensionNames = deviceExtensions.data();
|
2024-09-28 18:13:24 -04:00
|
|
|
|
|
|
|
if (enableValidationLayers) {
|
2024-09-28 21:55:26 -04:00
|
|
|
createInfo.enabledLayerCount = static_cast<u32>(validationLayers.size());
|
2024-09-28 18:13:24 -04:00
|
|
|
createInfo.ppEnabledLayerNames = validationLayers.data();
|
|
|
|
} else {
|
|
|
|
createInfo.enabledLayerCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice) != VK_SUCCESS) {
|
|
|
|
throw std::runtime_error("failed to create logical device!");
|
2024-09-28 14:54:39 -04:00
|
|
|
}
|
2024-09-28 18:13:24 -04:00
|
|
|
|
|
|
|
vkGetDeviceQueue(mDevice, indices.graphics_family.value(), 0, &mGraphicsQueue);
|
|
|
|
vkGetDeviceQueue(mDevice, indices.present_family.value(), 0, &mPresentQueue);
|
2024-09-28 14:54:39 -04:00
|
|
|
}
|
2024-09-25 23:03:56 -04:00
|
|
|
|
2024-09-28 21:55:26 -04:00
|
|
|
fn createSwapChain() -> void {
|
|
|
|
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(mPhysicalDevice);
|
|
|
|
|
|
|
|
VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
|
|
|
|
VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.present_modes);
|
|
|
|
VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
|
|
|
|
|
|
|
|
u32 imageCount = swapChainSupport.capabilities.minImageCount + 1;
|
|
|
|
if (swapChainSupport.capabilities.maxImageCount > 0 &&
|
|
|
|
imageCount > swapChainSupport.capabilities.maxImageCount)
|
|
|
|
imageCount = swapChainSupport.capabilities.maxImageCount;
|
|
|
|
|
|
|
|
VkSwapchainCreateInfoKHR createInfo {};
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
|
|
|
createInfo.surface = mSurface;
|
|
|
|
createInfo.minImageCount = imageCount;
|
|
|
|
createInfo.imageFormat = surfaceFormat.format;
|
|
|
|
createInfo.imageColorSpace = surfaceFormat.colorSpace;
|
|
|
|
createInfo.imageExtent = extent;
|
|
|
|
createInfo.imageArrayLayers = 1;
|
|
|
|
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
|
|
|
|
|
|
|
QueueFamilyIndices indices = findQueueFamilies(mPhysicalDevice);
|
|
|
|
std::array<u32, 2> queueFamilyIndices = { indices.graphics_family.value(),
|
|
|
|
indices.present_family.value() };
|
|
|
|
|
|
|
|
if (indices.graphics_family != indices.present_family) {
|
|
|
|
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
|
|
|
createInfo.queueFamilyIndexCount = 2;
|
|
|
|
createInfo.pQueueFamilyIndices = queueFamilyIndices.data();
|
|
|
|
} else {
|
|
|
|
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
createInfo.queueFamilyIndexCount = 0; // Optional
|
|
|
|
createInfo.pQueueFamilyIndices = nullptr; // Optional
|
|
|
|
}
|
|
|
|
|
|
|
|
createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
|
|
|
|
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
|
|
|
createInfo.presentMode = presentMode;
|
|
|
|
createInfo.clipped = VK_TRUE;
|
|
|
|
createInfo.oldSwapchain = VK_NULL_HANDLE;
|
|
|
|
|
|
|
|
if (vkCreateSwapchainKHR(mDevice, &createInfo, nullptr, &mSwapChain) != VK_SUCCESS)
|
|
|
|
throw std::runtime_error("Failed to create swap chain!");
|
|
|
|
|
|
|
|
vkGetSwapchainImagesKHR(mDevice, mSwapChain, &imageCount, nullptr);
|
|
|
|
mSwapChainImages.resize(imageCount);
|
|
|
|
vkGetSwapchainImagesKHR(mDevice, mSwapChain, &imageCount, mSwapChainImages.data());
|
|
|
|
|
|
|
|
mSwapChainImageFormat = surfaceFormat.format;
|
|
|
|
mSwapChainExtent = extent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fn chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats
|
|
|
|
) -> VkSurfaceFormatKHR {
|
|
|
|
for (const auto& availableFormat : availableFormats) {
|
|
|
|
if (availableFormat.format == VK_FORMAT_B8G8R8A8_SRGB &&
|
|
|
|
availableFormat.colorSpace == VK_COLORSPACE_SRGB_NONLINEAR_KHR)
|
|
|
|
return availableFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
return availableFormats[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static fn chooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes
|
|
|
|
) -> VkPresentModeKHR {
|
|
|
|
for (const auto& availablePresentMode : availablePresentModes)
|
|
|
|
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR)
|
|
|
|
return availablePresentMode;
|
|
|
|
|
|
|
|
return VK_PRESENT_MODE_FIFO_KHR;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) -> VkExtent2D {
|
|
|
|
if (capabilities.currentExtent.width != std::numeric_limits<u32>::max()) {
|
|
|
|
return capabilities.currentExtent;
|
|
|
|
}
|
|
|
|
|
|
|
|
int width = 0, height = 0;
|
|
|
|
glfwGetFramebufferSize(mWindow, &width, &height);
|
|
|
|
|
|
|
|
VkExtent2D actualExtent = { static_cast<u32>(width), static_cast<u32>(height) };
|
|
|
|
|
|
|
|
actualExtent.width =
|
|
|
|
std::clamp(actualExtent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
|
|
|
|
actualExtent.height =
|
|
|
|
std::clamp(actualExtent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
|
|
|
|
|
|
|
|
return actualExtent;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn querySwapChainSupport(VkPhysicalDevice device) -> SwapChainSupportDetails {
|
|
|
|
SwapChainSupportDetails details;
|
|
|
|
|
|
|
|
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, mSurface, &details.capabilities);
|
|
|
|
|
|
|
|
u32 formatCount = 0;
|
|
|
|
vkGetPhysicalDeviceSurfaceFormatsKHR(device, mSurface, &formatCount, nullptr);
|
|
|
|
|
|
|
|
if (formatCount != 0) {
|
|
|
|
details.formats.resize(formatCount);
|
|
|
|
vkGetPhysicalDeviceSurfaceFormatsKHR(device, mSurface, &formatCount, details.formats.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 presentModeCount = 0;
|
|
|
|
vkGetPhysicalDeviceSurfacePresentModesKHR(device, mSurface, &presentModeCount, nullptr);
|
|
|
|
|
|
|
|
if (presentModeCount != 0) {
|
|
|
|
details.present_modes.resize(presentModeCount);
|
|
|
|
vkGetPhysicalDeviceSurfacePresentModesKHR(
|
|
|
|
device, mSurface, &presentModeCount, details.present_modes.data()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return details;
|
|
|
|
}
|
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
fn isDeviceSuitable(VkPhysicalDevice device) -> bool {
|
|
|
|
QueueFamilyIndices indices = findQueueFamilies(device);
|
|
|
|
|
2024-09-28 21:55:26 -04:00
|
|
|
const bool extensionsSupported = checkDeviceExtensionSupport(device);
|
|
|
|
|
|
|
|
bool swapChainAdequate = false;
|
|
|
|
|
|
|
|
if (extensionsSupported) {
|
|
|
|
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device);
|
|
|
|
swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.present_modes.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return indices.isComplete() && extensionsSupported && swapChainAdequate;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fn checkDeviceExtensionSupport(VkPhysicalDevice device) -> bool {
|
|
|
|
u32 extensionCount = 0;
|
|
|
|
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
|
|
|
|
|
|
|
|
std::vector<VkExtensionProperties> availableExtensions(extensionCount);
|
|
|
|
vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
|
|
|
|
|
|
|
|
std::set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
|
|
|
|
|
|
|
|
for (const VkExtensionProperties& extension : availableExtensions)
|
|
|
|
requiredExtensions.erase(static_cast<const char*>(extension.extensionName));
|
|
|
|
|
|
|
|
return requiredExtensions.empty();
|
2024-09-25 23:03:56 -04:00
|
|
|
}
|
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
fn findQueueFamilies(VkPhysicalDevice device) -> QueueFamilyIndices {
|
|
|
|
QueueFamilyIndices indices;
|
|
|
|
|
2024-09-28 21:55:26 -04:00
|
|
|
u32 queueFamilyCount = 0;
|
2024-09-28 18:13:24 -04:00
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
|
|
|
|
|
|
|
|
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
|
|
|
|
|
|
|
|
u32 idx = 0;
|
|
|
|
|
|
|
|
for (const auto& queueFamily : queueFamilies) {
|
|
|
|
if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)
|
|
|
|
indices.graphics_family = idx;
|
|
|
|
|
|
|
|
VkBool32 presentSupport = false;
|
|
|
|
vkGetPhysicalDeviceSurfaceSupportKHR(device, idx, mSurface, &presentSupport);
|
|
|
|
|
|
|
|
if (presentSupport)
|
|
|
|
indices.present_family = idx;
|
|
|
|
|
|
|
|
if (indices.isComplete())
|
|
|
|
break;
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return indices;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fn getRequiredExtensions() -> std::vector<const char*> {
|
2024-09-28 21:55:26 -04:00
|
|
|
u32 glfwExtensionCount = 0;
|
2024-09-28 18:13:24 -04:00
|
|
|
const char** glfwExtensions = nullptr;
|
|
|
|
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
|
|
|
|
|
|
|
std::vector<const char*> extensions;
|
|
|
|
if (glfwExtensions) {
|
|
|
|
std::span<const char*> extSpan(glfwExtensions, glfwExtensionCount);
|
|
|
|
extensions.assign(extSpan.begin(), extSpan.end());
|
|
|
|
}
|
|
|
|
|
2024-09-28 14:54:39 -04:00
|
|
|
if (enableValidationLayers)
|
2024-09-28 18:13:24 -04:00
|
|
|
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
2024-09-28 14:54:39 -04:00
|
|
|
|
2024-09-28 18:13:24 -04:00
|
|
|
return extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fn checkValidationLayerSupport() -> bool {
|
2024-09-28 21:55:26 -04:00
|
|
|
u32 layerCount = 0;
|
2024-09-28 18:13:24 -04:00
|
|
|
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
|
|
|
|
|
|
|
|
std::vector<VkLayerProperties> availableLayers(layerCount);
|
|
|
|
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
|
|
|
|
|
|
|
|
for (const char* layerName : validationLayers) {
|
|
|
|
bool layerFound = false;
|
|
|
|
|
|
|
|
for (const auto& layerProperties : availableLayers) {
|
2024-09-28 21:55:26 -04:00
|
|
|
if (strcmp(layerName, static_cast<const char*>(layerProperties.layerName)) == 0) {
|
2024-09-28 18:13:24 -04:00
|
|
|
layerFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!layerFound)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VKAPI_ATTR fn VKAPI_CALL debugCallback(
|
|
|
|
VkDebugUtilsMessageSeverityFlagBitsEXT /*messageSeverity*/,
|
|
|
|
VkDebugUtilsMessageTypeFlagsEXT /*messageType*/,
|
|
|
|
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
|
|
|
void* /*pUserData*/
|
|
|
|
) -> VkBool32 {
|
2024-09-28 21:55:26 -04:00
|
|
|
fmt::println("Validation Layer: {}", pCallbackData->pMessage);
|
2024-09-28 18:13:24 -04:00
|
|
|
|
|
|
|
return VK_FALSE;
|
2024-09-25 23:03:56 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-28 21:55:26 -04:00
|
|
|
fn main() -> i32 {
|
2024-09-25 23:03:56 -04:00
|
|
|
Application app;
|
|
|
|
|
|
|
|
try {
|
|
|
|
app.run();
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|