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

251 lines
9.8 KiB

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. #include "game/graphics.hpp"
  20. #include "config.hpp"
  21. #include "debug/log.hpp"
  22. #include "gl/framebuffer.hpp"
  23. #include "gl/texture-2d.hpp"
  24. #include "gl/texture-filter.hpp"
  25. #include "gl/texture-wrapping.hpp"
  26. #include "render/passes/bloom-pass.hpp"
  27. #include "render/passes/final-pass.hpp"
  28. #include "render/passes/fxaa-pass.hpp"
  29. #include "render/passes/resample-pass.hpp"
  30. #include <chrono>
  31. #include <filesystem>
  32. #include <format>
  33. #include <glad/glad.h>
  34. #include <stb/stb_image_write.h>
  35. #include <thread>
  36. namespace game {
  37. namespace graphics {
  38. static void reroute_framebuffers(game::context& ctx);
  39. void create_framebuffers(game::context& ctx)
  40. {
  41. debug::log::trace("Creating framebuffers...");
  42. // Calculate render resolution
  43. const int2& viewport_size = ctx.app->get_viewport_size();
  44. ctx.render_resolution = {static_cast<int>(viewport_size.x() * ctx.render_scale + 0.5f), static_cast<int>(viewport_size.y() * ctx.render_scale + 0.5f)};
  45. // Create HDR framebuffer (32F color, 32F depth)
  46. 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);
  47. ctx.hdr_color_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  48. ctx.hdr_color_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  49. ctx.hdr_color_texture->set_max_anisotropy(0.0f);
  50. 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);
  51. ctx.hdr_depth_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  52. ctx.hdr_depth_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  53. ctx.hdr_depth_texture->set_max_anisotropy(0.0f);
  54. ctx.hdr_framebuffer = new gl::framebuffer(ctx.render_resolution.x(), ctx.render_resolution.y());
  55. ctx.hdr_framebuffer->attach(gl::framebuffer_attachment_type::color, ctx.hdr_color_texture);
  56. ctx.hdr_framebuffer->attach(gl::framebuffer_attachment_type::depth, ctx.hdr_depth_texture);
  57. ctx.hdr_framebuffer->attach(gl::framebuffer_attachment_type::stencil, ctx.hdr_depth_texture);
  58. // Create LDR framebuffers (8-bit color, no depth)
  59. ctx.ldr_color_texture_a = new gl::texture_2d(ctx.render_resolution.x(), ctx.render_resolution.y(), gl::pixel_type::uint_8, gl::pixel_format::rgb);
  60. ctx.ldr_color_texture_a->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  61. ctx.ldr_color_texture_a->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  62. ctx.ldr_color_texture_a->set_max_anisotropy(0.0f);
  63. ctx.ldr_framebuffer_a = new gl::framebuffer(ctx.render_resolution.x(), ctx.render_resolution.y());
  64. ctx.ldr_framebuffer_a->attach(gl::framebuffer_attachment_type::color, ctx.ldr_color_texture_a);
  65. ctx.ldr_color_texture_b = new gl::texture_2d(ctx.render_resolution.x(), ctx.render_resolution.y(), gl::pixel_type::uint_8, gl::pixel_format::rgb);
  66. ctx.ldr_color_texture_b->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  67. ctx.ldr_color_texture_b->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  68. ctx.ldr_color_texture_b->set_max_anisotropy(0.0f);
  69. ctx.ldr_framebuffer_b = new gl::framebuffer(ctx.render_resolution.x(), ctx.render_resolution.y());
  70. ctx.ldr_framebuffer_b->attach(gl::framebuffer_attachment_type::color, ctx.ldr_color_texture_b);
  71. // Create shadow map framebuffer
  72. ctx.shadow_map_depth_texture = new gl::texture_2d(ctx.shadow_map_resolution, ctx.shadow_map_resolution, gl::pixel_type::float_32, gl::pixel_format::d);
  73. ctx.shadow_map_depth_texture->set_wrapping(gl::texture_wrapping::extend, gl::texture_wrapping::extend);
  74. ctx.shadow_map_depth_texture->set_filters(gl::texture_min_filter::linear, gl::texture_mag_filter::linear);
  75. ctx.shadow_map_depth_texture->set_max_anisotropy(0.0f);
  76. ctx.shadow_map_framebuffer = new gl::framebuffer(ctx.shadow_map_resolution, ctx.shadow_map_resolution);
  77. ctx.shadow_map_framebuffer->attach(gl::framebuffer_attachment_type::depth, ctx.shadow_map_depth_texture);
  78. debug::log::trace("Created framebuffers");
  79. }
  80. void destroy_framebuffers(game::context& ctx)
  81. {
  82. debug::log::trace("Destroying framebuffers...");
  83. // Delete HDR framebuffer and its attachments
  84. delete ctx.hdr_framebuffer;
  85. ctx.hdr_framebuffer = nullptr;
  86. delete ctx.hdr_color_texture;
  87. ctx.hdr_color_texture = nullptr;
  88. delete ctx.hdr_depth_texture;
  89. ctx.hdr_depth_texture = nullptr;
  90. // Delete LDR framebuffers and attachments
  91. delete ctx.ldr_framebuffer_a;
  92. ctx.ldr_framebuffer_a = nullptr;
  93. delete ctx.ldr_color_texture_a;
  94. ctx.ldr_color_texture_a = nullptr;
  95. delete ctx.ldr_framebuffer_b;
  96. ctx.ldr_framebuffer_b = nullptr;
  97. delete ctx.ldr_color_texture_b;
  98. ctx.ldr_color_texture_b = nullptr;
  99. // Delete shadow map framebuffer and its attachments
  100. delete ctx.shadow_map_framebuffer;
  101. ctx.shadow_map_framebuffer = nullptr;
  102. delete ctx.shadow_map_depth_texture;
  103. ctx.shadow_map_depth_texture = nullptr;
  104. debug::log::trace("Destroyed framebuffers");
  105. }
  106. void change_render_resolution(game::context& ctx, float scale)
  107. {
  108. debug::log::trace("Changing render resolution to {}...", scale);
  109. // Update render resolution scale
  110. ctx.render_scale = scale;
  111. // Recalculate render resolution
  112. const int2& viewport_size = ctx.app->get_viewport_size();
  113. ctx.render_resolution = {static_cast<int>(viewport_size.x() * ctx.render_scale + 0.5f), static_cast<int>(viewport_size.y() * ctx.render_scale + 0.5f)};
  114. // Resize HDR framebuffer and attachments
  115. ctx.hdr_framebuffer->resize({ctx.render_resolution.x(), ctx.render_resolution.y()});
  116. ctx.hdr_color_texture->resize(ctx.render_resolution.x(), ctx.render_resolution.y(), nullptr);
  117. ctx.hdr_depth_texture->resize(ctx.render_resolution.x(), ctx.render_resolution.y(), nullptr);
  118. // Resize LDR framebuffers and attachments
  119. ctx.ldr_framebuffer_a->resize({ctx.render_resolution.x(), ctx.render_resolution.y()});
  120. ctx.ldr_color_texture_a->resize(ctx.render_resolution.x(), ctx.render_resolution.y(), nullptr);
  121. ctx.ldr_framebuffer_b->resize({ctx.render_resolution.x(), ctx.render_resolution.y()});
  122. ctx.ldr_color_texture_b->resize(ctx.render_resolution.x(), ctx.render_resolution.y(), nullptr);
  123. // Resize bloom render pass
  124. ctx.bloom_pass->resize();
  125. // Enable or disable resample pass
  126. if (viewport_size.x() != ctx.render_resolution.x() || viewport_size.y() != ctx.render_resolution.y())
  127. {
  128. ctx.resample_pass->set_enabled(true);
  129. }
  130. else
  131. {
  132. ctx.resample_pass->set_enabled(false);
  133. }
  134. reroute_framebuffers(ctx);
  135. debug::log::trace("Changed render resolution to {}", scale);
  136. }
  137. void save_screenshot(game::context& ctx)
  138. {
  139. // Determine timestamped screenshot filename
  140. const auto time = std::chrono::floor<std::chrono::milliseconds>(std::chrono::system_clock::now());
  141. const std::string screenshot_filename = std::format("{0}-{1:%Y%m%d}T{1:%H%M%S}Z.png", config::application_name, time);
  142. // Determine path to screenshot file
  143. std::filesystem::path screenshot_filepath = ctx.screenshots_path / screenshot_filename;
  144. std::string screenshot_filepath_string = screenshot_filepath.string();
  145. debug::log::debug("Saving screenshot to \"{}\"...", screenshot_filepath_string);
  146. // Get viewport dimensions
  147. const int2& viewport_size = ctx.app->get_viewport_size();
  148. // Allocate screenshot image
  149. std::shared_ptr<image> frame = std::make_shared<image>();
  150. frame->format(1, 3);
  151. frame->resize(viewport_size.x(), viewport_size.y());
  152. // Read pixel data from backbuffer into image
  153. glReadBuffer(GL_BACK);
  154. glReadPixels(0, 0, viewport_size.x(), viewport_size.y(), GL_RGB, GL_UNSIGNED_BYTE, frame->data());
  155. // Write screenshot file in separate thread
  156. std::thread
  157. (
  158. [frame, path = std::move(screenshot_filepath_string)]
  159. {
  160. stbi_flip_vertically_on_write(1);
  161. stbi_write_png(path.c_str(), frame->get_width(), frame->get_height(), frame->get_channel_count(), frame->data(), frame->get_width() * frame->get_channel_count());
  162. debug::log::debug("Saved screenshot to \"{}\"", path);
  163. }
  164. ).detach();
  165. }
  166. void select_anti_aliasing_method(game::context& ctx, render::anti_aliasing_method method)
  167. {
  168. // Switch AA method
  169. switch (method)
  170. {
  171. // Off
  172. case render::anti_aliasing_method::none:
  173. ctx.fxaa_pass->set_enabled(false);
  174. reroute_framebuffers(ctx);
  175. break;
  176. // FXAA
  177. case render::anti_aliasing_method::fxaa:
  178. ctx.fxaa_pass->set_enabled(true);
  179. reroute_framebuffers(ctx);
  180. break;
  181. }
  182. // Update AA method setting
  183. ctx.anti_aliasing_method = method;
  184. }
  185. void reroute_framebuffers(game::context& ctx)
  186. {
  187. if (ctx.fxaa_pass->is_enabled())
  188. {
  189. if (ctx.resample_pass->is_enabled())
  190. {
  191. ctx.common_final_pass->set_framebuffer(ctx.ldr_framebuffer_a);
  192. ctx.fxaa_pass->set_framebuffer(ctx.ldr_framebuffer_b);
  193. }
  194. else
  195. {
  196. ctx.common_final_pass->set_framebuffer(ctx.ldr_framebuffer_a);
  197. ctx.fxaa_pass->set_framebuffer(&ctx.rasterizer->get_default_framebuffer());
  198. }
  199. }
  200. else
  201. {
  202. if (ctx.resample_pass->is_enabled())
  203. {
  204. ctx.common_final_pass->set_framebuffer(ctx.ldr_framebuffer_b);
  205. }
  206. else
  207. {
  208. ctx.common_final_pass->set_framebuffer(&ctx.rasterizer->get_default_framebuffer());
  209. }
  210. }
  211. }
  212. } // namespace graphics
  213. } // namespace game