Update Vulkan backend

This commit is contained in:
Wojtek Figat
2024-02-05 18:45:01 +01:00
parent d4a483e656
commit 5b8846c8f0
9 changed files with 29 additions and 44 deletions

View File

@@ -18,7 +18,7 @@
#ifndef VULKAN_BACK_BUFFERS_COUNT #ifndef VULKAN_BACK_BUFFERS_COUNT
#define VULKAN_BACK_BUFFERS_COUNT 2 #define VULKAN_BACK_BUFFERS_COUNT 2
#endif #endif
#define VULKAN_BACK_BUFFERS_COUNT_MAX 16 #define VULKAN_BACK_BUFFERS_COUNT_MAX 4
/// <summary> /// <summary>
/// Default amount of frames to wait until resource delete. /// Default amount of frames to wait until resource delete.
@@ -34,8 +34,5 @@
#ifndef VULKAN_USE_QUERIES #ifndef VULKAN_USE_QUERIES
#define VULKAN_USE_QUERIES 1 #define VULKAN_USE_QUERIES 1
#endif #endif
#ifndef VULKAN_HAS_PHYSICAL_DEVICE_PROPERTIES2
#define VULKAN_HAS_PHYSICAL_DEVICE_PROPERTIES2 0
#endif
#endif #endif

View File

@@ -575,25 +575,15 @@ void GPUDeviceVulkan::GetDeviceExtensionsAndLayers(VkPhysicalDevice gpu, Array<c
void GPUDeviceVulkan::ParseOptionalDeviceExtensions(const Array<const char*>& deviceExtensions) void GPUDeviceVulkan::ParseOptionalDeviceExtensions(const Array<const char*>& deviceExtensions)
{ {
Platform::MemoryClear(&OptionalDeviceExtensions, sizeof(OptionalDeviceExtensions)); Platform::MemoryClear(&OptionalDeviceExtensions, sizeof(OptionalDeviceExtensions));
const auto HasExtension = [&deviceExtensions](const char* name) -> bool
{
const Function<bool(const char* const&)> CheckCallback = [&name](const char* const& extension) -> bool
{
return StringUtils::Compare(extension, name) == 0;
};
return ArrayExtensions::Any(deviceExtensions, CheckCallback);
};
#if VK_KHR_maintenance1 #if VK_KHR_maintenance1
OptionalDeviceExtensions.HasKHRMaintenance1 = HasExtension(VK_KHR_MAINTENANCE1_EXTENSION_NAME); OptionalDeviceExtensions.HasKHRMaintenance1 = RenderToolsVulkan::HasExtension(deviceExtensions, VK_KHR_MAINTENANCE1_EXTENSION_NAME);
#endif #endif
#if VK_KHR_maintenance2 #if VK_KHR_maintenance2
OptionalDeviceExtensions.HasKHRMaintenance2 = HasExtension(VK_KHR_MAINTENANCE2_EXTENSION_NAME); OptionalDeviceExtensions.HasKHRMaintenance2 = RenderToolsVulkan::HasExtension(deviceExtensions, VK_KHR_MAINTENANCE2_EXTENSION_NAME);
#endif #endif
OptionalDeviceExtensions.HasMirrorClampToEdge = HasExtension(VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME); OptionalDeviceExtensions.HasMirrorClampToEdge = RenderToolsVulkan::HasExtension(deviceExtensions, VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME);
#if VK_EXT_validation_cache #if VK_EXT_validation_cache
OptionalDeviceExtensions.HasEXTValidationCache = HasExtension(VK_EXT_VALIDATION_CACHE_EXTENSION_NAME); OptionalDeviceExtensions.HasEXTValidationCache = RenderToolsVulkan::HasExtension(deviceExtensions, VK_EXT_VALIDATION_CACHE_EXTENSION_NAME);
#endif #endif
} }

View File

@@ -998,11 +998,10 @@ void StagingManagerVulkan::ProcessPendingFree()
} }
// Free staging buffers that has not been used for a few frames // Free staging buffers that has not been used for a few frames
const uint64 SafeFramesCount = 30;
for (int32 i = _freeBuffers.Count() - 1; i >= 0; i--) for (int32 i = _freeBuffers.Count() - 1; i >= 0; i--)
{ {
auto& e = _freeBuffers[i]; auto& e = _freeBuffers.Get()[i];
if (e.FrameNumber + SafeFramesCount < Engine::FrameCount) if (e.FrameNumber + VULKAN_RESOURCE_DELETE_SAFE_FRAMES_COUNT < Engine::FrameCount)
{ {
auto buffer = e.Buffer; auto buffer = e.Buffer;
@@ -1027,7 +1026,7 @@ void StagingManagerVulkan::Dispose()
{ {
ScopeLock lock(_locker); ScopeLock lock(_locker);
#if !BUILD_RELEASE #if BUILD_DEBUG
LOG(Info, "Vulkan staging buffers peek memory usage: {0}, allocs: {1}, frees: {2}", Utilities::BytesToText(_allBuffersPeekSize), Utilities::BytesToText(_allBuffersAllocSize), Utilities::BytesToText(_allBuffersFreeSize)); LOG(Info, "Vulkan staging buffers peek memory usage: {0}, allocs: {1}, frees: {2}", Utilities::BytesToText(_allBuffersPeekSize), Utilities::BytesToText(_allBuffersAllocSize), Utilities::BytesToText(_allBuffersFreeSize));
#endif #endif
@@ -1078,7 +1077,7 @@ GPUDevice* GPUDeviceVulkan::Create()
#endif #endif
// Engine registration // Engine registration
const StringAsANSI<256> appName(*Globals::ProductName); const StringAsANSI<> appName(*Globals::ProductName);
VkApplicationInfo appInfo; VkApplicationInfo appInfo;
RenderToolsVulkan::ZeroStruct(appInfo, VK_STRUCTURE_TYPE_APPLICATION_INFO); RenderToolsVulkan::ZeroStruct(appInfo, VK_STRUCTURE_TYPE_APPLICATION_INFO);
appInfo.pApplicationName = appName.Get(); appInfo.pApplicationName = appName.Get();
@@ -1093,24 +1092,13 @@ GPUDevice* GPUDeviceVulkan::Create()
instInfo.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; instInfo.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
#endif #endif
instInfo.pApplicationInfo = &appInfo; instInfo.pApplicationInfo = &appInfo;
GetInstanceLayersAndExtensions(InstanceExtensions, InstanceLayers, SupportsDebugUtilsExt); GetInstanceLayersAndExtensions(InstanceExtensions, InstanceLayers, SupportsDebugUtilsExt);
const auto hasExtension = [](const Array<const char*>& extensions, const char* name) -> bool
{
const Function<bool(const char* const&)> callback = [&name](const char* const& extension) -> bool
{
return extension && StringUtils::Compare(extension, name) == 0;
};
return ArrayExtensions::Any(extensions, callback);
};
instInfo.enabledExtensionCount = InstanceExtensions.Count(); instInfo.enabledExtensionCount = InstanceExtensions.Count();
instInfo.ppEnabledExtensionNames = instInfo.enabledExtensionCount > 0 ? static_cast<const char* const*>(InstanceExtensions.Get()) : nullptr; instInfo.ppEnabledExtensionNames = instInfo.enabledExtensionCount > 0 ? static_cast<const char* const*>(InstanceExtensions.Get()) : nullptr;
instInfo.enabledLayerCount = InstanceLayers.Count(); instInfo.enabledLayerCount = InstanceLayers.Count();
instInfo.ppEnabledLayerNames = instInfo.enabledLayerCount > 0 ? InstanceLayers.Get() : nullptr; instInfo.ppEnabledLayerNames = instInfo.enabledLayerCount > 0 ? InstanceLayers.Get() : nullptr;
#if VULKAN_USE_DEBUG_LAYER #if VULKAN_USE_DEBUG_LAYER
SupportsDebugCallbackExt = !SupportsDebugUtilsExt && hasExtension(InstanceExtensions, VK_EXT_DEBUG_REPORT_EXTENSION_NAME); SupportsDebugCallbackExt = !SupportsDebugUtilsExt && RenderToolsVulkan::HasExtension(InstanceExtensions, VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
#endif #endif
// Create Vulkan instance // Create Vulkan instance
@@ -1658,9 +1646,7 @@ bool GPUDeviceVulkan::Init()
queue.pQueuePriorities = currentPriority; queue.pQueuePriorities = currentPriority;
const VkQueueFamilyProperties& properties = QueueFamilyProps[queue.queueFamilyIndex]; const VkQueueFamilyProperties& properties = QueueFamilyProps[queue.queueFamilyIndex];
for (int32 queueIndex = 0; queueIndex < (int32)properties.queueCount; queueIndex++) for (int32 queueIndex = 0; queueIndex < (int32)properties.queueCount; queueIndex++)
{
*currentPriority++ = 1.0f; *currentPriority++ = 1.0f;
}
} }
deviceInfo.queueCreateInfoCount = queueFamilyInfos.Count(); deviceInfo.queueCreateInfoCount = queueFamilyInfos.Count();
deviceInfo.pQueueCreateInfos = queueFamilyInfos.Get(); deviceInfo.pQueueCreateInfos = queueFamilyInfos.Get();
@@ -1847,12 +1833,17 @@ bool GPUDeviceVulkan::Init()
INIT_FUNC(vkDestroyImage); INIT_FUNC(vkDestroyImage);
INIT_FUNC(vkCmdCopyBuffer); INIT_FUNC(vkCmdCopyBuffer);
#if VMA_DEDICATED_ALLOCATION #if VMA_DEDICATED_ALLOCATION
#if PLATFORM_SWITCH
vulkanFunctions.vkGetBufferMemoryRequirements2KHR = vkGetBufferMemoryRequirements2;
vulkanFunctions.vkGetImageMemoryRequirements2KHR = vkGetImageMemoryRequirements2;
#else
INIT_FUNC(vkGetBufferMemoryRequirements2KHR); INIT_FUNC(vkGetBufferMemoryRequirements2KHR);
INIT_FUNC(vkGetImageMemoryRequirements2KHR); INIT_FUNC(vkGetImageMemoryRequirements2KHR);
#endif #endif
#endif
#undef INIT_FUNC #undef INIT_FUNC
VmaAllocatorCreateInfo allocatorInfo = {}; VmaAllocatorCreateInfo allocatorInfo = {};
allocatorInfo.vulkanApiVersion = VK_API_VERSION_1_0; allocatorInfo.vulkanApiVersion = VULKAN_API_VERSION;
allocatorInfo.physicalDevice = gpu; allocatorInfo.physicalDevice = gpu;
allocatorInfo.instance = Instance; allocatorInfo.instance = Instance;
allocatorInfo.device = Device; allocatorInfo.device = Device;

View File

@@ -400,8 +400,6 @@ public:
uint32 HasKHRMaintenance1 : 1; uint32 HasKHRMaintenance1 : 1;
uint32 HasKHRMaintenance2 : 1; uint32 HasKHRMaintenance2 : 1;
uint32 HasMirrorClampToEdge : 1; uint32 HasMirrorClampToEdge : 1;
uint32 HasKHRExternalMemoryCapabilities : 1;
uint32 HasKHRGetPhysicalDeviceProperties2 : 1;
uint32 HasEXTValidationCache : 1; uint32 HasEXTValidationCache : 1;
}; };

View File

@@ -14,7 +14,6 @@
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#undef VK_EXT_debug_utils #undef VK_EXT_debug_utils
#undef VK_EXT_validation_cache #undef VK_EXT_validation_cache
#define VMA_DEDICATED_ALLOCATION 0
#pragma clang diagnostic ignored "-Wpointer-bool-conversion" #pragma clang diagnostic ignored "-Wpointer-bool-conversion"
#pragma clang diagnostic ignored "-Wtautological-pointer-compare" #pragma clang diagnostic ignored "-Wtautological-pointer-compare"

View File

@@ -6,8 +6,6 @@
#if GRAPHICS_API_VULKAN && PLATFORM_LINUX #if GRAPHICS_API_VULKAN && PLATFORM_LINUX
#define VULKAN_HAS_PHYSICAL_DEVICE_PROPERTIES2 1
/// <summary> /// <summary>
/// The implementation for the Vulkan API support for Linux platform. /// The implementation for the Vulkan API support for Linux platform.
/// </summary> /// </summary>

View File

@@ -277,4 +277,15 @@ void RenderToolsVulkan::LogVkResult(VkResult result)
LogVkResult(result, "", 0); LogVkResult(result, "", 0);
} }
bool RenderToolsVulkan::HasExtension(const Array<const char*>& extensions, const char* name)
{
for (int32 i = 0; i < extensions.Count(); i++)
{
const char* extension = extensions[i];
if (extension && StringUtils::Compare(extension, name) == 0)
return true;
}
return false;
}
#endif #endif

View File

@@ -307,6 +307,8 @@ public:
} }
return result; return result;
} }
static bool HasExtension(const Array<const char*, HeapAllocation>& extensions, const char* name);
}; };
#endif #endif

View File

@@ -8,7 +8,6 @@
#define VULKAN_USE_PLATFORM_WIN32_KHR 1 #define VULKAN_USE_PLATFORM_WIN32_KHR 1
#define VULKAN_USE_PLATFORM_WIN32_KHX 1 #define VULKAN_USE_PLATFORM_WIN32_KHX 1
#define VULKAN_HAS_PHYSICAL_DEVICE_PROPERTIES2 1
#define VULKAN_USE_CREATE_WIN32_SURFACE 1 #define VULKAN_USE_CREATE_WIN32_SURFACE 1
/// <summary> /// <summary>