1
0
mirror of https://github.com/mpv-player/mpv synced 2025-04-09 11:11:52 +00:00

vulkan: use pl_vk_inst_create

This change is mostly motivated by missing
VK_KHR_portability_enumeration instance extension when enumerating the
devices. Which causes issues with MoltenVK which does not advertise full
Vulkan conformance.

To avoid duplicating code use pl_vk_inst_create() which correctly query
availability and enables the mentioned extension.

While at it fix the VkInstance leaking in vk_validate_dev().
This commit is contained in:
Kacper Michajłow 2024-04-07 16:18:51 +02:00
parent 0f4f1bcd63
commit 2f76536f62
2 changed files with 39 additions and 39 deletions

View File

@ -25,6 +25,7 @@
#include "options/m_config.h" #include "options/m_config.h"
#include "video/out/placebo/ra_pl.h" #include "video/out/placebo/ra_pl.h"
#include "video/out/placebo/utils.h"
#include "context.h" #include "context.h"
#include "utils.h" #include "utils.h"
@ -39,36 +40,30 @@ struct vulkan_opts {
static inline OPT_STRING_VALIDATE_FUNC(vk_validate_dev) static inline OPT_STRING_VALIDATE_FUNC(vk_validate_dev)
{ {
struct bstr param = bstr0(*value);
int ret = M_OPT_INVALID; int ret = M_OPT_INVALID;
VkResult res; void *ta_ctx = talloc_new(NULL);
pl_log pllog = mppl_log_create(ta_ctx, log);
if (!pllog)
goto done;
// Create a dummy instance to validate/list the devices // Create a dummy instance to validate/list the devices
VkInstanceCreateInfo info = { mppl_log_set_probing(pllog, true);
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, pl_vk_inst inst = pl_vk_inst_create(pllog, pl_vk_inst_params());
.pApplicationInfo = &(VkApplicationInfo) { mppl_log_set_probing(pllog, false);
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, if (!inst)
.apiVersion = VK_API_VERSION_1_1, goto done;
}
};
VkInstance inst;
VkPhysicalDevice *devices = NULL;
uint32_t num = 0; uint32_t num = 0;
VkResult res = vkEnumeratePhysicalDevices(inst->instance, &num, NULL);
res = vkCreateInstance(&info, NULL, &inst);
if (res != VK_SUCCESS) if (res != VK_SUCCESS)
goto done; goto done;
res = vkEnumeratePhysicalDevices(inst, &num, NULL); VkPhysicalDevice *devices = talloc_array(ta_ctx, VkPhysicalDevice, num);
if (res != VK_SUCCESS) res = vkEnumeratePhysicalDevices(inst->instance, &num, devices);
goto done;
devices = talloc_array(NULL, VkPhysicalDevice, num);
res = vkEnumeratePhysicalDevices(inst, &num, devices);
if (res != VK_SUCCESS) if (res != VK_SUCCESS)
goto done; goto done;
struct bstr param = bstr0(*value);
bool help = bstr_equals0(param, "help"); bool help = bstr_equals0(param, "help");
if (help) { if (help) {
mp_info(log, "Available vulkan devices:\n"); mp_info(log, "Available vulkan devices:\n");
@ -110,7 +105,9 @@ static inline OPT_STRING_VALIDATE_FUNC(vk_validate_dev)
BSTR_P(param)); BSTR_P(param));
done: done:
talloc_free(devices); pl_vk_inst_destroy(&inst);
pl_log_destroy(&pllog);
talloc_free(ta_ctx);
return ret; return ret;
} }

View File

@ -19,6 +19,7 @@
#include "context.h" #include "context.h"
#include "options/m_config.h" #include "options/m_config.h"
#include "utils.h" #include "utils.h"
#include "video/out/placebo/utils.h"
#if HAVE_DRM #if HAVE_DRM
#include <errno.h> #include <errno.h>
@ -215,35 +216,36 @@ done:
} }
static int print_display_info(struct mp_log *log, const struct m_option *opt, static int print_display_info(struct mp_log *log, const struct m_option *opt,
struct bstr name) { struct bstr name)
VkResult res; {
VkPhysicalDevice *devices = NULL; void *ta_ctx = talloc_new(NULL);
pl_log pllog = mppl_log_create(ta_ctx, log);
if (!pllog)
goto done;
// Create a dummy instance to list the resources // Create a dummy instance to list the resources
VkInstanceCreateInfo info = { mppl_log_set_probing(pllog, true);
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, pl_vk_inst inst = pl_vk_inst_create(pllog, pl_vk_inst_params(
.enabledExtensionCount = 1, .extensions = (const char *[]){
.ppEnabledExtensionNames = (const char*[]) { VK_KHR_DISPLAY_EXTENSION_NAME,
VK_KHR_DISPLAY_EXTENSION_NAME
}, },
}; .num_extensions = 1,
));
VkInstance inst = NULL; mppl_log_set_probing(pllog, false);
res = vkCreateInstance(&info, NULL, &inst); if (!inst) {
if (res != VK_SUCCESS) {
mp_warn(log, "Unable to create Vulkan instance.\n"); mp_warn(log, "Unable to create Vulkan instance.\n");
goto done; goto done;
} }
uint32_t num_devices = 0; uint32_t num_devices = 0;
vkEnumeratePhysicalDevices(inst, &num_devices, NULL); VkResult res = vkEnumeratePhysicalDevices(inst->instance, &num_devices, NULL);
if (!num_devices) { if (res != VK_SUCCESS || !num_devices) {
mp_info(log, "No Vulkan devices detected.\n"); mp_info(log, "No Vulkan devices detected.\n");
goto done; goto done;
} }
devices = talloc_array(NULL, VkPhysicalDevice, num_devices); VkPhysicalDevice *devices = talloc_array(ta_ctx, VkPhysicalDevice, num_devices);
vkEnumeratePhysicalDevices(inst, &num_devices, devices); res = vkEnumeratePhysicalDevices(inst->instance, &num_devices, devices);
if (res != VK_SUCCESS) { if (res != VK_SUCCESS) {
mp_warn(log, "Failed enumerating physical devices.\n"); mp_warn(log, "Failed enumerating physical devices.\n");
goto done; goto done;
@ -255,8 +257,9 @@ static int print_display_info(struct mp_log *log, const struct m_option *opt,
} }
done: done:
talloc_free(devices); pl_vk_inst_destroy(&inst);
vkDestroyInstance(inst, NULL); pl_log_destroy(&pllog);
talloc_free(ta_ctx);
return M_OPT_EXIT; return M_OPT_EXIT;
} }