dx8indexbuffer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. ** Command & Conquer Generals(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 : ww3d *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/dx8indexbuffer.cpp $*
  25. * *
  26. * Original Author:: Jani Penttinen *
  27. * *
  28. * $Author:: Jani_p $*
  29. * *
  30. * $Modtime:: 7/10/01 1:30p $*
  31. * *
  32. * $Revision:: 22 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. //#define INDEX_BUFFER_LOG
  38. #include "dx8indexbuffer.h"
  39. #include "dx8wrapper.h"
  40. #include "dx8caps.h"
  41. #include "sphere.h"
  42. #include "thread.h"
  43. #define DEFAULT_IB_SIZE 5000
  44. static bool _DynamicSortingIndexArrayInUse=false;
  45. static SortingIndexBufferClass* _DynamicSortingIndexArray;
  46. static unsigned short _DynamicSortingIndexArraySize=0;
  47. static unsigned short _DynamicSortingIndexArrayOffset=0;
  48. static bool _DynamicDX8IndexBufferInUse=false;
  49. static DX8IndexBufferClass* _DynamicDX8IndexBuffer=NULL;
  50. static unsigned short _DynamicDX8IndexBufferSize=DEFAULT_IB_SIZE;
  51. static unsigned short _DynamicDX8IndexBufferOffset=0;
  52. static int _IndexBufferCount;
  53. static int _IndexBufferTotalIndices;
  54. static int _IndexBufferTotalSize;
  55. // ----------------------------------------------------------------------------
  56. //
  57. //
  58. //
  59. // ----------------------------------------------------------------------------
  60. IndexBufferClass::IndexBufferClass(unsigned type_, unsigned short index_count_)
  61. :
  62. index_count(index_count_),
  63. type(type_),
  64. engine_refs(0)
  65. {
  66. WWASSERT(type==BUFFER_TYPE_DX8 || type==BUFFER_TYPE_SORTING);
  67. WWASSERT(index_count);
  68. _IndexBufferCount++;
  69. _IndexBufferTotalIndices+=index_count;
  70. _IndexBufferTotalSize+=index_count*sizeof(unsigned short);
  71. #ifdef VERTEX_BUFFER_LOG
  72. WWDEBUG_SAY(("New IB, %d indices, size %d bytes\n",index_count,index_count*sizeof(unsigned short)));
  73. WWDEBUG_SAY(("Total IB count: %d, total %d indices, total size %d bytes\n",
  74. _IndexBufferCount,
  75. _IndexBufferTotalIndices,
  76. _IndexBufferTotalSize));
  77. #endif
  78. }
  79. IndexBufferClass::~IndexBufferClass()
  80. {
  81. _IndexBufferCount--;
  82. _IndexBufferTotalIndices-=index_count;
  83. _IndexBufferTotalSize-=index_count*sizeof(unsigned short);
  84. #ifdef VERTEX_BUFFER_LOG
  85. WWDEBUG_SAY(("Delete IB, %d indices, size %d bytes\n",index_count,index_count*sizeof(unsigned short)));
  86. WWDEBUG_SAY(("Total IB count: %d, total %d indices, total size %d bytes\n",
  87. _IndexBufferCount,
  88. _IndexBufferTotalIndices,
  89. _IndexBufferTotalSize));
  90. #endif
  91. }
  92. unsigned IndexBufferClass::Get_Total_Buffer_Count()
  93. {
  94. return _IndexBufferCount;
  95. }
  96. unsigned IndexBufferClass::Get_Total_Allocated_Indices()
  97. {
  98. return _IndexBufferTotalIndices;
  99. }
  100. unsigned IndexBufferClass::Get_Total_Allocated_Memory()
  101. {
  102. return _IndexBufferTotalSize;
  103. }
  104. void IndexBufferClass::Add_Engine_Ref() const
  105. {
  106. engine_refs++;
  107. }
  108. void IndexBufferClass::Release_Engine_Ref() const
  109. {
  110. engine_refs--;
  111. WWASSERT(engine_refs>=0);
  112. }
  113. // ----------------------------------------------------------------------------
  114. //
  115. //
  116. //
  117. // ----------------------------------------------------------------------------
  118. void IndexBufferClass::Copy(unsigned int* indices,unsigned first_index,unsigned count)
  119. {
  120. WWASSERT(indices);
  121. if (first_index) {
  122. DX8IndexBufferClass::AppendLockClass l(this,first_index,count);
  123. unsigned short* inds=l.Get_Index_Array();
  124. for (unsigned v=0;v<count;++v) {
  125. *inds++=unsigned short(*indices++);
  126. }
  127. }
  128. else {
  129. DX8IndexBufferClass::WriteLockClass l(this);
  130. unsigned short* inds=l.Get_Index_Array();
  131. for (unsigned v=0;v<count;++v) {
  132. *inds++=unsigned short(*indices++);
  133. }
  134. }
  135. }
  136. // ----------------------------------------------------------------------------
  137. void IndexBufferClass::Copy(unsigned short* indices,unsigned first_index,unsigned count)
  138. {
  139. WWASSERT(indices);
  140. if (first_index) {
  141. DX8IndexBufferClass::AppendLockClass l(this,first_index,count);
  142. unsigned short* inds=l.Get_Index_Array();
  143. for (unsigned v=0;v<count;++v) {
  144. *inds++=*indices++;
  145. }
  146. }
  147. else {
  148. DX8IndexBufferClass::WriteLockClass l(this);
  149. unsigned short* inds=l.Get_Index_Array();
  150. for (unsigned v=0;v<count;++v) {
  151. *inds++=*indices++;
  152. }
  153. }
  154. }
  155. // ----------------------------------------------------------------------------
  156. //
  157. //
  158. // ----------------------------------------------------------------------------
  159. IndexBufferClass::WriteLockClass::WriteLockClass(IndexBufferClass* index_buffer_) : index_buffer(index_buffer_)
  160. {
  161. DX8_THREAD_ASSERT();
  162. WWASSERT(index_buffer);
  163. WWASSERT(!index_buffer->Engine_Refs());
  164. index_buffer->Add_Ref();
  165. switch (index_buffer->Type()) {
  166. case BUFFER_TYPE_DX8:
  167. DX8_Assert();
  168. DX8_ErrorCode(static_cast<DX8IndexBufferClass*>(index_buffer)->Get_DX8_Index_Buffer()->Lock(
  169. 0,
  170. index_buffer->Get_Index_Count()*sizeof(WORD),
  171. (unsigned char**)&indices,
  172. 0));
  173. break;
  174. case BUFFER_TYPE_SORTING:
  175. indices=static_cast<SortingIndexBufferClass*>(index_buffer)->index_buffer;
  176. break;
  177. default:
  178. WWASSERT(0);
  179. break;
  180. }
  181. }
  182. // ----------------------------------------------------------------------------
  183. //
  184. //
  185. // ----------------------------------------------------------------------------
  186. IndexBufferClass::WriteLockClass::~WriteLockClass()
  187. {
  188. DX8_THREAD_ASSERT();
  189. switch (index_buffer->Type()) {
  190. case BUFFER_TYPE_DX8:
  191. DX8_Assert();
  192. DX8_ErrorCode(static_cast<DX8IndexBufferClass*>(index_buffer)->index_buffer->Unlock());
  193. break;
  194. case BUFFER_TYPE_SORTING:
  195. break;
  196. default:
  197. WWASSERT(0);
  198. break;
  199. }
  200. index_buffer->Release_Ref();
  201. }
  202. // ----------------------------------------------------------------------------
  203. IndexBufferClass::AppendLockClass::AppendLockClass(IndexBufferClass* index_buffer_,unsigned start_index, unsigned index_range)
  204. :
  205. index_buffer(index_buffer_)
  206. {
  207. DX8_THREAD_ASSERT();
  208. WWASSERT(start_index+index_range<=index_buffer->Get_Index_Count());
  209. WWASSERT(index_buffer);
  210. WWASSERT(!index_buffer->Engine_Refs());
  211. index_buffer->Add_Ref();
  212. switch (index_buffer->Type()) {
  213. case BUFFER_TYPE_DX8:
  214. DX8_Assert();
  215. DX8_ErrorCode(static_cast<DX8IndexBufferClass*>(index_buffer)->index_buffer->Lock(
  216. start_index*sizeof(unsigned short),
  217. index_range*sizeof(unsigned short),
  218. (unsigned char**)&indices,
  219. NULL)); // Optional pointer to receive the buffer size
  220. break;
  221. case BUFFER_TYPE_SORTING:
  222. indices=static_cast<SortingIndexBufferClass*>(index_buffer)->index_buffer+start_index;
  223. break;
  224. default:
  225. WWASSERT(0);
  226. break;
  227. }
  228. }
  229. // ----------------------------------------------------------------------------
  230. IndexBufferClass::AppendLockClass::~AppendLockClass()
  231. {
  232. DX8_THREAD_ASSERT();
  233. switch (index_buffer->Type()) {
  234. case BUFFER_TYPE_DX8:
  235. DX8_Assert();
  236. DX8_ErrorCode(static_cast<DX8IndexBufferClass*>(index_buffer)->index_buffer->Unlock());
  237. break;
  238. case BUFFER_TYPE_SORTING:
  239. break;
  240. default:
  241. WWASSERT(0);
  242. break;
  243. }
  244. index_buffer->Release_Ref();
  245. }
  246. // ----------------------------------------------------------------------------
  247. //
  248. //
  249. //
  250. // ----------------------------------------------------------------------------
  251. DX8IndexBufferClass::DX8IndexBufferClass(unsigned short index_count_,UsageType usage)
  252. :
  253. IndexBufferClass(BUFFER_TYPE_DX8,index_count_)
  254. {
  255. DX8_THREAD_ASSERT();
  256. WWASSERT(index_count);
  257. unsigned usage_flags=
  258. D3DUSAGE_WRITEONLY|
  259. ((usage&USAGE_DYNAMIC) ? D3DUSAGE_DYNAMIC : 0)|
  260. ((usage&USAGE_NPATCHES) ? D3DUSAGE_NPATCHES : 0)|
  261. ((usage&USAGE_SOFTWAREPROCESSING) ? D3DUSAGE_SOFTWAREPROCESSING : 0);
  262. DX8CALL(CreateIndexBuffer(
  263. sizeof(WORD)*index_count,
  264. usage_flags,
  265. D3DFMT_INDEX16,
  266. (usage&USAGE_DYNAMIC) ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED,
  267. &index_buffer));
  268. }
  269. // ----------------------------------------------------------------------------
  270. DX8IndexBufferClass::~DX8IndexBufferClass()
  271. {
  272. index_buffer->Release();
  273. }
  274. // ----------------------------------------------------------------------------
  275. //
  276. //
  277. //
  278. // ----------------------------------------------------------------------------
  279. SortingIndexBufferClass::SortingIndexBufferClass(unsigned short index_count_)
  280. :
  281. IndexBufferClass(BUFFER_TYPE_SORTING,index_count_)
  282. {
  283. WWASSERT(index_count);
  284. index_buffer=W3DNEWARRAY unsigned short[index_count];
  285. }
  286. // ----------------------------------------------------------------------------
  287. SortingIndexBufferClass::~SortingIndexBufferClass()
  288. {
  289. delete[] index_buffer;
  290. }
  291. // ----------------------------------------------------------------------------
  292. //
  293. //
  294. //
  295. // ----------------------------------------------------------------------------
  296. DynamicIBAccessClass::DynamicIBAccessClass(unsigned short type_, unsigned short index_count_)
  297. :
  298. IndexCount(index_count_),
  299. IndexBuffer(0),
  300. Type(type_)
  301. {
  302. WWASSERT(Type==BUFFER_TYPE_DYNAMIC_DX8 || Type==BUFFER_TYPE_DYNAMIC_SORTING);
  303. if (Type==BUFFER_TYPE_DYNAMIC_DX8) {
  304. Allocate_DX8_Dynamic_Buffer();
  305. }
  306. else {
  307. Allocate_Sorting_Dynamic_Buffer();
  308. }
  309. }
  310. DynamicIBAccessClass::~DynamicIBAccessClass()
  311. {
  312. REF_PTR_RELEASE(IndexBuffer);
  313. if (Type==BUFFER_TYPE_DYNAMIC_DX8) {
  314. _DynamicDX8IndexBufferInUse=false;
  315. _DynamicDX8IndexBufferOffset+=IndexCount;
  316. }
  317. else {
  318. _DynamicSortingIndexArrayInUse=false;
  319. _DynamicSortingIndexArrayOffset+=IndexCount;
  320. }
  321. }
  322. void DynamicIBAccessClass::_Deinit()
  323. {
  324. WWASSERT ((_DynamicDX8IndexBuffer == NULL) || (_DynamicDX8IndexBuffer->Num_Refs() == 1));
  325. REF_PTR_RELEASE(_DynamicDX8IndexBuffer);
  326. _DynamicDX8IndexBufferInUse=false;
  327. _DynamicDX8IndexBufferSize=DEFAULT_IB_SIZE;
  328. _DynamicDX8IndexBufferOffset=0;
  329. WWASSERT ((_DynamicSortingIndexArray == NULL) || (_DynamicSortingIndexArray->Num_Refs() == 1));
  330. REF_PTR_RELEASE(_DynamicSortingIndexArray);
  331. _DynamicSortingIndexArrayInUse=false;
  332. _DynamicSortingIndexArraySize=0;
  333. _DynamicSortingIndexArrayOffset=0;
  334. }
  335. // ----------------------------------------------------------------------------
  336. //
  337. //
  338. //
  339. // ----------------------------------------------------------------------------
  340. DynamicIBAccessClass::WriteLockClass::WriteLockClass(DynamicIBAccessClass* ib_access_)
  341. :
  342. DynamicIBAccess(ib_access_)
  343. {
  344. DX8_THREAD_ASSERT();
  345. DynamicIBAccess->IndexBuffer->Add_Ref();
  346. switch (DynamicIBAccess->Get_Type()) {
  347. case BUFFER_TYPE_DYNAMIC_DX8:
  348. WWASSERT(DynamicIBAccess);
  349. // WWASSERT(!dynamic_dx8_index_buffer->Engine_Refs());
  350. DX8_Assert();
  351. DX8_ErrorCode(
  352. static_cast<DX8IndexBufferClass*>(DynamicIBAccess->IndexBuffer)->Get_DX8_Index_Buffer()->Lock(
  353. DynamicIBAccess->IndexBufferOffset*sizeof(WORD),
  354. DynamicIBAccess->Get_Index_Count()*sizeof(WORD),
  355. (unsigned char**)&Indices,
  356. !DynamicIBAccess->IndexBufferOffset ? D3DLOCK_DISCARD : D3DLOCK_NOOVERWRITE));
  357. break;
  358. case BUFFER_TYPE_DYNAMIC_SORTING:
  359. Indices=static_cast<SortingIndexBufferClass*>(DynamicIBAccess->IndexBuffer)->index_buffer;
  360. Indices+=DynamicIBAccess->IndexBufferOffset;
  361. break;
  362. default:
  363. WWASSERT(0);
  364. break;
  365. }
  366. }
  367. DynamicIBAccessClass::WriteLockClass::~WriteLockClass()
  368. {
  369. DX8_THREAD_ASSERT();
  370. switch (DynamicIBAccess->Get_Type()) {
  371. case BUFFER_TYPE_DYNAMIC_DX8:
  372. DX8_Assert();
  373. DX8_ErrorCode(static_cast<DX8IndexBufferClass*>(DynamicIBAccess->IndexBuffer)->Get_DX8_Index_Buffer()->Unlock());
  374. break;
  375. case BUFFER_TYPE_DYNAMIC_SORTING:
  376. break;
  377. default:
  378. WWASSERT(0);
  379. break;
  380. }
  381. DynamicIBAccess->IndexBuffer->Release_Ref();
  382. }
  383. // ----------------------------------------------------------------------------
  384. //
  385. //
  386. //
  387. // ----------------------------------------------------------------------------
  388. void DynamicIBAccessClass::Allocate_DX8_Dynamic_Buffer()
  389. {
  390. WWASSERT(!_DynamicDX8IndexBufferInUse);
  391. _DynamicDX8IndexBufferInUse=true;
  392. // If requesting more indices than dynamic index buffer can fit, delete the ib
  393. // and adjust the size to the new count.
  394. if (IndexCount>_DynamicDX8IndexBufferSize) {
  395. REF_PTR_RELEASE(_DynamicDX8IndexBuffer);
  396. _DynamicDX8IndexBufferSize=IndexCount;
  397. if (_DynamicDX8IndexBufferSize<DEFAULT_IB_SIZE) _DynamicDX8IndexBufferSize=DEFAULT_IB_SIZE;
  398. }
  399. // Create a new vb if one doesn't exist currently
  400. if (!_DynamicDX8IndexBuffer) {
  401. unsigned usage=DX8IndexBufferClass::USAGE_DYNAMIC;
  402. if (DX8Caps::Support_NPatches()) {
  403. usage|=DX8IndexBufferClass::USAGE_NPATCHES;
  404. }
  405. _DynamicDX8IndexBuffer=NEW_REF(DX8IndexBufferClass,(
  406. _DynamicDX8IndexBufferSize,
  407. (DX8IndexBufferClass::UsageType)usage));
  408. _DynamicDX8IndexBufferOffset=0;
  409. }
  410. // Any room at the end of the buffer?
  411. if (((unsigned)IndexCount+_DynamicDX8IndexBufferOffset)>_DynamicDX8IndexBufferSize) {
  412. _DynamicDX8IndexBufferOffset=0;
  413. }
  414. REF_PTR_SET(IndexBuffer,_DynamicDX8IndexBuffer);
  415. IndexBufferOffset=_DynamicDX8IndexBufferOffset;
  416. }
  417. void DynamicIBAccessClass::Allocate_Sorting_Dynamic_Buffer()
  418. {
  419. WWASSERT(!_DynamicSortingIndexArrayInUse);
  420. _DynamicSortingIndexArrayInUse=true;
  421. unsigned new_index_count=_DynamicSortingIndexArrayOffset+IndexCount;
  422. WWASSERT(new_index_count<65536);
  423. if (new_index_count>_DynamicSortingIndexArraySize) {
  424. REF_PTR_RELEASE(_DynamicSortingIndexArray);
  425. _DynamicSortingIndexArraySize=new_index_count;
  426. if (_DynamicSortingIndexArraySize<DEFAULT_IB_SIZE) _DynamicSortingIndexArraySize=DEFAULT_IB_SIZE;
  427. }
  428. if (!_DynamicSortingIndexArray) {
  429. _DynamicSortingIndexArray=NEW_REF(SortingIndexBufferClass,(_DynamicSortingIndexArraySize));
  430. _DynamicSortingIndexArrayOffset=0;
  431. }
  432. REF_PTR_SET(IndexBuffer,_DynamicSortingIndexArray);
  433. IndexBufferOffset=_DynamicSortingIndexArrayOffset;
  434. }
  435. void DynamicIBAccessClass::_Reset(bool frame_changed)
  436. {
  437. _DynamicSortingIndexArrayOffset=0;
  438. if (frame_changed) _DynamicDX8IndexBufferOffset=0;
  439. }
  440. unsigned short DynamicIBAccessClass::Get_Default_Index_Count(void)
  441. {
  442. return _DynamicDX8IndexBufferSize;
  443. }