NV12 and P010 frames now transfer on the GPU as well. A two plane
texture cannot be imported whole and D3D11 copies cannot address its
planes, so each plane moves through a shared single plane texture,
written and read by a compute shader on the D3D11 side and imported
into Vulkan. Texture arrays and padded decoder textures work too,
which the whole texture import could not. The bridge owning the plane
textures and shaders lives in hwcontext_d3d11va, where other interop
paths can share it.
---
libavutil/Makefile | 3 +-
libavutil/hwcontext_d3d11va.c | 278 ++++++++++++++++++++++
libavutil/hwcontext_d3d11va_internal.h | 79 +++++++
libavutil/hwcontext_vulkan.c | 313 +++++++++++++++++--------
4 files changed, 580 insertions(+), 93 deletions(-)
create mode 100644 libavutil/hwcontext_d3d11va_internal.h
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 9cb3108b38..5c8af36625 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -240,7 +240,8 @@ SKIPHEADERS-$(CONFIG_ZLIB) += zlib_utils.h
SKIPHEADERS-$(HAVE_CUDA_H) += hwcontext_cuda.h
SKIPHEADERS-$(CONFIG_CUDA) += hwcontext_cuda_internal.h \
cuda_check.h
-SKIPHEADERS-$(CONFIG_D3D11VA) += hwcontext_d3d11va.h
+SKIPHEADERS-$(CONFIG_D3D11VA) += hwcontext_d3d11va.h \
+ hwcontext_d3d11va_internal.h
SKIPHEADERS-$(CONFIG_D3D12VA) += hwcontext_d3d12va.h
SKIPHEADERS-$(CONFIG_DXVA2) += hwcontext_dxva2.h
SKIPHEADERS-$(CONFIG_AMF) += hwcontext_amf.h \
diff --git a/libavutil/hwcontext_d3d11va.c b/libavutil/hwcontext_d3d11va.c
index 4efa302792..023a8d7fe5 100644
--- a/libavutil/hwcontext_d3d11va.c
+++ b/libavutil/hwcontext_d3d11va.c
@@ -24,6 +24,7 @@
#include <initguid.h>
#include <d3d11.h>
+#include <d3dcompiler.h>
#include <dxgi1_2.h>
#if HAVE_DXGIDEBUG_H
@@ -34,6 +35,7 @@
#include "common.h"
#include "hwcontext.h"
#include "hwcontext_d3d11va.h"
+#include "hwcontext_d3d11va_internal.h"
#include "hwcontext_internal.h"
#if CONFIG_CUDA
#include "cuda_check.h"
@@ -519,6 +521,282 @@ map_failed:
return AVERROR_UNKNOWN;
}
+#if CONFIG_VULKAN || CONFIG_CUDA
+
+/* The plane bridge shared by the interop transfer paths, see
+ * hwcontext_d3d11va_internal.h for what it is and why. */
+
+static const char bridge_shader_r[] =
+ "Texture2D<float> s : register(t0);\n"
+ "RWTexture2D<float> d : register(u0);\n"
+ "[numthreads(8, 8, 1)]\n"
+ "void main(uint3 t : SV_DispatchThreadID) { d[t.xy] = s[t.xy]; }\n";
+
+static const char bridge_shader_rg[] =
+ "Texture2D<float2> s : register(t0);\n"
+ "RWTexture2D<float2> d : register(u0);\n"
+ "[numthreads(8, 8, 1)]\n"
+ "void main(uint3 t : SV_DispatchThreadID) { d[t.xy] = s[t.xy]; }\n";
+
+int ff_d3d11va_bridge_formats(enum AVPixelFormat sw, DXGI_FORMAT *tex,
+ DXGI_FORMAT plane[2])
+{
+ switch (sw) {
+ case AV_PIX_FMT_NV12:
+ *tex = DXGI_FORMAT_NV12;
+ plane[0] = DXGI_FORMAT_R8_UNORM;
+ plane[1] = DXGI_FORMAT_R8G8_UNORM;
+ return 0;
+ case AV_PIX_FMT_P010:
+ case AV_PIX_FMT_P012:
+ case AV_PIX_FMT_P016:
+ *tex = sw == AV_PIX_FMT_P010 ? DXGI_FORMAT_P010 : DXGI_FORMAT_P016;
+ plane[0] = DXGI_FORMAT_R16_UNORM;
+ plane[1] = DXGI_FORMAT_R16G16_UNORM;
+ return 0;
+ default:
+ return AVERROR(ENOSYS);
+ }
+}
+
+void ff_d3d11va_bridge_free(FFD3D11PlaneBridge **bridge)
+{
+ FFD3D11PlaneBridge *b = *bridge;
+
+ if (!b)
+ return;
+ for (int i = 0; i < 2; i++) {
+ if (b->plane_uav[i])
+ ID3D11UnorderedAccessView_Release(b->plane_uav[i]);
+ if (b->plane_srv[i])
+ ID3D11ShaderResourceView_Release(b->plane_srv[i]);
+ if (b->planes[i])
+ ID3D11Texture2D_Release(b->planes[i]);
+ if (b->staging_uav[i])
+ ID3D11UnorderedAccessView_Release(b->staging_uav[i]);
+ if (b->staging_srv[i])
+ ID3D11ShaderResourceView_Release(b->staging_srv[i]);
+ if (b->cs[i])
+ ID3D11ComputeShader_Release(b->cs[i]);
+ }
+ if (b->staging)
+ ID3D11Texture2D_Release(b->staging);
+ if (b->compiler)
+ dlclose(b->compiler);
+ av_freep(bridge);
+}
+
+int ff_d3d11va_bridge_create(FFD3D11PlaneBridge **bridge, ID3D11Device *dev,
+ int width, int height,
+ enum AVPixelFormat sw_format, void *log_ctx)
+{
+ const char *cs_src[2] = { bridge_shader_r, bridge_shader_rg };
+ const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(sw_format);
+ D3D11_TEXTURE2D_DESC desc;
+ DXGI_FORMAT fmt, pfmts[2];
+ FFD3D11PlaneBridge *b;
+ pD3DCompile compile;
+ HRESULT hr;
+ int err;
+
+ err = ff_d3d11va_bridge_formats(sw_format, &fmt, pfmts);
+ if (err < 0)
+ return err;
+
+ b = *bridge = av_mallocz(sizeof(*b));
+ if (!b)
+ return AVERROR(ENOMEM);
+ b->width = width;
+ b->height = height;
+ b->plane_w[0] = width;
+ b->plane_h[0] = height;
+ b->plane_w[1] = AV_CEIL_RSHIFT(width, pixdesc->log2_chroma_w);
+ b->plane_h[1] = AV_CEIL_RSHIFT(height, pixdesc->log2_chroma_h);
+
+ /* The shaders are compiled at run time: libavutil cannot compile HLSL at
+ * build time, and d3dcompiler_47 ships with the OS. */
+ b->compiler = dlopen("d3dcompiler_47.dll", 0);
+ compile = b->compiler ? (pD3DCompile)dlsym(b->compiler, "D3DCompile")
+ : NULL;
+ if (!compile) {
+ av_log(log_ctx, AV_LOG_DEBUG, "d3dcompiler_47 is not available\n");
+ err = AVERROR(ENOSYS);
+ goto fail;
+ }
+
+ for (int i = 0; i < 2; i++) {
+ ID3DBlob *code = NULL, *errors = NULL;
+ hr = compile(cs_src[i], strlen(cs_src[i]), NULL, NULL, NULL, "main",
+ "cs_5_0", 0, 0, &code, &errors);
+ if (SUCCEEDED(hr))
+ hr = ID3D11Device_CreateComputeShader(dev,
+
ID3D10Blob_GetBufferPointer(code),
+
ID3D10Blob_GetBufferSize(code),
+ NULL, &b->cs[i]);
+ if (code)
+ ID3D10Blob_Release(code);
+ if (errors)
+ ID3D10Blob_Release(errors);
+ if (FAILED(hr)) {
+ av_log(log_ctx, AV_LOG_DEBUG, "Cannot build the copy shader
(%lx)\n",
+ (long)hr);
+ err = AVERROR(ENOSYS);
+ goto fail;
+ }
+ }
+
+ desc = (D3D11_TEXTURE2D_DESC) {
+ .Width = width,
+ .Height = height,
+ .MipLevels = 1,
+ .ArraySize = 1,
+ .Format = fmt,
+ .SampleDesc = { .Count = 1 },
+ .Usage = D3D11_USAGE_DEFAULT,
+ .BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS,
+ };
+ hr = ID3D11Device_CreateTexture2D(dev, &desc, NULL, &b->staging);
+ if (FAILED(hr)) {
+ /* Without UAV support the plane textures cannot be assembled into the
+ * staging texture, but moves toward them only ever read it, so keep
+ * those working. */
+ desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
+ hr = ID3D11Device_CreateTexture2D(dev, &desc, NULL, &b->staging);
+ }
+ if (FAILED(hr)) {
+ av_log(log_ctx, AV_LOG_DEBUG, "Cannot create the staging texture
(%lx)\n",
+ (long)hr);
+ err = AVERROR(ENOSYS);
+ goto fail;
+ }
+
+ for (int i = 0; i < 2; i++) {
+ D3D11_SHADER_RESOURCE_VIEW_DESC sd = {
+ .ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D,
+ .Texture2D = { .MipLevels = 1 },
+ };
+ D3D11_UNORDERED_ACCESS_VIEW_DESC ud = {
+ .ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D,
+ };
+ D3D11_TEXTURE2D_DESC pd;
+
+ /* A view with a single-plane format selects that plane of the planar
+ * staging texture. */
+ sd.Format = pfmts[i];
+ ud.Format = pfmts[i];
+ hr = ID3D11Device_CreateShaderResourceView(dev,
+ (ID3D11Resource
*)b->staging,
+ &sd, &b->staging_srv[i]);
+ if (FAILED(hr)) {
+ av_log(log_ctx, AV_LOG_DEBUG, "Cannot create a plane view (%lx)\n",
+ (long)hr);
+ err = AVERROR(ENOSYS);
+ goto fail;
+ }
+ if (desc.BindFlags & D3D11_BIND_UNORDERED_ACCESS)
+ ID3D11Device_CreateUnorderedAccessView(dev,
+ (ID3D11Resource
*)b->staging,
+ &ud, &b->staging_uav[i]);
+
+ pd = (D3D11_TEXTURE2D_DESC) {
+ .Width = b->plane_w[i],
+ .Height = b->plane_h[i],
+ .MipLevels = 1,
+ .ArraySize = 1,
+ .Format = pfmts[i],
+ .SampleDesc = { .Count = 1 },
+ .Usage = D3D11_USAGE_DEFAULT,
+ .BindFlags = D3D11_BIND_SHADER_RESOURCE |
D3D11_BIND_UNORDERED_ACCESS,
+ .MiscFlags = D3D11_RESOURCE_MISC_SHARED,
+ };
+ hr = ID3D11Device_CreateTexture2D(dev, &pd, NULL, &b->planes[i]);
+ if (SUCCEEDED(hr))
+ hr = ID3D11Device_CreateShaderResourceView(dev,
+ (ID3D11Resource
*)b->planes[i],
+ NULL, &b->plane_srv[i]);
+ if (SUCCEEDED(hr))
+ hr = ID3D11Device_CreateUnorderedAccessView(dev,
+ (ID3D11Resource
*)b->planes[i],
+ NULL,
&b->plane_uav[i]);
+ if (FAILED(hr)) {
+ av_log(log_ctx, AV_LOG_DEBUG, "Cannot create a plane texture
(%lx)\n",
+ (long)hr);
+ err = AVERROR(ENOSYS);
+ goto fail;
+ }
+ }
+
+ /* Assembling writes every staging plane, so all or nothing. */
+ if (!b->staging_uav[0] || !b->staging_uav[1]) {
+ for (int i = 0; i < 2; i++) {
+ if (b->staging_uav[i])
+ ID3D11UnorderedAccessView_Release(b->staging_uav[i]);
+ b->staging_uav[i] = NULL;
+ }
+ }
+
+ return 0;
+
+fail:
+ ff_d3d11va_bridge_free(bridge);
+ return err;
+}
+
+void ff_d3d11va_bridge_run(FFD3D11PlaneBridge *b, ID3D11DeviceContext *ctx,
+ ID3D11Resource *tex, unsigned index, int to_planes)
+{
+ ID3D11ShaderResourceView *null_srv = NULL;
+ ID3D11UnorderedAccessView *null_uav = NULL;
+ ID3D11ComputeShader *prev_cs = NULL;
+ ID3D11ClassInstance *prev_inst[D3D11_SHADER_MAX_INTERFACES];
+ UINT prev_inst_n = FF_ARRAY_ELEMS(prev_inst);
+ ID3D11ShaderResourceView *prev_srv = NULL;
+ ID3D11UnorderedAccessView *prev_uav = NULL;
+ D3D11_BOX box = { 0, 0, 0, b->width, b->height, 1 };
+
+ /* The context belongs to the caller, so everything the passes below bind
+ * is saved here and put back at the end. */
+ ID3D11DeviceContext_CSGetShader(ctx, &prev_cs, prev_inst, &prev_inst_n);
+ ID3D11DeviceContext_CSGetShaderResources(ctx, 0, 1, &prev_srv);
+ ID3D11DeviceContext_CSGetUnorderedAccessViews(ctx, 0, 1, &prev_uav);
+
+ if (to_planes)
+ ID3D11DeviceContext_CopySubresourceRegion(ctx,
+ (ID3D11Resource *)b->staging, 0, 0, 0, 0, tex, index, &box);
+
+ for (int i = 0; i < 2; i++) {
+ ID3D11DeviceContext_CSSetShader(ctx, b->cs[i], NULL, 0);
+ ID3D11DeviceContext_CSSetShaderResources(ctx, 0, 1,
+ to_planes ? &b->staging_srv[i] : &b->plane_srv[i]);
+ ID3D11DeviceContext_CSSetUnorderedAccessViews(ctx, 0, 1,
+ to_planes ? &b->plane_uav[i] : &b->staging_uav[i], NULL);
+ ID3D11DeviceContext_Dispatch(ctx, (b->plane_w[i] + 7) / 8,
+ (b->plane_h[i] + 7) / 8, 1);
+ /* Unbind before the next pass: two views of one resource cannot be
+ * bound as input and output at the same time. */
+ ID3D11DeviceContext_CSSetShaderResources(ctx, 0, 1, &null_srv);
+ ID3D11DeviceContext_CSSetUnorderedAccessViews(ctx, 0, 1, &null_uav,
NULL);
+ }
+
+ ID3D11DeviceContext_CSSetShader(ctx, prev_cs, prev_inst, prev_inst_n);
+ ID3D11DeviceContext_CSSetShaderResources(ctx, 0, 1, &prev_srv);
+ ID3D11DeviceContext_CSSetUnorderedAccessViews(ctx, 0, 1, &prev_uav, NULL);
+ if (prev_cs)
+ ID3D11ComputeShader_Release(prev_cs);
+ for (UINT i = 0; i < prev_inst_n; i++)
+ ID3D11ClassInstance_Release(prev_inst[i]);
+ if (prev_srv)
+ ID3D11ShaderResourceView_Release(prev_srv);
+ if (prev_uav)
+ ID3D11UnorderedAccessView_Release(prev_uav);
+
+ if (!to_planes)
+ ID3D11DeviceContext_CopySubresourceRegion(ctx, tex, index, 0, 0, 0,
+ (ID3D11Resource *)b->staging, 0, NULL);
+}
+
+#endif /* CONFIG_VULKAN || CONFIG_CUDA */
+
static int d3d11va_device_init(AVHWDeviceContext *hwdev)
{
AVD3D11VADeviceContext *device_hwctx = hwdev->hwctx;
diff --git a/libavutil/hwcontext_d3d11va_internal.h
b/libavutil/hwcontext_d3d11va_internal.h
new file mode 100644
index 0000000000..0b78aeac5a
--- /dev/null
+++ b/libavutil/hwcontext_d3d11va_internal.h
@@ -0,0 +1,79 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_HWCONTEXT_D3D11VA_INTERNAL_H
+#define AVUTIL_HWCONTEXT_D3D11VA_INTERNAL_H
+
+#include <d3d11.h>
+
+#include "pixfmt.h"
+
+/**
+ * A plane bridge for sharing two-plane textures with another API. Such a
+ * texture cannot be shared whole: its import is reported as dedicated-only,
+ * a dedicated import always maps the start of the resource, and no API
+ * reports where the second plane begins. What can be shared is one texture
+ * per plane, so the bridge keeps a shared single-plane texture for each
+ * plane and moves the data between them and a staging texture in the
+ * two-plane format. D3D11's copy functions cannot address the planes of a
+ * planar texture either, so that move is a compute shader reading and
+ * writing through views that each select one plane.
+ */
+typedef struct FFD3D11PlaneBridge {
+ void *compiler; /* d3dcompiler_47.dll */
+ ID3D11ComputeShader *cs[2]; /* one and two channel copies */
+ ID3D11Texture2D *staging; /* in the two-plane format */
+ ID3D11ShaderResourceView *staging_srv[2];
+ ID3D11UnorderedAccessView *staging_uav[2]; /* missing without UAV support
*/
+ ID3D11Texture2D *planes[2]; /* created with MISC_SHARED */
+ ID3D11ShaderResourceView *plane_srv[2];
+ ID3D11UnorderedAccessView *plane_uav[2];
+ int width;
+ int height;
+ int plane_w[2]; /* in texels of the plane format */
+ int plane_h[2];
+} FFD3D11PlaneBridge;
+
+/**
+ * The DXGI format a two-plane sw format is represented by, and the single
+ * plane formats of its planes. Returns ENOSYS for everything else.
+ */
+int ff_d3d11va_bridge_formats(enum AVPixelFormat sw, DXGI_FORMAT *tex,
+ DXGI_FORMAT plane[2]);
+
+/**
+ * Create a bridge for frames of the given size and sw format. On failure
+ * (including ENOSYS for an unhandled format) *bridge is left NULL.
+ */
+int ff_d3d11va_bridge_create(FFD3D11PlaneBridge **bridge, ID3D11Device *dev,
+ int width, int height,
+ enum AVPixelFormat sw_format, void *log_ctx);
+
+void ff_d3d11va_bridge_free(FFD3D11PlaneBridge **bridge);
+
+/**
+ * Move the frame-sized region of subresource index of tex into the plane
+ * textures (to_planes) or assemble the plane textures into it. Downloads
+ * from the plane textures require staging_uav, which the caller has to
+ * check. The device lock must be held; the context's compute state is
+ * saved and restored.
+ */
+void ff_d3d11va_bridge_run(FFD3D11PlaneBridge *b, ID3D11DeviceContext *ctx,
+ ID3D11Resource *tex, unsigned index, int to_planes);
+
+#endif /* AVUTIL_HWCONTEXT_D3D11VA_INTERNAL_H */
diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c
index 78b87570d3..caff8ee02a 100644
--- a/libavutil/hwcontext_vulkan.c
+++ b/libavutil/hwcontext_vulkan.c
@@ -32,6 +32,7 @@
#include <d3d11_4.h>
#include <dxgi1_2.h>
#include "hwcontext_d3d11va.h"
+#include "hwcontext_d3d11va_internal.h"
#endif
#else
#include <dlfcn.h>
@@ -3024,6 +3025,16 @@ typedef struct D3D11SyncState {
ID3D11Fence *fence;
VkSemaphore sem; /* the fence imported into Vulkan */
uint64_t value; /* last signaled point, device lock held */
+
+ /* Plane bridge for two-plane formats, implemented by hwcontext_d3d11va
+ * (see hwcontext_d3d11va_internal.h): the bridge's shared plane textures
+ * are what Vulkan imports. All of it is created once and kept,
+ * bridge_lock serializes the transfers that use it. */
+ pthread_mutex_t bridge_lock;
+ int bridge_status; /* 0 untried, 1 ready, else error */
+ FFD3D11PlaneBridge *bridge;
+ VkImage plane_img[2];
+ VkDeviceMemory plane_mem[2];
} D3D11SyncState;
static void d3d11_sync_states_free(AVHWFramesContext *hwfc)
@@ -3042,6 +3053,14 @@ static void d3d11_sync_states_free(AVHWFramesContext
*hwfc)
ID3D11Fence_Release(sync->fence);
if (sync->ctx4)
ID3D11DeviceContext4_Release(sync->ctx4);
+ for (int i = 0; i < 2; i++) {
+ if (sync->plane_img[i])
+ vk->DestroyImage(hwctx->act_dev, sync->plane_img[i],
hwctx->alloc);
+ if (sync->plane_mem[i])
+ vk->FreeMemory(hwctx->act_dev, sync->plane_mem[i],
hwctx->alloc);
+ }
+ ff_d3d11va_bridge_free(&sync->bridge);
+ pthread_mutex_destroy(&sync->bridge_lock);
if (sync->dev)
ID3D11Device_Release(sync->dev);
av_free(sync);
@@ -5143,6 +5162,10 @@ static VkFormat d3d11_to_vulkan_fmt(DXGI_FORMAT f)
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;
+ case DXGI_FORMAT_R8_UNORM: return VK_FORMAT_R8_UNORM;
+ case DXGI_FORMAT_R8G8_UNORM: return VK_FORMAT_R8G8_UNORM;
+ case DXGI_FORMAT_R16_UNORM: return VK_FORMAT_R16_UNORM;
+ case DXGI_FORMAT_R16G16_UNORM: return VK_FORMAT_R16G16_UNORM;
default: return VK_FORMAT_UNDEFINED;
}
}
@@ -5215,6 +5238,11 @@ static int d3d11_sync_state_get(AVHWFramesContext *hwfc,
err = AVERROR(ENOMEM);
goto fail;
}
+ if (pthread_mutex_init(&sync->bridge_lock, NULL)) {
+ av_freep(&sync);
+ err = AVERROR(ENOMEM);
+ goto fail;
+ }
/* Fences arrived in D3D11.4, so a device that predates them has nothing
* the copy could synchronize against. */
@@ -5279,6 +5307,7 @@ fail:
ID3D11Fence_Release(sync->fence);
if (sync->ctx4)
ID3D11DeviceContext4_Release(sync->ctx4);
+ pthread_mutex_destroy(&sync->bridge_lock);
av_free(sync);
}
pthread_mutex_unlock(&fp->d3d11_sync_lock);
@@ -5288,7 +5317,7 @@ fail:
/* 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)
+ uint32_t width, uint32_t height, D3D11Import *im)
{
VulkanDevicePriv *p = hwfc->device_ctx->hwctx;
AVVulkanDeviceContext *hwctx = &p->p;
@@ -5321,12 +5350,12 @@ static int d3d11_import(AVHWFramesContext *hwfc,
ID3D11Texture2D *tex,
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",
+ /* The copy below covers the whole image, so anything but an exact match
+ * would read or write outside one of the two. */
+ if (desc.Width != width || desc.Height != height) {
+ av_log(hwfc, AV_LOG_DEBUG, "D3D11 texture is %ux%u, expected %ux%u\n",
(unsigned)desc.Width, (unsigned)desc.Height,
- hwfc->width, hwfc->height);
+ (unsigned)width, (unsigned)height);
return AVERROR(ENOSYS);
}
/* Keyed mutex sharing only guarantees coherency to a user that acquires
the
@@ -5513,6 +5542,37 @@ fail:
return err;
}
+/* The bridge outlives the transfer, so a failed creation attempt is not
+ * retried every frame, and whatever a failed attempt did create is released
+ * with the sync state. */
+static int d3d11_bridge_get(AVHWFramesContext *hwfc, D3D11SyncState *sync,
+ AVD3D11VADeviceContext *d3d_hw)
+{
+ VulkanFramesPriv *fp = hwfc->hwctx;
+ int err;
+
+ pthread_mutex_lock(&fp->d3d11_sync_lock);
+ if (!sync->bridge_status) {
+ err = ff_d3d11va_bridge_create(&sync->bridge, d3d_hw->device,
+ hwfc->width, hwfc->height,
+ hwfc->sw_format, hwfc);
+ for (int i = 0; !err && i < 2; i++) {
+ D3D11Import im;
+ err = d3d11_import(hwfc, sync->bridge->planes[i],
+ sync->bridge->plane_w[i],
+ sync->bridge->plane_h[i], &im);
+ if (!err) {
+ sync->plane_img[i] = im.img;
+ sync->plane_mem[i] = im.mem;
+ }
+ }
+ sync->bridge_status = err < 0 ? err : 1;
+ }
+ err = sync->bridge_status > 0 ? 0 : sync->bridge_status;
+ pthread_mutex_unlock(&fp->d3d11_sync_lock);
+ 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. */
@@ -5525,28 +5585,21 @@ static int vulkan_transfer_d3d11(AVHWFramesContext
*hwfc, AVFrame *dst,
AVFrame *hwf = (AVFrame *)(upload ? dst : src);
const AVFrame *d3df = upload ? src : dst;
AVVkFrame *hwf_vk = (AVVkFrame *)hwf->data[0];
+ const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
+ const int nb_images = ff_vk_count_images(hwf_vk);
AVHWFramesContext *d3d_fc;
AVD3D11VADeviceContext *d3d_hw;
- VkImageMemoryBarrier2 img_bar[AV_NUM_DATA_POINTERS + 1];
+ VkImageMemoryBarrier2 img_bar[AV_NUM_DATA_POINTERS + 2];
int nb_img_bar = 0;
- VkImageCopy region;
D3D11SyncState *sync;
- D3D11Import im;
+ D3D11Import im = { 0 };
+ VkImage imp_img[2];
+ int bridged = 0;
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. */
@@ -5561,9 +5614,49 @@ static int vulkan_transfer_d3d11(AVHWFramesContext
*hwfc, AVFrame *dst,
if (err < 0)
return err;
- err = d3d11_import(hwfc, (ID3D11Texture2D *)d3df->data[0], &im);
- if (err < 0)
- return err;
+ if (planes == 1) {
+ if ((intptr_t)d3df->data[1] != 0)
+ return AVERROR(ENOSYS); /* an index into a texture array */
+ err = d3d11_import(hwfc, (ID3D11Texture2D *)d3df->data[0],
+ hwfc->width, hwfc->height, &im);
+ if (err < 0)
+ return err;
+ imp_img[0] = im.img;
+ } else if (planes == 2) {
+ D3D11_TEXTURE2D_DESC desc;
+ DXGI_FORMAT tex_fmt, plane_fmts[2];
+
+ ID3D11Texture2D_GetDesc((ID3D11Texture2D *)d3df->data[0], &desc);
+
+ /* The staging copies address the frame-sized region of the texture,
+ * which decoders often pad, and copies of video formats only accept
+ * aligned regions. The texture must really be in the format the sw
+ * format implies, or the copies would silently move nothing. A
+ * keyed-mutex texture is out for the same reason as above: nothing
+ * here acquires the mutex. */
+ if (((hwfc->width | hwfc->height) & 1) ||
+ desc.Width < hwfc->width || desc.Height < hwfc->height ||
+ desc.SampleDesc.Count != 1 ||
+ (desc.MiscFlags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX) ||
+ ff_d3d11va_bridge_formats(hwfc->sw_format, &tex_fmt, plane_fmts) <
0 ||
+ desc.Format != tex_fmt)
+ return AVERROR(ENOSYS);
+
+ err = d3d11_bridge_get(hwfc, sync, d3d_hw);
+ if (err < 0)
+ return err;
+ /* Downloads write the staging texture through views its format does
+ * not support everywhere. */
+ if (!upload && !sync->bridge->staging_uav[0])
+ return AVERROR(ENOSYS);
+
+ pthread_mutex_lock(&sync->bridge_lock);
+ bridged = 1;
+ imp_img[0] = sync->plane_img[0];
+ imp_img[1] = sync->plane_img[1];
+ } else {
+ return AVERROR(ENOSYS);
+ }
/* 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
@@ -5572,6 +5665,10 @@ static int vulkan_transfer_d3d11(AVHWFramesContext
*hwfc, AVFrame *dst,
* 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);
+ if (bridged && upload)
+ ff_d3d11va_bridge_run(sync->bridge, d3d_hw->device_context,
+ (ID3D11Resource *)d3df->data[0],
+ (UINT)(intptr_t)d3df->data[1], 1);
sync_point = ++sync->value;
ID3D11DeviceContext4_Signal(sync->ctx4, sync->fence, sync_point);
ID3D11DeviceContext_Flush(d3d_hw->device_context);
@@ -5581,10 +5678,8 @@ static int vulkan_transfer_d3d11(AVHWFramesContext
*hwfc, AVFrame *dst,
&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;
- }
+ if (err < 0)
+ goto end;
err = ff_vk_exec_add_dep_frame(&p->vkctx, exec, hwf,
VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
@@ -5607,31 +5702,33 @@ static int vulkan_transfer_d3d11(AVHWFramesContext
*hwfc, AVFrame *dst,
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,
- },
- };
+ /* Acquire the imported images from the external owner. When we are
+ * reading them, the contents D3D11 left behind have to be preserved, so
+ * they cannot be acquired from VK_IMAGE_LAYOUT_UNDEFINED. */
+ for (int i = 0; i < planes; i++) {
+ 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 = imp_img[i],
+ .subresourceRange = {
+ .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
+ .levelCount = 1,
+ .layerCount = 1,
+ },
+ };
+ }
vk->CmdPipelineBarrier2(cmd_buf, &(VkDependencyInfo) {
.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
@@ -5639,46 +5736,66 @@ static int vulkan_transfer_d3d11(AVHWFramesContext
*hwfc, AVFrame *dst,
.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,
- },
- };
+ /* One copy per plane. The frame may hold one image per plane or a single
+ * multi-planar image addressed through plane aspects, so the aspect comes
+ * from the frame and the image index is clamped to what it has. */
+ for (int i = 0; i < planes; i++) {
+ const int img_idx = FFMIN(i, nb_images - 1);
+ VkImageAspectFlags aspect = ff_vk_aspect_flag(hwf, i);
+ uint32_t p_w, p_h;
+ VkImageCopy region;
+
+ get_plane_wh(&p_w, &p_h, hwfc->sw_format, hwfc->width, hwfc->height,
i);
+
+ region = (VkImageCopy) {
+ .srcSubresource = { .layerCount = 1 },
+ .dstSubresource = { .layerCount = 1 },
+ .extent = { p_w, p_h, 1 },
+ };
+ if (upload) {
+ region.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
+ region.dstSubresource.aspectMask = aspect;
+ vk->CmdCopyImage(cmd_buf, imp_img[i],
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
+ hwf_vk->img[img_idx],
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
+ 1, ®ion);
+ } else {
+ region.srcSubresource.aspectMask = aspect;
+ region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
+ vk->CmdCopyImage(cmd_buf, hwf_vk->img[img_idx],
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
+ imp_img[i], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
+ 1, ®ion);
+ }
+ }
+
+ /* Release the imported images back to the external owner, which makes the
+ * writes available to D3D11 when this was a download. */
+ nb_img_bar = 0;
+ for (int i = 0; i < planes; i++) {
+ img_bar[nb_img_bar++] = (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 = imp_img[i],
+ .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,
+ .imageMemoryBarrierCount = nb_img_bar,
});
err = ff_vk_exec_submit(&p->vkctx, exec);
@@ -5687,15 +5804,27 @@ static int vulkan_transfer_d3d11(AVHWFramesContext
*hwfc, AVFrame *dst,
* the transfer completes before it returns, like the system memory
* paths do. */
ff_vk_exec_wait(&p->vkctx, exec);
+ /* Only now do the plane textures hold the frame, so a download
+ * finishes by assembling them into the destination texture. */
+ if (bridged && !upload) {
+ d3d_hw->lock(d3d_hw->lock_ctx);
+ ff_d3d11va_bridge_run(sync->bridge, d3d_hw->device_context,
+ (ID3D11Resource *)d3df->data[0],
+ (UINT)(intptr_t)d3df->data[1], 0);
+ d3d_hw->unlock(d3d_hw->lock_ctx);
+ }
}
-
- d3d11_import_free(hwfc, &im);
-
- return err;
+ goto end;
fail:
ff_vk_exec_discard(&p->vkctx, exec);
- d3d11_import_free(hwfc, &im);
+
+end:
+ if (bridged)
+ pthread_mutex_unlock(&sync->bridge_lock);
+ else
+ d3d11_import_free(hwfc, &im);
+
return err;
}
--
2.45.1.windows.1
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]