我正在尝试导出由Vulkan创建的内存分配句柄,以便将其导入OpenGL。但是,当我在vk::ExportMemoryAllocateInfo
的pNext
链中添加一个OutOfDeviceMemory
时,vk::Device::allocateMemory
会抛出一个OutOfDeviceMemory
异常。
当我不导出句柄时,分配不会失败,但返回的句柄无效。
下面是有罪代码(基于jherico的示例:vulkan-opengl-interop-示例):
// The physical device and the device are correct
// requirements are for a RGBA image of 1024x1024 pixels
// memory properties is just vk::MemoryPropertyFlagBits::eDeviceLocal
void IMemoryObject::Allocate(vk::PhysicalDevice physicalDevice, vk::Device device, const vk::MemoryRequirements& requirements, vk::MemoryPropertyFlags properties)
{
unsigned int memoryTypeIndex = 0;
bool memoryIndexTypeFound = false;
vk::PhysicalDeviceMemoryProperties memoryProperties = physicalDevice.getMemoryProperties();
for (unsigned int i = 0; i < memoryProperties.memoryTypeCount && !memoryIndexTypeFound; i++)
{
vk::MemoryType memoryType = memoryProperties.memoryTypes[i];
if (requirements.memoryTypeBits & 1 << i && (memoryType.propertyFlags & properties) == properties)
{
memoryTypeIndex = i;
memoryIndexTypeFound = true;
}
}
if (!memoryIndexTypeFound)
throw std::exception();
vk::ExportMemoryAllocateInfo exportAllocInfo;
exportAllocInfo.setHandleTypes(vk::ExternalMemoryHandleTypeFlagBits::eOpaqueWin32);
vk::MemoryAllocateInfo allocateInfo;
allocateInfo.setPNext(&exportAllocInfo); // Remove this line and the allocation won't fail
allocateInfo.setAllocationSize(requirements.size);
allocateInfo.setMemoryTypeIndex(memoryTypeIndex);
deviceMemory_ = device.allocateMemoryUnique(allocateInfo);
// Call VkBindBufferMemory or VkBindImageMemory, never reached anyway when allocateInfo.pNext == &exportAllocInfo;
BindMemory(*deviceMemory_, 0);
}
我的系统是:
验证层在我的实例中是存在的,但仍然是无用的,扩展VK_KHR_external_memory
和VK_KHR_external_memory_win32
在我的设备中是可用的,分配大小符合API限制,memoryIndexType是正确的。
我是不是做错了什么,还是我错过了一个限制?
谢谢!
编辑:
我试图将句柄导出为vk::ExternalMemoryHandleTypeFlagBits::eOpaqueWin32Kmt
,并且分配工作正常。下面的代码是如何测试分配是否需要专用分配来导出句柄类型。
bool RequireDedicatedAllocation(vk::PhysicalDevice physicalDevice, const vk::ImageCreateInfo& createInfo, vk::ExternalMemoryHandleTypeFlagBits handleType)
{
vk::PhysicalDeviceExternalImageFormatInfo externalImageFormatInfo;
externalImageFormatInfo.setHandleType(handleType);
vk::PhysicalDeviceImageFormatInfo2 imageFormatInfo;
imageFormatInfo.setUsage(createInfo.usage)
.setFormat(createInfo.format)
.setTiling(createInfo.tiling)
.setType(createInfo.imageType)
.setPNext(&externalImageFormatInfo);
vk::StructureChain<vk::ImageFormatProperties2, vk::ExternalImageFormatProperties> imageFormatPropertiesChain = physicalDevice.getImageFormatProperties2<vk::ImageFormatProperties2, vk::ExternalImageFormatProperties>(imageFormatInfo);
vk::ExternalImageFormatProperties externalImageProperties = imageFormatPropertiesChain.get<vk::ExternalImageFormatProperties>();
return static_cast<bool>(externalImageProperties.externalMemoryProperties.externalMemoryFeatures & vk::ExternalMemoryFeatureFlagBits::eDedicatedOnly);
}
在我的系统上,它用ErrorFormatNotSupported
抛出一个vk::PhysicalDevice::getImageFormatProperties2
错误(在vk::PhysicalDevice::getImageFormatProperties2
上),用vk::ExternalMemoryHandleTypeFlagBits::eOpaqueWin32Kmt
返回false。
发布于 2019-10-11 08:41:42
我终于得到了一个vk::ExternalMemoryHandleTypeFlagBits::eOpaqueWin32Kmt.我不需要通过Windows与它交互,只需要OpenGL。
https://stackoverflow.com/questions/58282968
复制相似问题