LCOV - code coverage report
Current view: top level - source/renderer - VertexArray.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 13 25 52.0 %
Date: 2026-12-03 01:19:39 Functions: 1 10 10.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_VERTEXARRAY
      19             : #define INCLUDED_VERTEXARRAY
      20             : 
      21             : #include "lib/debug.h"
      22             : #include "lib/posix/posix_types.h"
      23             : #include "renderer/VertexBuffer.h"
      24             : #include "renderer/VertexBufferManager.h"
      25             : #include "renderer/backend/Format.h"
      26             : #include "renderer/backend/IBuffer.h"
      27             : 
      28             : #include <cstddef>
      29             : #include <cstdint>
      30             : #include <vector>
      31             : 
      32             : namespace Renderer::Backend { class IDeviceCommandContext; }
      33             : 
      34             : // Iterator
      35             : template<typename T>
      36             : class VertexArrayIterator
      37             : {
      38             : public:
      39             :     typedef T Type;
      40             : 
      41             : public:
      42             :     VertexArrayIterator() :
      43             :         m_Data(0), m_Stride(0)
      44             :     {
      45             :     }
      46             : 
      47          10 :     VertexArrayIterator(char* data, size_t stride) :
      48          10 :         m_Data(data), m_Stride(stride)
      49             :     {
      50             :     }
      51             : 
      52       10240 :     VertexArrayIterator(const VertexArrayIterator& rhs) :
      53       10240 :         m_Data(rhs.m_Data), m_Stride(rhs.m_Stride)
      54             :     {
      55             :     }
      56             : 
      57             :     VertexArrayIterator& operator=(const VertexArrayIterator& rhs)
      58             :     {
      59             :         m_Data = rhs.m_Data;
      60             :         m_Stride = rhs.m_Stride;
      61             :         return *this;
      62             :     }
      63             : 
      64             :     // Accessors
      65           0 :     T& operator*() const { return *(T*)m_Data; }
      66           0 :     T* operator->() const { return (T*)m_Data; }
      67           0 :     T& operator[](size_t idx) const { return *(T*)(m_Data + idx*m_Stride); }
      68             : 
      69             :     // Walking
      70           0 :     VertexArrayIterator& operator++()
      71             :     {
      72           0 :         m_Data += m_Stride;
      73             :         return *this;
      74             :     }
      75       10240 :     VertexArrayIterator operator++(int)
      76             :     {
      77       10240 :         VertexArrayIterator tmp = *this;
      78       10240 :         m_Data += m_Stride;
      79             :         return tmp;
      80             :     }
      81             :     VertexArrayIterator& operator--()
      82             :     {
      83             :         m_Data -= m_Stride;
      84             :         return *this;
      85             :     }
      86             :     VertexArrayIterator operator--(int)
      87             :     {
      88             :         VertexArrayIterator tmp = *this;
      89             :         m_Data -= m_Stride;
      90             :         return tmp;
      91             :     }
      92             : 
      93             :     VertexArrayIterator& operator+=(ssize_t rhs)
      94             :     {
      95             :         m_Data += rhs*m_Stride;
      96             :         return *this;
      97             :     }
      98             :     VertexArrayIterator& operator-=(ssize_t rhs)
      99             :     {
     100             :         m_Data -= rhs*m_Stride;
     101             :         return *this;
     102             :     }
     103             : 
     104             :     VertexArrayIterator operator+(ssize_t rhs) const
     105             :     {
     106             :         VertexArrayIterator tmp = *this;
     107             :         tmp.m_Data += rhs*m_Stride;
     108             :         return tmp;
     109             :     }
     110             :     VertexArrayIterator operator-(ssize_t rhs) const
     111             :     {
     112             :         VertexArrayIterator tmp = *this;
     113             :         tmp.m_Data -= rhs*m_Stride;
     114             :         return tmp;
     115             :     }
     116             : 
     117             :     // Accessors for raw buffer data, for performance-critical code
     118           0 :     char* GetData() const
     119             :     {
     120           0 :         return m_Data;
     121             :     }
     122           0 :     size_t GetStride() const
     123             :     {
     124           0 :         return m_Stride;
     125             :     }
     126             : 
     127             : private:
     128             :     char* m_Data;
     129             :     size_t m_Stride;
     130             : };
     131             : 
     132             : 
     133             : // Manage a vertex array with a runtime-determined set of attributes.
     134             : //
     135             : // Purpose: Different rendering paths sometimes require different sets of
     136             : // attributes (e.g. normal vector vs. color data), which is difficult to
     137             : // support with hardcoded vertex structures.
     138             : // This class chooses the vertex layout at runtime, based on the attributes
     139             : // that are actually needed.
     140             : //
     141             : // Note that this class will not allocate any backend resources until one
     142             : // of the Upload functions is called.
     143             : class VertexArray
     144             : {
     145             : public:
     146             :     struct Attribute
     147             :     {
     148             :         Renderer::Backend::Format format = Renderer::Backend::Format::UNDEFINED;
     149             : 
     150             :         // Offset (in bytes) into a vertex structure (filled in by Layout())
     151             :         uint32_t offset = 0;
     152             : 
     153             :         VertexArray* vertexArray = nullptr;
     154             : 
     155          30 :         Attribute() {}
     156             : 
     157             :         // Get an iterator over the backing store for the given attribute that
     158             :         // initially points at the first vertex.
     159             :         // Supported types T: CVector3D, CVector4D, float[2], SColor4ub,
     160             :         // std::uint16_t, std::uint16_t[2], std::uint8_t[4], short, short[2].
     161             :         // This function verifies at runtime that the requested type T matches
     162             :         // the attribute definition passed to AddAttribute().
     163             :         template<typename T>
     164             :         VertexArrayIterator<T> GetIterator() const;
     165             :     };
     166             : 
     167             : public:
     168             :     VertexArray(
     169             :         const Renderer::Backend::IBuffer::Type type, const uint32_t usage);
     170             :     ~VertexArray();
     171             : 
     172             :     // Set the number of vertices stored in the array
     173             :     void SetNumberOfVertices(const size_t numberOfVertices);
     174             :     // Add vertex attributes
     175             :     void AddAttribute(Attribute* attr);
     176             : 
     177             :     // Sets the minimum alignment for each attribute to have offsets multiples
     178             :     // of `minimumAttributeAlignment`.
     179             :     void SetMinimumAttributeAlignment(const uint32_t minimumAttributeAlignment);
     180             : 
     181           0 :     size_t GetNumberOfVertices() const { return m_NumberOfVertices; }
     182          10 :     uint32_t GetStride() const { return m_Stride; }
     183             : 
     184             :     // Layout the vertex array format and create backing buffer in RAM.
     185             :     // You must call Layout() after changing the number of vertices or
     186             :     // attributes.
     187             :     // All vertex data is lost when a vertex array is re-layouted.
     188             :     void Layout();
     189             :     // (Re-)Upload the attributes of the vertex array from the backing store to
     190             :     // the underlying buffer.
     191             :     void Upload();
     192             :     // Make this vertex array's data available for the next series of calls to Bind
     193             :     void PrepareForRendering();
     194             : 
     195             :     void UploadIfNeeded(Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
     196             : 
     197             :     // If you know for certain that you'll never have to change the data again,
     198             :     // call this to free some memory.
     199             :     void FreeBackingStore();
     200             : 
     201           0 :     Renderer::Backend::IBuffer* GetBuffer() { return m_VB ? m_VB->m_Owner->GetBuffer() : nullptr; }
     202             : 
     203           0 :     uint32_t GetOffset() const { return m_VB ? m_VB->m_Index : 0; }
     204             : 
     205             : private:
     206             :     void Free();
     207             : 
     208             :     template<typename T>
     209          10 :     VertexArrayIterator<T> MakeIterator(const Attribute* attr)
     210             :     {
     211          10 :         ENSURE(attr->format != Renderer::Backend::Format::UNDEFINED);
     212          10 :         return VertexArrayIterator<T>(m_BackingStore + attr->offset, m_Stride);
     213             :     }
     214             : 
     215             :     Renderer::Backend::IBuffer::Type m_Type;
     216             :     uint32_t m_Usage{0};
     217             :     uint32_t m_MinimumAttributeAlignment{0};
     218             :     size_t m_NumberOfVertices{0};
     219             :     std::vector<Attribute*> m_Attributes;
     220             : 
     221             :     CVertexBufferManager::Handle m_VB;
     222             :     uint32_t m_Stride{0};
     223             :     char* m_BackingStore{nullptr}; // 16-byte aligned, to allow fast SSE access
     224             : };
     225             : 
     226             : /**
     227             :  * A VertexArray that is specialised to handle 16-bit array indices.
     228             :  * Call UploadIfNeeded() before use in Draw/DrawIndexed.
     229             :  */
     230          10 : class VertexIndexArray : public VertexArray
     231             : {
     232             : public:
     233             :     VertexIndexArray(const uint32_t usage);
     234             : 
     235             :     /// Gets the iterator over the (only) attribute in this array, i.e. a std::uint16_t.
     236             :     VertexArrayIterator<std::uint16_t> GetIterator() const;
     237             : 
     238             : private:
     239             :     Attribute m_Attr;
     240             : };
     241             : 
     242             : #endif // INCLUDED_VERTEXARRAY

Generated by: LCOV version 1.16