LCOV - code coverage report
Current view: top level - source/ps - Profiler2GPU.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 103 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 12 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : #include "precompiled.h"
      24             : 
      25             : #include "Profiler2GPU.h"
      26             : 
      27             : #include "lib/debug.h"
      28             : #include "ps/ConfigDB.h"
      29             : #include "ps/Profiler2.h"
      30             : #include "ps/VideoMode.h"
      31             : #include "renderer/backend/IDevice.h"
      32             : #include "renderer/backend/IDeviceCommandContext.h"
      33             : 
      34             : #include <cstddef>
      35             : #include <cstdint>
      36             : #include <deque>
      37             : #include <string>
      38             : #include <utility>
      39             : #include <vector>
      40             : 
      41             : /**
      42             :  * At each enter/leave-region event, we do an async GPU timestamp query.
      43             :  * When all the queries for a frame have their results available,
      44             :  * we convert their GPU timestamps into durations and record the data.
      45             :  */
      46             : class CProfiler2GPUImpl
      47             : {
      48             :     NONCOPYABLE(CProfiler2GPUImpl);
      49             : 
      50             :     struct SEvent
      51             :     {
      52             :         const char* id;
      53             :         uint32_t queryHandle;
      54             :         bool isEnter; // true if entering region; false if leaving
      55             :     };
      56             : 
      57           0 :     struct SFrame
      58             :     {
      59             :         std::uint32_t num;
      60             : 
      61             :         double syncTimeStart; // CPU time at start of this frame.
      62             : 
      63             :         std::vector<SEvent> events;
      64             :     };
      65             : 
      66             :     std::deque<SFrame> m_Frames;
      67             : 
      68             : public:
      69           0 :     CProfiler2GPUImpl(CProfiler2& profiler)
      70           0 :         : m_Profiler(profiler), m_Storage(*new CProfiler2::ThreadStorage(profiler, "gpu"))
      71             :     {
      72           0 :         m_Storage.RecordSyncMarker(m_Profiler.GetTime());
      73           0 :         m_Storage.Record(CProfiler2::ITEM_EVENT, m_Profiler.GetTime(), "thread start");
      74             : 
      75           0 :         m_Profiler.AddThreadStorage(&m_Storage);
      76           0 :     }
      77             : 
      78           0 :     ~CProfiler2GPUImpl()
      79             :     {
      80           0 :         while (!m_Frames.empty())
      81           0 :             PopFrontFrame();
      82             : 
      83           0 :         for (const uint32_t queryHandle : m_FreeQueries)
      84           0 :             g_VideoMode.GetBackendDevice()->FreeQuery(queryHandle);
      85           0 :         m_FreeQueries.clear();
      86             : 
      87           0 :         m_Profiler.RemoveThreadStorage(&m_Storage);
      88           0 :     }
      89             : 
      90           0 :     void FrameStart(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
      91             :     {
      92           0 :         ProcessFrames();
      93             : 
      94           0 :         SFrame frame;
      95           0 :         frame.num = m_Profiler.GetFrameNumber();
      96             : 
      97             :         // GL backend:
      98             :         // On (at least) some NVIDIA Windows drivers, when GPU-bound, or when
      99             :         // vsync enabled and not CPU-bound, the first glGet* call at the start
     100             :         // of a frame appears to trigger a wait (to stop the GPU getting too
     101             :         // far behind, or to wait for the vsync period).
     102             :         // That will be this GL_TIMESTAMP get, which potentially distorts the
     103             :         // reported results. So we'll only do it fairly rarely, and for most
     104             :         // frames we'll just assume the clocks don't drift much
     105             : 
     106             :         // Timestamps might shift and overflow for all backends. So for
     107             :         // simplicity we don't synchronize the frame start on CPU and GPU. As
     108             :         // we only need durations. We just roughly assume that the first
     109             :         // timestamp on GPU matches the CPU frame start. For real timestamps
     110             :         // it's better to use GPU Trace instruments.
     111             : 
     112           0 :         frame.syncTimeStart = m_Profiler.GetTime();
     113           0 :         m_Frames.push_back(frame);
     114             : 
     115           0 :         RegionEnter(deviceCommandContext, "frame");
     116           0 :     }
     117             : 
     118           0 :     void FrameEnd(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
     119             :     {
     120           0 :         RegionLeave(deviceCommandContext, "frame");
     121           0 :     }
     122             : 
     123           0 :     void RecordRegion(Renderer::Backend::IDeviceCommandContext* deviceCommandContext, const char* id, bool isEnter)
     124             :     {
     125           0 :         ENSURE(!m_Frames.empty());
     126           0 :         SFrame& frame = m_Frames.back();
     127             : 
     128           0 :         SEvent event;
     129           0 :         event.id = id;
     130           0 :         event.queryHandle = NewQuery();
     131           0 :         event.isEnter = isEnter;
     132             : 
     133           0 :         deviceCommandContext->InsertTimestampQuery(event.queryHandle, isEnter);
     134             : 
     135           0 :         frame.events.push_back(event);
     136           0 :     }
     137             : 
     138           0 :     void RegionEnter(Renderer::Backend::IDeviceCommandContext* deviceCommandContext, const char* id)
     139             :     {
     140           0 :         RecordRegion(deviceCommandContext, id, true);
     141           0 :     }
     142             : 
     143           0 :     void RegionLeave(Renderer::Backend::IDeviceCommandContext* deviceCommandContext, const char* id)
     144             :     {
     145           0 :         RecordRegion(deviceCommandContext, id, false);
     146           0 :     }
     147             : 
     148             : private:
     149             : 
     150           0 :     void ProcessFrames()
     151             :     {
     152           0 :         Renderer::Backend::IDevice* device{g_VideoMode.GetBackendDevice()};
     153           0 :         while (!m_Frames.empty())
     154             :         {
     155           0 :             SFrame& frame = m_Frames.front();
     156             : 
     157             :             // We assume queries become available in order so we only need to
     158             :             // check the last one.
     159           0 :             if (!device->IsQueryResultAvailable(frame.events.back().queryHandle))
     160             :                 break;
     161             : 
     162             :             // We use the first event GPU timestamp as a frame start.
     163           0 :             const uint64_t firstFrameTimestamp{!frame.events.empty()
     164           0 :                 ? device->GetQueryResult(frame.events[0].queryHandle) : 0u};
     165             : 
     166           0 :             const double timestampMultiplier{
     167           0 :                 device->GetCapabilities().timestampMultiplier};
     168             : 
     169           0 :             std::vector<std::pair<int, uint64_t>> stack;
     170             : 
     171             :             // The frame's queries are now available, so retrieve and record all their results:
     172           0 :             for (size_t i = 0; i < frame.events.size(); ++i)
     173             :             {
     174           0 :                 const uint64_t queryTimestamp{
     175           0 :                     i == 0 ? firstFrameTimestamp : device->GetQueryResult(frame.events[i].queryHandle)};
     176           0 :                 ENSURE(queryTimestamp >= firstFrameTimestamp);
     177             : 
     178             :                 // Convert to absolute CPU-clock time
     179           0 :                 const double t{
     180           0 :                     frame.syncTimeStart + static_cast<double>(queryTimestamp - firstFrameTimestamp) * timestampMultiplier};
     181             : 
     182             :                 // Record a frame-start for syncing
     183           0 :                 if (i == 0)
     184           0 :                     m_Storage.RecordFrameStart(t);
     185             : 
     186           0 :                 if (frame.events[i].isEnter)
     187           0 :                     m_Storage.Record(CProfiler2::ITEM_ENTER, t, frame.events[i].id);
     188             :                 else
     189           0 :                     m_Storage.RecordLeave(t);
     190             : 
     191             :                 // Associate the frame number with the "frame" region
     192           0 :                 if (i == 0)
     193           0 :                     m_Storage.RecordAttributePrintf("%u", frame.num);
     194             :             }
     195             : 
     196           0 :             PopFrontFrame();
     197           0 :         }
     198           0 :     }
     199             : 
     200           0 :     void PopFrontFrame()
     201             :     {
     202           0 :         ENSURE(!m_Frames.empty());
     203           0 :         SFrame& frame = m_Frames.front();
     204           0 :         for (size_t i = 0; i < frame.events.size(); ++i)
     205           0 :             m_FreeQueries.push_back(frame.events[i].queryHandle);
     206           0 :         m_Frames.pop_front();
     207           0 :     }
     208             : 
     209             :     // Returns a new backend query handle (or a recycled old one).
     210           0 :     uint32_t NewQuery()
     211             :     {
     212           0 :         if (m_FreeQueries.empty())
     213           0 :             return g_VideoMode.GetBackendDevice()->AllocateQuery();
     214             : 
     215           0 :         const uint32_t queryHandle{m_FreeQueries.back()};
     216           0 :         m_FreeQueries.pop_back();
     217           0 :         return queryHandle;
     218             :     }
     219             : 
     220             :     CProfiler2& m_Profiler;
     221             :     CProfiler2::ThreadStorage& m_Storage;
     222             : 
     223             :     std::vector<uint32_t> m_FreeQueries; // query objects that are allocated but not currently in used
     224             : };
     225             : 
     226           0 : CProfiler2GPU::CProfiler2GPU(CProfiler2& profiler) :
     227           0 :     m_Profiler(profiler)
     228             : {
     229           0 :     if (g_ConfigDB.Get("profiler2.gpu.enable", false) && g_VideoMode.GetBackendDevice()->GetCapabilities().timestamps)
     230             :     {
     231           0 :         m_Impl = std::make_unique<CProfiler2GPUImpl>(m_Profiler);
     232             :     }
     233           0 : }
     234             : 
     235           0 : CProfiler2GPU::~CProfiler2GPU() = default;
     236             : 
     237           0 : void CProfiler2GPU::FrameStart(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
     238             : {
     239           0 :     if (m_Impl)
     240           0 :         m_Impl->FrameStart(deviceCommandContext);
     241           0 : }
     242             : 
     243           0 : void CProfiler2GPU::FrameEnd(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
     244             : {
     245           0 :     if (m_Impl)
     246           0 :         m_Impl->FrameEnd(deviceCommandContext);
     247           0 : }
     248             : 
     249           0 : void CProfiler2GPU::RegionEnter(
     250             :     Renderer::Backend::IDeviceCommandContext* deviceCommandContext, const char* id)
     251             : {
     252           0 :     if (m_Impl)
     253           0 :         m_Impl->RegionEnter(deviceCommandContext, id);
     254           0 : }
     255             : 
     256           0 : void CProfiler2GPU::RegionLeave(
     257             :     Renderer::Backend::IDeviceCommandContext* deviceCommandContext, const char* id)
     258             : {
     259           0 :     if (m_Impl)
     260           0 :         m_Impl->RegionLeave(deviceCommandContext, id);
     261           0 : }

Generated by: LCOV version 1.16