Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_frame_buffer_object.hpp
1#pragma once
2
3#include "structure/design_pattern/spk_activable_object.hpp"
4#include "structure/design_pattern/spk_synchronizable_object.hpp"
5#include "structure/graphics/spk_color.hpp"
6#include "structure/math/spk_vector2.hpp"
7#include "structure/opengl/spk_opengl_includes.hpp"
8#include "structure/opengl/spk_texture_object.hpp"
9#include "structure/opengl/spk_view_region.hpp"
10#include "utils/spk_opengl_utils.hpp"
11
12#include <algorithm>
13#include <cstdint>
14#include <iostream>
15#include <ostream>
16#include <stdexcept>
17#include <string>
18#include <unordered_map>
19#include <utility>
20#include <vector>
21
22namespace spk
23{
24 namespace OpenGL
25 {
31 {
32 public:
38 {
39 friend class FrameBufferObject;
40
41 public:
45 enum class Type
46 {
47 GreyScale,
48 TwoChannelColor,
49 ThreeChannelColor,
50 Color,
51 Depth,
52 DepthStencil
53 };
54
55 private:
61
62 Type _type;
63
64 public:
69 explicit Attachment(Type p_type = Type::Color) :
71 _type(p_type)
72 {
73 switch (p_type)
74 {
75
76 case Attachment::Type::GreyScale:
77 setFormat(spk::OpenGL::TextureObject::Format::GreyLevel);
78 break;
79
80 case Attachment::Type::TwoChannelColor:
81 setFormat(spk::OpenGL::TextureObject::Format::DualChannel);
82 break;
83
84 case Attachment::Type::ThreeChannelColor:
85 setFormat(spk::OpenGL::TextureObject::Format::RGB);
86 break;
87
88 case Attachment::Type::Color:
89 setFormat(spk::OpenGL::TextureObject::Format::RGBA);
90 break;
91
92 case Attachment::Type::Depth:
93 setFormat(spk::OpenGL::TextureObject::Format::DepthComponent);
94 break;
95
96 case Attachment::Type::DepthStencil:
97 setFormat(spk::OpenGL::TextureObject::Format::DepthStencil);
98 break;
99 }
100 }
101
106 Type type() const noexcept
107 {
108 return (_type);
109 }
110 };
111
112 private:
115
116 void _ensureAttachmentTypeAvailability(GLuint p_index, Attachment::Type p_type) const;
117 void _ensureAttachmentsValidity();
118 void _activateAttachment(GLuint p_index, Attachment &p_attachment);
119 void _activateAttachments();
120 void _attachTexture(GLuint p_index, Attachment &p_attachment);
121 void _attachAllTextures();
122 void _configureDrawBuffers();
123
124 static GLenum _pixelDataType(TextureObject::Format p_format);
125 static std::size_t _bytesPerPixel(TextureObject::Format p_format);
126
127 void _onSynchronize() override;
128 void _registerCallbacks();
129
130 const spk::OpenGL::ViewRegion *_viewRegionWhenActivating = nullptr;
131 spk::Extend2D _windowSizeWhenActivating = {};
132 GLuint _activeFrameBufferWhenActivating = 0;
133 spk::OpenGL::ViewRegion _viewRegion;
134 spk::CachedData<GLuint> _id{OpenGLUtils::generateFrameBufferObjectID, OpenGLUtils::releaseFrameBufferObjectID};
135 std::unordered_map<GLuint, Attachment> _attachments;
136 spk::Vector2UInt _size = {0, 0};
137
138 public:
147 FrameBufferObject(const FrameBufferObject &p_other);
154
160 void addAttachment(const GLuint &p_index, Attachment::Type p_type);
161
167 Attachment &attachment(const GLuint &p_index);
173 const Attachment &attachment(const GLuint &p_index) const;
174
180 bool hasAttachment(const GLuint &p_index) const;
181
186 void resize(const spk::Vector2UInt &p_size);
187
192 const spk::Vector2UInt &size() const;
193
198 const spk::OpenGL::ViewRegion &viewRegion() const;
199
205 void clear(const spk::Color &p_color, const ViewRegion::ClearMask &p_clearMask);
206
212 spk::OpenGL::TextureObject exportAttachmentTexture(GLuint p_index);
213 };
214
215 std::ostream &operator<<(std::ostream &p_os, const FrameBufferObject::Attachment::Type &p_type);
216 }
217}
Stateful helper toggling between activated/deactivated states.
Definition spk_activable_object.hpp:20
Lazily generates and caches a value with optional custom destructor.
Definition spk_cached_data.hpp:26
Texture attachment with basic format presets for FBOs.
Definition spk_frame_buffer_object.hpp:38
Attachment(Type p_type=Type::Color)
Creates a texture attachment with a preset texture format.
Definition spk_frame_buffer_object.hpp:69
Type type() const noexcept
Returns the underlying attachment category.
Definition spk_frame_buffer_object.hpp:106
Type
Attachment storage format used for the framebuffer slot.
Definition spk_frame_buffer_object.hpp:46
const spk::Vector2UInt & size() const
Returns current framebuffer size.
Definition spk_frame_buffer_object.cpp:341
FrameBufferObject()
Constructs an empty framebuffer.
Definition spk_frame_buffer_object.cpp:242
FrameBufferObject & operator=(const FrameBufferObject &p_other)
Assigns framebuffer content from another instance.
Definition spk_frame_buffer_object.cpp:267
void addAttachment(const GLuint &p_index, Attachment::Type p_type)
Adds an attachment at the given color index.
Definition spk_frame_buffer_object.cpp:297
Attachment & attachment(const GLuint &p_index)
Accesses an attachment by index.
Definition spk_frame_buffer_object.cpp:311
bool hasAttachment(const GLuint &p_index) const
Checks if an attachment exists.
Definition spk_frame_buffer_object.cpp:321
void resize(const spk::Vector2UInt &p_size)
Resizes all attachments and viewport to the given size.
Definition spk_frame_buffer_object.cpp:326
const spk::OpenGL::ViewRegion & viewRegion() const
Returns the view region associated with this framebuffer.
Definition spk_frame_buffer_object.cpp:346
spk::OpenGL::TextureObject exportAttachmentTexture(GLuint p_index)
Exports an attachment texture by value (copy).
Definition spk_frame_buffer_object.cpp:363
void clear(const spk::Color &p_color, const ViewRegion::ClearMask &p_clearMask)
Clears the framebuffer with the given color and mask.
Definition spk_frame_buffer_object.cpp:351
void clearData()
Clears CPU data and marks for sync.
Definition spk_texture_object.cpp:351
void setData(const uint8_t *p_data, const spk::Vector2UInt &p_size, Format p_format=Format::RGBA)
Uploads pixel data from raw pointer.
Definition spk_texture_object.cpp:340
void resize(const spk::Vector2UInt &p_size)
Resizes CPU data and marks for sync.
Definition spk_texture_object.cpp:358
TextureObject(Format p_format=Format::RGBA, Filter p_filter={Filtering::Linear, Filtering::Linear}, Wrapper p_wrapper={Wrap::Repeat, Wrap::Repeat}, Mipmap p_mipmap=Mipmap::Activated)
Constructs a texture with format, filtering, and wrapping options.
Definition spk_texture_object.cpp:130
size_t sizeAsBytes() const
Returns CPU data size in bytes.
Definition spk_texture_object.cpp:330
Format
Supported GPU pixel formats.
Definition spk_texture_object.hpp:29
void setFormat(Format p_format)
Sets the texture format (affects upload interpretation).
Definition spk_texture_object.cpp:279
Manages viewport and scissor rectangles and clearing.
Definition spk_view_region.hpp:26
spk::Flags< ClearMode > ClearMask
Mask of buffers to clear at once.
Definition spk_view_region.hpp:41
Base for objects requiring deferred synchronization hooks.
Definition spk_synchronizable_object.hpp:10
void forceSynchronization()
Forces synchronization regardless of current flag.
Definition spk_synchronizable_object.hpp:54
void synchronize()
Performs synchronization if requested.
Definition spk_synchronizable_object.hpp:40
Axis-aligned rectangle defined by an anchor point and size.
Definition spk_extend_2d.hpp:24