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

182 lines
7.7 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. #include "utility/timestamp.hpp"
  26. #include <glad/glad.h>
  27. #include <stb/stb_image_write.h>
  28. #include <thread>
  29. #include <filesystem>
  30. namespace game {
  31. namespace graphics {
  32. static void resize_framebuffer_attachment(gl::texture_2d& texture, const int2& resolution);
  33. void create_framebuffers(game::context& ctx)
  34. {
  35. ctx.logger->push_task("Creating framebuffers");
  36. // Load render resolution scale from config
  37. ctx.render_resolution_scale = 1.0f;
  38. if (ctx.config->contains("render_resolution"))
  39. ctx.render_resolution_scale = (*ctx.config)["render_resolution"].get<float>();
  40. // Calculate render resolution
  41. const int2& viewport_dimensions = ctx.app->get_viewport_dimensions();
  42. 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)};
  43. // Create HDR framebuffer (32F color, 32F depth)
  44. 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);
  45. ctx.hdr_color_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  46. ctx.hdr_color_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  47. ctx.hdr_color_texture->set_max_anisotropy(0.0f);
  48. 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);
  49. ctx.hdr_depth_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  50. ctx.hdr_depth_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  51. ctx.hdr_depth_texture->set_max_anisotropy(0.0f);
  52. ctx.hdr_framebuffer = new gl::framebuffer(ctx.render_resolution.x, ctx.render_resolution.y);
  53. ctx.hdr_framebuffer->attach(gl::framebuffer_attachment_type::color, ctx.hdr_color_texture);
  54. ctx.hdr_framebuffer->attach(gl::framebuffer_attachment_type::depth, ctx.hdr_depth_texture);
  55. ctx.hdr_framebuffer->attach(gl::framebuffer_attachment_type::stencil, ctx.hdr_depth_texture);
  56. // Calculate bloom resolution
  57. int2 bloom_resolution = ctx.render_resolution / 2;
  58. // Create bloom framebuffer (16F color, no depth)
  59. ctx.bloom_color_texture = new gl::texture_2d(bloom_resolution.x, bloom_resolution.y, gl::pixel_type::float_16, gl::pixel_format::rgb);
  60. ctx.bloom_color_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  61. ctx.bloom_color_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  62. ctx.bloom_color_texture->set_max_anisotropy(0.0f);
  63. ctx.bloom_framebuffer = new gl::framebuffer(bloom_resolution.x, bloom_resolution.y);
  64. ctx.bloom_framebuffer->attach(gl::framebuffer_attachment_type::color, ctx.bloom_color_texture);
  65. // Load shadow map resolution from config
  66. int shadow_map_resolution = 4096;
  67. if (ctx.config->contains("shadow_map_resolution"))
  68. shadow_map_resolution = (*ctx.config)["shadow_map_resolution"].get<int>();
  69. // Create shadow map framebuffer
  70. ctx.shadow_map_depth_texture = new gl::texture_2d(shadow_map_resolution, shadow_map_resolution, gl::pixel_type::float_32, gl::pixel_format::d);
  71. ctx.shadow_map_depth_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  72. ctx.shadow_map_depth_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  73. ctx.shadow_map_depth_texture->set_max_anisotropy(0.0f);
  74. ctx.shadow_map_framebuffer = new gl::framebuffer(shadow_map_resolution, shadow_map_resolution);
  75. ctx.shadow_map_framebuffer->attach(gl::framebuffer_attachment_type::depth, ctx.shadow_map_depth_texture);
  76. ctx.logger->pop_task(EXIT_SUCCESS);
  77. }
  78. void destroy_framebuffers(game::context& ctx)
  79. {
  80. ctx.logger->push_task("Destroying framebuffers");
  81. // Delete HDR framebuffer and its attachments
  82. delete ctx.hdr_framebuffer;
  83. ctx.hdr_framebuffer = nullptr;
  84. delete ctx.hdr_color_texture;
  85. ctx.hdr_color_texture = nullptr;
  86. delete ctx.hdr_depth_texture;
  87. ctx.hdr_depth_texture = nullptr;
  88. // Delete bloom framebuffer and its attachments
  89. delete ctx.bloom_framebuffer;
  90. ctx.bloom_framebuffer = nullptr;
  91. delete ctx.bloom_color_texture;
  92. ctx.bloom_color_texture = nullptr;
  93. // Delete shadow map framebuffer and its attachments
  94. delete ctx.shadow_map_framebuffer;
  95. ctx.shadow_map_framebuffer = nullptr;
  96. delete ctx.shadow_map_depth_texture;
  97. ctx.shadow_map_depth_texture = nullptr;
  98. ctx.logger->pop_task(EXIT_SUCCESS);
  99. }
  100. void change_render_resolution(game::context& ctx, float scale)
  101. {
  102. ctx.logger->push_task("Changing render resolution");
  103. // Update render resolution scale
  104. ctx.render_resolution_scale = scale;
  105. // Recalculate render resolution
  106. const int2& viewport_dimensions = ctx.app->get_viewport_dimensions();
  107. 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)};
  108. // Resize HDR framebuffer and attachments
  109. ctx.hdr_framebuffer->resize({ctx.render_resolution.x, ctx.render_resolution.y});
  110. resize_framebuffer_attachment(*ctx.hdr_color_texture, ctx.render_resolution);
  111. resize_framebuffer_attachment(*ctx.hdr_depth_texture, ctx.render_resolution);
  112. // Recalculate bloom resolution
  113. int2 bloom_resolution = ctx.render_resolution / 2;
  114. // Resize bloom framebuffer and attachments
  115. ctx.bloom_framebuffer->resize({bloom_resolution.x, bloom_resolution.y});
  116. resize_framebuffer_attachment(*ctx.bloom_color_texture, bloom_resolution);
  117. ctx.logger->pop_task(EXIT_SUCCESS);
  118. }
  119. void resize_framebuffer_attachment(gl::texture_2d& texture, const int2& resolution)
  120. {
  121. texture.resize(resolution.x, resolution.y, texture.get_pixel_type(), texture.get_pixel_format(), texture.get_color_space(), nullptr);
  122. }
  123. void save_screenshot(game::context& ctx)
  124. {
  125. // Determine screenshot path
  126. std::string filename = "antkeeper-" + timestamp() + ".png";
  127. std::filesystem::path path = ctx.config_path / "gallery" / filename;
  128. ctx.logger->push_task("Saving screenshot to \"" + path.string() + "\"");
  129. const int2& viewport_dimensions = ctx.app->get_viewport_dimensions();
  130. // Allocate image
  131. std::shared_ptr<image> frame = std::make_shared<image>();
  132. frame->format(1, 3);
  133. frame->resize(viewport_dimensions.x, viewport_dimensions.y);
  134. // Read pixel data from backbuffer into image
  135. glReadBuffer(GL_BACK);
  136. glReadPixels(0, 0, viewport_dimensions.x, viewport_dimensions.y, GL_RGB, GL_UNSIGNED_BYTE, frame->get_pixels());
  137. // Write screenshot file in separate thread
  138. std::thread
  139. (
  140. [frame, path]
  141. {
  142. stbi_flip_vertically_on_write(1);
  143. stbi_write_png(path.string().c_str(), frame->get_width(), frame->get_height(), frame->get_channel_count(), frame->get_pixels(), frame->get_width() * frame->get_channel_count());
  144. }
  145. ).detach();
  146. ctx.logger->pop_task(EXIT_SUCCESS);
  147. }
  148. } // namespace graphics
  149. } // namespace game