什么是使用福尔康VkImage作为CUDA cuArray的正确方法是什么?
我一直在试图遵循一些例子,但是我得到一个呼叫CUDA_ERROR_INVALID_VALUE cuExternalMemoryGetMappedMipmappedArray()
为了提供一个有序的方式的信息。
我使用CUDA 10.1
基本代码来自https://github.com/SaschaWillems/Vulkan ,特别是我用01 -福尔康齿轮演示,与saveScreenshot方法丰富了09 -捕获截图
相反,快照图像保存到一个文件中,我将发送快照图像到CUDA作为CUarray。
我已经启用了以下实例和设备扩展:
std::vector<const char*> instanceExtensions = {
VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME };
std::vector<const char*> deviceExtensions = { VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME };
我有一个VkImage,创建如下:
// Create the linear tiled destination image to copy to and to read the memory from
VkImageCreateInfo imageCreateCI(vks::initializers::imageCreateInfo());
imageCreateCI.imageType = VK_IMAGE_TYPE_2D;
// Note that vkCmdBlitImage (if supported) will also do format conversions if the swapchain color format would differ
imageCreateCI.format = VK_FORMAT_R8G8B8A8_UNORM;
imageCreateCI.extent.width = width;
imageCreateCI.extent.height = height;
imageCreateCI.extent.depth = 1;
imageCreateCI.arrayLayers = 1;
imageCreateCI.mipLevels = 1;
imageCreateCI.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageCreateCI.samples = VK_SAMPLE_COUNT_1_BIT;
imageCreateCI.tiling = VK_IMAGE_TILING_LINEAR;
imageCreateCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageCreateCI.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
VkExternalMemoryImageCreateInfoKHR extImageCreateInfo = {};
/*
* Indicate that the memory backing this image will be exported in an
* fd. In some implementations, this may affect the call to
* GetImageMemoryRequirements() with this image.
*/
extImageCreateInfo.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO_KHR;
extImageCreateInfo.handleTypes |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
imageCreateCI.pNext = &extImageCreateInfo;
// Create the image
VkImage dstImage;
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateCI, nullptr, &dstImage));
// Create memory to back up the image
VkMemoryRequirements memRequirements;
VkMemoryAllocateInfo memAllocInfo(vks::initializers::memoryAllocateInfo());
VkDeviceMemory dstImageMemory;
vkGetImageMemoryRequirements(device, dstImage, &memRequirements);
memAllocInfo.allocationSize = memRequirements.size;
// Memory must be host visible to copy from
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
VkExportMemoryAllocateInfoKHR exportInfo = {};
exportInfo.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO_KHR;
exportInfo.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
memAllocInfo.pNext = &exportInfo;
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &dstImageMemory));
VK_CHECK_RESULT(vkBindImageMemory(device, dstImage, dstImageMemory, 0));
从那里,我会:
获取福尔康内存处理程序:
int CuEncoderImpl::getVulkanMemoryHandle(VkDevice device,
VkDeviceMemory memory) {
// Get handle to memory of the VkImage
int fd = -1;
VkMemoryGetFdInfoKHR fdInfo = { };
fdInfo.sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR;
fdInfo.memory = memory;
fdInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
auto func = (PFN_vkGetMemoryFdKHR) vkGetDeviceProcAddr(device,
"vkGetMemoryFdKHR");
if (!func) {
printf("Failed to locate function vkGetMemoryFdKHR\n");
return -1;
}
VkResult r = func(device, &fdInfo, &fd);
if (r != VK_SUCCESS) {
printf("Failed executing vkGetMemoryFdKHR [%d]\n", r);
return -1;
}
return fd;
}
导入内存:
CUDA_EXTERNAL_MEMORY_HANDLE_DESC memDesc = { };
memDesc.type = CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD;
memDesc.handle.fd = getVulkanMemoryHandle(device, memory);
memDesc.size = extent.width*extent.height*4;
CUDA_DRVAPI_CALL(cuImportExternalMemory(&externalMem, &memDesc));
并映射内存:这是它失败的步骤。
CUarray CuEncoderImpl::getCUDAArrayFromExternalMemory(const VkExtent3D &extent,const CUexternalMemory &m_extMem) {
CUmipmappedArray m_mipmapArray;
CUresult result = CUDA_SUCCESS;
CUarray array;
CUDA_ARRAY3D_DESCRIPTOR arrayDesc = { };
arrayDesc.Width = extent.width;
arrayDesc.Height = extent.height;
arrayDesc.Depth = 0;
arrayDesc.Format = CU_AD_FORMAT_UNSIGNED_INT32;
arrayDesc.NumChannels = 4;
arrayDesc.Flags = CUDA_ARRAY3D_SURFACE_LDST;
CUDA_EXTERNAL_MEMORY_MIPMAPPED_ARRAY_DESC mipmapArrayDesc = { };
mipmapArrayDesc.arrayDesc = arrayDesc;
mipmapArrayDesc.numLevels = 1;
mipmapArrayDesc.offset = 0;
CUDA_DRVAPI_CALL(cuExternalMemoryGetMappedMipmappedArray(&m_mipmapArray, m_extMem, &mipmapArrayDesc));
CUDA_DRVAPI_CALL(cuMipmappedArrayGetLevel(&array, m_mipmapArray, 0));
return array;
}
我一直在试图参数的多种组合,但到目前为止还没有。 错误指向一个无效的参数,但我不知道如何找到什么是错的。
只有干过的事情是映射福尔康图像存储到主机缓冲,然后将其复制到CUDA数组...但我想这是昂贵的,我想如果可能的话,以避免它。