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 "RenderPassManager.h" 21 : 22 : #include "lib/hash.h" 23 : #include "ps/CLogger.h" 24 : #include "ps/containers/StaticVector.h" 25 : #include "renderer/backend/IFramebuffer.h" 26 : #include "renderer/backend/ITexture.h" 27 : #include "renderer/backend/vulkan/Device.h" 28 : #include "renderer/backend/vulkan/Mapping.h" 29 : #include "renderer/backend/vulkan/Texture.h" 30 : #include "renderer/backend/vulkan/Utilities.h" 31 : 32 : #include <utility> 33 : 34 : namespace Renderer 35 : { 36 : 37 : namespace Backend 38 : { 39 : 40 : namespace Vulkan 41 : { 42 : 43 0 : size_t CRenderPassManager::DescHash::operator()(const Desc& desc) const 44 : { 45 0 : size_t seed = 0; 46 0 : hash_combine(seed, desc.sampleCount); 47 : 48 0 : if (desc.colorAttachment.has_value()) 49 : { 50 0 : hash_combine(seed, (*desc.colorAttachment).format); 51 0 : hash_combine(seed, (*desc.colorAttachment).loadOp); 52 0 : hash_combine(seed, (*desc.colorAttachment).storeOp); 53 : } 54 : else 55 0 : hash_combine(seed, VK_FORMAT_UNDEFINED); 56 : 57 0 : if (desc.depthStencilAttachment.has_value()) 58 : { 59 0 : hash_combine(seed, (*desc.depthStencilAttachment).format); 60 0 : hash_combine(seed, (*desc.depthStencilAttachment).loadOp); 61 0 : hash_combine(seed, (*desc.depthStencilAttachment).storeOp); 62 : } 63 : else 64 0 : hash_combine(seed, VK_FORMAT_UNDEFINED); 65 : 66 0 : return seed; 67 : } 68 : 69 0 : bool CRenderPassManager::DescEqual::operator()(const Desc& lhs, const Desc& rhs) const 70 : { 71 0 : auto compareAttachments = [](const std::optional<Attachment>& lhs, const std::optional<Attachment>& rhs) -> bool 72 : { 73 0 : if (lhs.has_value() != rhs.has_value()) 74 : return false; 75 0 : if (!lhs.has_value()) 76 : return true; 77 0 : return 78 0 : (*lhs).format == (*rhs).format && 79 0 : (*lhs).loadOp == (*rhs).loadOp && 80 0 : (*lhs).storeOp == (*rhs).storeOp; 81 : }; 82 0 : if (!compareAttachments(lhs.colorAttachment, rhs.colorAttachment)) 83 : return false; 84 0 : if (!compareAttachments(lhs.depthStencilAttachment, rhs.depthStencilAttachment)) 85 : return false; 86 0 : return lhs.sampleCount == rhs.sampleCount; 87 : } 88 : 89 0 : CRenderPassManager::CRenderPassManager(CDevice* device) 90 0 : : m_Device(device) 91 : { 92 0 : } 93 : 94 0 : CRenderPassManager::~CRenderPassManager() 95 : { 96 0 : for (const auto& it : m_RenderPassMap) 97 0 : if (it.second != VK_NULL_HANDLE) 98 : { 99 0 : m_Device->ScheduleObjectToDestroy( 100 0 : VK_OBJECT_TYPE_RENDER_PASS, it.second, VK_NULL_HANDLE); 101 : } 102 0 : m_RenderPassMap.clear(); 103 0 : } 104 : 105 0 : VkRenderPass CRenderPassManager::GetOrCreateRenderPass( 106 : SColorAttachment* colorAttachment, 107 : SDepthStencilAttachment* depthStencilAttachment) 108 : { 109 0 : Desc desc{}; 110 0 : if (colorAttachment) 111 : { 112 0 : CTexture* colorAttachmentTexture = colorAttachment->texture->As<CTexture>(); 113 0 : desc.sampleCount = colorAttachmentTexture->GetSampleCount(); 114 0 : desc.colorAttachment.emplace(Attachment{ 115 0 : colorAttachmentTexture->GetVkFormat(), 116 0 : colorAttachment->loadOp, 117 0 : colorAttachment->storeOp}); 118 : } 119 0 : if (depthStencilAttachment) 120 : { 121 0 : CTexture* depthStencilAttachmentTexture = depthStencilAttachment->texture->As<CTexture>(); 122 0 : desc.sampleCount = depthStencilAttachmentTexture->GetSampleCount(); 123 0 : desc.depthStencilAttachment.emplace(Attachment{ 124 0 : depthStencilAttachmentTexture->GetVkFormat(), 125 0 : depthStencilAttachment->loadOp, 126 0 : depthStencilAttachment->storeOp}); 127 : } 128 0 : auto it = m_RenderPassMap.find(desc); 129 0 : if (it != m_RenderPassMap.end()) 130 0 : return it->second; 131 : 132 0 : uint32_t attachmentCount = 0; 133 0 : PS::StaticVector<VkAttachmentDescription, 4> attachmentDescs; 134 0 : std::optional<VkAttachmentReference> colorAttachmentRef; 135 0 : std::optional<VkAttachmentReference> depthStencilAttachmentRef; 136 : 137 0 : if (colorAttachment) 138 : { 139 0 : CTexture* colorAttachmentTexture = colorAttachment->texture->As<CTexture>(); 140 : 141 0 : VkAttachmentDescription colorAttachmentDesc{}; 142 0 : colorAttachmentDesc.format = colorAttachmentTexture->GetVkFormat(); 143 0 : colorAttachmentDesc.samples = Mapping::FromSampleCount(colorAttachmentTexture->GetSampleCount()); 144 0 : colorAttachmentDesc.loadOp = Mapping::FromAttachmentLoadOp(colorAttachment->loadOp); 145 0 : colorAttachmentDesc.storeOp = Mapping::FromAttachmentStoreOp(colorAttachment->storeOp); 146 0 : colorAttachmentDesc.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; 147 0 : colorAttachmentDesc.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; 148 : 149 0 : attachmentDescs.emplace_back(std::move(colorAttachmentDesc)); 150 : 151 0 : colorAttachmentRef.emplace(VkAttachmentReference{ 152 0 : attachmentCount++, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL}); 153 : } 154 : 155 0 : if (depthStencilAttachment) 156 : { 157 0 : CTexture* depthStencilAttachmentTexture = depthStencilAttachment->texture->As<CTexture>(); 158 : 159 0 : VkAttachmentDescription depthStencilAttachmentDesc{}; 160 0 : depthStencilAttachmentDesc.format = depthStencilAttachmentTexture->GetVkFormat(); 161 0 : depthStencilAttachmentDesc.samples = Mapping::FromSampleCount(depthStencilAttachmentTexture->GetSampleCount()); 162 0 : depthStencilAttachmentDesc.loadOp = Mapping::FromAttachmentLoadOp(depthStencilAttachment->loadOp); 163 0 : depthStencilAttachmentDesc.storeOp = Mapping::FromAttachmentStoreOp(depthStencilAttachment->storeOp); 164 0 : depthStencilAttachmentDesc.stencilLoadOp = depthStencilAttachmentDesc.loadOp; 165 0 : depthStencilAttachmentDesc.stencilStoreOp = depthStencilAttachmentDesc.storeOp; 166 0 : depthStencilAttachmentDesc.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; 167 0 : depthStencilAttachmentDesc.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; 168 : 169 0 : attachmentDescs.emplace_back(std::move(depthStencilAttachmentDesc)); 170 : 171 0 : depthStencilAttachmentRef.emplace(VkAttachmentReference{ 172 0 : attachmentCount++, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL}); 173 : } 174 : 175 0 : VkSubpassDescription subpassDesc{}; 176 0 : subpassDesc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; 177 0 : if (colorAttachment) 178 : { 179 0 : subpassDesc.colorAttachmentCount = 1; 180 0 : subpassDesc.pColorAttachments = &(*colorAttachmentRef); 181 : } 182 0 : if (depthStencilAttachment) 183 0 : subpassDesc.pDepthStencilAttachment = &(*depthStencilAttachmentRef); 184 : 185 0 : VkRenderPassCreateInfo renderPassCreateInfo{}; 186 0 : renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; 187 0 : renderPassCreateInfo.attachmentCount = attachmentDescs.size(); 188 0 : renderPassCreateInfo.pAttachments = attachmentDescs.data(); 189 0 : renderPassCreateInfo.subpassCount = 1; 190 0 : renderPassCreateInfo.pSubpasses = &subpassDesc; 191 : 192 0 : VkRenderPass renderPass = VK_NULL_HANDLE; 193 0 : ENSURE_VK_SUCCESS( 194 : vkCreateRenderPass( 195 : m_Device->GetVkDevice(), &renderPassCreateInfo, nullptr, &renderPass)); 196 0 : it = m_RenderPassMap.emplace(desc, renderPass).first; 197 : 198 0 : return renderPass; 199 0 : } 200 : 201 0 : void CRenderPassManager::CollectStatistics(IDevice::StatisticsVector& statistics) const 202 : { 203 0 : statistics.emplace_back("VkRenderPass count", "", static_cast<uint32_t>(m_RenderPassMap.size())); 204 0 : } 205 : 206 : } // namespace Vulkan 207 : 208 : } // namespace Backend 209 : 210 : } // namespace Renderer