Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
framebuffer.h
1#ifndef OE_RENDER_FRAMEBUFFER_H
2#define OE_RENDER_FRAMEBUFFER_H
3
4#include <map>
5#include <glm/vec2.hpp>
6#include <glm/vec4.hpp>
7#include <memory>
8#include <span>
9#include "texture.h"
10#include "../util/non_copyable.h"
11
12namespace oe::render
13{
14 enum TargetBuffer : int
15 {
16 COLOR_BUFFER = 0x4000,
17 DEPTH_BUFFER = 0x0100,
18 STENCIL_BUFFER = 0x0400,
19
20 ALL = 0x4500,
21 };
22
27 {
28 std::span<glm::ivec2> dimensions;
29 };
30
52 class Framebuffer : public oe::util::NonCopyable
53 {
54 public:
55 Framebuffer(const glm::ivec2& dimensions);
56
63 Framebuffer(const glm::ivec2& dimensions, uint32_t id):
64 _width(dimensions.x), _height(dimensions.y),
65 _fbo(id)
66 {}
67
68 Framebuffer(const Framebuffer&) = delete;
69 Framebuffer& operator=(const Framebuffer&) = delete;
70
71 Framebuffer(Framebuffer&& other) noexcept
72 {
73 _fbo = other._fbo;
74 _rbo = other._rbo;
75 _width = other._width;
76 _height = other._height;
77
78 // Prevent releasing handls when other is destroyed
79 other._fbo = 0;
80 other._rbo = 0;
81 }
82
83 Framebuffer(const uint32_t width, const uint32_t height);
84 ~Framebuffer();
85
90 std::shared_ptr<Texture> addColorAttachment(const uint32_t attachment_level = 0, const uint32_t internal_format = 0, const uint32_t format = 0, const uint32_t type = 0,
91 const uint32_t filter = 0, const uint8_t max_mips = 1
92 );
93
98 void addDepthAttachment(const uint32_t format = 0, const uint32_t type = 0, const uint32_t filter = 0);
99
103 void setDepthAttachment(std::shared_ptr<Texture> texture);
104
111 void setTextureAttachment(const uint32_t attachment_level, std::shared_ptr<Texture> texture, const int32_t mip_level = 0);
112
121 void setTextureAttachment(const uint32_t attachment_level, Texture* texture, const int32_t mip_level = 0);
122
126 void clearContents(const glm::vec4& color = glm::vec4(0.f), const int& target = 0);
127
131 void beginRender(const bool resize_viewport = true);
132
136 void endRender();
137
141 operator bool();
142
147 inline const std::shared_ptr<Texture>& getColorAttachment(const uint32_t attachment_level) const
148 {
149 return _color_attachments.at(attachment_level);
150 }
151
156 inline const std::shared_ptr<Texture>& getDepthAttachment() const
157 {
158 return _depth_attachment;
159 }
160
174 const Framebuffer* src,
175 const glm::ivec4& src_bounds = glm::ivec4(0),
176 const glm::ivec4& target_bounds = glm::ivec4(0),
177 const TargetBuffer& type = TargetBuffer::ALL,
178 const uint8_t color_attachment_src = 0,
179 const uint8_t color_attachment_dest = 0,
180 const bool usefilter = false
181 );
182
195 void blitTo(
196 Framebuffer* target,
197 const glm::ivec4& src_bounds = glm::ivec4(0),
198 const glm::ivec4& target_bounds = glm::ivec4(0),
199 const TargetBuffer& type = TargetBuffer::ALL,
200 const uint8_t color_attachment_src = 0,
201 const uint8_t color_attachment_dest = 0,
202 const bool usefilter = false
203 ) const;
204
210 void copyFrom(Framebuffer* source, const TargetBuffer& type = TargetBuffer::ALL)
211 {
212 blitFrom(source, {}, {}, type);
213 }
214
220 void blitToDefault(const uint8_t color_attachment_src = 0, const bool& usefilter = false) const;
221
222 /*
223 * Blit textures using quad rendering
224 * faster than a classic blit
225 */
226 //void blitTexture(render::Texture* target, const glm::ivec4& src_bounds, const glm::ivec4& target_bounds, const TargetBuffer& type, const bool& usefilter = false);
227
231 inline glm::ivec2 getDimensions() const noexcept
232 {
233 return {_width, _height};
234 }
235
236 #ifdef OXYGEN_ENGINE_DEBUG_VALIDATE_ORDERED_OPERATIONS
237 bool is_between_begin_end = false;
238 #endif
239
240 private:
241 void _update_color_attachments();
242 void _clear_all_color_attachments(const glm::vec4& color);
243
244 static void _blit(
245 const Framebuffer* src,
246 Framebuffer* target,
247 const glm::ivec4& src_bounds,
248 const glm::ivec4& target_bounds,
249 const TargetBuffer& type,
250 const uint8_t color_attachment_src,
251 const uint8_t color_attachment_dest,
252 const bool usefilter
253 );
254
255 std::map<uint32_t, std::shared_ptr<Texture>> _color_attachments;
256 std::shared_ptr<Texture> _depth_attachment = {};
257
258 uint32_t _width = 0;
259 uint32_t _height = 0;
260 uint32_t _fbo = 0;
261 uint32_t _rbo = 0;
262
263 friend void generateMultipleFramebuffers(MultipleFramebufferInfo, std::vector<Framebuffer>&);
264 };
265
271 void generateMultipleFramebuffers(MultipleFramebufferInfo input, std::vector<Framebuffer>& result);
272}
273
274#endif
Definition framebuffer.h:53
void blitTo(Framebuffer *target, const glm::ivec4 &src_bounds=glm::ivec4(0), const glm::ivec4 &target_bounds=glm::ivec4(0), const TargetBuffer &type=TargetBuffer::ALL, const uint8_t color_attachment_src=0, const uint8_t color_attachment_dest=0, const bool usefilter=false) const
Framebuffer(const glm::ivec2 &dimensions, uint32_t id)
Definition framebuffer.h:63
void addDepthAttachment(const uint32_t format=0, const uint32_t type=0, const uint32_t filter=0)
void blitToDefault(const uint8_t color_attachment_src=0, const bool &usefilter=false) const
void blitFrom(const Framebuffer *src, const glm::ivec4 &src_bounds=glm::ivec4(0), const glm::ivec4 &target_bounds=glm::ivec4(0), const TargetBuffer &type=TargetBuffer::ALL, const uint8_t color_attachment_src=0, const uint8_t color_attachment_dest=0, const bool usefilter=false)
const std::shared_ptr< Texture > & getDepthAttachment() const
Definition framebuffer.h:156
const std::shared_ptr< Texture > & getColorAttachment(const uint32_t attachment_level) const
Definition framebuffer.h:147
void clearContents(const glm::vec4 &color=glm::vec4(0.f), const int &target=0)
void setDepthAttachment(std::shared_ptr< Texture > texture)
void setTextureAttachment(const uint32_t attachment_level, std::shared_ptr< Texture > texture, const int32_t mip_level=0)
friend void generateMultipleFramebuffers(MultipleFramebufferInfo, std::vector< Framebuffer > &)
Generate multiple framebuffers at once.
void beginRender(const bool resize_viewport=true)
std::shared_ptr< Texture > addColorAttachment(const uint32_t attachment_level=0, const uint32_t internal_format=0, const uint32_t format=0, const uint32_t type=0, const uint32_t filter=0, const uint8_t max_mips=1)
glm::ivec2 getDimensions() const noexcept
Definition framebuffer.h:231
void setTextureAttachment(const uint32_t attachment_level, Texture *texture, const int32_t mip_level=0)
void copyFrom(Framebuffer *source, const TargetBuffer &type=TargetBuffer::ALL)
Definition framebuffer.h:210
Definition texture.h:31
Prevent class to be copied.
Definition non_copyable.h:12
Render related abstractions (Shader, Framebuffer, Cubemaps, Textures).
Definition opengl.h:12
void generateMultipleFramebuffers(MultipleFramebufferInfo input, std::vector< Framebuffer > &result)
Generate multiple framebuffers at once.
structure used to create multiple Framebuffer at once
Definition framebuffer.h:27