2022-02-05 08:59:23 -08:00
|
|
|
|
/* Copyright (C) 2022 Wildfire Games.
|
2023-07-27 13:54:46 -07:00
|
|
|
|
* This file is part of 0 A.D.
|
2022-02-05 08:59:23 -08:00
|
|
|
|
*
|
2023-07-27 13:54:46 -07:00
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2022-02-05 08:59:23 -08:00
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
|
*
|
2023-07-27 13:54:46 -07:00
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2022-02-05 08:59:23 -08:00
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
*
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-07-27 13:54:46 -07:00
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2022-02-05 08:59:23 -08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "Framebuffer.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/code_annotation.h"
|
|
|
|
|
|
#include "lib/config2.h"
|
|
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
|
|
#include "renderer/backend/gl/Device.h"
|
|
|
|
|
|
#include "renderer/backend/gl/Texture.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace Renderer
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
namespace Backend
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
namespace GL
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
// static
|
|
|
|
|
|
std::unique_ptr<CFramebuffer> CFramebuffer::Create(
|
2022-11-26 13:55:17 -08:00
|
|
|
|
CDevice* device, const char* name, SColorAttachment* colorAttachment,
|
|
|
|
|
|
SDepthStencilAttachment* depthStencilAttachment)
|
2022-02-05 08:59:23 -08:00
|
|
|
|
{
|
|
|
|
|
|
ENSURE(colorAttachment || depthStencilAttachment);
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CFramebuffer> framebuffer(new CFramebuffer());
|
2022-02-18 15:00:11 -08:00
|
|
|
|
framebuffer->m_Device = device;
|
2022-11-26 13:55:17 -08:00
|
|
|
|
if (colorAttachment)
|
|
|
|
|
|
{
|
|
|
|
|
|
framebuffer->m_ClearColor = colorAttachment->clearColor;
|
|
|
|
|
|
framebuffer->m_ColorAttachmentLoadOp = colorAttachment->loadOp;
|
|
|
|
|
|
framebuffer->m_ColorAttachmentStoreOp = colorAttachment->storeOp;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (depthStencilAttachment)
|
|
|
|
|
|
{
|
|
|
|
|
|
framebuffer->m_DepthStencilAttachmentLoadOp = depthStencilAttachment->loadOp;
|
|
|
|
|
|
framebuffer->m_DepthStencilAttachmentStoreOp = depthStencilAttachment->storeOp;
|
|
|
|
|
|
}
|
2022-02-05 08:59:23 -08:00
|
|
|
|
|
|
|
|
|
|
glGenFramebuffersEXT(1, &framebuffer->m_Handle);
|
|
|
|
|
|
if (!framebuffer->m_Handle)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOGERROR("Failed to create CFramebuffer object");
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer->m_Handle);
|
|
|
|
|
|
|
|
|
|
|
|
if (colorAttachment)
|
|
|
|
|
|
{
|
2022-11-26 13:55:17 -08:00
|
|
|
|
CTexture* colorAttachmentTexture = colorAttachment->texture->As<CTexture>();
|
|
|
|
|
|
ENSURE(device->IsFramebufferFormatSupported(colorAttachmentTexture->GetFormat()));
|
|
|
|
|
|
ENSURE(colorAttachmentTexture->GetUsage() & Renderer::Backend::ITexture::Usage::COLOR_ATTACHMENT);
|
2022-05-26 09:36:57 -07:00
|
|
|
|
|
2022-02-05 08:59:23 -08:00
|
|
|
|
framebuffer->m_AttachmentMask |= GL_COLOR_BUFFER_BIT;
|
|
|
|
|
|
|
|
|
|
|
|
#if CONFIG2_GLES
|
2022-11-26 13:55:17 -08:00
|
|
|
|
ENSURE(colorAttachmentTexture->GetType() == CTexture::Type::TEXTURE_2D);
|
2022-02-05 08:59:23 -08:00
|
|
|
|
const GLenum textureTarget = GL_TEXTURE_2D;
|
|
|
|
|
|
#else
|
2022-11-26 13:55:17 -08:00
|
|
|
|
const GLenum textureTarget = colorAttachmentTexture->GetType() == CTexture::Type::TEXTURE_2D_MULTISAMPLE ?
|
2022-02-05 08:59:23 -08:00
|
|
|
|
GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
|
|
|
|
|
|
#endif
|
2022-11-26 13:55:17 -08:00
|
|
|
|
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0,
|
|
|
|
|
|
textureTarget, colorAttachmentTexture->GetHandle(), 0);
|
2022-02-05 08:59:23 -08:00
|
|
|
|
}
|
2022-11-26 13:55:17 -08:00
|
|
|
|
|
2022-02-05 08:59:23 -08:00
|
|
|
|
if (depthStencilAttachment)
|
|
|
|
|
|
{
|
2022-11-26 13:55:17 -08:00
|
|
|
|
CTexture* depthStencilAttachmentTexture = depthStencilAttachment->texture->As<CTexture>();
|
|
|
|
|
|
ENSURE(depthStencilAttachmentTexture->GetUsage() & Renderer::Backend::ITexture::Usage::DEPTH_STENCIL_ATTACHMENT);
|
2022-10-12 16:19:27 -07:00
|
|
|
|
|
2022-11-26 13:55:17 -08:00
|
|
|
|
framebuffer->m_Width = depthStencilAttachmentTexture->GetWidth();
|
|
|
|
|
|
framebuffer->m_Height = depthStencilAttachmentTexture->GetHeight();
|
2022-02-05 08:59:23 -08:00
|
|
|
|
framebuffer->m_AttachmentMask |= GL_DEPTH_BUFFER_BIT;
|
2023-01-11 22:32:52 -08:00
|
|
|
|
const bool hasStencil =
|
|
|
|
|
|
depthStencilAttachmentTexture->GetFormat() == Format::D24_UNORM_S8_UINT ||
|
|
|
|
|
|
depthStencilAttachmentTexture->GetFormat() == Format::D32_SFLOAT_S8_UINT;
|
|
|
|
|
|
if (hasStencil)
|
2022-02-05 08:59:23 -08:00
|
|
|
|
framebuffer->m_AttachmentMask |= GL_STENCIL_BUFFER_BIT;
|
|
|
|
|
|
if (colorAttachment)
|
|
|
|
|
|
{
|
2022-11-26 13:55:17 -08:00
|
|
|
|
ENSURE(colorAttachment->texture->GetWidth() == depthStencilAttachmentTexture->GetWidth());
|
|
|
|
|
|
ENSURE(colorAttachment->texture->GetHeight() == depthStencilAttachmentTexture->GetHeight());
|
|
|
|
|
|
ENSURE(colorAttachment->texture->GetType() == depthStencilAttachmentTexture->GetType());
|
2022-02-05 08:59:23 -08:00
|
|
|
|
}
|
2023-01-11 22:32:52 -08:00
|
|
|
|
ENSURE(IsDepthFormat(depthStencilAttachmentTexture->GetFormat()));
|
2022-02-05 08:59:23 -08:00
|
|
|
|
#if CONFIG2_GLES
|
2023-01-11 22:32:52 -08:00
|
|
|
|
ENSURE(depthStencilAttachmentTexture->GetFormat() == Format::D24_UNORM);
|
2022-02-05 08:59:23 -08:00
|
|
|
|
const GLenum attachment = GL_DEPTH_ATTACHMENT;
|
2022-11-26 13:55:17 -08:00
|
|
|
|
ENSURE(depthStencilAttachmentTexture->GetType() == CTexture::Type::TEXTURE_2D);
|
2022-02-05 08:59:23 -08:00
|
|
|
|
const GLenum textureTarget = GL_TEXTURE_2D;
|
|
|
|
|
|
#else
|
2023-01-11 22:32:52 -08:00
|
|
|
|
const GLenum attachment = hasStencil ?
|
2022-02-05 08:59:23 -08:00
|
|
|
|
GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT;
|
2022-11-26 13:55:17 -08:00
|
|
|
|
const GLenum textureTarget = depthStencilAttachmentTexture->GetType() == CTexture::Type::TEXTURE_2D_MULTISAMPLE ?
|
2022-02-05 08:59:23 -08:00
|
|
|
|
GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attachment,
|
2022-11-26 13:55:17 -08:00
|
|
|
|
textureTarget, depthStencilAttachmentTexture->GetHandle(), 0);
|
2022-02-05 08:59:23 -08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2022-11-26 13:55:17 -08:00
|
|
|
|
framebuffer->m_Width = colorAttachment->texture->GetWidth();
|
|
|
|
|
|
framebuffer->m_Height = colorAttachment->texture->GetHeight();
|
2022-02-05 08:59:23 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ogl_WarnIfError();
|
|
|
|
|
|
|
|
|
|
|
|
#if !CONFIG2_GLES
|
|
|
|
|
|
if (!colorAttachment)
|
|
|
|
|
|
{
|
|
|
|
|
|
glReadBuffer(GL_NONE);
|
|
|
|
|
|
glDrawBuffer(GL_NONE);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2022-11-26 13:55:17 -08:00
|
|
|
|
glDrawBuffer(GL_COLOR_ATTACHMENT0);
|
2022-02-05 08:59:23 -08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
ogl_WarnIfError();
|
|
|
|
|
|
|
2022-02-18 15:00:11 -08:00
|
|
|
|
#if !CONFIG2_GLES
|
|
|
|
|
|
if (framebuffer->m_Device->GetCapabilities().debugLabels)
|
|
|
|
|
|
{
|
|
|
|
|
|
glObjectLabel(GL_FRAMEBUFFER, framebuffer->m_Handle, -1, name);
|
|
|
|
|
|
}
|
2022-04-07 23:49:38 -07:00
|
|
|
|
#else
|
|
|
|
|
|
UNUSED2(name);
|
2022-02-18 15:00:11 -08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2022-02-05 08:59:23 -08:00
|
|
|
|
const GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
|
|
|
|
|
if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
|
|
|
|
|
|
{
|
|
|
|
|
|
LOGERROR("CFramebuffer object incomplete: 0x%04X", status);
|
|
|
|
|
|
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ogl_WarnIfError();
|
|
|
|
|
|
|
|
|
|
|
|
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
|
|
|
|
|
|
|
|
|
|
|
return framebuffer;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// static
|
2022-02-18 15:00:11 -08:00
|
|
|
|
std::unique_ptr<CFramebuffer> CFramebuffer::CreateBackbuffer(
|
2022-11-26 13:55:17 -08:00
|
|
|
|
CDevice* device,
|
2022-12-28 22:53:06 -08:00
|
|
|
|
const int surfaceDrawableWidth, const int surfaceDrawableHeight,
|
2022-11-26 13:55:17 -08:00
|
|
|
|
const AttachmentLoadOp colorAttachmentLoadOp,
|
|
|
|
|
|
const AttachmentStoreOp colorAttachmentStoreOp,
|
|
|
|
|
|
const AttachmentLoadOp depthStencilAttachmentLoadOp,
|
|
|
|
|
|
const AttachmentStoreOp depthStencilAttachmentStoreOp)
|
2022-02-05 08:59:23 -08:00
|
|
|
|
{
|
|
|
|
|
|
// Backbuffer for GL is a special case with a zero framebuffer.
|
|
|
|
|
|
std::unique_ptr<CFramebuffer> framebuffer(new CFramebuffer());
|
2022-02-18 15:00:11 -08:00
|
|
|
|
framebuffer->m_Device = device;
|
2022-02-05 08:59:23 -08:00
|
|
|
|
framebuffer->m_AttachmentMask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT;
|
2022-11-11 15:03:01 -08:00
|
|
|
|
framebuffer->m_ClearColor = CColor(0.0f, 0.0f, 0.0f, 0.0f);
|
2022-11-26 13:55:17 -08:00
|
|
|
|
framebuffer->m_ColorAttachmentLoadOp = colorAttachmentLoadOp;
|
|
|
|
|
|
framebuffer->m_ColorAttachmentStoreOp = colorAttachmentStoreOp;
|
|
|
|
|
|
framebuffer->m_DepthStencilAttachmentLoadOp = depthStencilAttachmentLoadOp;
|
|
|
|
|
|
framebuffer->m_DepthStencilAttachmentStoreOp = depthStencilAttachmentStoreOp;
|
2022-12-28 22:53:06 -08:00
|
|
|
|
framebuffer->m_Width = surfaceDrawableWidth;
|
|
|
|
|
|
framebuffer->m_Height = surfaceDrawableHeight;
|
2022-02-05 08:59:23 -08:00
|
|
|
|
return framebuffer;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CFramebuffer::CFramebuffer() = default;
|
|
|
|
|
|
|
|
|
|
|
|
CFramebuffer::~CFramebuffer()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_Handle)
|
|
|
|
|
|
glDeleteFramebuffersEXT(1, &m_Handle);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-08 15:02:46 -07:00
|
|
|
|
IDevice* CFramebuffer::GetDevice()
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_Device;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-05 08:59:23 -08:00
|
|
|
|
} // namespace GL
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Backend
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Renderer
|