adding define option to disable all KHR functions

This commit is contained in:
2024-09-12 23:33:34 +02:00
parent 1a734cd185
commit 6333bc667c

118
kvf.h
View File

@@ -43,6 +43,8 @@
* or VK_NO_PROTOTYPES before including this file to avoid conflicts with Vulkan prototypes. * or VK_NO_PROTOTYPES before including this file to avoid conflicts with Vulkan prototypes.
* *
* You can also #define KVF_ENABLE_VALIDATION_LAYERS to enable validation layers. * You can also #define KVF_ENABLE_VALIDATION_LAYERS to enable validation layers.
*
* Use #define KVF_NO_KHR to remove all functions that use KHR calls.
*/ */
#ifndef KBZ_8_VULKAN_FRAMEWORK_H #ifndef KBZ_8_VULKAN_FRAMEWORK_H
@@ -99,11 +101,15 @@ VkPhysicalDevice kvfPickGoodPhysicalDevice(VkInstance instance, VkSurfaceKHR sur
VkQueue kvfGetDeviceQueue(VkDevice device, KvfQueueType queue); VkQueue kvfGetDeviceQueue(VkDevice device, KvfQueueType queue);
uint32_t kvfGetDeviceQueueFamily(VkDevice device, KvfQueueType queue); uint32_t kvfGetDeviceQueueFamily(VkDevice device, KvfQueueType queue);
bool kvfQueuePresentKHR(VkDevice device, VkSemaphore wait, VkSwapchainKHR swapchain, uint32_t image_index); // return false when the swapchain must be recreated #ifndef KVF_NO_KHR
bool kvfQueuePresentKHR(VkDevice device, VkSemaphore wait, VkSwapchainKHR swapchain, uint32_t image_index); // return false when the swapchain must be recreated
#endif
// Meant to be used when creating a VkDevice with a custom VkPhysicalDevice // Meant to be used when creating a VkDevice with a custom VkPhysicalDevice
int32_t kvfFindDeviceQueueFamily(VkPhysicalDevice physical, KvfQueueType type); // This function cannot find present queue int32_t kvfFindDeviceQueueFamily(VkPhysicalDevice physical, KvfQueueType type); // This function cannot find present queue
int32_t kvfFindDeviceQueueFamilyKHR(VkPhysicalDevice physical, VkSurfaceKHR surface, KvfQueueType type); // This one can find present queue #ifndef KVF_NO_KHR
int32_t kvfFindDeviceQueueFamilyKHR(VkPhysicalDevice physical, VkSurfaceKHR surface, KvfQueueType type); // This one can find present queue
#endif
VkDevice kvfCreateDefaultDevice(VkPhysicalDevice physical); VkDevice kvfCreateDefaultDevice(VkPhysicalDevice physical);
VkDevice kvfCreateDevice(VkPhysicalDevice physical, const char** extensions, uint32_t extensions_count, VkPhysicalDeviceFeatures* features); VkDevice kvfCreateDevice(VkPhysicalDevice physical, const char** extensions, uint32_t extensions_count, VkPhysicalDeviceFeatures* features);
@@ -118,12 +124,14 @@ void kvfDestroyFence(VkDevice device, VkFence fence);
VkSemaphore kvfCreateSemaphore(VkDevice device); VkSemaphore kvfCreateSemaphore(VkDevice device);
void kvfDestroySemaphore(VkDevice device, VkSemaphore semaphore); void kvfDestroySemaphore(VkDevice device, VkSemaphore semaphore);
VkSwapchainKHR kvfCreateSwapchainKHR(VkDevice device, VkPhysicalDevice physical, VkSurfaceKHR surface, VkExtent2D extent, bool try_vsync); #ifndef KVF_NO_KHR
VkFormat kvfGetSwapchainImagesFormat(VkSwapchainKHR swapchain); VkSwapchainKHR kvfCreateSwapchainKHR(VkDevice device, VkPhysicalDevice physical, VkSurfaceKHR surface, VkExtent2D extent, bool try_vsync);
uint32_t kvfGetSwapchainImagesCount(VkSwapchainKHR swapchain); VkFormat kvfGetSwapchainImagesFormat(VkSwapchainKHR swapchain);
uint32_t kvfGetSwapchainMinImagesCount(VkSwapchainKHR swapchain); uint32_t kvfGetSwapchainImagesCount(VkSwapchainKHR swapchain);
VkExtent2D kvfGetSwapchainImagesSize(VkSwapchainKHR swapchain); uint32_t kvfGetSwapchainMinImagesCount(VkSwapchainKHR swapchain);
void kvfDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain); VkExtent2D kvfGetSwapchainImagesSize(VkSwapchainKHR swapchain);
void kvfDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain);
#endif
VkImage kvfCreateImage(VkDevice device, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, KvfImageType type); VkImage kvfCreateImage(VkDevice device, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, KvfImageType type);
void kvfImageToBuffer(VkCommandBuffer cmd, VkBuffer dst, VkImage src, size_t buffer_offset, VkImageAspectFlagBits aspect, VkExtent3D extent); void kvfImageToBuffer(VkCommandBuffer cmd, VkBuffer dst, VkImage src, size_t buffer_offset, VkImageAspectFlagBits aspect, VkExtent3D extent);
@@ -151,7 +159,9 @@ void kvfSubmitCommandBuffer(VkDevice device, VkCommandBuffer buffer, KvfQueueTyp
void kvfSubmitSingleTimeCommandBuffer(VkDevice device, VkCommandBuffer buffer, KvfQueueType queue, VkFence fence); void kvfSubmitSingleTimeCommandBuffer(VkDevice device, VkCommandBuffer buffer, KvfQueueType queue, VkFence fence);
VkAttachmentDescription kvfBuildAttachmentDescription(KvfImageType type, VkFormat format, VkImageLayout initial, VkImageLayout final, bool clear, VkSampleCountFlagBits samples); VkAttachmentDescription kvfBuildAttachmentDescription(KvfImageType type, VkFormat format, VkImageLayout initial, VkImageLayout final, bool clear, VkSampleCountFlagBits samples);
VkAttachmentDescription kvfBuildSwapchainAttachmentDescription(VkSwapchainKHR swapchain, bool clear); #ifndef KVF_NO_KHR
VkAttachmentDescription kvfBuildSwapchainAttachmentDescription(VkSwapchainKHR swapchain, bool clear);
#endif
VkRenderPass kvfCreateRenderPass(VkDevice device, VkAttachmentDescription* attachments, size_t attachments_count, VkPipelineBindPoint bind_point); VkRenderPass kvfCreateRenderPass(VkDevice device, VkAttachmentDescription* attachments, size_t attachments_count, VkPipelineBindPoint bind_point);
VkRenderPass kvfCreateRenderPassWithSubpassDependencies(VkDevice device, VkAttachmentDescription* attachments, size_t attachments_count, VkPipelineBindPoint bind_point, VkSubpassDependency* dependencies, size_t dependencies_count); VkRenderPass kvfCreateRenderPassWithSubpassDependencies(VkDevice device, VkAttachmentDescription* attachments, size_t attachments_count, VkPipelineBindPoint bind_point, VkSubpassDependency* dependencies, size_t dependencies_count);
@@ -1128,6 +1138,7 @@ __KvfQueueFamilies __kvfFindQueueFamilies(VkPhysicalDevice physical, VkSurfaceKH
if(queue_families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) if(queue_families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
queues.graphics = i; queues.graphics = i;
VkBool32 present_support = false; VkBool32 present_support = false;
#ifndef KVF_NO_KHR
if(surface != VK_NULL_HANDLE) if(surface != VK_NULL_HANDLE)
{ {
vkGetPhysicalDeviceSurfaceSupportKHR(physical, i, surface, &present_support); vkGetPhysicalDeviceSurfaceSupportKHR(physical, i, surface, &present_support);
@@ -1136,6 +1147,7 @@ __KvfQueueFamilies __kvfFindQueueFamilies(VkPhysicalDevice physical, VkSurfaceKH
if(queues.graphics != -1 && queues.present != -1 && queues.compute != -1) if(queues.graphics != -1 && queues.present != -1 && queues.compute != -1)
break; break;
} }
#endif
else if(queues.graphics != -1 && queues.compute != -1) else if(queues.graphics != -1 && queues.compute != -1)
break; break;
} }
@@ -1202,6 +1214,7 @@ int32_t __kvfScorePhysicalDevice(VkPhysicalDevice device, VkSurfaceKHR surface,
if(queues.graphics == -1 || (surface != VK_NULL_HANDLE && queues.present == -1)) if(queues.graphics == -1 || (surface != VK_NULL_HANDLE && queues.present == -1))
return -1; return -1;
#ifndef KVF_NO_KHR
if(surface != VK_NULL_HANDLE) if(surface != VK_NULL_HANDLE)
{ {
/* Check surface formats counts */ /* Check surface formats counts */
@@ -1210,6 +1223,7 @@ int32_t __kvfScorePhysicalDevice(VkPhysicalDevice device, VkSurfaceKHR surface,
if(format_count == 0) if(format_count == 0)
return -1; return -1;
} }
#endif
VkPhysicalDeviceProperties device_props; VkPhysicalDeviceProperties device_props;
vkGetPhysicalDeviceProperties(device, &device_props); vkGetPhysicalDeviceProperties(device, &device_props);
@@ -1401,8 +1415,9 @@ uint32_t kvfGetDeviceQueueFamily(VkDevice device, KvfQueueType queue)
return 0; return 0;
} }
bool kvfQueuePresentKHR(VkDevice device, VkSemaphore wait, VkSwapchainKHR swapchain, uint32_t image_index) #ifndef KVF_NO_KHR
{ bool kvfQueuePresentKHR(VkDevice device, VkSemaphore wait, VkSwapchainKHR swapchain, uint32_t image_index)
{
KVF_ASSERT(device != VK_NULL_HANDLE); KVF_ASSERT(device != VK_NULL_HANDLE);
VkPresentInfoKHR present_info = {}; VkPresentInfoKHR present_info = {};
present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
@@ -1417,7 +1432,8 @@ bool kvfQueuePresentKHR(VkDevice device, VkSemaphore wait, VkSwapchainKHR swapch
else else
__kvfCheckVk(result); __kvfCheckVk(result);
return true; return true;
} }
#endif
int32_t kvfFindDeviceQueueFamily(VkPhysicalDevice physical, KvfQueueType type) int32_t kvfFindDeviceQueueFamily(VkPhysicalDevice physical, KvfQueueType type)
{ {
@@ -1453,8 +1469,9 @@ int32_t kvfFindDeviceQueueFamily(VkPhysicalDevice physical, KvfQueueType type)
return queue; return queue;
} }
int32_t kvfFindDeviceQueueFamilyKHR(VkPhysicalDevice physical, VkSurfaceKHR surface, KvfQueueType type) #ifndef KVF_NO_KHR
{ int32_t kvfFindDeviceQueueFamilyKHR(VkPhysicalDevice physical, VkSurfaceKHR surface, KvfQueueType type)
{
KVF_ASSERT(physical != VK_NULL_HANDLE); KVF_ASSERT(physical != VK_NULL_HANDLE);
KVF_ASSERT(surface != VK_NULL_HANDLE); KVF_ASSERT(surface != VK_NULL_HANDLE);
@@ -1479,7 +1496,8 @@ int32_t kvfFindDeviceQueueFamilyKHR(VkPhysicalDevice physical, VkSurfaceKHR surf
} }
KVF_FREE(queue_families); KVF_FREE(queue_families);
return queue; return queue;
} }
#endif
VkFence kvfCreateFence(VkDevice device) VkFence kvfCreateFence(VkDevice device)
{ {
@@ -1525,8 +1543,9 @@ void kvfDestroySemaphore(VkDevice device, VkSemaphore semaphore)
vkDestroySemaphore(device, semaphore, NULL); vkDestroySemaphore(device, semaphore, NULL);
} }
__KvfSwapchainSupportInternal __kvfQuerySwapchainSupport(VkPhysicalDevice physical, VkSurfaceKHR surface) #ifndef KVF_NO_KHR
{ __KvfSwapchainSupportInternal __kvfQuerySwapchainSupport(VkPhysicalDevice physical, VkSurfaceKHR surface)
{
__KvfSwapchainSupportInternal support; __KvfSwapchainSupportInternal support;
__kvfCheckVk(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical, surface, &support.capabilities)); __kvfCheckVk(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical, surface, &support.capabilities));
@@ -1545,20 +1564,20 @@ __KvfSwapchainSupportInternal __kvfQuerySwapchainSupport(VkPhysicalDevice physic
vkGetPhysicalDeviceSurfacePresentModesKHR(physical, surface, &support.presentModes_count, support.presentModes); vkGetPhysicalDeviceSurfacePresentModesKHR(physical, surface, &support.presentModes_count, support.presentModes);
} }
return support; return support;
} }
VkSurfaceFormatKHR __kvfChooseSwapSurfaceFormat(__KvfSwapchainSupportInternal* support) VkSurfaceFormatKHR __kvfChooseSwapSurfaceFormat(__KvfSwapchainSupportInternal* support)
{ {
for(int i = 0; i < support->formats_count; i++) for(int i = 0; i < support->formats_count; i++)
{ {
if(support->formats[i].format == VK_FORMAT_R8G8B8A8_SRGB && support->formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) if(support->formats[i].format == VK_FORMAT_R8G8B8A8_SRGB && support->formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
return support->formats[i]; return support->formats[i];
} }
return support->formats[0]; return support->formats[0];
} }
VkPresentModeKHR __kvfChooseSwapPresentMode(__KvfSwapchainSupportInternal* support, bool try_vsync) VkPresentModeKHR __kvfChooseSwapPresentMode(__KvfSwapchainSupportInternal* support, bool try_vsync)
{ {
if(try_vsync == false) if(try_vsync == false)
return VK_PRESENT_MODE_IMMEDIATE_KHR; return VK_PRESENT_MODE_IMMEDIATE_KHR;
for(int i = 0; i < support->presentModes_count; i++) for(int i = 0; i < support->presentModes_count; i++)
@@ -1567,16 +1586,16 @@ VkPresentModeKHR __kvfChooseSwapPresentMode(__KvfSwapchainSupportInternal* suppo
return support->presentModes[i]; return support->presentModes[i];
} }
return VK_PRESENT_MODE_FIFO_KHR; return VK_PRESENT_MODE_FIFO_KHR;
} }
uint32_t __kvfClamp(uint32_t i, uint32_t min, uint32_t max) uint32_t __kvfClamp(uint32_t i, uint32_t min, uint32_t max)
{ {
const uint32_t t = i < min ? min : i; const uint32_t t = i < min ? min : i;
return t > max ? max : t; return t > max ? max : t;
} }
VkSwapchainKHR kvfCreateSwapchainKHR(VkDevice device, VkPhysicalDevice physical, VkSurfaceKHR surface, VkExtent2D extent, bool try_vsync) VkSwapchainKHR kvfCreateSwapchainKHR(VkDevice device, VkPhysicalDevice physical, VkSurfaceKHR surface, VkExtent2D extent, bool try_vsync)
{ {
KVF_ASSERT(device != VK_NULL_HANDLE); KVF_ASSERT(device != VK_NULL_HANDLE);
VkSwapchainKHR swapchain; VkSwapchainKHR swapchain;
__KvfSwapchainSupportInternal support = __kvfQuerySwapchainSupport(physical, surface); __KvfSwapchainSupportInternal support = __kvfQuerySwapchainSupport(physical, surface);
@@ -1633,43 +1652,44 @@ VkSwapchainKHR kvfCreateSwapchainKHR(VkDevice device, VkPhysicalDevice physical,
__kvfAddSwapchainToArray(swapchain, support, surfaceFormat.format, images_count, extent); __kvfAddSwapchainToArray(swapchain, support, surfaceFormat.format, images_count, extent);
return swapchain; return swapchain;
} }
VkFormat kvfGetSwapchainImagesFormat(VkSwapchainKHR swapchain) VkFormat kvfGetSwapchainImagesFormat(VkSwapchainKHR swapchain)
{ {
__KvfSwapchain* kvf_swapchain = __kvfGetKvfSwapchainFromVkSwapchainKHR(swapchain); __KvfSwapchain* kvf_swapchain = __kvfGetKvfSwapchainFromVkSwapchainKHR(swapchain);
KVF_ASSERT(kvf_swapchain != NULL); KVF_ASSERT(kvf_swapchain != NULL);
return kvf_swapchain->images_format; return kvf_swapchain->images_format;
} }
uint32_t kvfGetSwapchainImagesCount(VkSwapchainKHR swapchain) uint32_t kvfGetSwapchainImagesCount(VkSwapchainKHR swapchain)
{ {
__KvfSwapchain* kvf_swapchain = __kvfGetKvfSwapchainFromVkSwapchainKHR(swapchain); __KvfSwapchain* kvf_swapchain = __kvfGetKvfSwapchainFromVkSwapchainKHR(swapchain);
KVF_ASSERT(kvf_swapchain != NULL); KVF_ASSERT(kvf_swapchain != NULL);
return kvf_swapchain->images_count; return kvf_swapchain->images_count;
} }
uint32_t kvfGetSwapchainMinImagesCount(VkSwapchainKHR swapchain) uint32_t kvfGetSwapchainMinImagesCount(VkSwapchainKHR swapchain)
{ {
__KvfSwapchain* kvf_swapchain = __kvfGetKvfSwapchainFromVkSwapchainKHR(swapchain); __KvfSwapchain* kvf_swapchain = __kvfGetKvfSwapchainFromVkSwapchainKHR(swapchain);
KVF_ASSERT(kvf_swapchain != NULL); KVF_ASSERT(kvf_swapchain != NULL);
return kvf_swapchain->support.capabilities.minImageCount; return kvf_swapchain->support.capabilities.minImageCount;
} }
VkExtent2D kvfGetSwapchainImagesSize(VkSwapchainKHR swapchain) VkExtent2D kvfGetSwapchainImagesSize(VkSwapchainKHR swapchain)
{ {
__KvfSwapchain* kvf_swapchain = __kvfGetKvfSwapchainFromVkSwapchainKHR(swapchain); __KvfSwapchain* kvf_swapchain = __kvfGetKvfSwapchainFromVkSwapchainKHR(swapchain);
KVF_ASSERT(kvf_swapchain != NULL); KVF_ASSERT(kvf_swapchain != NULL);
return kvf_swapchain->images_extent; return kvf_swapchain->images_extent;
} }
void kvfDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain) void kvfDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain)
{ {
if(swapchain == VK_NULL_HANDLE) if(swapchain == VK_NULL_HANDLE)
return; return;
KVF_ASSERT(device != VK_NULL_HANDLE); KVF_ASSERT(device != VK_NULL_HANDLE);
__kvfDestroySwapchain(device, swapchain); __kvfDestroySwapchain(device, swapchain);
} }
#endif
VkImage kvfCreateImage(VkDevice device, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, KvfImageType type) VkImage kvfCreateImage(VkDevice device, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, KvfImageType type)
{ {
@@ -2051,13 +2071,15 @@ VkAttachmentDescription kvfBuildAttachmentDescription(KvfImageType type, VkForma
return attachment; return attachment;
} }
VkAttachmentDescription kvfBuildSwapchainAttachmentDescription(VkSwapchainKHR swapchain, bool clear) #ifndef KVF_NO_KHR
{ VkAttachmentDescription kvfBuildSwapchainAttachmentDescription(VkSwapchainKHR swapchain, bool clear)
{
__KvfSwapchain* kvf_swapchain = __kvfGetKvfSwapchainFromVkSwapchainKHR(swapchain); __KvfSwapchain* kvf_swapchain = __kvfGetKvfSwapchainFromVkSwapchainKHR(swapchain);
KVF_ASSERT(kvf_swapchain != NULL); KVF_ASSERT(kvf_swapchain != NULL);
KVF_ASSERT(kvf_swapchain->images_count != 0); KVF_ASSERT(kvf_swapchain->images_count != 0);
return kvfBuildAttachmentDescription(KVF_IMAGE_COLOR, kvf_swapchain->images_format, VK_IMAGE_LAYOUT_UNDEFINED,VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, clear, VK_SAMPLE_COUNT_1_BIT); return kvfBuildAttachmentDescription(KVF_IMAGE_COLOR, kvf_swapchain->images_format, VK_IMAGE_LAYOUT_UNDEFINED,VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, clear, VK_SAMPLE_COUNT_1_BIT);
} }
#endif
VkRenderPass kvfCreateRenderPass(VkDevice device, VkAttachmentDescription* attachments, size_t attachments_count, VkPipelineBindPoint bind_point) VkRenderPass kvfCreateRenderPass(VkDevice device, VkAttachmentDescription* attachments, size_t attachments_count, VkPipelineBindPoint bind_point)
{ {