]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/newcache.cxx
Check for the plib version when using display lists, just to be sure.
[flightgear.git] / src / Scenery / newcache.cxx
1 // newcache.cxx -- routines to handle scenery tile caching
2 //
3 // Written by Curtis Olson, started December 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <plib/ssg.h>           // plib include
29
30 #include <simgear/bucket/newbucket.hxx>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/misc/sg_path.hxx>
33
34 #include <Main/globals.hxx>
35 #include <Main/viewer.hxx>
36 #include <Scenery/scenery.hxx>  // for scenery.center
37
38 #include "newcache.hxx"
39 #include "tileentry.hxx"
40
41
42 SG_USING_NAMESPACE(std);
43
44
45 // Constructor
46 FGNewCache::FGNewCache( void ) :
47     max_cache_size(100)
48 {
49     tile_cache.clear();
50 }
51
52
53 // Destructor
54 FGNewCache::~FGNewCache( void ) {
55 }
56
57
58 // Free a tile cache entry
59 void FGNewCache::entry_free( long cache_index ) {
60     SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
61     FGTileEntry *tile = tile_cache[cache_index];
62     tile->disconnect_ssg_nodes();
63
64 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
65     tile->sched_removal();
66 #else
67     tile->free_tile();
68     delete tile;
69 #endif
70
71     tile_cache.erase( cache_index );
72 }
73
74
75 // Initialize the tile cache subsystem
76 void FGNewCache::init( void ) {
77     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
78
79     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = " 
80             << max_cache_size );
81     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = " 
82             << tile_cache.size() );
83
84 #if 0 // don't clear the cache
85     clear_cache();
86 #endif
87
88     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
89 }
90
91
92 // Search for the specified "bucket" in the cache
93 bool FGNewCache::exists( const SGBucket& b ) const {
94     long tile_index = b.gen_index();
95     const_tile_map_iterator it = tile_cache.find( tile_index );
96
97     return ( it != tile_cache.end() );
98 }
99
100
101 #if 0
102 // Ensure at least one entry is free in the cache
103 bool FGNewCache::make_space() {
104     SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
105     SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
106     SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
107
108     if ( (int)tile_cache.size() < max_cache_size ) {
109         // space in the cache, return
110         return true;
111     }
112
113     while ( (int)tile_cache.size() >= max_cache_size ) {
114         sgdVec3 abs_view_pos;
115         float dist;
116         double timestamp = 0.0;
117         int max_index = -1;
118         double min_time = 2419200000.0f;  // one month should be enough
119         double max_time = 0;
120
121         // we need to free the furthest entry
122         tile_map_iterator current = tile_cache.begin();
123         tile_map_iterator end = tile_cache.end();
124     
125         for ( ; current != end; ++current ) {
126             long index = current->first;
127             FGTileEntry *e = current->second;
128             // if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
129             if ( e->is_loaded() ) {
130
131                 timestamp = e->get_timestamp();
132                 if ( timestamp < min_time ) {
133                     max_index = index;
134                     min_time = timestamp;
135                 }
136                 if ( timestamp > max_time ) {
137                     max_time = timestamp;
138                 }
139
140             } else {
141                 SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
142                         << " pending models = " << e->get_pending_models()
143                         << " time stamp = " << e->get_timestamp() );
144             }
145         }
146
147         // If we made it this far, then there were no open cache entries.
148         // We will instead free the oldest cache entry and return true
149         
150         SG_LOG( SG_TERRAIN, SG_DEBUG, "    min_time = " << min_time );
151         SG_LOG( SG_TERRAIN, SG_DEBUG, "    index = " << max_index );
152         SG_LOG( SG_TERRAIN, SG_DEBUG, "    max_time = " << max_time );
153         if ( max_index >= 0 ) {
154             entry_free( max_index );
155             return true;
156         } else {
157             SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
158                     "cache is full, but no entries available for removal." );
159             return false;
160         }
161     }
162
163     SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in  "
164             "FGNewCache::make_space()." );
165     return false;
166 }
167 #endif
168
169
170 // Return the index of the oldest tile in the cache, return -1 if
171 // nothing available to be removed.
172 long FGNewCache::get_oldest_tile() {
173     // we need to free the furthest entry
174     long min_index = -1;
175     double timestamp = 0.0;
176     double min_time = 2419200000.0f; // one month should be enough
177     double max_time = 0;
178
179     tile_map_iterator current = tile_cache.begin();
180     tile_map_iterator end = tile_cache.end();
181     
182     for ( ; current != end; ++current ) {
183         long index = current->first;
184         FGTileEntry *e = current->second;
185         if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
186             
187             timestamp = e->get_timestamp();
188             if ( timestamp < min_time ) {
189                 min_index = index;
190                 min_time = timestamp;
191             }
192             if ( timestamp > max_time ) {
193                 max_time = timestamp;
194             }
195
196         } else {
197             SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
198                     << " pending models = " << e->get_pending_models()
199                     << " time stamp = " << e->get_timestamp() );
200         }
201     }
202
203     SG_LOG( SG_TERRAIN, SG_DEBUG, "    min_time = " << min_time );
204     SG_LOG( SG_TERRAIN, SG_DEBUG, "    index = " << min_index );
205     SG_LOG( SG_TERRAIN, SG_DEBUG, "    max_time = " << max_time );
206
207     return min_index;
208 }
209
210
211 // Clear the inner ring flag for all tiles in the cache so that the
212 // external tile scheduler can flag the inner ring correctly.
213 void FGNewCache::clear_inner_ring_flags() {
214     tile_map_iterator current = tile_cache.begin();
215     tile_map_iterator end = tile_cache.end();
216     
217     for ( ; current != end; ++current ) {
218         FGTileEntry *e = current->second;
219         if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
220             e->set_inner_ring( false );
221         }
222     }
223 }
224
225 // Clear a cache entry, note that the cache only holds pointers
226 // and this does not free the object which is pointed to.
227 void FGNewCache::clear_entry( long cache_index ) {
228     tile_cache.erase( cache_index );
229 }
230
231
232 // Clear all completely loaded tiles (ignores partially loaded tiles)
233 void FGNewCache::clear_cache() {
234
235     tile_map_iterator current = tile_cache.begin();
236     tile_map_iterator end = tile_cache.end();
237     
238     for ( ; current != end; ++current ) {
239         long index = current->first;
240         SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
241         FGTileEntry *e = current->second;
242         if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
243             e->tile_bucket.make_bad();
244             entry_free(index);
245         }
246     }
247
248     // and ... just in case we missed something ... 
249     globals->get_scenery()->get_terrain_branch()->removeAllKids();
250 }
251
252
253 /**
254  * Create a new tile and schedule it for loading.
255  */
256 bool FGNewCache::insert_tile( FGTileEntry *e ) {
257     // set time of insertion for tracking age of tiles...
258     e->set_timestamp(globals->get_sim_time_sec());
259
260     // register it in the cache
261     long tile_index = e->get_tile_bucket().gen_index();
262     tile_cache[tile_index] = e;
263
264     return true;
265 }
266
267
268 // Note this is the old version of FGNewCache::make_space(), currently disabled
269 // It uses distance from a center point to determine tiles to be discarded...
270 #if 0
271 // Ensure at least one entry is free in the cache
272 bool FGNewCache::make_space() {
273     SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
274     SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
275     SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
276
277     if ( (int)tile_cache.size() < max_cache_size ) {
278         // space in the cache, return
279         return true;
280     }
281
282     while ( (int)tile_cache.size() >= max_cache_size ) {
283         sgdVec3 abs_view_pos;
284         float dist;
285         float max_dist = 0.0;
286         int max_index = -1;
287
288         // we need to free the furthest entry
289         tile_map_iterator current = tile_cache.begin();
290         tile_map_iterator end = tile_cache.end();
291     
292         for ( ; current != end; ++current ) {
293             long index = current->first;
294             FGTileEntry *e = current->second;
295
296             if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
297                 // calculate approximate distance from view point
298                 sgdCopyVec3( abs_view_pos,
299                              globals->get_current_view()->get_absolute_view_pos() );
300
301                 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = " 
302                         << abs_view_pos[0] << ","
303                         << abs_view_pos[1] << ","
304                         << abs_view_pos[2] );
305                 SG_LOG( SG_TERRAIN, SG_DEBUG,
306                         "    ref point = " << e->center );
307
308                 sgdVec3 center;
309                 sgdSetVec3( center,
310                             e->center.x(), e->center.y(), e->center.z() );
311                 dist = sgdDistanceVec3( center, abs_view_pos );
312
313                 SG_LOG( SG_TERRAIN, SG_DEBUG, "    distance = " << dist );
314
315                 if ( dist > max_dist ) {
316                     max_dist = dist;
317                     max_index = index;
318                 }
319             } else {
320                 SG_LOG( SG_TERRAIN, SG_INFO, "loaded = " << e->is_loaded()
321                         << " pending models = " << e->get_pending_models() );
322             }
323         }
324
325         // If we made it this far, then there were no open cache entries.
326         // We will instead free the furthest cache entry and return true
327         
328         SG_LOG( SG_TERRAIN, SG_INFO, "    max_dist = " << max_dist );
329         SG_LOG( SG_TERRAIN, SG_INFO, "    index = " << max_index );
330         if ( max_index >= 0 ) {
331             entry_free( max_index );
332             return true;
333         } else {
334             SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
335                     "cache is full, but no entries available for removal." );
336             return false;
337         }
338     }
339
340     SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in  "
341             "FGNewCache::make_space()." );
342     return false;
343 }
344 #endif
345
346