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/math/vector2.hpp>
13
14#include <libsbx/graphics/resource_storage.hpp>
15
16#include <libsbx/graphics/images/image.hpp>
17
18namespace sbx::graphics {
19
20enum class format : std::int32_t {
21 undefined = VK_FORMAT_UNDEFINED,
22 r8_unorm = VK_FORMAT_R8_UNORM,
23 r16_sfloat = VK_FORMAT_R16_SFLOAT,
24 r32_sfloat = VK_FORMAT_R32_SFLOAT,
25 r32_uint = VK_FORMAT_R32_UINT,
26 r64_uint = VK_FORMAT_R64_UINT,
27 r16g16_sfloat = VK_FORMAT_R16G16_SFLOAT,
28 r32g32_sfloat = VK_FORMAT_R32G32_SFLOAT,
29 r32g32_uint = VK_FORMAT_R32G32_UINT,
30 r8g8b8a8_unorm = VK_FORMAT_R8G8B8A8_UNORM,
31 r8g8b8a8_srgb = VK_FORMAT_R8G8B8A8_SRGB,
32 b8g8r8a8_srgb = VK_FORMAT_B8G8R8A8_SRGB,
33 a2b10g10r10_unorm_pack32 = VK_FORMAT_A2B10G10R10_UNORM_PACK32,
34 r16g16b16a16_sfloat = VK_FORMAT_R16G16B16A16_SFLOAT,
35 r32g32b32a32_sfloat = VK_FORMAT_R32G32B32A32_SFLOAT
36}; // enum class format
37
38enum class address_mode : std::int32_t {
39 repeat = VK_SAMPLER_ADDRESS_MODE_REPEAT,
40 mirror = VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT,
41 clamp_to_edge = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
42 clamp_to_border = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
43}; // enum class address_mode
44
45enum class filter : std::int32_t {
46 nearest = VK_SAMPLER_MIPMAP_MODE_NEAREST,
47 linear = VK_SAMPLER_MIPMAP_MODE_LINEAR
48}; // enum class filter
49
50class image2d : public image {
51
52public:
53
54 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);
55
56 image2d(const std::filesystem::path& path, graphics::format format = graphics::format::r8g8b8a8_srgb, VkFilter filter = VK_FILTER_LINEAR, VkSamplerAddressMode address_mode = VK_SAMPLER_ADDRESS_MODE_REPEAT, bool anisotropic = false, bool mipmap = false);
57
58 image2d(const math::vector2u& extent, graphics::format format, graphics::filter filter, memory::observer_ptr<const std::uint8_t> pixels);
59
60 ~image2d() override = default;
61
62 auto set_pixels(memory::observer_ptr<const std::uint8_t> pixels) -> void;
63
64 auto name() const noexcept -> std::string override {
65 return "Image 2D";
66 }
67
68private:
69
70 enum class file_flags : std::uint16_t {
71 none = 0,
72 compressed = utility::bit_v<0>
73 }; // enum class file_flags
74
75 struct file_header {
76 std::uint64_t magic;
77 std::uint16_t version;
78 std::uint16_t flags;
79 std::uint32_t width;
80 std::uint32_t height;
81 std::uint32_t channels;
82 std::uint32_t uncompressed_size;
83 std::uint32_t compressed_size;
84 }; // struct file_header
85
86 static_assert(sizeof(file_header) == 32u, "file_header layout changed");
87
88 static constexpr auto file_magic = utility::make_magic<std::uint64_t>("SBXTEXTR");
89 static constexpr auto file_version = std::uint16_t{1u};
90 static constexpr auto binary_file_extension = std::string_view{".sbxtex"};
91
92 auto _upload_pixels(const std::uint8_t* pixels, std::size_t size) -> void;
93
94 auto _load(const std::filesystem::path& path = {}) -> void;
95
96 auto _load_binary(const std::filesystem::path& path) -> void;
97
98 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;
99
100 bool _anisotropic;
101 bool _mipmap;
102 std::uint8_t _channels;
103
104}; // class image2d
105
107
108} // namespace sbx::graphics
109
110#endif // LIBSBX_GRAPHICS_IMAGES_IMAGE2D_HPP_
Definition: image2d.hpp:50
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:28