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

          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             : #ifndef INCLUDED_RENDERER_BACKEND_VULKAN_RINGCOMMANDCONTEXT
      19             : #define INCLUDED_RENDERER_BACKEND_VULKAN_RINGCOMMANDCONTEXT
      20             : 
      21             : #include "renderer/backend/vulkan/SubmitScheduler.h"
      22             : 
      23             : #include <cstddef>
      24             : #include <cstdint>
      25             : #include <functional>
      26             : #include <glad/vulkan.h>
      27             : #include <memory>
      28             : #include <vector>
      29             : 
      30             : namespace Renderer::Backend { enum class Format; }
      31             : namespace Renderer::Backend::Vulkan { class CBuffer; }
      32             : namespace Renderer::Backend::Vulkan { class CDevice; }
      33             : namespace Renderer::Backend::Vulkan { class CTexture; }
      34             : 
      35             : namespace Renderer
      36             : {
      37             : 
      38             : namespace Backend
      39             : {
      40             : 
      41             : namespace Vulkan
      42             : {
      43             : 
      44             : /**
      45             :  * A simple helper class to decouple command buffers rotation from frames
      46             :  * presenting. It might be useful when sometimes we need to submit more work
      47             :  * than we can usually have during a frame. For example if we need to upload
      48             :  * something, an upload buffer is full and we can't extend it at the moment.
      49             :  * Then the only way is to wait until uploading is done and submit more work.
      50             :  * @note not thread-safe, should be created and used from the same thread.
      51             :  */
      52             : class CRingCommandContext
      53             : {
      54             : public:
      55             :     static std::unique_ptr<CRingCommandContext> Create(
      56             :         CDevice* device, const size_t size, const uint32_t queueFamilyIndex,
      57             :         CSubmitScheduler& submitScheduler);
      58             : 
      59             :     ~CRingCommandContext();
      60             : 
      61             :     /**
      62             :      * @return the current available command buffer. If there is none waits until
      63             :      * it appeared.
      64             :      */
      65             :     VkCommandBuffer GetCommandBuffer();
      66             : 
      67             :     /**
      68             :      * Submits the current command buffer to the SubmitScheduler.
      69             :      */
      70             :     void Flush();
      71             : 
      72             :     /**
      73             :      * The same as Flush but also waits until it's completed. It means it
      74             :      * forces SubmitScheduler to submit all previously queued work to GPU.
      75             :      */
      76             :     void FlushAndWait();
      77             : 
      78             :     /**
      79             :      * Schedules uploads until next render pass or flush.
      80             :      * @note doesn't save a command buffer returned by GetCommandBuffer during
      81             :      * scheduling uploads, because it might be changed.
      82             :      */
      83             :     void ScheduleUpload(
      84             :         CTexture* texture, const Format dataFormat,
      85             :         const void* data, const size_t dataSize,
      86             :         const uint32_t level, const uint32_t layer);
      87             :     void ScheduleUpload(
      88             :         CTexture* texture, const Format dataFormat,
      89             :         const void* data, const size_t dataSize,
      90             :         const uint32_t xOffset, const uint32_t yOffset,
      91             :         const uint32_t width, const uint32_t height,
      92             :         const uint32_t level, const uint32_t layer);
      93             : 
      94             :     void ScheduleUpload(
      95             :         CBuffer* buffer, const void* data, const uint32_t dataOffset,
      96             :         const uint32_t dataSize);
      97             :     using UploadBufferFunction = std::function<void(std::uint8_t*)>;
      98             :     void ScheduleUpload(
      99             :         CBuffer* buffer,
     100             :         const uint32_t dataOffset, const uint32_t dataSize,
     101             :         const UploadBufferFunction& uploadFunction);
     102             : 
     103             : private:
     104             :     CRingCommandContext(
     105             :         CDevice* device, CSubmitScheduler& submitScheduler);
     106             : 
     107             :     void Begin();
     108             :     void End();
     109             : 
     110             :     void ScheduleUpload(
     111             :         CBuffer* buffer, const uint32_t dataOffset, const uint32_t dataSize,
     112             :         const uint32_t acquiredOffset);
     113             : 
     114             :     uint32_t AcquireFreeSpace(
     115             :         const uint32_t requiredSize, const uint32_t requiredAlignment);
     116             :     uint32_t GetFreeSpaceOffset(
     117             :         const uint32_t requiredSize, const uint32_t requiredAlignment) const;
     118             : 
     119             :     CDevice* m_Device = nullptr;
     120             :     CSubmitScheduler& m_SubmitScheduler;
     121             : 
     122             :     std::unique_ptr<CBuffer> m_StagingBuffer;
     123             :     uint32_t m_StagingBufferFirst = 0, m_StagingBufferCurrentFirst = 0, m_StagingBufferLast = 0;
     124             :     uint32_t m_OptimalBufferCopyOffsetAlignment = 1;
     125             :     uint32_t m_MaxStagingBufferCapacity = 0;
     126             : 
     127           0 :     struct RingItem
     128             :     {
     129             :         VkCommandPool commandPool = VK_NULL_HANDLE;
     130             :         VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
     131             :         CSubmitScheduler::SubmitHandle handle = CSubmitScheduler::INVALID_SUBMIT_HANDLE;
     132             :         bool isBegan = false;
     133             :         uint32_t stagingBufferFirst = 0, stagingBufferLast = 0;
     134             :     };
     135             :     std::vector<RingItem> m_Ring;
     136             :     size_t m_RingIndex = 0;
     137             : 
     138             :     void WaitUntilFree(RingItem& item);
     139             : };
     140             : 
     141             : } // namespace Vulkan
     142             : 
     143             : } // namespace Backend
     144             : 
     145             : } // namespace Renderer
     146             : 
     147             : #endif // INCLUDED_RENDERER_BACKEND_VULKAN_RINGCOMMANDCONTEXT

Generated by: LCOV version 1.16