LCOV - code coverage report
Current view: top level - source/renderer - VertexArray.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 69 121 57.0 %
Date: 2026-12-03 01:19:39 Functions: 10 23 43.5 %

          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             : #include "precompiled.h"
      19             : 
      20             : #include "lib/alignment.h"
      21             : #include "lib/sysdep/rtl.h"
      22             : #include "ps/CLogger.h"
      23             : #include "renderer/Renderer.h"
      24             : #include "renderer/VertexArray.h"
      25             : #include "renderer/VertexBuffer.h"
      26             : #include "renderer/VertexBufferManager.h"
      27             : 
      28             : #include <bit>
      29             : 
      30             : class CVector3D;
      31             : class CVector4D;
      32             : struct SColor4ub;
      33             : 
      34             : namespace
      35             : {
      36             : 
      37          80 : uint32_t GetAttributeSize(const Renderer::Backend::Format format)
      38             : {
      39          80 :     switch (format)
      40             :     {
      41             :     case Renderer::Backend::Format::R8G8B8A8_UNORM:
      42             :     case Renderer::Backend::Format::R8G8B8A8_UINT:
      43             :         return sizeof(std::uint8_t) * 4;
      44             :     case Renderer::Backend::Format::A8_UNORM:
      45             :         return sizeof(std::uint8_t);
      46             :     case Renderer::Backend::Format::R16_UNORM:
      47             :     case Renderer::Backend::Format::R16_UINT:
      48             :     case Renderer::Backend::Format::R16_SINT:
      49             :     case Renderer::Backend::Format::R16_SFLOAT:
      50             :         return sizeof(std::uint16_t);
      51             :     case Renderer::Backend::Format::R16G16_UNORM:
      52             :     case Renderer::Backend::Format::R16G16_UINT:
      53             :     case Renderer::Backend::Format::R16G16_SINT:
      54             :     case Renderer::Backend::Format::R16G16_SFLOAT:
      55             :         return sizeof(std::uint16_t) * 2;
      56             :     case Renderer::Backend::Format::R16G16B16_SFLOAT:
      57             :         return sizeof(std::uint16_t) * 3;
      58             :     case Renderer::Backend::Format::R16G16B16A16_SFLOAT:
      59             :         return sizeof(std::uint16_t) * 4;
      60             :     case Renderer::Backend::Format::R32_SFLOAT:
      61             :         return sizeof(float);
      62             :     case Renderer::Backend::Format::R32G32_SFLOAT:
      63             :         return sizeof(float) * 2;
      64             :     case Renderer::Backend::Format::R32G32B32_SFLOAT:
      65             :         return sizeof(float) * 3;
      66             :     case Renderer::Backend::Format::R32G32B32A32_SFLOAT:
      67             :         return sizeof(float) * 4;
      68             :     default:
      69             :         break;
      70             :     };
      71             :     return 0;
      72             : }
      73             : 
      74             : } // anonymous namespace
      75             : 
      76          30 : VertexArray::VertexArray(
      77          30 :     const Renderer::Backend::IBuffer::Type type, const uint32_t usage)
      78          30 :     : m_Type(type), m_Usage(usage)
      79             : {
      80          20 : }
      81             : 
      82          30 : VertexArray::~VertexArray()
      83             : {
      84          60 :     Free();
      85          30 : }
      86             : 
      87             : // Free all resources on destruction or when a layout parameter changes
      88         110 : void VertexArray::Free()
      89             : {
      90          30 :     rtl_FreeAligned(m_BackingStore);
      91         110 :     m_BackingStore = 0;
      92             : 
      93         110 :     m_VB.Reset();
      94           0 : }
      95             : 
      96             : // Set the number of vertices stored in the array
      97          20 : void VertexArray::SetNumberOfVertices(const size_t numberOfVertices)
      98             : {
      99          20 :     if (numberOfVertices == m_NumberOfVertices)
     100             :         return;
     101             : 
     102          20 :     Free();
     103          20 :     m_NumberOfVertices = numberOfVertices;
     104             : }
     105             : 
     106           0 : void VertexArray::SetMinimumAttributeAlignment(const uint32_t minimumAttributeAlignment)
     107             : {
     108           0 :     ENSURE(minimumAttributeAlignment >= 4 || std::has_single_bit(minimumAttributeAlignment));
     109           0 :     if (minimumAttributeAlignment == m_MinimumAttributeAlignment)
     110             :         return;
     111             : 
     112           0 :     Free();
     113           0 :     m_MinimumAttributeAlignment = minimumAttributeAlignment;
     114             : }
     115             : 
     116             : // Add vertex attributes like Position, Normal, UV
     117          40 : void VertexArray::AddAttribute(Attribute* attr)
     118             : {
     119             :     // Attribute is supported is its size is greater than zero.
     120          40 :     ENSURE(GetAttributeSize(attr->format) > 0 && "Unsupported attribute.");
     121             : 
     122          40 :     attr->vertexArray = this;
     123          40 :     m_Attributes.push_back(attr);
     124             : 
     125          40 :     Free();
     126          40 : }
     127             : 
     128             : // Template specialization for GetIterator().
     129             : // We can put this into the source file because only a fixed set of types
     130             : // is supported for type safety.
     131             : template<>
     132           0 : VertexArrayIterator<CVector3D> VertexArray::Attribute::GetIterator<CVector3D>() const
     133             : {
     134           0 :     ENSURE(vertexArray);
     135           0 :     ENSURE(
     136             :         format == Renderer::Backend::Format::R32G32B32_SFLOAT ||
     137             :         format == Renderer::Backend::Format::R32G32B32A32_SFLOAT);
     138             : 
     139           0 :     return vertexArray->MakeIterator<CVector3D>(this);
     140             : }
     141             : 
     142             : template<>
     143           0 : VertexArrayIterator<CVector4D> VertexArray::Attribute::GetIterator<CVector4D>() const
     144             : {
     145           0 :     ENSURE(vertexArray);
     146           0 :     ENSURE(format == Renderer::Backend::Format::R32G32B32A32_SFLOAT);
     147             : 
     148           0 :     return vertexArray->MakeIterator<CVector4D>(this);
     149             : }
     150             : 
     151             : template<>
     152           0 : VertexArrayIterator<float[2]> VertexArray::Attribute::GetIterator<float[2]>() const
     153             : {
     154           0 :     ENSURE(vertexArray);
     155           0 :     ENSURE(format == Renderer::Backend::Format::R32G32_SFLOAT);
     156             : 
     157           0 :     return vertexArray->MakeIterator<float[2]>(this);
     158             : }
     159             : 
     160             : template<>
     161           0 : VertexArrayIterator<SColor4ub> VertexArray::Attribute::GetIterator<SColor4ub>() const
     162             : {
     163           0 :     ENSURE(vertexArray);
     164           0 :     ENSURE(
     165             :         format == Renderer::Backend::Format::R8G8B8A8_UNORM ||
     166             :         format == Renderer::Backend::Format::R8G8B8A8_UINT);
     167             : 
     168           0 :     return vertexArray->MakeIterator<SColor4ub>(this);
     169             : }
     170             : 
     171             : template<>
     172          10 : VertexArrayIterator<std::uint16_t> VertexArray::Attribute::GetIterator<std::uint16_t>() const
     173             : {
     174          10 :     ENSURE(vertexArray);
     175          10 :     ENSURE(format == Renderer::Backend::Format::R16_UINT);
     176             : 
     177          10 :     return vertexArray->MakeIterator<std::uint16_t>(this);
     178             : }
     179             : 
     180             : template<>
     181           0 : VertexArrayIterator<std::uint16_t[2]> VertexArray::Attribute::GetIterator<std::uint16_t[2]>() const
     182             : {
     183           0 :     ENSURE(vertexArray);
     184           0 :     ENSURE(format == Renderer::Backend::Format::R16G16_UINT);
     185             : 
     186           0 :     return vertexArray->MakeIterator<std::uint16_t[2]>(this);
     187             : }
     188             : 
     189             : template<>
     190           0 : VertexArrayIterator<std::uint8_t> VertexArray::Attribute::GetIterator<std::uint8_t>() const
     191             : {
     192           0 :     ENSURE(vertexArray);
     193           0 :     ENSURE(format == Renderer::Backend::Format::A8_UNORM);
     194             : 
     195           0 :     return vertexArray->MakeIterator<std::uint8_t>(this);
     196             : }
     197             : 
     198             : template<>
     199           0 : VertexArrayIterator<std::uint8_t[4]> VertexArray::Attribute::GetIterator<std::uint8_t[4]>() const
     200             : {
     201           0 :     ENSURE(vertexArray);
     202           0 :     ENSURE(
     203             :         format == Renderer::Backend::Format::R8G8B8A8_UNORM ||
     204             :         format == Renderer::Backend::Format::R8G8B8A8_UINT);
     205             : 
     206           0 :     return vertexArray->MakeIterator<std::uint8_t[4]>(this);
     207             : }
     208             : 
     209             : template<>
     210           0 : VertexArrayIterator<short> VertexArray::Attribute::GetIterator<short>() const
     211             : {
     212           0 :     ENSURE(vertexArray);
     213           0 :     ENSURE(format == Renderer::Backend::Format::R16_SINT);
     214             : 
     215           0 :     return vertexArray->MakeIterator<short>(this);
     216             : }
     217             : 
     218             : template<>
     219           0 : VertexArrayIterator<short[2]> VertexArray::Attribute::GetIterator<short[2]>() const
     220             : {
     221           0 :     ENSURE(vertexArray);
     222           0 :     ENSURE(format == Renderer::Backend::Format::R16G16_SINT);
     223             : 
     224           0 :     return vertexArray->MakeIterator<short[2]>(this);
     225             : }
     226             : 
     227          10 : static uint32_t RoundStride(uint32_t stride)
     228             : {
     229          10 :     if (stride <= 0)
     230             :         return 0;
     231          10 :     if (stride <= 4)
     232             :         return 4;
     233          10 :     if (stride <= 8)
     234             :         return 8;
     235             : 
     236          10 :     return Align<16>(stride);
     237             : }
     238             : 
     239             : // Re-layout by assigning offsets on a first-come first-serve basis,
     240             : // then round up to a reasonable stride.
     241             : // Backing store is also created here, backend buffers are created on upload.
     242          20 : void VertexArray::Layout()
     243             : {
     244          20 :     Free();
     245             : 
     246          20 :     m_Stride = 0;
     247             : 
     248          60 :     for (ssize_t idx = m_Attributes.size()-1; idx >= 0; --idx)
     249             :     {
     250          40 :         Attribute* attr = m_Attributes[idx];
     251          40 :         if (attr->format == Renderer::Backend::Format::UNDEFINED)
     252           0 :             continue;
     253             : 
     254          40 :         const uint32_t attrSize = GetAttributeSize(attr->format);
     255          40 :         ENSURE(attrSize > 0);
     256             : 
     257          40 :         attr->offset = m_Stride;
     258             : 
     259          40 :         m_Stride += attrSize;
     260             : 
     261          40 :         if (m_Type == Renderer::Backend::IBuffer::Type::VERTEX)
     262          30 :             m_Stride = Align<4>(m_Stride);
     263          40 :         if (m_MinimumAttributeAlignment > 0)
     264           0 :             m_Stride = (m_Stride + m_MinimumAttributeAlignment - 1) & ~(m_MinimumAttributeAlignment - 1);
     265             :     }
     266             : 
     267          20 :     if (m_Type == Renderer::Backend::IBuffer::Type::VERTEX)
     268          20 :         m_Stride = RoundStride(m_Stride);
     269             : 
     270          20 :     if (m_Stride)
     271          20 :         m_BackingStore = (char*)rtl_AllocateAligned(m_Stride * m_NumberOfVertices, 16);
     272          20 : }
     273             : 
     274           0 : void VertexArray::PrepareForRendering()
     275             : {
     276           0 :     m_VB->m_Owner->PrepareForRendering(m_VB.Get());
     277           0 : }
     278             : 
     279             : // (Re-)Upload the attributes.
     280             : // Create the backend buffer if necessary.
     281          10 : void VertexArray::Upload()
     282             : {
     283          10 :     ENSURE(m_BackingStore);
     284             : 
     285          10 :     if (!m_VB)
     286             :     {
     287          20 :         m_VB = g_Renderer.GetVertexBufferManager().AllocateChunk(
     288          10 :             m_Stride, m_NumberOfVertices, m_Type, m_Usage, m_BackingStore);
     289             :     }
     290             : 
     291          10 :     if (!m_VB)
     292             :     {
     293           0 :         LOGERROR("Failed to allocate backend buffer for vertex array");
     294           0 :         return;
     295             :     }
     296             : 
     297          10 :     m_VB->m_Owner->UpdateChunkVertices(m_VB.Get(), m_BackingStore);
     298             : }
     299             : 
     300           0 : void VertexArray::UploadIfNeeded(
     301             :     Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
     302             : {
     303           0 :     m_VB->m_Owner->UploadIfNeeded(deviceCommandContext);
     304           0 : }
     305             : 
     306             : // Free the backing store to save some memory
     307          10 : void VertexArray::FreeBackingStore()
     308             : {
     309             :     // In streaming modes, the backing store must be retained
     310          10 :     ENSURE(!CVertexBuffer::UseStreaming(m_Usage));
     311             : 
     312          10 :     rtl_FreeAligned(m_BackingStore);
     313          10 :     m_BackingStore = 0;
     314          10 : }
     315             : 
     316          10 : VertexIndexArray::VertexIndexArray(const uint32_t usage) :
     317          10 :     VertexArray(Renderer::Backend::IBuffer::Type::INDEX, usage)
     318             : {
     319          10 :     m_Attr.format = Renderer::Backend::Format::R16_UINT;
     320          10 :     AddAttribute(&m_Attr);
     321          10 : }
     322             : 
     323          10 : VertexArrayIterator<std::uint16_t> VertexIndexArray::GetIterator() const
     324             : {
     325          10 :     return m_Attr.GetIterator<std::uint16_t>();
     326             : }

Generated by: LCOV version 1.16