Lluvia
ImageDescriptor.h
Go to the documentation of this file.
1 
8 #ifndef LLUVIA_CORE_IMAGE_IMAGE_DESCRIPTOR_H_
9 #define LLUVIA_CORE_IMAGE_IMAGE_DESCRIPTOR_H_
10 
12 #include "lluvia/core/error.h"
15 #include "lluvia/core/types.h"
16 
17 #include <array>
18 #include <cstdint>
19 #include <string>
20 #include <tuple>
21 #include <type_traits>
22 
24 
25 namespace ll {
26 
27 // FIXME: move enums to their own file.
33 enum class ChannelType : ll::enum_t {
34  Uint8 = 0,
35  Int8 = 1,
37  Uint16 = 2,
38  Int16 = 3,
39  Float16 = 4,
41  Uint32 = 5,
42  Int32 = 6,
43  Float32 = 7,
45  Uint64 = 8,
46  Int64 = 9,
47  Float64 = 10
48 };
49 
53 enum class ChannelCount : ll::enum_t {
54  C1 = 1,
55  C2 = 2,
56  C3 = 3,
57  C4 = 4
58 };
59 
60 namespace impl {
61 
67  constexpr const std::array<std::tuple<const char*, ll::ChannelType>, 11> ChannelTypeStrings {{
68  std::make_tuple("Uint8", ll::ChannelType::Uint8),
69  std::make_tuple("Int8", ll::ChannelType::Int8),
70 
71  std::make_tuple("Uint16", ll::ChannelType::Uint16),
72  std::make_tuple("Int16", ll::ChannelType::Int16),
73  std::make_tuple("Float16", ll::ChannelType::Float16),
74 
75  std::make_tuple("Uint32", ll::ChannelType::Uint32),
76  std::make_tuple("Int32", ll::ChannelType::Int32),
77  std::make_tuple("Float32", ll::ChannelType::Float32),
78 
79  std::make_tuple("Uint64", ll::ChannelType::Uint64),
80  std::make_tuple("Int64", ll::ChannelType::Int64),
81  std::make_tuple("Float64", ll::ChannelType::Float64),
82  }};
83 
89  constexpr const std::array<std::tuple<const char*, ll::ChannelCount>, 4> ChannelCountStrings {{std::make_tuple("C1", ll::ChannelCount::C1),
90  std::make_tuple("C2", ll::ChannelCount::C2),
91  std::make_tuple("C3", ll::ChannelCount::C3),
92  std::make_tuple("C4", ll::ChannelCount::C4)}};
93 
94 } // namespace impl
95 
105 template <typename T = std::string>
106 inline T channelTypeToString(ll::ChannelType&& value) noexcept
107 {
108  return ll::impl::enumToString<ll::ChannelType, ll::impl::ChannelTypeStrings.size(), impl::ChannelTypeStrings>(std::forward<ll::ChannelType>(value));
109 }
110 
111 template <typename T = std::string>
112 inline T channelTypeToString(const ll::ChannelType& value) noexcept
113 {
115 }
116 
132 template <typename T>
133 inline ll::ChannelType stringToChannelType(T&& stringValue)
134 {
135  return impl::stringToEnum<ll::ChannelType, T, ll::impl::ChannelTypeStrings.size(), impl::ChannelTypeStrings>(std::forward<T>(stringValue));
136 }
137 
138 template <typename T = uint32_t>
140 {
141  static_assert(std::is_integral<T>::value, "T must be an integral type.");
142 
143  const auto casted = static_cast<std::underlying_type<ll::ChannelCount>::type>(c);
144 
145  constexpr const auto minValue = static_cast<std::underlying_type<ll::ChannelCount>::type>(ll::ChannelCount::C1);
146  constexpr const auto maxValue = static_cast<std::underlying_type<ll::ChannelCount>::type>(ll::ChannelCount::C4);
147 
149  casted < minValue || casted > maxValue,
151  "Error casting integral value " + std::to_string(c) + " to ll::ChannelCount. " + "Expected value must be within range [" + std::to_string(minValue) + ", " + std::to_string(maxValue) + "].");
152 
153  return static_cast<ll::ChannelCount>(c);
154 }
155 
164 
173 vk::Format getVulkanImageFormat(ll::ChannelCount channelCount, ll::ChannelType channelType) noexcept;
174 
190 
191 public:
192  ImageDescriptor() = default;
193  ImageDescriptor(const ImageDescriptor& descriptor) = default;
194  ImageDescriptor(ImageDescriptor&& descriptor) = default;
195 
205  ImageDescriptor(const uint32_t depth,
206  const uint32_t height,
207  const uint32_t width,
208  const ll::ChannelCount channelCount,
209  const ll::ChannelType channelType);
210 
222  ImageDescriptor(const uint32_t depth,
223  const uint32_t height,
224  const uint32_t width,
225  const ll::ChannelCount channelCount,
226  const ll::ChannelType channelType,
227  const ll::ImageUsageFlags usageFlags,
228  const ll::ImageTiling tiling);
229 
230  ~ImageDescriptor() = default;
231 
232  ImageDescriptor& operator=(const ImageDescriptor& descriptor) = default;
233  ImageDescriptor& operator=(ImageDescriptor&& descriptor) = default;
234 
243 
252 
260  ImageDescriptor& setWidth(const uint32_t width) noexcept;
261 
269  ImageDescriptor& setHeight(const uint32_t height) noexcept;
270 
278  ImageDescriptor& setDepth(const uint32_t depth) noexcept;
279 
292  ImageDescriptor& setShape(const ll::vec3ui& shape) noexcept;
293 
300 
310  ImageDescriptor& setUsageFlagsUnsafe(const uint32_t flags) noexcept;
311 
319  ImageDescriptor& setTiling(const ll::ImageTiling tTiling) noexcept;
320 
326  ll::ChannelType getChannelType() const noexcept;
327 
335  template <typename T = ll::ChannelCount>
336  T getChannelCount() const noexcept
337  {
338  return static_cast<T>(m_channelCount);
339  }
340 
346  uint32_t getWidth() const noexcept;
347 
353  uint32_t getHeight() const noexcept;
354 
360  uint32_t getDepth() const noexcept;
361 
370  uint64_t getSize() const noexcept;
371 
383  ll::vec3ui getShape() const noexcept;
384 
398  vk::ImageType getImageType() const noexcept;
399 
409  vk::Format getFormat() const noexcept;
410 
419  ll::ImageUsageFlags getUsageFlags() const noexcept;
420 
426  ll::ImageTiling getTiling() const noexcept;
427 
435  uint32_t getUsageFlagsUnsafe() const noexcept;
436 
437 private:
438  ll::ChannelType m_channelType {ll::ChannelType::Uint8};
439  ll::ChannelCount m_channelCount {ll::ChannelCount::C1};
440 
441  // dimensions along each axis
442  // x : width
443  // y : height
444  // z : depth
445  ll::vec3ui m_shape {1, 1, 1};
446 
449 };
450 
451 } // namespace ll
452 
453 #endif /* LLUVIA_CORE_IMAGE_IMAGE_DESCRIPTOR_H_ */
Stores all relevant information for creating ll::Image objects.
Definition: ImageDescriptor.h:189
ll::ImageTiling getTiling() const noexcept
Gets the vulkan image tiling.
ImageDescriptor & setHeight(const uint32_t height) noexcept
Sets the height.
uint32_t getUsageFlagsUnsafe() const noexcept
Gets the usage flags casted to an integer type.
ImageDescriptor & setWidth(const uint32_t width) noexcept
Sets the width.
ImageDescriptor & operator=(ImageDescriptor &&descriptor)=default
vk::ImageType getImageType() const noexcept
Gets the Vulkan image type.
ImageDescriptor & setTiling(const ll::ImageTiling tTiling) noexcept
Sets the vulkan image tiling.
ll::ChannelType getChannelType() const noexcept
Gets the channel type.
ImageDescriptor & operator=(const ImageDescriptor &descriptor)=default
ImageDescriptor(const ImageDescriptor &descriptor)=default
ImageDescriptor & setChannelType(const ll::ChannelType type) noexcept
Sets the channel type.
ImageDescriptor & setUsageFlagsUnsafe(const uint32_t flags) noexcept
Sets the usage flags from an integer type.
uint32_t getHeight() const noexcept
Gets the image height in pixels.
ImageDescriptor(const uint32_t depth, const uint32_t height, const uint32_t width, const ll::ChannelCount channelCount, const ll::ChannelType channelType)
Constructs the object.
ImageDescriptor(ImageDescriptor &&descriptor)=default
uint32_t getDepth() const noexcept
Gets the image depth in pixels.
ImageDescriptor & setDepth(const uint32_t depth) noexcept
Sets the depth.
~ImageDescriptor()=default
uint64_t getSize() const noexcept
Gets the size in bytes required to store the image.
ImageDescriptor & setShape(const ll::vec3ui &shape) noexcept
Sets the image shape.
ImageDescriptor()=default
uint32_t getWidth() const noexcept
Gets the image width in pixels.
ImageDescriptor & setChannelCount(const ll::ChannelCount count) noexcept
Sets the channel count.
ImageDescriptor & setUsageFlags(const ll::ImageUsageFlags flags) noexcept
Sets the usage flags.
ll::vec3ui getShape() const noexcept
Gets the shape of the image.
vk::Format getFormat() const noexcept
Gets the corresponding Vulkan image format.
T getChannelCount() const noexcept
Gets the channel count.
Definition: ImageDescriptor.h:336
ImageDescriptor(const uint32_t depth, const uint32_t height, const uint32_t width, const ll::ChannelCount channelCount, const ll::ChannelType channelType, const ll::ImageUsageFlags usageFlags, const ll::ImageTiling tiling)
Constructs the object.
ll::ImageUsageFlags getUsageFlags() const noexcept
Gets the Vulkan image usage flags.
Enums.
error related classes and methods.
constexpr const std::array< std::tuple< const char *, ll::ChannelType >, 11 > ChannelTypeStrings
Channel type string values used for converting ll::ChannelType to std::string and vice-versa.
Definition: ImageDescriptor.h:67
E stringToEnum(T &&stringValue)
Definition: enums.h:172
std::string enumToString(E &&value) noexcept
Definition: enums.h:146
constexpr const std::array< std::tuple< const char *, ll::ChannelCount >, 4 > ChannelCountStrings
Channel type string values used for converting ll::ChannelType to std::string and vice-versa.
Definition: ImageDescriptor.h:89
Definition: Buffer.h:28
ChannelType
Supported image channel types.
Definition: ImageDescriptor.h:33
ll::ChannelType stringToChannelType(T &&stringValue)
Converts from a string-like object to ll::ChannelType enum.
Definition: ImageDescriptor.h:133
void throwSystemErrorIf(bool condition, ll::ErrorCode errorCode, T &&msg)
Throws a std::system_error exception if the condition passed is true.
Definition: error.h:153
ImageTiling
Definition: ImageTiling.h:16
vk::Format getVulkanImageFormat(ll::ChannelCount channelCount, ll::ChannelType channelType) noexcept
Gets the vulkan image format for a given channel count and type.
ChannelCount
Supported image channel count.
Definition: ImageDescriptor.h:53
T channelTypeToString(ll::ChannelType &&value) noexcept
Converts from ll::ChannelType enum value to std::string.
Definition: ImageDescriptor.h:106
ll::ChannelCount castChannelCount(T c)
Definition: ImageDescriptor.h:139
uint32_t enum_t
Definition: enums.h:23
uint64_t getChannelTypeSize(ll::ChannelType type)
Gets size in bytes of a given channel type.
Common types.