c++ 将'vk::raii:ImageView'与'std::vector'一起使用

w8f9ii69  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(163)

我试着用这个函数把std::vector<VkImage>Map到std::vector<vk::raii::ImageView>,Map的方法是从他们的示例中得到的,所以可能示例是错误的,idk。

std::vector<vk::raii::ImageView> Render::createImageViews(
    std::vector<VkImage>& images,
    vk::raii::Device& device
) {
    std::vector<vk::raii::ImageView> views(images.size());

    vk::ImageViewCreateInfo createInfo(
        {},
        {},
        vk::ImageViewType::e2D,
        vk::Format::eB8G8R8A8Srgb,
        {},
        { vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 }
    );

    for (size_t i = 0; i < images.size(); i++) {
        createInfo.image = images[i];
        views[i] = std::move(
            vk::raii::ImageView(device, createInfo)
        );
    }

    return views;
}

然后我得到了这个错误,它会变成这样:

make RUN=1
mkdir -p build/debug/obj
g++ -c -std=c++17 -Wall -pedantic -g -D ENABLE_VK_VALIDATION_LAYERS -D ENABLE_DEBUG_LOG -D VULKAN_HPP_NO_SMART_HANDLE -D VULKAN_HPP_NO_SPACESHIP_OPERATOR -D VULKAN_HPP_NO_TO_STRING -D VULKAN_HPP_NO_SETTERS src/render.cpp -o build/debug/obj/render.o
In file included from /usr/include/c++/11/optional:44,
                 from src/render.hpp:3,
                 from src/render.cpp:1:
/usr/include/c++/11/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_Tp*, _Args&& ...) [with _Tp = vk::raii::ImageView; _Args = {}]’:
/usr/include/c++/11/bits/stl_uninitialized.h:579:18:   required from ‘static _ForwardIterator std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = vk::raii::ImageView*; _Size = long unsigned int; bool _TrivialValueType = false]’
/usr/include/c++/11/bits/stl_uninitialized.h:640:20:   required from ‘_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = vk::raii::ImageView*; _Size = long unsigned int]’
/usr/include/c++/11/bits/stl_uninitialized.h:704:44:   required from ‘_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = vk::raii::ImageView*; _Size = long unsigned int; _Tp = vk::raii::ImageView]’
/usr/include/c++/11/bits/stl_vector.h:1606:36:   required from ‘void std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = vk::raii::ImageView; _Alloc = std::allocator<vk::raii::ImageView>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/11/bits/stl_vector.h:512:9:   required from ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = vk::raii::ImageView; _Alloc = std::allocator<vk::raii::ImageView>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<vk::raii::ImageView>]’
src/render.cpp:121:57:   required from here
/usr/include/c++/11/bits/stl_construct.h:119:7: error: use of deleted function ‘vk::raii::ImageView::ImageView()’
  119 |       ::new(static_cast<void*>(__p)) _Tp(std::forward<_Args>(__args)...);
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from src/render.hpp:12,
                 from src/render.cpp:1:
/usr/include/vulkan/vulkan_raii.hpp:7501:7: note: declared here
 7501 |       ImageView()                    = delete;
      |       ^~~~~~~~~
make: *** [Makefile:44: build/debug/obj/render.o] Error 1

这是我唯一的错误,我敢肯定,这是由这个代码造成的。
我已经检查了vk::raii::ImageView的定义,它的默认构造函数被标记为已删除。
这是vulkan_raii.hpp的一个“bug”还是我应该使用非RAII版本来创建VkImage的图像视图?

bcs8qyzn

bcs8qyzn1#

views[i] = std::move(
        vk::raii::ImageView(device, createInfo)
    );

不移动右值,直接赋值:

views[i] = vk::raii::ImageView(device, createInfo);

如果删除默认构造函数,则无法执行此操作:

std::vector<vk::raii::ImageView> views(images.size());

创建空矢量

std::vector<vk::raii::ImageView> views;

如果需要,可以保留空间

views.reserve(images.size());

在循环的后期

views.emplace_back(device, createInfo);

相关问题