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

435 lines
13 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_PIPELINE_HPP
  20. #define ANTKEEPER_GL_PIPELINE_HPP
  21. #include <engine/gl/pipeline-viewport-state.hpp>
  22. #include <engine/gl/pipeline-rasterization-state.hpp>
  23. #include <engine/gl/pipeline-depth-stencil-state.hpp>
  24. #include <engine/gl/pipeline-input-assembly-state.hpp>
  25. #include <engine/gl/pipeline-vertex-input-state.hpp>
  26. #include <engine/gl/pipeline-color-blend-state.hpp>
  27. #include <engine/gl/vertex-array.hpp>
  28. #include <engine/gl/vertex-buffer.hpp>
  29. #include <engine/gl/clear-value.hpp>
  30. #include <engine/gl/framebuffer.hpp>
  31. #include <engine/gl/shader-program.hpp>
  32. #include <engine/gl/clear-bits.hpp>
  33. #include <engine/gl/stencil-face-bits.hpp>
  34. #include <array>
  35. #include <cstdint>
  36. #include <span>
  37. namespace app { class sdl_window_manager; }
  38. namespace gl {
  39. /**
  40. * Graphics pipeline interface.
  41. */
  42. class pipeline
  43. {
  44. public:
  45. /**
  46. * Constructs a pipeline.
  47. */
  48. pipeline();
  49. /// @name Vertex input state
  50. /// @{
  51. /**
  52. * Sets the vertex input.
  53. *
  54. * @param vertex_bindings Vertex bindings.
  55. * @param vertex_attributes Vertex attributes.
  56. */
  57. // void set_vertex_input(std::span<const vertex_input_binding> vertex_bindings, std::span<const vertex_input_attribute> vertex_attributes);
  58. void bind_framebuffer(const gl::framebuffer* framebuffer);
  59. void bind_shader_program(const gl::shader_program* shader_program);
  60. /**
  61. * Binds a vertex array.
  62. *
  63. * @param array Vertex array to bind.
  64. */
  65. void bind_vertex_array(const vertex_array* array);
  66. /**
  67. * Binds vertex buffers.
  68. *
  69. * @param first_binding Index of the first vertex input binding.
  70. * @param buffers Sequence of buffers to bind.
  71. * @param offsets Sequence of byte offsets into each buffer.
  72. * @param strides Sequence of byte strides between consecutive elements within each buffer.
  73. */
  74. void bind_vertex_buffers(std::uint32_t first_binding, std::span<const vertex_buffer* const> buffers, std::span<const std::size_t> offsets, std::span<const std::size_t> strides);
  75. /// @}
  76. /// @name Input assembly state
  77. /// @{
  78. /**
  79. * Sets the primitive topology to use for drawing.
  80. *
  81. * @param topology Primitive topology to use for drawing.
  82. */
  83. void set_primitive_topology(primitive_topology topology);
  84. /**
  85. * Controls whether a special vertex index value is treated as restarting the assembly of primitives.
  86. *
  87. * @param enabled `true` if a special vertex index value should restart the assembly of primitives, `false` otherwise.
  88. */
  89. void set_primitive_restart_enabled(bool enabled);
  90. /// @}
  91. /// @name Viewport state
  92. /// @{
  93. /**
  94. * Sets one or more viewports.
  95. *
  96. * @param first_viewport Index of the first viewport to set.
  97. * @param viewports Sequence of viewports.
  98. *
  99. * @except std::out_of_range Viewport index out of range.
  100. *
  101. * @warning Currently only a single viewport is supported.
  102. */
  103. void set_viewport(std::uint32_t first_viewport, std::span<const viewport> viewports);
  104. /**
  105. * Sets one or more scissor regions.
  106. *
  107. * @param first_scissor Index of the first scissor region to set.
  108. * @param scissors Sequence of scissor regions.
  109. *
  110. * @except std::out_of_range Scissor region index out of range.
  111. *
  112. * @warning Currently only a single scissor region is supported.
  113. */
  114. void set_scissor(std::uint32_t first_scissor, std::span<const scissor_region> scissors);
  115. /// @}
  116. /// @name Rasterizer state
  117. /// @{
  118. /**
  119. * Controls whether primitives are discarded before the rasterization stage.
  120. *
  121. * @param enabled `true` if primitives should be discarded before the rasterization stage, `false` otherwise.
  122. */
  123. void set_rasterizer_discard_enabled(bool enabled);
  124. /**
  125. * Sets the polygon rasterization mode.
  126. *
  127. * @param mode Polygon rasterization mode.
  128. */
  129. void set_fill_mode(fill_mode mode);
  130. /**
  131. * Sets the triangle culling mode.
  132. *
  133. * @param mode Triangle culling mode.
  134. */
  135. void set_cull_mode(cull_mode mode);
  136. /**
  137. * Sets the front-facing triangle orientation.
  138. *
  139. * @param face Front-facing triangle orientation.
  140. */
  141. void set_front_face(front_face face);
  142. /**
  143. * Controls whether to bias fragment depth values.
  144. *
  145. * @param enabled `true` if fragment depth values should be biased, `false` otherwise.
  146. */
  147. void set_depth_bias_enabled(bool enabled);
  148. /**
  149. * Sets depth bias factors.
  150. *
  151. * @param constant_factor Scalar factor controlling the constant depth value added to each fragment.
  152. * @param slope_factor Scalar factor applied to a fragment's slope in depth bias calculations.
  153. */
  154. void set_depth_bias_factors(float constant_factor, float slope_factor);
  155. /**
  156. * Controls whether depth clamping is enabled.
  157. *
  158. * @param enabled `true` if depth clamping should be enabled, `false` otherwise.
  159. */
  160. void set_depth_clamp_enabled(bool enabled);
  161. /**
  162. * Enables or disables scissor testing.
  163. *
  164. * @param enabled `true` if scissor testing should be enabled, `false` otherwise.
  165. */
  166. void set_scissor_test_enabled(bool enabled);
  167. /**
  168. * Sets the vertex to be used as the source of data for flat-shaded varyings.
  169. *
  170. * @param mode Provoking vertex mode.
  171. */
  172. void set_provoking_vertex_mode(provoking_vertex_mode mode);
  173. /**
  174. * Sets the the diameter of rasterized points.
  175. *
  176. * @param size Point size.
  177. */
  178. void set_point_size(float size);
  179. /**
  180. * Sets the width of rasterized lines.
  181. *
  182. * @param width Width of rasterized line segments.
  183. */
  184. void set_line_width(float width);
  185. /// @}
  186. /// @name Depth/stencil state
  187. /// @{
  188. /**
  189. * Controls whether depth testing is enabled.
  190. *
  191. * @param enabled `true` if depth testing should be enabled, `false` otherwise.
  192. */
  193. void set_depth_test_enabled(bool enabled);
  194. /**
  195. * Controls whether depth writes are enabled.
  196. *
  197. * @param enabled `true` if depth writes should be enabled when depth testing is enabled, `false` otherwise.
  198. *
  199. * @note Depth writes are always disabled when depth testing is disabled.
  200. */
  201. void set_depth_write_enabled(bool enabled);
  202. /**
  203. * Sets the depth comparison operator.
  204. *
  205. * @param compare_op Comparison operator to use in the depth comparison step of the depth test.
  206. */
  207. void set_depth_compare_op(gl::compare_op compare_op);
  208. /**
  209. * Controls whether stencil testing is enabled.
  210. *
  211. * @param enabled `true` if stencil testing should be enabled, `false` otherwise.
  212. */
  213. void set_stencil_test_enabled(bool enabled);
  214. /**
  215. * Sets the stencil operations.
  216. *
  217. * @param face_mask Bit mask specifying the set of stencil states for which to update the stencil operation.
  218. * @param fail_op Action performed on samples that fail the stencil test.
  219. * @param pass_op Action performed on samples that pass both the depth and stencil tests.
  220. * @param depth_fail_op Action performed on samples that pass the stencil test and fail the depth test.
  221. * @param compare_op Comparison operator used in the stencil test.
  222. */
  223. void set_stencil_op(std::uint8_t face_mask, stencil_op fail_op, stencil_op pass_op, stencil_op depth_fail_op, gl::compare_op compare_op);
  224. /**
  225. * Sets the stencil compare mask.
  226. *
  227. * @param face_mask Bit mask specifying the set of stencil states for which to update the compare mask.
  228. * @param compare_mask New value to use as the stencil compare mask.
  229. */
  230. void set_stencil_compare_mask(std::uint8_t face_mask, std::uint32_t compare_mask);
  231. /**
  232. * Sets the stencil reference value.
  233. *
  234. * @param face_mask Bit mask specifying the set of stencil states for which to update the reference value.
  235. * @param reference New value to use as the stencil reference value.
  236. */
  237. void set_stencil_reference(std::uint8_t face_mask, std::uint32_t reference);
  238. /**
  239. * Sets the stencil write mask.
  240. *
  241. * @param face_mask Bit mask specifying the set of stencil states for which to update the write mask.
  242. * @param write_mask New value to use as the stencil write mask.
  243. */
  244. void set_stencil_write_mask(std::uint8_t face_mask, std::uint32_t write_mask);
  245. /// @}
  246. /// @name Color blend state
  247. /// @{
  248. /**
  249. * Controls whether whether logical operations are enabled.
  250. *
  251. * @param enabled `true` if logical operations should be enabled, `false` otherwise.
  252. */
  253. void set_logic_op_enabled(bool enabled);
  254. /**
  255. * Selects which logical operation to apply.
  256. *
  257. * @param logic_op Logical operation to apply.
  258. */
  259. void set_logic_op(gl::logic_op logic_op);
  260. /**
  261. * Controls whether blending is enabled for the corresponding color attachment.
  262. *
  263. * @param enabled `true` if color blending should be enabled, `false` otherwise.
  264. */
  265. void set_color_blend_enabled(bool enabled);
  266. /**
  267. * Sets the color blend factors and operations.
  268. *
  269. * @param equation Color blend factors and operations.
  270. */
  271. void set_color_blend_equation(const color_blend_equation& equation);
  272. /**
  273. * Sets the color write mask.
  274. *
  275. * @param mask Bitmask indicating which of the RGBA components are enabled for writing.
  276. */
  277. void set_color_write_mask(std::uint8_t mask);
  278. /**
  279. * Sets the values of the blend constants.
  280. *
  281. * @param blend_constants RGBA components of the blend constant that are used in blending, depending on the blend factor.
  282. */
  283. void set_blend_constants(const std::array<float, 4>& blend_constants);
  284. /// @}
  285. /// @name Drawing
  286. /// @{
  287. /**
  288. * Draws primitives.
  289. *
  290. * @param vertex_count Number of vertices to draw.
  291. * @param instance_count Number of instances to draw.
  292. * @param first_vertex Index of the first vertex to draw.
  293. * @param first_instance Instance ID of the first instance to draw. (WARNING: not currently supported)
  294. *
  295. * @warning @p first_instance currently not supported.
  296. */
  297. void draw(std::uint32_t vertex_count, std::uint32_t instance_count, std::uint32_t first_vertex, std::uint32_t first_instance);
  298. /**
  299. * Draws primitives with indexed vertices.
  300. *
  301. * @param index_count Number of vertices to draw.
  302. * @param instance_count Number of instances to draw.
  303. * @param first_index Base index within the index buffer.
  304. * @param vertex_offset Value added to the vertex index before indexing into the vertex buffer.
  305. * @param first_instance Instance ID of the first instance to draw. (WARNING: not currently supported)
  306. *
  307. * @warning @p first_instance currently not supported.
  308. */
  309. void draw_indexed(std::uint32_t index_count, std::uint32_t instance_count, std::uint32_t first_index, std::int32_t vertex_offset, std::uint32_t first_instance);
  310. /// @}
  311. /// @name Clear
  312. /// @{
  313. /**
  314. * Clears the color, depth, or stencil buffers of current attachments.
  315. *
  316. * @param mask Bit mask indicating which buffers should be cleared.
  317. * @param value Color, depth, and stencil values with which to fill the cleared attachments.
  318. */
  319. void clear_attachments(std::uint8_t mask, const clear_value& value);
  320. /// @}
  321. /// @name Limitations
  322. /// @{
  323. /// Returns the dimensions of the default framebuffer.
  324. [[nodiscard]] inline constexpr const std::array<std::uint32_t, 2>& get_default_framebuffer_dimensions() const noexcept
  325. {
  326. return m_default_framebuffer_dimensions;
  327. }
  328. /// Returns the maximum number of supported viewports.
  329. [[nodiscard]] inline constexpr std::uint32_t get_max_viewports() const noexcept
  330. {
  331. return m_max_viewports;
  332. }
  333. /// Returns the maximum supported degree of sampler anisotropy.
  334. [[nodiscard]] inline constexpr float get_max_sampler_anisotropy() const noexcept
  335. {
  336. return m_max_sampler_anisotropy;
  337. }
  338. /// @}
  339. private:
  340. friend class app::sdl_window_manager;
  341. /// Changes the reported dimensions of the default framebuffer.
  342. void defaut_framebuffer_resized(std::uint32_t width, std::uint32_t height) noexcept;
  343. void fetch_vertex_input_state();
  344. void fetch_input_assembly_state();
  345. void fetch_viewport_state();
  346. void fetch_rasterization_state();
  347. void fetch_depth_stencil_state();
  348. void fetch_color_blend_state();
  349. void fetch_clear_value();
  350. std::uint32_t m_max_viewports{1};
  351. float m_max_sampler_anisotropy{0.0f};
  352. std::array<std::uint32_t, 2> m_default_framebuffer_dimensions{0, 0};
  353. pipeline_vertex_input_state m_vertex_input_state;
  354. pipeline_input_assembly_state m_input_assembly_state;
  355. pipeline_viewport_state m_viewport_state;
  356. pipeline_rasterization_state m_rasterization_state;
  357. pipeline_depth_stencil_state m_depth_stencil_state;
  358. pipeline_color_blend_state m_color_blend_state;
  359. clear_value m_clear_value;
  360. const framebuffer* m_framebuffer{};
  361. const shader_program* m_shader_program{};
  362. const vertex_array* m_vertex_array{};
  363. };
  364. } // namespace gl
  365. #endif // ANTKEEPER_GL_PIPELINE_HPP