CSEngine
Loading...
Searching...
No Matches
CSE::SFrameBuffer Class Reference

#include <SFrameBuffer.h>

Inheritance diagram for CSE::SFrameBuffer:
CSE::SResource CSE::SObject

Public Types

enum  BufferType { RENDER = 0 , DEPTH = 1 , STENCIL = 2 }
 
enum  BufferDimension { PLANE = 0 , CUBE = 1 }
 
enum  BufferStatus { NONE = 0 , COLOR_ONLY = 1 , DEPTH_ONLY = 2 , MULTI = 3 }
 
enum  BlitType { IN_ORDER = 0 , REVERSE = 1 }
 

Public Member Functions

void GenerateFramebuffer (BufferDimension dimension, int width, int height)
 
unsigned int GenerateRenderbuffer (BufferType type, int internalFormat)
 
STextureGenerateTexturebuffer (BufferType type, int channel, int level=0)
 
void RasterizeFramebuffer ()
 
void AttachCubeBuffer (int index, int level=0) const
 
void AttachFrameBuffer (int target=GL_FRAMEBUFFER) const
 
void DetachFrameBuffer () const
 
void ResizeFrameBuffer (int width, int height)
 
void Exterminate () override
 
void BlitFrameBuffer (const SFrameBuffer &dst, BlitType type=IN_ORDER)
 
int GetWidth () const
 
int GetHeight () const
 
const ivec2GetSize () const
 
BufferStatus GetBufferStatus () const
 
STextureGetTexture (int index) const
 
STextureGetTexture (const char *id) const
 
unsigned int GetRenderbufferID (int index) const
 
STextureGetMainColorTexture () const
 
STextureGetDepthTexture () const
 
- Public Member Functions inherited from CSE::SResource
 SResource (bool isRegister)
 
 SResource (const SResource *resource, bool isRegister)
 
void SetName (std::string name)
 
void SetAbsoluteID (std::string id)
 
std::string GetName () const
 
std::string GetAbsoluteID () const
 
AssetMgr::AssetReferenceGetAssetReference (std::string hash="") const
 
void LinkResource (AssetMgr::AssetReference *asset)
 
void LinkResource (std::string name)
 
void SetHash (std::string &hash) override
 
- Public Member Functions inherited from CSE::SObject
 SObject (bool isRegister)
 
virtual void SetUndestroyable (bool enable)
 
virtual void Destroy ()
 
virtual void __FORCE_DESTROY__ ()
 
virtual std::string GenerateMeta ()
 
std::string GetHash () const
 

Protected Member Functions

void Init (const AssetMgr::AssetReference *asset) override
 

Additional Inherited Members

- Static Public Member Functions inherited from CSE::SResource
template<class T >
static T * Create (const std::string &name)
 
template<class T >
static T * Create (const AssetMgr::AssetReference *asset)
 
template<class T >
static T * Get (std::string name)
 
- Protected Attributes inherited from CSE::SObject
std::string m_hash
 

Detailed Description

See also
To use the SFrameBuffer class, you need to follow the steps below:
  1. Generate a framebuffer.
    m_frameBuffer->GenerateFramebuffer(SFrameBuffer::PLANE);
  2. Generate buffers.
    m_frameBuffer->GenerateTexturebuffer(SFrameBuffer::DEPTH, width, height, GL_DEPTH_COMPONENT);
  3. Rasterize the framebuffer.
    m_frameBuffer->RasterizeFramebuffer();

Definition at line 31 of file SFrameBuffer.h.

Member Enumeration Documentation

◆ BlitType

enum CSE::SFrameBuffer::BlitType

Definition at line 42 of file SFrameBuffer.h.

42 {
43 IN_ORDER = 0, REVERSE = 1,
44 };

◆ BufferDimension

enum CSE::SFrameBuffer::BufferDimension

Definition at line 36 of file SFrameBuffer.h.

36 {
37 PLANE = 0, CUBE = 1,
38 };

◆ BufferStatus

enum CSE::SFrameBuffer::BufferStatus

Definition at line 39 of file SFrameBuffer.h.

39 {
40 NONE = 0, COLOR_ONLY = 1, DEPTH_ONLY = 2, MULTI = 3,
41 };

◆ BufferType

enum CSE::SFrameBuffer::BufferType

Definition at line 33 of file SFrameBuffer.h.

33 {
34 RENDER = 0, DEPTH = 1, STENCIL = 2,
35 };

Constructor & Destructor Documentation

◆ SFrameBuffer()

SFrameBuffer::SFrameBuffer ( )

Definition at line 31 of file SFrameBuffer.cpp.

31 {
32 SetUndestroyable(true);
33}

Member Function Documentation

◆ AttachCubeBuffer()

void SFrameBuffer::AttachCubeBuffer ( int  index,
int  level = 0 
) const

Definition at line 188 of file SFrameBuffer.cpp.

188 {
189 if (m_dimension != CUBE) return;
190
191 for (const auto& buffer : m_buffers) {
192 const auto& texture = buffer->texture;
193 if (texture == nullptr) continue;
194 glFramebufferTexture2D(GL_FRAMEBUFFER, GenerateAttachmentType(buffer->type, false),
195 GL_TEXTURE_CUBE_MAP_POSITIVE_X + index, texture->GetTextureID(), level);
196 }
197}

◆ AttachFrameBuffer()

void SFrameBuffer::AttachFrameBuffer ( int  target = GL_FRAMEBUFFER) const

Definition at line 199 of file SFrameBuffer.cpp.

199 {
200 glViewport(0, 0, m_size->x, m_size->y);
201 glBindFramebuffer(target, m_fbo);
202}

◆ BlitFrameBuffer()

void SFrameBuffer::BlitFrameBuffer ( const SFrameBuffer dst,
BlitType  type = IN_ORDER 
)

Safely blit the framebuffer. Each framebuffer must be in BufferType::MULTI state, and all buffers must be in the form of STexture. For blit techniques that are not suitable for that condition, it is faster to use glBlitFramebuffer.

Note
The format of the framebuffer should be organized in the following order:
  • [0] = Color Buffer
  • [1] = Depth Buffer
Parameters
dstThe framebuffer to merge.
typeDetermine the order in which they will be merged. Default is IN_ORDER.

Definition at line 234 of file SFrameBuffer.cpp.

234 {
235 if (m_mainColorBuffer == nullptr || m_depthBuffer == nullptr) {
236 Exterminate();
237 GenerateFramebuffer(PLANE, m_size->x, m_size->y);
238 GenerateTexturebuffer(RENDER, GL_RGB);
239 GenerateTexturebuffer(DEPTH, GL_DEPTH_COMPONENT);
241 }
242
243 const SFrameBuffer* a;
244 const SFrameBuffer* b;
245 if (type == REVERSE) {
246 a = this;
247 b = &dst;
248 } else {
249 a = &dst;
250 b = this;
251 }
252 const auto& aColorTexture = a->m_mainColorBuffer->texture;
253 const auto& bColorTexture = b->m_mainColorBuffer->texture;
254 const auto& aDepthTexture = a->m_depthBuffer->texture;
255 const auto& bDepthTexture = b->m_depthBuffer->texture;
256
257 if (m_blitObject.handle == nullptr) {
258 m_blitObject.handle = SResource::Create<GLProgramHandle>(Settings::GetDefaultBlitBufferShaderID());
259 m_blitObject.aColor = m_blitObject.handle->UniformLocation("a.color")->id;
260 m_blitObject.bColor = m_blitObject.handle->UniformLocation("b.color")->id;
261 m_blitObject.aDepth = m_blitObject.handle->UniformLocation("a.depth")->id;
262 m_blitObject.bDepth = m_blitObject.handle->UniformLocation("b.depth")->id;
263 }
264
265 AttachFrameBuffer();
266 glViewport(0, 0, m_size->x, m_size->y);
267 glUseProgram(m_blitObject.handle->Program);
268 aColorTexture->Bind(m_blitObject.aColor, 0);
269 bColorTexture->Bind(m_blitObject.bColor, 1);
270 aDepthTexture->Bind(m_blitObject.aDepth, 2);
271 bDepthTexture->Bind(m_blitObject.bDepth, 3);
272
273 ShaderUtil::BindAttributeToPlane();
274}
STexture * GenerateTexturebuffer(BufferType type, int channel, int level=0)
void GenerateFramebuffer(BufferDimension dimension, int width, int height)

References GenerateFramebuffer(), GenerateTexturebuffer(), and RasterizeFramebuffer().

◆ DetachFrameBuffer()

void SFrameBuffer::DetachFrameBuffer ( ) const

Definition at line 204 of file SFrameBuffer.cpp.

204 {
205 glBindFramebuffer(GL_FRAMEBUFFER, 0);
206}

◆ Exterminate()

void SFrameBuffer::Exterminate ( )
overridevirtual

Implements CSE::SObject.

Definition at line 37 of file SFrameBuffer.cpp.

37 {
38 glDeleteFramebuffers(1, &m_fbo);
39
40 for (const auto& buffer : m_buffers) {
41 ReleaseBufferObject(buffer);
42 }
43 m_buffers.clear();
44 m_depthBuffer = m_mainColorBuffer = nullptr;
45 SAFE_DELETE(m_size);
46}

◆ GenerateFramebuffer()

void SFrameBuffer::GenerateFramebuffer ( BufferDimension  dimension,
int  width,
int  height 
)

This is the first function called when creating a framebuffer. You specify the dimensions of the framebuffer through that function.

Parameters
dimensionSets the dimensions of the framebuffer. The default is SFrameBuffer::PLANE.
widthWrite the width of the render buffer.
heightWrites the height of the render buffer.

Definition at line 104 of file SFrameBuffer.cpp.

104 {
105 m_dimension = dimension;
106 m_size->x = width;
107 m_size->y = height;
108
109 glGenFramebuffers(1, &m_fbo);
110 glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
111}

Referenced by BlitFrameBuffer().

◆ GenerateRenderbuffer()

unsigned int SFrameBuffer::GenerateRenderbuffer ( BufferType  type,
int  internalFormat 
)

Creates a render buffer in a non-texture format.

Parameters
typeSpecifies the type of buffer to create. The default is RENDER.
internalFormatSets the internal format for the render buffer. Format details can be found at this link.
Returns
Returns the ID value of the created render buffer. If creation fails, 0 is returned.

Definition at line 113 of file SFrameBuffer.cpp.

113 {
114 if (m_dimension == CUBE) return 0;
115
116 auto buffer = new BufferObject();
117 buffer->type = type;
118 buffer->format = internalFormat;
119
120 glGenRenderbuffers(1, &buffer->renderbufferId);
121 glBindRenderbuffer(GL_RENDERBUFFER, buffer->renderbufferId);
122 glRenderbufferStorage(GL_RENDERBUFFER, GenerateInternalFormat(internalFormat), m_size->x, m_size->y);
123
124 glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
125 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GenerateAttachmentType(type, false), GL_RENDERBUFFER,
126 buffer->renderbufferId);
127
128 if (type == RENDER) {
129 if (m_mainColorBuffer == nullptr)
130 m_mainColorBuffer = buffer;
131 } else if (m_depthBuffer == nullptr) {
132 m_depthBuffer = buffer;
133 }
134 m_buffers.push_back(buffer);
135 return buffer->renderbufferId;
136}

◆ GenerateTexturebuffer()

STexture * SFrameBuffer::GenerateTexturebuffer ( BufferType  type,
int  channel,
int  level = 0 
)

Creates a render buffer in texture format.

Parameters
typeSpecifies the type of buffer to create. The default is RENDER.
channelSets the channel format for the render buffer. Format details can be found at this link.
levelSets the texture mipmap level. Default is 0.
Returns
Returns the STexture of the created render texture buffer. If creation fails, nullptr is returned.

Definition at line 138 of file SFrameBuffer.cpp.

138 {
139 auto buffer = new BufferObject();
140 buffer->type = type;
141 buffer->format = channel;
142 buffer->level = (short) level;
143 buffer->texture = new STexture(static_cast<STexture::Type>(m_dimension));
144 buffer->texture->InitTexture(m_size->x, m_size->y, channel, GenerateInternalFormat(channel),
145 GenerateInternalType(channel));
146 unsigned int texId = buffer->texture->GetTextureID();
147// if(level > 0)
148
149 glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
150
151 if (m_dimension == PLANE) {
152 glFramebufferTexture2D(GL_FRAMEBUFFER, GenerateAttachmentType(type), GL_TEXTURE_2D, texId, level);
153 } else {
154 // TODO: 큐브맵 텍스쳐 바인딩 설정 필요
155 GenerateAttachmentType(buffer->type, false);
156 }
157
158 if (type == RENDER) {
159 if (m_mainColorBuffer == nullptr)
160 m_mainColorBuffer = buffer;
161 } else if (m_depthBuffer == nullptr) {
162 m_depthBuffer = buffer;
163 }
164 m_buffers.push_back(buffer);
165 return buffer->texture;
166}

Referenced by BlitFrameBuffer(), and RasterizeFramebuffer().

◆ GetBufferStatus()

SFrameBuffer::BufferStatus SFrameBuffer::GetBufferStatus ( ) const

Definition at line 343 of file SFrameBuffer.cpp.

343 {
344 return m_bufferStatus;
345}

◆ GetDepthTexture()

STexture * CSE::SFrameBuffer::GetDepthTexture ( ) const
inline

Get the texture assigned to the framebuffer as depth.

Returns
if the corresponding index is an invalid texture type, nullptr is returned.

Definition at line 166 of file SFrameBuffer.h.

166 {
167 if (m_depthBuffer == nullptr) return nullptr;
168 return m_depthBuffer->texture;
169 }

◆ GetHeight()

int SFrameBuffer::GetHeight ( ) const

Definition at line 280 of file SFrameBuffer.cpp.

280 {
281 return m_size->y;
282}

◆ GetMainColorTexture()

STexture * CSE::SFrameBuffer::GetMainColorTexture ( ) const
inline

Get the texture assigned to the framebuffer as GL_COLOR_ATTACHMENT0.

Returns
if the corresponding index is an invalid texture type, nullptr is returned.

Definition at line 157 of file SFrameBuffer.h.

157 {
158 if (m_mainColorBuffer == nullptr) return nullptr;
159 return m_mainColorBuffer->texture;
160 }

Referenced by CSE::SRenderGroup::BindSourceBuffer().

◆ GetRenderbufferID()

unsigned int SFrameBuffer::GetRenderbufferID ( int  index) const

Get renderbuffer ID that exist in the framebuffer.

Parameters
indexbuffer index in framebuffer
Returns
if the corresponding index is an invalid buffer ID, 0 is returned.

Definition at line 365 of file SFrameBuffer.cpp.

365 {
366 if (index < 0 || index > m_buffers.size() - 1) return 0;
367
368 auto id = m_buffers[index]->renderbufferId;
369 return id;
370}

◆ GetSize()

const ivec2 & CSE::SFrameBuffer::GetSize ( ) const
inline

Definition at line 126 of file SFrameBuffer.h.

126 {
127 return *m_size;
128 }

◆ GetTexture() [1/2]

STexture * SFrameBuffer::GetTexture ( const char *  id) const

Get textures that exist in the framebuffer.

Parameters
idtexture id in framebuffer
Returns
if the corresponding index is an invalid texture type, nullptr is returned.

Definition at line 354 of file SFrameBuffer.cpp.

354 {
355 for (const auto& buffer : m_buffers) {
356 const auto& texture = buffer->texture;
357 if (texture == nullptr) continue;
358
359 std::string bufferId = texture->GetHash();
360 if (bufferId == id) return texture;
361 }
362 return nullptr;
363}

◆ GetTexture() [2/2]

STexture * SFrameBuffer::GetTexture ( int  index) const

Get textures that exist in the framebuffer.

Parameters
indexbuffer index in framebuffer
Returns
if the corresponding index is an invalid texture type, nullptr is returned.

Definition at line 347 of file SFrameBuffer.cpp.

347 {
348 if (index < 0 || index > m_buffers.size() - 1) return nullptr;
349
350 auto texture = m_buffers[index]->texture;
351 return texture;
352}

◆ GetWidth()

int SFrameBuffer::GetWidth ( ) const

Definition at line 276 of file SFrameBuffer.cpp.

276 {
277 return m_size->x;
278}

◆ Init()

void SFrameBuffer::Init ( const AssetMgr::AssetReference asset)
overrideprotectedvirtual

Implements CSE::SResource.

Definition at line 49 of file SFrameBuffer.cpp.

49 {
50 const XNode* root = nullptr;
51
52 try {
53 root = XFILE(asset->name_path.c_str()).getRoot();
54 }
55 catch (int e) {
56 SAFE_DELETE(root);
57 return;
58 }
59
60 try {
61 const XNode& cse_framebuffer = root->getChild("CSEFRAMEBUFFER");
62 const XNode& info = cse_framebuffer.getChild("info");
63
64 auto width_str = info.getAttribute("width").value;
65 auto height_str = info.getAttribute("height").value;
66 auto dimension_str = info.getAttribute("dimension").value;
67
68 m_size->x = std::stoi(width_str);
69 m_size->y = std::stoi(height_str);
70 auto dimension = static_cast<BufferDimension>(std::stoi(dimension_str));
71 GenerateFramebuffer(dimension, m_size->x, m_size->y);
72
73 const XNode& buffers = cse_framebuffer.getChild("buffers");
74 int index = 0;
75 for (const auto& buffer_node : buffers.children) {
76 auto type_str = buffer_node.getAttribute("type").value;
77 auto format_str = buffer_node.getAttribute("format").value;
78 auto isTexture_str = buffer_node.getAttribute("isTexture").value;
79
80 auto type = static_cast<BufferType>(std::stoi(type_str));
81 auto format = std::stoi(format_str);
82 auto isTexture = std::stoi(isTexture_str) == 1;
83
84 if (isTexture) {
85 const auto& texture = GenerateTexturebuffer(type, format);
86 const auto& textureName = "?Texture" + std::to_string(index);
87 texture->SetName(GetName() + textureName);
88 texture->SetAbsoluteID(GetHash() + textureName);
89 } else {
90 GenerateRenderbuffer(type, format);
91 }
92 ++index;
93 }
95 }
96 catch (int e) {
97 SAFE_DELETE(root);
98 auto resMgr = CORE->GetCore(ResMgr);
99 resMgr->Remove(this);
100 }
101 SAFE_DELETE(root);
102}
unsigned int GenerateRenderbuffer(BufferType type, int internalFormat)
Definition XML.h:77
Definition XML.h:43

◆ RasterizeFramebuffer()

void SFrameBuffer::RasterizeFramebuffer ( )

Create all the render buffers to allocate to the framebuffer and proceed with rasterization. You can use the framebuffer after calling the function.

Definition at line 168 of file SFrameBuffer.cpp.

168 {
169 glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
170 m_buffers.reserve(m_buffers.size());
171
172 // tell OpenGL which color attachments we'll use (of this framebuffer) for rendering
173 if (m_colorAttachmentSize > 1) {
174 std::vector<unsigned int> attachments;
175 attachments.reserve(m_colorAttachmentSize);
176 for (int i = 0; i < m_colorAttachmentSize; ++i) {
177 attachments.push_back(GL_COLOR_ATTACHMENT0 + i);
178 }
179 glDrawBuffers(m_colorAttachmentSize, &attachments[0]);
180 }
181
182 if (m_bufferStatus == COLOR_ONLY) {
183 GenerateTexturebuffer(SFrameBuffer::DEPTH, GL_DEPTH_COMPONENT);
184 m_bufferStatus = MULTI;
185 }
186}

References GenerateTexturebuffer().

Referenced by BlitFrameBuffer().

◆ ResizeFrameBuffer()

void SFrameBuffer::ResizeFrameBuffer ( int  width,
int  height 
)

Definition at line 208 of file SFrameBuffer.cpp.

208 {
209 std::vector<BufferObject*> origBufferVector(m_buffers);
210
211 m_buffers.clear();
212 m_buffers.reserve(origBufferVector.size());
213 m_colorAttachmentSize = 0;
214 glDeleteFramebuffers(1, &m_fbo);
215 GenerateFramebuffer(m_dimension, width, height);
216 m_depthBuffer = m_mainColorBuffer = nullptr;
217
218 for (const auto& buffer : origBufferVector) {
219 auto type = buffer->type;
220 auto format = buffer->format;
221 auto level = buffer->level;
222 auto isTexture = buffer->texture != nullptr;
223
224 ReleaseBufferObject(buffer);
225
226 if (isTexture)
227 GenerateTexturebuffer(type, format, level);
228 else
229 GenerateRenderbuffer(type, format);
230 }
232}

The documentation for this class was generated from the following files: