adding D3D11 base, working on Vulkan backend

This commit is contained in:
2024-10-08 09:40:39 +02:00
parent 9f753ecae3
commit 8de4b8677b
21 changed files with 755 additions and 16 deletions

30
Sources/Backends/Vulkan/Vulkan.c git.filemode.normal_file
View File

@@ -0,0 +1,30 @@
// Copyright (C) 2024 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <Pulse.h>
#include "../../PulseInternal.h"
#include "Vulkan.h"
#include "VulkanLoader.h"
VulkanGlobal* VulkanGetGlobal()
{
static VulkanGlobal vulkan = { 0 };
return &vulkan;
}
bool VulkanLoadBackend()
{
if(!VulkanInitLoader())
return false;
return true;
}
PulseBackendLoader VulkanDriver = {
.PFN_LoadBackend = VulkanLoadBackend,
.PFN_CreateDevice = PULSE_NULLPTR,
.backend = PULSE_BACKEND_VULKAN,
.supported_shader_formats = PULSE_SHADER_FORMAT_SPIRV_BIT
};

32
Sources/Backends/Vulkan/Vulkan.h git.filemode.normal_file
View File

@@ -0,0 +1,32 @@
// Copyright (C) 2024 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifdef PULSE_ENABLE_VULKAN_BACKEND
#ifndef PULSE_VULKAN_H_
#define PULSE_VULKAN_H_
#include <vulkan/vulkan_core.h>
#include "VulkanDevice.h"
#include "VulkanInstance.h"
typedef struct VulkanGlobal
{
#define PULSE_VULKAN_GLOBAL_FUNCTION(fn) PFN_##fn fn;
#include "VulkanGlobalPrototypes.h"
#undef PULSE_VULKAN_GLOBAL_FUNCTION
} VulkanGlobal;
typedef struct VulkanContext
{
VulkanInstance instance;
VulkanDevice device;
} VulkanContext;
VulkanGlobal* VulkanGetGlobal();
#endif // PULSE_VULKAN_H_
#endif // PULSE_ENABLE_VULKAN_BACKEND

35
Sources/Backends/Vulkan/VulkanDevice.h git.filemode.normal_file
View File

@@ -0,0 +1,35 @@
// Copyright (C) 2024 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifdef PULSE_ENABLE_VULKAN_BACKEND
#ifndef PULSE_VULKAN_DEVICE_H_
#define PULSE_VULKAN_DEVICE_H_
#include <vulkan/vulkan_core.h>
#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0
#define VMA_VULKAN_VERSION 1000000
#include <vk_mem_alloc.h>
typedef struct VulkanDevice
{
VkPhysicalDeviceFeatures features;
VkPhysicalDeviceMemoryProperties memory_properties;
VkPhysicalDeviceProperties properties;
VkPhysicalDevice physical;
VkDevice device;
VmaAllocator allocator;
#define PULSE_VULKAN_DEVICE_FUNCTION(fn) PFN_##fn fn;
#include "VulkanDevicePrototypes.h"
#undef PULSE_VULKAN_DEVICE_FUNCTION
} VulkanDevice;
#endif // PULSE_VULKAN_DEVICE_H_
#endif // PULSE_ENABLE_VULKAN_BACKEND

View File

@@ -0,0 +1,75 @@
// Copyright (C) 2024 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
// No header guard
#ifndef PULSE_VULKAN_DEVICE_FUNCTION
#error "You must define PULSE_VULKAN_DEVICE_FUNCTION before including this file"
#endif
#ifdef VK_VERSION_1_0
PULSE_VULKAN_DEVICE_FUNCTION(vkAllocateCommandBuffers)
PULSE_VULKAN_DEVICE_FUNCTION(vkAllocateDescriptorSets)
PULSE_VULKAN_DEVICE_FUNCTION(vkAllocateMemory)
PULSE_VULKAN_DEVICE_FUNCTION(vkBeginCommandBuffer)
PULSE_VULKAN_DEVICE_FUNCTION(vkBindBufferMemory)
PULSE_VULKAN_DEVICE_FUNCTION(vkBindImageMemory)
PULSE_VULKAN_DEVICE_FUNCTION(vkCmdBindDescriptorSets)
PULSE_VULKAN_DEVICE_FUNCTION(vkCmdBindPipeline)
PULSE_VULKAN_DEVICE_FUNCTION(vkCmdCopyBuffer)
PULSE_VULKAN_DEVICE_FUNCTION(vkCmdDispatch)
PULSE_VULKAN_DEVICE_FUNCTION(vkCmdDispatchIndirect)
PULSE_VULKAN_DEVICE_FUNCTION(vkCmdExecuteCommands)
PULSE_VULKAN_DEVICE_FUNCTION(vkCmdFillBuffer)
PULSE_VULKAN_DEVICE_FUNCTION(vkCmdPipelineBarrier)
PULSE_VULKAN_DEVICE_FUNCTION(vkCmdPushConstants)
PULSE_VULKAN_DEVICE_FUNCTION(vkCmdUpdateBuffer)
PULSE_VULKAN_DEVICE_FUNCTION(vkCreateBuffer)
PULSE_VULKAN_DEVICE_FUNCTION(vkCreateBufferView)
PULSE_VULKAN_DEVICE_FUNCTION(vkCreateCommandPool)
PULSE_VULKAN_DEVICE_FUNCTION(vkCreateComputePipelines)
PULSE_VULKAN_DEVICE_FUNCTION(vkCreateDescriptorPool)
PULSE_VULKAN_DEVICE_FUNCTION(vkCreateDescriptorSetLayout)
PULSE_VULKAN_DEVICE_FUNCTION(vkCreateFence)
PULSE_VULKAN_DEVICE_FUNCTION(vkCreateImage)
PULSE_VULKAN_DEVICE_FUNCTION(vkCreatePipelineCache)
PULSE_VULKAN_DEVICE_FUNCTION(vkCreatePipelineLayout)
PULSE_VULKAN_DEVICE_FUNCTION(vkCreateSemaphore)
PULSE_VULKAN_DEVICE_FUNCTION(vkCreateShaderModule)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroyBuffer)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroyBufferView)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroyCommandPool)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroyDescriptorPool)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroyDescriptorSetLayout)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroyDevice)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroyFence)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroyImage)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroyPipeline)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroyPipelineCache)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroyPipelineLayout)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroySemaphore)
PULSE_VULKAN_DEVICE_FUNCTION(vkDestroyShaderModule)
PULSE_VULKAN_DEVICE_FUNCTION(vkDeviceWaitIdle)
PULSE_VULKAN_DEVICE_FUNCTION(vkEndCommandBuffer)
PULSE_VULKAN_DEVICE_FUNCTION(vkFlushMappedMemoryRanges)
PULSE_VULKAN_DEVICE_FUNCTION(vkFreeCommandBuffers)
PULSE_VULKAN_DEVICE_FUNCTION(vkFreeMemory)
PULSE_VULKAN_DEVICE_FUNCTION(vkGetBufferMemoryRequirements)
PULSE_VULKAN_DEVICE_FUNCTION(vkGetDeviceMemoryCommitment)
PULSE_VULKAN_DEVICE_FUNCTION(vkGetDeviceQueue)
PULSE_VULKAN_DEVICE_FUNCTION(vkGetFenceStatus)
PULSE_VULKAN_DEVICE_FUNCTION(vkGetImageMemoryRequirements)
PULSE_VULKAN_DEVICE_FUNCTION(vkInvalidateMappedMemoryRanges)
PULSE_VULKAN_DEVICE_FUNCTION(vkMapMemory)
PULSE_VULKAN_DEVICE_FUNCTION(vkMergePipelineCaches)
PULSE_VULKAN_DEVICE_FUNCTION(vkQueueSubmit)
PULSE_VULKAN_DEVICE_FUNCTION(vkQueueWaitIdle)
PULSE_VULKAN_DEVICE_FUNCTION(vkResetCommandBuffer)
PULSE_VULKAN_DEVICE_FUNCTION(vkResetCommandPool)
PULSE_VULKAN_DEVICE_FUNCTION(vkResetDescriptorPool)
PULSE_VULKAN_DEVICE_FUNCTION(vkResetFences)
PULSE_VULKAN_DEVICE_FUNCTION(vkUnmapMemory)
PULSE_VULKAN_DEVICE_FUNCTION(vkUpdateDescriptorSets)
PULSE_VULKAN_DEVICE_FUNCTION(vkWaitForFences)
#endif

View File

@@ -0,0 +1,16 @@
// Copyright (C) 2024 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
// No header guard
#ifndef PULSE_VULKAN_GLOBAL_FUNCTION
#error "You must define PULSE_VULKAN_GLOBAL_FUNCTION before including this file"
#endif
#ifdef VK_VERSION_1_0
PULSE_VULKAN_GLOBAL_FUNCTION(vkCreateInstance)
PULSE_VULKAN_GLOBAL_FUNCTION(vkEnumerateInstanceExtensionProperties)
PULSE_VULKAN_GLOBAL_FUNCTION(vkEnumerateInstanceLayerProperties)
PULSE_VULKAN_GLOBAL_FUNCTION(vkGetInstanceProcAddr)
#endif

23
Sources/Backends/Vulkan/VulkanInstance.h git.filemode.normal_file
View File

@@ -0,0 +1,23 @@
// Copyright (C) 2024 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifdef PULSE_ENABLE_VULKAN_BACKEND
#ifndef PULSE_VULKAN_INSTANCE_H_
#define PULSE_VULKAN_INSTANCE_H_
#include <vulkan/vulkan_core.h>
typedef struct VulkanInstance
{
VkInstance instance;
#define PULSE_VULKAN_INSTANCE_FUNCTION(fn) PFN_##fn fn;
#include "VulkanInstancePrototypes.h"
#undef PULSE_VULKAN_INSTANCE_FUNCTION
} VulkanInstance;
#endif // PULSE_VULKAN_INSTANCE_H_
#endif // PULSE_ENABLE_VULKAN_BACKEND

View File

@@ -0,0 +1,23 @@
// Copyright (C) 2024 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
// No header guard
#ifndef PULSE_VULKAN_INSTANCE_FUNCTION
#error "You must define PULSE_VULKAN_INSTANCE_FUNCTION before including this file"
#endif
#ifdef VK_VERSION_1_0
PULSE_VULKAN_INSTANCE_FUNCTION(vkCreateDevice)
PULSE_VULKAN_INSTANCE_FUNCTION(vkDestroyInstance)
PULSE_VULKAN_INSTANCE_FUNCTION(vkEnumerateDeviceExtensionProperties)
PULSE_VULKAN_INSTANCE_FUNCTION(vkEnumeratePhysicalDevices)
PULSE_VULKAN_INSTANCE_FUNCTION(vkGetDeviceProcAddr)
PULSE_VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceFeatures)
PULSE_VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceFormatProperties)
PULSE_VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceImageFormatProperties)
PULSE_VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceMemoryProperties)
PULSE_VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceProperties)
PULSE_VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceQueueFamilyProperties)
#endif

169
Sources/Backends/Vulkan/VulkanLoader.c git.filemode.normal_file
View File

@@ -0,0 +1,169 @@
// Copyright (C) 2024 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <vulkan/vulkan_core.h>
#include <Pulse.h>
#include "../../PulseInternal.h"
#include "Vulkan.h"
#include "VulkanDevice.h"
#include "VulkanInstance.h"
#ifdef PULSE_PLAT_MACOS
#include <stdlib.h> // getenv
#endif
#ifdef PULSE_PLAT_WINDOWS
typedef const char* LPCSTR;
typedef struct HINSTANCE__* HINSTANCE;
typedef HINSTANCE HMODULE;
#if defined(_MINWINDEF_)
/* minwindef.h defines FARPROC, and attempting to redefine it may conflict with -Wstrict-prototypes */
#elif defined(_WIN64)
typedef __int64 (__stdcall* FARPROC)(void);
#else
typedef int (__stdcall* FARPROC)(void);
#endif
#endif
#ifdef PULSE_PLAT_WINDOWS
__declspec(dllimport) HMODULE __stdcall LoadLibraryA(LPCSTR);
__declspec(dllimport) FARPROC __stdcall GetProcAddress(HMODULE, LPCSTR);
__declspec(dllimport) int __stdcall FreeLibrary(HMODULE);
typedef HMODULE LibModule;
#else
#include <dlfcn.h>
typedef void* LibModule;
#endif
static LibModule vulkan_lib_module = PULSE_NULLPTR;
static void VulkanLoadGlobalFunctions(void* context, PFN_vkVoidFunction (*load)(void*, const char*));
static void VulkanLoadInstanceFunctions(VulkanInstance* instance, PFN_vkVoidFunction (*load)(void*, const char*));
static void VulkanLoadDeviceFunctions(VulkanInstance* instance, VulkanDevice* device, PFN_vkVoidFunction (*load)(VulkanInstance*, void*, const char*));
static inline PFN_vkVoidFunction vkGetInstanceProcAddrStub(void* context, const char* name)
{
PFN_vkVoidFunction fn = VulkanGetGlobal()->vkGetInstanceProcAddr((VkInstance)context, name);
if(!fn)
PulseSetInternalError(PULSE_ERROR_INITIALIZATION_FAILED);
return fn;
}
static inline PFN_vkVoidFunction vkGetDeviceProcAddrStub(VulkanInstance* instance, void* context, const char* name)
{
PFN_vkVoidFunction fn = instance->vkGetDeviceProcAddr((VkDevice)context, name);
if(!fn)
PulseSetInternalError(PULSE_ERROR_INITIALIZATION_FAILED);
return fn;
}
static inline LibModule LoadLibrary(const char* libname)
{
#ifdef PULSE_PLAT_WINDOWS
return LoadLibraryA(libname);
#else
return dlopen(libname, RTLD_NOW | RTLD_LOCAL);
#endif
}
static inline void* GetSymbol(LibModule module, const char* name)
{
#ifdef PULSE_PLAT_WINDOWS
return (void*)(void(*)(void))GetProcAddress(module, name);
#else
return dlsym(module, name);
#endif
}
static inline void UnloadLibrary(LibModule lib)
{
#ifdef PULSE_PLAT_WINDOWS
FreeLibrary(lib);
#else
dlclose(lib);
#endif
}
bool VulkanInitLoader()
{
#if defined(PULSE_PLAT_WINDOWS)
const char* libnames[] = {
"vulkan-1.dll"
};
#elif defined(PULSE_PLAT_MACOS)
const char* libnames[] = {
"libvulkan.dylib",
"libvulkan.1.dylib",
"libMoltenVK.dylib",
"vulkan.framework/vulkan",
"MoltenVK.framework/MoltenVK",
"/usr/local/lib/libvulkan.dylib",
};
#else
const char* libnames[] = {
"libvulkan.so.1",
"libvulkan.so"
};
#endif
for(size_t i = 0; i < sizeof(libnames) / sizeof(const char*); i++)
{
vulkan_lib_module = LoadLibrary(libnames[i]);
if(vulkan_lib_module)
{
VulkanGetGlobal()->vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)GetSymbol(vulkan_lib_module, "vkGetInstanceProcAddr");
if(VulkanGetGlobal()->vkGetInstanceProcAddr == PULSE_NULLPTR)
{
UnloadLibrary(vulkan_lib_module);
vulkan_lib_module = PULSE_NULLPTR;
}
}
}
if(!vulkan_lib_module)
{
PulseSetInternalError(PULSE_ERROR_INITIALIZATION_FAILED);
return false;
}
VulkanLoadGlobalFunctions(PULSE_NULLPTR, vkGetInstanceProcAddrStub);
return true;
}
void VulkanLoadInstance(VulkanInstance* instance)
{
VulkanLoadInstanceFunctions(instance, vkGetInstanceProcAddrStub);
}
void VulkanLoadDevice(VulkanInstance* instance, VulkanDevice* device)
{
VulkanLoadDeviceFunctions(instance, device, vkGetDeviceProcAddrStub);
}
void VulkanLoaderShutdown()
{
UnloadLibrary(vulkan_lib_module);
vulkan_lib_module = PULSE_NULLPTR;
}
static void VulkanLoadGlobalFunctions(void* context, PFN_vkVoidFunction (*load)(void*, const char*))
{
#define PULSE_VULKAN_GLOBAL_FUNCTION(func) VulkanGetGlobal()->func = (PFN_##func)load(context, #func);
#include "VulkanGlobalPrototypes.h"
#undef PULSE_VULKAN_GLOBAL_FUNCTION
}
static void VulkanLoadInstanceFunctions(VulkanInstance* instance, PFN_vkVoidFunction (*load)(void*, const char*))
{
#define PULSE_VULKAN_INSTANCE_FUNCTION(func) instance->func = (PFN_##func)load(instance->instance, #func);
#include "VulkanInstancePrototypes.h"
#undef PULSE_VULKAN_INSTANCE_FUNCTION
}
static void VulkanLoadDeviceFunctions(VulkanInstance* instance, VulkanDevice* device, PFN_vkVoidFunction (*load)(VulkanInstance*, void*, const char*))
{
#define PULSE_VULKAN_DEVICE_FUNCTION(func) device->func = (PFN_##func)load(instance, device->device, #func);
#include "VulkanDevicePrototypes.h"
#undef PULSE_VULKAN_DEVICE_FUNCTION
}

22
Sources/Backends/Vulkan/VulkanLoader.h git.filemode.normal_file
View File

@@ -0,0 +1,22 @@
// Copyright (C) 2024 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifdef PULSE_ENABLE_VULKAN_BACKEND
#ifndef PULSE_VULKAN_LOADER_H_
#define PULSE_VULKAN_LOADER_H_
#include <stdbool.h>
typedef struct VulkanInstance VulkanInstance;
typedef struct VulkanDevice VulkanDevice;
bool VulkanInitLoader();
bool VulkanLoadInstance(VulkanInstance* instance);
bool VulkanLoadDevice(VulkanInstance* instance, VulkanDevice* device);
void VulkanLoaderShutdown();
#endif // PULSE_VULKAN_LOADER_H_
#endif // PULSE_ENABLE_VULKAN_BACKEND

View File

@@ -2,5 +2,5 @@
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <Pulse.h>
#define VMA_IMPLEMENTATION
#include <vk_mem_alloc.h>