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