2020-09-21 22:10:50 +08:00
|
|
|
#include "RenderTargetGL.h"
|
2020-10-27 16:58:37 +08:00
|
|
|
#include "DeviceGL.h"
|
2021-07-07 13:52:56 +08:00
|
|
|
#include "renderer/backend/opengl/MacrosGL.h"
|
2020-09-21 22:10:50 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_BEGIN
|
2020-09-21 22:10:50 +08:00
|
|
|
|
2020-10-27 16:58:37 +08:00
|
|
|
RenderTargetGL::RenderTargetGL(bool defaultRenderTarget, DeviceGL* deviceGL) : RenderTarget(defaultRenderTarget)
|
2020-09-21 22:10:50 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!defaultRenderTarget)
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
glGenFramebuffers(1, &_FBO);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2020-10-27 16:58:37 +08:00
|
|
|
_FBO = deviceGL->getDefaultFBO();
|
2020-09-21 22:10:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RenderTargetGL::~RenderTargetGL()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_defaultRenderTarget)
|
|
|
|
{
|
2020-09-21 22:10:50 +08:00
|
|
|
bindFrameBuffer();
|
|
|
|
|
|
|
|
for (auto slot = 0; slot < MAX_COLOR_ATTCHMENT; ++slot)
|
2021-12-25 10:04:45 +08:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + slot, GL_TEXTURE_2D, 0, 0);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
|
|
|
|
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
|
2020-09-21 22:10:50 +08:00
|
|
|
|
|
|
|
unbindFrameBuffer();
|
|
|
|
glDeleteFramebuffers(1, &_FBO);
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderTargetGL::bindFrameBuffer() const
|
|
|
|
{
|
2023-08-31 21:20:01 +08:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, _FBO);
|
2020-09-21 22:10:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderTargetGL::unbindFrameBuffer() const
|
|
|
|
{
|
2023-08-31 21:20:01 +08:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2020-09-21 22:10:50 +08:00
|
|
|
}
|
|
|
|
|
2022-06-24 14:18:48 +08:00
|
|
|
void RenderTargetGL::update() const {
|
|
|
|
if (!_dirty) return;
|
|
|
|
if(!_defaultRenderTarget) {
|
|
|
|
if(bitmask::any(_flags, TargetBufferFlags::COLOR_ALL))
|
|
|
|
{ // color attachments
|
|
|
|
GLenum bufs[MAX_COLOR_ATTCHMENT] = {GL_NONE};
|
|
|
|
for (size_t i = 0; i < MAX_COLOR_ATTCHMENT; ++i)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2022-06-24 14:18:48 +08:00
|
|
|
if (bitmask::any(_flags, getMRTColorFlag(i)))
|
|
|
|
{
|
|
|
|
auto textureInfo = _color[i];
|
2023-08-31 21:20:01 +08:00
|
|
|
auto textureHandler = textureInfo.texture != nullptr ? textureInfo.texture->getHandler() : 0;
|
2022-06-24 14:18:48 +08:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textureHandler,
|
|
|
|
textureInfo.level);
|
|
|
|
bufs[i] = GL_COLOR_ATTACHMENT0 + i;
|
|
|
|
}
|
2020-09-21 22:10:50 +08:00
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_TARGET_PLATFORM == AX_PLATFORM_WIN32 || AX_TARGET_PLATFORM == AX_PLATFORM_LINUX
|
2022-06-24 14:18:48 +08:00
|
|
|
glDrawBuffers(MAX_COLOR_ATTCHMENT, bufs);
|
|
|
|
#endif
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2020-09-21 22:10:50 +08:00
|
|
|
}
|
|
|
|
|
2022-06-24 14:18:48 +08:00
|
|
|
// depth attacmhemt
|
2021-12-25 10:04:45 +08:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
|
2022-06-24 14:18:48 +08:00
|
|
|
_depth.texture != nullptr ? _depth.texture->getHandler() : 0, _depth.level);
|
2020-09-21 22:10:50 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
2022-06-24 14:18:48 +08:00
|
|
|
// stencil attachment
|
2021-12-25 10:04:45 +08:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
|
2022-06-24 14:18:48 +08:00
|
|
|
_stencil.texture != nullptr ? _stencil.texture->getHandler() : 0, _stencil.level);
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2020-09-21 22:10:50 +08:00
|
|
|
}
|
2022-06-24 14:18:48 +08:00
|
|
|
|
|
|
|
_dirty = false;
|
2020-09-21 22:10:50 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BACKEND_END
|