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 r32_sfloat = VK_FORMAT_R32_SFLOAT,
27 r32_uint = VK_FORMAT_R32_UINT,
28 r64_uint = VK_FORMAT_R64_UINT,
29 r16g16_sfloat = VK_FORMAT_R16G16_SFLOAT,
30 r32g32_sfloat = VK_FORMAT_R32G32_SFLOAT,
31 r32g32_uint = VK_FORMAT_R32G32_UINT,
32 r8g8b8a8_unorm = VK_FORMAT_R8G8B8A8_UNORM,
33 r8g8b8a8_srgb = VK_FORMAT_R8G8B8A8_SRGB,
34 b8g8r8a8_srgb = VK_FORMAT_B8G8R8A8_SRGB,
35 a2b10g10r10_unorm_pack32 = VK_FORMAT_A2B10G10R10_UNORM_PACK32,
36 r16g16b16a16_sfloat = VK_FORMAT_R16G16B16A16_SFLOAT,
37 r32g32b32a32_sfloat = VK_FORMAT_R32G32B32A32_SFLOAT
38}; // enum class format
39
40enum class address_mode : std::int32_t {
41 repeat = VK_SAMPLER_ADDRESS_MODE_REPEAT,
42 mirror = VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT,
43 clamp_to_edge = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
44 clamp_to_border = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
45}; // enum class address_mode
46
47enum class filter : std::int32_t {
48 nearest = VK_SAMPLER_MIPMAP_MODE_NEAREST,
49 linear = VK_SAMPLER_MIPMAP_MODE_LINEAR
50}; // enum class filter
51
52class image2d : public image {
53
54public:
55
56 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);
57
58 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);
59
60 image2d(const math::vector2u& extent, graphics::format format, graphics::filter filter, memory::observer_ptr<const std::uint8_t> pixels);
61
62 ~image2d() override = default;
63
64 auto set_pixels(memory::observer_ptr<const std::uint8_t> pixels) -> void;
65
66 auto name() const noexcept -> std::string override {
67 return "Image 2D";
68 }
69
70private:
71
72 enum class file_flags : std::uint16_t {
73 none = 0,
74 compressed = utility::bit_v<0>
75 }; // enum class file_flags
76
77 struct file_header {
78 std::uint64_t magic;
79 std::uint16_t version;
80 std::uint16_t flags;
81 std::uint32_t width;
82 std::uint32_t height;
83 std::uint32_t channels;
84 std::uint32_t uncompressed_size;
85 std::uint32_t compressed_size;
86 }; // struct file_header
87
88 static_assert(sizeof(file_header) == 32u, "file_header layout changed");
89
90 static constexpr auto file_magic = utility::make_magic<std::uint64_t>("SBXTEXTR");
91 static constexpr auto file_version = std::uint16_t{1u};
92 static constexpr auto binary_file_extension = std::string_view{".sbxtex"};
93
94 auto _upload_pixels(const std::uint8_t* pixels, std::size_t size) -> void;
95
96 auto _load(const std::filesystem::path& path = {}) -> void;
97
98 auto _load_binary(const std::filesystem::path& path) -> void;
99
100 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;
101
102 bool _anisotropic;
103 bool _mipmap;
104 std::uint8_t _channels;
105
106}; // class image2d
107
109
110} // namespace sbx::graphics
111
112template<>
113struct sbx::reflection::description<sbx::graphics::format> {
114
115 static constexpr auto name() -> std::string_view {
116 return "format";
117 }
118
119 static constexpr auto enumerators() {
120 return std::make_tuple(
121 enumerator{"undefined", sbx::graphics::format::undefined},
122 enumerator{"r8_unorm", sbx::graphics::format::r8_unorm},
123 enumerator{"r16_sfloat", sbx::graphics::format::r16_sfloat},
124 enumerator{"r32_sfloat", sbx::graphics::format::r32_sfloat},
125 enumerator{"r32_uint", sbx::graphics::format::r32_uint},
126 enumerator{"r64_uint", sbx::graphics::format::r64_uint},
127 enumerator{"r16g16_sfloat", sbx::graphics::format::r16g16_sfloat},
128 enumerator{"r32g32_sfloat", sbx::graphics::format::r32g32_sfloat},
129 enumerator{"r32g32_uint", sbx::graphics::format::r32g32_uint},
130 enumerator{"r8g8b8a8_unorm", sbx::graphics::format::r8g8b8a8_unorm},
131 enumerator{"r8g8b8a8_srgb", sbx::graphics::format::r8g8b8a8_srgb},
132 enumerator{"b8g8r8a8_srgb", sbx::graphics::format::b8g8r8a8_srgb},
133 enumerator{"a2b10g10r10_unorm_pack32", sbx::graphics::format::a2b10g10r10_unorm_pack32},
134 enumerator{"r16g16b16a16_sfloat", sbx::graphics::format::r16g16b16a16_sfloat},
135 enumerator{"r32g32b32a32_sfloat", sbx::graphics::format::r32g32b32a32_sfloat}
136 );
137 }
138
139}; // struct sbx::reflection::description<sbx::graphics::format>
140
141#endif // LIBSBX_GRAPHICS_IMAGES_IMAGE2D_HPP_
Definition: image2d.hpp:52
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