Vulkan-hpp:无法毫无例外地分配命令缓冲区

Vulkan-hpp: cant allocate command buffers with no exceptions

首先,我用独特的句柄在 vulkan-hpp 中重写了 Vulkan 教程三角形。关闭程序时,我得到以下断言: Assertion: m_dispatch && m_owner 查看代码,我发现启用 VULKAN_HPP_NO_EXCEPTIONS 会修复它,但现在我收到此错误:

vk::UniqueHandle<vk::CommandBuffer,vk::DispatchLoaderStatic> &vk::UniqueHandle<vk::CommandBuffer,vk::DispatchLoaderStatic>::operator =(const vk::UniqueHandle<vk::CommandBuffer,vk::DispatchLoaderStatic> &)': attempting to reference a deleted function

这是我的代码:

std::tie(res, commandBuffers) = device->allocateCommandBuffersUnique(allocInfo);

正确的使用方法是什么?

device->allocateCommandBuffersUnique();

这似乎对我有用:

#include <iostream>
#include <utility>
#include <vulkan/vulkan.hpp>

int main() try{
    const auto instance = vk::createInstanceUnique( {} );
    const auto physical_devices = instance->enumeratePhysicalDevices();

    const uint32_t q_family = 0;
    const uint32_t q_count = 1;
    const float q_prio[] = {1.0f};
    const vk::DeviceQueueCreateInfo queue_info( vk::DeviceQueueCreateFlags(), q_family, q_count, q_prio );
    const vk::DeviceCreateInfo device_info( vk::DeviceCreateFlags(), 1, &queue_info );
    const auto device = physical_devices[0].createDeviceUnique( device_info );

    const vk::CommandPoolCreateInfo cmd_pool_info( vk::CommandPoolCreateFlags(), q_family );
    const auto command_pool = device->createCommandPoolUnique( cmd_pool_info );

    const vk::CommandBufferAllocateInfo alloc_info( command_pool.get(), vk::CommandBufferLevel::ePrimary, 1 );
    const auto cmd_buffers = device->allocateCommandBuffersUnique( alloc_info );
}
catch ( ... ){
    std::cout << "error\n";
}

VULKAN_HPP_NO_EXCEPTIONS 环境中,结果只是 valueresultstruct。所以:

    const vk::CommandBufferAllocateInfo alloc_info( command_pool.value.get(), vk::CommandBufferLevel::ePrimary, 1 );
    const auto cmd_buffers = device.value->allocateCommandBuffersUnique( alloc_info );
    if( cmd_buffers.result != vk::Result::eSuccess ) panic();

如果您想使用 std::tie,通常这应该有效:

vk::Result res;
vk::UniqueInstance instance;
std::tie(res, instance) = vk::createInstanceUnique( {} ).asTuple();

但是 allocateCommandBuffersUnique(可能还有所有 std::vector 类型)似乎存在一些问题。我已经在 Vulkan-Hpp repo 报告了它。

虽然如果您可以访问 C++17,那么 结构化绑定 似乎可以工作:

    const vk::CommandBufferAllocateInfo alloc_info( command_pool.value.get(), vk::CommandBufferLevel::ePrimary, 1 );
    auto [res, cmd_buffers] = device.value->allocateCommandBuffersUnique( alloc_info );
    if( res != vk::Result::eSuccess ) panic();

最近有一些style change at Vulkan-Hpp. They now suggest using vulkan_raii.hpp instead, though it has not yet bubbled to the Vulkan SDK. To get to the official samples using Create*Unique you have to go to an older version of the repo