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

122 lines
3.5 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  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_RENDER_BLOOM_PASS_HPP
  20. #define ANTKEEPER_RENDER_BLOOM_PASS_HPP
  21. #include <engine/render/pass.hpp>
  22. #include <engine/gl/shader-template.hpp>
  23. #include <engine/gl/shader-program.hpp>
  24. #include <engine/gl/shader-variable.hpp>
  25. #include <engine/gl/vertex-buffer.hpp>
  26. #include <engine/gl/vertex-array.hpp>
  27. #include <engine/gl/texture.hpp>
  28. #include <engine/gl/sampler.hpp>
  29. #include <memory>
  30. #include <functional>
  31. class resource_manager;
  32. namespace render {
  33. /**
  34. * Physically-based bloom render pass.
  35. *
  36. * @see Jimenez, J. (2014). Next generation post processing in call of duty advanced warfare. SIGGRAPH Advances in Real-Time Rendering.
  37. * @see https://learnopengl.com/Guest-Articles/2022/Phys.-Based-Bloom
  38. */
  39. class bloom_pass: public pass
  40. {
  41. public:
  42. /**
  43. * Constructs a bloom pass.
  44. *
  45. * @param pipeline Graphics pipeline.
  46. * @param resource_manager Resource manager.
  47. */
  48. bloom_pass(gl::pipeline* pipeline, resource_manager* resource_manager);
  49. /**
  50. * Renders a bloom texture.
  51. *
  52. * @param ctx Render context.
  53. * @param queue Render queue.
  54. */
  55. void render(render::context& ctx) override;
  56. /**
  57. * Resizes the mip chain resolution according to the resolution of the source texture.
  58. */
  59. void resize();
  60. /**
  61. * Sets the bloom source texture.
  62. *
  63. * @param texture Bloom source texture.
  64. */
  65. void set_source_texture(std::shared_ptr<gl::texture_2d> texture);
  66. /**
  67. * Sets the mip chain length. A length of `1` indicates a single stage bloom.
  68. *
  69. * @param length Mip chain length.
  70. */
  71. void set_mip_chain_length(unsigned int length);
  72. /**
  73. * Sets the upsample filter radius.
  74. *
  75. * @param radius Upsample filter radius, in texture coordinates.
  76. */
  77. void set_filter_radius(float radius);
  78. /**
  79. * Returns the texture containing the bloom result.
  80. */
  81. [[nodiscard]] inline std::shared_ptr<gl::texture_2d> get_bloom_texture() const
  82. {
  83. return m_target_textures.empty() ? nullptr : m_target_textures.front();
  84. }
  85. private:
  86. void rebuild_mip_chain();
  87. void correct_filter_radius();
  88. void rebuild_command_buffer();
  89. std::shared_ptr<gl::texture_2d> m_source_texture;
  90. std::shared_ptr<gl::image_2d> m_target_image;
  91. std::vector<std::shared_ptr<gl::texture_2d>> m_target_textures;
  92. std::vector<std::shared_ptr<gl::framebuffer>> m_target_framebuffers;
  93. std::unique_ptr<gl::shader_program> m_downsample_karis_shader;
  94. std::unique_ptr<gl::shader_program> m_downsample_shader;
  95. std::unique_ptr<gl::shader_program> m_upsample_shader;
  96. std::shared_ptr<gl::sampler> m_sampler;
  97. std::unique_ptr<gl::vertex_array> m_vertex_array;
  98. unsigned int m_mip_chain_length{0};
  99. float m_filter_radius{0.005f};
  100. math::fvec2 m_corrected_filter_radius{0.005f, 0.005f};
  101. std::vector<std::function<void()>> m_command_buffer;
  102. };
  103. } // namespace render
  104. #endif // ANTKEEPER_RENDER_BLOOM_PASS_HPP