1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#ifndef AVUTIL_VULKAN_LOADER_H
20#define AVUTIL_VULKAN_LOADER_H
21
22#include "vulkan_functions.h"
23
24/* Macro to turn a function name into a loader struct */
25#define PFN_LOAD_INFO(req_inst, req_dev, ext_flag, name) \
26    {                                                    \
27        req_inst,                                        \
28        req_dev,                                         \
29        offsetof(FFVulkanFunctions, name),               \
30        ext_flag,                                        \
31        { "vk"#name, "vk"#name"EXT", "vk"#name"KHR" }    \
32    },
33
34static inline uint64_t ff_vk_extensions_to_mask(const char * const *extensions,
35                                                int nb_extensions)
36{
37    static const struct ExtensionMap {
38        const char *name;
39        FFVulkanExtensions flag;
40    } extension_map[] = {
41        { VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,   FF_VK_EXT_EXTERNAL_DMABUF_MEMORY },
42        { VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME, FF_VK_EXT_DRM_MODIFIER_FLAGS     },
43        { VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,        FF_VK_EXT_EXTERNAL_FD_MEMORY     },
44        { VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,     FF_VK_EXT_EXTERNAL_FD_SEM        },
45        { VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME,      FF_VK_EXT_EXTERNAL_HOST_MEMORY   },
46        { VK_EXT_DEBUG_UTILS_EXTENSION_NAME,               FF_VK_EXT_DEBUG_UTILS            },
47#ifdef _WIN32
48        { VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME,     FF_VK_EXT_EXTERNAL_WIN32_MEMORY  },
49        { VK_KHR_EXTERNAL_SEMAPHORE_WIN32_EXTENSION_NAME,  FF_VK_EXT_EXTERNAL_WIN32_SEM     },
50#endif
51    };
52
53    FFVulkanExtensions mask = 0x0;
54
55    for (int i = 0; i < nb_extensions; i++) {
56        for (int j = 0; j < FF_ARRAY_ELEMS(extension_map); j++) {
57            if (!strcmp(extensions[i], extension_map[j].name)) {
58                mask |= extension_map[j].flag;
59                continue;
60            }
61        }
62    }
63
64    return mask;
65}
66
67/**
68 * Function loader.
69 * Vulkan function from scratch loading happens in 3 stages - the first one
70 * is before any initialization has happened, and you have neither an instance
71 * structure nor a device structure. At this stage, you can only get the bare
72 * minimals to initialize an instance.
73 * The second stage is when you have an instance. At this stage, you can
74 * initialize a VkDevice, and have an idea of what extensions each device
75 * supports.
76 * Finally, in the third stage, you can proceed and load all core functions,
77 * plus you can be sure that any extensions you've enabled during device
78 * initialization will be available.
79 */
80static inline int ff_vk_load_functions(AVHWDeviceContext *ctx,
81                                       FFVulkanFunctions *vk,
82                                       uint64_t extensions_mask,
83                                       int has_inst, int has_dev)
84{
85    AVVulkanDeviceContext *hwctx = ctx->hwctx;
86
87    static const struct FunctionLoadInfo {
88        int req_inst;
89        int req_dev;
90        size_t struct_offset;
91        FFVulkanExtensions ext_flag;
92        const char *names[3];
93    } vk_load_info[] = {
94        FN_LIST(PFN_LOAD_INFO)
95#ifdef _WIN32
96        FN_LIST_WIN32(PFN_LOAD_INFO)
97#endif
98    };
99
100    for (int i = 0; i < FF_ARRAY_ELEMS(vk_load_info); i++) {
101        const struct FunctionLoadInfo *load = &vk_load_info[i];
102        PFN_vkVoidFunction fn;
103
104        if (load->req_dev  && !has_dev)
105            continue;
106        if (load->req_inst && !has_inst)
107            continue;
108
109        for (int j = 0; j < FF_ARRAY_ELEMS(load->names); j++) {
110            const char *name = load->names[j];
111
112            if (load->req_dev)
113                fn = vk->GetDeviceProcAddr(hwctx->act_dev, name);
114            else if (load->req_inst)
115                fn = hwctx->get_proc_addr(hwctx->inst, name);
116            else
117                fn = hwctx->get_proc_addr(NULL, name);
118
119            if (fn)
120                break;
121        }
122
123        if (!fn && ((extensions_mask &~ FF_VK_EXT_NO_FLAG) & load->ext_flag)) {
124            av_log(ctx, AV_LOG_ERROR, "Loader error, function \"%s\" indicated "
125                   "as supported, but got NULL function pointer!\n", load->names[0]);
126            return AVERROR_EXTERNAL;
127        }
128
129        *(PFN_vkVoidFunction *)((uint8_t *)vk + load->struct_offset) = fn;
130    }
131
132    return 0;
133}
134
135#endif /* AVUTIL_VULKAN_LOADER_H */
136