This commit is contained in:
Bailey Harrison 2022-09-17 01:22:35 +01:00
parent 2c4a47541d
commit 40c0c024a1
6 changed files with 308 additions and 235 deletions

View File

@ -1,11 +1,5 @@
#pragma once #pragma once
#include <filesystem>
#include "log.hpp"
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>
namespace engine { namespace engine {
struct AppInfo { struct AppInfo {

View File

@ -4,13 +4,16 @@
#include "engine.hpp" #include "engine.hpp"
class Window; #include <SDL_video.h>
#include <memory>
namespace engine::gfx { namespace engine::gfx {
struct ENGINE_API Device { struct ENGINE_API Device {
Device(AppInfo appInfo, const Window& window); Device(AppInfo appInfo, SDL_Window* window);
~Device(); ~Device();
private: private:

View File

@ -124,14 +124,14 @@ public:
bool infoBox(const std::string& title, const std::string& msg); bool infoBox(const std::string& title, const std::string& msg);
std::vector<const char*> getRequiredVulkanExtensions() const;
/* STATIC METHODS */ /* STATIC METHODS */
static void errorBox(const std::string& message); static void errorBox(const std::string& message);
private: public:
SDL_Window* m_handle; SDL_Window* m_handle;
private:
bool m_shouldClose = false; bool m_shouldClose = false;
std::string m_title; std::string m_title;
@ -196,5 +196,4 @@ public:
void onMouseButtonEvent(SDL_MouseButtonEvent& e); void onMouseButtonEvent(SDL_MouseButtonEvent& e);
void onMouseMotionEvent(SDL_MouseMotionEvent& e); void onMouseMotionEvent(SDL_MouseMotionEvent& e);
void onMouseWheelEvent(SDL_MouseWheelEvent& e); void onMouseWheelEvent(SDL_MouseWheelEvent& e);
}; };

View File

@ -1,5 +1,7 @@
#include "engine.hpp" #include "engine.hpp"
#include <cstdio>
namespace engine { namespace engine {
bool versionFromCharArray(const char* version, int* major, int* minor, int* patch) bool versionFromCharArray(const char* version, int* major, int* minor, int* patch)

View File

@ -3,14 +3,13 @@
#include "gfx_device.hpp" #include "gfx_device.hpp"
#include "config.h" #include "config.h"
#include "window.hpp"
#include "log.hpp" #include "log.hpp"
#define VOLK_IMPLEMENTATION #define VOLK_IMPLEMENTATION
#include "volk.h" #include "volk.h"
#include <SDL_vulkan.h>
#include <assert.h> #include <assert.h>
#include <iostream> #include <iostream>
@ -18,99 +17,24 @@
namespace engine::gfx { namespace engine::gfx {
class Device::Impl { static std::vector<const char*> getRequiredVulkanExtensions(SDL_Window* window)
friend Device;
VkInstance m_instance;
#ifndef NDEBUG
VkDebugUtilsMessengerEXT m_debugMessenger;
#endif
static constexpr const char * VALIDATION_LAYER_NAME = "VK_LAYER_KHRONOS_validation";
static constexpr VkDebugUtilsMessageSeverityFlagBitsEXT MESSAGE_LEVEL = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
std::vector<VkLayerProperties> m_layersAvailable{};
std::optional<std::vector<VkLayerProperties>::iterator> m_validationLayer;
void findAvailableLayers()
{ {
VkResult res; #ifdef ENGINE_BUILD_VULKAN
SDL_bool res;
uint32_t layerCount; unsigned int sdlExtensionCount = 0;
res = vkEnumerateInstanceLayerProperties(&layerCount, nullptr); res = SDL_Vulkan_GetInstanceExtensions(window, &sdlExtensionCount, nullptr);
assert(res == VK_SUCCESS); assert(res == SDL_TRUE);
m_layersAvailable.resize(layerCount); std::vector<const char*> requiredExtensions(sdlExtensionCount);
res = vkEnumerateInstanceLayerProperties(&layerCount, m_layersAvailable.data()); res = SDL_Vulkan_GetInstanceExtensions(window, &sdlExtensionCount, requiredExtensions.data());
assert(res == VK_SUCCESS); assert(res == SDL_TRUE);
#ifndef NDEBUG return requiredExtensions;
for (auto it = m_layersAvailable.begin(); it != m_layersAvailable.end(); it++) { #else
TRACE("Found Vulkan layer: {}, {}", it->layerName, it->description); return std::vector<const char*>{};
if (strncmp(it->layerName, VALIDATION_LAYER_NAME, 256) == 0) {
m_validationLayer = it;
}
}
if (m_validationLayer.has_value() == false) {
WARN("The validation layer was not found. Continuing.");
}
#endif #endif
} }
void createInstance(AppInfo appInfo, const std::vector<const char*>& windowExtensions)
{
VkResult res;
int appVersionMajor, appVersionMinor, appVersionPatch;
assert(versionFromCharArray(appInfo.version, &appVersionMajor, &appVersionMinor, &appVersionPatch));
int engineVersionMajor, engineVersionMinor, engineVersionPatch;
assert(versionFromCharArray(ENGINE_VERSION, &engineVersionMajor, &engineVersionMinor, &engineVersionPatch));
VkApplicationInfo applicationInfo {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pNext = nullptr,
.pApplicationName = appInfo.name,
.applicationVersion = VK_MAKE_VERSION(appVersionMajor, appVersionMinor, appVersionPatch),
.pEngineName = "engine",
.engineVersion = VK_MAKE_VERSION(engineVersionMajor, engineVersionMinor, engineVersionPatch),
.apiVersion = VK_API_VERSION_1_0,
};
// make a list of all extensions to use
std::vector<const char*> extensions{};
extensions.insert(extensions.end(), windowExtensions.begin(), windowExtensions.end());
#ifndef NDEBUG
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
#endif
std::vector<const char*> layers{};
#ifndef NDEBUG
if (m_validationLayer.has_value())
layers.push_back(m_validationLayer.value()->layerName);
#endif
VkInstanceCreateInfo instanceInfo {
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.pApplicationInfo = &applicationInfo,
.enabledLayerCount = (uint32_t)layers.size(),
.ppEnabledLayerNames = layers.data(),
.enabledExtensionCount = (uint32_t)extensions.size(),
.ppEnabledExtensionNames = extensions.data(),
};
res = vkCreateInstance(&instanceInfo, nullptr, &m_instance);
if (res == VK_ERROR_INCOMPATIBLE_DRIVER) {
CRITICAL("The graphics driver is incompatible with vulkan");
throw std::runtime_error("Graphics driver is incompatible with Vulkan");
}
assert(res == VK_SUCCESS);
}
#ifndef NDEBUG
static VkBool32 debugMessenger( static VkBool32 debugMessenger(
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageTypes, VkDebugUtilsMessageTypeFlagsEXT messageTypes,
@ -145,16 +69,159 @@ namespace engine::gfx {
} }
return VK_FALSE; return VK_FALSE;
} }
#endif
void createDebugMessenger() class Device::Impl {
friend Device;
public:
Impl(AppInfo appInfo, SDL_Window* window)
{ {
m_instance = std::make_unique<Instance>(appInfo, getRequiredVulkanExtensions(window));
#ifndef NDEBUG #ifndef NDEBUG
m_debugMessenger = std::make_unique<DebugMessenger>(m_instance->getHandle());
#endif
}
private:
// VkSurfaceKHR m_surface;
static constexpr VkDebugUtilsMessageSeverityFlagBitsEXT MESSAGE_LEVEL = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
class Instance {
public:
Instance(AppInfo appInfo, const std::vector<const char*>& windowExtensions)
{
VkResult res; VkResult res;
if (m_validationLayer.has_value() == false) return; findAvailableLayers();
VkDebugUtilsMessengerCreateInfoEXT createInfo { int appVersionMajor, appVersionMinor, appVersionPatch;
assert(versionFromCharArray(appInfo.version, &appVersionMajor, &appVersionMinor, &appVersionPatch));
int engineVersionMajor, engineVersionMinor, engineVersionPatch;
assert(versionFromCharArray(ENGINE_VERSION, &engineVersionMajor, &engineVersionMinor, &engineVersionPatch));
VkApplicationInfo applicationInfo{
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pNext = nullptr,
.pApplicationName = appInfo.name,
.applicationVersion = VK_MAKE_VERSION(appVersionMajor, appVersionMinor, appVersionPatch),
.pEngineName = "engine",
.engineVersion = VK_MAKE_VERSION(engineVersionMajor, engineVersionMinor, engineVersionPatch),
.apiVersion = VK_API_VERSION_1_0,
};
// make a list of all extensions to use
std::vector<const char*> extensions{};
extensions.insert(extensions.end(), windowExtensions.begin(), windowExtensions.end());
std::vector<const char*> layers{};
if (m_validationLayer.has_value()) {
layers.push_back(m_validationLayer.value()->layerName);
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
}
VkInstanceCreateInfo instanceInfo{
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.pApplicationInfo = &applicationInfo,
.enabledLayerCount = (uint32_t)layers.size(),
.ppEnabledLayerNames = layers.data(),
.enabledExtensionCount = (uint32_t)extensions.size(),
.ppEnabledExtensionNames = extensions.data(),
};
if (m_validationLayer.has_value()) {
VkDebugUtilsMessengerCreateInfoEXT debugMessengerCreateInfo = DebugMessenger::getCreateInfo();
instanceInfo.pNext = &debugMessengerCreateInfo;
}
#ifndef NDEBUG
for (const char* ext : extensions) {
TRACE("Using Vulkan instance extension: {}", ext);
}
#endif
res = vkCreateInstance(&instanceInfo, nullptr, &m_handle);
if (res == VK_ERROR_INCOMPATIBLE_DRIVER) {
CRITICAL("The graphics driver is incompatible with vulkan");
throw std::runtime_error("Graphics driver is incompatible with Vulkan");
}
assert(res == VK_SUCCESS);
volkLoadInstance(m_handle);
}
~Instance()
{
vkDestroyInstance(m_handle, nullptr);
}
VkInstance getHandle()
{
return m_handle;
}
private:
VkInstance m_handle;
std::vector<VkLayerProperties> m_layersAvailable{};
static constexpr const char* VALIDATION_LAYER_NAME = "VK_LAYER_KHRONOS_validation";
std::optional<std::vector<VkLayerProperties>::iterator> m_validationLayer;
void findAvailableLayers()
{
VkResult res;
uint32_t layerCount;
res = vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
assert(res == VK_SUCCESS);
m_layersAvailable.resize(layerCount);
res = vkEnumerateInstanceLayerProperties(&layerCount, m_layersAvailable.data());
assert(res == VK_SUCCESS);
#ifndef NDEBUG
// find validation layer and print all layers to log
for (auto it = m_layersAvailable.begin(); it != m_layersAvailable.end(); it++) {
// TRACE("Found Vulkan layer: {}, {}", it->layerName, it->description);
if (strncmp(it->layerName, VALIDATION_LAYER_NAME, 256) == 0) {
m_validationLayer = it;
}
}
if (m_validationLayer.has_value() == false) {
CRITICAL("The validation layer was not found. Quitting.");
throw std::runtime_error("Validation layer not found");
}
#endif
}
};
#ifndef NDEBUG
class DebugMessenger {
public:
DebugMessenger(const VkInstance& instance) : m_instance(instance)
{
VkDebugUtilsMessengerCreateInfoEXT createInfo = getCreateInfo();
VkResult res = vkCreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &m_debugMessenger);
assert(res == VK_SUCCESS);
}
~DebugMessenger()
{
vkDestroyDebugUtilsMessengerEXT(m_instance, m_debugMessenger, nullptr);
}
static VkDebugUtilsMessengerCreateInfoEXT getCreateInfo()
{
VkDebugUtilsMessengerCreateInfoEXT createInfo{
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT, .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
.pNext = nullptr, .pNext = nullptr,
.flags = 0, .flags = 0,
@ -184,28 +251,52 @@ namespace engine::gfx {
break; break;
} }
res = vkCreateDebugUtilsMessengerEXT(m_instance, &createInfo, nullptr, &m_debugMessenger); return createInfo;
assert(res == VK_SUCCESS);
#endif
} }
private:
VkDebugUtilsMessengerEXT m_debugMessenger;
const VkInstance& m_instance;
};
#endif
std::unique_ptr<Instance> m_instance;
#ifndef NDEBUG
std::unique_ptr<DebugMessenger> m_debugMessenger;
#endif
/*
void createSurface(SDL_Window* window)
{
if (SDL_Vulkan_CreateSurface(window, m_instance, &m_surface) == false) {
CRITICAL("Unable to create window surface");
throw std::runtime_error("Unable to create window surface");
}
}
*/
}; };
Device::Device(AppInfo appInfo, SDL_Window* window)
Device::Device(AppInfo appInfo, const Window& window) : pimpl(std::make_unique<Impl>())
{ {
pimpl->findAvailableLayers(); VkResult res;
pimpl->createInstance(appInfo, window.getRequiredVulkanExtensions()); res = volkInitialize();
pimpl->createDebugMessenger(); if (res == VK_ERROR_INITIALIZATION_FAILED) {
CRITICAL("Unable to load vulkan, is it installed?");
throw std::runtime_error("Unable to load vulkan, is it installed?");
}
assert(res == VK_SUCCESS);
pimpl = std::make_unique<Impl>(appInfo, window);
//pimpl->createDebugMessenger();
} }
Device::~Device() Device::~Device()
{ {
vkDestroyInstance(pimpl->m_instance, nullptr); //pimpl->cleanup();
#ifndef NDEBUG
vkDestroyDebugUtilsMessengerEXT(pimpl->m_instance, pimpl->m_debugMessenger, nullptr);
#endif
} }
} }

View File

@ -1,12 +1,10 @@
#include "window.hpp" #include "window.hpp"
#include "log.hpp"
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#ifdef ENGINE_BUILD_VULKAN
#include <SDL2/SDL_vulkan.h>
#endif
const uint64_t BILLION = 1000000000; const uint64_t BILLION = 1000000000;
Window::Window(const std::string& title) : m_title(title) Window::Window(const std::string& title) : m_title(title)
@ -32,7 +30,7 @@ Window::Window(const std::string& title) : m_title(title)
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
static_cast<int>(m_winSize.x), static_cast<int>(m_winSize.x),
static_cast<int>(m_winSize.y), static_cast<int>(m_winSize.y),
0); SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN);
if (m_handle == NULL) { if (m_handle == NULL) {
SDL_Quit(); SDL_Quit();
throw std::runtime_error("Unable to create window: " + std::string(SDL_GetError())); throw std::runtime_error("Unable to create window: " + std::string(SDL_GetError()));
@ -447,20 +445,6 @@ bool Window::infoBox(const std::string& title, const std::string& msg)
} }
} }
std::vector<const char*> Window::getRequiredVulkanExtensions() const
{
#ifdef ENGINE_BUILD_VULKAN
unsigned int sdlExtensionCount = 0;
SDL_Vulkan_GetInstanceExtensions(m_handle, &sdlExtensionCount, nullptr);
std::vector<const char*> requiredExtensions(sdlExtensionCount);
SDL_Vulkan_GetInstanceExtensions(m_handle, &sdlExtensionCount, requiredExtensions.data());
return requiredExtensions;
#else
return std::vector<const char*>{};
#endif
}
/* STATIC METHODS */ /* STATIC METHODS */
// Display an error message box // Display an error message box