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

163 lines
6.0 KiB

  1. /*
  2. * Copyright (C) 2021 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. #include "renderer/passes/bloom-pass.hpp"
  20. #include "resources/resource-manager.hpp"
  21. #include "gl/rasterizer.hpp"
  22. #include "gl/framebuffer.hpp"
  23. #include "gl/shader-program.hpp"
  24. #include "gl/shader-input.hpp"
  25. #include "gl/vertex-buffer.hpp"
  26. #include "gl/vertex-array.hpp"
  27. #include "gl/vertex-attribute-type.hpp"
  28. #include "gl/drawing-mode.hpp"
  29. #include "gl/texture-2d.hpp"
  30. #include "gl/texture-wrapping.hpp"
  31. #include "gl/texture-filter.hpp"
  32. #include "renderer/vertex-attributes.hpp"
  33. #include "renderer/render-context.hpp"
  34. #include "math/math.hpp"
  35. #include <cmath>
  36. #include <glad/glad.h>
  37. bloom_pass::bloom_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, resource_manager* resource_manager):
  38. render_pass(rasterizer, framebuffer),
  39. source_texture(nullptr),
  40. brightness_threshold(1.0f),
  41. blur_iterations(1)
  42. {
  43. // Create clone of framebuffer texture
  44. const gl::texture_2d* framebuffer_texture = framebuffer->get_color_attachment();
  45. auto dimensions = framebuffer_texture->get_dimensions();
  46. auto pixel_type = framebuffer_texture->get_pixel_type();
  47. auto pixel_format = framebuffer_texture->get_pixel_format();
  48. auto wrapping = framebuffer_texture->get_wrapping();
  49. auto filters = framebuffer_texture->get_filters();
  50. float max_anisotropy = framebuffer_texture->get_max_anisotropy();
  51. cloned_framebuffer_texture = new gl::texture_2d(std::get<0>(dimensions), std::get<1>(dimensions), pixel_type, pixel_format);
  52. cloned_framebuffer_texture->set_wrapping(std::get<0>(wrapping), std::get<1>(wrapping));
  53. cloned_framebuffer_texture->set_filters(std::get<0>(filters), std::get<1>(filters));
  54. cloned_framebuffer_texture->set_max_anisotropy(max_anisotropy);
  55. // Create clone of framebuffer
  56. cloned_framebuffer = new gl::framebuffer(std::get<0>(dimensions), std::get<1>(dimensions));
  57. cloned_framebuffer->attach(gl::framebuffer_attachment_type::color, cloned_framebuffer_texture);
  58. // Setup pingponging
  59. pingpong_textures[0] = framebuffer_texture;
  60. pingpong_textures[1] = cloned_framebuffer_texture;
  61. pingpong_framebuffers[0] = framebuffer;
  62. pingpong_framebuffers[1] = cloned_framebuffer;
  63. // Load brightness threshold shader
  64. threshold_shader = resource_manager->load<gl::shader_program>("brightness-threshold.glsl");
  65. threshold_shader_image_input = threshold_shader->get_input("image");
  66. threshold_shader_resolution_input = threshold_shader->get_input("resolution");
  67. threshold_shader_threshold_input = threshold_shader->get_input("threshold");
  68. // Load blur shader
  69. blur_shader = resource_manager->load<gl::shader_program>("blur.glsl");
  70. blur_shader_image_input = blur_shader->get_input("image");
  71. blur_shader_resolution_input = blur_shader->get_input("resolution");
  72. blur_shader_direction_input = blur_shader->get_input("direction");
  73. const float vertex_data[] =
  74. {
  75. -1.0f, 1.0f, 0.0f,
  76. -1.0f, -1.0f, 0.0f,
  77. 1.0f, 1.0f, 0.0f,
  78. 1.0f, 1.0f, 0.0f,
  79. -1.0f, -1.0f, 0.0f,
  80. 1.0f, -1.0f, 0.0f
  81. };
  82. std::size_t vertex_size = 3;
  83. std::size_t vertex_stride = sizeof(float) * vertex_size;
  84. std::size_t vertex_count = 6;
  85. quad_vbo = new gl::vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  86. quad_vao = new gl::vertex_array();
  87. quad_vao->bind_attribute(VERTEX_POSITION_LOCATION, *quad_vbo, 3, gl::vertex_attribute_type::float_32, vertex_stride, 0);
  88. }
  89. bloom_pass::~bloom_pass()
  90. {
  91. delete cloned_framebuffer;
  92. delete cloned_framebuffer_texture;
  93. delete quad_vao;
  94. delete quad_vbo;
  95. }
  96. void bloom_pass::render(render_context* context) const
  97. {
  98. glDisable(GL_BLEND);
  99. glDisable(GL_DEPTH_TEST);
  100. glDepthMask(GL_FALSE);
  101. glEnable(GL_CULL_FACE);
  102. glCullFace(GL_BACK);
  103. // Determine viewport based on framebuffer resolution
  104. auto viewport = framebuffer->get_dimensions();
  105. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  106. float2 resolution = {std::get<0>(viewport), std::get<1>(viewport)};
  107. // Perform brightness threshold subpass, rendering to the first pingpong fbo
  108. rasterizer->use_framebuffer(*pingpong_framebuffers[0]);
  109. rasterizer->use_program(*threshold_shader);
  110. threshold_shader_image_input->upload(source_texture);
  111. threshold_shader_resolution_input->upload(resolution);
  112. threshold_shader_threshold_input->upload(brightness_threshold);
  113. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  114. // Perform iterative blur subpass
  115. const float2 direction_horizontal = {1, 0};
  116. const float2 direction_vertical = {0, 1};
  117. rasterizer->use_program(*blur_shader);
  118. blur_shader_resolution_input->upload(resolution);
  119. for (int i = 0; i < blur_iterations; ++i)
  120. {
  121. // Perform horizontal blur
  122. rasterizer->use_framebuffer(*pingpong_framebuffers[1]);
  123. blur_shader_image_input->upload(pingpong_textures[0]);
  124. blur_shader_direction_input->upload(direction_horizontal);
  125. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  126. // Perform vertical blur
  127. rasterizer->use_framebuffer(*pingpong_framebuffers[0]);
  128. blur_shader_image_input->upload(pingpong_textures[1]);
  129. blur_shader_direction_input->upload(direction_vertical);
  130. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  131. }
  132. }
  133. void bloom_pass::set_source_texture(const gl::texture_2d* texture)
  134. {
  135. this->source_texture = texture;
  136. }
  137. void bloom_pass::set_brightness_threshold(float threshold)
  138. {
  139. this->brightness_threshold = threshold;
  140. }
  141. void bloom_pass::set_blur_iterations(int iterations)
  142. {
  143. this->blur_iterations = iterations;
  144. }