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

173 lines
6.3 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.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-attribute.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. // Define position vertex attribute
  88. gl::vertex_attribute position_attribute;
  89. position_attribute.buffer = quad_vbo;
  90. position_attribute.offset = 0;
  91. position_attribute.stride = vertex_stride;
  92. position_attribute.type = gl::vertex_attribute_type::float_32;
  93. position_attribute.components = 3;
  94. // Bind vertex attributes to VAO
  95. quad_vao->bind(render::vertex_attribute::position, position_attribute);
  96. }
  97. bloom_pass::~bloom_pass()
  98. {
  99. delete cloned_framebuffer;
  100. delete cloned_framebuffer_texture;
  101. delete quad_vao;
  102. delete quad_vbo;
  103. }
  104. void bloom_pass::render(render_context* context) const
  105. {
  106. glDisable(GL_BLEND);
  107. glDisable(GL_DEPTH_TEST);
  108. glDepthMask(GL_FALSE);
  109. glEnable(GL_CULL_FACE);
  110. glCullFace(GL_BACK);
  111. // Determine viewport based on framebuffer resolution
  112. auto viewport = framebuffer->get_dimensions();
  113. rasterizer->set_viewport(0, 0, std::get<0>(viewport), std::get<1>(viewport));
  114. float2 resolution = {std::get<0>(viewport), std::get<1>(viewport)};
  115. // Perform brightness threshold subpass, rendering to the first pingpong fbo
  116. rasterizer->use_framebuffer(*pingpong_framebuffers[0]);
  117. rasterizer->use_program(*threshold_shader);
  118. threshold_shader_image_input->upload(source_texture);
  119. threshold_shader_resolution_input->upload(resolution);
  120. threshold_shader_threshold_input->upload(brightness_threshold);
  121. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  122. // Perform iterative blur subpass
  123. const float2 direction_horizontal = {1, 0};
  124. const float2 direction_vertical = {0, 1};
  125. rasterizer->use_program(*blur_shader);
  126. blur_shader_resolution_input->upload(resolution);
  127. for (int i = 0; i < blur_iterations; ++i)
  128. {
  129. // Perform horizontal blur
  130. rasterizer->use_framebuffer(*pingpong_framebuffers[1]);
  131. blur_shader_image_input->upload(pingpong_textures[0]);
  132. blur_shader_direction_input->upload(direction_horizontal);
  133. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  134. // Perform vertical blur
  135. rasterizer->use_framebuffer(*pingpong_framebuffers[0]);
  136. blur_shader_image_input->upload(pingpong_textures[1]);
  137. blur_shader_direction_input->upload(direction_vertical);
  138. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  139. }
  140. }
  141. void bloom_pass::set_source_texture(const gl::texture_2d* texture)
  142. {
  143. this->source_texture = texture;
  144. }
  145. void bloom_pass::set_brightness_threshold(float threshold)
  146. {
  147. this->brightness_threshold = threshold;
  148. }
  149. void bloom_pass::set_blur_iterations(int iterations)
  150. {
  151. this->blur_iterations = iterations;
  152. }