LCOV - code coverage report
Current view: top level - source/renderer - VertexBufferManager.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 9 12 75.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             : /*
      19             :  * Allocate and destroy CVertexBuffers
      20             :  */
      21             : 
      22             : #ifndef INCLUDED_VERTEXBUFFERMANAGER
      23             : #define INCLUDED_VERTEXBUFFERMANAGER
      24             : 
      25             : #include "renderer/VertexBuffer.h"
      26             : #include "renderer/backend/IBuffer.h"
      27             : 
      28             : #include <cstddef>
      29             : #include <cstdint>
      30             : #include <memory>
      31             : #include <utility>
      32             : #include <vector>
      33             : 
      34             : namespace Renderer::Backend { class IDevice; }
      35             : 
      36             : // CVertexBufferManager: owner object for CVertexBuffer objects; acts as
      37             : // 'front end' for their allocation and destruction
      38           0 : class CVertexBufferManager
      39             : {
      40             : public:
      41          10 :     CVertexBufferManager(Renderer::Backend::IDevice* device) : m_Device(device) {}
      42             : 
      43             :     enum class Group : std::uint32_t
      44             :     {
      45             :         DEFAULT,
      46             :         TERRAIN,
      47             :         WATER,
      48             :         COUNT
      49             :     };
      50             : 
      51             :     // Helper for automatic VBChunk lifetime management.
      52             :     class Handle
      53             :     {
      54             :     public:
      55          20 :         Handle() = default;
      56             :         Handle(const Handle&) = delete;
      57             :         Handle& operator=(const Handle&) = delete;
      58             : 
      59             :         explicit Handle(CVertexBuffer::VBChunk* chunk);
      60             :         Handle(Handle&& other);
      61             :         Handle& operator=(Handle&& other);
      62             : 
      63          60 :         ~Handle() { Reset(); }
      64             : 
      65         210 :         bool IsValid() const { return m_Chunk != nullptr; }
      66          20 :         explicit operator bool() const { return IsValid(); }
      67          20 :         bool operator!() const { return !static_cast<bool>(*this); }
      68             :         void Reset();
      69             : 
      70          10 :         friend void swap(Handle& lhs, Handle& rhs)
      71             :         {
      72          10 :             using std::swap;
      73             : 
      74          10 :             swap(lhs.m_Chunk, rhs.m_Chunk);
      75             :         }
      76             : 
      77             :         CVertexBuffer::VBChunk& operator*() const { return *m_Chunk; }
      78           0 :         CVertexBuffer::VBChunk* operator->() const { return m_Chunk; }
      79           0 :         CVertexBuffer::VBChunk* Get() const { return m_Chunk; }
      80             : 
      81             :     private:
      82             :         CVertexBuffer::VBChunk* m_Chunk = nullptr;
      83             :     };
      84             : 
      85             :     /**
      86             :      * Try to allocate a vertex buffer of the given size and type.
      87             :      *
      88             :      * @param vertexSize size of each vertex in the buffer
      89             :      * @param numberOfVertices number of vertices in the buffer
      90             :      * @param type buffer type
      91             :      * @param dynamic will be buffer updated frequently or not
      92             :      * @param backingStore if not dynamic, this is nullptr; else for dynamic,
      93             :      *            this must be a copy of the vertex data that remains valid for the
      94             :      *            lifetime of the VBChunk
      95             :      * @return chunk, or empty handle if no free chunks available
      96             :      */
      97             :     Handle AllocateChunk(
      98             :         const size_t vertexSize, const size_t numberOfVertices,
      99             :         const Renderer::Backend::IBuffer::Type type,
     100             :         const uint32_t usage, void* backingStore = nullptr, Group group = Group::DEFAULT);
     101             : 
     102             :     /// Returns the given @p chunk to its owning buffer
     103             :     void Release(CVertexBuffer::VBChunk* chunk);
     104             : 
     105             :     size_t GetBytesReserved() const;
     106             :     size_t GetBytesAllocated() const;
     107             : 
     108             : private:
     109             :     Renderer::Backend::IDevice* m_Device{nullptr};
     110             : 
     111             :     /// List of all known vertex buffers
     112             :     std::vector<std::unique_ptr<CVertexBuffer>> m_Buffers[static_cast<std::size_t>(Group::COUNT)];
     113             : };
     114             : 
     115             : #endif // INCLUDED_VERTEXBUFFERMANAGER

Generated by: LCOV version 1.16