2024-09-25 23:03:56 -04:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <fmt/format.h>
|
|
|
|
#include <iostream>
|
2024-09-28 14:54:39 -04:00
|
|
|
#include <span>
|
|
|
|
#include <vulkan/vulkan_core.h>
|
2024-09-25 23:03:56 -04:00
|
|
|
|
|
|
|
#define GLFW_INCLUDE_NONE
|
|
|
|
#define GLFW_INCLUDE_VULKAN
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
2024-09-28 14:54:39 -04:00
|
|
|
#include "util/magic_enum.hpp"
|
2024-09-25 23:03:56 -04:00
|
|
|
#include "util/types.h"
|
|
|
|
|
|
|
|
constexpr u32 WIDTH = 800;
|
|
|
|
constexpr u32 HEIGHT = 600;
|
|
|
|
|
|
|
|
constexpr std::array<const char*, 1> validationLayers = { "VK_LAYER_KHRONOS_validation" };
|
|
|
|
|
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")
|
|
|
|
);
|
|
|
|
|
|
|
|
if (func == nullptr)
|
|
|
|
return VK_ERROR_EXTENSION_NOT_PRESENT;
|
|
|
|
|
|
|
|
return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-25 23:03:56 -04:00
|
|
|
class Application {
|
|
|
|
public:
|
|
|
|
fn run() -> void {
|
|
|
|
initWindow();
|
|
|
|
initVulkan();
|
|
|
|
mainLoop();
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-09-28 14:54:39 -04:00
|
|
|
VkDebugUtilsMessengerEXT mDebugMessenger;
|
|
|
|
VkInstance mInstance;
|
|
|
|
GLFWwindow* mWindow;
|
2024-09-25 23:03:56 -04:00
|
|
|
|
|
|
|
static fn checkValidationLayerSupport() -> bool {
|
|
|
|
u32 layerCount = 0;
|
|
|
|
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) {
|
|
|
|
if (strcmp(
|
2024-09-28 14:54:39 -04:00
|
|
|
static_cast<const char*>(layerName), static_cast<const char*>(layerProperties.layerName)
|
2024-09-25 23:03:56 -04:00
|
|
|
) == 0) {
|
|
|
|
layerFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!layerFound)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static fn getAvailableExtensions() -> std::vector<VkExtensionProperties> {
|
|
|
|
u32 extensionCount = 0;
|
|
|
|
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
|
|
|
|
|
|
|
|
std::vector<VkExtensionProperties> extensions(extensionCount);
|
|
|
|
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());
|
|
|
|
|
|
|
|
return extensions;
|
|
|
|
}
|
|
|
|
|
2024-09-28 14:54:39 -04:00
|
|
|
static fn getRequiredExtensions() -> std::vector<const char*> {
|
|
|
|
u32 glfwExtensionCount = 0;
|
|
|
|
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
|
|
|
|
|
|
|
|
std::vector<const char*> extensions;
|
|
|
|
|
|
|
|
if (glfwExtensions) {
|
|
|
|
std::span<const char*> extSpan(glfwExtensions, glfwExtensionCount);
|
|
|
|
extensions.assign(extSpan.begin(), extSpan.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enableValidationLayers)
|
|
|
|
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
|
|
|
|
|
|
|
return extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VKAPI_ATTR fn VKAPI_CALL debugCallback(
|
|
|
|
// Severity - verbose, info, warning, error
|
|
|
|
VkDebugUtilsMessageSeverityFlagBitsEXT /*messageSeverity*/,
|
|
|
|
|
|
|
|
// Message Type - general, validation, performance
|
|
|
|
VkDebugUtilsMessageTypeFlagsEXT /*messageType*/,
|
|
|
|
|
|
|
|
// Callback Data - Contains details of the message
|
|
|
|
// * pMessage - The message as a null-terminated string
|
|
|
|
// * pObjects - Array of related objects
|
|
|
|
// * objectCount - Number of related objects
|
|
|
|
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
|
|
|
|
|
|
|
// User Data - Any extra data the user may want to pass to the function
|
|
|
|
void* /*pUserData*/
|
|
|
|
) -> VkBool32 /* If true, abort the call */ {
|
|
|
|
std::cerr << "validation layer: " << pCallbackData->pMessage << std::endl;
|
|
|
|
|
|
|
|
return VK_FALSE;
|
|
|
|
}
|
|
|
|
|
2024-09-25 23:03:56 -04:00
|
|
|
fn createInstance() -> void {
|
|
|
|
if (enableValidationLayers && !checkValidationLayerSupport())
|
|
|
|
throw std::runtime_error("Validation layers requested, but not available!");
|
|
|
|
|
2024-09-28 14:54:39 -04:00
|
|
|
// General Metadata
|
2024-09-25 23:03:56 -04:00
|
|
|
VkApplicationInfo appInfo {};
|
|
|
|
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; // Used for pNext
|
|
|
|
appInfo.pApplicationName = "Hello Triangle";
|
|
|
|
appInfo.pEngineName = "No Engine";
|
|
|
|
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
|
|
|
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
|
|
|
appInfo.apiVersion = VK_API_VERSION_1_0;
|
|
|
|
|
2024-09-28 14:54:39 -04:00
|
|
|
// Information used by vkCreateInstance
|
2024-09-25 23:03:56 -04:00
|
|
|
VkInstanceCreateInfo createInfo {};
|
2024-09-28 14:54:39 -04:00
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
|
|
createInfo.pApplicationInfo = &appInfo;
|
|
|
|
|
|
|
|
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo {};
|
2024-09-26 19:56:19 -04:00
|
|
|
|
|
|
|
if (enableValidationLayers) {
|
|
|
|
createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
|
|
|
createInfo.ppEnabledLayerNames = validationLayers.data();
|
2024-09-28 14:54:39 -04:00
|
|
|
|
|
|
|
populateDebugMessengerCreateInfo(debugCreateInfo);
|
|
|
|
createInfo.pNext = (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
|
|
|
// Fixes macOS crashes
|
|
|
|
std::vector<const char*> extensions = getRequiredExtensions();
|
|
|
|
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 14:54:39 -04:00
|
|
|
createInfo.enabledExtensionCount = static_cast<u32>(extensions.size());
|
|
|
|
createInfo.ppEnabledExtensionNames = extensions.data();
|
|
|
|
|
|
|
|
// Finally, create the instance (and throw an error if it fails)
|
|
|
|
if (auto result = vkCreateInstance(&createInfo, nullptr, &mInstance); result != VK_SUCCESS)
|
|
|
|
throw std::runtime_error(
|
|
|
|
string("Failed to create instance! Error: ") + string(magic_enum::enum_name(result))
|
|
|
|
);
|
2024-09-25 23:03:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn initWindow() -> void {
|
|
|
|
// Initialize GLFW
|
|
|
|
glfwInit();
|
|
|
|
|
|
|
|
// Don't create an OpenGL context
|
|
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
|
|
|
|
|
|
|
// Disable Resizing
|
|
|
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
|
|
|
|
|
|
|
mWindow = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
2024-09-28 14:54:39 -04:00
|
|
|
fn initVulkan() -> void {
|
|
|
|
createInstance();
|
|
|
|
setupDebugMessenger();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (CreateDebugUtilsMessengerEXT(mInstance, &createInfo, nullptr, &mDebugMessenger) != VK_SUCCESS) {
|
|
|
|
throw std::runtime_error("Failed to set up debug messenger!");
|
|
|
|
}
|
|
|
|
}
|
2024-09-25 23:03:56 -04:00
|
|
|
|
|
|
|
fn mainLoop() -> void {
|
|
|
|
while (!glfwWindowShouldClose(mWindow)) glfwPollEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cleanup() -> void {
|
2024-09-28 14:54:39 -04:00
|
|
|
if (enableValidationLayers)
|
|
|
|
DestroyDebugUtilsMessengerEXT(mInstance, mDebugMessenger, nullptr);
|
|
|
|
|
2024-09-25 23:03:56 -04:00
|
|
|
vkDestroyInstance(mInstance, nullptr);
|
|
|
|
glfwDestroyWindow(mWindow);
|
|
|
|
glfwTerminate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() -> int {
|
|
|
|
Application app;
|
|
|
|
|
|
|
|
try {
|
|
|
|
app.run();
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|