summaryrefslogtreecommitdiff
path: root/include/resources/image.h
blob: 391c023f44b26dbada225dedbefc0336fd304ac6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once

#include <unordered_map>
#include <vk_mem_alloc.h>
#include <vulkan/vulkan_core.h>
namespace iris {

enum struct ImageType {
    Texture1D,
    Texture1DArray,
    Texture2D,
    Texture2DArray,
    Texture3D,
    TextureCube,
    TextureCubeArray,
};

struct ImageDesc {
    ImageType image_type;
    VkImageUsageFlags image_usage;
    VkImageCreateFlags create_flags;
    VmaAllocationCreateFlags alloc_flags;

    VkFormat format;
    VkExtent3D extent;
    uint32_t mip_levels;
    uint32_t array_layers;
};

struct ImageViewDesc {
    VkImageViewType view_type;
    VkFormat format;
    VkImageAspectFlags aspect_mask;
    uint32_t base_mip_level;
    uint32_t mip_levels;
    uint32_t base_array_layer;
    uint32_t array_layers;

    bool operator==(const ImageViewDesc& other) const = default;
};

struct ImageViewDescHash {
    std::size_t operator()(const ImageViewDesc& desc) const noexcept {
        auto h = std::hash<int>{}(desc.view_type);
        h ^= std::hash<int>{}(desc.format) + 0x9e3779b9 + (h << 6) + (h >> 2);
        h ^= std::hash<int>{}(desc.aspect_mask) + 0x9e3779b9 + (h << 6) + (h >> 2);
        h ^= std::hash<int>{}(desc.base_mip_level) + 0x9e3779b9 + (h << 6) + (h >> 2);
        h ^= std::hash<int>{}(desc.mip_levels) + 0x9e3779b9 + (h << 6) + (h >> 2);
        h ^= std::hash<int>{}(desc.base_array_layer) + 0x9e3779b9 + (h << 6) + (h >> 2);
        h ^= std::hash<int>{}(desc.array_layers) + 0x9e3779b9 + (h << 6) + (h >> 2);
        return h;
    }
};

struct Image_tt {
    VkImage handle;
    ImageDesc desc;

    // Used during release
    VkDevice device;
    VmaAllocator allocator;
    VmaAllocation allocation;

    // store all the image views
    std::unordered_map<ImageViewDesc, VkImageView, ImageViewDescHash> image_views;
    VkImageView get_image_view(const ImageViewDesc &desc);

    void release();
};

} // namespace iris