💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
7.9 KiB

  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. #ifndef ANTKEEPER_GL_IMAGE_VIEW_HPP
  20. #define ANTKEEPER_GL_IMAGE_VIEW_HPP
  21. #include <engine/gl/image.hpp>
  22. #include <engine/gl/image-view-flag.hpp>
  23. #include <cstdint>
  24. #include <memory>
  25. namespace gl {
  26. /**
  27. * Image view.
  28. */
  29. class image_view
  30. {
  31. public:
  32. /// Destructs an image view.
  33. virtual ~image_view() = 0;
  34. image_view(const image_view&) = delete;
  35. image_view(image_view&&) = delete;
  36. image_view& operator=(const image_view&) = delete;
  37. image_view& operator=(image_view&&) = delete;
  38. /// Returns the image on which the view was created.
  39. [[nodiscard]] inline constexpr const std::shared_ptr<image>& get_image() const noexcept
  40. {
  41. return m_image;
  42. }
  43. /// Returns the format and type used to interpret texel blocks of the image.
  44. [[nodiscard]] inline constexpr format get_format() const noexcept
  45. {
  46. return m_format;
  47. }
  48. /// Returns the first mipmap level accessible to the view.
  49. [[nodiscard]] inline constexpr std::uint32_t get_first_mip_level() const noexcept
  50. {
  51. return m_first_mip_level;
  52. }
  53. /// Returns the number of mipmap levels accessible to the view.
  54. [[nodiscard]] inline constexpr std::uint32_t get_mip_level_count() const noexcept
  55. {
  56. return m_mip_level_count;
  57. }
  58. /// Returns the first array layer accessible to the view.
  59. [[nodiscard]] inline constexpr std::uint32_t get_first_array_layer() const noexcept
  60. {
  61. return m_first_array_layer;
  62. }
  63. /// Returns the number of array layers accessible to the view.
  64. [[nodiscard]] inline constexpr std::uint32_t get_array_layer_count() const noexcept
  65. {
  66. return m_array_layer_count;
  67. }
  68. /// Returns the dimensionality of the image view.
  69. [[nodiscard]] inline constexpr std::uint8_t get_dimensionality() const noexcept
  70. {
  71. return m_dimensionality;
  72. }
  73. /// Returns `true` if the image view is 1D, `false` otherwise.
  74. [[nodiscard]] inline constexpr bool is_1d() const noexcept
  75. {
  76. return m_dimensionality == 1;
  77. }
  78. /// Returns `true` if the image view is 2D, `false` otherwise.
  79. [[nodiscard]] inline constexpr bool is_2d() const noexcept
  80. {
  81. return m_dimensionality == 2;
  82. }
  83. /// Returns `true` if the image view is 3D, `false` otherwise.
  84. [[nodiscard]] inline constexpr bool is_3d() const noexcept
  85. {
  86. return m_dimensionality == 3;
  87. }
  88. /// Returns `true` if the image view is an array view, `false` otherwise.
  89. [[nodiscard]] inline constexpr bool is_array() const noexcept
  90. {
  91. return m_flags & std::to_underlying(image_view_flag::array);
  92. }
  93. /// Returns `true` if the image view is a cube map view, `false` otherwise.
  94. [[nodiscard]] inline constexpr bool is_cube() const noexcept
  95. {
  96. return m_flags & std::to_underlying(image_view_flag::cube);
  97. }
  98. protected:
  99. /**
  100. * Constructs an image view from an image.
  101. *
  102. * @param image Image on which the view will be created.
  103. * @param dimensionality Image view dimensionality, on `[1, 3]`.
  104. * @param format Format and type used to interpret texel blocks of the image. If gl::format::undefined, the format will be set to the format of the image.
  105. * @param first_mip_level First mipmap level accessible to the view.
  106. * @param mip_level_count Number of mipmap levels accessible to the view.
  107. * @param first_array_layer First array layer accessible to the view.
  108. * @param array_layer Number of array layers accessible to the view.
  109. * @param flags Image view flags.
  110. *
  111. * @except std::invalid_argument Image view has null image.
  112. * @except std::invalid_argument Image view has unsupported format.
  113. * @except std::invalid_argument Image view has zero mip levels.
  114. * @except std::out_of_range Image view mip range out of image mip range.
  115. * @except std::invalid_argument Image view has zero array layers.
  116. * @except std::out_of_range Image view array layer range out of image array layer range.
  117. * @except std::invalid_argument Image view dimensionality must match image dimensionality.
  118. * @except std::invalid_argument Cube image views must be constructed from cube-compatible images.
  119. * @except std::invalid_argument Cube image views array layer count must be a multiple of 6.
  120. */
  121. image_view
  122. (
  123. std::shared_ptr<gl::image> image,
  124. std::uint8_t dimensionality,
  125. gl::format format,
  126. std::uint32_t first_mip_level,
  127. std::uint32_t mip_level_count,
  128. std::uint32_t first_array_layer,
  129. std::uint32_t array_layer_count,
  130. std::uint8_t flags
  131. );
  132. private:
  133. friend class framebuffer;
  134. friend class gl_shader_texture_1d;
  135. friend class gl_shader_texture_2d;
  136. friend class gl_shader_texture_3d;
  137. friend class gl_shader_texture_cube;
  138. unsigned int m_gl_texture_name{0};
  139. std::shared_ptr<image> m_image;
  140. std::uint8_t m_dimensionality{0};
  141. format m_format{format::undefined};
  142. std::uint32_t m_first_mip_level{0};
  143. std::uint32_t m_mip_level_count{0};
  144. std::uint32_t m_first_array_layer{0};
  145. std::uint32_t m_array_layer_count{0};
  146. std::uint8_t m_flags{0};
  147. };
  148. /**
  149. * 1D image view.
  150. */
  151. class image_view_1d: public image_view
  152. {
  153. public:
  154. /// @copydoc image_view::image_view
  155. image_view_1d
  156. (
  157. std::shared_ptr<gl::image> image,
  158. gl::format format = gl::format::undefined,
  159. std::uint32_t first_mip_level = 0,
  160. std::uint32_t mip_level_count = 1,
  161. std::uint32_t first_array_layer = 0
  162. );
  163. };
  164. /**
  165. * 1D image array view.
  166. */
  167. class image_view_1d_array: public image_view
  168. {
  169. public:
  170. /// @copydoc image_view::image_view
  171. image_view_1d_array
  172. (
  173. std::shared_ptr<gl::image> image,
  174. gl::format format = gl::format::undefined,
  175. std::uint32_t first_mip_level = 0,
  176. std::uint32_t mip_level_count = 1,
  177. std::uint32_t first_array_layer = 0,
  178. std::uint32_t array_layer_count = 1
  179. );
  180. };
  181. /**
  182. * 2D image view.
  183. */
  184. class image_view_2d: public image_view
  185. {
  186. public:
  187. /// @copydoc image_view::image_view
  188. image_view_2d
  189. (
  190. std::shared_ptr<gl::image> image,
  191. gl::format format = gl::format::undefined,
  192. std::uint32_t first_mip_level = 0,
  193. std::uint32_t mip_level_count = 1,
  194. std::uint32_t first_array_layer = 0
  195. );
  196. };
  197. /**
  198. * 2D image array view.
  199. */
  200. class image_view_2d_array: public image_view
  201. {
  202. public:
  203. /// @copydoc image_view::image_view
  204. image_view_2d_array
  205. (
  206. std::shared_ptr<gl::image> image,
  207. gl::format format = gl::format::undefined,
  208. std::uint32_t first_mip_level = 0,
  209. std::uint32_t mip_level_count = 1,
  210. std::uint32_t first_array_layer = 0,
  211. std::uint32_t array_layer_count = 1
  212. );
  213. };
  214. /**
  215. * 3D image view.
  216. */
  217. class image_view_3d: public image_view
  218. {
  219. public:
  220. /// @copydoc image_view::image_view
  221. image_view_3d
  222. (
  223. std::shared_ptr<gl::image> image,
  224. gl::format format = gl::format::undefined,
  225. std::uint32_t first_mip_level = 0,
  226. std::uint32_t mip_level_count = 1
  227. );
  228. };
  229. /**
  230. * Cube image view.
  231. */
  232. class image_view_cube: public image_view
  233. {
  234. public:
  235. /// @copydoc image_view::image_view
  236. image_view_cube
  237. (
  238. std::shared_ptr<gl::image> image,
  239. gl::format format = gl::format::undefined,
  240. std::uint32_t first_mip_level = 0,
  241. std::uint32_t mip_level_count = 1,
  242. std::uint32_t first_array_layer = 0
  243. );
  244. };
  245. /**
  246. * Cube image array view.
  247. */
  248. class image_view_cube_array: public image_view
  249. {
  250. public:
  251. /// @copydoc image_view::image_view
  252. image_view_cube_array
  253. (
  254. std::shared_ptr<gl::image> image,
  255. gl::format format = gl::format::undefined,
  256. std::uint32_t first_mip_level = 0,
  257. std::uint32_t mip_level_count = 1,
  258. std::uint32_t first_array_layer = 0,
  259. std::uint32_t array_layer_count = 6
  260. );
  261. };
  262. } // namespace gl
  263. #endif // ANTKEEPER_GL_IMAGE_VIEW_HPP