LCOV - code coverage report
Current view: top level - source/renderer/backend/vulkan - Utilities.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 94 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 7 0.0 %

          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 "Utilities.h"
      21             : 
      22             : #include "renderer/backend/vulkan/Buffer.h"
      23             : #include "renderer/backend/vulkan/Texture.h"
      24             : 
      25             : namespace Renderer
      26             : {
      27             : 
      28             : namespace Backend
      29             : {
      30             : 
      31             : namespace Vulkan
      32             : {
      33             : 
      34             : namespace Utilities
      35             : {
      36             : 
      37           0 : void SetTextureLayout(
      38             :     VkCommandBuffer commandBuffer, CTexture* texture,
      39             :     const VkImageLayout oldLayout, const VkImageLayout newLayout,
      40             :     const VkAccessFlags srcAccessMask, const VkAccessFlags dstAccessMask,
      41             :     const VkPipelineStageFlags srcStageMask, const VkPipelineStageFlags dstStageMask)
      42             : {
      43           0 :     ENSURE(texture->GetMIPLevelCount() == 1);
      44           0 :     ENSURE(texture->GetLayerCount() == 1);
      45             : 
      46           0 :     VkImageMemoryBarrier imageMemoryBarrier{};
      47           0 :     imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
      48           0 :     imageMemoryBarrier.image = texture->GetImage();
      49           0 :     imageMemoryBarrier.srcAccessMask = srcAccessMask;
      50           0 :     imageMemoryBarrier.dstAccessMask = dstAccessMask;
      51           0 :     imageMemoryBarrier.oldLayout = oldLayout;
      52           0 :     imageMemoryBarrier.newLayout = newLayout;
      53           0 :     imageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
      54           0 :     imageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
      55           0 :     imageMemoryBarrier.subresourceRange.aspectMask = texture->GetAttachmentImageAspectMask();
      56           0 :     imageMemoryBarrier.subresourceRange.baseMipLevel = 0;
      57           0 :     imageMemoryBarrier.subresourceRange.levelCount = texture->GetMIPLevelCount();
      58           0 :     imageMemoryBarrier.subresourceRange.baseArrayLayer = 0;
      59           0 :     imageMemoryBarrier.subresourceRange.layerCount = texture->GetLayerCount();
      60             : 
      61           0 :     vkCmdPipelineBarrier(commandBuffer,
      62             :         srcStageMask, dstStageMask, 0,
      63             :         0, nullptr, 0, nullptr, 1, &imageMemoryBarrier);
      64             : 
      65           0 :     texture->SetInitialized();
      66           0 : }
      67             : 
      68           0 : void SubmitImageMemoryBarrier(
      69             :     VkCommandBuffer commandBuffer, VkImage image, const uint32_t level, const uint32_t layer,
      70             :     const VkAccessFlags srcAccessMask, const VkAccessFlags dstAccessMask,
      71             :     const VkImageLayout oldLayout, const VkImageLayout newLayout,
      72             :     const VkPipelineStageFlags srcStageMask, const VkPipelineStageFlags dstStageMask,
      73             :     const VkImageAspectFlags aspectMask)
      74             : {
      75           0 :     VkImageMemoryBarrier imageMemoryBarrier{};
      76           0 :     imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
      77           0 :     imageMemoryBarrier.image = image;
      78           0 :     imageMemoryBarrier.srcAccessMask = srcAccessMask;
      79           0 :     imageMemoryBarrier.dstAccessMask = dstAccessMask;
      80           0 :     imageMemoryBarrier.oldLayout = oldLayout;
      81           0 :     imageMemoryBarrier.newLayout = newLayout;
      82           0 :     imageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
      83           0 :     imageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
      84           0 :     imageMemoryBarrier.subresourceRange.aspectMask = aspectMask;
      85           0 :     imageMemoryBarrier.subresourceRange.baseMipLevel = level;
      86           0 :     imageMemoryBarrier.subresourceRange.levelCount = 1;
      87           0 :     imageMemoryBarrier.subresourceRange.baseArrayLayer = layer;
      88           0 :     imageMemoryBarrier.subresourceRange.layerCount = 1;
      89             : 
      90           0 :     vkCmdPipelineBarrier(commandBuffer,
      91             :         srcStageMask, dstStageMask, 0,
      92             :         0, nullptr, 0, nullptr, 1, &imageMemoryBarrier);
      93           0 : }
      94             : 
      95           0 : void SubmitBufferMemoryBarrier(
      96             :     VkCommandBuffer commandBuffer, CBuffer* buffer,
      97             :     const VkDeviceSize offset, const VkDeviceSize size,
      98             :     const VkAccessFlags srcAccessMask, const VkAccessFlags dstAccessMask,
      99             :     const VkPipelineStageFlags srcStageMask, const VkPipelineStageFlags dstStageMask)
     100             : {
     101           0 :     VkBufferMemoryBarrier bufferMemoryBarrier{};
     102           0 :     bufferMemoryBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
     103           0 :     bufferMemoryBarrier.srcAccessMask = srcAccessMask;
     104           0 :     bufferMemoryBarrier.dstAccessMask = dstAccessMask;
     105           0 :     bufferMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
     106           0 :     bufferMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
     107           0 :     bufferMemoryBarrier.buffer = buffer->GetVkBuffer();
     108           0 :     bufferMemoryBarrier.offset = offset;
     109           0 :     bufferMemoryBarrier.size = size;
     110             : 
     111           0 :     vkCmdPipelineBarrier(
     112             :         commandBuffer, srcStageMask, dstStageMask, 0,
     113             :         0, nullptr, 1, &bufferMemoryBarrier, 0, nullptr);
     114           0 : }
     115             : 
     116           0 : void SubmitMemoryBarrier(
     117             :     VkCommandBuffer commandBuffer,
     118             :     const VkAccessFlags srcAccessMask, const VkAccessFlags dstAccessMask,
     119             :     const VkPipelineStageFlags srcStageMask, const VkPipelineStageFlags dstStageMask)
     120             : {
     121           0 :     VkMemoryBarrier memoryBarrier{};
     122           0 :     memoryBarrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
     123           0 :     memoryBarrier.srcAccessMask = srcAccessMask;
     124           0 :     memoryBarrier.dstAccessMask = dstAccessMask;
     125           0 :     vkCmdPipelineBarrier(
     126             :         commandBuffer, srcStageMask, dstStageMask, 0,
     127             :         1, &memoryBarrier, 0, nullptr, 0, nullptr);
     128           0 : }
     129             : 
     130           0 : void SubmitPipelineBarrier(
     131             :     VkCommandBuffer commandBuffer,
     132             :     const VkPipelineStageFlags srcStageMask, const VkPipelineStageFlags dstStageMask)
     133             : {
     134           0 :     vkCmdPipelineBarrier(
     135             :         commandBuffer, srcStageMask, dstStageMask, 0,
     136             :         0, nullptr, 0, nullptr, 0, nullptr);
     137           0 : }
     138             : 
     139           0 : void SubmitDebugSyncMemoryBarrier(VkCommandBuffer commandBuffer)
     140             : {
     141           0 :     const VkAccessFlags accessMask =
     142             :         VK_ACCESS_INDIRECT_COMMAND_READ_BIT |
     143             :         VK_ACCESS_INDEX_READ_BIT |
     144             :         VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT |
     145             :         VK_ACCESS_UNIFORM_READ_BIT |
     146             :         VK_ACCESS_INPUT_ATTACHMENT_READ_BIT |
     147             :         VK_ACCESS_SHADER_READ_BIT |
     148             :         VK_ACCESS_SHADER_WRITE_BIT |
     149             :         VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
     150             :         VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
     151             :         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
     152             :         VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
     153             :         VK_ACCESS_TRANSFER_READ_BIT |
     154             :         VK_ACCESS_TRANSFER_WRITE_BIT |
     155             :         VK_ACCESS_HOST_READ_BIT |
     156             :         VK_ACCESS_HOST_WRITE_BIT;
     157           0 :     SubmitMemoryBarrier(
     158             :         commandBuffer, accessMask, accessMask,
     159             :         VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
     160           0 : }
     161             : 
     162           0 : const char* GetVkResultName(const VkResult result)
     163             : {
     164             : #define CASE(NAME) case NAME: return #NAME
     165           0 :     switch (result)
     166             :     {
     167             :     CASE(VK_SUCCESS);
     168           0 :     CASE(VK_NOT_READY);
     169           0 :     CASE(VK_TIMEOUT);
     170           0 :     CASE(VK_EVENT_SET);
     171           0 :     CASE(VK_EVENT_RESET);
     172           0 :     CASE(VK_INCOMPLETE);
     173           0 :     CASE(VK_ERROR_OUT_OF_HOST_MEMORY);
     174           0 :     CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY);
     175           0 :     CASE(VK_ERROR_INITIALIZATION_FAILED);
     176           0 :     CASE(VK_ERROR_DEVICE_LOST);
     177           0 :     CASE(VK_ERROR_MEMORY_MAP_FAILED);
     178           0 :     CASE(VK_ERROR_LAYER_NOT_PRESENT);
     179           0 :     CASE(VK_ERROR_EXTENSION_NOT_PRESENT);
     180           0 :     CASE(VK_ERROR_FEATURE_NOT_PRESENT);
     181           0 :     CASE(VK_ERROR_INCOMPATIBLE_DRIVER);
     182           0 :     CASE(VK_ERROR_TOO_MANY_OBJECTS);
     183           0 :     CASE(VK_ERROR_FORMAT_NOT_SUPPORTED);
     184           0 :     CASE(VK_ERROR_FRAGMENTED_POOL);
     185           0 :     CASE(VK_ERROR_UNKNOWN);
     186           0 :     CASE(VK_ERROR_OUT_OF_POOL_MEMORY);
     187           0 :     CASE(VK_ERROR_INVALID_EXTERNAL_HANDLE);
     188           0 :     CASE(VK_ERROR_FRAGMENTATION);
     189           0 :     CASE(VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS);
     190           0 :     CASE(VK_ERROR_SURFACE_LOST_KHR);
     191           0 :     CASE(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
     192           0 :     CASE(VK_SUBOPTIMAL_KHR);
     193           0 :     CASE(VK_ERROR_OUT_OF_DATE_KHR);
     194           0 :     default:
     195           0 :         break;
     196             :     }
     197             : #undef CASE
     198           0 :     return "UNLISTED";
     199             : }
     200             : 
     201             : } // namespace Utilities
     202             : 
     203             : } // namespace Vulkan
     204             : 
     205             : } // namespace Backend
     206             : 
     207             : } // namespace Renderer

Generated by: LCOV version 1.16