cuda: Load external resource interop symbols

Required for d3d12 interop

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/7480>
This commit is contained in:
Seungha Yang 2024-09-14 03:12:46 +09:00 committed by GStreamer Marge Bot
parent 577df3a02f
commit 6f92807759
5 changed files with 355 additions and 1 deletions

View file

@ -339,6 +339,60 @@ CUresult CUDAAPI CuMemPoolGetAttribute (CUmemoryPool pool,
CUmemPool_attribute attr, CUmemPool_attribute attr,
void *value); void *value);
GST_CUDA_API
CUresult CUDAAPI CuDestroyExternalMemory (CUexternalMemory extMem);
GST_CUDA_API
CUresult CUDAAPI CuDestroyExternalSemaphore (CUexternalSemaphore extSem);
/**
* CuExternalMemoryGetMappedBuffer: (skip) (attributes doc.skip=true)
*/
GST_CUDA_API
CUresult CUDAAPI CuExternalMemoryGetMappedBuffer (CUdeviceptr *devPtr,
CUexternalMemory extMem,
const CUDA_EXTERNAL_MEMORY_BUFFER_DESC *bufferDesc);
/**
* CuExternalMemoryGetMappedMipmappedArray: (skip) (attributes doc.skip=true)
*/
GST_CUDA_API
CUresult CUDAAPI CuExternalMemoryGetMappedMipmappedArray (CUmipmappedArray *mipmap,
CUexternalMemory extMem,
const CUDA_EXTERNAL_MEMORY_MIPMAPPED_ARRAY_DESC *mipmapDesc);
/**
* CuImportExternalMemory: (skip) (attributes doc.skip=true)
*/
GST_CUDA_API
CUresult CUDAAPI CuImportExternalMemory (CUexternalMemory *extMem_out,
const CUDA_EXTERNAL_MEMORY_HANDLE_DESC *memHandleDesc);
/**
* CuImportExternalSemaphore: (skip) (attributes doc.skip=true)
*/
GST_CUDA_API
CUresult CUDAAPI CuImportExternalSemaphore (CUexternalSemaphore *extSem_out,
const CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC *semHandleDesc);
/**
* CuSignalExternalSemaphoresAsync: (skip) (attributes doc.skip=true)
*/
GST_CUDA_API
CUresult CUDAAPI CuSignalExternalSemaphoresAsync (const CUexternalSemaphore *extSemArray,
const CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS *paramsArray,
unsigned int numExtSems,
CUstream stream);
/**
* CuWaitExternalSemaphoresAsync: (skip) (attributes doc.skip=true)
*/
GST_CUDA_API
CUresult CUDAAPI CuWaitExternalSemaphoresAsync (const CUexternalSemaphore *extSemArray,
const CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS *paramsArray,
unsigned int numExtSems,
CUstream stream);
/* cudaGL.h */ /* cudaGL.h */
GST_CUDA_API GST_CUDA_API
CUresult CUDAAPI CuGraphicsGLRegisterImage (CUgraphicsResource * pCudaResource, CUresult CUDAAPI CuGraphicsGLRegisterImage (CUgraphicsResource * pCudaResource,

View file

@ -56,6 +56,7 @@ enum
PROP_OS_HANDLE, PROP_OS_HANDLE,
PROP_STREAM_ORDERED_ALLOC, PROP_STREAM_ORDERED_ALLOC,
PROP_PREFER_STREAM_ORDERED_ALLLOC, PROP_PREFER_STREAM_ORDERED_ALLLOC,
PROP_EXT_INTEROP,
}; };
struct _GstCudaContextPrivate struct _GstCudaContextPrivate
@ -68,6 +69,7 @@ struct _GstCudaContextPrivate
gboolean os_handle_supported; gboolean os_handle_supported;
gboolean stream_ordered_alloc_supported; gboolean stream_ordered_alloc_supported;
gboolean prefer_stream_ordered_alloc; gboolean prefer_stream_ordered_alloc;
gboolean ext_interop_supported;
gint tex_align; gint tex_align;
@ -167,6 +169,19 @@ gst_cuda_context_class_init (GstCudaContextClass * klass)
"Prefer Stream Ordered Alloc", "Prefers stream ordered allocation", "Prefer Stream Ordered Alloc", "Prefers stream ordered allocation",
FALSE, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS))); FALSE, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
/**
* GstCudaContext:external-resource-interop:
*
* External resource interop API support
*
* Since: 1.26
*/
g_object_class_install_property (gobject_class, PROP_EXT_INTEROP,
g_param_spec_boolean ("external-resource-interop",
"External Resource Interop",
"External resource interop API support", FALSE,
(GParamFlags) (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
gst_cuda_memory_init_once (); gst_cuda_memory_init_once ();
} }
@ -233,6 +248,9 @@ gst_cuda_context_get_property (GObject * object, guint prop_id,
g_value_set_boolean (value, priv->prefer_stream_ordered_alloc); g_value_set_boolean (value, priv->prefer_stream_ordered_alloc);
g_mutex_unlock (&priv->lock); g_mutex_unlock (&priv->lock);
break; break;
case PROP_EXT_INTEROP:
g_value_set_boolean (value, priv->ext_interop_supported);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -673,6 +691,9 @@ gst_cuda_context_new_wrapped (CUcontext handler, CUdevice device)
self->priv->stream_ordered_alloc_supported = TRUE; self->priv->stream_ordered_alloc_supported = TRUE;
} }
self->priv->ext_interop_supported =
gst_cuda_external_resource_interop_symbol_loaded ();
std::lock_guard < std::mutex > lk (list_lock); std::lock_guard < std::mutex > lk (list_lock);
g_object_weak_ref (G_OBJECT (self), g_object_weak_ref (G_OBJECT (self),
(GWeakNotify) gst_cuda_context_weak_ref_notify, nullptr); (GWeakNotify) gst_cuda_context_weak_ref_notify, nullptr);

View file

@ -27,4 +27,6 @@ gboolean gst_cuda_virtual_memory_symbol_loaded (void);
gboolean gst_cuda_stream_ordered_symbol_loaded (void); gboolean gst_cuda_stream_ordered_symbol_loaded (void);
gboolean gst_cuda_external_resource_interop_symbol_loaded (void);
G_END_DECLS G_END_DECLS

View file

@ -64,6 +64,7 @@ typedef struct _GstNvCodecCudaVTable
gboolean loaded; gboolean loaded;
gboolean have_virtual_alloc; gboolean have_virtual_alloc;
gboolean have_stream_ordered_alloc; gboolean have_stream_ordered_alloc;
gboolean have_ext_interop;
CUresult (CUDAAPI * CuInit) (unsigned int Flags); CUresult (CUDAAPI * CuInit) (unsigned int Flags);
CUresult (CUDAAPI * CuGetErrorName) (CUresult error, const char **pStr); CUresult (CUDAAPI * CuGetErrorName) (CUresult error, const char **pStr);
@ -226,6 +227,29 @@ typedef struct _GstNvCodecCudaVTable
CUmemPool_attribute attr, void *value); CUmemPool_attribute attr, void *value);
CUresult (CUDAAPI * CuMemPoolGetAttribute) (CUmemoryPool pool, CUresult (CUDAAPI * CuMemPoolGetAttribute) (CUmemoryPool pool,
CUmemPool_attribute attr, void *value); CUmemPool_attribute attr, void *value);
CUresult (CUDAAPI * CuDestroyExternalMemory) (CUexternalMemory extMem);
CUresult (CUDAAPI * CuDestroyExternalSemaphore) (CUexternalSemaphore extSem);
CUresult (CUDAAPI * CuExternalMemoryGetMappedBuffer) (CUdeviceptr *devPtr,
CUexternalMemory extMem,
const CUDA_EXTERNAL_MEMORY_BUFFER_DESC *bufferDesc);
CUresult (CUDAAPI * CuExternalMemoryGetMappedMipmappedArray)
(CUmipmappedArray *mipmap, CUexternalMemory extMem,
const CUDA_EXTERNAL_MEMORY_MIPMAPPED_ARRAY_DESC *mipmapDesc);
CUresult (CUDAAPI * CuImportExternalMemory) (CUexternalMemory *extMem_out,
const CUDA_EXTERNAL_MEMORY_HANDLE_DESC *memHandleDesc);
CUresult (CUDAAPI * CuImportExternalSemaphore)
(CUexternalSemaphore *extSem_out,
const CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC *semHandleDesc);
CUresult (CUDAAPI * CuSignalExternalSemaphoresAsync)
(const CUexternalSemaphore *extSemArray,
const CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS *paramsArray,
unsigned int numExtSems, CUstream stream);
CUresult (CUDAAPI * CuWaitExternalSemaphoresAsync)
(const CUexternalSemaphore *extSemArray,
const CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS *paramsArray,
unsigned int numExtSems, CUstream stream);
} GstNvCodecCudaVTable; } GstNvCodecCudaVTable;
/* *INDENT-ON* */ /* *INDENT-ON* */
@ -278,6 +302,29 @@ gst_cuda_load_stream_ordered_alloc_symbols (GModule * module)
vtable->have_stream_ordered_alloc = TRUE; vtable->have_stream_ordered_alloc = TRUE;
} }
static void
gst_cuda_load_ext_interop_symbols (GModule * module)
{
GstNvCodecCudaVTable *vtable = &gst_cuda_vtable;
LOAD_OPTIONAL_SYMBOL (cuDestroyExternalMemory, CuDestroyExternalMemory);
LOAD_OPTIONAL_SYMBOL (cuDestroyExternalSemaphore, CuDestroyExternalSemaphore);
LOAD_OPTIONAL_SYMBOL (cuExternalMemoryGetMappedBuffer,
CuExternalMemoryGetMappedBuffer);
LOAD_OPTIONAL_SYMBOL (cuExternalMemoryGetMappedMipmappedArray,
CuExternalMemoryGetMappedMipmappedArray);
LOAD_OPTIONAL_SYMBOL (cuImportExternalMemory, CuImportExternalMemory);
LOAD_OPTIONAL_SYMBOL (cuImportExternalSemaphore, CuImportExternalSemaphore);
LOAD_OPTIONAL_SYMBOL (cuSignalExternalSemaphoresAsync,
CuSignalExternalSemaphoresAsync);
LOAD_OPTIONAL_SYMBOL (cuWaitExternalSemaphoresAsync,
CuWaitExternalSemaphoresAsync);
GST_INFO ("External resource interop symbols are loaded");
vtable->have_ext_interop = TRUE;
}
static void static void
gst_cuda_load_library_once_func (void) gst_cuda_load_library_once_func (void)
{ {
@ -387,6 +434,7 @@ gst_cuda_load_library_once_func (void)
gst_cuda_load_optional_symbols (module); gst_cuda_load_optional_symbols (module);
gst_cuda_load_stream_ordered_alloc_symbols (module); gst_cuda_load_stream_ordered_alloc_symbols (module);
gst_cuda_load_ext_interop_symbols (module);
} }
/** /**
@ -424,6 +472,14 @@ gst_cuda_stream_ordered_symbol_loaded (void)
return gst_cuda_vtable.have_stream_ordered_alloc; return gst_cuda_vtable.have_stream_ordered_alloc;
} }
gboolean
gst_cuda_external_resource_interop_symbol_loaded (void)
{
gst_cuda_load_library ();
return gst_cuda_vtable.have_ext_interop;
}
CUresult CUDAAPI CUresult CUDAAPI
CuInit (unsigned int Flags) CuInit (unsigned int Flags)
{ {
@ -1073,6 +1129,91 @@ CuMemPoolGetAttribute (CUmemoryPool pool, CUmemPool_attribute attr, void *value)
return gst_cuda_vtable.CuMemPoolGetAttribute (pool, attr, value); return gst_cuda_vtable.CuMemPoolGetAttribute (pool, attr, value);
} }
CUresult CUDAAPI
CuDestroyExternalMemory (CUexternalMemory extMem)
{
if (!gst_cuda_vtable.CuDestroyExternalMemory)
return CUDA_ERROR_NOT_SUPPORTED;
return gst_cuda_vtable.CuDestroyExternalMemory (extMem);
}
CUresult CUDAAPI
CuDestroyExternalSemaphore (CUexternalSemaphore extSem)
{
if (!gst_cuda_vtable.CuDestroyExternalSemaphore)
return CUDA_ERROR_NOT_SUPPORTED;
return gst_cuda_vtable.CuDestroyExternalSemaphore (extSem);
}
CUresult CUDAAPI
CuExternalMemoryGetMappedBuffer (CUdeviceptr * devPtr, CUexternalMemory extMem,
const CUDA_EXTERNAL_MEMORY_BUFFER_DESC * bufferDesc)
{
if (!gst_cuda_vtable.CuExternalMemoryGetMappedBuffer)
return CUDA_ERROR_NOT_SUPPORTED;
return gst_cuda_vtable.CuExternalMemoryGetMappedBuffer (devPtr, extMem,
bufferDesc);
}
CUresult CUDAAPI
CuExternalMemoryGetMappedMipmappedArray (CUmipmappedArray * mipmap,
CUexternalMemory extMem,
const CUDA_EXTERNAL_MEMORY_MIPMAPPED_ARRAY_DESC * mipmapDesc)
{
if (!gst_cuda_vtable.CuExternalMemoryGetMappedMipmappedArray)
return CUDA_ERROR_NOT_SUPPORTED;
return gst_cuda_vtable.CuExternalMemoryGetMappedMipmappedArray (mipmap,
extMem, mipmapDesc);
}
CUresult CUDAAPI
CuImportExternalMemory (CUexternalMemory * extMem_out,
const CUDA_EXTERNAL_MEMORY_HANDLE_DESC * memHandleDesc)
{
if (!gst_cuda_vtable.CuImportExternalMemory)
return CUDA_ERROR_NOT_SUPPORTED;
return gst_cuda_vtable.CuImportExternalMemory (extMem_out, memHandleDesc);
}
CUresult CUDAAPI
CuImportExternalSemaphore (CUexternalSemaphore * extSem_out,
const CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC * semHandleDesc)
{
if (!gst_cuda_vtable.CuImportExternalSemaphore)
return CUDA_ERROR_NOT_SUPPORTED;
return gst_cuda_vtable.CuImportExternalSemaphore (extSem_out, semHandleDesc);
}
CUresult CUDAAPI
CuSignalExternalSemaphoresAsync (const CUexternalSemaphore * extSemArray,
const CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS * paramsArray,
unsigned int numExtSems, CUstream stream)
{
if (!gst_cuda_vtable.CuSignalExternalSemaphoresAsync)
return CUDA_ERROR_NOT_SUPPORTED;
return gst_cuda_vtable.CuSignalExternalSemaphoresAsync (extSemArray,
paramsArray, numExtSems, stream);
}
CUresult CUDAAPI
CuWaitExternalSemaphoresAsync (const CUexternalSemaphore * extSemArray,
const CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS * paramsArray,
unsigned int numExtSems, CUstream stream)
{
if (!gst_cuda_vtable.CuWaitExternalSemaphoresAsync)
return CUDA_ERROR_NOT_SUPPORTED;
return gst_cuda_vtable.CuWaitExternalSemaphoresAsync (extSemArray,
paramsArray, numExtSems, stream);
}
/* cudaGL.h */ /* cudaGL.h */
CUresult CUDAAPI CUresult CUDAAPI
CuGraphicsGLRegisterImage (CUgraphicsResource * pCudaResource, CuGraphicsGLRegisterImage (CUgraphicsResource * pCudaResource,

View file

@ -32,6 +32,8 @@ typedef gpointer CUfunction;
typedef gpointer CUmipmappedArray; typedef gpointer CUmipmappedArray;
typedef gpointer CUevent; typedef gpointer CUevent;
typedef gpointer CUmemoryPool; typedef gpointer CUmemoryPool;
typedef gpointer CUexternalMemory;
typedef gpointer CUexternalSemaphore;
typedef guint64 CUtexObject; typedef guint64 CUtexObject;
typedef guintptr CUdeviceptr; typedef guintptr CUdeviceptr;
@ -191,7 +193,7 @@ typedef struct
gsize pitchInBytes; gsize pitchInBytes;
} pitch2D; } pitch2D;
struct { struct {
gint reserved[32]; gint reserved[32];
} reserved; } reserved;
} res; } res;
@ -316,6 +318,140 @@ typedef enum
CU_MEMPOOL_ATTR_USED_MEM_HIGH, CU_MEMPOOL_ATTR_USED_MEM_HIGH,
} CUmemPool_attribute; } CUmemPool_attribute;
typedef struct
{
unsigned long long offset;
unsigned long long size;
unsigned int flags;
unsigned int reserved[16];
} CUDA_EXTERNAL_MEMORY_BUFFER_DESC;
typedef enum
{
CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD = 1,
CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32 = 2,
CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT = 3,
CU_EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_HEAP = 4,
CU_EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_RESOURCE = 5,
CU_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_RESOURCE = 6,
CU_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_RESOURCE_KMT = 7,
CU_EXTERNAL_MEMORY_HANDLE_TYPE_NVSCIBUF = 8
} CUexternalMemoryHandleType;
/**
* CUDA_EXTERNAL_MEMORY_HANDLE_DESC: (skip) (attributes doc.skip=true)
*/
typedef struct
{
CUexternalMemoryHandleType type;
union {
int fd;
struct {
void *handle;
const void *name;
} win32;
const void *nvSciBufObject;
} handle;
unsigned long long size;
unsigned int flags;
unsigned int reserved[16];
} CUDA_EXTERNAL_MEMORY_HANDLE_DESC;
typedef enum
{
CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD = 1,
CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32 = 2,
CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT = 3,
CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE = 4,
CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_FENCE = 5,
CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_NVSCISYNC = 6,
CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_KEYED_MUTEX = 7,
CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_KEYED_MUTEX_KMT = 8,
CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_FD = 9,
CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_WIN32 = 10
} CUexternalSemaphoreHandleType;
/**
* CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC: (skip) (attributes doc.skip=true)
*/
typedef struct
{
CUexternalSemaphoreHandleType type;
union {
int fd;
struct {
void *handle;
const void *name;
} win32;
const void* nvSciSyncObj;
} handle;
unsigned int flags;
unsigned int reserved[16];
} CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC;
/**
* CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS: (skip) (attributes doc.skip=true)
*/
typedef struct
{
struct {
struct {
unsigned long long value;
} fence;
union {
void *fence;
unsigned long long reserved;
} nvSciSync;
struct {
unsigned long long key;
} keyedMutex;
unsigned int reserved[12];
} params;
unsigned int flags;
unsigned int reserved[16];
} CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS;
/**
* CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS: (skip) (attributes doc.skip=true)
*/
typedef struct
{
struct {
struct {
unsigned long long value;
} fence;
union {
void *fence;
unsigned long long reserved;
} nvSciSync;
struct {
unsigned long long key;
unsigned int timeoutMs;
} keyedMutex;
unsigned int reserved[10];
} params;
unsigned int flags;
unsigned int reserved[16];
} CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS;
typedef struct
{
size_t Width;
size_t Height;
size_t Depth;
CUarray_format Format;
unsigned int NumChannels;
unsigned int Flags;
} CUDA_ARRAY3D_DESCRIPTOR;
typedef struct
{
unsigned long long offset;
CUDA_ARRAY3D_DESCRIPTOR arrayDesc;
unsigned int numLevels;
unsigned int reserved[16];
} CUDA_EXTERNAL_MEMORY_MIPMAPPED_ARRAY_DESC;
#define CUDA_VERSION 10000 #define CUDA_VERSION 10000
#ifdef _WIN32 #ifdef _WIN32