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

145 lines
6.5 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 "game/graphics.hpp"
  20. #include "gl/framebuffer.hpp"
  21. #include "gl/texture-2d.hpp"
  22. #include "gl/texture-wrapping.hpp"
  23. #include "gl/texture-filter.hpp"
  24. #include "debug/logger.hpp"
  25. namespace game {
  26. namespace graphics {
  27. static void resize_framebuffer_attachment(gl::texture_2d& texture, const int2& resolution);
  28. void create_framebuffers(game::context& ctx)
  29. {
  30. ctx.logger->push_task("Creating framebuffers");
  31. // Load render resolution scale from config
  32. ctx.render_resolution_scale = 1.0f;
  33. if (ctx.config->contains("render_resolution"))
  34. ctx.render_resolution_scale = (*ctx.config)["render_resolution"].get<float>();
  35. // Calculate render resolution
  36. const int2& viewport_dimensions = ctx.app->get_viewport_dimensions();
  37. ctx.render_resolution = {static_cast<int>(viewport_dimensions.x * ctx.render_resolution_scale + 0.5f), static_cast<int>(viewport_dimensions.y * ctx.render_resolution_scale + 0.5f)};
  38. // Create HDR framebuffer (32F color, 32F depth)
  39. ctx.hdr_color_texture = new gl::texture_2d(ctx.render_resolution.x, ctx.render_resolution.y, gl::pixel_type::float_32, gl::pixel_format::rgb);
  40. ctx.hdr_color_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  41. ctx.hdr_color_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  42. ctx.hdr_color_texture->set_max_anisotropy(0.0f);
  43. ctx.hdr_depth_texture = new gl::texture_2d(ctx.render_resolution.x, ctx.render_resolution.y, gl::pixel_type::float_32, gl::pixel_format::ds);
  44. ctx.hdr_depth_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  45. ctx.hdr_depth_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  46. ctx.hdr_depth_texture->set_max_anisotropy(0.0f);
  47. ctx.hdr_framebuffer = new gl::framebuffer(ctx.render_resolution.x, ctx.render_resolution.y);
  48. ctx.hdr_framebuffer->attach(gl::framebuffer_attachment_type::color, ctx.hdr_color_texture);
  49. ctx.hdr_framebuffer->attach(gl::framebuffer_attachment_type::depth, ctx.hdr_depth_texture);
  50. ctx.hdr_framebuffer->attach(gl::framebuffer_attachment_type::stencil, ctx.hdr_depth_texture);
  51. // Calculate bloom resolution
  52. int2 bloom_resolution = ctx.render_resolution / 2;
  53. // Create bloom framebuffer (16F color, no depth)
  54. ctx.bloom_color_texture = new gl::texture_2d(bloom_resolution.x, bloom_resolution.y, gl::pixel_type::float_16, gl::pixel_format::rgb);
  55. ctx.bloom_color_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  56. ctx.bloom_color_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  57. ctx.bloom_color_texture->set_max_anisotropy(0.0f);
  58. ctx.bloom_framebuffer = new gl::framebuffer(bloom_resolution.x, bloom_resolution.y);
  59. ctx.bloom_framebuffer->attach(gl::framebuffer_attachment_type::color, ctx.bloom_color_texture);
  60. // Load shadow map resolution from config
  61. int shadow_map_resolution = 4096;
  62. if (ctx.config->contains("shadow_map_resolution"))
  63. shadow_map_resolution = (*ctx.config)["shadow_map_resolution"].get<int>();
  64. // Create shadow map framebuffer
  65. ctx.shadow_map_depth_texture = new gl::texture_2d(shadow_map_resolution, shadow_map_resolution, gl::pixel_type::float_32, gl::pixel_format::d);
  66. ctx.shadow_map_depth_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  67. ctx.shadow_map_depth_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  68. ctx.shadow_map_depth_texture->set_max_anisotropy(0.0f);
  69. ctx.shadow_map_framebuffer = new gl::framebuffer(shadow_map_resolution, shadow_map_resolution);
  70. ctx.shadow_map_framebuffer->attach(gl::framebuffer_attachment_type::depth, ctx.shadow_map_depth_texture);
  71. ctx.logger->pop_task(EXIT_SUCCESS);
  72. }
  73. void destroy_framebuffers(game::context& ctx)
  74. {
  75. ctx.logger->push_task("Destroying framebuffers");
  76. // Delete HDR framebuffer and its attachments
  77. delete ctx.hdr_framebuffer;
  78. ctx.hdr_framebuffer = nullptr;
  79. delete ctx.hdr_color_texture;
  80. ctx.hdr_color_texture = nullptr;
  81. delete ctx.hdr_depth_texture;
  82. ctx.hdr_depth_texture = nullptr;
  83. // Delete bloom framebuffer and its attachments
  84. delete ctx.bloom_framebuffer;
  85. ctx.bloom_framebuffer = nullptr;
  86. delete ctx.bloom_color_texture;
  87. ctx.bloom_color_texture = nullptr;
  88. // Delete shadow map framebuffer and its attachments
  89. delete ctx.shadow_map_framebuffer;
  90. ctx.shadow_map_framebuffer = nullptr;
  91. delete ctx.shadow_map_depth_texture;
  92. ctx.shadow_map_depth_texture = nullptr;
  93. ctx.logger->pop_task(EXIT_SUCCESS);
  94. }
  95. void change_render_resolution(game::context& ctx, float scale)
  96. {
  97. ctx.logger->push_task("Changing render resolution");
  98. // Update render resolution scale
  99. ctx.render_resolution_scale = scale;
  100. // Recalculate render resolution
  101. const int2& viewport_dimensions = ctx.app->get_viewport_dimensions();
  102. ctx.render_resolution = {static_cast<int>(viewport_dimensions.x * ctx.render_resolution_scale + 0.5f), static_cast<int>(viewport_dimensions.y * ctx.render_resolution_scale + 0.5f)};
  103. // Resize HDR framebuffer and attachments
  104. ctx.hdr_framebuffer->resize({ctx.render_resolution.x, ctx.render_resolution.y});
  105. resize_framebuffer_attachment(*ctx.hdr_color_texture, ctx.render_resolution);
  106. resize_framebuffer_attachment(*ctx.hdr_depth_texture, ctx.render_resolution);
  107. // Recalculate bloom resolution
  108. int2 bloom_resolution = ctx.render_resolution / 2;
  109. // Resize bloom framebuffer and attachments
  110. ctx.bloom_framebuffer->resize({bloom_resolution.x, bloom_resolution.y});
  111. resize_framebuffer_attachment(*ctx.bloom_color_texture, bloom_resolution);
  112. ctx.logger->pop_task(EXIT_SUCCESS);
  113. }
  114. void resize_framebuffer_attachment(gl::texture_2d& texture, const int2& resolution)
  115. {
  116. texture.resize(resolution.x, resolution.y, texture.get_pixel_type(), texture.get_pixel_format(), texture.get_color_space(), nullptr);
  117. }
  118. } // namespace graphics
  119. } // namespace game