sparse.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. #pragma warning(disable:4018) //amckern - 64bit - '<' Singed/Unsigned Mismatch
  2. #include "qrad.h"
  3. typedef struct
  4. {
  5. unsigned offset:24;
  6. unsigned values:8;
  7. }
  8. sparse_row_t;
  9. typedef struct
  10. {
  11. sparse_row_t* row;
  12. int count;
  13. }
  14. sparse_column_t;
  15. sparse_column_t* s_vismatrix;
  16. // Vismatrix protected
  17. #ifdef HLRAD_HULLU
  18. static int IsVisbitInArray(const unsigned x, const unsigned y)
  19. #else
  20. static unsigned IsVisbitInArray(const unsigned x, const unsigned y)
  21. #endif
  22. {
  23. int first, last, current;
  24. int y_byte = y / 8;
  25. sparse_row_t* row;
  26. sparse_column_t* column = s_vismatrix + x;
  27. if (!column->count)
  28. {
  29. return -1;
  30. }
  31. first = 0;
  32. last = column->count - 1;
  33. // Warning("Searching . . .");
  34. // binary search to find visbit
  35. while (1)
  36. {
  37. current = (first + last) / 2;
  38. row = column->row + current;
  39. // Warning("first %u, last %u, current %u, row %p, row->offset %u", first, last, current, row, row->offset);
  40. if ((row->offset) < y_byte)
  41. {
  42. first = current + 1;
  43. }
  44. else if ((row->offset) > y_byte)
  45. {
  46. last = current - 1;
  47. }
  48. else
  49. {
  50. return current;
  51. }
  52. if (first > last)
  53. {
  54. return -1;
  55. }
  56. }
  57. }
  58. // Vismatrix protected
  59. static void InsertVisbitIntoArray(const unsigned x, const unsigned y)
  60. {
  61. unsigned count;
  62. unsigned y_byte = y / 8;
  63. sparse_column_t* column = s_vismatrix + x;
  64. sparse_row_t* row = column->row;
  65. if (!column->count)
  66. {
  67. column->count++;
  68. row = column->row = (sparse_row_t*)malloc(sizeof(sparse_row_t));
  69. row->offset = y_byte;
  70. row->values = 1 << (y & 7);
  71. return;
  72. }
  73. // Insertion
  74. count = 0;
  75. while (count < column->count)
  76. {
  77. if (row->offset > y_byte)
  78. {
  79. unsigned newsize = (column->count + 1) * sizeof(sparse_row_t);
  80. sparse_row_t* newrow = (sparse_row_t*)malloc(newsize);
  81. memcpy(newrow, column->row, count * sizeof(sparse_row_t));
  82. memcpy(newrow + count + 1, column->row + count, (column->count - count) * sizeof(sparse_row_t));
  83. row = newrow + count;
  84. row->offset = y_byte;
  85. row->values = 1 << (y & 7);
  86. free(column->row);
  87. column->row = newrow;
  88. column->count++;
  89. return;
  90. }
  91. row++;
  92. count++;
  93. }
  94. // Append
  95. {
  96. unsigned newsize = (count + 1) * sizeof(sparse_row_t);
  97. sparse_row_t* newrow = (sparse_row_t*)malloc(newsize);
  98. memcpy(newrow, column->row, column->count * sizeof(sparse_row_t));
  99. row = newrow + column->count;
  100. row->offset = y_byte;
  101. row->values = 1 << (y & 7);
  102. free(column->row);
  103. column->row = newrow;
  104. column->count++;
  105. return;
  106. }
  107. }
  108. // Vismatrix public
  109. static void SetVisBit(unsigned x, unsigned y)
  110. {
  111. #ifdef HLRAD_HULLU
  112. int offset;
  113. #else
  114. unsigned offset;
  115. #endif
  116. if (x == y)
  117. {
  118. return;
  119. }
  120. if (x > y)
  121. {
  122. const unsigned a = x;
  123. const unsigned b = y;
  124. x = b;
  125. y = a;
  126. }
  127. if (x > g_num_patches)
  128. {
  129. Warning("in SetVisBit(), x > num_patches");
  130. }
  131. if (y > g_num_patches)
  132. {
  133. Warning("in SetVisBit(), y > num_patches");
  134. }
  135. ThreadLock();
  136. if ((offset = IsVisbitInArray(x, y)) != -1)
  137. {
  138. s_vismatrix[x].row[offset].values |= 1 << (y & 7);
  139. }
  140. else
  141. {
  142. InsertVisbitIntoArray(x, y);
  143. }
  144. ThreadUnlock();
  145. }
  146. // Vismatrix public
  147. #ifdef HLRAD_HULLU
  148. static bool CheckVisBitSparse(unsigned x, unsigned y, vec3_t &transparency_out, unsigned int &next_index)
  149. #else
  150. static bool CheckVisBitSparse(unsigned x, unsigned y)
  151. #endif
  152. {
  153. #ifdef HLRAD_HULLU
  154. int offset;
  155. #else
  156. unsigned offset;
  157. #endif
  158. #ifdef HLRAD_HULLU
  159. VectorFill(transparency_out, 1.0);
  160. #endif
  161. if (x == y)
  162. {
  163. return 1;
  164. }
  165. #ifdef HLRAD_HULLU
  166. const unsigned a = x;
  167. const unsigned b = y;
  168. #endif
  169. if (x > y)
  170. {
  171. #ifndef HLRAD_HULLU
  172. const unsigned a = x;
  173. const unsigned b = y;
  174. #endif
  175. x = b;
  176. y = a;
  177. }
  178. if (x > g_num_patches)
  179. {
  180. Warning("in CheckVisBit(), x > num_patches");
  181. }
  182. if (y > g_num_patches)
  183. {
  184. Warning("in CheckVisBit(), y > num_patches");
  185. }
  186. if ((offset = IsVisbitInArray(x, y)) != -1)
  187. {
  188. #ifdef HLRAD_HULLU
  189. if(g_customshadow_with_bouncelight)
  190. {
  191. GetTransparency(a, b, transparency_out, next_index);
  192. }
  193. #endif
  194. return s_vismatrix[x].row[offset].values & (1 << (y & 7));
  195. }
  196. return false;
  197. }
  198. /*
  199. * ==============
  200. * TestPatchToFace
  201. *
  202. * Sets vis bits for all patches in the face
  203. * ==============
  204. */
  205. static void TestPatchToFace(const unsigned patchnum, const int facenum, const int head)
  206. {
  207. patch_t* patch = &g_patches[patchnum];
  208. patch_t* patch2 = g_face_patches[facenum];
  209. #ifdef HLRAD_HULLU
  210. vec3_t transparency;
  211. #endif
  212. // if emitter is behind that face plane, skip all patches
  213. if (patch2)
  214. {
  215. const dplane_t* plane2 = getPlaneFromFaceNumber(facenum);
  216. if (DotProduct(patch->origin, plane2->normal) > (PatchPlaneDist(patch2) + MINIMUM_PATCH_DISTANCE))
  217. {
  218. // we need to do a real test
  219. const dplane_t* plane = getPlaneFromFaceNumber(patch->faceNumber);
  220. for (; patch2; patch2 = patch2->next)
  221. {
  222. unsigned m = patch2 - g_patches;
  223. // check vis between patch and patch2
  224. // if bit has not already been set
  225. // && v2 is not behind light plane
  226. // && v2 is visible from v1
  227. #ifdef HLRAD_HULLU
  228. //removed reset of transparency - TestSegmentAgainstOpaqueList already resets to 1,1,1
  229. int facenum = TestSegmentAgainstOpaqueList(patch->origin, patch2->origin, transparency);
  230. #else
  231. int facenum = TestSegmentAgainstOpaqueList(patch->origin, patch2->origin);
  232. #endif
  233. if (m > patchnum
  234. && (facenum < 0 || facenum == patch2->faceNumber)
  235. && (DotProduct(patch2->origin, plane->normal) > (PatchPlaneDist(patch) + MINIMUM_PATCH_DISTANCE))
  236. && (TestLine_r(0, patch->origin, patch2->origin) == CONTENTS_EMPTY))
  237. {
  238. #ifdef HLRAD_HULLU
  239. // transparency face fix table
  240. if(g_customshadow_with_bouncelight && !VectorCompare(transparency, vec3_one) )
  241. {
  242. AddTransparencyToRawArray(patchnum, m, transparency);
  243. }
  244. #endif
  245. SetVisBit(m, patchnum);
  246. }
  247. }
  248. }
  249. }
  250. }
  251. /*
  252. * ==============
  253. * BuildVisRow
  254. *
  255. * Calc vis bits from a single patch
  256. * ==============
  257. */
  258. static void BuildVisRow(const int patchnum, byte* pvs, const int head)
  259. {
  260. int j, k, l;
  261. byte face_tested[MAX_MAP_FACES];
  262. dleaf_t* leaf;
  263. memset(face_tested, 0, g_numfaces);
  264. // leaf 0 is the solid leaf (skipped)
  265. for (j = 1, leaf = g_dleafs + 1; j < g_numleafs; j++, leaf++)
  266. {
  267. if (!(pvs[(j - 1) >> 3] & (1 << ((j - 1) & 7))))
  268. {
  269. continue; // not in pvs
  270. }
  271. for (k = 0; k < leaf->nummarksurfaces; k++)
  272. {
  273. l = g_dmarksurfaces[leaf->firstmarksurface + k];
  274. // faces can be marksurfed by multiple leaves, but
  275. // don't bother testing again
  276. if (face_tested[l])
  277. continue;
  278. face_tested[l] = 1;
  279. TestPatchToFace(patchnum, l, head);
  280. }
  281. }
  282. }
  283. /*
  284. * ===========
  285. * BuildVisLeafs
  286. *
  287. * This is run by multiple threads
  288. * ===========
  289. */
  290. #ifdef SYSTEM_WIN32
  291. #pragma warning(push)
  292. #pragma warning(disable:4100) // unreferenced formal parameter
  293. #endif
  294. static void BuildVisLeafs(int threadnum)
  295. {
  296. int i;
  297. int lface, facenum, facenum2;
  298. byte pvs[(MAX_MAP_LEAFS + 7) / 8];
  299. dleaf_t* srcleaf;
  300. dleaf_t* leaf;
  301. patch_t* patch;
  302. int head;
  303. unsigned patchnum;
  304. while (1)
  305. {
  306. //
  307. // build a minimal BSP tree that only
  308. // covers areas relevent to the PVS
  309. //
  310. i = GetThreadWork();
  311. if (i == -1)
  312. {
  313. break;
  314. }
  315. i++; // skip leaf 0
  316. srcleaf = &g_dleafs[i];
  317. DecompressVis(&g_dvisdata[srcleaf->visofs], pvs, sizeof(pvs));
  318. head = 0;
  319. //
  320. // go through all the faces inside the
  321. // leaf, and process the patches that
  322. // actually have origins inside
  323. //
  324. for (lface = 0; lface < srcleaf->nummarksurfaces; lface++)
  325. {
  326. facenum = g_dmarksurfaces[srcleaf->firstmarksurface + lface];
  327. for (patch = g_face_patches[facenum]; patch; patch = patch->next)
  328. {
  329. leaf = PointInLeaf(patch->origin);
  330. if (leaf != srcleaf)
  331. {
  332. continue;
  333. }
  334. patchnum = patch - g_patches;
  335. // build to all other world leafs
  336. BuildVisRow(patchnum, pvs, head);
  337. // build to bmodel faces
  338. if (g_nummodels < 2)
  339. {
  340. continue;
  341. }
  342. for (facenum2 = g_dmodels[1].firstface; facenum2 < g_numfaces; facenum2++)
  343. {
  344. TestPatchToFace(patchnum, facenum2, head);
  345. }
  346. }
  347. }
  348. }
  349. }
  350. #ifdef SYSTEM_WIN32
  351. #pragma warning(pop)
  352. #endif
  353. /*
  354. * ==============
  355. * BuildVisMatrix
  356. * ==============
  357. */
  358. static void BuildVisMatrix()
  359. {
  360. s_vismatrix = (sparse_column_t*)AllocBlock(g_num_patches * sizeof(sparse_column_t));
  361. if (!s_vismatrix)
  362. {
  363. Log("Failed to allocate vismatrix");
  364. hlassume(s_vismatrix != NULL, assume_NoMemory);
  365. }
  366. NamedRunThreadsOn(g_numleafs - 1, g_estimate, BuildVisLeafs);
  367. }
  368. static void FreeVisMatrix()
  369. {
  370. if (s_vismatrix)
  371. {
  372. unsigned x;
  373. sparse_column_t* item;
  374. for (x = 0, item = s_vismatrix; x < g_num_patches; x++, item++)
  375. {
  376. if (item->row)
  377. {
  378. free(item->row);
  379. }
  380. }
  381. if (FreeBlock(s_vismatrix))
  382. {
  383. s_vismatrix = NULL;
  384. }
  385. else
  386. {
  387. Warning("Unable to free vismatrix");
  388. }
  389. }
  390. }
  391. static void DumpVismatrixInfo()
  392. {
  393. unsigned totals[8];
  394. unsigned total_vismatrix_memory = sizeof(sparse_column_t) * g_num_patches;
  395. sparse_column_t* column_end = s_vismatrix + g_num_patches;
  396. sparse_column_t* column = s_vismatrix;
  397. memset(totals, 0, sizeof(totals));
  398. while (column < column_end)
  399. {
  400. total_vismatrix_memory += column->count * sizeof(sparse_row_t);
  401. column++;
  402. }
  403. Log("Visibility matrix: %.1f MB\n", total_vismatrix_memory / (1024 * 1024.0));
  404. }
  405. //
  406. // end old vismat.c
  407. ////////////////////////////
  408. void MakeScalesSparseVismatrix()
  409. {
  410. char transferfile[_MAX_PATH];
  411. hlassume(g_num_patches < MAX_SPARSE_VISMATRIX_PATCHES, assume_MAX_PATCHES);
  412. safe_strncpy(transferfile, g_source, _MAX_PATH);
  413. StripExtension(transferfile);
  414. DefaultExtension(transferfile, ".inc");
  415. if (!g_incremental || !readtransfers(transferfile, g_num_patches))
  416. {
  417. // determine visibility between g_patches
  418. BuildVisMatrix();
  419. DumpVismatrixInfo();
  420. g_CheckVisBit = CheckVisBitSparse;
  421. #ifdef HLRAD_HULLU
  422. CreateFinalTransparencyArrays("custom shadow array");
  423. #endif
  424. #ifndef HLRAD_HULLU
  425. NamedRunThreadsOn(g_num_patches, g_estimate, MakeScales);
  426. #else
  427. if(g_rgb_transfers)
  428. {NamedRunThreadsOn(g_num_patches, g_estimate, MakeRGBScales);}
  429. else
  430. {NamedRunThreadsOn(g_num_patches, g_estimate, MakeScales);}
  431. #endif
  432. FreeVisMatrix();
  433. #ifdef HLRAD_HULLU
  434. FreeTransparencyArrays();
  435. #endif
  436. // invert the transfers for gather vs scatter
  437. #ifndef HLRAD_HULLU
  438. NamedRunThreadsOnIndividual(g_num_patches, g_estimate, SwapTransfers);
  439. #else
  440. if(g_rgb_transfers)
  441. {NamedRunThreadsOnIndividual(g_num_patches, g_estimate, SwapRGBTransfers);}
  442. else
  443. {NamedRunThreadsOnIndividual(g_num_patches, g_estimate, SwapTransfers);}
  444. #endif
  445. if (g_incremental)
  446. {
  447. writetransfers(transferfile, g_num_patches);
  448. }
  449. else
  450. {
  451. _unlink(transferfile);
  452. }
  453. // release visibility matrix
  454. DumpTransfersMemoryUsage();
  455. }
  456. }