]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/newcache.cxx
Threaded tile paging:
[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 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>
30 #endif
31
32 #include <GL/glut.h>
33 #include <GL/gl.h>
34
35 #include <plib/ssg.h>           // plib include
36
37 #include <simgear/bucket/newbucket.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/sg_path.hxx>
40
41 #include <Main/globals.hxx>
42 #include <Scenery/scenery.hxx>  // for scenery.center
43
44 #include "newcache.hxx"
45 #include "tileentry.hxx"
46
47
48 SG_USING_NAMESPACE(std);
49
50
51 // Constructor
52 FGNewCache::FGNewCache( void ) {
53     tile_cache.clear();
54 }
55
56
57 // Destructor
58 FGNewCache::~FGNewCache( void ) {
59 }
60
61
62 // Free a tile cache entry
63 void FGNewCache::entry_free( long cache_index ) {
64     SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
65     FGTileEntry *tile = tile_cache[cache_index];
66     tile->disconnect_ssg_nodes();
67     tile->sched_removal();
68
69 #if 0
70     tile->free_tile();
71     delete tile;
72 #endif
73
74     tile_cache.erase( cache_index );
75 }
76
77
78 // Initialize the tile cache subsystem
79 void FGNewCache::init( void ) {
80     // This is a hack that should really get cleaned up at some point
81     extern ssgBranch *terrain;
82
83     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
84
85     // expand cache if needed.  For best results ... i.e. to avoid
86     // tile load problems and blank areas: 
87     max_cache_size = 50;        // a random number to start with
88     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = " 
89             << max_cache_size );
90     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = " 
91             << tile_cache.size() );
92     
93     tile_map_iterator current = tile_cache.begin();
94     tile_map_iterator end = tile_cache.end();
95     
96     for ( ; current != end; ++current ) {
97         long index = current->first;
98         SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
99         FGTileEntry *e = current->second;
100         e->tile_bucket.make_bad();
101         entry_free(index);
102     }
103
104     // and ... just in case we missed something ... 
105     terrain->removeAllKids();
106
107     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
108 }
109
110
111 // Search for the specified "bucket" in the cache
112 bool FGNewCache::exists( const SGBucket& b ) const {
113     long tile_index = b.gen_index();
114     const_tile_map_iterator it = tile_cache.find( tile_index );
115
116     return ( it != tile_cache.end() );
117 }
118
119
120 // depricated for threading
121 #if 0
122 // Fill in a tile cache entry with real data for the specified bucket
123 void FGNewCache::fill_in( const SGBucket& b ) {
124     SG_LOG( SG_TERRAIN, SG_DEBUG, "FILL IN CACHE ENTRY = " << b.gen_index() );
125
126     // clear out a distant entry in the cache if needed.
127     make_space();
128
129     // create the entry
130     FGTileEntry *e = new FGTileEntry( b );
131
132     // register it in the cache
133     long tile_index = b.gen_index();
134     tile_cache[tile_index] = e;
135
136     SGPath tile_path;
137     if ( globals->get_fg_scenery() != (string)"" ) {
138         tile_path.set( globals->get_fg_scenery() );
139     } else {
140         tile_path.set( globals->get_fg_root() );
141         tile_path.append( "Scenery" );
142     }
143     
144     // Load the appropriate data file
145     e->load( tile_path, true );
146 }
147 #endif
148
149
150 // Ensure at least one entry is free in the cache
151 void FGNewCache::make_space() {
152     SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
153     SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
154     SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
155
156     if ( (int)tile_cache.size() < max_cache_size ) {
157         // space in the cache, return
158         return;
159     }
160
161     while ( (int)tile_cache.size() >= max_cache_size ) {
162         sgdVec3 abs_view_pos;
163         float dist;
164         float max_dist = 0.0;
165         int max_index = -1;
166
167         // we need to free the furthest entry
168         tile_map_iterator current = tile_cache.begin();
169         tile_map_iterator end = tile_cache.end();
170     
171         for ( ; current != end; ++current ) {
172             long index = current->first;
173             FGTileEntry *e = current->second;
174
175             if ( e->is_loaded() && e->get_pending_models() == 0 ) {
176                 // calculate approximate distance from view point
177                 sgdCopyVec3( abs_view_pos,
178                              globals->get_current_view()->get_abs_view_pos() );
179
180                 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = " 
181                         << abs_view_pos[0] << ","
182                         << abs_view_pos[1] << ","
183                         << abs_view_pos[2] );
184                 SG_LOG( SG_TERRAIN, SG_DEBUG,
185                         "    ref point = " << e->center );
186
187                 sgdVec3 center;
188                 sgdSetVec3( center,
189                             e->center.x(), e->center.y(), e->center.z() );
190                 dist = sgdDistanceVec3( center, abs_view_pos );
191
192                 SG_LOG( SG_TERRAIN, SG_DEBUG, "    distance = " << dist );
193
194                 if ( dist > max_dist ) {
195                     max_dist = dist;
196                     max_index = index;
197                 }
198             }
199         }
200
201         // If we made it this far, then there were no open cache entries.
202         // We will instead free the furthest cache entry and return it's
203         // index.
204
205         if ( max_index >= 0 ) {
206             SG_LOG( SG_TERRAIN, SG_DEBUG, "    max_dist = " << max_dist );
207             SG_LOG( SG_TERRAIN, SG_DEBUG, "    index = " << max_index );
208             entry_free( max_index );
209         } else {
210             SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Dying in next_avail()" );
211             exit( -1 );
212         }
213     }
214 }
215
216
217 /**
218  * Create a new tile and schedule it for loading.
219  */
220 void
221 FGNewCache::insert_tile( FGTileEntry *e )
222 {
223     // clear out a distant entry in the cache if needed.
224     make_space();
225
226     // register it in the cache
227     long tile_index = e->get_tile_bucket().gen_index();
228     tile_cache[tile_index] = e;
229
230 }