Search code examples
c++vulkan

Usage of `vk::raii:ImageView` with `std::vector`


I'm trying to map std::vector<VkImage> to std::vector<vk::raii::ImageView> with this function. The mapping approach was taken from their samples, so maybe samples are wrong, 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;
}

And I'm getting this error, which unwraps into this:

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

It is the only error I have and I'm certain that it is caused by this code.

I've checked the vk::raii::ImageView definition, and it's default constructor marked as deleted.

Is it a "bug" of vulkan_raii.hpp or I supposed to use non RAII version to create image views out of VkImages?


Solution

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

    Don't move rvalue, assign it directly:

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

    This can't be done if the default constructor is deleted:

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

    Make an empty vector

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

    The space can be reserved if it's desired

    views.reserve(images.size());
    

    And later in the loop

    views.emplace_back(device, createInfo);