Skip to content

Commit

Permalink
vo_gpu: vulkan: add support for win32 external memory extension
Browse files Browse the repository at this point in the history
  • Loading branch information
BtbN authored and philipl committed Oct 17, 2018
1 parent 3e7d18b commit ad4e136
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.
5 changes: 5 additions & 0 deletions video/out/vulkan/common.h
Expand Up @@ -25,6 +25,10 @@

#include <vulkan/vulkan.h>

#if HAVE_WIN32_DESKTOP
#include <vulkan/vulkan_win32.h>
#endif

// Vulkan allows the optional use of a custom allocator. We don't need one but
// mark this parameter with a better name in case we ever decide to change this
// in the future. (And to make the code more readable)
Expand Down Expand Up @@ -77,4 +81,5 @@ struct mpvk_ctx {
// Extension availability
bool has_ext_external_memory;
bool has_ext_external_memory_fd;
bool has_ext_external_memory_win32;
};
19 changes: 16 additions & 3 deletions video/out/vulkan/malloc.c
Expand Up @@ -2,6 +2,10 @@
#include "utils.h"
#include "osdep/timer.h"

#if HAVE_WIN32_DESKTOP
#include <versionhelpers.h>
#endif

// Controls the multiplication factor for new slab allocations. The new slab
// will always be allocated such that the size of the slab is this factor times
// the previous slab. Higher values make it grow faster.
Expand Down Expand Up @@ -129,7 +133,13 @@ static struct vk_slab *slab_alloc(struct mpvk_ctx *vk, struct vk_heap *heap,

VkExportMemoryAllocateInfoKHR eminfo = {
.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR,
#if HAVE_WIN32_DESKTOP
.handleTypes = IsWindows8OrGreater()
? VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT_KHR
: VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT,
#else
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
#endif
};

VkMemoryAllocateInfo minfo = {
Expand All @@ -150,7 +160,7 @@ static struct vk_slab *slab_alloc(struct mpvk_ctx *vk, struct vk_heap *heap,

VkExternalMemoryBufferCreateInfo ebinfo = {
.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
.handleTypes = eminfo.handleTypes,
};

VkBufferCreateInfo binfo = {
Expand Down Expand Up @@ -442,8 +452,11 @@ bool vk_malloc_buffer(struct mpvk_ctx *vk, VkBufferUsageFlags bufFlags,
{
if (exportable) {
#if HAVE_WIN32_DESKTOP
MP_ERR(vk, "Exportable memory is not currently supported on Windows\n");
return false;
if (!vk->has_ext_external_memory_win32) {
MP_ERR(vk, "Exportable memory requires the %s extension\n",
VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME);
return false;
}
#else
if (!vk->has_ext_external_memory_fd) {
MP_ERR(vk, "Exportable memory requires the %s extension\n",
Expand Down
28 changes: 25 additions & 3 deletions video/out/vulkan/ra_vk.c
Expand Up @@ -4,6 +4,10 @@
#include "ra_vk.h"
#include "malloc.h"

#if HAVE_WIN32_DESKTOP
#include <versionhelpers.h>
#endif

static struct ra_fns ra_fns_vk;

enum queue_type {
Expand Down Expand Up @@ -933,10 +937,27 @@ static bool vk_tex_upload(struct ra *ra,
return false;
}

static bool ra_vk_mem_get_external_fd(struct ra *ra, struct vk_memslice *mem, struct vk_external_fd *ret)
static bool ra_vk_mem_get_external_info(struct ra *ra, struct vk_memslice *mem, struct vk_external_mem *ret)
{
struct mpvk_ctx *vk = ra_vk_get(ra);

#if HAVE_WIN32_DESKTOP
HANDLE mem_handle;

VkMemoryGetWin32HandleInfoKHR info = {
.sType = VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR,
.pNext = NULL,
.memory = mem->vkmem,
.handleType = IsWindows8OrGreater()
? VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT_KHR
: VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT,
};

VK_LOAD_PFN(vkGetMemoryWin32HandleKHR);
VK(pfn_vkGetMemoryWin32HandleKHR(vk->dev, &info, &mem_handle));

ret->mem_handle = mem_handle;
#else
int mem_fd;

VkMemoryGetFdInfoKHR info = {
Expand All @@ -950,6 +971,7 @@ static bool ra_vk_mem_get_external_fd(struct ra *ra, struct vk_memslice *mem, st
VK(pfn_vkGetMemoryFdKHR(vk->dev, &info, &mem_fd));

ret->mem_fd = mem_fd;
#endif
ret->size = mem->size;
ret->offset = mem->offset;
ret->mem_size = mem->slab_size;
Expand All @@ -960,7 +982,7 @@ static bool ra_vk_mem_get_external_fd(struct ra *ra, struct vk_memslice *mem, st
return false;
}

bool ra_vk_buf_get_external_fd(struct ra *ra, struct ra_buf *buf, struct vk_external_fd *ret)
bool ra_vk_buf_get_external_info(struct ra *ra, struct ra_buf *buf, struct vk_external_mem *ret)
{
if (buf->params.type != RA_BUF_TYPE_SHARED_MEMORY) {
MP_ERR(ra, "Buffer must be of TYPE_SHARED_MEMORY to be able to export it...");
Expand All @@ -970,7 +992,7 @@ bool ra_vk_buf_get_external_fd(struct ra *ra, struct ra_buf *buf, struct vk_exte
struct ra_buf_vk *buf_vk = buf->priv;
struct vk_memslice *mem = &buf_vk->slice.mem;

return ra_vk_mem_get_external_fd(ra, mem, ret);
return ra_vk_mem_get_external_info(ra, mem, ret);
}

#define MPVK_NUM_DS MPVK_MAX_STREAMING_DEPTH
Expand Down
10 changes: 7 additions & 3 deletions video/out/vulkan/ra_vk.h
Expand Up @@ -30,15 +30,19 @@ struct vk_cmd *ra_vk_submit(struct ra *ra, struct ra_tex *tex);
// a vulkan ra.
struct mpvk_ctx *ra_vk_get(struct ra *ra);

struct vk_external_fd {
struct vk_external_mem {
#if HAVE_WIN32_DESKTOP
HANDLE mem_handle;
#else
int mem_fd;
#endif
size_t mem_size;
size_t size;
size_t offset;
};

// Export an ra_buf as an external fd.
bool ra_vk_buf_get_external_fd(struct ra *ra, struct ra_buf *buf, struct vk_external_fd *ret);
// Export an ra_buf for importing by another api.
bool ra_vk_buf_get_external_info(struct ra *ra, struct ra_buf *buf, struct vk_external_mem *ret);

// Set the buffer user data
void ra_vk_buf_set_user_data(struct ra_buf *buf, void *priv);
Expand Down
13 changes: 13 additions & 0 deletions video/out/vulkan/utils.c
Expand Up @@ -479,11 +479,19 @@ static bool detect_device_extensions(struct mpvk_ctx *vk)
vk->has_ext_external_memory = true;
continue;
}
#if HAVE_WIN32_DESKTOP
if (!strcmp(VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME,
props[i].extensionName)) {
vk->has_ext_external_memory_win32 = true;
continue;
}
#else
if (!strcmp(VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
props[i].extensionName)) {
vk->has_ext_external_memory_fd = true;
continue;
}
#endif
}

ret = true;
Expand Down Expand Up @@ -557,8 +565,13 @@ bool mpvk_device_init(struct mpvk_ctx *vk, struct mpvk_device_opts opts)
MP_TARRAY_APPEND(tmp, exts, num_exts, VK_KHR_SWAPCHAIN_EXTENSION_NAME);
if (vk->has_ext_external_memory)
MP_TARRAY_APPEND(tmp, exts, num_exts, VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME);
#if HAVE_WIN32_DESKTOP
if (vk->has_ext_external_memory_win32)
MP_TARRAY_APPEND(tmp, exts, num_exts, VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME);
#else
if (vk->has_ext_external_memory_fd)
MP_TARRAY_APPEND(tmp, exts, num_exts, VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME);
#endif
if (vk->spirv->required_ext)
MP_TARRAY_APPEND(tmp, exts, num_exts, vk->spirv->required_ext);

Expand Down

0 comments on commit ad4e136

Please sign in to comment.