Vulkan frames can now be transferred to and from single plane D3D11
textures. The texture is imported into Vulkan and the copy runs on the
GPU, ordered against D3D11 work by a shared D3D11 fence imported as a
timeline semaphore. Callers no longer need to route these frames
through system memory.
Unsupported cases report ENOSYS instead of failing. The d3d11va side
now reports it too. When that happens, av_hwframe_transfer_data()
tries the other frames context instead.
---
libavutil/hwcontext_d3d11va.c | 8 +-
libavutil/hwcontext_vulkan.c | 676 ++++++++++++++++++++++++++++++++++
libavutil/vulkan_functions.h | 5 +-
3 files changed, 687 insertions(+), 2 deletions(-)
diff --git a/libavutil/hwcontext_d3d11va.c b/libavutil/hwcontext_d3d11va.c
index 904ccd55d5..4efa302792 100644
--- a/libavutil/hwcontext_d3d11va.c
+++ b/libavutil/hwcontext_d3d11va.c
@@ -454,9 +454,15 @@ static int d3d11va_transfer_data(AVHWFramesContext *ctx,
AVFrame *dst,
HRESULT hr;
int res;
- if (frame->hw_frames_ctx->data != (uint8_t *)ctx || other->format !=
ctx->sw_format)
+ if (frame->hw_frames_ctx->data != (uint8_t *)ctx)
return AVERROR(EINVAL);
+ /* Not a transfer to or from a software frame we can handle. Report this as
+ * unimplemented rather than invalid, so that a hardware to hardware
+ * transfer can still be tried from the other side. */
+ if (other->format != ctx->sw_format)
+ return AVERROR(ENOSYS);
+
device_hwctx->lock(device_hwctx->lock_ctx);
if (!s->staging_texture) {
diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c
index 668b3c53d9..78b87570d3 100644
--- a/libavutil/hwcontext_vulkan.c
+++ b/libavutil/hwcontext_vulkan.c
@@ -25,6 +25,14 @@
#include <windows.h> /* Included to prevent conflicts with CreateSemaphore */
#include <versionhelpers.h>
#include "compat/w32dlfcn.h"
+#if CONFIG_D3D11VA
+#define COBJMACROS
+#include <initguid.h>
+#include <d3d11.h>
+#include <d3d11_4.h>
+#include <dxgi1_2.h>
+#include "hwcontext_d3d11va.h"
+#endif
#else
#include <dlfcn.h>
#include <unistd.h>
@@ -156,6 +164,12 @@ typedef struct VulkanDevicePriv {
/* Opaque FD external semaphore properties */
VkExternalSemaphoreProperties ext_sem_props_opaque;
+#ifdef _WIN32
+ /* D3D12 fence external semaphore properties for timeline semaphores.
+ * D3D11 fences are shared through the same handle type. */
+ VkExternalSemaphoreProperties ext_sem_props_d3d12_fence;
+#endif
+
/* Enabled features */
VulkanDeviceFeatures feats;
@@ -208,6 +222,14 @@ typedef struct VulkanFramesPriv {
/* Set when physical device reports DEDICATED_ONLY for DMA-BUF export
(try_export_flags) */
int export_requires_dedicated;
+
+#if CONFIG_D3D11VA
+ /* Shared D3D11 fences imported as timeline semaphores, one for each D3D11
+ * device that frames have been transferred to or from */
+ pthread_mutex_t d3d11_sync_lock;
+ int d3d11_sync_lock_init;
+ struct D3D11SyncState *d3d11_sync;
+#endif
} VulkanFramesPriv;
typedef struct AVVkFrameInternal {
@@ -2026,6 +2048,23 @@ static int vulkan_device_init(AVHWDeviceContext *ctx)
&ext_sem_props_info,
&p->ext_sem_props_opaque);
+#ifdef _WIN32
+ /* D3D12 fence properties, queried for a timeline semaphore */
+ {
+ VkSemaphoreTypeCreateInfo timeline_info = {
+ .sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO,
+ .semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE,
+ };
+ ext_sem_props_info.pNext = &timeline_info;
+ ext_sem_props_info.handleType =
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE_BIT;
+ p->ext_sem_props_d3d12_fence.sType =
VK_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_PROPERTIES;
+ vk->GetPhysicalDeviceExternalSemaphoreProperties(hwctx->phys_dev,
+ &ext_sem_props_info,
+
&p->ext_sem_props_d3d12_fence);
+ ext_sem_props_info.pNext = NULL;
+ }
+#endif
+
qf = av_malloc_array(qf_num, sizeof(VkQueueFamilyProperties2));
if (!qf)
return AVERROR(ENOMEM);
@@ -2972,6 +3011,46 @@ static void unlock_frame(AVHWFramesContext *fc,
AVVkFrame *vkf)
pthread_mutex_unlock(&vkf->internal->update_mutex);
}
+#if CONFIG_D3D11VA
+/* A D3D11 fence shared with Vulkan as a timeline semaphore. D3D11 signals it
+ * on its immediate context after the commands that produce or consume a
+ * texture, and the transfer submission waits on the imported side, which
+ * orders the copy against D3D11 work entirely on the GPU. One state is kept
+ * for each D3D11 device that frames have been transferred to or from. */
+typedef struct D3D11SyncState {
+ struct D3D11SyncState *next;
+ ID3D11Device *dev; /* identity of the paired device */
+ ID3D11DeviceContext4 *ctx4;
+ ID3D11Fence *fence;
+ VkSemaphore sem; /* the fence imported into Vulkan */
+ uint64_t value; /* last signaled point, device lock held */
+} D3D11SyncState;
+
+static void d3d11_sync_states_free(AVHWFramesContext *hwfc)
+{
+ VulkanFramesPriv *fp = hwfc->hwctx;
+ VulkanDevicePriv *p = hwfc->device_ctx->hwctx;
+ AVVulkanDeviceContext *hwctx = &p->p;
+ FFVulkanFunctions *vk = &p->vkctx.vkfn;
+ D3D11SyncState *sync = fp->d3d11_sync;
+
+ while (sync) {
+ D3D11SyncState *next = sync->next;
+ if (sync->sem)
+ vk->DestroySemaphore(hwctx->act_dev, sync->sem, hwctx->alloc);
+ if (sync->fence)
+ ID3D11Fence_Release(sync->fence);
+ if (sync->ctx4)
+ ID3D11DeviceContext4_Release(sync->ctx4);
+ if (sync->dev)
+ ID3D11Device_Release(sync->dev);
+ av_free(sync);
+ sync = next;
+ }
+ fp->d3d11_sync = NULL;
+}
+#endif
+
static void vulkan_frames_uninit(AVHWFramesContext *hwfc)
{
VulkanDevicePriv *p = hwfc->device_ctx->hwctx;
@@ -2987,6 +3066,14 @@ static void vulkan_frames_uninit(AVHWFramesContext *hwfc)
ff_vk_exec_pool_free(&p->vkctx, &fp->upload_exec);
ff_vk_exec_pool_free(&p->vkctx, &fp->download_exec);
+#if CONFIG_D3D11VA
+ /* After the exec pools have drained every submission that could still
+ * wait on the imported semaphores */
+ d3d11_sync_states_free(hwfc);
+ if (fp->d3d11_sync_lock_init)
+ pthread_mutex_destroy(&fp->d3d11_sync_lock);
+#endif
+
av_refstruct_pool_uninit(&fp->tmp);
}
@@ -3136,6 +3223,13 @@ static int vulkan_frames_init(AVHWFramesContext *hwfc)
((hwctx->usage &
VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR) &&
!(hwctx->usage &
VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR)));
+#if CONFIG_D3D11VA
+ err = pthread_mutex_init(&fp->d3d11_sync_lock, NULL);
+ if (err != 0)
+ return AVERROR(err);
+ fp->d3d11_sync_lock_init = 1;
+#endif
+
/* Defaults */
if (!hwctx->nb_layers)
hwctx->nb_layers = 1;
@@ -5040,12 +5134,587 @@ end:
return err;
}
+#if CONFIG_D3D11VA
+
+static VkFormat d3d11_to_vulkan_fmt(DXGI_FORMAT f)
+{
+ switch (f) {
+ case DXGI_FORMAT_B8G8R8A8_UNORM: return VK_FORMAT_B8G8R8A8_UNORM;
+ case DXGI_FORMAT_R8G8B8A8_UNORM: return VK_FORMAT_R8G8B8A8_UNORM;
+ case DXGI_FORMAT_R10G10B10A2_UNORM: return
VK_FORMAT_A2B10G10R10_UNORM_PACK32;
+ case DXGI_FORMAT_R16G16B16A16_FLOAT: return VK_FORMAT_R16G16B16A16_SFLOAT;
+ default: return VK_FORMAT_UNDEFINED;
+ }
+}
+
+typedef struct D3D11Import {
+ VkImage img;
+ VkDeviceMemory mem;
+} D3D11Import;
+
+static void d3d11_import_free(AVHWFramesContext *hwfc, D3D11Import *im)
+{
+ VulkanDevicePriv *p = hwfc->device_ctx->hwctx;
+ AVVulkanDeviceContext *hwctx = &p->p;
+ FFVulkanFunctions *vk = &p->vkctx.vkfn;
+
+ if (im->img)
+ vk->DestroyImage(hwctx->act_dev, im->img, hwctx->alloc);
+ if (im->mem)
+ vk->FreeMemory(hwctx->act_dev, im->mem, hwctx->alloc);
+ im->img = VK_NULL_HANDLE;
+ im->mem = VK_NULL_HANDLE;
+}
+
+/* Find or create the fence pair for this D3D11 device. Setups on which the
+ * fence cannot be created or imported report unimplemented, so the caller
+ * falls back to system memory. */
+static int d3d11_sync_state_get(AVHWFramesContext *hwfc,
+ AVD3D11VADeviceContext *d3d_hw,
+ D3D11SyncState **out)
+{
+ VulkanFramesPriv *fp = hwfc->hwctx;
+ VulkanDevicePriv *p = hwfc->device_ctx->hwctx;
+ AVVulkanDeviceContext *hwctx = &p->p;
+ FFVulkanFunctions *vk = &p->vkctx.vkfn;
+ VkSemaphoreTypeCreateInfo timeline_info = {
+ .sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO,
+ .semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE,
+ };
+ VkSemaphoreCreateInfo sem_info = {
+ .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
+ .pNext = &timeline_info,
+ };
+ VkImportSemaphoreWin32HandleInfoKHR imp;
+ D3D11SyncState *sync = NULL;
+ ID3D11Device5 *dev5 = NULL;
+ HANDLE handle = NULL;
+ VkResult ret;
+ HRESULT hr;
+ int err = AVERROR(ENOSYS);
+
+ pthread_mutex_lock(&fp->d3d11_sync_lock);
+
+ for (sync = fp->d3d11_sync; sync; sync = sync->next) {
+ if (sync->dev == d3d_hw->device) {
+ pthread_mutex_unlock(&fp->d3d11_sync_lock);
+ *out = sync;
+ return 0;
+ }
+ }
+
+ if (!(p->vkctx.extensions & FF_VK_EXT_EXTERNAL_WIN32_SEM) ||
+ !(p->ext_sem_props_d3d12_fence.externalSemaphoreFeatures &
+ VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT)) {
+ av_log(hwfc, AV_LOG_DEBUG, "D3D12 fence import is not supported\n");
+ goto fail;
+ }
+
+ sync = av_mallocz(sizeof(*sync));
+ if (!sync) {
+ err = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ /* Fences arrived in D3D11.4, so a device that predates them has nothing
+ * the copy could synchronize against. */
+ hr = ID3D11Device_QueryInterface(d3d_hw->device, &IID_ID3D11Device5,
+ (void **)&dev5);
+ if (SUCCEEDED(hr))
+ hr = ID3D11DeviceContext_QueryInterface(d3d_hw->device_context,
+ &IID_ID3D11DeviceContext4,
+ (void **)&sync->ctx4);
+ if (SUCCEEDED(hr))
+ hr = ID3D11Device5_CreateFence(dev5, 0, D3D11_FENCE_FLAG_SHARED,
+ &IID_ID3D11Fence, (void
**)&sync->fence);
+ if (SUCCEEDED(hr))
+ hr = ID3D11Fence_CreateSharedHandle(sync->fence, NULL, GENERIC_ALL,
+ NULL, &handle);
+ if (dev5)
+ ID3D11Device5_Release(dev5);
+ if (FAILED(hr) || !handle) {
+ av_log(hwfc, AV_LOG_DEBUG, "Unable to create a shared fence (%lx)\n",
+ (long)hr);
+ goto fail;
+ }
+
+ ret = vk->CreateSemaphore(hwctx->act_dev, &sem_info, hwctx->alloc,
&sync->sem);
+ if (ret != VK_SUCCESS) {
+ av_log(hwfc, AV_LOG_ERROR, "Cannot create the import semaphore: %s\n",
+ ff_vk_ret2str(ret));
+ err = AVERROR_EXTERNAL;
+ goto fail;
+ }
+
+ imp = (VkImportSemaphoreWin32HandleInfoKHR) {
+ .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_WIN32_HANDLE_INFO_KHR,
+ .semaphore = sync->sem,
+ .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE_BIT,
+ .handle = handle,
+ };
+ ret = vk->ImportSemaphoreWin32HandleKHR(hwctx->act_dev, &imp);
+ if (ret != VK_SUCCESS) {
+ av_log(hwfc, AV_LOG_DEBUG, "Cannot import the fence: %s\n",
+ ff_vk_ret2str(ret));
+ goto fail;
+ }
+ CloseHandle(handle);
+
+ ID3D11Device_AddRef(d3d_hw->device);
+ sync->dev = d3d_hw->device;
+ sync->next = fp->d3d11_sync;
+ fp->d3d11_sync = sync;
+ pthread_mutex_unlock(&fp->d3d11_sync_lock);
+
+ *out = sync;
+ return 0;
+
+fail:
+ if (handle)
+ CloseHandle(handle);
+ if (sync) {
+ if (sync->sem)
+ vk->DestroySemaphore(hwctx->act_dev, sync->sem, hwctx->alloc);
+ if (sync->fence)
+ ID3D11Fence_Release(sync->fence);
+ if (sync->ctx4)
+ ID3D11DeviceContext4_Release(sync->ctx4);
+ av_free(sync);
+ }
+ pthread_mutex_unlock(&fp->d3d11_sync_lock);
+ return err;
+}
+
+/* Import a shared D3D11 texture as a VkImage aliasing the same memory. The
+ * texture must be single-subresource and created with
D3D11_RESOURCE_MISC_SHARED. */
+static int d3d11_import(AVHWFramesContext *hwfc, ID3D11Texture2D *tex,
+ D3D11Import *im)
+{
+ VulkanDevicePriv *p = hwfc->device_ctx->hwctx;
+ AVVulkanDeviceContext *hwctx = &p->p;
+ FFVulkanFunctions *vk = &p->vkctx.vkfn;
+ VkExternalMemoryHandleTypeFlagBits handle_type;
+ VkExternalMemoryImageCreateInfo ext_info;
+ VkMemoryWin32HandlePropertiesKHR hprops = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_WIN32_HANDLE_PROPERTIES_KHR,
+ };
+ VkImageMemoryRequirementsInfo2 req_info = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
+ };
+ VkMemoryRequirements2 req = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
+ };
+ D3D11_TEXTURE2D_DESC desc;
+ HANDLE handle = NULL;
+ uint32_t bits;
+ int nt_handle = 0;
+ VkImageUsageFlags usage;
+ VkResult ret;
+ HRESULT hr;
+ int index, err = AVERROR_EXTERNAL;
+
+ memset(im, 0, sizeof(*im));
+
+ ID3D11Texture2D_GetDesc(tex, &desc);
+
+ if (desc.ArraySize != 1) {
+ av_log(hwfc, AV_LOG_DEBUG, "Cannot import a D3D11 texture array\n");
+ return AVERROR(ENOSYS);
+ }
+ /* The copy below covers the whole frame, so anything but an exact match
+ * would read or write outside one of the two images. */
+ if (desc.Width != hwfc->width || desc.Height != hwfc->height) {
+ av_log(hwfc, AV_LOG_DEBUG, "D3D11 texture is %ux%u, expected %dx%d\n",
+ (unsigned)desc.Width, (unsigned)desc.Height,
+ hwfc->width, hwfc->height);
+ return AVERROR(ENOSYS);
+ }
+ /* Keyed mutex sharing only guarantees coherency to a user that acquires
the
+ * mutex around every access, which this code does not do. Such a texture
+ * imports without complaint and then reads back contents that predate what
+ * D3D11 wrote, so refuse it and let the caller use system memory. */
+ if (desc.MiscFlags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX) {
+ av_log(hwfc, AV_LOG_DEBUG, "D3D11 texture is shared through a keyed
mutex\n");
+ return AVERROR(ENOSYS);
+ }
+ if (desc.MiscFlags & D3D11_RESOURCE_MISC_SHARED_NTHANDLE) {
+ nt_handle = 1;
+ handle_type = VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT;
+ } else if (desc.MiscFlags & D3D11_RESOURCE_MISC_SHARED) {
+ handle_type = VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_KMT_BIT;
+ } else {
+ av_log(hwfc, AV_LOG_DEBUG, "D3D11 texture is not shared\n");
+ return AVERROR(ENOSYS);
+ }
+ if (d3d11_to_vulkan_fmt(desc.Format) == VK_FORMAT_UNDEFINED) {
+ av_log(hwfc, AV_LOG_DEBUG, "DXGI format %d cannot be imported\n",
+ (int)desc.Format);
+ return AVERROR(ENOSYS);
+ }
+
+ /* The layout a driver picks for an image can depend on how it may be
+ * used, so mirror the texture's bind flags. */
+ usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
+ if (desc.BindFlags & D3D11_BIND_SHADER_RESOURCE)
+ usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
+ if (desc.BindFlags & D3D11_BIND_RENDER_TARGET)
+ usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
+ if (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS)
+ usage |= VK_IMAGE_USAGE_STORAGE_BIT;
+
+ /* Creating an external image is only defined for combinations the
+ * implementation reports as importable */
+ {
+ VkPhysicalDeviceExternalImageFormatInfo ext_fmt_info = {
+ .sType =
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO,
+ .handleType = handle_type,
+ };
+ VkPhysicalDeviceImageFormatInfo2 fmt_info = {
+ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
+ .pNext = &ext_fmt_info,
+ .format = d3d11_to_vulkan_fmt(desc.Format),
+ .type = VK_IMAGE_TYPE_2D,
+ .tiling = VK_IMAGE_TILING_OPTIMAL,
+ .usage = usage,
+ };
+ VkExternalImageFormatProperties ext_fmt_props = {
+ .sType = VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES,
+ };
+ VkImageFormatProperties2 fmt_props = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
+ .pNext = &ext_fmt_props,
+ };
+
+ ret = vk->GetPhysicalDeviceImageFormatProperties2(hwctx->phys_dev,
+ &fmt_info,
&fmt_props);
+ if (ret != VK_SUCCESS ||
+ !(ext_fmt_props.externalMemoryProperties.externalMemoryFeatures &
+ VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT)) {
+ av_log(hwfc, AV_LOG_DEBUG, "The image cannot import this handle
type\n");
+ return AVERROR(ENOSYS);
+ }
+ }
+
+ if (nt_handle) {
+ IDXGIResource1 *res1 = NULL;
+ hr = ID3D11Texture2D_QueryInterface(tex, &IID_IDXGIResource1, (void
**)&res1);
+ if (SUCCEEDED(hr)) {
+ hr = IDXGIResource1_CreateSharedHandle(res1, NULL,
+ DXGI_SHARED_RESOURCE_READ |
+ DXGI_SHARED_RESOURCE_WRITE,
+ NULL, &handle);
+ IDXGIResource1_Release(res1);
+ }
+ } else {
+ IDXGIResource *res = NULL;
+ hr = ID3D11Texture2D_QueryInterface(tex, &IID_IDXGIResource, (void
**)&res);
+ if (SUCCEEDED(hr)) {
+ hr = IDXGIResource_GetSharedHandle(res, &handle);
+ IDXGIResource_Release(res);
+ }
+ }
+ if (FAILED(hr) || !handle) {
+ av_log(hwfc, AV_LOG_ERROR, "Unable to get a shared handle (%lx)\n",
(long)hr);
+ return AVERROR_EXTERNAL;
+ }
+
+ ext_info = (VkExternalMemoryImageCreateInfo) {
+ .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
+ .handleTypes = handle_type,
+ };
+
+ ret = vk->CreateImage(hwctx->act_dev, &(VkImageCreateInfo) {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
+ .pNext = &ext_info,
+ .imageType = VK_IMAGE_TYPE_2D,
+ .format = d3d11_to_vulkan_fmt(desc.Format),
+ .extent = { desc.Width, desc.Height, 1 },
+ .mipLevels = 1,
+ .arrayLayers = 1,
+ .samples = VK_SAMPLE_COUNT_1_BIT,
+ .tiling = VK_IMAGE_TILING_OPTIMAL,
+ .usage = usage,
+ .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
+ .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
+ }, hwctx->alloc, &im->img);
+ if (ret != VK_SUCCESS) {
+ av_log(hwfc, AV_LOG_ERROR, "Cannot create the import image: %s\n",
+ ff_vk_ret2str(ret));
+ goto fail;
+ }
+
+ /* A handle this device cannot import at all, a texture belonging to
+ * another adapter being the likely reason, is a fall back rather than
+ * an error, so report it as unimplemented. */
+ ret = vk->GetMemoryWin32HandlePropertiesKHR(hwctx->act_dev, handle_type,
+ handle, &hprops);
+ if (ret != VK_SUCCESS) {
+ av_log(hwfc, AV_LOG_DEBUG, "Cannot query the handle properties: %s\n",
+ ff_vk_ret2str(ret));
+ err = AVERROR(ENOSYS);
+ goto fail;
+ }
+
+ req_info.image = im->img;
+ vk->GetImageMemoryRequirements2(hwctx->act_dev, &req_info, &req);
+
+ bits = req.memoryRequirements.memoryTypeBits & hprops.memoryTypeBits;
+ if (!bits) {
+ av_log(hwfc, AV_LOG_ERROR, "No memory type is common to the image "
+ "and the imported handle\n");
+ goto fail;
+ }
+ for (index = 0; !(bits & (1u << index)); index++)
+ ;
+
+ /* Imports of D3D11 textures are reported as dedicated-only */
+ ret = vk->AllocateMemory(hwctx->act_dev, &(VkMemoryAllocateInfo) {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
+ .pNext = &(VkMemoryDedicatedAllocateInfo) {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
+ .pNext = &(VkImportMemoryWin32HandleInfoKHR) {
+ .sType =
VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR,
+ .handleType = handle_type,
+ .handle = handle,
+ },
+ .image = im->img,
+ },
+ .allocationSize = req.memoryRequirements.size,
+ .memoryTypeIndex = index,
+ }, hwctx->alloc, &im->mem);
+ if (ret != VK_SUCCESS) {
+ av_log(hwfc, AV_LOG_ERROR, "Cannot import the texture memory: %s\n",
+ ff_vk_ret2str(ret));
+ goto fail;
+ }
+
+ ret = vk->BindImageMemory2(hwctx->act_dev, 1, &(VkBindImageMemoryInfo) {
+ .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO,
+ .image = im->img,
+ .memory = im->mem,
+ });
+ if (ret != VK_SUCCESS) {
+ av_log(hwfc, AV_LOG_ERROR, "Cannot bind the imported memory: %s\n",
+ ff_vk_ret2str(ret));
+ goto fail;
+ }
+
+ /* Importing an NT handle does not transfer ownership, so ours has to be
+ * closed. KMT handles are not reference counted and must not be closed. */
+ if (nt_handle)
+ CloseHandle(handle);
+
+ return 0;
+
+fail:
+ if (nt_handle)
+ CloseHandle(handle);
+ d3d11_import_free(hwfc, im);
+ return err;
+}
+
+/* Copy between a Vulkan frame and a D3D11 texture. D3D11 cannot import Vulkan
+ * memory, so the texture is always the side that gets imported, and the copy
+ * itself runs on the GPU. */
+static int vulkan_transfer_d3d11(AVHWFramesContext *hwfc, AVFrame *dst,
+ const AVFrame *src, int upload)
+{
+ VulkanDevicePriv *p = hwfc->device_ctx->hwctx;
+ VulkanFramesPriv *fp = hwfc->hwctx;
+ FFVulkanFunctions *vk = &p->vkctx.vkfn;
+ AVFrame *hwf = (AVFrame *)(upload ? dst : src);
+ const AVFrame *d3df = upload ? src : dst;
+ AVVkFrame *hwf_vk = (AVVkFrame *)hwf->data[0];
+ AVHWFramesContext *d3d_fc;
+ AVD3D11VADeviceContext *d3d_hw;
+ VkImageMemoryBarrier2 img_bar[AV_NUM_DATA_POINTERS + 1];
+ int nb_img_bar = 0;
+ VkImageCopy region;
+ D3D11SyncState *sync;
+ D3D11Import im;
+ uint64_t sync_point;
+ FFVkExecContext *exec;
+ VkCommandBuffer cmd_buf;
+ int err;
+
+ if ((intptr_t)d3df->data[1] != 0)
+ return AVERROR(ENOSYS); /* an index into a texture array */
+
+ /* Multi-planar textures cannot be imported: their imports are reported as
+ * dedicated-only, a dedicated import always maps the start of the
+ * resource, and no API reports where the other planes begin. Refuse them
+ * and the caller falls back to system memory. */
+ if (av_pix_fmt_count_planes(hwfc->sw_format) != 1)
+ return AVERROR(ENOSYS);
+
+ /* The copy moves bits, so the two sides have to agree on what the bits
+ * mean. Differing formats of the same size would silently reinterpret the
+ * pixels, and differing sizes are not a legal copy at all. */
+ if (!d3df->hw_frames_ctx ||
+ ((AVHWFramesContext *)d3df->hw_frames_ctx->data)->sw_format !=
hwfc->sw_format)
+ return AVERROR(ENOSYS);
+
+ d3d_fc = (AVHWFramesContext *)d3df->hw_frames_ctx->data;
+ d3d_hw = d3d_fc->device_ctx->hwctx;
+
+ err = d3d11_sync_state_get(hwfc, d3d_hw, &sync);
+ if (err < 0)
+ return err;
+
+ err = d3d11_import(hwfc, (ID3D11Texture2D *)d3df->data[0], &im);
+ if (err < 0)
+ return err;
+
+ /* Signal the fence after the D3D11 commands that produced the texture, or
+ * that may still be reading the one about to be overwritten. The copy
+ * below waits for that point on the imported side, so it is ordered
+ * against D3D11 work without blocking the CPU. Signals go only this way:
+ * a D3D11 wait for a fence value signaled from a Vulkan queue takes a
+ * scheduler timeout of more than a second to wake on current drivers. */
+ d3d_hw->lock(d3d_hw->lock_ctx);
+ sync_point = ++sync->value;
+ ID3D11DeviceContext4_Signal(sync->ctx4, sync->fence, sync_point);
+ ID3D11DeviceContext_Flush(d3d_hw->device_context);
+ d3d_hw->unlock(d3d_hw->lock_ctx);
+
+ exec = ff_vk_exec_get(&p->vkctx, upload ? &fp->upload_exec :
+ &fp->download_exec);
+ cmd_buf = exec->buf;
+ err = ff_vk_exec_start(&p->vkctx, exec);
+ if (err < 0) {
+ d3d11_import_free(hwfc, &im);
+ return err;
+ }
+
+ err = ff_vk_exec_add_dep_frame(&p->vkctx, exec, hwf,
+ VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
+ VK_PIPELINE_STAGE_2_TRANSFER_BIT);
+ if (err < 0)
+ goto fail;
+
+ /* ALL_COMMANDS rather than TRANSFER: the wait has to order the queue
+ * family acquire below too, and acquire operations happen in no defined
+ * stage. */
+ ff_vk_exec_add_dep_wait_sem(&p->vkctx, exec, sync->sem, sync_point,
+ VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT);
+
+ ff_vk_frame_barrier(&p->vkctx, exec, hwf, img_bar, &nb_img_bar,
+ VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
+ VK_PIPELINE_STAGE_2_TRANSFER_BIT_KHR,
+ upload ? VK_ACCESS_TRANSFER_WRITE_BIT :
+ VK_ACCESS_TRANSFER_READ_BIT,
+ upload ? VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL :
+ VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
+ p->nb_img_qfs > 1 ? VK_QUEUE_FAMILY_IGNORED :
p->img_qfs[0]);
+
+ /* Acquire the imported image from the external owner. When we are reading
+ * it, the contents D3D11 left behind have to be preserved, so it cannot
+ * be acquired from VK_IMAGE_LAYOUT_UNDEFINED. */
+ img_bar[nb_img_bar++] = (VkImageMemoryBarrier2) {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
+ .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
+ .dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT,
+ .srcAccessMask = 0,
+ .dstAccessMask = upload ? VK_ACCESS_2_TRANSFER_READ_BIT :
+ VK_ACCESS_2_TRANSFER_WRITE_BIT,
+ .oldLayout = upload ? VK_IMAGE_LAYOUT_GENERAL :
+ VK_IMAGE_LAYOUT_UNDEFINED,
+ .newLayout = upload ? VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL :
+ VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
+ /* This is an ownership acquire, so the destination has to be the
+ * queue family the command buffer itself was allocated from. */
+ .srcQueueFamilyIndex = VK_QUEUE_FAMILY_EXTERNAL,
+ .dstQueueFamilyIndex = exec->qf,
+ .image = im.img,
+ .subresourceRange = {
+ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
+ .levelCount = 1,
+ .layerCount = 1,
+ },
+ };
+
+ vk->CmdPipelineBarrier2(cmd_buf, &(VkDependencyInfo) {
+ .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
+ .pImageMemoryBarriers = img_bar,
+ .imageMemoryBarrierCount = nb_img_bar,
+ });
+
+ region = (VkImageCopy) {
+ .srcSubresource = { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.layerCount = 1 },
+ .dstSubresource = { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.layerCount = 1 },
+ .extent = { hwfc->width, hwfc->height, 1 },
+ };
+ if (upload)
+ vk->CmdCopyImage(cmd_buf, im.img, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
+ hwf_vk->img[0], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
+ 1, ®ion);
+ else
+ vk->CmdCopyImage(cmd_buf, hwf_vk->img[0],
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
+ im.img, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
+ 1, ®ion);
+
+ /* Release the imported image back to the external owner, which makes the
+ * write available to D3D11 when this was a download. */
+ img_bar[0] = (VkImageMemoryBarrier2) {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
+ .srcStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT,
+ .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
+ .srcAccessMask = upload ? VK_ACCESS_2_TRANSFER_READ_BIT :
+ VK_ACCESS_2_TRANSFER_WRITE_BIT,
+ .dstAccessMask = 0,
+ .oldLayout = upload ? VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL :
+ VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
+ .newLayout = VK_IMAGE_LAYOUT_GENERAL,
+ .srcQueueFamilyIndex = exec->qf,
+ .dstQueueFamilyIndex = VK_QUEUE_FAMILY_EXTERNAL,
+ .image = im.img,
+ .subresourceRange = {
+ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
+ .levelCount = 1,
+ .layerCount = 1,
+ },
+ };
+
+ vk->CmdPipelineBarrier2(cmd_buf, &(VkDependencyInfo) {
+ .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
+ .pImageMemoryBarriers = img_bar,
+ .imageMemoryBarrierCount = 1,
+ });
+
+ err = ff_vk_exec_submit(&p->vkctx, exec);
+ if (err >= 0) {
+ /* The D3D11 side cannot wait for the copy on the GPU, see above, so
+ * the transfer completes before it returns, like the system memory
+ * paths do. */
+ ff_vk_exec_wait(&p->vkctx, exec);
+ }
+
+ d3d11_import_free(hwfc, &im);
+
+ return err;
+
+fail:
+ ff_vk_exec_discard(&p->vkctx, exec);
+ d3d11_import_free(hwfc, &im);
+ return err;
+}
+
+#endif /* CONFIG_D3D11VA */
+
static int vulkan_transfer_data_to(AVHWFramesContext *hwfc, AVFrame *dst,
const AVFrame *src)
{
av_unused VulkanDevicePriv *p = hwfc->device_ctx->hwctx;
switch (src->format) {
+#if CONFIG_D3D11VA
+ case AV_PIX_FMT_D3D11:
+ if (p->vkctx.extensions & FF_VK_EXT_EXTERNAL_WIN32_MEMORY)
+ return vulkan_transfer_d3d11(hwfc, dst, src, 1);
+ /* Deliberately not falling through: the CUDA case below would then be
+ * reached with a D3D11 frame. The generic path rejects hw frames too.
*/
+ return AVERROR(ENOSYS);
+#endif
#if CONFIG_CUDA
case AV_PIX_FMT_CUDA:
#ifdef _WIN32
@@ -5167,6 +5836,13 @@ static int vulkan_transfer_data_from(AVHWFramesContext
*hwfc, AVFrame *dst,
av_unused VulkanDevicePriv *p = hwfc->device_ctx->hwctx;
switch (dst->format) {
+#if CONFIG_D3D11VA
+ case AV_PIX_FMT_D3D11:
+ if (p->vkctx.extensions & FF_VK_EXT_EXTERNAL_WIN32_MEMORY)
+ return vulkan_transfer_d3d11(hwfc, dst, src, 0);
+ /* Deliberately not falling through, as above. */
+ return AVERROR(ENOSYS);
+#endif
#if CONFIG_CUDA
case AV_PIX_FMT_CUDA:
#ifdef _WIN32
diff --git a/libavutil/vulkan_functions.h b/libavutil/vulkan_functions.h
index 16398ab9d0..a3cff77bda 100644
--- a/libavutil/vulkan_functions.h
+++ b/libavutil/vulkan_functions.h
@@ -156,6 +156,7 @@ typedef uint64_t FFVulkanExtensions;
MACRO(1, 1, FF_VK_EXT_NO_FLAG, CmdPipelineBarrier)
\
MACRO(1, 1, FF_VK_EXT_NO_FLAG, CmdCopyBufferToImage)
\
MACRO(1, 1, FF_VK_EXT_NO_FLAG, CmdCopyImageToBuffer)
\
+ MACRO(1, 1, FF_VK_EXT_NO_FLAG, CmdCopyImage)
\
MACRO(1, 1, FF_VK_EXT_NO_FLAG, CmdClearColorImage)
\
MACRO(1, 1, FF_VK_EXT_NO_FLAG, CmdCopyBuffer)
\
MACRO(1, 1, FF_VK_EXT_NO_FLAG, CmdUpdateBuffer)
\
@@ -265,7 +266,9 @@ typedef uint64_t FFVulkanExtensions;
/* Macro containing every win32 specific function that we utilize in our
codebase */
#define FN_LIST_WIN32(MACRO)
\
MACRO(1, 1, FF_VK_EXT_EXTERNAL_WIN32_SEM, GetSemaphoreWin32HandleKHR)
\
- MACRO(1, 1, FF_VK_EXT_EXTERNAL_WIN32_MEMORY, GetMemoryWin32HandleKHR)
+ MACRO(1, 1, FF_VK_EXT_EXTERNAL_WIN32_SEM,
ImportSemaphoreWin32HandleKHR) \
+ MACRO(1, 1, FF_VK_EXT_EXTERNAL_WIN32_MEMORY, GetMemoryWin32HandleKHR)
\
+ MACRO(1, 1, FF_VK_EXT_EXTERNAL_WIN32_MEMORY,
GetMemoryWin32HandlePropertiesKHR)
/* Macro to turn a function name into a definition */
#define PFN_DEF(req_inst, req_dev, ext_flag, name) \
--
2.45.1.windows.1
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]