💿🐜 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.

274 lines
8.4 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_SAMPLER_HPP
  20. #define ANTKEEPER_GL_SAMPLER_HPP
  21. #include <engine/gl/sampler-filter.hpp>
  22. #include <engine/gl/sampler-mipmap-mode.hpp>
  23. #include <engine/gl/sampler-address-mode.hpp>
  24. #include <engine/gl/compare-op.hpp>
  25. #include <array>
  26. namespace gl {
  27. /**
  28. * Sampler object.
  29. */
  30. class sampler
  31. {
  32. public:
  33. /**
  34. * Constructs a sampler object.
  35. *
  36. * @param mag_filter Magnification filter to apply to lookups.
  37. * @param min_filter Minification filter to apply to lookups.
  38. * @param mipmap_mode Mipmap filter to apply to lookups.
  39. * @param address_mode_u Addressing mode for U-coordinates outside `[0, 1)`.
  40. * @param address_mode_v Addressing mode for V-coordinates outside `[0, 1)`.
  41. * @param address_mode_w Addressing mode for W-coordinates outside `[0, 1)`.
  42. * @param mip_lod_bias Bias to be added to mipmap LOD calculation.
  43. * @param max_anisotropy Anisotropy clamp value.
  44. * @param compare_enabled `true` to enable comparison against a reference value during lookups, `false` otherwise.
  45. * @param compare_op Comparison operator to apply to fetched data, if compare is enabled.
  46. * @param min_lod Minimum clamp value of the computed LOD.
  47. * @param max_lod Maximum clamp value of the computed LOD.
  48. * @param border_color Border color used for texture lookups.
  49. */
  50. explicit sampler
  51. (
  52. sampler_filter mag_filter = sampler_filter::linear,
  53. sampler_filter min_filter = sampler_filter::nearest,
  54. sampler_mipmap_mode mipmap_mode = sampler_mipmap_mode::linear,
  55. sampler_address_mode address_mode_u = sampler_address_mode::repeat,
  56. sampler_address_mode address_mode_v = sampler_address_mode::repeat,
  57. sampler_address_mode address_mode_w = sampler_address_mode::repeat,
  58. float mip_lod_bias = 0.0f,
  59. float max_anisotropy = 0.0f,
  60. bool compare_enabled = false,
  61. gl::compare_op compare_op = gl::compare_op::less,
  62. float min_lod = -1000.0f,
  63. float max_lod = 1000.0f,
  64. const std::array<float, 4>& border_color = {0.0f, 0.0f, 0.0f, 0.0f}
  65. );
  66. /// Destroys a sampler object.
  67. ~sampler();
  68. sampler(const sampler&) = delete;
  69. sampler(sampler&&) = delete;
  70. sampler& operator=(const sampler&) = delete;
  71. sampler& operator=(sampler&&) = delete;
  72. /**
  73. * Sets the magnification filter to apply to lookups.
  74. *
  75. * @param filter Magnification filter to apply to lookups.
  76. */
  77. void set_mag_filter(sampler_filter filter);
  78. /**
  79. * Sets the minification filter to apply to lookups.
  80. *
  81. * @param filter Minification filter to apply to lookups.
  82. */
  83. void set_min_filter(sampler_filter filter);
  84. /**
  85. * Sets the mipmap filter to apply to lookups.
  86. *
  87. * @param mode Mipmap filter to apply to lookups.
  88. */
  89. void set_mipmap_mode(sampler_mipmap_mode mode);
  90. /**
  91. * Sets the addressing mode for U-coordinates outside `[0, 1)`.
  92. *
  93. * @param Addressing mode for U-coordinates outside `[0, 1)`.
  94. */
  95. void set_address_mode_u(sampler_address_mode mode);
  96. /**
  97. * Sets the addressing mode for V-coordinates outside `[0, 1)`.
  98. *
  99. * @param Addressing mode for V-coordinates outside `[0, 1)`.
  100. */
  101. void set_address_mode_v(sampler_address_mode mode);
  102. /**
  103. * Sets the addressing mode for W-coordinates outside `[0, 1)`.
  104. *
  105. * @param Addressing mode for W-coordinates outside `[0, 1)`.
  106. */
  107. void set_address_mode_w(sampler_address_mode mode);
  108. /**
  109. * Sets the bias to be added to mipmap LOD calculation.
  110. *
  111. * @param bias Bias to be added to mipmap LOD calculation.
  112. */
  113. void set_mip_lod_bias(float bias);
  114. /**
  115. * Sets the anisotropy clamp value.
  116. *
  117. * @param anisotropy Anisotropy clamp value.
  118. */
  119. void set_max_anisotropy(float anisotropy);
  120. /**
  121. * Enables or disables a comparison against a reference value during lookups.
  122. *
  123. * @param enabled `true` to enable comparison against a reference value during lookups, `false` otherwise.
  124. */
  125. void set_compare_enabled(bool enabled);
  126. /**
  127. * Sets the comparison operator to apply to fetched data, if compare is enabled.
  128. *
  129. * @param op Comparison operator to apply to fetched data, if compare is enabled.
  130. */
  131. void set_compare_op(gl::compare_op op);
  132. /**
  133. * Sets the minimum clamp value of the computed LOD.
  134. *
  135. * @param lod Minimum clamp value of the computed LOD.
  136. */
  137. void set_min_lod(float lod);
  138. /**
  139. * Sets the maximum clamp value of the computed LOD.
  140. *
  141. * @param lod Maximum clamp value of the computed LOD.
  142. */
  143. void set_max_lod(float lod);
  144. /**
  145. * Sets the border color used for texture lookups.
  146. *
  147. * @param color Border color used for texture lookups.
  148. */
  149. void set_border_color(const std::array<float, 4>& color);
  150. /// Returns the magnification filter to apply to lookups.
  151. [[nodiscard]] inline constexpr sampler_filter get_mag_filter() const noexcept
  152. {
  153. return m_mag_filter;
  154. }
  155. /// Returns the minification filter to apply to lookups.
  156. [[nodiscard]] inline constexpr sampler_filter get_min_filter() const noexcept
  157. {
  158. return m_min_filter;
  159. }
  160. /// Returns the mipmap filter to apply to lookups.
  161. [[nodiscard]] inline constexpr sampler_mipmap_mode get_mipmap_mode() const noexcept
  162. {
  163. return m_mipmap_mode;
  164. }
  165. /// Returns the addressing mode for U-coordinates outside `[0, 1)`.
  166. [[nodiscard]] inline constexpr sampler_address_mode get_address_mode_u() const noexcept
  167. {
  168. return m_address_mode_u;
  169. }
  170. /// Returns the addressing mode for V-coordinates outside `[0, 1)`.
  171. [[nodiscard]] inline constexpr sampler_address_mode get_address_mode_v() const noexcept
  172. {
  173. return m_address_mode_v;
  174. }
  175. /// Returns the addressing mode for W-coordinates outside `[0, 1)`.
  176. [[nodiscard]] inline constexpr sampler_address_mode get_address_mode_w() const noexcept
  177. {
  178. return m_address_mode_w;
  179. }
  180. /// Returns the bias to be added to mipmap LOD calculation.
  181. [[nodiscard]] inline constexpr float get_mip_lod_bias() const noexcept
  182. {
  183. return m_mip_lod_bias;
  184. }
  185. /// Returns the anisotropy clamp value.
  186. [[nodiscard]] inline constexpr float get_max_anisotropy() const noexcept
  187. {
  188. return m_max_anisotropy;
  189. }
  190. /// Returns `true` if comparison against a reference value during lookups is enabled, `false` otherwise.
  191. [[nodiscard]] inline constexpr float get_compare_enabled() const noexcept
  192. {
  193. return m_compare_enabled;
  194. }
  195. /// Returns the comparison operator to apply to fetched data, if compare is enabled.
  196. [[nodiscard]] inline constexpr compare_op get_compare_op() const noexcept
  197. {
  198. return m_compare_op;
  199. }
  200. /// Returns the minimum clamp value of the computed LOD.
  201. [[nodiscard]] inline constexpr float get_min_lod() const noexcept
  202. {
  203. return m_min_lod;
  204. }
  205. /// Returns the maximum clamp value of the computed LOD.
  206. [[nodiscard]] inline constexpr float get_max_lod() const noexcept
  207. {
  208. return m_max_lod;
  209. }
  210. /// Returns the border color used for texture lookups.
  211. [[nodiscard]] inline constexpr const std::array<float, 4>& get_border_color() const noexcept
  212. {
  213. return m_border_color;
  214. }
  215. private:
  216. friend class pipeline;
  217. friend class gl_shader_texture_1d;
  218. friend class gl_shader_texture_2d;
  219. friend class gl_shader_texture_3d;
  220. friend class gl_shader_texture_cube;
  221. unsigned int m_gl_named_sampler{0};
  222. sampler_filter m_mag_filter{sampler_filter::linear};
  223. sampler_filter m_min_filter{sampler_filter::nearest};
  224. sampler_mipmap_mode m_mipmap_mode{sampler_mipmap_mode::linear};
  225. sampler_address_mode m_address_mode_u{sampler_address_mode::repeat};
  226. sampler_address_mode m_address_mode_v{sampler_address_mode::repeat};
  227. sampler_address_mode m_address_mode_w{sampler_address_mode::repeat};
  228. float m_mip_lod_bias{0.0f};
  229. float m_max_anisotropy{0.0f};
  230. bool m_compare_enabled{false};
  231. gl::compare_op m_compare_op{gl::compare_op::less};
  232. float m_min_lod{-1000.0f};
  233. float m_max_lod{1000.0f};
  234. std::array<float, 4> m_border_color{0.0f, 0.0f, 0.0f, 0.0f};
  235. };
  236. } // namespace gl
  237. #endif // ANTKEEPER_GL_SAMPLER_HPP