sandbox
Loading...
Searching...
No Matches
image2d.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_GRAPHICS_IMAGES_IMAGE2D_HPP_
3#define LIBSBX_GRAPHICS_IMAGES_IMAGE2D_HPP_
4
5#include <filesystem>
6
7#include <libsbx/utility/crc32.hpp>
8#include <libsbx/utility/enum.hpp>
9
10#include <libsbx/memory/observer_ptr.hpp>
11
12#include <libsbx/reflection/description.hpp>
13
14#include <libsbx/math/vector2.hpp>
15
16#include <libsbx/graphics/resource_storage.hpp>
17
18#include <libsbx/graphics/images/image.hpp>
19
20namespace sbx::graphics {
21
22enum class format : std::int32_t {
23 undefined = VK_FORMAT_UNDEFINED,
24 r8_unorm = VK_FORMAT_R8_UNORM,
25 r16_sfloat = VK_FORMAT_R16_SFLOAT,
26 r16_unorm = VK_FORMAT_R16_UNORM,
27 r32_sfloat = VK_FORMAT_R32_SFLOAT,
28 r32_uint = VK_FORMAT_R32_UINT,
29 r64_uint = VK_FORMAT_R64_UINT,
30 r16g16_sfloat = VK_FORMAT_R16G16_SFLOAT,
31 r32g32_sfloat = VK_FORMAT_R32G32_SFLOAT,
32 r32g32_uint = VK_FORMAT_R32G32_UINT,
33 r8g8b8a8_unorm = VK_FORMAT_R8G8B8A8_UNORM,
34 r8g8b8a8_srgb = VK_FORMAT_R8G8B8A8_SRGB,
35 b8g8r8a8_srgb = VK_FORMAT_B8G8R8A8_SRGB,
36 a2b10g10r10_unorm_pack32 = VK_FORMAT_A2B10G10R10_UNORM_PACK32,
37 r16g16b16a16_sfloat = VK_FORMAT_R16G16B16A16_SFLOAT,
38 r32g32b32a32_sfloat = VK_FORMAT_R32G32B32A32_SFLOAT
39}; // enum class format
40
41enum class address_mode : std::int32_t {
42 repeat = VK_SAMPLER_ADDRESS_MODE_REPEAT,
43 mirror = VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT,
44 clamp_to_edge = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
45 clamp_to_border = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
46}; // enum class address_mode
47
48enum class filter : std::int32_t {
49 nearest = VK_SAMPLER_MIPMAP_MODE_NEAREST,
50 linear = VK_SAMPLER_MIPMAP_MODE_LINEAR
51}; // enum class filter
52
53class image2d : public image {
54
55public:
56
57 image2d(const math::vector2u& extent, graphics::format format = graphics::format::r8g8b8a8_unorm, graphics::filter filter = graphics::filter::linear, graphics::address_mode address_mode = graphics::address_mode::repeat, VkImageUsageFlags usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_STORAGE_BIT, VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT, bool anisotropic = false, bool mipmap = false, std::uint32_t array_layers = 1u);
58
59 image2d(const std::filesystem::path& path, graphics::format format = graphics::format::r8g8b8a8_srgb, graphics::filter filter = graphics::filter::linear, graphics::address_mode address_mode = graphics::address_mode::repeat, bool anisotropic = false, bool mipmap = false);
60
61 image2d(const math::vector2u& extent, graphics::format format, graphics::filter filter, memory::observer_ptr<const std::uint8_t> pixels);
62
63 ~image2d() override = default;
64
65 auto set_pixels(memory::observer_ptr<const std::uint8_t> pixels) -> void;
66
67 auto name() const noexcept -> std::string override {
68 return "Image 2D";
69 }
70
71private:
72
73 enum class file_flags : std::uint16_t {
74 none = 0,
75 compressed = utility::bit_v<0>
76 }; // enum class file_flags
77
78 struct file_header {
79 std::uint64_t magic;
80 std::uint16_t version;
81 std::uint16_t flags;
82 std::uint32_t width;
83 std::uint32_t height;
84 std::uint32_t channels;
85 std::uint32_t uncompressed_size;
86 std::uint32_t compressed_size;
87 }; // struct file_header
88
89 static_assert(sizeof(file_header) == 32u, "file_header layout changed");
90
91 static constexpr auto file_magic = utility::make_magic<std::uint64_t>("SBXTEXTR");
92 static constexpr auto file_version = std::uint16_t{1u};
93 static constexpr auto binary_file_extension = std::string_view{".sbxtex"};
94
95 auto _upload_pixels(const std::uint8_t* pixels, std::size_t size) -> void;
96
97 auto _load(const std::filesystem::path& path = {}) -> void;
98
99 auto _load_binary(const std::filesystem::path& path) -> void;
100
101 static auto _process(const std::filesystem::path& path, std::uint32_t width, std::uint32_t height, std::uint32_t channels, const std::uint8_t* pixels) -> void;
102
103 bool _anisotropic;
104 bool _mipmap;
105 std::uint8_t _channels;
106
107}; // class image2d
108
110
111} // namespace sbx::graphics
112
113template<>
114struct sbx::reflection::description<sbx::graphics::format> {
115
116 static constexpr auto name() -> std::string_view {
117 return "format";
118 }
119
120 static constexpr auto enumerators() {
121 return std::make_tuple(
122 enumerator{"undefined", sbx::graphics::format::undefined},
123 enumerator{"r8_unorm", sbx::graphics::format::r8_unorm},
124 enumerator{"r16_sfloat", sbx::graphics::format::r16_sfloat},
125 enumerator{"r16_unorm", sbx::graphics::format::r16_unorm},
126 enumerator{"r32_sfloat", sbx::graphics::format::r32_sfloat},
127 enumerator{"r32_uint", sbx::graphics::format::r32_uint},
128 enumerator{"r64_uint", sbx::graphics::format::r64_uint},
129 enumerator{"r16g16_sfloat", sbx::graphics::format::r16g16_sfloat},
130 enumerator{"r32g32_sfloat", sbx::graphics::format::r32g32_sfloat},
131 enumerator{"r32g32_uint", sbx::graphics::format::r32g32_uint},
132 enumerator{"r8g8b8a8_unorm", sbx::graphics::format::r8g8b8a8_unorm},
133 enumerator{"r8g8b8a8_srgb", sbx::graphics::format::r8g8b8a8_srgb},
134 enumerator{"b8g8r8a8_srgb", sbx::graphics::format::b8g8r8a8_srgb},
135 enumerator{"a2b10g10r10_unorm_pack32", sbx::graphics::format::a2b10g10r10_unorm_pack32},
136 enumerator{"r16g16b16a16_sfloat", sbx::graphics::format::r16g16b16a16_sfloat},
137 enumerator{"r32g32b32a32_sfloat", sbx::graphics::format::r32g32b32a32_sfloat}
138 );
139 }
140
141}; // struct sbx::reflection::description<sbx::graphics::format>
142
143#endif // LIBSBX_GRAPHICS_IMAGES_IMAGE2D_HPP_
Definition: image2d.hpp:53
Definition: image.hpp:21
Definition: resource_storage.hpp:18
A vector in two-dimensional space.
Definition: vector2.hpp:28
A non-owning pointer that can be used to observe the value of a pointer.
Definition: observer_ptr.hpp:29
Definition: description.hpp:16
Definition: enumerator.hpp:10