使用 `vk::raii::ImageView` 与 `std::vector`

huangapple go评论67阅读模式
英文:

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

问题

我正在尝试使用此函数将std::vector<VkImage>映射到std::vector<vk::raii::ImageView>。映射方法来自它们的示例,所以也许示例是错误的,我不知道。

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;
}

我遇到了这个错误,它展开为以下内容:

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创建图像视图?

英文:

I'm trying to map std::vector&lt;VkImage&gt; to std::vector&lt;vk::raii::ImageView&gt; with this function. The mapping approach was taken from their samples, so maybe samples are wrong, idk.


std::vector&lt;vk::raii::ImageView&gt; Render::createImageViews(
    std::vector&lt;VkImage&gt;&amp; images,
    vk::raii::Device&amp; device
) {
    std::vector&lt;vk::raii::ImageView&gt; 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 &lt; 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&amp;&amp; ...) [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&lt;_TrivialValueType&gt;::__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&lt;_Tp&gt;&amp;) [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&lt;_Tp, _Alloc&gt;::_M_default_initialize(std::vector&lt;_Tp, _Alloc&gt;::size_type) [with _Tp = vk::raii::ImageView; _Alloc = std::allocator&lt;vk::raii::ImageView&gt;; std::vector&lt;_Tp, _Alloc&gt;::size_type = long unsigned int]’
/usr/include/c++/11/bits/stl_vector.h:512:9:   required from ‘std::vector&lt;_Tp, _Alloc&gt;::vector(std::vector&lt;_Tp, _Alloc&gt;::size_type, const allocator_type&amp;) [with _Tp = vk::raii::ImageView; _Alloc = std::allocator&lt;vk::raii::ImageView&gt;; std::vector&lt;_Tp, _Alloc&gt;::size_type = long unsigned int; std::vector&lt;_Tp, _Alloc&gt;::allocator_type = std::allocator&lt;vk::raii::ImageView&gt;]’
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&lt;void*&gt;(__p)) _Tp(std::forward&lt;_Args&gt;(__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?

答案1

得分: 2

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

不要移动 rvalue,直接赋值:

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

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

std::vectorvk::raii::ImageView views(images.size());

创建一个空的向量

std::vectorvk::raii::ImageView views;

如果需要,可以预留空间

views.reserve(images.size());

然后在循环中

views.emplace_back(device, createInfo);

英文:
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&lt;vk::raii::ImageView&gt; views(images.size());

Make an empty vector

std::vector&lt;vk::raii::ImageView&gt; views;

The space can be reserved if it's desired

views.reserve(images.size());

And later in the loop

views.emplace_back(device, createInfo);

huangapple
  • 本文由 发表于 2023年3月3日 23:20:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定