LCOV - code coverage report
Current view: top level - engine/render - rendertimers.cpp (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 0.0 % 83 0
Test Date: 2025-04-16 12:09:02 Functions: 0.0 % 7 0

            Line data    Source code
       1              : /* rendertimers.cpp: renderer functionality used for displaying rendering stats
       2              :  * while the program is running
       3              :  *
       4              :  * timers can be created with designated start/stop points in the code; sub-ms
       5              :  * times needed for accurate diagnosis possible (each frame is ~16.6ms @ 60Hz)
       6              :  *
       7              :  * used in rendergl.cpp
       8              :  */
       9              : #include "../libprimis-headers/cube.h"
      10              : #include "../../shared/geomexts.h"
      11              : #include "../../shared/glexts.h"
      12              : 
      13              : #include "rendergl.h"
      14              : #include "rendertext.h"
      15              : #include "renderttf.h"
      16              : #include "renderva.h"
      17              : 
      18              : #include "interface/control.h"
      19              : 
      20              : void cleanuptimers();                              //needed for timer script gvar
      21            0 : VARFN(timer, usetimers, 0, 0, 1, cleanuptimers()); //toggles logging timer information & rendering it
      22              : VAR(frametimer, 0, 0, 1);                          //toggles timing how long each frame takes (and rendering it to timer ui)
      23              : 
      24              : struct timer
      25              : {
      26              :     enum
      27              :     {
      28              :         Timer_MaxQuery = 4          //max number of gl queries
      29              :     };
      30              :     const char *name;               //name the timer reports as
      31              :     bool gpu;                       //whether the timer is for gpu time (true) or cpu time
      32              :     std::array<GLuint, Timer_MaxQuery> query; //gpu query information
      33              :     int waiting;                    //internal bitmask for queries
      34              :     size_t starttime;               //time the timer was started (in terms of ms since game started)
      35              :     float result,                   //raw value of the timer, -1 if no info available
      36              :           print;                    //the time the timer displays: ms per frame for whatever object
      37              : };
      38              : 
      39              : //locally relevant functionality
      40              : namespace
      41              : {
      42              :     std::vector<timer> timers;
      43              :     std::vector<size_t> timerorder;
      44              :     size_t timercycle = 0;
      45              : 
      46            0 :     timer *findtimer(const char *name, bool gpu) //also creates a new timer if none found
      47              :     {
      48            0 :         for(size_t i = 0; i < timers.size(); i++)
      49              :         {
      50            0 :             if(!std::strcmp(timers[i].name, name) && timers[i].gpu == gpu)
      51              :             {
      52            0 :                 timerorder.erase(std::find(timerorder.begin(), timerorder.end(), i));
      53            0 :                 timerorder.push_back(i);
      54            0 :                 return &timers[i];
      55              :             }
      56              :         }
      57            0 :         timerorder.push_back(timers.size());
      58            0 :         timers.emplace_back();
      59            0 :         timer &t = timers.back();
      60            0 :         t.name = name;
      61            0 :         t.gpu = gpu;
      62            0 :         t.query.fill(0);
      63            0 :         if(gpu)
      64              :         {
      65            0 :             glGenQueries(timer::Timer_MaxQuery, t.query.data());
      66              :         }
      67            0 :         t.waiting = 0;
      68            0 :         t.starttime = 0;
      69            0 :         t.result = -1;
      70            0 :         t.print = -1;
      71            0 :         return &t;
      72              :     }
      73              : }
      74              : 
      75              : //externally relevant functionality
      76              : 
      77              : //used to start a timer in some part of the code, cannot be used outside of rendering part
      78              : 
      79            0 : timer *begintimer(const char *name, bool gpu)
      80              : {
      81            0 :     if(!usetimers || inbetweenframes || (gpu && (!hasTQ || deferquery)))
      82              :     {
      83            0 :         return nullptr;
      84              :     }
      85            0 :     timer *t = findtimer(name, gpu);
      86            0 :     if(t->gpu)
      87              :     {
      88            0 :         deferquery++;
      89            0 :         glBeginQuery(GL_TIME_ELAPSED_EXT, t->query[timercycle]);
      90            0 :         t->waiting |= 1<<timercycle;
      91              :     }
      92              :     else
      93              :     {
      94            0 :         t->starttime = getclockmillis();
      95              :     }
      96            0 :     return t;
      97              : }
      98              : 
      99              : //used to end a timer started by begintimer(), needs to be included sometime after begintimer
     100              : //the part between begintimer() and endtimer() is what gets timed
     101            0 : void endtimer(timer *t)
     102              : {
     103            0 :     if(!t)
     104              :     {
     105            0 :         return;
     106              :     }
     107            0 :     if(t->gpu)
     108              :     {
     109            0 :         glEndQuery(GL_TIME_ELAPSED_EXT);
     110            0 :         deferquery--;
     111              :     }
     112              :     else
     113              :     {
     114            0 :         t->result = std::max(static_cast<float>(getclockmillis() - t->starttime), 0.0f);
     115              :     }
     116              : }
     117              : 
     118              : //foreach timer, query what time has passed since last update
     119            0 : void synctimers()
     120              : {
     121            0 :     timercycle = (timercycle + 1) % timer::Timer_MaxQuery;
     122              : 
     123            0 :     for(timer& t : timers)
     124              :     {
     125            0 :         if(t.waiting&(1<<timercycle))
     126              :         {
     127            0 :             GLint available = 0;
     128            0 :             while(!available)
     129              :             {
     130            0 :                 glGetQueryObjectiv(t.query[timercycle], GL_QUERY_RESULT_AVAILABLE, &available);
     131              :             }
     132            0 :             GLuint64EXT result = 0;
     133            0 :             glGetQueryObjectui64v(t.query[timercycle], GL_QUERY_RESULT, &result);
     134            0 :             t.result = std::max(static_cast<float>(result) * 1e-6f, 0.0f);
     135            0 :             t.waiting &= ~(1<<timercycle);
     136              :         }
     137              :         else
     138              :         {
     139            0 :             t.result = -1;
     140              :         }
     141              :     }
     142            0 : }
     143              : 
     144            0 : void cleanuptimers()
     145              : {
     146            0 :     for(const timer& t : timers)
     147              :     {
     148            0 :         if(t.gpu)
     149              :         {
     150            0 :             glDeleteQueries(timer::Timer_MaxQuery, t.query.data());
     151              :         }
     152              :     }
     153            0 :     timers.clear();
     154            0 :     timerorder.clear();
     155            0 : }
     156              : 
     157            0 : void printtimers(int conw, int framemillis)
     158              : {
     159            0 :     if(!frametimer && !usetimers)
     160              :     {
     161            0 :         return;
     162              :     }
     163              :     static int lastprint = 0;
     164            0 :     int offset = 0;
     165            0 :     if(frametimer)
     166              :     {
     167              :         static int printmillis = 0;
     168            0 :         if(totalmillis - lastprint >= 200)
     169              :         {
     170            0 :             printmillis = framemillis;
     171              :         }
     172              :         char framestring[200];
     173            0 :         constexpr int size = 42;
     174            0 :         std::sprintf(framestring, "frame time %i ms", printmillis);
     175            0 :         ttr.fontsize(size);
     176            0 :         ttr.renderttf(framestring, {0xFF, 0xFF, 0xFF, 0}, conw-20*size, size*3/2+offset*9*size/8);
     177              :         //draw_textf("frame time %i ms", conw-20*FONTH, conh-FONTH*3/2-offset*9*FONTH/8, printmillis);
     178            0 :         offset++;
     179              :     }
     180            0 :     if(usetimers)
     181              :     {
     182            0 :         for(int i : timerorder)
     183              :         {
     184            0 :             timer &t = timers[i];
     185            0 :             if(t.print < 0 ? t.result >= 0 : totalmillis - lastprint >= 200)
     186              :             {
     187            0 :                 t.print = t.result;
     188              :             }
     189            0 :             if(t.print < 0 || (t.gpu && !(t.waiting&(1<<timercycle))))
     190              :             {
     191            0 :                 continue;
     192              :             }
     193              :             char framestring[200];
     194            0 :             constexpr int size = 42;
     195            0 :             std::sprintf(framestring, "%s%s %5.2f ms", t.name, t.gpu ? "" : " (cpu)", t.print);
     196            0 :             ttr.fontsize(size);
     197            0 :             ttr.renderttf(framestring, {0xFF, 0xFF, 0xFF, 0}, conw-20*size, size*3/2+offset*9*size/8);
     198              : 
     199              :             //draw_textf("%s%s %5.2f ms", conw-20*FONTH, conh-FONTH*3/2-offset*9*FONTH/8, t.name, t.gpu ? "" : " (cpu)", t.print);
     200            0 :             offset++;
     201              :         }
     202              :     }
     203            0 :     if(totalmillis - lastprint >= 200)
     204              :     {
     205            0 :         lastprint = totalmillis;
     206              :     }
     207              : }
        

Generated by: LCOV version 2.0-1