optimizations and refactoring to get a small (1-2%) speed gain
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Sat, 4 Mar 2006 07:14:10 +0000 (07:14 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Sat, 4 Mar 2006 07:14:10 +0000 (07:14 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@6055 d7cf8633-e32d-0410-b094-e92efae38249

cl_main.c
cl_screen.c
cl_video.c
cl_video.h
client.h
gl_backend.c
gl_rmain.c
prvm_cmds.c
r_lightning.c
render.h

index 46db360..9a651db 100644 (file)
--- a/cl_main.c
+++ b/cl_main.c
@@ -92,6 +92,8 @@ int cl_max_dlights;
 int cl_max_lightstyle;
 int cl_max_brushmodel_entities;
 int cl_activedlights;
+int cl_activeeffects;
+int cl_activebeams;
 
 entity_t *cl_entities;
 entity_t *cl_csqcentities;     //[515]: csqc
@@ -170,6 +172,8 @@ void CL_ClearState(void)
        cl_max_lightstyle = MAX_LIGHTSTYLES;
        cl_max_brushmodel_entities = MAX_EDICTS;
        cl_activedlights = 0;
+       cl_activeeffects = 0;
+       cl_activebeams = 0;
 
        cl_entities = (entity_t *)Mem_Alloc(cl_mempool, cl_max_entities * sizeof(entity_t));
        cl_csqcentities = (entity_t *)Mem_Alloc(cl_mempool, cl_max_csqcentities * sizeof(entity_t));    //[515]: csqc
@@ -576,6 +580,7 @@ void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float
                e->frame = 0;
                e->frame1time = cl.time;
                e->frame2time = cl.time;
+               cl_activeeffects = max(cl_activeeffects, i + 1);
                break;
        }
 }
@@ -1378,7 +1383,7 @@ static void CL_RelinkEffects(void)
        entity_t *ent;
        float frame;
 
-       for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
+       for (i = 0, e = cl_effects;i < cl_activeeffects;i++, e++)
        {
                if (e->active)
                {
@@ -1387,6 +1392,8 @@ static void CL_RelinkEffects(void)
                        if (intframe < 0 || intframe >= e->endframe)
                        {
                                memset(e, 0, sizeof(*e));
+                               while (cl_activeeffects > 0 && !cl_effects[cl_activeeffects - 1].active)
+                                       cl_activeeffects--;
                                continue;
                        }
 
@@ -1440,10 +1447,17 @@ void CL_RelinkBeams(void)
        float forward;
        matrix4x4_t tempmatrix;
 
-       for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
+       while (cl_activebeams > 0 && !cl_beams[cl_activebeams - 1].model)
+               cl_activeeffects--;
+       for (i = 0, b = cl_beams;i < cl_activebeams;i++, b++)
        {
-               if (!b->model || b->endtime < cl.time)
+               if (!b->model)
+                       continue;
+               if (b->endtime < cl.time)
+               {
+                       b->model = NULL;
                        continue;
+               }
 
                // if coming from the player, update the start position
                //if (b->entity == cl.viewentity)
index bb4ddfd..75a8a26 100644 (file)
@@ -536,11 +536,44 @@ void R_TimeReport(char *desc)
        }
 }
 
-void R_TimeReport_Start(void)
+void R_TimeReport_Frame(void)
 {
-       r_timereport_active = r_speeds.integer && cls.signon == SIGNONS && cls.state == ca_connected;
-       r_speeds_string[0] = 0;
-       if (r_timereport_active)
+       int i, j, lines, y;
+
+       if (r_speeds_string[0])
+       {
+               if (r_timereport_active)
+                       R_TimeReport("total");
+
+               r_timereport_current = r_timereport_start;
+               j = (int)strlen(r_speeds_string);
+               if (r_timereport_active && j > 0)
+               {
+                       if (r_speeds_string[j-1] == '\n')
+                               r_speeds_string[j-1] = 0;
+                       lines = 1;
+                       for (i = 0;r_speeds_string[i];i++)
+                               if (r_speeds_string[i] == '\n')
+                                       lines++;
+                       y = vid_conheight.integer - sb_lines - lines * 8;
+                       i = j = 0;
+                       DrawQ_Fill(0, y, vid_conwidth.integer, lines * 8, 0, 0, 0, 0.5, 0);
+                       while (r_speeds_string[i])
+                       {
+                               j = i;
+                               while (r_speeds_string[i] && r_speeds_string[i] != '\n')
+                                       i++;
+                               if (i - j > 0)
+                                       DrawQ_String(0, y, r_speeds_string + j, i - j, 8, 8, 1, 1, 1, 1, 0);
+                               if (r_speeds_string[i] == '\n')
+                                       i++;
+                               y += 8;
+                       }
+               }
+               r_speeds_string[0] = 0;
+               r_timereport_active = false;
+       }
+       if (r_speeds.integer && cls.signon == SIGNONS && cls.state == ca_connected)
        {
                speedstringcount = 0;
                sprintf(r_speeds_string + strlen(r_speeds_string), "org:'%+8.2f %+8.2f %+8.2f' dir:'%+2.3f %+2.3f %+2.3f'\n", r_vieworigin[0], r_vieworigin[1], r_vieworigin[2], r_viewforward[0], r_viewforward[1], r_viewforward[2]);
@@ -551,41 +584,12 @@ void R_TimeReport_Start(void)
                else
                        sprintf(r_speeds_string + strlen(r_speeds_string), "rendered%6i meshes%8i triangles\n", renderstats.meshes, renderstats.meshes_elements / 3);
 
-               r_timereport_start = Sys_DoubleTime();
-       }
-
-       memset(&renderstats, 0, sizeof(renderstats));
-}
-
-void R_TimeReport_End(void)
-{
-       int i, j, lines, y;
-
-       r_timereport_current = r_timereport_start;
-       R_TimeReport("total");
+               memset(&renderstats, 0, sizeof(renderstats));
 
-       j = (int)strlen(r_speeds_string);
-       if (r_timereport_active && j > 0)
-       {
-               if (r_speeds_string[j-1] == '\n')
-                       r_speeds_string[j-1] = 0;
-               lines = 1;
-               for (i = 0;r_speeds_string[i];i++)
-                       if (r_speeds_string[i] == '\n')
-                               lines++;
-               y = vid_conheight.integer - sb_lines - lines * 8;
-               i = j = 0;
-               DrawQ_Fill(0, y, vid_conwidth.integer, lines * 8, 0, 0, 0, 0.5, 0);
-               while (r_speeds_string[i])
+               if (r_speeds.integer >= 2)
                {
-                       j = i;
-                       while (r_speeds_string[i] && r_speeds_string[i] != '\n')
-                               i++;
-                       if (i - j > 0)
-                               DrawQ_String(0, y, r_speeds_string + j, i - j, 8, 8, 1, 1, 1, 1, 0);
-                       if (r_speeds_string[i] == '\n')
-                               i++;
-                       y += 8;
+                       r_timereport_active = true;
+                       r_timereport_start = Sys_DoubleTime();
                }
        }
 }
@@ -1511,14 +1515,14 @@ void CL_UpdateScreen(void)
 
        SCR_CaptureVideo();
 
-       if (cls.signon == SIGNONS)
+       if (r_timereport_active)
                R_TimeReport("other");
 
        CL_SetupScreenSize();
 
        DrawQ_Clear();
 
-       if (cls.signon == SIGNONS)
+       if (r_timereport_active)
                R_TimeReport("setup");
 
        //FIXME: force menu if nothing else to look at?
@@ -1540,9 +1544,9 @@ void CL_UpdateScreen(void)
        //ui_draw();
        if (cls.signon == SIGNONS)
        {
-               R_TimeReport("2d");
-               R_TimeReport_End();
-               R_TimeReport_Start();
+               if (r_timereport_active)
+                       R_TimeReport("2d");
+               R_TimeReport_Frame();
        }
        R_Shadow_EditLights_DrawSelectedLightProperties();
 
index c969eaa..a6f067d 100644 (file)
@@ -9,18 +9,10 @@ static int  cl_videobmask;
 static int  cl_videogmask;
 static int     cl_videobytesperpixel;
 
+static int cl_activevideos;
 static clvideo_t videoarray[ MAXCLVIDEOS ];
 static rtexturepool_t *cl_videotexturepool;
 
-static clvideo_t *FindUnusedVid( void )
-{
-       int i;
-       for( i = 1 ; i < MAXCLVIDEOS ; i++ )
-               if( videoarray[ i ].state == CLVIDEO_UNUSED )
-                       return &videoarray[ i ];
-       return NULL;
-}
-
 static qboolean OpenStream( clvideo_t * video )
 {
        char *errorstring;
@@ -69,12 +61,34 @@ static qboolean WakeVideo( clvideo_t * video )
        return true;
 }
 
-static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner )
+clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner, qboolean cinematic )
 {
+       int i;
+       clvideo_t *video;
+
+       if (cinematic)
+       {
+               video = videoarray;
+               i = 0;
+       }
+       else
+       {
+               for (i = 1, video = videoarray; i < cl_activevideos;i++, video++)
+                       if (videoarray[i].state == CLVIDEO_UNUSED)
+                               break;
+               if (i == MAXCLVIDEOS)
+               {
+                       Con_Printf( "unable to open video \"%s\" - video limit reached\n", filename );
+                       return NULL;
+               }
+       }
+
        strncpy( video->filename, filename, MAX_QPATH );
        video->ownertag = owner;
+
        if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
                return NULL;
+
        strncpy( video->cpif.name, name, MAX_QPATH );
 
        if( !OpenStream( video ) )
@@ -87,36 +101,25 @@ static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char
 
        video->cpif.width = dpvsimpledecode_getwidth( video->stream );
        video->cpif.height = dpvsimpledecode_getheight( video->stream );
-       video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
-               video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
+       video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
 
-    video->imagedata = Mem_Alloc( cl_mempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
+       video->imagedata = Mem_Alloc( cl_mempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
 
+       // expand the active range to include the new entry
+       cl_activevideos = max(cl_activevideos, i + 1);
        return video;
 }
 
-clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner )
-{
-       clvideo_t *video;
-
-       video = FindUnusedVid();
-       if( !video ) {
-               Con_Printf( "unable to open video \"%s\" - video limit reached\n", filename );
-               return NULL;
-       }
-       return OpenVideo( video, filename, name, owner );
-}
-
 clvideo_t* CL_GetVideo( const char *name )
 {
        int i;
        clvideo_t *video;
 
-       for( i = 0 ; i < MAXCLVIDEOS ; i++ )
+       for( i = 0 ; i < cl_activevideos ; i++ )
                if( videoarray[ i ].state != CLVIDEO_UNUSED
                        &&      !strcmp( videoarray[ i ].cpif.name , name ) )
                        break;
-       if( i == MAXCLVIDEOS )
+       if( i == cl_activevideos )
                return NULL;
        video = &videoarray[ i ];
 
@@ -204,7 +207,7 @@ void CL_VideoFrame( void ) // update all videos
        int i;
        clvideo_t *video;
 
-       for( video = videoarray, i = 0 ; i < MAXCLVIDEOS ; video++, i++ )
+       for( video = videoarray, i = 0 ; i < cl_activevideos ; video++, i++ )
                if( video->state != CLVIDEO_UNUSED && !video->suspended )
                {
                        if( realtime - video->lasttime > CLTHRESHOLD )
@@ -217,19 +220,23 @@ void CL_VideoFrame( void ) // update all videos
 
        if( videoarray->state == CLVIDEO_FIRSTFRAME )
                CL_VideoStop();
+
+       // reduce range to exclude unnecessary entries
+       while (cl_activevideos > 0 && videoarray[cl_activevideos-1].state == CLVIDEO_UNUSED)
+               cl_activevideos--;
 }
 
 void CL_Video_Shutdown( void )
 {
        int i;
-       for( i = 0 ; i < MAXCLVIDEOS ; i++ )
+       for( i = 0 ; i < cl_activevideos ; i++ )
                CL_CloseVideo( &videoarray[ i ] );
 }
 
 void CL_PurgeOwner( int owner )
 {
        int i;
-       for( i = 0 ; i < MAXCLVIDEOS ; i++ )
+       for( i = 0 ; i < cl_activevideos ; i++ )
                if( videoarray[ i ].ownertag == owner )
                        CL_CloseVideo( &videoarray[ i ] );
 }
@@ -248,7 +255,7 @@ void CL_VideoStart(char *filename)
 
        if( videoarray->state != CLVIDEO_UNUSED )
                CL_CloseVideo( videoarray );
-       if( !OpenVideo( videoarray, filename, va( CLVIDEOPREFIX "%s", filename ), 0 ) )
+       if( !CL_OpenVideo( filename, va( CLVIDEOPREFIX "%s", filename ), 0, true ) )
                return;
 
        cl_videoplaying = true;
@@ -292,7 +299,7 @@ static void cl_video_start( void )
 
        cl_videotexturepool = R_AllocTexturePool();
 
-       for( video = videoarray, i = 0 ; i < MAXCLVIDEOS ; i++, video++ )
+       for( video = videoarray, i = 0 ; i < cl_activevideos ; i++, video++ )
                if( video->state != CLVIDEO_UNUSED && !video->suspended )
                        video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
                                video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
@@ -309,6 +316,7 @@ static void cl_video_newmap( void )
 
 void CL_Video_Init( void )
 {
+       cl_activevideos = 0;
        cl_videobytesperpixel = 4;
        cl_videormask = BigLong(0xFF000000);
        cl_videogmask = BigLong(0x00FF0000);
index 8439e92..c651eb4 100644 (file)
@@ -45,7 +45,7 @@ typedef struct clvideo_s
        char    filename[MAX_QPATH];
 } clvideo_t;
 
-clvideo_t*     CL_OpenVideo( const char *filename, const char *name, int owner );
+clvideo_t*     CL_OpenVideo( const char *filename, const char *name, int owner, qboolean cinematic );
 clvideo_t*     CL_GetVideo( const char *name );
 void           CL_SetVideoState( clvideo_t *video, clvideostate_t state );
 void           CL_RestartVideo( clvideo_t *video );
index 5b485c0..b004d8b 100644 (file)
--- a/client.h
+++ b/client.h
@@ -833,6 +833,8 @@ extern int cl_max_dlights;
 extern int cl_max_lightstyle;
 extern int cl_max_brushmodel_entities;
 extern int cl_activedlights;
+extern int cl_activeeffects;
+extern int cl_activebeams;
 
 extern entity_t *cl_entities;
 extern entity_t *cl_csqcentities;      //[515]: csqc
index 8e4ec15..439ec64 100644 (file)
@@ -1742,7 +1742,8 @@ void SCR_DrawScreen (void)
 {
        R_Mesh_Start();
 
-       R_TimeReport("setup");
+       if (r_timereport_active)
+               R_TimeReport("setup");
 
        if (cls.signon == SIGNONS)
        {
@@ -1818,7 +1819,8 @@ void SCR_DrawScreen (void)
 
        R_Mesh_Finish();
 
-       R_TimeReport("meshfinish");
+       if (r_timereport_active)
+               R_TimeReport("meshfinish");
 }
 
 void SCR_UpdateLoadingScreen (void)
@@ -1904,7 +1906,8 @@ void SCR_UpdateScreen (void)
        qglClear(GL_COLOR_BUFFER_BIT);
        CHECKGLERROR
 
-       R_TimeReport("clear");
+       if (r_timereport_active)
+               R_TimeReport("clear");
 
        if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer || r_stereo_sidebyside.integer)
        {
@@ -1947,7 +1950,8 @@ void SCR_UpdateScreen (void)
                SCR_DrawScreen();
 
        VID_Finish();
-       R_TimeReport("finish");
+       if (r_timereport_active)
+               R_TimeReport("finish");
 }
 
 
index 45ee836..3e6c5d6 100644 (file)
@@ -1091,7 +1091,8 @@ void R_RenderView(void)
        R_ClearScreen();
        R_Textures_Frame();
        R_UpdateFog();
-       R_TimeReport("setup");
+       if (r_timereport_active)
+               R_TimeReport("setup");
 
        qglDepthFunc(GL_LEQUAL);
        qglPolygonOffset(0, 0);
@@ -1103,7 +1104,8 @@ void R_RenderView(void)
        qglDisable(GL_POLYGON_OFFSET_FILL);
 
        R_BlendView();
-       R_TimeReport("blendview");
+       if (r_timereport_active)
+               R_TimeReport("blendview");
 
        GL_Scissor(0, 0, vid.width, vid.height);
        GL_ScissorTest(false);
@@ -1137,7 +1139,8 @@ void CSQC_R_ClearScreen (void)
        R_ClearScreen();
        R_Textures_Frame();
        R_UpdateFog();
-       R_TimeReport("setup");
+       if (r_timereport_active)
+               R_TimeReport("setup");
 }
 
 //[515]: csqc
@@ -1153,7 +1156,8 @@ void CSQC_R_RenderScene (void)
        qglDisable(GL_POLYGON_OFFSET_FILL);
 
        R_BlendView();
-       R_TimeReport("blendview");
+       if (r_timereport_active)
+               R_TimeReport("blendview");
 
        GL_Scissor(0, 0, vid.width, vid.height);
        GL_ScissorTest(false);
@@ -1186,10 +1190,12 @@ void R_RenderScene(void)
        R_SkyStartFrame();
 
        R_WorldVisibility();
-       R_TimeReport("worldvis");
+       if (r_timereport_active)
+               R_TimeReport("worldvis");
 
        R_MarkEntities();
-       R_TimeReport("markentity");
+       if (r_timereport_active)
+               R_TimeReport("markentity");
 
        R_Shadow_UpdateWorldLightSelection();
 
@@ -1216,21 +1222,25 @@ void R_RenderScene(void)
                        if (r_refdef.extraupdate)
                                S_ExtraUpdate ();
 
-                       GL_ShowTrisColor(0.025, 0.025, 0, 1);
+                       if (r_showtrispass)
+                               GL_ShowTrisColor(0.025, 0.025, 0, 1);
                        if (r_refdef.worldmodel && r_refdef.worldmodel->DrawSky)
                        {
                                r_refdef.worldmodel->DrawSky(r_refdef.worldentity);
-                               R_TimeReport("worldsky");
+                               if (r_timereport_active)
+                                       R_TimeReport("worldsky");
                        }
 
-                       if (R_DrawBrushModelsSky())
+                       if (R_DrawBrushModelsSky() && r_timereport_active)
                                R_TimeReport("bmodelsky");
 
-                       GL_ShowTrisColor(0.05, 0.05, 0.05, 1);
+                       if (r_showtrispass)
+                               GL_ShowTrisColor(0.05, 0.05, 0.05, 1);
                        if (r_refdef.worldmodel && r_refdef.worldmodel->Draw)
                        {
                                r_refdef.worldmodel->Draw(r_refdef.worldentity);
-                               R_TimeReport("world");
+                               if (r_timereport_active)
+                                       R_TimeReport("world");
                        }
                }
 
@@ -1238,49 +1248,60 @@ void R_RenderScene(void)
                if (r_refdef.extraupdate)
                        S_ExtraUpdate ();
 
-               GL_ShowTrisColor(0, 0.015, 0, 1);
+               if (r_showtrispass)
+                       GL_ShowTrisColor(0, 0.015, 0, 1);
 
                R_DrawModels();
-               R_TimeReport("models");
+               if (r_timereport_active)
+                       R_TimeReport("models");
 
                // don't let sound skip if going slow
                if (r_refdef.extraupdate)
                        S_ExtraUpdate ();
 
-               GL_ShowTrisColor(0, 0, 0.033, 1);
+               if (r_showtrispass)
+                       GL_ShowTrisColor(0, 0, 0.033, 1);
                R_ShadowVolumeLighting(false);
-               R_TimeReport("rtlights");
+               if (r_timereport_active)
+                       R_TimeReport("rtlights");
 
                // don't let sound skip if going slow
                if (r_refdef.extraupdate)
                        S_ExtraUpdate ();
 
-               GL_ShowTrisColor(0.1, 0, 0, 1);
+               if (r_showtrispass)
+                       GL_ShowTrisColor(0.1, 0, 0, 1);
 
                if (cl.csqc_vidvars.drawworld)
                {
                        R_DrawLightningBeams();
-                       R_TimeReport("lightning");
+                       if (r_timereport_active)
+                               R_TimeReport("lightning");
 
                        R_DrawParticles();
-                       R_TimeReport("particles");
+                       if (r_timereport_active)
+                               R_TimeReport("particles");
 
                        R_DrawExplosions();
-                       R_TimeReport("explosions");
+                       if (r_timereport_active)
+                               R_TimeReport("explosions");
                }
 
                R_MeshQueue_RenderTransparent();
-               R_TimeReport("drawtrans");
+               if (r_timereport_active)
+                       R_TimeReport("drawtrans");
 
                if (cl.csqc_vidvars.drawworld)
                {
                        R_DrawCoronas();
-                       R_TimeReport("coronas");
+                       if (r_timereport_active)
+                               R_TimeReport("coronas");
                }
                if(cl.csqc_vidvars.drawcrosshair)
                {
                        R_DrawWorldCrosshair();
-                       R_TimeReport("crosshair");
+                       if (r_timereport_active)
+                               R_TimeReport("crosshair");
                }
 
                VM_AddPolygonsToMeshQueue();
index 722582e..19925a9 100644 (file)
@@ -2683,7 +2683,7 @@ void VM_cin_open( void )
        VM_CheckEmptyString( file );
     VM_CheckEmptyString( name );
 
-       if( CL_OpenVideo( file, name, MENUOWNER ) )
+       if( CL_OpenVideo( file, name, MENUOWNER, false ) )
                PRVM_G_FLOAT( OFS_RETURN ) = 1;
        else
                PRVM_G_FLOAT( OFS_RETURN ) = 0;
index 7226b8a..bc4efbb 100644 (file)
@@ -337,7 +337,7 @@ void R_DrawLightningBeams(void)
                return;
 
        beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
-       for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
+       for (i = 0, b = cl_beams;i < cl_activebeams;i++, b++)
        {
                if (b->model && b->endtime >= r_refdef.time && b->lightning)
                {
index d931764..5646c66 100644 (file)
--- a/render.h
+++ b/render.h
@@ -60,6 +60,7 @@ extern void SHOWLMP_clear(void);
 
 // render profiling stuff
 extern char r_speeds_string[1024];
+extern int r_timereport_active;
 
 // lighting stuff
 extern cvar_t r_ambient;
@@ -239,8 +240,6 @@ extern rtexture_t *r_texture_fogattenuation;
 extern rtexture_t *r_texture_fogintensity;
 
 void R_TimeReport(char *name);
-void R_TimeReport_Start(void);
-void R_TimeReport_End(void);
 
 // r_stain
 void R_Stain(const vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2);