LCOV - code coverage report
Current view: top level - source/renderer - InstancingModelRenderer.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 7 104 6.7 %
Date: 2026-12-03 01:19:39 Functions: 2 9 22.2 %

          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 "InstancingModelRenderer.h"
      21             : 
      22             : #include "graphics/MeshManager.h"
      23             : #include "graphics/Model.h"
      24             : #include "graphics/ModelDef.h"
      25             : #include "lib/debug.h"
      26             : #include "maths/Vector3D.h"
      27             : #include "maths/Vector4D.h"
      28             : #include "ps/containers/StaticVector.h"
      29             : #include "renderer/ModelRenderer.h"
      30             : #include "renderer/Renderer.h"
      31             : #include "renderer/VertexArray.h"
      32             : #include "renderer/backend/Format.h"
      33             : #include "renderer/backend/IBuffer.h"
      34             : #include "renderer/backend/IDeviceCommandContext.h"
      35             : #include "renderer/backend/IShaderProgram.h"
      36             : #include "third_party/mikktspace/weldmesh.h"
      37             : 
      38             : #include <algorithm>
      39             : #include <cstddef>
      40             : #include <cstdint>
      41             : #include <vector>
      42             : 
      43             : struct IModelDef : public CModelDefRPrivate
      44             : {
      45             :     /// Static per-CModel vertex array
      46             :     VertexArray m_Array;
      47             : 
      48             :     /// Position and normals are static
      49             :     VertexArray::Attribute m_Position;
      50             :     VertexArray::Attribute m_Normal;
      51             :     VertexArray::Attribute m_Tangent;
      52             : 
      53             :     /// The number of UVs is determined by the model
      54             :     std::vector<VertexArray::Attribute> m_UVs;
      55             : 
      56             :     Renderer::Backend::IVertexInputLayout* m_VertexInputLayout = nullptr;
      57             : 
      58             :     /// Indices are the same for all models, so share them
      59             :     VertexIndexArray m_IndexArray;
      60             : 
      61             :     IModelDef(const CModelDefPtr& mdef);
      62             : };
      63             : 
      64             : 
      65           0 : IModelDef::IModelDef(const CModelDefPtr& mdef)
      66           0 :     : m_IndexArray(Renderer::Backend::IBuffer::Usage::TRANSFER_DST),
      67           0 :     m_Array(Renderer::Backend::IBuffer::Type::VERTEX, Renderer::Backend::IBuffer::Usage::TRANSFER_DST)
      68             : {
      69           0 :     size_t numVertices = mdef->GetNumVertices();
      70             : 
      71           0 :     m_Position.format = Renderer::Backend::Format::R32G32B32_SFLOAT;
      72           0 :     m_Array.AddAttribute(&m_Position);
      73             : 
      74           0 :     m_Normal.format = Renderer::Backend::Format::R32G32B32_SFLOAT;
      75           0 :     m_Array.AddAttribute(&m_Normal);
      76             : 
      77           0 :     m_UVs.resize(mdef->GetNumUVsPerVertex());
      78           0 :     for (size_t i = 0; i < mdef->GetNumUVsPerVertex(); i++)
      79             :     {
      80           0 :         m_UVs[i].format = Renderer::Backend::Format::R32G32_SFLOAT;
      81           0 :         m_Array.AddAttribute(&m_UVs[i]);
      82             :     }
      83             : 
      84             :     // Generate tangents for the geometry:-
      85             : 
      86           0 :     m_Tangent.format = Renderer::Backend::Format::R32G32B32A32_SFLOAT;
      87           0 :     m_Array.AddAttribute(&m_Tangent);
      88             : 
      89             :     // floats per vertex; position + normal + tangent + UV*sets
      90           0 :     int numVertexAttrs = 3 + 3 + 4 + 2 * mdef->GetNumUVsPerVertex();
      91             : 
      92             :     // the tangent generation can increase the number of vertices temporarily
      93             :     // so reserve a bit more memory to avoid reallocations in GenTangents (in most cases)
      94           0 :     std::vector<float> newVertices;
      95           0 :     newVertices.reserve(numVertexAttrs * numVertices * 2);
      96             : 
      97             :     // Generate the tangents
      98           0 :     ModelRenderer::GenTangents(mdef, newVertices, false);
      99             : 
     100             :     // how many vertices do we have after generating tangents?
     101           0 :     int newNumVert = newVertices.size() / numVertexAttrs;
     102             : 
     103           0 :     std::vector<int> remapTable(newNumVert);
     104           0 :     std::vector<float> vertexDataOut(newNumVert * numVertexAttrs);
     105             : 
     106             :     // re-weld the mesh to remove duplicated vertices
     107           0 :     int numVertices2 = WeldMesh(&remapTable[0], &vertexDataOut[0],
     108           0 :                 &newVertices[0], newNumVert, numVertexAttrs);
     109             : 
     110             :     // Copy the model data to graphics memory:-
     111             : 
     112           0 :     m_Array.SetNumberOfVertices(numVertices2);
     113           0 :     m_Array.Layout();
     114             : 
     115           0 :     VertexArrayIterator<CVector3D> Position = m_Position.GetIterator<CVector3D>();
     116           0 :     VertexArrayIterator<CVector3D> Normal = m_Normal.GetIterator<CVector3D>();
     117           0 :     VertexArrayIterator<CVector4D> Tangent = m_Tangent.GetIterator<CVector4D>();
     118             : 
     119             :     // copy everything into the vertex array
     120           0 :     for (int i = 0; i < numVertices2; i++)
     121             :     {
     122           0 :         int q = numVertexAttrs * i;
     123             : 
     124           0 :         Position[i] = CVector3D(vertexDataOut[q + 0], vertexDataOut[q + 1], vertexDataOut[q + 2]);
     125           0 :         q += 3;
     126             : 
     127           0 :         Normal[i] = CVector3D(vertexDataOut[q + 0], vertexDataOut[q + 1], vertexDataOut[q + 2]);
     128           0 :         q += 3;
     129             : 
     130           0 :         Tangent[i] = CVector4D(vertexDataOut[q + 0], vertexDataOut[q + 1], vertexDataOut[q + 2],
     131           0 :                 vertexDataOut[q + 3]);
     132           0 :         q += 4;
     133             : 
     134           0 :         for (size_t j = 0; j < mdef->GetNumUVsPerVertex(); j++)
     135             :         {
     136           0 :             VertexArrayIterator<float[2]> UVit = m_UVs[j].GetIterator<float[2]>();
     137           0 :             UVit[i][0] = vertexDataOut[q + 0 + 2 * j];
     138           0 :             UVit[i][1] = vertexDataOut[q + 1 + 2 * j];
     139             :         }
     140             :     }
     141             : 
     142             :     // upload vertex data
     143           0 :     m_Array.Upload();
     144           0 :     m_Array.FreeBackingStore();
     145             : 
     146           0 :     m_IndexArray.SetNumberOfVertices(mdef->GetNumFaces() * 3);
     147           0 :     m_IndexArray.Layout();
     148             : 
     149           0 :     VertexArrayIterator<std::uint16_t> Indices = m_IndexArray.GetIterator();
     150             : 
     151             :     size_t idxidx = 0;
     152             : 
     153             :     // reindex geometry and upload index
     154           0 :     for (size_t j = 0; j < mdef->GetNumFaces(); ++j)
     155             :     {
     156           0 :         Indices[idxidx++] = remapTable[j * 3 + 0];
     157           0 :         Indices[idxidx++] = remapTable[j * 3 + 1];
     158           0 :         Indices[idxidx++] = remapTable[j * 3 + 2];
     159             :     }
     160             : 
     161           0 :     m_IndexArray.Upload();
     162           0 :     m_IndexArray.FreeBackingStore();
     163             : 
     164           0 :     const uint32_t stride = m_Array.GetStride();
     165           0 :     constexpr size_t MAX_UV = 2;
     166             : 
     167           0 :     PS::StaticVector<Renderer::Backend::SVertexAttributeFormat, 5 + MAX_UV> attributes{
     168             :         {Renderer::Backend::VertexAttributeStream::POSITION,
     169           0 :             m_Position.format, m_Position.offset, stride,
     170             :             Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0},
     171             :         {Renderer::Backend::VertexAttributeStream::NORMAL,
     172           0 :             m_Normal.format, m_Normal.offset, stride,
     173             :             Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0}
     174           0 :     };
     175             : 
     176           0 :     for (size_t uv = 0; uv < std::min(MAX_UV, mdef->GetNumUVsPerVertex()); ++uv)
     177             :     {
     178           0 :         const Renderer::Backend::VertexAttributeStream stream =
     179           0 :             static_cast<Renderer::Backend::VertexAttributeStream>(
     180             :                 static_cast<int>(Renderer::Backend::VertexAttributeStream::UV0) + uv);
     181           0 :         attributes.push_back({
     182           0 :             stream, m_UVs[uv].format, m_UVs[uv].offset, stride,
     183             :             Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0});
     184             :     }
     185             : 
     186           0 :     attributes.push_back({
     187             :         Renderer::Backend::VertexAttributeStream::UV2,
     188           0 :         m_Tangent.format, m_Tangent.offset, stride,
     189             :         Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0});
     190             : 
     191           0 :     m_VertexInputLayout = g_Renderer.GetVertexInputLayout({attributes.begin(), attributes.end()});
     192           0 : }
     193             : 
     194             : struct InstancingModelRendererInternals
     195             : {
     196             :     /// Previously prepared modeldef
     197             :     IModelDef* imodeldef;
     198             : };
     199             : 
     200             : 
     201             : // Construction and Destruction
     202          10 : InstancingModelRenderer::InstancingModelRenderer()
     203             : {
     204          10 :     m = new InstancingModelRendererInternals;
     205          10 :     m->imodeldef = 0;
     206          10 : }
     207             : 
     208          10 : InstancingModelRenderer::~InstancingModelRenderer()
     209             : {
     210          10 :     delete m;
     211          10 : }
     212             : 
     213             : 
     214             : // Build modeldef data if necessary - we have no per-CModel data
     215           0 : CModelRData* InstancingModelRenderer::CreateModelData(const void* key, CModel* model)
     216             : {
     217           0 :     CModelDefPtr mdef = model->GetModelDef();
     218           0 :     IModelDef* imodeldef = (IModelDef*)mdef->GetRenderData(m);
     219             : 
     220           0 :     ENSURE(!model->IsSkinned());
     221             : 
     222           0 :     if (!imodeldef)
     223             :     {
     224           0 :         imodeldef = new IModelDef(mdef);
     225           0 :         mdef->SetRenderData(m, imodeldef);
     226             :     }
     227             : 
     228           0 :     return new CModelRData(key);
     229           0 : }
     230             : 
     231           0 : void InstancingModelRenderer::UpdateModelsData(Renderer::Backend::IDeviceCommandContext*,
     232             :     std::span<CModel*>)
     233             : {
     234             :     // We have no per-CModel data
     235           0 : }
     236             : 
     237           0 : void InstancingModelRenderer::UploadModelsData(Renderer::Backend::IDeviceCommandContext*,
     238             :     std::span<CModel*>)
     239             : {
     240             :     // Data uploaded once during creation as we don't update it dynamically.
     241           0 : }
     242             : 
     243             : // Prepare UV coordinates for this modeldef
     244           0 : void InstancingModelRenderer::PrepareModelDef(
     245             :     Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
     246             :     const CModelDef& def)
     247             : {
     248           0 :     m->imodeldef = (IModelDef*)def.GetRenderData(m);
     249           0 :     ENSURE(m->imodeldef);
     250             : 
     251           0 :     deviceCommandContext->SetVertexInputLayout(m->imodeldef->m_VertexInputLayout);
     252             : 
     253           0 :     deviceCommandContext->SetIndexBuffer(m->imodeldef->m_IndexArray.GetBuffer());
     254             : 
     255           0 :     const uint32_t stride = m->imodeldef->m_Array.GetStride();
     256           0 :     const uint32_t firstVertexOffset = m->imodeldef->m_Array.GetOffset() * stride;
     257             : 
     258           0 :     deviceCommandContext->SetVertexBuffer(
     259             :         0, m->imodeldef->m_Array.GetBuffer(), firstVertexOffset);
     260           0 : }
     261             : 
     262             : 
     263             : // Render one model
     264           0 : void InstancingModelRenderer::RenderModel(Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
     265             :     Renderer::Backend::IShaderProgram*, CModel* model, CModelRData*)
     266             : {
     267           0 :     const CModelDefPtr& mdldef = model->GetModelDef();
     268             : 
     269             :     // Render the lot.
     270           0 :     const size_t numberOfFaces = mdldef->GetNumFaces();
     271             : 
     272           0 :     deviceCommandContext->DrawIndexedInRange(
     273           0 :         m->imodeldef->m_IndexArray.GetOffset(), numberOfFaces * 3, 0, m->imodeldef->m_Array.GetNumberOfVertices() - 1);
     274             : 
     275             :     // Bump stats.
     276           0 :     g_Renderer.m_Stats.m_DrawCalls++;
     277           0 :     g_Renderer.m_Stats.m_ModelTris += numberOfFaces;
     278           0 : }

Generated by: LCOV version 1.16