英文:
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<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 VkImage
s?
答案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<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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论