vulkan-test/src/main.cpp

192 lines
5.8 KiB
C++
Raw Normal View History

2024-09-25 23:03:56 -04:00
#define GLFW_INCLUDE_NONE
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
2024-09-28 18:13:24 -04:00
#include <cstdlib>
#include <iostream>
#include <stdexcept>
2024-09-25 23:03:56 -04:00
2024-09-29 23:02:04 -04:00
#define VULKAN_HPP_NO_CONSTRUCTORS
#include <vulkan/vulkan.hpp>
#include "fmt/base.h"
2024-09-28 19:38:13 -04:00
#include "util/types.h"
2024-09-25 23:03:56 -04:00
2024-09-29 23:02:04 -04:00
constexpr int WIDTH = 800;
constexpr int HEIGHT = 600;
2024-09-25 23:03:56 -04:00
2024-09-29 23:02:04 -04:00
constexpr 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-29 23:02:04 -04:00
constexpr bool enableValidationLayers = false;
2024-09-25 23:03:56 -04:00
#else
2024-09-29 23:02:04 -04:00
constexpr bool enableValidationLayers = true;
2024-09-25 23:03:56 -04:00
#endif
2024-09-29 23:02:04 -04:00
class HelloTriangleApplication {
2024-09-25 23:03:56 -04:00
public:
2024-09-29 23:02:04 -04:00
void run() {
2024-09-25 23:03:56 -04:00
initWindow();
initVulkan();
mainLoop();
cleanup();
}
private:
2024-09-28 18:13:24 -04:00
GLFWwindow* mWindow;
2024-09-29 23:02:04 -04:00
vk::UniqueInstance mInstance;
2024-09-25 23:03:56 -04:00
2024-09-29 23:02:04 -04:00
vk::UniqueHandle<vk::DebugUtilsMessengerEXT, vk::DispatchLoaderDynamic> mDebugMessenger;
vk::DispatchLoaderDynamic mLoader;
2024-09-28 21:55:26 -04:00
2024-09-29 23:02:04 -04:00
void initWindow() {
glfwInit();
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-29 23:02:04 -04:00
mWindow = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
2024-09-25 23:03:56 -04:00
}
2024-09-29 23:02:04 -04:00
void initVulkan() {
2024-09-28 18:13:24 -04:00
createInstance();
setupDebugMessenger();
2024-09-25 23:03:56 -04:00
}
2024-09-29 23:02:04 -04:00
void setupDebugMessenger() {
if (!enableValidationLayers)
return;
2024-09-28 14:54:39 -04:00
2024-09-29 23:02:04 -04:00
auto messengerCreateInfo = vk::DebugUtilsMessengerCreateInfoEXT()
.setMessageSeverity(
vk::DebugUtilsMessageSeverityFlagBitsEXT::eVerbose |
vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning |
vk::DebugUtilsMessageSeverityFlagBitsEXT::eError
)
.setMessageType(
vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral |
vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance |
vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation
)
.setPfnUserCallback(debugCallback)
.setPUserData(nullptr); // Optional
2024-09-28 22:03:38 -04:00
2024-09-29 23:02:04 -04:00
mDebugMessenger = mInstance->createDebugUtilsMessengerEXTUnique(messengerCreateInfo, nullptr, mLoader);
}
2024-09-28 14:54:39 -04:00
2024-09-29 23:02:04 -04:00
void mainLoop() {
while (!glfwWindowShouldClose(mWindow)) { glfwPollEvents(); }
}
2024-09-28 14:54:39 -04:00
2024-09-29 23:02:04 -04:00
void cleanup() {
// NOTE: instance destruction is handled by UniqueInstance
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
2024-09-29 20:12:56 -04:00
vk::ApplicationInfo appInfo { .pApplicationName = "Hello Triangle",
.applicationVersion = 1,
.pEngineName = "No Engine",
.engineVersion = 1,
2024-09-29 23:02:04 -04:00
.apiVersion = VK_API_VERSION_1_0 };
2024-09-25 23:03:56 -04:00
2024-09-29 20:12:56 -04:00
vk::InstanceCreateInfo createInfo { .pApplicationInfo = &appInfo };
2024-09-28 14:54:39 -04:00
2024-09-29 20:12:56 -04:00
// Retrieve extensions using custom function
2024-09-28 18:13:24 -04:00
std::vector<const char*> extensions = getRequiredExtensions();
2024-09-26 19:56:19 -04:00
2024-09-29 20:12:56 -04:00
vk::DebugUtilsMessengerCreateInfoEXT debugCreateInfo {};
2024-09-26 19:56:19 -04:00
if (enableValidationLayers) {
2024-09-29 20:12:56 -04:00
createInfo.setEnabledLayerCount(static_cast<uint32_t>(validationLayers.size()))
.setPpEnabledLayerNames(validationLayers.data());
2024-09-28 14:54:39 -04:00
2024-09-29 20:12:56 -04:00
createInfo.setPNext(&debugCreateInfo);
2024-09-26 19:56:19 -04:00
} else {
2024-09-29 20:12:56 -04:00
createInfo.setEnabledLayerCount(0).setPNext(nullptr);
2024-09-28 14:54:39 -04:00
}
2024-09-26 17:18:45 -04:00
2024-09-29 20:12:56 -04:00
// Enable the portability extension and set flags
extensions.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
createInfo.setEnabledExtensionCount(static_cast<uint32_t>(extensions.size()))
.setPpEnabledExtensionNames(extensions.data())
.setFlags(vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR);
2024-09-28 14:54:39 -04:00
2024-09-29 23:02:04 -04:00
fmt::println("Available extensions:");
for (const char* extension : extensions) fmt::println("\t{}", extension);
2024-09-25 23:03:56 -04:00
2024-09-29 20:12:56 -04:00
try {
2024-09-29 23:02:04 -04:00
mInstance = vk::createInstanceUnique(createInfo);
mLoader = vk::DispatchLoaderDynamic(mInstance.get(), vkGetInstanceProcAddr);
2024-09-29 20:12:56 -04:00
} catch (const vk::SystemError& err) {
throw std::runtime_error("Failed to create instance: " + std::string(err.what()));
}
2024-09-28 14:54:39 -04:00
}
2024-09-29 23:02:04 -04:00
static fn checkValidationLayerSupport() -> bool {
std::vector<vk::LayerProperties> availableLayers = vk::enumerateInstanceLayerProperties();
2024-09-28 18:13:24 -04:00
2024-09-29 23:02:04 -04:00
for (const char* layerName : validationLayers) {
bool layerFound = false;
2024-09-28 18:13:24 -04:00
2024-09-29 23:02:04 -04:00
for (const auto& layerProperties : availableLayers)
if (strcmp(layerName, layerProperties.layerName) == 0) {
layerFound = true;
break;
}
2024-09-28 18:13:24 -04:00
2024-09-29 23:02:04 -04:00
if (!layerFound)
return false;
2024-09-28 18:13:24 -04:00
}
2024-09-29 23:02:04 -04:00
return true;
2024-09-28 18:13:24 -04:00
}
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 VKAPI_ATTR fn VKAPI_CALL debugCallback(
VkDebugUtilsMessageSeverityFlagBitsEXT /*messageSeverity*/,
VkDebugUtilsMessageTypeFlagsEXT /*messageType*/,
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
void* /*pUserData*/
) -> VkBool32 {
2024-09-29 23:02:04 -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-29 23:02:04 -04:00
HelloTriangleApplication app;
2024-09-25 23:03:56 -04:00
try {
app.run();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}