Avoid calling vulkan functions before the instance has been created

This commit is contained in:
Magnus Norddahl 2022-12-05 04:29:17 +01:00 committed by Christoph Oelckers
parent 0a80ad3b83
commit 4c85f461b1

View file

@ -61,7 +61,6 @@
#endif #endif
bool I_CreateVulkanSurface(VkInstance instance, VkSurfaceKHR *surface); bool I_CreateVulkanSurface(VkInstance instance, VkSurfaceKHR *surface);
bool I_GetVulkanPlatformExtensions(unsigned int *count, const char **names);
extern bool ToggleFullscreen; extern bool ToggleFullscreen;
@ -440,8 +439,9 @@ public:
VulkanInstanceBuilder builder; VulkanInstanceBuilder builder;
builder.DebugLayer(vk_debug); builder.DebugLayer(vk_debug);
for (unsigned int i = 0; i < count; i++) builder.RequireExtension(VK_KHR_SURFACE_EXTENSION_NAME); // KHR_surface, required
builder.RequireExtension(names[i]); builder.OptionalExtension(VK_EXT_METAL_SURFACE_EXTENSION_NAME); // EXT_metal_surface, optional, preferred
builder.OptionalExtension(VK_MVK_MACOS_SURFACE_EXTENSION_NAME); // MVK_macos_surface, optional, deprecated
auto vulkanInstance = builder.Create(); auto vulkanInstance = builder.Create();
VkSurfaceKHR surfacehandle = nullptr; VkSurfaceKHR surfacehandle = nullptr;
@ -924,63 +924,6 @@ void I_GetVulkanDrawableSize(int *width, int *height)
} }
} }
bool I_GetVulkanPlatformExtensions(unsigned int *count, const char **names)
{
static std::vector<const char*> extensions;
if (extensions.empty())
{
uint32_t extensionPropertyCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionPropertyCount, nullptr);
std::vector<VkExtensionProperties> extensionProperties(extensionPropertyCount);
vkEnumerateInstanceExtensionProperties(nullptr, &extensionPropertyCount, extensionProperties.data());
static const char* const EXTENSION_NAMES[] =
{
VK_KHR_SURFACE_EXTENSION_NAME, // KHR_surface, required
VK_EXT_METAL_SURFACE_EXTENSION_NAME, // EXT_metal_surface, optional, preferred
VK_MVK_MACOS_SURFACE_EXTENSION_NAME, // MVK_macos_surface, optional, deprecated
};
for (const VkExtensionProperties &currentProperties : extensionProperties)
{
for (const char *const extensionName : EXTENSION_NAMES)
{
if (strcmp(currentProperties.extensionName, extensionName) == 0)
{
extensions.push_back(extensionName);
}
}
}
}
static const unsigned int extensionCount = static_cast<unsigned int>(extensions.size());
assert(extensionCount >= 2); // KHR_surface + at least one of the platform surface extentions
if (count == nullptr && names == nullptr)
{
return false;
}
else if (names == nullptr)
{
*count = extensionCount;
return true;
}
else
{
const bool result = *count >= extensionCount;
*count = min(*count, extensionCount);
for (unsigned int i = 0; i < *count; ++i)
{
names[i] = extensions[i];
}
return result;
}
}
bool I_CreateVulkanSurface(VkInstance instance, VkSurfaceKHR *surface) bool I_CreateVulkanSurface(VkInstance instance, VkSurfaceKHR *surface)
{ {
NSView *const view = CocoaVideo::GetWindow().contentView; NSView *const view = CocoaVideo::GetWindow().contentView;