首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >导出vulkan内存分配句柄会导致设备内存不足

导出vulkan内存分配句柄会导致设备内存不足
EN

Stack Overflow用户
提问于 2019-10-08 08:47:10
回答 1查看 668关注 0票数 2

我正在尝试导出由Vulkan创建的内存分配句柄,以便将其导入OpenGL。但是,当我在vk::ExportMemoryAllocateInfopNext链中添加一个OutOfDeviceMemory时,vk::Device::allocateMemory会抛出一个OutOfDeviceMemory异常。

当我不导出句柄时,分配不会失败,但返回的句柄无效。

下面是有罪代码(基于jherico的示例:vulkan-opengl-interop-示例):

代码语言:javascript
运行
复制
// 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);
}

我的系统是:

  • Windows 7
  • Nvidia Quadro RTX 4000
  • 驱动程序版本431.02或431.94

验证层在我的实例中是存在的,但仍然是无用的,扩展VK_KHR_external_memoryVK_KHR_external_memory_win32在我的设备中是可用的,分配大小符合API限制,memoryIndexType是正确的。

我是不是做错了什么,还是我错过了一个限制?

谢谢!

编辑:

我试图将句柄导出为vk::ExternalMemoryHandleTypeFlagBits::eOpaqueWin32Kmt,并且分配工作正常。下面的代码是如何测试分配是否需要专用分配来导出句柄类型。

代码语言:javascript
运行
复制
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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-11 08:41:42

我终于得到了一个vk::ExternalMemoryHandleTypeFlagBits::eOpaqueWin32Kmt.我不需要通过Windows与它交互,只需要OpenGL。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58282968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档