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

122 lines
3.5 KiB

  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 "render/passes/fxaa-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 "render/vertex-attribute.hpp"
  31. #include "render/context.hpp"
  32. #include "debug/log.hpp"
  33. #include <glad/glad.h>
  34. namespace render {
  35. fxaa_pass::fxaa_pass(gl::rasterizer* rasterizer, const gl::framebuffer* framebuffer, resource_manager* resource_manager):
  36. pass(rasterizer, framebuffer),
  37. source_texture(nullptr)
  38. {
  39. // Load FXAA shader template
  40. shader_template = resource_manager->load<render::shader_template>("fxaa.glsl");
  41. // Build FXAA shader program
  42. shader = shader_template->build();
  43. source_texture_input = shader->get_input("source_texture");
  44. texel_size_input = shader->get_input("texel_size");
  45. const float vertex_data[] =
  46. {
  47. -1.0f, 1.0f,
  48. -1.0f, -1.0f,
  49. 1.0f, 1.0f,
  50. 1.0f, 1.0f,
  51. -1.0f, -1.0f,
  52. 1.0f, -1.0f
  53. };
  54. std::size_t vertex_size = 2;
  55. std::size_t vertex_stride = sizeof(float) * vertex_size;
  56. std::size_t vertex_count = 6;
  57. quad_vbo = new gl::vertex_buffer(sizeof(float) * vertex_size * vertex_count, vertex_data);
  58. quad_vao = new gl::vertex_array();
  59. // Define position vertex attribute
  60. gl::vertex_attribute position_attribute;
  61. position_attribute.buffer = quad_vbo;
  62. position_attribute.offset = 0;
  63. position_attribute.stride = vertex_stride;
  64. position_attribute.type = gl::vertex_attribute_type::float_32;
  65. position_attribute.components = 2;
  66. // Bind vertex attributes to VAO
  67. quad_vao->bind(render::vertex_attribute::position, position_attribute);
  68. }
  69. fxaa_pass::~fxaa_pass()
  70. {
  71. delete quad_vao;
  72. delete quad_vbo;
  73. delete shader;
  74. /// @TODO
  75. // resource_manager->unload("fxaa.glsl");
  76. }
  77. void fxaa_pass::render(const render::context& ctx, render::queue& queue) const
  78. {
  79. if (!source_texture)
  80. return;
  81. // Set rasterizer state
  82. glDisable(GL_DEPTH_TEST);
  83. glDepthMask(GL_FALSE);
  84. glEnable(GL_CULL_FACE);
  85. glCullFace(GL_BACK);
  86. glDisable(GL_BLEND);
  87. // Render FXAA
  88. rasterizer->use_framebuffer(*framebuffer);
  89. rasterizer->set_viewport(0, 0, framebuffer->get_dimensions()[0], framebuffer->get_dimensions()[1]);
  90. rasterizer->use_program(*shader);
  91. source_texture_input->upload(source_texture);
  92. if (texel_size_input)
  93. {
  94. const float2 texel_size = 1.0f / float2{static_cast<float>(source_texture->get_width()), static_cast<float>(source_texture->get_height())};
  95. texel_size_input->upload(texel_size);
  96. }
  97. rasterizer->draw_arrays(*quad_vao, gl::drawing_mode::triangles, 0, 6);
  98. }
  99. void fxaa_pass::set_source_texture(const gl::texture_2d* texture)
  100. {
  101. source_texture = texture;
  102. }
  103. } // namespace render