Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "Framebuffer.h" 21 : 22 : #include "lib/config2.h" 23 : #include "lib/debug.h" 24 : #include "ps/CLogger.h" 25 : #include "renderer/backend/Format.h" 26 : #include "renderer/backend/ITexture.h" 27 : #include "renderer/backend/gl/Device.h" 28 : #include "renderer/backend/gl/Texture.h" 29 : 30 : namespace Renderer 31 : { 32 : 33 : namespace Backend 34 : { 35 : 36 : namespace GL 37 : { 38 : 39 : // static 40 0 : std::unique_ptr<CFramebuffer> CFramebuffer::Create( 41 : CDevice* device, [[maybe_unused]] const char* name, SColorAttachment* colorAttachment, 42 : SDepthStencilAttachment* depthStencilAttachment) 43 : { 44 0 : ENSURE(colorAttachment || depthStencilAttachment); 45 : 46 0 : std::unique_ptr<CFramebuffer> framebuffer(new CFramebuffer()); 47 0 : framebuffer->m_Device = device; 48 0 : if (colorAttachment) 49 : { 50 0 : framebuffer->m_ClearColor = colorAttachment->clearColor; 51 0 : framebuffer->m_ColorAttachmentLoadOp = colorAttachment->loadOp; 52 0 : framebuffer->m_ColorAttachmentStoreOp = colorAttachment->storeOp; 53 : } 54 0 : if (depthStencilAttachment) 55 : { 56 0 : framebuffer->m_DepthStencilAttachmentLoadOp = depthStencilAttachment->loadOp; 57 0 : framebuffer->m_DepthStencilAttachmentStoreOp = depthStencilAttachment->storeOp; 58 : } 59 : 60 0 : glGenFramebuffersEXT(1, &framebuffer->m_Handle); 61 0 : if (!framebuffer->m_Handle) 62 : { 63 0 : LOGERROR("Failed to create CFramebuffer object"); 64 0 : return nullptr; 65 : } 66 0 : glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer->m_Handle); 67 : 68 0 : if (colorAttachment) 69 : { 70 0 : CTexture* colorAttachmentTexture = colorAttachment->texture->As<CTexture>(); 71 0 : ENSURE(device->IsFramebufferFormatSupported(colorAttachmentTexture->GetFormat())); 72 0 : ENSURE(colorAttachmentTexture->GetUsage() & Renderer::Backend::ITexture::Usage::COLOR_ATTACHMENT); 73 : 74 0 : framebuffer->m_AttachmentMask |= GL_COLOR_BUFFER_BIT; 75 : 76 : #if CONFIG2_GLES 77 : ENSURE(colorAttachmentTexture->GetType() == CTexture::Type::TEXTURE_2D); 78 : const GLenum textureTarget = GL_TEXTURE_2D; 79 : #else 80 0 : const GLenum textureTarget = colorAttachmentTexture->GetType() == CTexture::Type::TEXTURE_2D_MULTISAMPLE ? 81 0 : GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D; 82 : #endif 83 0 : glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0, 84 : textureTarget, colorAttachmentTexture->GetHandle(), 0); 85 : } 86 : 87 0 : if (depthStencilAttachment) 88 : { 89 0 : CTexture* depthStencilAttachmentTexture = depthStencilAttachment->texture->As<CTexture>(); 90 0 : ENSURE(depthStencilAttachmentTexture->GetUsage() & Renderer::Backend::ITexture::Usage::DEPTH_STENCIL_ATTACHMENT); 91 : 92 0 : framebuffer->m_Width = depthStencilAttachmentTexture->GetWidth(); 93 0 : framebuffer->m_Height = depthStencilAttachmentTexture->GetHeight(); 94 0 : framebuffer->m_AttachmentMask |= GL_DEPTH_BUFFER_BIT; 95 0 : const bool hasStencil = 96 0 : depthStencilAttachmentTexture->GetFormat() == Format::D24_UNORM_S8_UINT || 97 0 : depthStencilAttachmentTexture->GetFormat() == Format::D32_SFLOAT_S8_UINT; 98 0 : if (hasStencil) 99 0 : framebuffer->m_AttachmentMask |= GL_STENCIL_BUFFER_BIT; 100 0 : if (colorAttachment) 101 : { 102 0 : ENSURE(colorAttachment->texture->GetWidth() == depthStencilAttachmentTexture->GetWidth()); 103 0 : ENSURE(colorAttachment->texture->GetHeight() == depthStencilAttachmentTexture->GetHeight()); 104 0 : ENSURE(colorAttachment->texture->GetType() == depthStencilAttachmentTexture->GetType()); 105 : } 106 0 : ENSURE(IsDepthFormat(depthStencilAttachmentTexture->GetFormat())); 107 : #if CONFIG2_GLES 108 : ENSURE(depthStencilAttachmentTexture->GetFormat() == Format::D24_UNORM); 109 : const GLenum attachment = GL_DEPTH_ATTACHMENT; 110 : ENSURE(depthStencilAttachmentTexture->GetType() == CTexture::Type::TEXTURE_2D); 111 : const GLenum textureTarget = GL_TEXTURE_2D; 112 : #else 113 0 : const GLenum attachment = hasStencil ? 114 : GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT; 115 0 : const GLenum textureTarget = depthStencilAttachmentTexture->GetType() == CTexture::Type::TEXTURE_2D_MULTISAMPLE ? 116 0 : GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D; 117 : #endif 118 0 : glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attachment, 119 : textureTarget, depthStencilAttachmentTexture->GetHandle(), 0); 120 : } 121 : else 122 : { 123 0 : framebuffer->m_Width = colorAttachment->texture->GetWidth(); 124 0 : framebuffer->m_Height = colorAttachment->texture->GetHeight(); 125 : } 126 : 127 : #if !CONFIG2_GLES 128 0 : if (!colorAttachment) 129 : { 130 0 : glReadBuffer(GL_NONE); 131 0 : glDrawBuffer(GL_NONE); 132 : } 133 : else 134 0 : glDrawBuffer(GL_COLOR_ATTACHMENT0); 135 : #endif 136 : 137 : #if !CONFIG2_GLES 138 0 : if (framebuffer->m_Device->GetCapabilities().debugLabels) 139 : { 140 0 : glObjectLabel(GL_FRAMEBUFFER, framebuffer->m_Handle, -1, name); 141 : } 142 : #endif 143 : 144 0 : const GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); 145 0 : if (status != GL_FRAMEBUFFER_COMPLETE_EXT) 146 : { 147 0 : LOGERROR("CFramebuffer object incomplete: 0x{:04X}", status); 148 0 : glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 149 0 : return nullptr; 150 : } 151 : 152 0 : glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 153 : 154 0 : return framebuffer; 155 : } 156 : 157 : // static 158 0 : std::unique_ptr<CFramebuffer> CFramebuffer::CreateBackbuffer( 159 : CDevice* device, 160 : const int surfaceDrawableWidth, const int surfaceDrawableHeight, 161 : const AttachmentLoadOp colorAttachmentLoadOp, 162 : const AttachmentStoreOp colorAttachmentStoreOp, 163 : const AttachmentLoadOp depthStencilAttachmentLoadOp, 164 : const AttachmentStoreOp depthStencilAttachmentStoreOp) 165 : { 166 : // Backbuffer for GL is a special case with a zero framebuffer. 167 0 : std::unique_ptr<CFramebuffer> framebuffer(new CFramebuffer()); 168 0 : framebuffer->m_Device = device; 169 0 : framebuffer->m_AttachmentMask = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT; 170 0 : framebuffer->m_ClearColor = CColor(0.0f, 0.0f, 0.0f, 0.0f); 171 0 : framebuffer->m_ColorAttachmentLoadOp = colorAttachmentLoadOp; 172 0 : framebuffer->m_ColorAttachmentStoreOp = colorAttachmentStoreOp; 173 0 : framebuffer->m_DepthStencilAttachmentLoadOp = depthStencilAttachmentLoadOp; 174 0 : framebuffer->m_DepthStencilAttachmentStoreOp = depthStencilAttachmentStoreOp; 175 0 : framebuffer->m_Width = surfaceDrawableWidth; 176 0 : framebuffer->m_Height = surfaceDrawableHeight; 177 0 : return framebuffer; 178 : } 179 : 180 0 : CFramebuffer::CFramebuffer() = default; 181 : 182 0 : CFramebuffer::~CFramebuffer() 183 : { 184 0 : if (m_Handle) 185 0 : glDeleteFramebuffersEXT(1, &m_Handle); 186 0 : } 187 : 188 0 : IDevice* CFramebuffer::GetDevice() 189 : { 190 0 : return m_Device; 191 : } 192 : 193 : } // namespace GL 194 : 195 : } // namespace Backend 196 : 197 : } // namespace Renderer