17#include <vulkan/vulkan.hpp>
36 if (res != VK_SUCCESS) \
37 log::engine::critical("Vulkan error: " #x); \
45inline std::expected<void, std::string>
47 const vk::Queue&
queue,
49 const std::function<
void(
const vk::CommandBuffer&)>&
action) {
51 .level = vk::CommandBufferLevel::ePrimary,
52 .commandBufferCount = 1};
55 if (
alloc_res.result != vk::Result::eSuccess) {
56 return std::unexpected(
"Failed to allocate command buffers: " +
61 vk::CommandBufferBeginInfo
begin_info{.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit};
65 return std::unexpected(
"Failed to begin cmdBuffer: " + vk::to_string(
res));
72 return std::unexpected(
"Failed to end cmdbuffer: " + vk::to_string(
res));
76 if (
fence_res.result != vk::Result::eSuccess) {
78 return std::unexpected(
"Failed to create fence: " + vk::to_string(
fence_res.result));
86 return std::unexpected(
"Failed to submit to queue: " + vk::to_string(
res));
90 res != vk::Result::eSuccess) {
92 return std::unexpected(
"Failed to wait for fence: " + vk::to_string(
res));
105inline std::pair<vk::UniqueBuffer, vk::UniqueDeviceMemory>
109 vk::BufferUsageFlags
usage,
116 .sharingMode = vk::SharingMode::eExclusive};
119 "Failed to create unique buffer: ");
130 "Failed to allocate unique memory for buffer: ");
133 "Failed to bind buffer memory");
142inline std::pair<vk::UniqueImage, vk::UniqueDeviceMemory>
149 vk::ImageUsageFlags
usage,
154 .imageType = vk::ImageType::e2D,
156 .extent = vk::Extent3D{width, height, 1},
159 .samples = vk::SampleCountFlagBits::e1,
162 .sharingMode = vk::SharingMode::eExclusive
165 vk::UniqueImage image =
173 vk::UniqueDeviceMemory image_memory =
175 "Failed to allocate unique memory:");
178 "Failed to bind image memory: ");
180 return {std::move(image), std::move(image_memory)};
188 const vk::Image& image,
191 vk::BufferImageCopy
region{
193 .bufferRowLength = 0,
194 .bufferImageHeight = 0,
195 .imageSubresource = vk::ImageSubresourceLayers{.aspectMask = vk::ImageAspectFlagBits::eColor,
199 .imageOffset = vk::Offset3D{0, 0, 0},
200 .imageExtent = vk::Extent3D{width, height, 1}
211 const vk::Image& image,
221 if (
old_layout == vk::ImageLayout::eUndefined &&
222 new_layout == vk::ImageLayout::eTransferDstOptimal) {
228 }
else if (
old_layout == vk::ImageLayout::eTransferDstOptimal &&
229 new_layout == vk::ImageLayout::eShaderReadOnlyOptimal) {
236 log::engine::critical(
"unsupported layout transition!");
239 vk::ImageMemoryBarrier
barrier{
247 .subresourceRange = vk::ImageSubresourceRange{.aspectMask = vk::ImageAspectFlagBits::eColor,
278 const std::filesystem::path&
file_path) {
285 return std::unexpected(
"Failed to load texture image: " +
file_path.string());
292 vk::BufferUsageFlagBits::eTransferSrc,
293 vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
296 "Failed to map texture image: ");
303 auto [image, image_memory] =
308 vk::Format::eR8G8B8A8Srgb,
309 vk::ImageTiling::eOptimal,
310 vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled,
311 vk::MemoryPropertyFlagBits::eDeviceLocal);
317 [&](
const vk::CommandBuffer&
cmdBuffer) {
320 vk::Format::eR8G8B8A8Srgb,
321 vk::ImageLayout::eUndefined,
322 vk::ImageLayout::eTransferDstOptimal);
330 vk::Format::eR8G8B8A8Srgb,
331 vk::ImageLayout::eTransferDstOptimal,
332 vk::ImageLayout::eShaderReadOnlyOptimal);
336 return std::unexpected(
cmd_res.error());
341 .viewType = vk::ImageViewType::e2D,
342 .format = vk::Format::eR8G8B8A8Srgb,
343 .subresourceRange = vk::ImageSubresourceRange{.aspectMask = vk::ImageAspectFlagBits::eColor,
350 "Failed to create unique image view: ");
353 .image_memory = std::move(image_memory),
354 .image_view = std::move(image_view)};
363template<std::ranges::input_range R>
364 requires std::constructible_from<std::string_view, std::ranges::range_value_t<R>>
366 std::vector<std::string_view>
svs;
369 std::string_view
sv(
item);
389 if ((
i + 1) %
cols_ == 0 && (
i + 1) <
svs.size())
Definition Application.hpp:22
void transition_image_layout(const vk::CommandBuffer &cmd_buffer, const vk::Image &image, vk::Format format, vk::ImageLayout old_layout, vk::ImageLayout new_layout)
Definition utils.hpp:210
auto check_vulkan_res_val(T &&result, std::string_view message, std::source_location loc=std::source_location::current())
Definition vulkan_utils.hpp:25
std::pair< vk::UniqueImage, vk::UniqueDeviceMemory > create_image(const vk::Device &device, const vk::PhysicalDevice &physical_device, uint32_t width, uint32_t height, vk::Format format, vk::ImageTiling tiling, vk::ImageUsageFlags usage, vk::MemoryPropertyFlags properties)
Definition utils.hpp:143
void check_vulkan_res(vk::Result result, std::string_view message, std::source_location loc=std::source_location::current())
Definition vulkan_utils.hpp:10
void copy_buffer_to_image(const vk::CommandBuffer &cmd_buffer, const vk::Buffer &buffer, const vk::Image &image, uint32_t width, uint32_t height)
Definition utils.hpp:186
std::pair< vk::UniqueBuffer, vk::UniqueDeviceMemory > create_buffer(const vk::Device &device, const vk::PhysicalDevice &physical_device, vk::DeviceSize size, vk::BufferUsageFlags usage, vk::MemoryPropertyFlags properties)
Definition utils.hpp:106
std::string tab_format(R &&items, USize cols=4, USize spacing=2, bool row_major=true)
Definition utils.hpp:365
U32 find_memory_type(const vk::PhysicalDevice &physical_device, U32 type_filter, vk::MemoryPropertyFlags properties)
Definition vulkan_utils.hpp:43
std::expected< void, std::string > single_time_command(const vk::Device &device, const vk::Queue &queue, const vk::CommandPool &command_pool, const std::function< void(const vk::CommandBuffer &)> &action)
Definition utils.hpp:46
std::expected< Texture, std::string > create_texture(const vk::Device &device, const vk::PhysicalDevice &physical_device, const vk::Queue &graphics_queue, const vk::CommandPool &command_pool, const std::filesystem::path &file_path)
Definition utils.hpp:274
vk::UniqueDeviceMemory image_memory
Definition utils.hpp:264
vk::UniqueImageView image_view
Definition utils.hpp:265
vk::UniqueImage image
Definition utils.hpp:263
consteval U64 type_id_of()
Definition type_id.hpp:6
std::size_t USize
Definition types.hpp:18