无法将信号量从 Vulkan 导入 Cuda

Fail to import semaphore from Vulkan to Cuda

我正在尝试导入 Vulkan 信号量以供 CUDA 在 windows 平台上使用,但总是出现 cudaErrorInvalidValue 错误。我不知道是什么原因造成的。

考虑到我已经有一个 Vulkan 信号量对象的句柄,Cuda 端位于 DLL1,如下所示:

 void test_createCudaImportSemaphor(HANDLE handle)
{
    cudaExternalSemaphoreHandleDesc externalSemaphoreHandleDesc;
    externalSemaphoreHandleDesc.flags = 0;
    externalSemaphoreHandleDesc.type = cudaExternalSemaphoreHandleTypeOpaqueWin32;
    externalSemaphoreHandleDesc.handle.win32.handle = handle;

    cudaExternalSemaphore_t cudaExternalSemaphore;
    cudaError_t err = cudaImportExternalSemaphore(&cudaExternalSemaphore, &externalSemaphoreHandleDesc);
    ////////////////////////////////////////////////
    // err= 1 (cudaErrorInvalidValue )    why ????
    ////////////////////////////////////////////////

    // destory
    err = cudaDestroyExternalSemaphore(cudaExternalSemaphore);
}

测试 vulkan 信号量的创建和调用 test_createCudaImportSemaphor 以导入到 cuda 是在 DLL2 中,如下所示:

void test_exportVulkanSemphoreToCuda(vk::Device device)
{
    // create semaphore that can be exported (windows platform)
    vk::ExportSemaphoreWin32HandleInfoKHR exportSemaphoreWin32HandleInfoKHR;
    WindowsSecurityAttributes cWinSecurityAttributes; // taken from cuda samples 02_graphics
    exportSemaphoreWin32HandleInfoKHR.pAttributes = &cWinSecurityAttributes;
    exportSemaphoreWin32HandleInfoKHR.dwAccess = DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE;

    vk::ExportSemaphoreCreateInfo exportSemaphoreCreateInfo;
    exportSemaphoreCreateInfo.pNext = &exportSemaphoreWin32HandleInfoKHR;
    exportSemaphoreCreateInfo.handleTypes = vk::ExternalSemaphoreHandleTypeFlagBits::eOpaqueWin32;

    vk::SemaphoreCreateInfo semaphoreCreateInfo;
    semaphoreCreateInfo.pNext = &exportSemaphoreCreateInfo;
    vk::UniqueSemaphore upSemaphore = device.createSemaphoreUnique(semaphoreCreateInfo);

    // get semaphore windows handle 
    vk::SemaphoreGetWin32HandleInfoKHR semaphoreGetWin32HandleInfoKHR = {};
    semaphoreGetWin32HandleInfoKHR.semaphore = upSemaphore.get();
    semaphoreGetWin32HandleInfoKHR.handleType = vk::ExternalSemaphoreHandleTypeFlagBits::eOpaqueWin32;
    HANDLE handle = device.getSemaphoreWin32HandleKHR(semaphoreGetWin32HandleInfoKHR);

    // Now try to import sempahore to cuda
    test_createCudaImportSemaphor(handle);

    CloseHandle(handle);
}

正如@talonmies 评论的那样,事实上,结构 cudaExternalSemaphoreHandleDesc 实际上没有用显示的代码完全初始化。将结构体初始化为零时问题解决:

 cudaExternalSemaphoreHandleDesc externalSemaphoreHandleDesc = {};