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

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