hi all,

i have continued my random optimizations to flightgear (and simgear, this 
time).

changes in the attached diffs
=================

in simgear:
  * removed _tex_cache from SGMaterial (osg takes care of texture caching)
  * made textures load on demand when SGMaterial::get_state() is first called
    (the only drawback so far is get_state() becoming non-const)
  * osg::State held a reference to its "parent" material in userData - this 
prevented the reference count for SGMaterial from dropping to 0

in flightgear:
  * valgrind showed leaks in HUD -- all SGCondition * changed to 
SGSharedPtr<SGCondition>
  * also changed HUD_decue to deque<SGSharedPtr<instr_item> >
  * "simplified" (and switchified) the nav.dat-loading code
  * f_interpolate in NasalSys was leaky (valgrind)
  * just cosmetics. if noone is working on Traffic/Schedule.cxx i'd like to 
have it produce less noise in info-log

both patches need to be applied for SGMaterial to work properly.

future plans
=======

i'd like to look at apt.dat-loading more closely. as pietro reported, the 
default.tower and default.atis files are out of sync. since the data exists 
in apt.dat, i'd like to load it all in one go from there.

also there is version 850 of apt.dat with interesting new features. is anyone 
working on getting this into terragear?

i consider the changes to SGMaterial only the first step. ideally, textures 
should get unloaded when not being used anymore.

of course there is also still a lot of output from valgrind, which i plan to 
investigate further.

as last time: please comment and / or commit

regards,

 -till
Index: simgear/scene/material/mat.cxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/scene/material/mat.cxx,v
retrieving revision 1.40
diff -u -3 -p -r1.40 mat.cxx
--- simgear/scene/material/mat.cxx	4 Dec 2007 22:38:41 -0000	1.40
+++ simgear/scene/material/mat.cxx	21 Jan 2008 21:23:47 -0000
@@ -52,8 +52,6 @@ SG_USING_STD(map);
 
 #include "mat.hxx"
 
-static map<string, osg::ref_ptr<osg::Texture2D> > _tex_cache;
-
 
 ////////////////////////////////////////////////////////////////////////
 // Constructors and destructor.
@@ -222,35 +220,24 @@ SGMaterial::init ()
     }
 }
 
-bool
-SGMaterial::load_texture ( int n )
-{
-    int i   = (n >= 0) ? n   : 0 ;
-    int end = (n >= 0) ? n+1 : _status.size();
-
-    for (; i < end; i++)
-    {
-        if ( !_status[i].texture_loaded ) {
-            SG_LOG( SG_GENERAL, SG_INFO, "Loading deferred texture "
-                                          << _status[i].texture_path );
-            assignTexture(_status[i].state.get(), _status[i].texture_path,
-                                         wrapu, wrapv, mipmap);
-            _status[i].texture_loaded = true;
-       }
-    }
-    return true;
-}
-
 osg::StateSet *
-SGMaterial::get_state (int n) const
+SGMaterial::get_state (int n)
 {
     if (_status.size() == 0) {
         SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
         return NULL;
     }
+    
+    int i = n >= 0 ? n : _current_ptr;
+
+    if(!_status[i].texture_loaded)
+    {
+        assignTexture(_status[i].state.get(), _status[i].texture_path,
+                      wrapu, wrapv, mipmap);
+        _status[i].texture_loaded = true;
+    }
+    osg::StateSet *st = _status[i].state.get();
 
-    osg::StateSet *st = (n >= 0) ? _status[n].state.get()
-                             : _status[_current_ptr].state.get();
     _current_ptr += 1;
     if (_current_ptr >= _status.size())
         _current_ptr = 0;
@@ -279,13 +266,7 @@ SGMaterial::build_state( bool defer_tex_
 
         stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
 
-        if ( !defer_tex_load ) {
-            SG_LOG(SG_INPUT, SG_INFO, "    " << _status[i].texture_path );
-	    assignTexture( stateSet, _status[i].texture_path, wrapu, wrapv);
-            _status[i].texture_loaded = true;
-        } else {
-            _status[i].texture_loaded = false;
-        }
+        _status[i].texture_loaded = false;
 
         osg::Material* material = new osg::Material;
         material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
@@ -320,21 +301,11 @@ void SGMaterial::set_state( osg::StateSe
 void SGMaterial::assignTexture( osg::StateSet *state, const std::string &fname,
                  int _wrapu, int _wrapv, int _mipmap )
 {
-   map<string, osg::ref_ptr<osg::Texture2D> >::iterator _tex_cache_iter;
-   _tex_cache_iter = _tex_cache.find(fname);
-   if (_tex_cache_iter == _tex_cache.end())
-   {
-     osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv,
-                                                mipmap ? -1 : 0);
-	  texture->setMaxAnisotropy( SGGetTextureFilter());
-      state->setTextureAttributeAndModes(0, texture);
-      _tex_cache[fname] = texture;
-   }
-   else
-   {
-      state->setTextureAttributeAndModes(0, _tex_cache_iter->second.get());
-      // cout << "Cache hit: " << fname << endl;
-   }
+   osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv,
+                                             mipmap ? -1 : 0);
+   texture->setMaxAnisotropy( SGGetTextureFilter());
+   state->setTextureAttributeAndModes(0, texture);
+
    osg::TexEnv* texEnv = new osg::TexEnv;
    texEnv->setMode(osg::TexEnv::MODULATE);
    state->setTextureAttributeAndModes(0, texEnv);
Index: simgear/scene/material/mat.hxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/scene/material/mat.hxx,v
retrieving revision 1.30
diff -u -3 -p -r1.30 mat.hxx
--- simgear/scene/material/mat.hxx	8 Jun 2007 06:50:16 -0000	1.30
+++ simgear/scene/material/mat.hxx	21 Jan 2008 21:23:48 -0000
@@ -114,17 +114,9 @@ public:
   ////////////////////////////////////////////////////////////////////
 
   /**
-   * Force the texture to load if it hasn't already.
-   *
-   * @return true if the texture loaded, false if it was loaded
-   * already.
-   */
-  bool load_texture (int n = -1);
-
-  /**
    * Get the textured state.
    */
-  osg::StateSet *get_state (int n = -1) const;
+  osg::StateSet *get_state (int n = -1);
 
 
   /**
@@ -331,7 +323,9 @@ public:
   const SGMaterial* getMaterial() const
   { return mMaterial; }
 private:
-  SGSharedPtr<const SGMaterial> mMaterial;
+  // this cannot be an SGSharedPtr since that would create a cicrular reference
+  // making it impossible to ever free the space needed by SGMaterial
+  const SGMaterial* mMaterial;
 };
 
 void
Index: simgear/scene/material/matlib.cxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/scene/material/matlib.cxx,v
retrieving revision 1.28
diff -u -3 -p -r1.28 matlib.cxx
--- simgear/scene/material/matlib.cxx	15 Oct 2007 18:49:50 -0000	1.28
+++ simgear/scene/material/matlib.cxx	21 Jan 2008 21:23:48 -0000
@@ -168,23 +168,9 @@ SGMaterial *SGMaterialLib::find( const s
     return NULL;
 }
 
-
 // Destructor
 SGMaterialLib::~SGMaterialLib ( void ) {
-}
-
-
-// Load one pending "deferred" texture.  Return true if a texture
-// loaded successfully, false if no pending, or error.
-void SGMaterialLib::load_next_deferred() {
-    // container::iterator it = begin();
-    for ( material_map_iterator it = begin(); it != end(); it++ ) {
-	/* we don't need the key, but here's how we'd get it if we wanted it. */
-        // const string &key = it->first;
-	SGMaterial *slot = it->second;
-	if (slot->load_texture())
-	  return;
-    }
+    SG_LOG( SG_GENERAL, SG_INFO, "SGMaterialLib::~SGMaterialLib() size=" << matlib.size());
 }
 
 const SGMaterial*
Index: simgear/scene/material/matlib.hxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/scene/material/matlib.hxx,v
retrieving revision 1.15
diff -u -3 -p -r1.15 matlib.hxx
--- simgear/scene/material/matlib.hxx	28 May 2007 05:00:29 -0000	1.15
+++ simgear/scene/material/matlib.hxx	21 Jan 2008 21:23:48 -0000
@@ -75,11 +75,6 @@ public:
     // find a material record by material name
     SGMaterial *find( const string& material );
 
-    /**
-     * Load the next deferred texture, if there is any.
-     */
-    void load_next_deferred();
-
     material_map_iterator begin() { return matlib.begin(); }
     const_material_map_iterator begin() const { return matlib.begin(); }
 
Index: src/Cockpit/hud.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Cockpit/hud.cxx,v
retrieving revision 1.41
diff -u -3 -p -u -r1.41 hud.cxx
--- src/Cockpit/hud.cxx	21 Dec 2007 12:03:10 -0000	1.41
+++ src/Cockpit/hud.cxx	21 Jan 2008 21:30:10 -0000
@@ -63,7 +63,7 @@ static HUD_Properties *HUDprop = 0;
 
 static char units[5];
 
-deque<instr_item *> HUD_deque;
+deque<SGSharedPtr<instr_item> > HUD_deque;
 
 fgTextList HUD_TextList;
 fgLineList HUD_LineList;
Index: src/Cockpit/hud.hxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Cockpit/hud.hxx,v
retrieving revision 1.39
diff -u -3 -p -u -r1.39 hud.hxx
--- src/Cockpit/hud.hxx	24 Jul 2007 05:36:27 -0000	1.39
+++ src/Cockpit/hud.hxx	21 Jan 2008 21:30:10 -0000
@@ -367,7 +367,7 @@ extern fgLineList         HUD_LineList;
 extern fgLineList         HUD_StippleLineList;
 
 
-class instr_item {  // An Abstract Base Class (ABC)
+class instr_item : public SGReferenced {  // An Abstract Base Class (ABC)
 private:
     static UINT        instances;     // More than 64K instruments? Nah!
     static int         brightness;
Index: src/Instrumentation/HUD/HUD.hxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Instrumentation/HUD/HUD.hxx,v
retrieving revision 1.33
diff -u -3 -p -u -r1.33 HUD.hxx
--- src/Instrumentation/HUD/HUD.hxx	29 Oct 2006 19:30:28 -0000	1.33
+++ src/Instrumentation/HUD/HUD.hxx	21 Jan 2008 21:30:10 -0000
@@ -23,6 +23,7 @@
 #define _HUD_HXX
 
 #include <simgear/compiler.h>
+#include <simgear/props/condition.hxx>
 
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
@@ -52,7 +53,6 @@ SG_USING_NAMESPACE(std);
 
 
 class FGViewer;
-class SGCondition;
 
 
 class LineSegment {
@@ -344,7 +344,7 @@ protected:
     float       _center_x, _center_y;
 
 private:
-    SGCondition *_condition;
+    SGSharedPtr<SGCondition> _condition;
     float       _disp_factor;   // Multiply by to get numbers shown on scale.
     float       _scr_span;      // Working values for draw;
     int         _digits;
@@ -370,7 +370,7 @@ private:
     float   _pointer_width;
     float   _pointer_length;
 
-    SGCondition *_blink_condition;
+    SGSharedPtr<SGCondition> _blink_condition;
     double  _blink_interval;
     double  _blink_target;  // time for next blink state change
     bool    _blink_state;
@@ -583,7 +583,7 @@ public:
     virtual void draw();
 
 private:
-    SGCondition *_active_condition;  // stadiametric (true) or standby (false)
+    SGSharedPtr<SGCondition> _active_condition;  // stadiametric (true) or standby (false)
     Input   _diameter;               // inner/outer radius relation
     float   _bullet_size;
     float   _inner_radius;
Index: src/Instrumentation/HUD/HUD_instrument.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Instrumentation/HUD/HUD_instrument.cxx,v
retrieving revision 1.13
diff -u -3 -p -u -r1.13 HUD_instrument.cxx
--- src/Instrumentation/HUD/HUD_instrument.cxx	1 Aug 2006 21:11:38 -0000	1.13
+++ src/Instrumentation/HUD/HUD_instrument.cxx	21 Jan 2008 21:30:10 -0000
@@ -24,7 +24,6 @@
 #endif
 
 #include <simgear/math/SGLimits.hxx>
-#include <simgear/props/condition.hxx>
 #include "HUD.hxx"
 
 
Index: src/Instrumentation/HUD/HUD_label.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Instrumentation/HUD/HUD_label.cxx,v
retrieving revision 1.15
diff -u -3 -p -u -r1.15 HUD_label.cxx
--- src/Instrumentation/HUD/HUD_label.cxx	22 Jan 2007 05:46:51 -0000	1.15
+++ src/Instrumentation/HUD/HUD_label.cxx	21 Jan 2008 21:30:10 -0000
@@ -23,7 +23,6 @@
 #  include <config.h>
 #endif
 
-#include <simgear/props/condition.hxx>
 #include "HUD.hxx"
 
 
Index: src/Instrumentation/HUD/HUD_misc.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Instrumentation/HUD/HUD_misc.cxx,v
retrieving revision 1.4
diff -u -3 -p -u -r1.4 HUD_misc.cxx
--- src/Instrumentation/HUD/HUD_misc.cxx	22 Jul 2006 17:27:43 -0000	1.4
+++ src/Instrumentation/HUD/HUD_misc.cxx	21 Jan 2008 21:30:10 -0000
@@ -22,7 +22,6 @@
 #  include <config.h>
 #endif
 
-#include <simgear/props/condition.hxx>
 #include "HUD.hxx"
 
 
Index: src/Main/main.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Main/main.cxx,v
retrieving revision 1.250
diff -u -3 -p -u -r1.250 main.cxx
--- src/Main/main.cxx	14 Dec 2007 22:51:57 -0000	1.250
+++ src/Main/main.cxx	21 Jan 2008 21:30:11 -0000
@@ -492,9 +492,6 @@ static void fgMainLoop( void ) {
     // Do any I/O channel work that might need to be done
     globals->get_io()->update( real_delta_time_sec );
 
-    // see if we need to load any deferred-load textures
-    globals->get_matlib()->load_next_deferred();
-
     // Run audio scheduler
 #ifdef ENABLE_AUDIO_SUPPORT
     if ( globals->get_soundmgr()->is_working() ) {
Index: src/Navaids/navdb.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Navaids/navdb.cxx,v
retrieving revision 1.20
diff -u -3 -p -u -r1.20 navdb.cxx
--- src/Navaids/navdb.cxx	11 Oct 2007 09:28:05 -0000	1.20
+++ src/Navaids/navdb.cxx	21 Jan 2008 21:30:11 -0000
@@ -48,6 +48,7 @@ bool fgNavDBInit( FGAirportList *airport
                   FGTACANList *channellist)
 {
     SG_LOG(SG_GENERAL, SG_INFO, "Loading Navaid Databases");
+    int cnt=0;
     // SG_LOG(SG_GENERAL, SG_INFO, "  VOR/NDB");
     // SGPath p_nav( globals->get_fg_root() );
     // p_nav.append( "Navaids/default.nav" );
@@ -115,37 +116,48 @@ bool fgNavDBInit( FGAirportList *airport
             }
         }
 
-        if ( r->get_type() == 2 || r->get_type() == 3 ) {
-            // NDB=2, VOR=3
-            navlist->add( r );
-        } else if ( r->get_type() == 4 || r->get_type() == 5 ) {
-            // ILS=4, LOC(only)=5
-            loclist->add( r );
-        } else if ( r->get_type() == 6 ) {
-            // GS=6
-            gslist->add( r );
-        } else if ( r->get_type() == 7 || r->get_type() == 8
-                    || r->get_type() == 9 )
-        {
-            // Marker Beacon = 7,8,9
-            mkrlist->add( r );
-        } else if ( r->get_type() == 12 || r->get_type() == 13) {
-            // DME with ILS=12; standalone DME=13
-            string str1( r->get_name() );
-            string::size_type loc1= str1.find( "TACAN", 0 );
-            string::size_type loc2 = str1.find( "VORTAC", 0 );
+        switch(r->get_type()) {
+            case 2:
+            case 3:
+                // NDB=2, VOR=3
+                navlist->add( r );
+		break;
+            case 4:
+            case 5:
+                // ILS=4, LOC(only)=5
+                loclist->add( r );
+                break;
+            case 6:
+                // GS=6
+                gslist->add( r );
+                break;
+            case 7:
+            case 8:
+            case 9:
+                // Marker Beacon = 7,8,9
+                mkrlist->add( r );
+                break;
+            case 12:
+            case 13:
+            {
+                // DME with ILS=12; standalone DME=13
+                string str1( r->get_name() );
+                string::size_type loc1= str1.find( "TACAN", 0 );
+                string::size_type loc2 = str1.find( "VORTAC", 0 );
                        
-            if( loc1 != string::npos || loc2 != string::npos ){
-                 //cout << " name = " << r->get_name() ;
-                 //cout << " freq = " << r->get_freq() ;
-                 tacanlist->add( r );
-                 }
+                if( loc1 != string::npos || loc2 != string::npos ) {
+                    //cout << " name = " << r->get_name() ;
+                    //cout << " freq = " << r->get_freq() ;
+                    tacanlist->add( r );
+                }
                 
-            dmelist->add( r );
-            
+                dmelist->add( r );
+                break;
+            }
+            default:
+                SG_LOG( SG_GENERAL, SG_WARN, "fgNavDBInit: bad type: " << r->get_type() );
         }
-		
-        in >> skipcomment;
+	cnt++;
     }
 
 // load the carrier navaids file
@@ -223,11 +235,13 @@ bool fgNavDBInit( FGAirportList *airport
        	//cout << "channel = " << r->get_channel() ;
        	//cout << " freq = " << r->get_freq() << endl;
  	
+	cnt++;
     } // end while
 
  
  // end ReadChanFile
 
+    SG_LOG( SG_GENERAL, SG_INFO, "fgNavDBInit: loaded " << cnt << " navaids" );
 
     return true;
 }
Index: src/Navaids/navrecord.hxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Navaids/navrecord.hxx,v
retrieving revision 1.13
diff -u -3 -p -u -r1.13 navrecord.hxx
--- src/Navaids/navrecord.hxx	20 Oct 2007 18:28:22 -0000	1.13
+++ src/Navaids/navrecord.hxx	21 Jan 2008 21:30:11 -0000
@@ -117,11 +117,7 @@ FGNavRecord::FGNavRecord(void) :
     freq(0),
     range(0),
     multiuse(0.0),
-    ident(""),
-    name(""),
-    apt_id(""),
-    serviceable(true),
-    trans_ident("")
+    serviceable(true)
 {
 }
 
@@ -146,12 +142,15 @@ operator >> ( istream& in, FGNavRecord& 
     }
 
     double lat, lon, elev_ft;
-    in >> lat >> lon >> elev_ft >> n.freq >> n.range >> n.multiuse
-       >> n.ident;
+    string apt, name;
+
+    in >> ::skipws >> lat >> lon >> elev_ft >> n.freq >> n.range >> n.multiuse
+       >> n.ident >> apt >> ws;
     n.pos.setLatitudeDeg(lat);
     n.pos.setLongitudeDeg(lon);
     n.pos.setElevationFt(elev_ft);
-    getline( in, n.name );
+    getline( in, name );
+    n.name=apt+" "+name;
 
     // silently multiply adf frequencies by 100 so that adf
     // vs. nav/loc frequency lookups can use the same code.
@@ -159,15 +158,9 @@ operator >> ( istream& in, FGNavRecord& 
         n.freq *= 100;
     }
 
-    // Remove any leading spaces before the name
-    while ( n.name.substr(0,1) == " " ) {
-        n.name = n.name.erase(0,1);
-    }
-
     if ( n.type >= 4 && n.type <= 9 ) {
         // these types are always associated with an airport id
-        string::size_type pos = n.name.find(" ");
-        n.apt_id = n.name.substr(0, pos);
+        n.apt_id = apt;
     }
 
     // Ranges are included with the latest data format, no need to
@@ -176,15 +169,21 @@ operator >> ( istream& in, FGNavRecord& 
 
     if ( n.range < 0.1 ) {
         // assign default ranges
-    
-        if ( n.type == 2 || n.type == 3 ) {
-            n.range = FG_NAV_DEFAULT_RANGE;
-        } else if ( n.type == 4 || n.type == 5 || n.type == 6 ) {
-            n.range = FG_LOC_DEFAULT_RANGE;
-        } else if ( n.type == 12 ) {
-            n.range = FG_DME_DEFAULT_RANGE;
-        } else {
-            n.range = FG_LOC_DEFAULT_RANGE;
+        switch(n.type) {
+            case 2:
+            case 3:
+                n.range = FG_NAV_DEFAULT_RANGE;
+                break;
+            case 4:
+            case 5:
+            case 6:
+                n.range = FG_LOC_DEFAULT_RANGE;
+                break;
+            case 12:
+                n.range = FG_DME_DEFAULT_RANGE;
+                break;
+            default:
+                n.range = FG_LOC_DEFAULT_RANGE;
         }
     }
 
Index: src/Scripting/NasalSys.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Scripting/NasalSys.cxx,v
retrieving revision 1.97
diff -u -3 -p -u -r1.97 NasalSys.cxx
--- src/Scripting/NasalSys.cxx	5 Dec 2007 10:57:51 -0000	1.97
+++ src/Scripting/NasalSys.cxx	21 Jan 2008 21:30:11 -0000
@@ -317,8 +317,8 @@ static naRef f_interpolate(naContext c, 
     naRef curve = argc > 1 ? args[1] : naNil();
     if(!naIsVector(curve)) return naNil();
     int nPoints = naVec_size(curve) / 2;
-    double* values = new double[nPoints];
-    double* deltas = new double[nPoints];
+    double values[nPoints];
+    double deltas[nPoints];
     for(int i=0; i<nPoints; i++) {
         values[i] = naNumValue(naVec_get(curve, 2*i)).num;
         deltas[i] = naNumValue(naVec_get(curve, 2*i+1)).num;
Index: src/Traffic/Schedule.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Traffic/Schedule.cxx,v
retrieving revision 1.29
diff -u -3 -p -u -r1.29 Schedule.cxx
--- src/Traffic/Schedule.cxx	27 Jul 2007 19:31:44 -0000	1.29
+++ src/Traffic/Schedule.cxx	21 Jan 2008 21:30:11 -0000
@@ -231,7 +231,7 @@ bool FGAISchedule::update(time_t now)
   if (!deptime)
     deptime = (*flights.begin())->getDepartureTime();
   FGScheduledFlightVecIterator i = flights.begin();
-  SG_LOG (SG_GENERAL, SG_INFO,"Processing registration " << registration << " with callsign " << (*i)->getCallSign());
+  SG_LOG (SG_GENERAL, SG_DEBUG,"Processing registration " << registration << " with callsign " << (*i)->getCallSign());
   if (AIManagerRef)
     {
       // Check if this aircraft has been released. 
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel

Reply via email to