image.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#pragma once


#include <planet/affine/extents2d.hpp>
#include <planet/vk/memory.hpp>


namespace planet::vk {


    template<typename T>
    class buffer;
    class command_pool;
    class swap_chain;

Vulkan image

Unlike a regular VkImage, this type also owns the device memory needed to back the image.

22
23
24
25
26
27
28
    class image final {
        using handle_type = vk::device_handle<VkImage, vkDestroyImage>;
        handle_type handle;

        device_memory memory;

      public:

Image creation

30
31
32
33
34
35
36
37
38
39
        image() {}
        image(device_memory_allocator &,
              std::uint32_t width,
              std::uint32_t height,
              std::uint32_t mip_levels,
              VkSampleCountFlagBits,
              VkFormat,
              VkImageTiling,
              VkImageUsageFlags,
              VkMemoryPropertyFlags);

Query image view

44
45
46
47
48
49
50
51
        std::uint32_t width = {}, height = {}, mip_levels = {};
        VkFormat format = {};
        auto device_handle() const noexcept { return handle.owner(); }
        auto get() const noexcept { return handle.get(); }

        affine::extents2d extents() const noexcept {
            return {static_cast<float>(width), static_cast<float>(height)};
        }

Image manipulation

Set up a layout transition

56
57
58
59
60
        void transition_layout(
                command_pool &,
                VkImageLayout old_layout,
                VkImageLayout new_layout,
                std::uint32_t mip_levels);

Copy staging buffer pixels to the image

63
        void copy_from(command_pool &, buffer<std::byte> const &);

Generate MIP map layers for image

66
67
        void generate_mip_maps(command_pool &, std::uint32_t mip_levels);
    };

Vulkan image view

71
72
73
74
75
    class image_view final {
        using handle_type = vk::device_handle<VkImageView, vkDestroyImageView>;
        handle_type handle;

      public:

Image view creation

78
        image_view() {}

Create from an image

81
82
83
84
85
86
87
88
89
90
        image_view(
                VkDevice,
                VkImage,
                VkFormat,
                VkImageAspectFlags,
                std::uint32_t mip_levels);
        image_view(vk::image const &image, VkImageAspectFlags const flags)
        : image_view{
                  image.device_handle(), image.get(), image.format, flags,
                  image.mip_levels} {}

Create an image view from the swap chain

93
        image_view(swap_chain const &, VkImage);

Query image view

 98
 99
100
101
102
103
        auto device_handle() const noexcept { return handle.owner(); }
        auto get() const noexcept { return handle.get(); }
    };


}