Line data Source code
1 : /* Copyright (C) 2025 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/debug.h" 23 : #include "ps/CLogger.h" 24 : #include "renderer/backend/ITexture.h" 25 : #include "renderer/backend/vulkan/Device.h" 26 : #include "renderer/backend/vulkan/RenderPassManager.h" 27 : #include "renderer/backend/vulkan/Texture.h" 28 : #include "renderer/backend/vulkan/Utilities.h" 29 : 30 : namespace Renderer 31 : { 32 : 33 : namespace Backend 34 : { 35 : 36 : namespace Vulkan 37 : { 38 : 39 : // static 40 0 : std::unique_ptr<CFramebuffer> CFramebuffer::Create( 41 : CDevice* device, const char* name, 42 : SColorAttachment* colorAttachment, SDepthStencilAttachment* depthStencilAttachment) 43 : { 44 0 : ENSURE(colorAttachment || depthStencilAttachment); 45 0 : if (colorAttachment) 46 0 : ENSURE(colorAttachment->texture); 47 0 : if (depthStencilAttachment) 48 0 : ENSURE(depthStencilAttachment->texture); 49 : 50 0 : if (colorAttachment && depthStencilAttachment) 51 : { 52 0 : CTexture* colorAttachmentTexture = colorAttachment->texture->As<CTexture>(); 53 0 : CTexture* depthStencilAttachmentTexture = depthStencilAttachment->texture->As<CTexture>(); 54 0 : ENSURE( 55 : colorAttachmentTexture->GetWidth() == depthStencilAttachmentTexture->GetWidth() && 56 : colorAttachmentTexture->GetHeight() == depthStencilAttachmentTexture->GetHeight() && 57 : colorAttachmentTexture->GetSampleCount() == depthStencilAttachmentTexture->GetSampleCount()); 58 : } 59 : 60 0 : std::unique_ptr<CFramebuffer> framebuffer(new CFramebuffer()); 61 0 : framebuffer->m_Device = device; 62 0 : framebuffer->m_UID = device->GenerateNextDeviceObjectUID(); 63 0 : if (colorAttachment) 64 0 : framebuffer->m_ClearColor = colorAttachment->clearColor; 65 : 66 0 : PS::StaticVector<VkImageView, 4> attachments; 67 : 68 0 : if (colorAttachment) 69 : { 70 0 : CTexture* colorAttachmentTexture = colorAttachment->texture->As<CTexture>(); 71 0 : ENSURE(colorAttachmentTexture->GetUsage() & ITexture::Usage::COLOR_ATTACHMENT); 72 : 73 0 : framebuffer->m_Width = colorAttachmentTexture->GetWidth(); 74 0 : framebuffer->m_Height = colorAttachmentTexture->GetHeight(); 75 0 : framebuffer->m_SampleCount = colorAttachmentTexture->GetSampleCount(); 76 0 : framebuffer->m_ColorAttachmentLoadOp = colorAttachment->loadOp; 77 0 : framebuffer->m_ColorAttachmentStoreOp = colorAttachment->storeOp; 78 : 79 0 : attachments.emplace_back(colorAttachmentTexture->GetAttachmentImageView()); 80 0 : framebuffer->m_ColorAttachments.emplace_back(colorAttachmentTexture); 81 : } 82 : 83 0 : if (depthStencilAttachment) 84 : { 85 0 : CTexture* depthStencilAttachmentTexture = depthStencilAttachment->texture->As<CTexture>(); 86 0 : ENSURE(depthStencilAttachmentTexture->GetUsage() & ITexture::Usage::DEPTH_STENCIL_ATTACHMENT); 87 : 88 0 : framebuffer->m_Width = depthStencilAttachmentTexture->GetWidth(); 89 0 : framebuffer->m_Height = depthStencilAttachmentTexture->GetHeight(); 90 0 : framebuffer->m_SampleCount = depthStencilAttachmentTexture->GetSampleCount(); 91 : 92 0 : framebuffer->m_DepthStencilAttachmentLoadOp = depthStencilAttachment->loadOp; 93 0 : framebuffer->m_DepthStencilAttachmentStoreOp = depthStencilAttachment->storeOp; 94 : 95 0 : attachments.emplace_back(depthStencilAttachmentTexture->GetAttachmentImageView()); 96 0 : framebuffer->m_DepthStencilAttachment = depthStencilAttachmentTexture; 97 : } 98 : 99 0 : ENSURE(framebuffer->m_Width > 0 && framebuffer->m_Height > 0); 100 0 : ENSURE(framebuffer->m_SampleCount > 0); 101 : 102 0 : framebuffer->m_RenderPass = device->GetRenderPassManager().GetOrCreateRenderPass( 103 : colorAttachment, depthStencilAttachment); 104 : 105 0 : VkFramebufferCreateInfo framebufferInfo{}; 106 0 : framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; 107 0 : framebufferInfo.renderPass = framebuffer->m_RenderPass; 108 0 : framebufferInfo.attachmentCount = attachments.size(); 109 0 : framebufferInfo.pAttachments = attachments.data(); 110 0 : framebufferInfo.width = framebuffer->m_Width; 111 0 : framebufferInfo.height = framebuffer->m_Height; 112 0 : framebufferInfo.layers = 1; 113 : 114 0 : ENSURE_VK_SUCCESS(vkCreateFramebuffer( 115 : device->GetVkDevice(), &framebufferInfo, nullptr, &framebuffer->m_Framebuffer)); 116 : 117 0 : device->SetObjectName(VK_OBJECT_TYPE_FRAMEBUFFER, framebuffer->m_Framebuffer, name); 118 : 119 0 : return framebuffer; 120 : } 121 : 122 0 : CFramebuffer::~CFramebuffer() 123 : { 124 0 : if (m_Framebuffer != VK_NULL_HANDLE) 125 0 : m_Device->ScheduleObjectToDestroy( 126 0 : VK_OBJECT_TYPE_FRAMEBUFFER, m_Framebuffer, VK_NULL_HANDLE); 127 0 : } 128 : 129 0 : IDevice* CFramebuffer::GetDevice() 130 : { 131 0 : return m_Device; 132 : } 133 : 134 : } // namespace Vulkan 135 : 136 : } // namespace Backend 137 : 138 : } // namespace Renderer