Properly seperated/cleaned up OpenGL and Vulkan implementations.
This commit is contained in:
45
public/raid/internal/opengl.hpp
Normal file
45
public/raid/internal/opengl.hpp
Normal file
@@ -0,0 +1,45 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(RAID_INTERNAL_OPENGL_HPP_INCLUDED)
|
||||
#define RAID_INTERNAL_OPENGL_HPP_INCLUDED 1
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace raid
|
||||
{
|
||||
struct MixinOpenGLApplication
|
||||
{
|
||||
using GLbitfield = std::uint32_t;
|
||||
using GLint = std::int32_t;
|
||||
using GLuint = std::uint32_t;
|
||||
using GLsizei = std::int32_t;
|
||||
using GLenum = std::uint32_t;
|
||||
using GLfloat = float;
|
||||
|
||||
using glClear_fn_t = void (*)(GLbitfield);
|
||||
using glClearColor_fn_t = void (*)(GLfloat, GLfloat, GLfloat, GLfloat);
|
||||
using glGenTextures_fn_t = void (*)(GLsizei, GLuint*);
|
||||
using glBindTexture_fn_t = void (*)(GLenum, GLuint);
|
||||
using glTexParameteri_fn_t = void (*)(GLenum, GLenum, GLint);
|
||||
using glPixelStorei_fn_t = void (*)(GLenum, GLint);
|
||||
using glTexImage2D_fn_t = void (*)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const void*);
|
||||
using glDeleteTextures_fn_t = void (*)(GLsizei, const GLuint*);
|
||||
|
||||
struct OpenGLData
|
||||
{
|
||||
SDL_GLContext context;
|
||||
|
||||
glClear_fn_t Clear;
|
||||
glClearColor_fn_t ClearColor;
|
||||
glGenTextures_fn_t GenTextures;
|
||||
glBindTexture_fn_t BindTexture;
|
||||
glTexParameteri_fn_t TexParameteri;
|
||||
glPixelStorei_fn_t PixelStorei;
|
||||
glTexImage2D_fn_t TexImage2D;
|
||||
glDeleteTextures_fn_t DeleteTextures;
|
||||
};
|
||||
};
|
||||
} // namespace raid
|
||||
|
||||
#endif // !defined(RAID_INTERNAL_OPENGL_HPP_INCLUDED)
|
||||
176
public/raid/internal/vulkan.hpp
Normal file
176
public/raid/internal/vulkan.hpp
Normal file
@@ -0,0 +1,176 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(RAID_INTERNAL_VULKAN_HPP_INCLUDED)
|
||||
#define RAID_INTERNAL_VULKAN_HPP_INCLUDED 1
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#if !defined(RAID_USE_VULKAN_H)
|
||||
#define RAID_USE_VULKAN_H 0
|
||||
#endif
|
||||
|
||||
#if RAID_USE_VULKAN_H
|
||||
#include <vulkan/vulkan.h>
|
||||
#endif
|
||||
|
||||
namespace raid
|
||||
{
|
||||
struct MixinVulkanApplication
|
||||
{
|
||||
#if !RAID_USE_VULKAN_H
|
||||
// flags
|
||||
using VkFlags = std::uint32_t;
|
||||
using VkInstanceCreateFlags = VkFlags;
|
||||
using VkQueueFlags = VkFlags;
|
||||
using VkDeviceCreateFlags = VkFlags;
|
||||
using VkDeviceQueueCreateFlags = VkFlags;
|
||||
|
||||
// enums
|
||||
enum VkResult
|
||||
{
|
||||
VK_SUCCESS = 0
|
||||
};
|
||||
enum VkStructureType
|
||||
{
|
||||
VK_STRUCTURE_TYPE_APPLICATION_INFO = 0,
|
||||
VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO = 1,
|
||||
VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO = 2,
|
||||
VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO = 3,
|
||||
VK_STRUCTURE_TYPE_QUEUE_FAMILY_PROPERTIES_2 = 1000059005
|
||||
};
|
||||
enum VkSystemAllocationScope {};
|
||||
enum VkInternalAllocationType {};
|
||||
enum VkQueueFlagBits : VkFlags
|
||||
{
|
||||
VK_QUEUE_GRAPHICS_BIT = 0x00000001
|
||||
};
|
||||
|
||||
// handles
|
||||
using VkDevice = struct VkDevice_*;
|
||||
using VkQueue = struct VkQueue_*;
|
||||
|
||||
// structs
|
||||
struct VkExtent3D
|
||||
{
|
||||
std::uint32_t width = 0;
|
||||
std::uint32_t height = 0;
|
||||
std::uint32_t depth = 0;
|
||||
};
|
||||
|
||||
struct VkApplicationInfo
|
||||
{
|
||||
VkStructureType sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
||||
const void* pNext = nullptr;
|
||||
const char* pApplicationNane = nullptr;
|
||||
std::uint32_t applicationVersion = 0;
|
||||
const char* pEngineName = nullptr;
|
||||
std::uint32_t engineVersion = 0;
|
||||
std::uint32_t apiVersion = 0;
|
||||
};
|
||||
struct VkInstanceCreateInfo
|
||||
{
|
||||
VkStructureType sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
||||
const void* pNext = nullptr;
|
||||
VkInstanceCreateFlags flags = 0;
|
||||
const VkApplicationInfo* pApplicationInfo = nullptr;
|
||||
std::uint32_t enabledLayerCount = 0;
|
||||
const char* const* ppEnabledLayerNames = nullptr;
|
||||
std::uint32_t enabledExtensionCount = 0;
|
||||
const char* const* ppEnabledExtensionNames = nullptr;
|
||||
};
|
||||
|
||||
struct VkQueueFamilyProperties
|
||||
{
|
||||
VkQueueFlags queueFlags = 0;
|
||||
std::uint32_t queueCount = 0;
|
||||
std::uint32_t timestampValidBits = 0;
|
||||
VkExtent3D minImageTransferGranularity = {};
|
||||
};
|
||||
|
||||
struct VkQueueFamilyProperties2
|
||||
{
|
||||
VkStructureType sType = VK_STRUCTURE_TYPE_QUEUE_FAMILY_PROPERTIES_2;
|
||||
void* pNext = nullptr;
|
||||
VkQueueFamilyProperties queueFamilyProperties = {};
|
||||
};
|
||||
|
||||
struct VkDeviceQueueCreateInfo
|
||||
{
|
||||
VkStructureType sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
||||
const void* pNext = nullptr;
|
||||
VkDeviceQueueCreateFlags flags = 0;
|
||||
std::uint32_t queueFamilyIndex = 0;
|
||||
std::uint32_t queueCount = 0;
|
||||
const float* pQueuePriorities = nullptr;
|
||||
};
|
||||
|
||||
struct VkPhysicalDeviceFeatures;
|
||||
struct VkDeviceCreateInfo
|
||||
{
|
||||
VkStructureType sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
||||
const void* pNext = nullptr;
|
||||
VkDeviceCreateFlags flags;
|
||||
std::uint32_t queueCreateInfoCount = 0;
|
||||
const VkDeviceQueueCreateInfo* pQueueCreateInfos = nullptr;
|
||||
std::uint32_t enabledLayerCount = 0;
|
||||
const char* const* ppEnabledLayerNames = nullptr;
|
||||
uint32_t enabledExtensionCount = 0;
|
||||
const char* const* ppEnabledExtensionNames = nullptr;
|
||||
const VkPhysicalDeviceFeatures* pEnabledFeatures = nullptr;
|
||||
};
|
||||
|
||||
// Vulkan function pointer types
|
||||
using PFN_vkVoidFunction = void (*)();
|
||||
// TODO: VKAPI_PTR?
|
||||
using PFN_vkAllocationFunction = void* (*)(void* pUserData, std::size_t size, std::size_t alignment, VkSystemAllocationScope allocationScope);
|
||||
using PFN_vkReallocationFunction = void* (*)(void* pUserData, void* pOriginal, std::size_t size, std::size_t alignment, VkSystemAllocationScope allocationScope);
|
||||
using PFN_vkFreeFunction = void (*)(void* pUserData, void* pMemory);
|
||||
using PFN_vkInternalAllocationNotification = void (*)(void* pUserData, std::size_t size, VkInternalAllocationType allocationType, VkSystemAllocationScope allocationScope);
|
||||
using PFN_vkInternalFreeNotification = void (*)(void* pUserData, std::size_t size, VkInternalAllocationType allocationType, VkSystemAllocationScope allocationScope);
|
||||
struct VkAllocationCallbacks
|
||||
{
|
||||
void* pUserData = nullptr;
|
||||
PFN_vkAllocationFunction pfnAllocation = nullptr;
|
||||
PFN_vkReallocationFunction pfnReallocation = nullptr;
|
||||
PFN_vkFreeFunction pfnFree = nullptr;
|
||||
PFN_vkInternalAllocationNotification pfnInternalAllocation = nullptr;
|
||||
PFN_vkInternalFreeNotification pfnInternalFree = nullptr;
|
||||
};
|
||||
|
||||
// function pointers
|
||||
using vkGetInstanceProcAddr_fn_t = PFN_vkVoidFunction (*)(VkInstance instance, const char* pName);
|
||||
using vkCreateInstance_fn_t = VkResult (*)(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance);
|
||||
using vkDestroyInstance_fn_t = void (*)(VkInstance instance, const VkAllocationCallbacks* pAllocator);
|
||||
using vkEnumeratePhysicalDevices_fn_t = VkResult (*)(VkInstance instance, std::uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices);
|
||||
using vkGetPhysicalDeviceQueueFamilyProperties2_fn_t = void (*)(VkPhysicalDevice physicalDevice, std::uint32_t* pQueueFamilyPropertyCount, VkQueueFamilyProperties2* pQueueFamilyProperties);
|
||||
using vkCreateDevice_fn_t = VkResult (*)(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice);
|
||||
using vkDestroyDevice_fn_t = void (*)(VkDevice device, const VkAllocationCallbacks* pAllocator);
|
||||
using vkDeviceWaitIdle_fn_t = VkResult (*)(VkDevice device);
|
||||
using vkGetDeviceQueue_fn_t = void (*)(VkDevice device, std::uint32_t queueFamilyIndex, std::uint32_t queueIndex, VkQueue* pQueue);
|
||||
#endif // !RAID_USE_VULKAN_H
|
||||
|
||||
struct VulkanData
|
||||
{
|
||||
VkInstance instance;
|
||||
VkPhysicalDevice physicalDevice;
|
||||
VkDevice device;
|
||||
VkQueue queue;
|
||||
VkSurfaceKHR surface;
|
||||
|
||||
vkGetInstanceProcAddr_fn_t GetInstanceProc;
|
||||
vkCreateInstance_fn_t CreateInstance;
|
||||
vkDestroyInstance_fn_t DestroyInstance;
|
||||
vkEnumeratePhysicalDevices_fn_t EnumeratePhysicalDevices;
|
||||
vkGetPhysicalDeviceQueueFamilyProperties2_fn_t GetPhysicalDeviceQueueFamilyProperties2;
|
||||
vkCreateDevice_fn_t CreateDevice;
|
||||
vkDestroyDevice_fn_t DestroyDevice;
|
||||
vkDeviceWaitIdle_fn_t DeviceWaitIdle;
|
||||
vkGetDeviceQueue_fn_t GetDeviceQueue;
|
||||
|
||||
std::uint32_t queueFamilyIndex;
|
||||
};
|
||||
};
|
||||
} // namespace raid
|
||||
|
||||
#endif // !defined(RAID_INTERNAL_VULKAN_HPP_INCLUDED)
|
||||
@@ -14,6 +14,9 @@
|
||||
#include <mijin/virtual_filesystem/memory.hpp>
|
||||
#include <mijin/virtual_filesystem/stacked.hpp>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_vulkan.h>
|
||||
#include "./internal/opengl.hpp"
|
||||
#include "./internal/vulkan.hpp"
|
||||
|
||||
namespace raid
|
||||
{
|
||||
@@ -61,16 +64,22 @@ struct ApplicationFlags : mijin::BitFlags<ApplicationFlags>
|
||||
bool x11OnWayland : 1 = false;
|
||||
};
|
||||
|
||||
enum class GraphicsAPI : std::uint8_t
|
||||
{
|
||||
OPENGL,
|
||||
VULKAN
|
||||
};
|
||||
|
||||
struct ApplicationConfig
|
||||
{
|
||||
ApplicationFlags flags = {};
|
||||
GraphicsAPI graphicsApi = GraphicsAPI::OPENGL;
|
||||
};
|
||||
|
||||
class Application
|
||||
class Application : private MixinOpenGLApplication, MixinVulkanApplication
|
||||
{
|
||||
private:
|
||||
SDL_Window* mWindow = nullptr;
|
||||
SDL_GLContext mGLContext = nullptr;
|
||||
|
||||
mijin::StackedFileSystemAdapter mFS;
|
||||
mijin::MemoryFileSystemAdapter* mMemoryFS = nullptr;
|
||||
@@ -82,30 +91,11 @@ private:
|
||||
std::unordered_map<ImGuiStyleVar, std::variant<float, ImVec2>> mMainWindowStyles;
|
||||
const ApplicationConfig mConfig;
|
||||
|
||||
using GLbitfield = std::uint32_t;
|
||||
using GLint = std::int32_t;
|
||||
using GLuint = std::uint32_t;
|
||||
using GLsizei = std::int32_t;
|
||||
using GLenum = std::uint32_t;
|
||||
using GLfloat = float;
|
||||
|
||||
using glClear_fn_t = void (*)(GLbitfield);
|
||||
using glClearColor_fn_t = void (*)(GLfloat, GLfloat, GLfloat, GLfloat);
|
||||
using glGenTextures_fn_t = void (*)(GLsizei, GLuint*);
|
||||
using glBindTexture_fn_t = void (*)(GLenum, GLuint);
|
||||
using glTexParameteri_fn_t = void (*)(GLenum, GLenum, GLint);
|
||||
using glPixelStorei_fn_t = void (*)(GLenum, GLint);
|
||||
using glTexImage2D_fn_t = void (*)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const void*);
|
||||
using glDeleteTextures_fn_t = void (*)(GLsizei, const GLuint*);
|
||||
|
||||
glClear_fn_t glClear;
|
||||
glClearColor_fn_t glClearColor;
|
||||
glGenTextures_fn_t glGenTextures;
|
||||
glBindTexture_fn_t glBindTexture;
|
||||
glTexParameteri_fn_t glTexParameteri;
|
||||
glPixelStorei_fn_t glPixelStorei;
|
||||
glTexImage2D_fn_t glTexImage2D;
|
||||
glDeleteTextures_fn_t glDeleteTextures;
|
||||
union
|
||||
{
|
||||
OpenGLData gl;
|
||||
VulkanData vk;
|
||||
};
|
||||
public:
|
||||
explicit Application(ApplicationConfig config = {}) noexcept : mConfig(config) {}
|
||||
virtual ~Application() = default;
|
||||
@@ -149,6 +139,8 @@ public:
|
||||
[[nodiscard]]
|
||||
ImTextureID getOrLoadTexture(fs::path path);
|
||||
|
||||
void destroyTexture(ImTextureID texture);
|
||||
|
||||
protected:
|
||||
virtual void render() = 0;
|
||||
virtual std::string getFolderName() = 0;
|
||||
@@ -215,7 +207,8 @@ protected:
|
||||
virtual void cleanup();
|
||||
private:
|
||||
bool initSDL();
|
||||
bool initGL();
|
||||
bool initOpenGL();
|
||||
bool initVulkan();
|
||||
bool initImGui();
|
||||
void handleSDLEvents();
|
||||
void loadImGuiConfig();
|
||||
|
||||
Reference in New Issue
Block a user