]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scenery/newcache.cxx
Fix a bug in tile manager updating when switching from one view to the next.
[flightgear.git] / src / Scenery / newcache.cxx
index 2f0a489af03aece77a68851ae54bd76532b14092..0ba6176f8acb61f26feb6301caa41976fcd7d3ae 100644 (file)
 #  include <config.h>
 #endif
 
-#ifdef HAVE_WINDOWS_H
-#  include <windows.h>
-#endif
-
-#include <GL/glut.h>
-#include <simgear/xgl/xgl.h>
-
 #include <plib/ssg.h>          // plib include
 
 #include <simgear/bucket/newbucket.hxx>
 #include <simgear/debug/logstream.hxx>
-#include <simgear/math/sg_geodesy.hxx>
-#include <simgear/math/sg_random.h>
-#include <simgear/misc/fgstream.hxx>
-#include <simgear/misc/fgpath.hxx>
+#include <simgear/misc/sg_path.hxx>
 
 #include <Main/globals.hxx>
-#include <Objects/matlib.hxx>
-#include <Objects/newmat.hxx>
-#include <Objects/obj.hxx>
+#include <Main/viewer.hxx>
 #include <Scenery/scenery.hxx>  // for scenery.center
 
 #include "newcache.hxx"
 #include "tileentry.hxx"
-#include "tilemgr.hxx"         // temp, need to delete later
 
-FG_USING_NAMESPACE(std);
 
-// a cheesy hack (to be fixed later)
-extern ssgBranch *terrain;
-extern ssgBranch *ground;
-
-
-// the tile cache
-FGNewCache global_tile_cache;
+SG_USING_NAMESPACE(std);
 
 
 // Constructor
-FGNewCache::FGNewCache( void ) {
+FGNewCache::FGNewCache( void ) :
+    max_cache_size(100)
+{
     tile_cache.clear();
 }
 
@@ -75,206 +57,211 @@ FGNewCache::~FGNewCache( void ) {
 
 // Free a tile cache entry
 void FGNewCache::entry_free( long cache_index ) {
-    FG_LOG( FG_TERRAIN, FG_INFO, "FREEING CACHE ENTRY = " << cache_index );
-    FGTileEntry *e = tile_cache[cache_index];
-    e->free_tile();
-    delete( e );
+    SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
+    FGTileEntry *tile = tile_cache[cache_index];
+    tile->disconnect_ssg_nodes();
+
+#ifdef WISH_PLIB_WAS_THREADED // but it isn't
+    tile->sched_removal();
+#else
+    tile->free_tile();
+    delete tile;
+#endif
+
     tile_cache.erase( cache_index );
 }
 
 
 // Initialize the tile cache subsystem
 void FGNewCache::init( void ) {
-    FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
+    SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
 
-    // expand cache if needed.  For best results ... i.e. to avoid
-    // tile load problems and blank areas: 
-    max_cache_size = 50;       // a random number to start with
-    FG_LOG( FG_TERRAIN, FG_INFO, "  max cache size = " 
+    SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = " 
            << max_cache_size );
-    FG_LOG( FG_TERRAIN, FG_INFO, "  current cache size = " 
+    SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = " 
            << tile_cache.size() );
-    
-    tile_map_iterator current = tile_cache.begin();
-    tile_map_iterator end = tile_cache.end();
-    
-    for ( ; current != end; ++current ) {
-       long index = current->first;
-       cout << "clearing " << index << endl;
-       FGTileEntry *e = current->second;
-       e->tile_bucket.make_bad();
-       entry_free(index);
-    }
 
-    // and ... just in case we missed something ... 
-    terrain->removeAllKids();
+#if 0 // don't clear the cache
+    clear_cache();
+#endif
 
-    FG_LOG( FG_TERRAIN, FG_INFO, "  done with init()"  );
+    SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
 }
 
 
 // Search for the specified "bucket" in the cache
-bool FGNewCache::exists( const FGBucket& b ) {
+bool FGNewCache::exists( const SGBucket& b ) const {
     long tile_index = b.gen_index();
-    tile_map_iterator it = tile_cache.find( tile_index );
+    const_tile_map_iterator it = tile_cache.find( tile_index );
 
     return ( it != tile_cache.end() );
 }
 
 
 #if 0
-static void print_refs( ssgSelector *sel, ssgTransform *trans, 
-                ssgRangeSelector *range) 
-{
-    cout << "selector -> " << sel->getRef()
-        << "  transform -> " << trans->getRef()
-        << "  range -> " << range->getRef() << endl;
-}
-#endif
-
+// Ensure at least one entry is free in the cache
+bool FGNewCache::make_space() {
+    SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
+    SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
+    SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
 
-static ssgLeaf *gen_lights( ssgVertexArray *lights ) {
-    // Allocate ssg structure
-    ssgNormalArray   *nl = NULL;
-    ssgTexCoordArray *tl = NULL;
-    ssgColourArray   *cl = new ssgColourArray( 1 );
+    if ( (int)tile_cache.size() < max_cache_size ) {
+       // space in the cache, return
+       return true;
+    }
 
-    // default to slightly yellow lights for now
-    sgVec4 color;
-    sgSetVec4( color, 1.0, 1.0, 0.7, 1.0 );
-    cl->add( color );
+    while ( (int)tile_cache.size() >= max_cache_size ) {
+       sgdVec3 abs_view_pos;
+       float dist;
+        double timestamp = 0.0;
+       int max_index = -1;
+        double min_time = 2419200000.0f;  // one month should be enough
+        double max_time = 0;
 
-    // create ssg leaf
-    ssgLeaf *leaf = 
-       new ssgVtxTable ( GL_POINTS, lights, nl, tl, cl );
+       // we need to free the furthest entry
+       tile_map_iterator current = tile_cache.begin();
+       tile_map_iterator end = tile_cache.end();
+    
+       for ( ; current != end; ++current ) {
+           long index = current->first;
+           FGTileEntry *e = current->second;
+           if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
+
+                timestamp = e->get_timestamp();
+                if ( timestamp < min_time ) {
+                    max_index = index;
+                    min_time = timestamp;
+                }
+                if ( timestamp > max_time ) {
+                    max_time = timestamp;
+                }
+
+           } else {
+                SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
+                        << " pending models = " << e->get_pending_models()
+                        << " time stamp = " << e->get_timestamp() );
+            }
+       }
 
-    // assign state
-    FGNewMat *newmat = material_lib.find( "LIGHTS" );
-    leaf->setState( newmat->get_state() );
+       // If we made it this far, then there were no open cache entries.
+       // We will instead free the oldest cache entry and return true
+        
+        SG_LOG( SG_TERRAIN, SG_DEBUG, "    min_time = " << min_time );
+        SG_LOG( SG_TERRAIN, SG_DEBUG, "    index = " << max_index );
+        SG_LOG( SG_TERRAIN, SG_DEBUG, "    max_time = " << max_time );
+       if ( max_index >= 0 ) {
+           entry_free( max_index );
+           return true;
+       } else {
+           SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
+                    "cache is full, but no entries available for removal." );
+           return false;
+       }
+    }
 
-    return leaf;
+    SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in  "
+            "FGNewCache::make_space()." );
+    return false;
 }
+#endif
 
 
-// Fill in a tile cache entry with real data for the specified bucket
-void FGNewCache::fill_in( const FGBucket& b ) {
-    FG_LOG( FG_TERRAIN, FG_INFO, "FILL IN CACHE ENTRY = " << b.gen_index() );
+// Return the index of the oldest tile in the cache, return -1 if
+// nothing available to be removed.
+long FGNewCache::get_oldest_tile() {
+    // we need to free the furthest entry
+    long min_index = -1;
+    double timestamp = 0.0;
+    double min_time = 2419200000.0f; // one month should be enough
+    double max_time = 0;
 
-    // clear out a distant entry in the cache if needed.
-    make_space();
+    tile_map_iterator current = tile_cache.begin();
+    tile_map_iterator end = tile_cache.end();
+    
+    for ( ; current != end; ++current ) {
+        long index = current->first;
+        FGTileEntry *e = current->second;
+        if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
+            
+            timestamp = e->get_timestamp();
+            if ( timestamp < min_time ) {
+                min_index = index;
+                min_time = timestamp;
+            }
+            if ( timestamp > max_time ) {
+                max_time = timestamp;
+            }
+
+        } else {
+            SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
+                    << " pending models = " << e->get_pending_models()
+                    << " time stamp = " << e->get_timestamp() );
+        }
+    }
 
-    // create the entry
-    FGTileEntry *e = new FGTileEntry;
+    SG_LOG( SG_TERRAIN, SG_DEBUG, "    min_time = " << min_time );
+    SG_LOG( SG_TERRAIN, SG_DEBUG, "    index = " << min_index );
+    SG_LOG( SG_TERRAIN, SG_DEBUG, "    max_time = " << max_time );
 
-    // register it in the cache
-    long tile_index = b.gen_index();
-    tile_cache[tile_index] = e;
+    return min_index;
+}
 
-    // update the contents
-    e->center = Point3D( 0.0 );
-    if ( e->vec3_ptrs.size() || e->vec2_ptrs.size() || e->index_ptrs.size() ) {
-       FG_LOG( FG_TERRAIN, FG_ALERT, 
-               "Attempting to overwrite existing or"
-               << " not properly freed leaf data." );
-       exit(-1);
-    }
 
-    e->terra_transform = new ssgTransform;
-    e->terra_range = new ssgRangeSelector;
-    e->tile_bucket = b;
+// Clear a cache entry, note that the cache only holds pointers
+// and this does not free the object which is pointed to.
+void FGNewCache::clear_entry( long cache_index ) {
+    tile_cache.erase( cache_index );
+}
 
-    FGPath tile_path;
-    if ( globals->get_options()->get_fg_scenery() != "" ) {
-       tile_path.set( globals->get_options()->get_fg_scenery() );
-    } else {
-       tile_path.set( globals->get_options()->get_fg_root() );
-       tile_path.append( "Scenery" );
-    }
-    tile_path.append( b.gen_base_path() );
-    
-    // fgObjLoad will generate ground lighting for us ...
-    ssgVertexArray *light_pts = new ssgVertexArray( 100 );
 
-    // Load the appropriate data file
-    FGPath tile_base = tile_path;
-    tile_base.append( b.gen_index_str() );
-    ssgBranch *new_tile = fgObjLoad( tile_base.str(), e, light_pts, true );
+// Clear all completely loaded tiles (ignores partially loaded tiles)
+void FGNewCache::clear_cache() {
 
-    if ( new_tile != NULL ) {
-       e->terra_range->addKid( new_tile );
+    tile_map_iterator current = tile_cache.begin();
+    tile_map_iterator end = tile_cache.end();
+    
+    for ( ; current != end; ++current ) {
+       long index = current->first;
+       SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
+       FGTileEntry *e = current->second;
+        if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
+            e->tile_bucket.make_bad();
+            entry_free(index);
+        }
     }
-  
-    // load custom objects
-    cout << "CUSTOM OBJECTS" << endl;
 
-    FGPath index_path = tile_path;
-    index_path.append( b.gen_index_str() );
-    index_path.concat( ".ind" );
-
-    cout << "Looking in " << index_path.str() << endl;
+    // and ... just in case we missed something ... 
+    globals->get_scenery()->get_terrain_branch()->removeAllKids();
+}
 
-    fg_gzifstream in( index_path.str() );
 
-    if ( in.is_open() ) {
-       string token, name;
+/**
+ * Create a new tile and schedule it for loading.
+ */
+bool FGNewCache::insert_tile( FGTileEntry *e ) {
+    // set time of insertion for tracking age of tiles...
+    e->set_timestamp(globals->get_sim_time_sec());
 
-       while ( ! in.eof() ) {
-           in >> token;
-           in >> name;
-#if defined ( macintosh ) || defined ( _MSC_VER )
-           in >> ::skipws;
-#else
-           in >> skipws;
-#endif
-           cout << "token = " << token << " name = " << name << endl;
-
-           FGPath custom_path = tile_path;
-           custom_path.append( name );
-           ssgBranch *custom_obj = 
-               fgObjLoad( custom_path.str(), e, NULL, false );
-           if ( (new_tile != NULL) && (custom_obj != NULL) ) {
-               new_tile -> addKid( custom_obj );
-           }
-       }
-    }
+    // register it in the cache
+    long tile_index = e->get_tile_bucket().gen_index();
+    tile_cache[tile_index] = e;
 
-    e->terra_transform->addKid( e->terra_range );
-
-    // calculate initial tile offset
-    e->SetOffset( scenery.center );
-    sgCoord sgcoord;
-    sgSetCoord( &sgcoord,
-               e->offset.x(), e->offset.y(), e->offset.z(),
-               0.0, 0.0, 0.0 );
-    e->terra_transform->setTransform( &sgcoord );
-    terrain->addKid( e->terra_transform );
-
-    e->lights_transform = NULL;
-    /* uncomment this section for testing ground lights */
-    ssgLeaf *lights = gen_lights( light_pts );
-    if ( lights ) {
-       e->lights_transform = new ssgTransform;
-       e->lights_range = new ssgRangeSelector;
-       e->lights_range->addKid( lights );
-       e->lights_transform->addKid( e->lights_range );
-       e->lights_transform->setTransform( &sgcoord );
-       ground->addKid( e->lights_transform );
-    }
-    /* end of ground light section */
+    return true;
 }
 
 
+// Note this is the old version of FGNewCache::make_space(), currently disabled
+// It uses distance from a center point to determine tiles to be discarded...
+#if 0
 // Ensure at least one entry is free in the cache
-void FGNewCache::make_space() {
-    FG_LOG( FG_TERRAIN, FG_INFO, "Make space in cache" );
-
-    
-    cout << "cache size = " << tile_cache.size() << endl;
-    cout << "max size = " << max_cache_size << endl;
+bool FGNewCache::make_space() {
+    SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
+    SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
+    SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
 
     if ( (int)tile_cache.size() < max_cache_size ) {
        // space in the cache, return
-       return;
+       return true;
     }
 
     while ( (int)tile_cache.size() >= max_cache_size ) {
@@ -291,40 +278,54 @@ void FGNewCache::make_space() {
            long index = current->first;
            FGTileEntry *e = current->second;
 
-           // calculate approximate distance from view point
-           sgdCopyVec3( abs_view_pos,
-                        globals->get_current_view()->get_abs_view_pos() );
-
-           FG_LOG( FG_TERRAIN, FG_DEBUG, "DIST Abs view pos = " 
-                   << abs_view_pos[0] << ","
-                   << abs_view_pos[1] << ","
-                   << abs_view_pos[2] );
-           FG_LOG( FG_TERRAIN, FG_DEBUG,
-                   "    ref point = " << e->center );
-
-           sgdVec3 center;
-           sgdSetVec3( center, e->center.x(), e->center.y(), e->center.z() );
-           dist = sgdDistanceVec3( center, abs_view_pos );
-
-           FG_LOG( FG_TERRAIN, FG_DEBUG, "    distance = " << dist );
-
-           if ( dist > max_dist ) {
-               max_dist = dist;
-               max_index = index;
-           }
+           if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
+               // calculate approximate distance from view point
+               sgdCopyVec3( abs_view_pos,
+                            globals->get_current_view()->get_absolute_view_pos() );
+
+               SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = " 
+                       << abs_view_pos[0] << ","
+                       << abs_view_pos[1] << ","
+                       << abs_view_pos[2] );
+               SG_LOG( SG_TERRAIN, SG_DEBUG,
+                       "    ref point = " << e->center );
+
+               sgdVec3 center;
+               sgdSetVec3( center,
+                           e->center.x(), e->center.y(), e->center.z() );
+               dist = sgdDistanceVec3( center, abs_view_pos );
+
+               SG_LOG( SG_TERRAIN, SG_DEBUG, "    distance = " << dist );
+
+               if ( dist > max_dist ) {
+                   max_dist = dist;
+                   max_index = index;
+               }
+           } else {
+                SG_LOG( SG_TERRAIN, SG_INFO, "loaded = " << e->is_loaded()
+                        << " pending models = " << e->get_pending_models() );
+            }
        }
 
        // If we made it this far, then there were no open cache entries.
-       // We will instead free the furthest cache entry and return it's
-       // index.
-
+       // We will instead free the furthest cache entry and return true
+        
+        SG_LOG( SG_TERRAIN, SG_INFO, "    max_dist = " << max_dist );
+        SG_LOG( SG_TERRAIN, SG_INFO, "    index = " << max_index );
        if ( max_index >= 0 ) {
-           FG_LOG( FG_TERRAIN, FG_DEBUG, "    max_dist = " << max_dist );
-           FG_LOG( FG_TERRAIN, FG_DEBUG, "    index = " << max_index );
            entry_free( max_index );
+           return true;
        } else {
-           FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
-           exit( -1 );
+           SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
+                    "cache is full, but no entries available for removal." );
+           return false;
        }
     }
+
+    SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in  "
+            "FGNewCache::make_space()." );
+    return false;
 }
+#endif
+
+