Skip to content

Commit

Permalink
Allow vulkan device/functions to be externally provided
Browse files Browse the repository at this point in the history
  • Loading branch information
sergcpp committed Nov 8, 2024
1 parent 5bafd81 commit 4f47925
Show file tree
Hide file tree
Showing 8 changed files with 620 additions and 244 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ set(SOURCE_FILES Bitmask.h
RendererBase.cpp
SceneBase.h
Span.h
Types.h)
Types.h
VulkanFunctions.h)

set(SIMD_FILES internal/simd/aligned_allocator.h
internal/simd/detect.h
Expand Down
4 changes: 4 additions & 0 deletions RendererBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Config.h"
#include "SceneBase.h"
#include "Types.h"
#include "VulkanFunctions.h"

/**
@file RendererBase.h
Expand Down Expand Up @@ -51,6 +52,9 @@ struct settings_t {
bool use_bindless = true;
bool use_spatial_cache = false;
int validation_level = 0;

VulkanDevice vk_device = {};
VulkanFunctions vk_functions = {};
};

/** Render region context,
Expand Down
393 changes: 393 additions & 0 deletions VulkanFunctions.h

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/RendererVK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ std::vector<uint8_t> deflate_data(const uint8_t *data, const int len) {

Ray::Vk::Renderer::Renderer(const settings_t &s, ILog *log) {
ctx_ = std::make_unique<Context>();
const bool res = ctx_->Init(log, s.preferred_device, s.validation_level);
const bool res = ctx_->Init(log, s.vk_device, s.vk_functions, s.preferred_device, s.validation_level);
if (!res) {
throw std::runtime_error("Error initializing vulkan context!");
}
Expand Down
14 changes: 5 additions & 9 deletions internal/Vk/Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool Ray::Vk::Api::Load(ILog *log) {
}

#define LOAD_VK_FUN(x) \
x = (PFN_##x)GetProcAddress(vulkan_module, #x); \
x = (decltype(x))GetProcAddress(vulkan_module, #x); \
if (!(x)) { \
log->Error("Failed to load %s", #x); \
return false; \
Expand All @@ -36,7 +36,7 @@ bool Ray::Vk::Api::Load(ILog *log) {
}

#define LOAD_VK_FUN(x) \
x = (PFN_##x)dlsym(vulkan_module, #x); \
x = (decltype(x))dlsym(vulkan_module, #x); \
if (!(x)) { \
log->Error("Failed to load %s", #x); \
return false; \
Expand All @@ -58,7 +58,7 @@ bool Ray::Vk::Api::Load(ILog *log) {
#endif

#define LOAD_VK_FUN(x) \
x = (PFN_##x)dlsym(vulkan_module, #x); \
x = (decltype(x))dlsym(vulkan_module, #x); \
if (!(x)) { \
log->Error("Failed to load %s", #x); \
return false; \
Expand Down Expand Up @@ -122,9 +122,6 @@ bool Ray::Vk::Api::Load(ILog *log) {
LOAD_VK_FUN(vkCreateRenderPass)
LOAD_VK_FUN(vkDestroyRenderPass)

LOAD_VK_FUN(vkCreateFramebuffer)
LOAD_VK_FUN(vkDestroyFramebuffer)

LOAD_VK_FUN(vkCreateBuffer)
LOAD_VK_FUN(vkGetBufferMemoryRequirements)
LOAD_VK_FUN(vkBindBufferMemory)
Expand Down Expand Up @@ -189,13 +186,12 @@ bool Ray::Vk::Api::Load(ILog *log) {

bool Ray::Vk::Api::LoadExtensions(VkInstance instance, ILog *log) {
#define LOAD_INSTANCE_FUN(x) \
x = (PFN_##x)vkGetInstanceProcAddr(instance, #x); \
x = (PFN_##x)vkGetInstanceProcAddr(instance, #x); \
if (!(x)) { \
log->Error("Failed to load %s", #x); \
return false; \
}
#define LOAD_OPTIONAL_INSTANCE_FUN(x) \
x = (PFN_##x)vkGetInstanceProcAddr(instance, #x); \
#define LOAD_OPTIONAL_INSTANCE_FUN(x) x = (PFN_##x)vkGetInstanceProcAddr(instance, #x);

LOAD_INSTANCE_FUN(vkCreateDebugReportCallbackEXT)
LOAD_INSTANCE_FUN(vkDestroyDebugReportCallbackEXT)
Expand Down
110 changes: 3 additions & 107 deletions internal/Vk/Api.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
#undef None
#undef Success

#include "../../VulkanFunctions.h"

namespace Ray {
class ILog;
namespace Vk {
struct Api {
struct Api : public VulkanFunctions {
#if defined(_WIN32)
HMODULE vulkan_module = {};
#else
Expand All @@ -30,121 +32,15 @@ struct Api {
PFN_vkDestroyInstance vkDestroyInstance;
PFN_vkEnumerateInstanceLayerProperties vkEnumerateInstanceLayerProperties;
PFN_vkEnumerateInstanceExtensionProperties vkEnumerateInstanceExtensionProperties;
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr;
PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr;

PFN_vkEnumeratePhysicalDevices vkEnumeratePhysicalDevices;
PFN_vkGetPhysicalDeviceProperties vkGetPhysicalDeviceProperties;
PFN_vkGetPhysicalDeviceFeatures vkGetPhysicalDeviceFeatures;
PFN_vkGetPhysicalDeviceQueueFamilyProperties vkGetPhysicalDeviceQueueFamilyProperties;

PFN_vkCreateDevice vkCreateDevice;
PFN_vkDestroyDevice vkDestroyDevice;

PFN_vkEnumerateDeviceExtensionProperties vkEnumerateDeviceExtensionProperties;

PFN_vkGetDeviceQueue vkGetDeviceQueue;
PFN_vkCreateCommandPool vkCreateCommandPool;
PFN_vkDestroyCommandPool vkDestroyCommandPool;

PFN_vkAllocateCommandBuffers vkAllocateCommandBuffers;
PFN_vkFreeCommandBuffers vkFreeCommandBuffers;

PFN_vkCreateFence vkCreateFence;
PFN_vkWaitForFences vkWaitForFences;
PFN_vkResetFences vkResetFences;
PFN_vkDestroyFence vkDestroyFence;
PFN_vkGetFenceStatus vkGetFenceStatus;

PFN_vkBeginCommandBuffer vkBeginCommandBuffer;
PFN_vkEndCommandBuffer vkEndCommandBuffer;
PFN_vkResetCommandBuffer vkResetCommandBuffer;
PFN_vkCmdPipelineBarrier vkCmdPipelineBarrier;

PFN_vkQueueSubmit vkQueueSubmit;
PFN_vkQueueWaitIdle vkQueueWaitIdle;

PFN_vkCreateImageView vkCreateImageView;
PFN_vkDestroyImageView vkDestroyImageView;

PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties;
PFN_vkGetPhysicalDeviceFormatProperties vkGetPhysicalDeviceFormatProperties;
PFN_vkGetPhysicalDeviceImageFormatProperties vkGetPhysicalDeviceImageFormatProperties;

PFN_vkCreateImage vkCreateImage;
PFN_vkDestroyImage vkDestroyImage;

PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements;
PFN_vkAllocateMemory vkAllocateMemory;
PFN_vkFreeMemory vkFreeMemory;
PFN_vkBindImageMemory vkBindImageMemory;

PFN_vkCreateRenderPass vkCreateRenderPass;
PFN_vkDestroyRenderPass vkDestroyRenderPass;

PFN_vkCreateFramebuffer vkCreateFramebuffer;
PFN_vkDestroyFramebuffer vkDestroyFramebuffer;

PFN_vkCreateBuffer vkCreateBuffer;
PFN_vkBindBufferMemory vkBindBufferMemory;
PFN_vkDestroyBuffer vkDestroyBuffer;
PFN_vkGetBufferMemoryRequirements vkGetBufferMemoryRequirements;

PFN_vkCreateBufferView vkCreateBufferView;
PFN_vkDestroyBufferView vkDestroyBufferView;

PFN_vkMapMemory vkMapMemory;
PFN_vkUnmapMemory vkUnmapMemory;

PFN_vkCreateShaderModule vkCreateShaderModule;
PFN_vkDestroyShaderModule vkDestroyShaderModule;

PFN_vkCreateDescriptorSetLayout vkCreateDescriptorSetLayout;
PFN_vkDestroyDescriptorSetLayout vkDestroyDescriptorSetLayout;

PFN_vkCreatePipelineLayout vkCreatePipelineLayout;
PFN_vkDestroyPipelineLayout vkDestroyPipelineLayout;

PFN_vkCreateGraphicsPipelines vkCreateGraphicsPipelines;
PFN_vkCreateComputePipelines vkCreateComputePipelines;
PFN_vkDestroyPipeline vkDestroyPipeline;

PFN_vkCreateSemaphore vkCreateSemaphore;
PFN_vkDestroySemaphore vkDestroySemaphore;

PFN_vkCreateSampler vkCreateSampler;
PFN_vkDestroySampler vkDestroySampler;

PFN_vkCreateDescriptorPool vkCreateDescriptorPool;
PFN_vkDestroyDescriptorPool vkDestroyDescriptorPool;
PFN_vkResetDescriptorPool vkResetDescriptorPool;

PFN_vkAllocateDescriptorSets vkAllocateDescriptorSets;
PFN_vkFreeDescriptorSets vkFreeDescriptorSets;
PFN_vkUpdateDescriptorSets vkUpdateDescriptorSets;

PFN_vkCreateQueryPool vkCreateQueryPool;
PFN_vkDestroyQueryPool vkDestroyQueryPool;
PFN_vkGetQueryPoolResults vkGetQueryPoolResults;

PFN_vkCmdBindPipeline vkCmdBindPipeline;
PFN_vkCmdBindDescriptorSets vkCmdBindDescriptorSets;
PFN_vkCmdBindVertexBuffers vkCmdBindVertexBuffers;
PFN_vkCmdBindIndexBuffer vkCmdBindIndexBuffer;
PFN_vkCmdCopyBufferToImage vkCmdCopyBufferToImage;
PFN_vkCmdCopyImageToBuffer vkCmdCopyImageToBuffer;
PFN_vkCmdCopyBuffer vkCmdCopyBuffer;
PFN_vkCmdFillBuffer vkCmdFillBuffer;
PFN_vkCmdUpdateBuffer vkCmdUpdateBuffer;
PFN_vkCmdPushConstants vkCmdPushConstants;
PFN_vkCmdBlitImage vkCmdBlitImage;
PFN_vkCmdClearColorImage vkCmdClearColorImage;
PFN_vkCmdCopyImage vkCmdCopyImage;
PFN_vkCmdDispatch vkCmdDispatch;
PFN_vkCmdDispatchIndirect vkCmdDispatchIndirect;
PFN_vkCmdResetQueryPool vkCmdResetQueryPool;
PFN_vkCmdWriteTimestamp vkCmdWriteTimestamp;

//

PFN_vkCreateDebugReportCallbackEXT vkCreateDebugReportCallbackEXT;
Expand Down
Loading

0 comments on commit 4f47925

Please sign in to comment.