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 "PipelineState.h" 21 : 22 : #include "lib/debug.h" 23 : #include "lib/hash.h" 24 : #include "ps/CLogger.h" 25 : #include "ps/containers/StaticVector.h" 26 : #include "renderer/backend/IShaderProgram.h" 27 : #include "renderer/backend/vulkan/Device.h" 28 : #include "renderer/backend/vulkan/DeviceSelection.h" 29 : #include "renderer/backend/vulkan/Framebuffer.h" 30 : #include "renderer/backend/vulkan/Mapping.h" 31 : #include "renderer/backend/vulkan/ShaderProgram.h" 32 : #include "renderer/backend/vulkan/Utilities.h" 33 : 34 : #include <algorithm> 35 : #include <cstdint> 36 : #include <iterator> 37 : #include <limits> 38 : #include <utility> 39 : #include <vector> 40 : 41 : namespace Renderer 42 : { 43 : 44 : namespace Backend 45 : { 46 : 47 : namespace Vulkan 48 : { 49 : 50 : namespace 51 : { 52 : 53 0 : VkStencilOpState MakeStencilOpState(const SStencilOpState& opState) 54 : { 55 0 : VkStencilOpState result{}; 56 0 : result.failOp = Mapping::FromStencilOp(opState.failOp); 57 0 : result.passOp = Mapping::FromStencilOp(opState.passOp); 58 0 : result.depthFailOp = Mapping::FromStencilOp(opState.depthFailOp); 59 0 : result.compareOp = Mapping::FromCompareOp(opState.compareOp); 60 0 : return result; 61 : } 62 : 63 : } // anonymous namespace 64 : 65 0 : size_t CGraphicsPipelineState::CacheKeyHash::operator()(const CacheKey& cacheKey) const 66 : { 67 0 : size_t seed = 0; 68 0 : hash_combine(seed, cacheKey.vertexInputLayoutUID); 69 0 : hash_combine(seed, cacheKey.framebufferUID); 70 0 : return seed; 71 : } 72 : 73 0 : bool CGraphicsPipelineState::CacheKeyEqual::operator()(const CacheKey& lhs, const CacheKey& rhs) const 74 : { 75 0 : return 76 0 : lhs.vertexInputLayoutUID == rhs.vertexInputLayoutUID && 77 0 : lhs.framebufferUID == rhs.framebufferUID; 78 : } 79 : 80 : // static 81 0 : std::unique_ptr<CGraphicsPipelineState> CGraphicsPipelineState::Create( 82 : CDevice* device, const SGraphicsPipelineStateDesc& desc) 83 : { 84 0 : ENSURE(desc.shaderProgram); 85 0 : std::unique_ptr<CGraphicsPipelineState> pipelineState{new CGraphicsPipelineState()}; 86 0 : pipelineState->m_Device = device; 87 0 : pipelineState->m_UID = device->GenerateNextDeviceObjectUID(); 88 0 : pipelineState->m_Desc = desc; 89 0 : return pipelineState; 90 : } 91 : 92 0 : CGraphicsPipelineState::~CGraphicsPipelineState() 93 : { 94 0 : for (const auto& it : m_PipelineMap) 95 : { 96 0 : if (it.second != VK_NULL_HANDLE) 97 0 : m_Device->ScheduleObjectToDestroy( 98 0 : VK_OBJECT_TYPE_PIPELINE, it.second, VK_NULL_HANDLE); 99 : } 100 0 : } 101 : 102 0 : VkPipeline CGraphicsPipelineState::GetOrCreatePipeline( 103 : const CVertexInputLayout* vertexInputLayout, CFramebuffer* framebuffer) 104 : { 105 0 : CShaderProgram* shaderProgram = m_Desc.shaderProgram->As<CShaderProgram>(); 106 : 107 0 : const CacheKey cacheKey = 108 : { 109 0 : vertexInputLayout->GetUID(), framebuffer->GetUID() 110 0 : }; 111 0 : auto it = m_PipelineMap.find(cacheKey); 112 0 : if (it != m_PipelineMap.end()) 113 0 : return it->second; 114 : 115 0 : PS::StaticVector<VkVertexInputBindingDescription, 16> attributeBindings; 116 0 : PS::StaticVector<VkVertexInputAttributeDescription, 16> attributes; 117 : 118 0 : const VkPhysicalDeviceLimits& limits = m_Device->GetChoosenPhysicalDevice().properties.limits; 119 0 : const uint32_t maxVertexInputAttributes = limits.maxVertexInputAttributes; 120 0 : const uint32_t maxVertexInputAttributeOffset = limits.maxVertexInputAttributeOffset; 121 0 : for (const SVertexAttributeFormat& vertexAttributeFormat : vertexInputLayout->GetAttributes()) 122 : { 123 0 : ENSURE(vertexAttributeFormat.bindingSlot < maxVertexInputAttributes); 124 0 : ENSURE(vertexAttributeFormat.offset < maxVertexInputAttributeOffset); 125 0 : const uint32_t streamLocation = shaderProgram->GetStreamLocation(vertexAttributeFormat.stream); 126 0 : if (streamLocation == std::numeric_limits<uint32_t>::max()) 127 0 : continue; 128 0 : auto it = std::find_if(attributeBindings.begin(), attributeBindings.end(), 129 0 : [slot = vertexAttributeFormat.bindingSlot](const VkVertexInputBindingDescription& desc) -> bool 130 : { 131 0 : return desc.binding == slot; 132 : }); 133 0 : const VkVertexInputBindingDescription desc{ 134 : vertexAttributeFormat.bindingSlot, 135 0 : vertexAttributeFormat.stride, 136 0 : vertexAttributeFormat.rate == VertexAttributeRate::PER_INSTANCE 137 0 : ? VK_VERTEX_INPUT_RATE_INSTANCE 138 0 : : VK_VERTEX_INPUT_RATE_VERTEX }; 139 0 : if (it == attributeBindings.end()) 140 0 : attributeBindings.emplace_back(desc); 141 : else 142 : { 143 : // All attribute sharing the same binding slot should have the same description. 144 0 : ENSURE(desc.inputRate == it->inputRate && desc.stride == it->stride); 145 : } 146 0 : attributes.push_back({ 147 : streamLocation, 148 0 : vertexAttributeFormat.bindingSlot, 149 0 : Mapping::FromFormat(vertexAttributeFormat.format), 150 0 : vertexAttributeFormat.offset 151 : }); 152 : } 153 : 154 0 : VkPipelineVertexInputStateCreateInfo vertexInputCreateInfo{}; 155 0 : vertexInputCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; 156 0 : vertexInputCreateInfo.vertexBindingDescriptionCount = std::size(attributeBindings); 157 0 : vertexInputCreateInfo.pVertexBindingDescriptions = attributeBindings.data(); 158 0 : vertexInputCreateInfo.vertexAttributeDescriptionCount = std::size(attributes); 159 0 : vertexInputCreateInfo.pVertexAttributeDescriptions = attributes.data(); 160 : 161 0 : VkPipelineInputAssemblyStateCreateInfo inputAssemblyCreateInfo{}; 162 0 : inputAssemblyCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; 163 0 : inputAssemblyCreateInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; 164 0 : inputAssemblyCreateInfo.primitiveRestartEnable = VK_FALSE; 165 : 166 : // We don't need to specify sizes for viewports and scissors as they're in 167 : // dynamic state. 168 0 : VkViewport viewport{}; 169 0 : viewport.x = 0.0f; 170 0 : viewport.y = 0.0f; 171 0 : viewport.width = 0.0f; 172 0 : viewport.height = 0.0f; 173 0 : viewport.minDepth = 0.0f; 174 0 : viewport.maxDepth = 1.0f; 175 : 176 0 : VkRect2D scissor{}; 177 : 178 0 : VkPipelineViewportStateCreateInfo viewportStateCreateInfo{}; 179 0 : viewportStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; 180 0 : viewportStateCreateInfo.viewportCount = 1; 181 0 : viewportStateCreateInfo.pViewports = &viewport; 182 0 : viewportStateCreateInfo.scissorCount = 1; 183 0 : viewportStateCreateInfo.pScissors = &scissor; 184 : 185 0 : VkPipelineDepthStencilStateCreateInfo depthStencilStateCreateInfo{}; 186 0 : depthStencilStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; 187 0 : depthStencilStateCreateInfo.depthTestEnable = 188 0 : m_Desc.depthStencilState.depthTestEnabled ? VK_TRUE : VK_FALSE; 189 0 : depthStencilStateCreateInfo.depthWriteEnable = 190 0 : m_Desc.depthStencilState.depthWriteEnabled ? VK_TRUE : VK_FALSE; 191 0 : depthStencilStateCreateInfo.depthCompareOp = 192 0 : Mapping::FromCompareOp(m_Desc.depthStencilState.depthCompareOp); 193 0 : depthStencilStateCreateInfo.stencilTestEnable = 194 0 : m_Desc.depthStencilState.stencilTestEnabled ? VK_TRUE : VK_FALSE; 195 : 196 0 : depthStencilStateCreateInfo.front = 197 0 : MakeStencilOpState(m_Desc.depthStencilState.stencilFrontFace); 198 0 : depthStencilStateCreateInfo.front.reference = m_Desc.depthStencilState.stencilReference; 199 0 : depthStencilStateCreateInfo.front.compareMask = m_Desc.depthStencilState.stencilReadMask; 200 0 : depthStencilStateCreateInfo.front.writeMask = m_Desc.depthStencilState.stencilWriteMask; 201 : 202 0 : depthStencilStateCreateInfo.back = 203 0 : MakeStencilOpState(m_Desc.depthStencilState.stencilBackFace); 204 0 : depthStencilStateCreateInfo.back.reference = m_Desc.depthStencilState.stencilReference; 205 0 : depthStencilStateCreateInfo.back.compareMask = m_Desc.depthStencilState.stencilReadMask; 206 0 : depthStencilStateCreateInfo.back.writeMask = m_Desc.depthStencilState.stencilWriteMask; 207 : 208 0 : VkPipelineRasterizationStateCreateInfo rasterizationStateCreateInfo{}; 209 0 : rasterizationStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; 210 0 : rasterizationStateCreateInfo.depthClampEnable = VK_FALSE; 211 0 : rasterizationStateCreateInfo.rasterizerDiscardEnable = VK_FALSE; 212 : 213 0 : const PolygonMode polygonMode = 214 0 : m_Device->GetChoosenPhysicalDevice().features.fillModeNonSolid 215 0 : ? m_Desc.rasterizationState.polygonMode : PolygonMode::FILL; 216 0 : rasterizationStateCreateInfo.polygonMode = 217 0 : Mapping::FromPolygonMode(polygonMode); 218 0 : rasterizationStateCreateInfo.cullMode = 219 0 : Mapping::FromCullMode(m_Desc.rasterizationState.cullMode); 220 0 : rasterizationStateCreateInfo.frontFace = 221 0 : m_Desc.rasterizationState.frontFace == FrontFace::CLOCKWISE 222 0 : ? VK_FRONT_FACE_CLOCKWISE : VK_FRONT_FACE_COUNTER_CLOCKWISE; 223 : 224 0 : rasterizationStateCreateInfo.depthBiasEnable = 225 0 : m_Desc.rasterizationState.depthBiasEnabled ? VK_TRUE : VK_FALSE; 226 0 : rasterizationStateCreateInfo.depthBiasConstantFactor = 227 0 : m_Desc.rasterizationState.depthBiasConstantFactor; 228 0 : rasterizationStateCreateInfo.depthBiasSlopeFactor = 229 0 : m_Desc.rasterizationState.depthBiasSlopeFactor; 230 0 : rasterizationStateCreateInfo.lineWidth = 1.0f; 231 : 232 0 : VkPipelineMultisampleStateCreateInfo multisampleStateCreateInfo{}; 233 0 : multisampleStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; 234 0 : multisampleStateCreateInfo.rasterizationSamples = 235 0 : Mapping::FromSampleCount(framebuffer->GetSampleCount()); 236 0 : multisampleStateCreateInfo.minSampleShading = 1.0f; 237 : 238 0 : VkPipelineColorBlendAttachmentState colorBlendAttachmentState{}; 239 0 : colorBlendAttachmentState.blendEnable = m_Desc.blendState.enabled ? VK_TRUE : VK_FALSE; 240 0 : colorBlendAttachmentState.colorBlendOp = 241 0 : Mapping::FromBlendOp(m_Desc.blendState.colorBlendOp); 242 0 : colorBlendAttachmentState.srcColorBlendFactor = 243 0 : Mapping::FromBlendFactor(m_Desc.blendState.srcColorBlendFactor); 244 0 : colorBlendAttachmentState.dstColorBlendFactor = 245 0 : Mapping::FromBlendFactor(m_Desc.blendState.dstColorBlendFactor); 246 0 : colorBlendAttachmentState.alphaBlendOp = 247 0 : Mapping::FromBlendOp(m_Desc.blendState.alphaBlendOp); 248 0 : colorBlendAttachmentState.srcAlphaBlendFactor = 249 0 : Mapping::FromBlendFactor(m_Desc.blendState.srcAlphaBlendFactor); 250 0 : colorBlendAttachmentState.dstAlphaBlendFactor = 251 0 : Mapping::FromBlendFactor(m_Desc.blendState.dstAlphaBlendFactor); 252 0 : colorBlendAttachmentState.colorWriteMask = 253 0 : Mapping::FromColorWriteMask(m_Desc.blendState.colorWriteMask); 254 : 255 0 : VkPipelineColorBlendStateCreateInfo colorBlendStateCreateInfo{}; 256 0 : colorBlendStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; 257 0 : colorBlendStateCreateInfo.logicOpEnable = VK_FALSE; 258 0 : colorBlendStateCreateInfo.logicOp = VK_LOGIC_OP_CLEAR; 259 0 : colorBlendStateCreateInfo.attachmentCount = 1; 260 0 : colorBlendStateCreateInfo.pAttachments = &colorBlendAttachmentState; 261 0 : colorBlendStateCreateInfo.blendConstants[0] = m_Desc.blendState.constant.r; 262 0 : colorBlendStateCreateInfo.blendConstants[1] = m_Desc.blendState.constant.g; 263 0 : colorBlendStateCreateInfo.blendConstants[2] = m_Desc.blendState.constant.b; 264 0 : colorBlendStateCreateInfo.blendConstants[3] = m_Desc.blendState.constant.a; 265 : 266 0 : const VkDynamicState dynamicStates[] = 267 : { 268 : VK_DYNAMIC_STATE_SCISSOR, 269 : VK_DYNAMIC_STATE_VIEWPORT 270 : }; 271 : 272 0 : VkPipelineDynamicStateCreateInfo dynamicStateCreateInfo{}; 273 0 : dynamicStateCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; 274 0 : dynamicStateCreateInfo.dynamicStateCount = static_cast<uint32_t>(std::size(dynamicStates)); 275 0 : dynamicStateCreateInfo.pDynamicStates = dynamicStates; 276 : 277 0 : VkGraphicsPipelineCreateInfo pipelineCreateInfo{}; 278 0 : pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; 279 : 280 0 : pipelineCreateInfo.stageCount = shaderProgram->GetStages().size(); 281 0 : pipelineCreateInfo.pStages = shaderProgram->GetStages().data(); 282 : 283 0 : pipelineCreateInfo.pVertexInputState = &vertexInputCreateInfo; 284 0 : pipelineCreateInfo.pInputAssemblyState = &inputAssemblyCreateInfo; 285 0 : pipelineCreateInfo.pViewportState = &viewportStateCreateInfo; 286 0 : pipelineCreateInfo.pRasterizationState = &rasterizationStateCreateInfo; 287 0 : pipelineCreateInfo.pMultisampleState = &multisampleStateCreateInfo; 288 : // If renderPass is not VK_NULL_HANDLE, the pipeline is being created with 289 : // fragment shader state, and subpass uses a depth/stencil attachment, 290 : // pDepthStencilState must be a not null pointer. 291 0 : if (framebuffer->GetDepthStencilAttachment()) 292 0 : pipelineCreateInfo.pDepthStencilState = &depthStencilStateCreateInfo; 293 0 : if (!framebuffer->GetColorAttachments().empty()) 294 0 : pipelineCreateInfo.pColorBlendState = &colorBlendStateCreateInfo; 295 0 : pipelineCreateInfo.pDynamicState = &dynamicStateCreateInfo; 296 : 297 0 : pipelineCreateInfo.layout = shaderProgram->GetPipelineLayout(); 298 0 : pipelineCreateInfo.renderPass = framebuffer->GetRenderPass(); 299 0 : pipelineCreateInfo.subpass = 0; 300 0 : pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE; 301 0 : pipelineCreateInfo.basePipelineIndex = -1; 302 : 303 0 : VkPipeline pipeline = VK_NULL_HANDLE; 304 0 : ENSURE_VK_SUCCESS(vkCreateGraphicsPipelines( 305 : m_Device->GetVkDevice(), VK_NULL_HANDLE, 1, &pipelineCreateInfo, nullptr, &pipeline)); 306 : 307 0 : m_PipelineMap[cacheKey] = pipeline; 308 : 309 0 : return pipeline; 310 0 : } 311 : 312 0 : IDevice* CGraphicsPipelineState::GetDevice() 313 : { 314 0 : return m_Device; 315 : } 316 : 317 : // static 318 0 : std::unique_ptr<CComputePipelineState> CComputePipelineState::Create( 319 : CDevice* device, const SComputePipelineStateDesc& desc) 320 : { 321 0 : ENSURE(desc.shaderProgram); 322 0 : CShaderProgram* shaderProgram = desc.shaderProgram->As<CShaderProgram>(); 323 0 : if (shaderProgram->GetStages().empty()) 324 0 : return nullptr; 325 : 326 0 : std::unique_ptr<CComputePipelineState> pipelineState{new CComputePipelineState()}; 327 0 : pipelineState->m_Device = device; 328 0 : pipelineState->m_UID = device->GenerateNextDeviceObjectUID(); 329 0 : pipelineState->m_Desc = desc; 330 : 331 0 : VkComputePipelineCreateInfo pipelineCreateInfo{}; 332 0 : pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; 333 : 334 0 : pipelineCreateInfo.layout = shaderProgram->GetPipelineLayout(); 335 0 : pipelineCreateInfo.basePipelineHandle = VK_NULL_HANDLE; 336 0 : pipelineCreateInfo.basePipelineIndex = -1; 337 0 : pipelineCreateInfo.stage = shaderProgram->GetStages()[0]; 338 : 339 0 : ENSURE_VK_SUCCESS(vkCreateComputePipelines( 340 : device->GetVkDevice(), VK_NULL_HANDLE, 1, &pipelineCreateInfo, nullptr, &pipelineState->m_Pipeline)); 341 0 : return pipelineState; 342 : } 343 : 344 0 : CComputePipelineState::~CComputePipelineState() 345 : { 346 0 : if (m_Pipeline != VK_NULL_HANDLE) 347 0 : m_Device->ScheduleObjectToDestroy( 348 0 : VK_OBJECT_TYPE_PIPELINE, m_Pipeline, VK_NULL_HANDLE); 349 0 : } 350 : 351 0 : IDevice* CComputePipelineState::GetDevice() 352 : { 353 0 : return m_Device; 354 : } 355 : 356 : } // namespace Vulkan 357 : 358 : } // namespace Backend 359 : 360 : } // namespace Renderer