vistablemgr.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/wwphys/vistablemgr.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Patrick $*
  29. * *
  30. * $Modtime:: 4/20/01 10:00a $*
  31. * *
  32. * $Revision:: 24 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "vistablemgr.h"
  38. #include "vistable.h"
  39. #include "chunkio.h"
  40. #include "wwmemlog.h"
  41. const int VIS_LRU_FRAMES = 5;
  42. /**
  43. ** VisDecompressionCacheClass
  44. ** This class encapsulates an LRU cache for decompressed vis tables. This was needed because
  45. ** light-sources now use VIS data and didn't want to pay the cpu cost of decompressing their
  46. ** vis table each time it is needed or the ram cost of keeping them all decompressed all of the
  47. ** time.
  48. */
  49. class VisDecompressionCacheClass
  50. {
  51. public:
  52. VisDecompressionCacheClass(void) { }
  53. ~VisDecompressionCacheClass(void) { Reset(0); }
  54. void Reset(int vis_sector_count = -1);
  55. VisTableClass * Get_Table(int vis_sector_id);
  56. void Add_Table(VisTableClass * pvs);
  57. void Release_Old_Tables(void);
  58. void Set_Current_Timestamp(int timestamp) { CurrentTimestamp = timestamp; }
  59. int Get_Current_Timestamp(void) { return CurrentTimestamp; }
  60. protected:
  61. SimpleVecClass<VisTableClass *> Cache;
  62. MultiListClass<VisTableClass> LRUQueue;
  63. int CurrentTimestamp;
  64. };
  65. void VisDecompressionCacheClass::Reset(int vis_sector_count)
  66. {
  67. /*
  68. ** Each table that we have should be in our LRU list
  69. */
  70. VisTableClass * pvs = LRUQueue.Peek_Head();
  71. while (pvs != NULL) {
  72. WWASSERT(Cache[pvs->Get_Vis_Sector_ID()] != NULL);
  73. LRUQueue.Remove_Head();
  74. int vis_id = pvs->Get_Vis_Sector_ID();
  75. REF_PTR_RELEASE(Cache[vis_id]);
  76. pvs = LRUQueue.Peek_Head();
  77. }
  78. /*
  79. ** Sanity check, every pointer in the cache array should now be NULL!
  80. */
  81. #ifdef WWDEBUG
  82. for (int i=0; i<Cache.Length(); i++) {
  83. WWASSERT(Cache[i] == NULL);
  84. }
  85. #endif
  86. /*
  87. ** Resize the pointer array to match the vis sector count if needed
  88. */
  89. if (vis_sector_count != -1) {
  90. Cache.Resize(vis_sector_count);
  91. Cache.Zero_Memory();
  92. }
  93. }
  94. VisTableClass *
  95. VisDecompressionCacheClass::Get_Table(int vis_sector_id)
  96. {
  97. if (Cache[vis_sector_id] != NULL) {
  98. /*
  99. ** Move the table to the end of the LRU Queue and
  100. ** update its timestamp
  101. */
  102. LRUQueue.Remove(Cache[vis_sector_id]);
  103. Cache[vis_sector_id]->Set_Time_Stamp(CurrentTimestamp);
  104. LRUQueue.Add_Tail(Cache[vis_sector_id]);
  105. /*
  106. ** Add a ref for the user and return the table
  107. */
  108. Cache[vis_sector_id]->Add_Ref();
  109. return Cache[vis_sector_id];
  110. } else {
  111. return NULL;
  112. }
  113. }
  114. void
  115. VisDecompressionCacheClass::Add_Table(VisTableClass * pvs)
  116. {
  117. WWASSERT(Cache[pvs->Get_Vis_Sector_ID()] == NULL);
  118. WWASSERT(pvs != NULL);
  119. /*
  120. ** Set the timestamp, add to the cache, and add to
  121. ** the tail of the LRUQueue
  122. */
  123. pvs->Set_Time_Stamp(CurrentTimestamp);
  124. REF_PTR_SET(Cache[pvs->Get_Vis_Sector_ID()],pvs);
  125. LRUQueue.Add_Tail(pvs);
  126. }
  127. void
  128. VisDecompressionCacheClass::Release_Old_Tables(void)
  129. {
  130. /*
  131. ** Release any vis table that hasn't been used in X frames
  132. */
  133. int timestamp_cutoff = CurrentTimestamp - VIS_LRU_FRAMES;
  134. VisTableClass * tbl = LRUQueue.Peek_Head();
  135. while (tbl && tbl->Get_Time_Stamp() < timestamp_cutoff) {
  136. int vis_id = tbl->Get_Vis_Sector_ID();
  137. REF_PTR_RELEASE(Cache[vis_id]);
  138. LRUQueue.Remove_Head();
  139. tbl = LRUQueue.Peek_Head();
  140. }
  141. }
  142. /*******************************************************************************************
  143. **
  144. ** VisTableManagerClass Implemenation
  145. **
  146. *******************************************************************************************/
  147. /*
  148. ** Save/Load constants
  149. */
  150. #define VISMGR_CURRENT_VERSION 0x00010001
  151. enum
  152. {
  153. VISMGR_CHUNK_VARIABLES = 0x34500000,
  154. VISMGR_CHUNK_VISTABLEID = 0x34500001,
  155. VISMGR_CHUNK_VISTABLEDATA = 0x34500002,
  156. VISMGR_VARIABLE_VERSION = 0x00,
  157. VISMGR_VARIABLE_IDCOUNT, // OBSOLETE
  158. VISMGR_VARIABLE_VISOBJECTCOUNT,
  159. VISMGR_VARIABLE_VISSECTORCOUNT,
  160. };
  161. VisTableMgrClass::VisTableMgrClass(void) :
  162. VisSectorCount(0),
  163. VisObjectCount(0),
  164. FrameCounter(0)
  165. {
  166. WWMEMLOG(MEM_VIS);
  167. Cache = new VisDecompressionCacheClass;
  168. Reset();
  169. }
  170. VisTableMgrClass::~VisTableMgrClass(void)
  171. {
  172. Reset ();
  173. delete Cache;
  174. }
  175. void VisTableMgrClass::Reset(void)
  176. {
  177. /*
  178. ** delete all allocated tables
  179. */
  180. Delete_All_Vis_Tables();
  181. /*
  182. ** reset the vector of pointers
  183. */
  184. VisTables.Delete_All();
  185. /*
  186. ** reset the id counters
  187. */
  188. VisObjectCount = 1; // always reserve Object ID 0 for objects that are forced visibile
  189. VisSectorCount = 0;
  190. FrameCounter = 0;
  191. /*
  192. ** reset the decompression cache
  193. */
  194. Cache->Reset(0);
  195. }
  196. int VisTableMgrClass::Allocate_Vis_Object_ID(int count /*= 1*/)
  197. {
  198. int id = VisObjectCount;
  199. VisObjectCount += count;
  200. return id;
  201. }
  202. void VisTableMgrClass::Set_Optimized_Vis_Object_Count(int count)
  203. {
  204. /*
  205. ** After the optimization process, we need to simply force the VisObjectCount
  206. */
  207. VisObjectCount = count;
  208. }
  209. int VisTableMgrClass::Allocate_Vis_Sector_ID(int count /*= 1*/)
  210. {
  211. int id = VisSectorCount;
  212. for (int i=0; i<count; i++) {
  213. VisTables.Add(NULL);
  214. }
  215. VisSectorCount += count;
  216. WWASSERT(VisSectorCount == VisTables.Count());
  217. Cache->Reset(VisSectorCount);
  218. return id;
  219. }
  220. int VisTableMgrClass::Get_Vis_Table_Size(void) const
  221. {
  222. return VisObjectCount;
  223. }
  224. int VisTableMgrClass::Get_Vis_Table_Count(void) const
  225. {
  226. return VisSectorCount;
  227. }
  228. VisTableClass * VisTableMgrClass::Get_Vis_Table(int id,bool allocate/*= false*/)
  229. {
  230. WWMEMLOG(MEM_VIS);
  231. /*
  232. ** if id is invalid, return NULL
  233. */
  234. if ((id == -1) || (id >= VisTables.Count())) {
  235. #ifdef WWDEBUG
  236. if (id > 0) WWDEBUG_SAY(("Vis Table ID out of range: %d\r\n",id));
  237. #endif
  238. return NULL;
  239. }
  240. /*
  241. ** - if the cache has the decompressed table just return a pointer
  242. ** - if we have the compressed version of this vis table, decompress it and return it
  243. ** - else if they want one allocated, just create a table and return it
  244. ** (we will install the compressed version when they release this table)
  245. ** - else return NULL
  246. */
  247. VisTableClass * pvs = Cache->Get_Table(id);
  248. if (pvs != NULL) {
  249. /*
  250. ** Cache had the table, just return the pointer. (Add-Ref'd by the cache...)
  251. */
  252. return pvs;
  253. } else if (VisTables[id] != NULL) {
  254. /*
  255. ** Cache didn't have it, but we have the compressed version.
  256. ** Decompress, add to the cache, and return the table.
  257. */
  258. pvs = NEW_REF(VisTableClass,(VisTables[id],Get_Vis_Table_Size(),id));
  259. Cache->Add_Table(pvs);
  260. return pvs;
  261. } else if (allocate) {
  262. /*
  263. ** This table doesn't exist yet but the user wants to allocate
  264. ** it. Create a new vis table for him and return it
  265. */
  266. VisTableClass * new_table = NEW_REF(VisTableClass,(Get_Vis_Table_Size(),id));
  267. new_table->Set_Bit(0,true); // Always set ID 0 to be visible...
  268. return new_table;
  269. } else {
  270. /*
  271. ** Table doesn't exist, just return NULL
  272. */
  273. return NULL;
  274. }
  275. }
  276. void VisTableMgrClass::Update_Vis_Table(int id,VisTableClass * pvs)
  277. {
  278. WWMEMLOG(MEM_VIS);
  279. WWASSERT(pvs->Get_Bit_Count() == VisObjectCount);
  280. /*
  281. ** release any existing vis table
  282. */
  283. if (VisTables[id] != NULL) {
  284. delete VisTables[id];
  285. VisTables[id] = NULL;
  286. }
  287. /*
  288. ** compress this one
  289. */
  290. VisTables[id] = new CompressedVisTableClass(pvs);
  291. /*
  292. ** Reset the decompression cache
  293. */
  294. Cache->Reset();
  295. }
  296. bool VisTableMgrClass::Has_Vis_Table(int id)
  297. {
  298. return (VisTables[id] != NULL);
  299. }
  300. void VisTableMgrClass::Notify_Frame_Ended(void)
  301. {
  302. FrameCounter++;
  303. Cache->Set_Current_Timestamp(FrameCounter);
  304. Cache->Release_Old_Tables();
  305. }
  306. void VisTableMgrClass::Delete_All_Vis_Tables(void)
  307. {
  308. // delete all allocated tables
  309. for (int i=0; i<VisTables.Count(); i++) {
  310. if (VisTables[i] != NULL) {
  311. delete VisTables[i];
  312. VisTables[i] = NULL;
  313. }
  314. }
  315. }
  316. void VisTableMgrClass::Save(ChunkSaveClass & csave)
  317. {
  318. WWMEMLOG(MEM_VIS);
  319. uint32 version = VISMGR_CURRENT_VERSION;
  320. csave.Begin_Chunk(VISMGR_CHUNK_VARIABLES);
  321. WRITE_MICRO_CHUNK(csave,VISMGR_VARIABLE_VERSION,version);
  322. WRITE_MICRO_CHUNK(csave,VISMGR_VARIABLE_VISOBJECTCOUNT,VisObjectCount);
  323. WRITE_MICRO_CHUNK(csave,VISMGR_VARIABLE_VISSECTORCOUNT,VisSectorCount);
  324. csave.End_Chunk();
  325. // loop over tables
  326. for (int i=0; i<VisTables.Count(); i++) {
  327. if (VisTables[i] != NULL) {
  328. csave.Begin_Chunk(VISMGR_CHUNK_VISTABLEID);
  329. uint32 index = i;
  330. csave.Write(&index,sizeof(index));
  331. csave.End_Chunk();
  332. csave.Begin_Chunk(VISMGR_CHUNK_VISTABLEDATA);
  333. VisTables[i]->Save(csave);
  334. csave.End_Chunk();
  335. }
  336. }
  337. }
  338. void VisTableMgrClass::Load(ChunkLoadClass & cload)
  339. {
  340. WWMEMLOG(MEM_VIS);
  341. // read in the variables
  342. uint32 version = 0;
  343. cload.Open_Chunk();
  344. WWASSERT(cload.Cur_Chunk_ID() == VISMGR_CHUNK_VARIABLES);
  345. while (cload.Open_Micro_Chunk()) {
  346. switch(cload.Cur_Micro_Chunk_ID()) {
  347. READ_MICRO_CHUNK(cload,VISMGR_VARIABLE_VERSION,version);
  348. READ_MICRO_CHUNK(cload,VISMGR_VARIABLE_VISOBJECTCOUNT,VisObjectCount);
  349. READ_MICRO_CHUNK(cload,VISMGR_VARIABLE_VISSECTORCOUNT,VisSectorCount);
  350. }
  351. cload.Close_Micro_Chunk();
  352. }
  353. cload.Close_Chunk();
  354. if (version < 0x00010001) {
  355. WWDEBUG_SAY(("Obsolete Vis-Data detected, Reseting Visiblity System!\r\n"));
  356. Reset();
  357. return;
  358. }
  359. // allocate a pointer for each table
  360. for (int i=0; i<VisSectorCount; i++) {
  361. VisTables.Add(NULL,VisSectorCount);
  362. }
  363. // reset the cache
  364. Cache->Reset(VisSectorCount);
  365. // read the actual vis tables
  366. uint32 id = 0xFFFFFFFF;
  367. while (cload.Open_Chunk()) {
  368. switch (cload.Cur_Chunk_ID()) {
  369. case VISMGR_CHUNK_VISTABLEID:
  370. cload.Read(&id,sizeof(id));
  371. break;
  372. case VISMGR_CHUNK_VISTABLEDATA:
  373. WWASSERT(id != 0xFFFFFFFF);
  374. if (VisTables[id] == NULL) {
  375. VisTables[id] = new CompressedVisTableClass;
  376. }
  377. VisTables[id]->Load(cload);
  378. id = 0xFFFFFFFF;
  379. break;
  380. }
  381. cload.Close_Chunk();
  382. }
  383. }