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

125 lines
3.3 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 <engine/gl/framebuffer.hpp>
  20. #include <glad/gl.h>
  21. #include <stdexcept>
  22. namespace gl {
  23. framebuffer::framebuffer(std::span<const framebuffer_attachment> attachments, std::uint32_t width, std::uint32_t height)
  24. {
  25. m_dimensions = {width, height};
  26. m_attachments.assign(attachments.begin(), attachments.end());
  27. // Generate framebuffer
  28. glCreateFramebuffers(1, &m_gl_named_framebuffer);
  29. GLenum gl_color_attachment = GL_COLOR_ATTACHMENT0;
  30. std::vector<GLenum> gl_draw_buffers;
  31. // Attach textures to framebuffer
  32. for (const auto& attachment: m_attachments)
  33. {
  34. if (attachment.usage_mask & gl::color_attachment_bit)
  35. {
  36. glNamedFramebufferTexture
  37. (
  38. m_gl_named_framebuffer,
  39. gl_color_attachment,
  40. attachment.image_view->m_gl_texture_name,
  41. static_cast<GLuint>(attachment.level)
  42. );
  43. gl_draw_buffers.emplace_back(gl_color_attachment);
  44. ++gl_color_attachment;
  45. }
  46. if (attachment.usage_mask & gl::depth_attachment_bit)
  47. {
  48. if (attachment.usage_mask & gl::stencil_attachment_bit)
  49. {
  50. glNamedFramebufferTexture
  51. (
  52. m_gl_named_framebuffer,
  53. GL_DEPTH_STENCIL_ATTACHMENT,
  54. attachment.image_view->m_gl_texture_name,
  55. static_cast<GLuint>(attachment.level)
  56. );
  57. }
  58. else
  59. {
  60. glNamedFramebufferTexture
  61. (
  62. m_gl_named_framebuffer,
  63. GL_DEPTH_ATTACHMENT,
  64. attachment.image_view->m_gl_texture_name,
  65. static_cast<GLuint>(attachment.level)
  66. );
  67. }
  68. }
  69. else if (attachment.usage_mask & gl::stencil_attachment_bit)
  70. {
  71. glNamedFramebufferTexture
  72. (
  73. m_gl_named_framebuffer,
  74. GL_STENCIL_ATTACHMENT,
  75. attachment.image_view->m_gl_texture_name,
  76. static_cast<GLuint>(attachment.level)
  77. );
  78. }
  79. }
  80. // Specify read and draw buffers
  81. if (!gl_draw_buffers.empty())
  82. {
  83. glNamedFramebufferReadBuffer(m_gl_named_framebuffer, GL_COLOR_ATTACHMENT0);
  84. glNamedFramebufferDrawBuffers
  85. (
  86. m_gl_named_framebuffer,
  87. static_cast<GLsizei>(gl_draw_buffers.size()),
  88. gl_draw_buffers.data()
  89. );
  90. }
  91. else
  92. {
  93. glNamedFramebufferReadBuffer(m_gl_named_framebuffer, GL_NONE);
  94. glNamedFramebufferDrawBuffer(m_gl_named_framebuffer, GL_NONE);
  95. }
  96. if (glCheckNamedFramebufferStatus(m_gl_named_framebuffer, GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  97. {
  98. throw std::runtime_error("OpenGL framebuffer incomplete.");
  99. }
  100. }
  101. framebuffer::~framebuffer()
  102. {
  103. if (m_gl_named_framebuffer)
  104. {
  105. glDeleteFramebuffers(1, &m_gl_named_framebuffer);
  106. }
  107. }
  108. void framebuffer::resize(std::uint32_t width, std::uint32_t height)
  109. {
  110. m_dimensions = {width, height};
  111. }
  112. } // namespace gl