Antkeeper  0.0.1
image.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_GL_IMAGE_HPP
21 #define ANTKEEPER_GL_IMAGE_HPP
22 
23 #include <engine/gl/format.hpp>
24 #include <engine/gl/image-flag.hpp>
25 #include <array>
26 #include <cstddef>
27 #include <cstdint>
28 #include <limits>
29 #include <span>
30 
31 namespace gl {
32 
36 class image
37 {
38 public:
40  virtual ~image() = 0;
41 
42  image(const image&) = delete;
43  image(image&&) = delete;
44  image& operator=(const image&) = delete;
45  image& operator=(image&&) = delete;
46 
63  void read
64  (
65  std::uint32_t mip_level,
66  std::uint32_t offset_x,
67  std::uint32_t offset_y,
68  std::uint32_t offset_z,
69  std::uint32_t width,
70  std::uint32_t height,
71  std::uint32_t depth,
73  std::span<std::byte> data
74  ) const;
75 
93  void write
94  (
95  std::uint32_t mip_level,
96  std::uint32_t offset_x,
97  std::uint32_t offset_y,
98  std::uint32_t offset_z,
99  std::uint32_t width,
100  std::uint32_t height,
101  std::uint32_t depth,
103  std::span<const std::byte> data
104  );
105 
122  void copy
123  (
124  std::uint32_t src_mip_level,
125  std::uint32_t src_x,
126  std::uint32_t src_y,
127  std::uint32_t src_z,
128  image& dst_image,
129  std::uint32_t dst_mip_level,
130  std::uint32_t dst_x,
131  std::uint32_t dst_y,
132  std::uint32_t dst_z,
133  std::uint32_t width,
134  std::uint32_t height,
135  std::uint32_t depth
136  ) const;
137 
141  void generate_mipmaps();
142 
144  [[nodiscard]] inline constexpr std::uint8_t get_dimensionality() const noexcept
145  {
146  return m_dimensionality;
147  }
148 
150  [[nodiscard]] inline constexpr bool is_1d() const noexcept
151  {
152  return m_dimensionality == 1;
153  }
154 
156  [[nodiscard]] inline constexpr bool is_2d() const noexcept
157  {
158  return m_dimensionality == 2;
159  }
160 
162  [[nodiscard]] inline constexpr bool is_3d() const noexcept
163  {
164  return m_dimensionality == 3;
165  }
166 
168  [[nodiscard]] inline constexpr format get_format() const noexcept
169  {
170  return m_format;
171  }
172 
174  [[nodiscard]] inline constexpr const std::array<std::uint32_t, 3>& get_dimensions() const noexcept
175  {
176  return m_dimensions;
177  }
178 
180  [[nodiscard]] inline constexpr std::uint32_t get_mip_levels() const noexcept
181  {
182  return m_mip_levels;
183  }
184 
186  [[nodiscard]] inline constexpr std::uint32_t get_array_layers() const noexcept
187  {
188  return m_array_layers;
189  }
190 
192  [[nodiscard]] inline constexpr std::uint8_t get_flags() const noexcept
193  {
194  return m_flags;
195  }
196 
198  [[nodiscard]] inline constexpr bool is_cube_compatible() const noexcept
199  {
200  return m_flags & std::to_underlying(image_flag::cube_compatible);
201  }
202 
203 protected:
228  image
229  (
230  std::uint8_t dimensionality,
232  std::uint32_t width,
233  std::uint32_t height,
234  std::uint32_t depth,
235  std::uint32_t mip_levels,
236  std::uint32_t array_layers,
237  std::uint32_t flags
238  );
239 
240 private:
241  unsigned int m_gl_texture_target{0};
242  unsigned int m_gl_texture_name{0};
243  std::uint8_t m_dimensionality{0};
244  format m_format{format::undefined};
245  std::array<std::uint32_t, 3> m_dimensions{0, 0, 0};
246  std::uint32_t m_mip_levels{0};
247  std::uint32_t m_array_layers{0};
248  std::uint8_t m_flags{0};
249 
250  friend class image_view;
251 };
252 
256 class image_1d: public image
257 {
258 public:
260  image_1d
261  (
263  std::uint32_t width,
264  std::uint32_t mip_levels = 1,
265  std::uint32_t array_layers = 1,
266  std::uint32_t flags = 0
267  );
268 };
269 
273 class image_2d: public image
274 {
275 public:
277  image_2d
278  (
280  std::uint32_t width,
281  std::uint32_t height,
282  std::uint32_t mip_levels = 1,
283  std::uint32_t array_layers = 1,
284  std::uint32_t flags = 0
285  );
286 };
287 
291 class image_3d: public image
292 {
293 public:
295  image_3d
296  (
298  std::uint32_t width,
299  std::uint32_t height,
300  std::uint32_t depth,
301  std::uint32_t mip_levels = 1,
302  std::uint32_t flags = 0
303  );
304 };
305 
309 class image_cube: public image_2d
310 {
311 public:
313  image_cube
314  (
316  std::uint32_t width,
317  std::uint32_t mip_levels = 1,
318  std::uint32_t array_layers = 6
319  );
320 };
321 
322 } // namespace gl
323 
324 #endif // ANTKEEPER_GL_IMAGE_HPP
1D image.
Definition: image.hpp:257
image_1d(gl::format format, std::uint32_t width, std::uint32_t mip_levels=1, std::uint32_t array_layers=1, std::uint32_t flags=0)
Definition: image.cpp:477
2D image.
Definition: image.hpp:274
image_2d(gl::format format, std::uint32_t width, std::uint32_t height, std::uint32_t mip_levels=1, std::uint32_t array_layers=1, std::uint32_t flags=0)
Definition: image.cpp:498
3D image.
Definition: image.hpp:292
image_3d(gl::format format, std::uint32_t width, std::uint32_t height, std::uint32_t depth, std::uint32_t mip_levels=1, std::uint32_t flags=0)
Definition: image.cpp:520
Cube-compatible 2D image.
Definition: image.hpp:310
image_cube(gl::format format, std::uint32_t width, std::uint32_t mip_levels=1, std::uint32_t array_layers=6)
Definition: image.cpp:542
Image view.
Definition: image-view.hpp:34
constexpr bool is_cube_compatible() const noexcept
Returns true if the image is cube map compatible, false otherwise.
Definition: image.hpp:198
constexpr const std::array< std::uint32_t, 3 > & get_dimensions() const noexcept
Returns the dimensions of the image.
Definition: image.hpp:174
void read(std::uint32_t mip_level, std::uint32_t offset_x, std::uint32_t offset_y, std::uint32_t offset_z, std::uint32_t width, std::uint32_t height, std::uint32_t depth, gl::format format, std::span< std::byte > data) const
Reads pixel data from the image.
Definition: image.cpp:246
constexpr bool is_2d() const noexcept
Returns true if the image is 2D, false otherwise.
Definition: image.hpp:156
void generate_mipmaps()
Generates mip subimages.
Definition: image.cpp:468
constexpr std::uint8_t get_dimensionality() const noexcept
Returns the dimensionality of the image.
Definition: image.hpp:144
void write(std::uint32_t mip_level, std::uint32_t offset_x, std::uint32_t offset_y, std::uint32_t offset_z, std::uint32_t width, std::uint32_t height, std::uint32_t depth, gl::format format, std::span< const std::byte > data)
Writes pixel data to the image.
Definition: image.cpp:290
virtual ~image()=0
Destructs an image.
Definition: image.cpp:240
constexpr std::uint8_t get_flags() const noexcept
Returns the image flags.
Definition: image.hpp:192
constexpr std::uint32_t get_mip_levels() const noexcept
Returns the number of levels of detail available for minified sampling of the image.
Definition: image.hpp:180
void copy(std::uint32_t src_mip_level, std::uint32_t src_x, std::uint32_t src_y, std::uint32_t src_z, image &dst_image, std::uint32_t dst_mip_level, std::uint32_t dst_x, std::uint32_t dst_y, std::uint32_t dst_z, std::uint32_t width, std::uint32_t height, std::uint32_t depth) const
Copies pixel data from this image into another the image.
Definition: image.cpp:433
image & operator=(const image &)=delete
image(image &&)=delete
constexpr bool is_3d() const noexcept
Returns true if the image is 3D, false otherwise.
Definition: image.hpp:162
image & operator=(image &&)=delete
constexpr format get_format() const noexcept
Returns the format and type of the texel blocks contained in the image.
Definition: image.hpp:168
constexpr std::uint32_t get_array_layers() const noexcept
Returns the number of layers in the image.
Definition: image.hpp:186
image(const image &)=delete
constexpr bool is_1d() const noexcept
Returns true if the image is 1D, false otherwise.
Definition: image.hpp:150
Graphics library interface.
Definition: window.hpp:28
@ cube_compatible
Cube map view compatible image.
format
Image and vertex formats.
Definition: format.hpp:29