mirror of
https://github.com/bailwillharr/engine.git
synced 2024-09-21 04:51:18 +00:00
gfx work
This commit is contained in:
parent
2c4a47541d
commit
40c0c024a1
@ -1,11 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
#include "log.hpp"
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <spdlog/sinks/basic_file_sink.h>
|
||||
|
||||
namespace engine {
|
||||
|
||||
struct AppInfo {
|
||||
|
@ -4,13 +4,16 @@
|
||||
|
||||
#include "engine.hpp"
|
||||
|
||||
class Window;
|
||||
#include <SDL_video.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
||||
namespace engine::gfx {
|
||||
|
||||
struct ENGINE_API Device {
|
||||
|
||||
Device(AppInfo appInfo, const Window& window);
|
||||
Device(AppInfo appInfo, SDL_Window* window);
|
||||
~Device();
|
||||
|
||||
private:
|
||||
|
@ -124,14 +124,14 @@ public:
|
||||
|
||||
bool infoBox(const std::string& title, const std::string& msg);
|
||||
|
||||
std::vector<const char*> getRequiredVulkanExtensions() const;
|
||||
|
||||
/* STATIC METHODS */
|
||||
static void errorBox(const std::string& message);
|
||||
|
||||
private:
|
||||
public:
|
||||
SDL_Window* m_handle;
|
||||
|
||||
private:
|
||||
|
||||
bool m_shouldClose = false;
|
||||
|
||||
std::string m_title;
|
||||
@ -196,5 +196,4 @@ public:
|
||||
void onMouseButtonEvent(SDL_MouseButtonEvent& e);
|
||||
void onMouseMotionEvent(SDL_MouseMotionEvent& e);
|
||||
void onMouseWheelEvent(SDL_MouseWheelEvent& e);
|
||||
|
||||
};
|
||||
|
@ -1,5 +1,7 @@
|
||||
#include "engine.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
namespace engine {
|
||||
|
||||
bool versionFromCharArray(const char* version, int* major, int* minor, int* patch)
|
||||
|
@ -3,14 +3,13 @@
|
||||
#include "gfx_device.hpp"
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "window.hpp"
|
||||
|
||||
#include "log.hpp"
|
||||
|
||||
#define VOLK_IMPLEMENTATION
|
||||
#include "volk.h"
|
||||
|
||||
#include <SDL_vulkan.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <iostream>
|
||||
@ -18,99 +17,24 @@
|
||||
|
||||
namespace engine::gfx {
|
||||
|
||||
class Device::Impl {
|
||||
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()
|
||||
static std::vector<const char*> getRequiredVulkanExtensions(SDL_Window* window)
|
||||
{
|
||||
VkResult res;
|
||||
#ifdef ENGINE_BUILD_VULKAN
|
||||
SDL_bool 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);
|
||||
unsigned int sdlExtensionCount = 0;
|
||||
res = SDL_Vulkan_GetInstanceExtensions(window, &sdlExtensionCount, nullptr);
|
||||
assert(res == SDL_TRUE);
|
||||
std::vector<const char*> requiredExtensions(sdlExtensionCount);
|
||||
res = SDL_Vulkan_GetInstanceExtensions(window, &sdlExtensionCount, requiredExtensions.data());
|
||||
assert(res == SDL_TRUE);
|
||||
|
||||
#ifndef NDEBUG
|
||||
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) {
|
||||
WARN("The validation layer was not found. Continuing.");
|
||||
}
|
||||
return requiredExtensions;
|
||||
#else
|
||||
return std::vector<const char*>{};
|
||||
#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(
|
||||
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
||||
VkDebugUtilsMessageTypeFlagsEXT messageTypes,
|
||||
@ -145,16 +69,159 @@ namespace engine::gfx {
|
||||
}
|
||||
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
|
||||
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;
|
||||
|
||||
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,
|
||||
.pNext = nullptr,
|
||||
.flags = 0,
|
||||
@ -184,28 +251,52 @@ namespace engine::gfx {
|
||||
break;
|
||||
}
|
||||
|
||||
res = vkCreateDebugUtilsMessengerEXT(m_instance, &createInfo, nullptr, &m_debugMessenger);
|
||||
assert(res == VK_SUCCESS);
|
||||
#endif
|
||||
return createInfo;
|
||||
}
|
||||
|
||||
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, const Window& window) : pimpl(std::make_unique<Impl>())
|
||||
Device::Device(AppInfo appInfo, SDL_Window* window)
|
||||
{
|
||||
pimpl->findAvailableLayers();
|
||||
pimpl->createInstance(appInfo, window.getRequiredVulkanExtensions());
|
||||
pimpl->createDebugMessenger();
|
||||
VkResult res;
|
||||
res = volkInitialize();
|
||||
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()
|
||||
{
|
||||
vkDestroyInstance(pimpl->m_instance, nullptr);
|
||||
#ifndef NDEBUG
|
||||
vkDestroyDebugUtilsMessengerEXT(pimpl->m_instance, pimpl->m_debugMessenger, nullptr);
|
||||
#endif
|
||||
//pimpl->cleanup();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,12 +1,10 @@
|
||||
#include "window.hpp"
|
||||
|
||||
#include "log.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
#ifdef ENGINE_BUILD_VULKAN
|
||||
#include <SDL2/SDL_vulkan.h>
|
||||
#endif
|
||||
|
||||
const uint64_t BILLION = 1000000000;
|
||||
|
||||
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,
|
||||
static_cast<int>(m_winSize.x),
|
||||
static_cast<int>(m_winSize.y),
|
||||
0);
|
||||
SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN);
|
||||
if (m_handle == NULL) {
|
||||
SDL_Quit();
|
||||
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 */
|
||||
|
||||
// Display an error message box
|
||||
|
Loading…
Reference in New Issue
Block a user