Interaction.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "tr_local.h"
  23. /*
  24. ===========================================================================
  25. idInteraction implementation
  26. ===========================================================================
  27. */
  28. // FIXME: use private allocator for srfCullInfo_t
  29. /*
  30. ================
  31. R_CalcInteractionFacing
  32. Determines which triangles of the surface are facing towards the light origin.
  33. The facing array should be allocated with one extra index than
  34. the number of surface triangles, which will be used to handle dangling
  35. edge silhouettes.
  36. ================
  37. */
  38. void R_CalcInteractionFacing( const idRenderEntityLocal *ent, const srfTriangles_t *tri, const idRenderLightLocal *light, srfCullInfo_t &cullInfo ) {
  39. idVec3 localLightOrigin;
  40. if ( cullInfo.facing != NULL ) {
  41. return;
  42. }
  43. R_GlobalPointToLocal( ent->modelMatrix, light->globalLightOrigin, localLightOrigin );
  44. int numFaces = tri->numIndexes / 3;
  45. if ( !tri->facePlanes || !tri->facePlanesCalculated ) {
  46. R_DeriveFacePlanes( const_cast<srfTriangles_t *>(tri) );
  47. }
  48. cullInfo.facing = (byte *) R_StaticAlloc( ( numFaces + 1 ) * sizeof( cullInfo.facing[0] ) );
  49. // calculate back face culling
  50. float *planeSide = (float *) _alloca16( numFaces * sizeof( float ) );
  51. // exact geometric cull against face
  52. SIMDProcessor->Dot( planeSide, localLightOrigin, tri->facePlanes, numFaces );
  53. SIMDProcessor->CmpGE( cullInfo.facing, planeSide, 0.0f, numFaces );
  54. cullInfo.facing[ numFaces ] = 1; // for dangling edges to reference
  55. }
  56. /*
  57. =====================
  58. R_CalcInteractionCullBits
  59. We want to cull a little on the sloppy side, because the pre-clipping
  60. of geometry to the lights in dmap will give many cases that are right
  61. at the border we throw things out on the border, because if any one
  62. vertex is clearly inside, the entire triangle will be accepted.
  63. =====================
  64. */
  65. void R_CalcInteractionCullBits( const idRenderEntityLocal *ent, const srfTriangles_t *tri, const idRenderLightLocal *light, srfCullInfo_t &cullInfo ) {
  66. int i, frontBits;
  67. if ( cullInfo.cullBits != NULL ) {
  68. return;
  69. }
  70. frontBits = 0;
  71. // cull the triangle surface bounding box
  72. for ( i = 0; i < 6; i++ ) {
  73. R_GlobalPlaneToLocal( ent->modelMatrix, -light->frustum[i], cullInfo.localClipPlanes[i] );
  74. // get front bits for the whole surface
  75. if ( tri->bounds.PlaneDistance( cullInfo.localClipPlanes[i] ) >= LIGHT_CLIP_EPSILON ) {
  76. frontBits |= 1<<i;
  77. }
  78. }
  79. // if the surface is completely inside the light frustum
  80. if ( frontBits == ( ( 1 << 6 ) - 1 ) ) {
  81. cullInfo.cullBits = LIGHT_CULL_ALL_FRONT;
  82. return;
  83. }
  84. cullInfo.cullBits = (byte *) R_StaticAlloc( tri->numVerts * sizeof( cullInfo.cullBits[0] ) );
  85. SIMDProcessor->Memset( cullInfo.cullBits, 0, tri->numVerts * sizeof( cullInfo.cullBits[0] ) );
  86. float *planeSide = (float *) _alloca16( tri->numVerts * sizeof( float ) );
  87. for ( i = 0; i < 6; i++ ) {
  88. // if completely infront of this clipping plane
  89. if ( frontBits & ( 1 << i ) ) {
  90. continue;
  91. }
  92. SIMDProcessor->Dot( planeSide, cullInfo.localClipPlanes[i], tri->verts, tri->numVerts );
  93. SIMDProcessor->CmpLT( cullInfo.cullBits, i, planeSide, LIGHT_CLIP_EPSILON, tri->numVerts );
  94. }
  95. }
  96. /*
  97. ================
  98. R_FreeInteractionCullInfo
  99. ================
  100. */
  101. void R_FreeInteractionCullInfo( srfCullInfo_t &cullInfo ) {
  102. if ( cullInfo.facing != NULL ) {
  103. R_StaticFree( cullInfo.facing );
  104. cullInfo.facing = NULL;
  105. }
  106. if ( cullInfo.cullBits != NULL ) {
  107. if ( cullInfo.cullBits != LIGHT_CULL_ALL_FRONT ) {
  108. R_StaticFree( cullInfo.cullBits );
  109. }
  110. cullInfo.cullBits = NULL;
  111. }
  112. }
  113. #define MAX_CLIPPED_POINTS 20
  114. typedef struct {
  115. int numVerts;
  116. idVec3 verts[MAX_CLIPPED_POINTS];
  117. } clipTri_t;
  118. /*
  119. =============
  120. R_ChopWinding
  121. Clips a triangle from one buffer to another, setting edge flags
  122. The returned buffer may be the same as inNum if no clipping is done
  123. If entirely clipped away, clipTris[returned].numVerts == 0
  124. I have some worries about edge flag cases when polygons are clipped
  125. multiple times near the epsilon.
  126. =============
  127. */
  128. static int R_ChopWinding( clipTri_t clipTris[2], int inNum, const idPlane plane ) {
  129. clipTri_t *in, *out;
  130. float dists[MAX_CLIPPED_POINTS];
  131. int sides[MAX_CLIPPED_POINTS];
  132. int counts[3];
  133. float dot;
  134. int i, j;
  135. idVec3 mid;
  136. bool front;
  137. in = &clipTris[inNum];
  138. out = &clipTris[inNum^1];
  139. counts[0] = counts[1] = counts[2] = 0;
  140. // determine sides for each point
  141. front = false;
  142. for ( i = 0; i < in->numVerts; i++ ) {
  143. dot = in->verts[i] * plane.Normal() + plane[3];
  144. dists[i] = dot;
  145. if ( dot < LIGHT_CLIP_EPSILON ) { // slop onto the back
  146. sides[i] = SIDE_BACK;
  147. } else {
  148. sides[i] = SIDE_FRONT;
  149. if ( dot > LIGHT_CLIP_EPSILON ) {
  150. front = true;
  151. }
  152. }
  153. counts[sides[i]]++;
  154. }
  155. // if none in front, it is completely clipped away
  156. if ( !front ) {
  157. in->numVerts = 0;
  158. return inNum;
  159. }
  160. if ( !counts[SIDE_BACK] ) {
  161. return inNum; // inout stays the same
  162. }
  163. // avoid wrapping checks by duplicating first value to end
  164. sides[i] = sides[0];
  165. dists[i] = dists[0];
  166. in->verts[in->numVerts] = in->verts[0];
  167. out->numVerts = 0;
  168. for ( i = 0 ; i < in->numVerts ; i++ ) {
  169. idVec3 &p1 = in->verts[i];
  170. if ( sides[i] == SIDE_FRONT ) {
  171. out->verts[out->numVerts] = p1;
  172. out->numVerts++;
  173. }
  174. if ( sides[i+1] == sides[i] ) {
  175. continue;
  176. }
  177. // generate a split point
  178. idVec3 &p2 = in->verts[i+1];
  179. dot = dists[i] / ( dists[i] - dists[i+1] );
  180. for ( j = 0; j < 3; j++ ) {
  181. mid[j] = p1[j] + dot * ( p2[j] - p1[j] );
  182. }
  183. out->verts[out->numVerts] = mid;
  184. out->numVerts++;
  185. }
  186. return inNum ^ 1;
  187. }
  188. /*
  189. ===================
  190. R_ClipTriangleToLight
  191. Returns false if nothing is left after clipping
  192. ===================
  193. */
  194. static bool R_ClipTriangleToLight( const idVec3 &a, const idVec3 &b, const idVec3 &c, int planeBits, const idPlane frustum[6] ) {
  195. int i;
  196. clipTri_t pingPong[2];
  197. int p;
  198. pingPong[0].numVerts = 3;
  199. pingPong[0].verts[0] = a;
  200. pingPong[0].verts[1] = b;
  201. pingPong[0].verts[2] = c;
  202. p = 0;
  203. for ( i = 0 ; i < 6 ; i++ ) {
  204. if ( planeBits & ( 1 << i ) ) {
  205. p = R_ChopWinding( pingPong, p, frustum[i] );
  206. if ( pingPong[p].numVerts < 1 ) {
  207. return false;
  208. }
  209. }
  210. }
  211. return true;
  212. }
  213. /*
  214. ====================
  215. R_CreateLightTris
  216. The resulting surface will be a subset of the original triangles,
  217. it will never clip triangles, but it may cull on a per-triangle basis.
  218. ====================
  219. */
  220. static srfTriangles_t *R_CreateLightTris( const idRenderEntityLocal *ent,
  221. const srfTriangles_t *tri, const idRenderLightLocal *light,
  222. const idMaterial *shader, srfCullInfo_t &cullInfo ) {
  223. int i;
  224. int numIndexes;
  225. glIndex_t *indexes;
  226. srfTriangles_t *newTri;
  227. int c_backfaced;
  228. int c_distance;
  229. idBounds bounds;
  230. bool includeBackFaces;
  231. int faceNum;
  232. tr.pc.c_createLightTris++;
  233. c_backfaced = 0;
  234. c_distance = 0;
  235. numIndexes = 0;
  236. indexes = NULL;
  237. // it is debatable if non-shadowing lights should light back faces. we aren't at the moment
  238. if ( r_lightAllBackFaces.GetBool() || light->lightShader->LightEffectsBackSides()
  239. || shader->ReceivesLightingOnBackSides()
  240. || ent->parms.noSelfShadow || ent->parms.noShadow ) {
  241. includeBackFaces = true;
  242. } else {
  243. includeBackFaces = false;
  244. }
  245. // allocate a new surface for the lit triangles
  246. newTri = R_AllocStaticTriSurf();
  247. // save a reference to the original surface
  248. newTri->ambientSurface = const_cast<srfTriangles_t *>(tri);
  249. // the light surface references the verts of the ambient surface
  250. newTri->numVerts = tri->numVerts;
  251. R_ReferenceStaticTriSurfVerts( newTri, tri );
  252. // calculate cull information
  253. if ( !includeBackFaces ) {
  254. R_CalcInteractionFacing( ent, tri, light, cullInfo );
  255. }
  256. R_CalcInteractionCullBits( ent, tri, light, cullInfo );
  257. // if the surface is completely inside the light frustum
  258. if ( cullInfo.cullBits == LIGHT_CULL_ALL_FRONT ) {
  259. // if we aren't self shadowing, let back facing triangles get
  260. // through so the smooth shaded bump maps light all the way around
  261. if ( includeBackFaces ) {
  262. // the whole surface is lit so the light surface just references the indexes of the ambient surface
  263. R_ReferenceStaticTriSurfIndexes( newTri, tri );
  264. numIndexes = tri->numIndexes;
  265. bounds = tri->bounds;
  266. } else {
  267. // the light tris indexes are going to be a subset of the original indexes so we generally
  268. // allocate too much memory here but we decrease the memory block when the number of indexes is known
  269. R_AllocStaticTriSurfIndexes( newTri, tri->numIndexes );
  270. // back face cull the individual triangles
  271. indexes = newTri->indexes;
  272. const byte *facing = cullInfo.facing;
  273. for ( faceNum = i = 0; i < tri->numIndexes; i += 3, faceNum++ ) {
  274. if ( !facing[ faceNum ] ) {
  275. c_backfaced++;
  276. continue;
  277. }
  278. indexes[numIndexes+0] = tri->indexes[i+0];
  279. indexes[numIndexes+1] = tri->indexes[i+1];
  280. indexes[numIndexes+2] = tri->indexes[i+2];
  281. numIndexes += 3;
  282. }
  283. // get bounds for the surface
  284. SIMDProcessor->MinMax( bounds[0], bounds[1], tri->verts, indexes, numIndexes );
  285. // decrease the size of the memory block to the size of the number of used indexes
  286. R_ResizeStaticTriSurfIndexes( newTri, numIndexes );
  287. }
  288. } else {
  289. // the light tris indexes are going to be a subset of the original indexes so we generally
  290. // allocate too much memory here but we decrease the memory block when the number of indexes is known
  291. R_AllocStaticTriSurfIndexes( newTri, tri->numIndexes );
  292. // cull individual triangles
  293. indexes = newTri->indexes;
  294. const byte *facing = cullInfo.facing;
  295. const byte *cullBits = cullInfo.cullBits;
  296. for ( faceNum = i = 0; i < tri->numIndexes; i += 3, faceNum++ ) {
  297. int i1, i2, i3;
  298. // if we aren't self shadowing, let back facing triangles get
  299. // through so the smooth shaded bump maps light all the way around
  300. if ( !includeBackFaces ) {
  301. // back face cull
  302. if ( !facing[ faceNum ] ) {
  303. c_backfaced++;
  304. continue;
  305. }
  306. }
  307. i1 = tri->indexes[i+0];
  308. i2 = tri->indexes[i+1];
  309. i3 = tri->indexes[i+2];
  310. // fast cull outside the frustum
  311. // if all three points are off one plane side, it definately isn't visible
  312. if ( cullBits[i1] & cullBits[i2] & cullBits[i3] ) {
  313. c_distance++;
  314. continue;
  315. }
  316. if ( r_usePreciseTriangleInteractions.GetBool() ) {
  317. // do a precise clipped cull if none of the points is completely inside the frustum
  318. // note that we do not actually use the clipped triangle, which would have Z fighting issues.
  319. if ( cullBits[i1] && cullBits[i2] && cullBits[i3] ) {
  320. int cull = cullBits[i1] | cullBits[i2] | cullBits[i3];
  321. if ( !R_ClipTriangleToLight( tri->verts[i1].xyz, tri->verts[i2].xyz, tri->verts[i3].xyz, cull, cullInfo.localClipPlanes ) ) {
  322. continue;
  323. }
  324. }
  325. }
  326. // add to the list
  327. indexes[numIndexes+0] = i1;
  328. indexes[numIndexes+1] = i2;
  329. indexes[numIndexes+2] = i3;
  330. numIndexes += 3;
  331. }
  332. // get bounds for the surface
  333. SIMDProcessor->MinMax( bounds[0], bounds[1], tri->verts, indexes, numIndexes );
  334. // decrease the size of the memory block to the size of the number of used indexes
  335. R_ResizeStaticTriSurfIndexes( newTri, numIndexes );
  336. }
  337. if ( !numIndexes ) {
  338. R_ReallyFreeStaticTriSurf( newTri );
  339. return NULL;
  340. }
  341. newTri->numIndexes = numIndexes;
  342. newTri->bounds = bounds;
  343. return newTri;
  344. }
  345. /*
  346. ===============
  347. idInteraction::idInteraction
  348. ===============
  349. */
  350. idInteraction::idInteraction( void ) {
  351. numSurfaces = 0;
  352. surfaces = NULL;
  353. entityDef = NULL;
  354. lightDef = NULL;
  355. lightNext = NULL;
  356. lightPrev = NULL;
  357. entityNext = NULL;
  358. entityPrev = NULL;
  359. dynamicModelFrameCount = 0;
  360. frustumState = FRUSTUM_UNINITIALIZED;
  361. frustumAreas = NULL;
  362. }
  363. /*
  364. ===============
  365. idInteraction::AllocAndLink
  366. ===============
  367. */
  368. idInteraction *idInteraction::AllocAndLink( idRenderEntityLocal *edef, idRenderLightLocal *ldef ) {
  369. if ( !edef || !ldef ) {
  370. common->Error( "idInteraction::AllocAndLink: NULL parm" );
  371. }
  372. idRenderWorldLocal *renderWorld = edef->world;
  373. idInteraction *interaction = renderWorld->interactionAllocator.Alloc();
  374. // link and initialize
  375. interaction->dynamicModelFrameCount = 0;
  376. interaction->lightDef = ldef;
  377. interaction->entityDef = edef;
  378. interaction->numSurfaces = -1; // not checked yet
  379. interaction->surfaces = NULL;
  380. interaction->frustumState = idInteraction::FRUSTUM_UNINITIALIZED;
  381. interaction->frustumAreas = NULL;
  382. // link at the start of the entity's list
  383. interaction->lightNext = ldef->firstInteraction;
  384. interaction->lightPrev = NULL;
  385. ldef->firstInteraction = interaction;
  386. if ( interaction->lightNext != NULL ) {
  387. interaction->lightNext->lightPrev = interaction;
  388. } else {
  389. ldef->lastInteraction = interaction;
  390. }
  391. // link at the start of the light's list
  392. interaction->entityNext = edef->firstInteraction;
  393. interaction->entityPrev = NULL;
  394. edef->firstInteraction = interaction;
  395. if ( interaction->entityNext != NULL ) {
  396. interaction->entityNext->entityPrev = interaction;
  397. } else {
  398. edef->lastInteraction = interaction;
  399. }
  400. // update the interaction table
  401. if ( renderWorld->interactionTable ) {
  402. int index = ldef->index * renderWorld->interactionTableWidth + edef->index;
  403. if ( renderWorld->interactionTable[index] != NULL ) {
  404. common->Error( "idInteraction::AllocAndLink: non NULL table entry" );
  405. }
  406. renderWorld->interactionTable[ index ] = interaction;
  407. }
  408. return interaction;
  409. }
  410. /*
  411. ===============
  412. idInteraction::FreeSurfaces
  413. Frees the surfaces, but leaves the interaction linked in, so it
  414. will be regenerated automatically
  415. ===============
  416. */
  417. void idInteraction::FreeSurfaces( void ) {
  418. if ( this->surfaces ) {
  419. for ( int i = 0 ; i < this->numSurfaces ; i++ ) {
  420. surfaceInteraction_t *sint = &this->surfaces[i];
  421. if ( sint->lightTris ) {
  422. if ( sint->lightTris != LIGHT_TRIS_DEFERRED ) {
  423. R_FreeStaticTriSurf( sint->lightTris );
  424. }
  425. sint->lightTris = NULL;
  426. }
  427. if ( sint->shadowTris ) {
  428. // if it doesn't have an entityDef, it is part of a prelight
  429. // model, not a generated interaction
  430. if ( this->entityDef ) {
  431. R_FreeStaticTriSurf( sint->shadowTris );
  432. sint->shadowTris = NULL;
  433. }
  434. }
  435. R_FreeInteractionCullInfo( sint->cullInfo );
  436. }
  437. R_StaticFree( this->surfaces );
  438. this->surfaces = NULL;
  439. }
  440. this->numSurfaces = -1;
  441. }
  442. /*
  443. ===============
  444. idInteraction::Unlink
  445. ===============
  446. */
  447. void idInteraction::Unlink( void ) {
  448. // unlink from the entity's list
  449. if ( this->entityPrev ) {
  450. this->entityPrev->entityNext = this->entityNext;
  451. } else {
  452. this->entityDef->firstInteraction = this->entityNext;
  453. }
  454. if ( this->entityNext ) {
  455. this->entityNext->entityPrev = this->entityPrev;
  456. } else {
  457. this->entityDef->lastInteraction = this->entityPrev;
  458. }
  459. this->entityNext = this->entityPrev = NULL;
  460. // unlink from the light's list
  461. if ( this->lightPrev ) {
  462. this->lightPrev->lightNext = this->lightNext;
  463. } else {
  464. this->lightDef->firstInteraction = this->lightNext;
  465. }
  466. if ( this->lightNext ) {
  467. this->lightNext->lightPrev = this->lightPrev;
  468. } else {
  469. this->lightDef->lastInteraction = this->lightPrev;
  470. }
  471. this->lightNext = this->lightPrev = NULL;
  472. }
  473. /*
  474. ===============
  475. idInteraction::UnlinkAndFree
  476. Removes links and puts it back on the free list.
  477. ===============
  478. */
  479. void idInteraction::UnlinkAndFree( void ) {
  480. // clear the table pointer
  481. idRenderWorldLocal *renderWorld = this->lightDef->world;
  482. if ( renderWorld->interactionTable ) {
  483. int index = this->lightDef->index * renderWorld->interactionTableWidth + this->entityDef->index;
  484. if ( renderWorld->interactionTable[index] != this ) {
  485. common->Error( "idInteraction::UnlinkAndFree: interactionTable wasn't set" );
  486. }
  487. renderWorld->interactionTable[index] = NULL;
  488. }
  489. Unlink();
  490. FreeSurfaces();
  491. // free the interaction area references
  492. areaNumRef_t *area, *nextArea;
  493. for ( area = frustumAreas; area; area = nextArea ) {
  494. nextArea = area->next;
  495. renderWorld->areaNumRefAllocator.Free( area );
  496. }
  497. // put it back on the free list
  498. renderWorld->interactionAllocator.Free( this );
  499. }
  500. /*
  501. ===============
  502. idInteraction::MakeEmpty
  503. Makes the interaction empty and links it at the end of the entity's and light's interaction lists.
  504. ===============
  505. */
  506. void idInteraction::MakeEmpty( void ) {
  507. // an empty interaction has no surfaces
  508. numSurfaces = 0;
  509. Unlink();
  510. // relink at the end of the entity's list
  511. this->entityNext = NULL;
  512. this->entityPrev = this->entityDef->lastInteraction;
  513. this->entityDef->lastInteraction = this;
  514. if ( this->entityPrev ) {
  515. this->entityPrev->entityNext = this;
  516. } else {
  517. this->entityDef->firstInteraction = this;
  518. }
  519. // relink at the end of the light's list
  520. this->lightNext = NULL;
  521. this->lightPrev = this->lightDef->lastInteraction;
  522. this->lightDef->lastInteraction = this;
  523. if ( this->lightPrev ) {
  524. this->lightPrev->lightNext = this;
  525. } else {
  526. this->lightDef->firstInteraction = this;
  527. }
  528. }
  529. /*
  530. ===============
  531. idInteraction::HasShadows
  532. ===============
  533. */
  534. ID_INLINE bool idInteraction::HasShadows( void ) const {
  535. return ( !lightDef->parms.noShadows && !entityDef->parms.noShadow && lightDef->lightShader->LightCastsShadows() );
  536. }
  537. /*
  538. ===============
  539. idInteraction::MemoryUsed
  540. Counts up the memory used by all the surfaceInteractions, which
  541. will be used to determine when we need to start purging old interactions.
  542. ===============
  543. */
  544. int idInteraction::MemoryUsed( void ) {
  545. int total = 0;
  546. for ( int i = 0 ; i < numSurfaces ; i++ ) {
  547. surfaceInteraction_t *inter = &surfaces[i];
  548. total += R_TriSurfMemory( inter->lightTris );
  549. total += R_TriSurfMemory( inter->shadowTris );
  550. }
  551. return total;
  552. }
  553. /*
  554. ==================
  555. idInteraction::CalcInteractionScissorRectangle
  556. ==================
  557. */
  558. idScreenRect idInteraction::CalcInteractionScissorRectangle( const idFrustum &viewFrustum ) {
  559. idBounds projectionBounds;
  560. idScreenRect portalRect;
  561. idScreenRect scissorRect;
  562. if ( r_useInteractionScissors.GetInteger() == 0 ) {
  563. return lightDef->viewLight->scissorRect;
  564. }
  565. if ( r_useInteractionScissors.GetInteger() < 0 ) {
  566. // this is the code from Cass at nvidia, it is more precise, but slower
  567. return R_CalcIntersectionScissor( lightDef, entityDef, tr.viewDef );
  568. }
  569. // the following is Mr.E's code
  570. // frustum must be initialized and valid
  571. if ( frustumState == idInteraction::FRUSTUM_UNINITIALIZED || frustumState == idInteraction::FRUSTUM_INVALID ) {
  572. return lightDef->viewLight->scissorRect;
  573. }
  574. // calculate scissors for the portals through which the interaction is visible
  575. if ( r_useInteractionScissors.GetInteger() > 1 ) {
  576. areaNumRef_t *area;
  577. if ( frustumState == idInteraction::FRUSTUM_VALID ) {
  578. // retrieve all the areas the interaction frustum touches
  579. for ( areaReference_t *ref = entityDef->entityRefs; ref; ref = ref->ownerNext ) {
  580. area = entityDef->world->areaNumRefAllocator.Alloc();
  581. area->areaNum = ref->area->areaNum;
  582. area->next = frustumAreas;
  583. frustumAreas = area;
  584. }
  585. frustumAreas = tr.viewDef->renderWorld->FloodFrustumAreas( frustum, frustumAreas );
  586. frustumState = idInteraction::FRUSTUM_VALIDAREAS;
  587. }
  588. portalRect.Clear();
  589. for ( area = frustumAreas; area; area = area->next ) {
  590. portalRect.Union( entityDef->world->GetAreaScreenRect( area->areaNum ) );
  591. }
  592. portalRect.Intersect( lightDef->viewLight->scissorRect );
  593. } else {
  594. portalRect = lightDef->viewLight->scissorRect;
  595. }
  596. // early out if the interaction is not visible through any portals
  597. if ( portalRect.IsEmpty() ) {
  598. return portalRect;
  599. }
  600. // calculate bounds of the interaction frustum projected into the view frustum
  601. if ( lightDef->parms.pointLight ) {
  602. viewFrustum.ClippedProjectionBounds( frustum, idBox( lightDef->parms.origin, lightDef->parms.lightRadius, lightDef->parms.axis ), projectionBounds );
  603. } else {
  604. viewFrustum.ClippedProjectionBounds( frustum, idBox( lightDef->frustumTris->bounds ), projectionBounds );
  605. }
  606. if ( projectionBounds.IsCleared() ) {
  607. return portalRect;
  608. }
  609. // derive a scissor rectangle from the projection bounds
  610. scissorRect = R_ScreenRectFromViewFrustumBounds( projectionBounds );
  611. // intersect with the portal crossing scissor rectangle
  612. scissorRect.Intersect( portalRect );
  613. if ( r_showInteractionScissors.GetInteger() > 0 ) {
  614. R_ShowColoredScreenRect( scissorRect, lightDef->index );
  615. }
  616. return scissorRect;
  617. }
  618. /*
  619. ===================
  620. idInteraction::CullInteractionByViewFrustum
  621. ===================
  622. */
  623. bool idInteraction::CullInteractionByViewFrustum( const idFrustum &viewFrustum ) {
  624. if ( !r_useInteractionCulling.GetBool() ) {
  625. return false;
  626. }
  627. if ( frustumState == idInteraction::FRUSTUM_INVALID ) {
  628. return false;
  629. }
  630. if ( frustumState == idInteraction::FRUSTUM_UNINITIALIZED ) {
  631. frustum.FromProjection( idBox( entityDef->referenceBounds, entityDef->parms.origin, entityDef->parms.axis ), lightDef->globalLightOrigin, MAX_WORLD_SIZE );
  632. if ( !frustum.IsValid() ) {
  633. frustumState = idInteraction::FRUSTUM_INVALID;
  634. return false;
  635. }
  636. if ( lightDef->parms.pointLight ) {
  637. frustum.ConstrainToBox( idBox( lightDef->parms.origin, lightDef->parms.lightRadius, lightDef->parms.axis ) );
  638. } else {
  639. frustum.ConstrainToBox( idBox( lightDef->frustumTris->bounds ) );
  640. }
  641. frustumState = idInteraction::FRUSTUM_VALID;
  642. }
  643. if ( !viewFrustum.IntersectsFrustum( frustum ) ) {
  644. return true;
  645. }
  646. if ( r_showInteractionFrustums.GetInteger() ) {
  647. static idVec4 colors[] = { colorRed, colorGreen, colorBlue, colorYellow, colorMagenta, colorCyan, colorWhite, colorPurple };
  648. tr.viewDef->renderWorld->DebugFrustum( colors[lightDef->index & 7], frustum, ( r_showInteractionFrustums.GetInteger() > 1 ) );
  649. if ( r_showInteractionFrustums.GetInteger() > 2 ) {
  650. tr.viewDef->renderWorld->DebugBox( colorWhite, idBox( entityDef->referenceBounds, entityDef->parms.origin, entityDef->parms.axis ) );
  651. }
  652. }
  653. return false;
  654. }
  655. /*
  656. ====================
  657. idInteraction::CreateInteraction
  658. Called when a entityDef and a lightDef are both present in a
  659. portalArea, and might be visible. Performs cull checking before doing the expensive
  660. computations.
  661. References tr.viewCount so lighting surfaces will only be created if the ambient surface is visible,
  662. otherwise it will be marked as deferred.
  663. The results of this are cached and valid until the light or entity change.
  664. ====================
  665. */
  666. void idInteraction::CreateInteraction( const idRenderModel *model ) {
  667. const idMaterial * lightShader = lightDef->lightShader;
  668. const idMaterial* shader;
  669. bool interactionGenerated;
  670. idBounds bounds;
  671. tr.pc.c_createInteractions++;
  672. bounds = model->Bounds( &entityDef->parms );
  673. // if it doesn't contact the light frustum, none of the surfaces will
  674. if ( R_CullLocalBox( bounds, entityDef->modelMatrix, 6, lightDef->frustum ) ) {
  675. MakeEmpty();
  676. return;
  677. }
  678. // use the turbo shadow path
  679. shadowGen_t shadowGen = SG_DYNAMIC;
  680. // really large models, like outside terrain meshes, should use
  681. // the more exactly culled static shadow path instead of the turbo shadow path.
  682. // FIXME: this is a HACK, we should probably have a material flag.
  683. if ( bounds[1][0] - bounds[0][0] > 3000 ) {
  684. shadowGen = SG_STATIC;
  685. }
  686. //
  687. // create slots for each of the model's surfaces
  688. //
  689. numSurfaces = model->NumSurfaces();
  690. surfaces = (surfaceInteraction_t *)R_ClearedStaticAlloc( sizeof( *surfaces ) * numSurfaces );
  691. interactionGenerated = false;
  692. // check each surface in the model
  693. for ( int c = 0 ; c < model->NumSurfaces() ; c++ ) {
  694. const modelSurface_t *surf;
  695. srfTriangles_t *tri;
  696. surf = model->Surface( c );
  697. tri = surf->geometry;
  698. if ( !tri ) {
  699. continue;
  700. }
  701. // determine the shader for this surface, possibly by skinning
  702. shader = surf->shader;
  703. shader = R_RemapShaderBySkin( shader, entityDef->parms.customSkin, entityDef->parms.customShader );
  704. if ( !shader ) {
  705. continue;
  706. }
  707. // try to cull each surface
  708. if ( R_CullLocalBox( tri->bounds, entityDef->modelMatrix, 6, lightDef->frustum ) ) {
  709. continue;
  710. }
  711. surfaceInteraction_t *sint = &surfaces[c];
  712. sint->shader = shader;
  713. // save the ambient tri pointer so we can reject lightTri interactions
  714. // when the ambient surface isn't in view, and we can get shared vertex
  715. // and shadow data from the source surface
  716. sint->ambientTris = tri;
  717. // "invisible ink" lights and shaders
  718. if ( shader->Spectrum() != lightShader->Spectrum() ) {
  719. continue;
  720. }
  721. // generate a lighted surface and add it
  722. if ( shader->ReceivesLighting() ) {
  723. if ( tri->ambientViewCount == tr.viewCount ) {
  724. sint->lightTris = R_CreateLightTris( entityDef, tri, lightDef, shader, sint->cullInfo );
  725. } else {
  726. // this will be calculated when sint->ambientTris is actually in view
  727. sint->lightTris = LIGHT_TRIS_DEFERRED;
  728. }
  729. interactionGenerated = true;
  730. }
  731. // if the interaction has shadows and this surface casts a shadow
  732. if ( HasShadows() && shader->SurfaceCastsShadow() && tri->silEdges != NULL ) {
  733. // if the light has an optimized shadow volume, don't create shadows for any models that are part of the base areas
  734. if ( lightDef->parms.prelightModel == NULL || !model->IsStaticWorldModel() || !r_useOptimizedShadows.GetBool() ) {
  735. // this is the only place during gameplay (outside the utilities) that R_CreateShadowVolume() is called
  736. sint->shadowTris = R_CreateShadowVolume( entityDef, tri, lightDef, shadowGen, sint->cullInfo );
  737. if ( sint->shadowTris ) {
  738. if ( shader->Coverage() != MC_OPAQUE || ( !r_skipSuppress.GetBool() && entityDef->parms.suppressSurfaceInViewID ) ) {
  739. // if any surface is a shadow-casting perforated or translucent surface, or the
  740. // base surface is suppressed in the view (world weapon shadows) we can't use
  741. // the external shadow optimizations because we can see through some of the faces
  742. sint->shadowTris->numShadowIndexesNoCaps = sint->shadowTris->numIndexes;
  743. sint->shadowTris->numShadowIndexesNoFrontCaps = sint->shadowTris->numIndexes;
  744. }
  745. }
  746. interactionGenerated = true;
  747. }
  748. }
  749. // free the cull information when it's no longer needed
  750. if ( sint->lightTris != LIGHT_TRIS_DEFERRED ) {
  751. R_FreeInteractionCullInfo( sint->cullInfo );
  752. }
  753. }
  754. // if none of the surfaces generated anything, don't even bother checking?
  755. if ( !interactionGenerated ) {
  756. MakeEmpty();
  757. }
  758. }
  759. /*
  760. ======================
  761. R_PotentiallyInsideInfiniteShadow
  762. If we know that we are "off to the side" of an infinite shadow volume,
  763. we can draw it without caps in zpass mode
  764. ======================
  765. */
  766. static bool R_PotentiallyInsideInfiniteShadow( const srfTriangles_t *occluder,
  767. const idVec3 &localView, const idVec3 &localLight ) {
  768. idBounds exp;
  769. // expand the bounds to account for the near clip plane, because the
  770. // view could be mathematically outside, but if the near clip plane
  771. // chops a volume edge, the zpass rendering would fail.
  772. float znear = r_znear.GetFloat();
  773. if ( tr.viewDef->renderView.cramZNear ) {
  774. znear *= 0.25f;
  775. }
  776. float stretch = znear * 2; // in theory, should vary with FOV
  777. exp[0][0] = occluder->bounds[0][0] - stretch;
  778. exp[0][1] = occluder->bounds[0][1] - stretch;
  779. exp[0][2] = occluder->bounds[0][2] - stretch;
  780. exp[1][0] = occluder->bounds[1][0] + stretch;
  781. exp[1][1] = occluder->bounds[1][1] + stretch;
  782. exp[1][2] = occluder->bounds[1][2] + stretch;
  783. if ( exp.ContainsPoint( localView ) ) {
  784. return true;
  785. }
  786. if ( exp.ContainsPoint( localLight ) ) {
  787. return true;
  788. }
  789. // if the ray from localLight to localView intersects a face of the
  790. // expanded bounds, we will be inside the projection
  791. idVec3 ray = localView - localLight;
  792. // intersect the ray from the view to the light with the near side of the bounds
  793. for ( int axis = 0; axis < 3; axis++ ) {
  794. float d, frac;
  795. idVec3 hit;
  796. if ( localLight[axis] < exp[0][axis] ) {
  797. if ( localView[axis] < exp[0][axis] ) {
  798. continue;
  799. }
  800. d = exp[0][axis] - localLight[axis];
  801. frac = d / ray[axis];
  802. hit = localLight + frac * ray;
  803. hit[axis] = exp[0][axis];
  804. } else if ( localLight[axis] > exp[1][axis] ) {
  805. if ( localView[axis] > exp[1][axis] ) {
  806. continue;
  807. }
  808. d = exp[1][axis] - localLight[axis];
  809. frac = d / ray[axis];
  810. hit = localLight + frac * ray;
  811. hit[axis] = exp[1][axis];
  812. } else {
  813. continue;
  814. }
  815. if ( exp.ContainsPoint( hit ) ) {
  816. return true;
  817. }
  818. }
  819. // the view is definitely not inside the projected shadow
  820. return false;
  821. }
  822. /*
  823. ==================
  824. idInteraction::AddActiveInteraction
  825. Create and add any necessary light and shadow triangles
  826. If the model doesn't have any surfaces that need interactions
  827. with this type of light, it can be skipped, but we might need to
  828. instantiate the dynamic model to find out
  829. ==================
  830. */
  831. void idInteraction::AddActiveInteraction( void ) {
  832. viewLight_t * vLight;
  833. viewEntity_t * vEntity;
  834. idScreenRect shadowScissor;
  835. idScreenRect lightScissor;
  836. idVec3 localLightOrigin;
  837. idVec3 localViewOrigin;
  838. vLight = lightDef->viewLight;
  839. vEntity = entityDef->viewEntity;
  840. // do not waste time culling the interaction frustum if there will be no shadows
  841. if ( !HasShadows() ) {
  842. // use the entity scissor rectangle
  843. shadowScissor = vEntity->scissorRect;
  844. // culling does not seem to be worth it for static world models
  845. } else if ( entityDef->parms.hModel->IsStaticWorldModel() ) {
  846. // use the light scissor rectangle
  847. shadowScissor = vLight->scissorRect;
  848. } else {
  849. // try to cull the interaction
  850. // this will also cull the case where the light origin is inside the
  851. // view frustum and the entity bounds are outside the view frustum
  852. if ( CullInteractionByViewFrustum( tr.viewDef->viewFrustum ) ) {
  853. return;
  854. }
  855. // calculate the shadow scissor rectangle
  856. shadowScissor = CalcInteractionScissorRectangle( tr.viewDef->viewFrustum );
  857. }
  858. // get out before making the dynamic model if the shadow scissor rectangle is empty
  859. if ( shadowScissor.IsEmpty() ) {
  860. return;
  861. }
  862. // We will need the dynamic surface created to make interactions, even if the
  863. // model itself wasn't visible. This just returns a cached value after it
  864. // has been generated once in the view.
  865. idRenderModel *model = R_EntityDefDynamicModel( entityDef );
  866. if ( model == NULL || model->NumSurfaces() <= 0 ) {
  867. return;
  868. }
  869. // the dynamic model may have changed since we built the surface list
  870. if ( !IsDeferred() && entityDef->dynamicModelFrameCount != dynamicModelFrameCount ) {
  871. FreeSurfaces();
  872. }
  873. dynamicModelFrameCount = entityDef->dynamicModelFrameCount;
  874. // actually create the interaction if needed, building light and shadow surfaces as needed
  875. if ( IsDeferred() ) {
  876. CreateInteraction( model );
  877. }
  878. R_GlobalPointToLocal( vEntity->modelMatrix, lightDef->globalLightOrigin, localLightOrigin );
  879. R_GlobalPointToLocal( vEntity->modelMatrix, tr.viewDef->renderView.vieworg, localViewOrigin );
  880. // calculate the scissor as the intersection of the light and model rects
  881. // this is used for light triangles, but not for shadow triangles
  882. lightScissor = vLight->scissorRect;
  883. lightScissor.Intersect( vEntity->scissorRect );
  884. bool lightScissorsEmpty = lightScissor.IsEmpty();
  885. // for each surface of this entity / light interaction
  886. for ( int i = 0; i < numSurfaces; i++ ) {
  887. surfaceInteraction_t *sint = &surfaces[i];
  888. // see if the base surface is visible, we may still need to add shadows even if empty
  889. if ( !lightScissorsEmpty && sint->ambientTris && sint->ambientTris->ambientViewCount == tr.viewCount ) {
  890. // make sure we have created this interaction, which may have been deferred
  891. // on a previous use that only needed the shadow
  892. if ( sint->lightTris == LIGHT_TRIS_DEFERRED ) {
  893. sint->lightTris = R_CreateLightTris( vEntity->entityDef, sint->ambientTris, vLight->lightDef, sint->shader, sint->cullInfo );
  894. R_FreeInteractionCullInfo( sint->cullInfo );
  895. }
  896. srfTriangles_t *lightTris = sint->lightTris;
  897. if ( lightTris ) {
  898. // try to cull before adding
  899. // FIXME: this may not be worthwhile. We have already done culling on the ambient,
  900. // but individual surfaces may still be cropped somewhat more
  901. if ( !R_CullLocalBox( lightTris->bounds, vEntity->modelMatrix, 5, tr.viewDef->frustum ) ) {
  902. // make sure the original surface has its ambient cache created
  903. srfTriangles_t *tri = sint->ambientTris;
  904. if ( !tri->ambientCache ) {
  905. if ( !R_CreateAmbientCache( tri, sint->shader->ReceivesLighting() ) ) {
  906. // skip if we were out of vertex memory
  907. continue;
  908. }
  909. }
  910. // reference the original surface's ambient cache
  911. lightTris->ambientCache = tri->ambientCache;
  912. // touch the ambient surface so it won't get purged
  913. vertexCache.Touch( lightTris->ambientCache );
  914. // regenerate the lighting cache (for non-vertex program cards) if it has been purged
  915. if ( !lightTris->lightingCache ) {
  916. if ( !R_CreateLightingCache( entityDef, lightDef, lightTris ) ) {
  917. // skip if we are out of vertex memory
  918. continue;
  919. }
  920. }
  921. // touch the light surface so it won't get purged
  922. // (vertex program cards won't have a light cache at all)
  923. if ( lightTris->lightingCache ) {
  924. vertexCache.Touch( lightTris->lightingCache );
  925. }
  926. if ( !lightTris->indexCache && r_useIndexBuffers.GetBool() ) {
  927. vertexCache.Alloc( lightTris->indexes, lightTris->numIndexes * sizeof( lightTris->indexes[0] ), &lightTris->indexCache, true );
  928. }
  929. if ( lightTris->indexCache ) {
  930. vertexCache.Touch( lightTris->indexCache );
  931. }
  932. // add the surface to the light list
  933. const idMaterial *shader = sint->shader;
  934. R_GlobalShaderOverride( &shader );
  935. // there will only be localSurfaces if the light casts shadows and
  936. // there are surfaces with NOSELFSHADOW
  937. if ( sint->shader->Coverage() == MC_TRANSLUCENT ) {
  938. R_LinkLightSurf( &vLight->translucentInteractions, lightTris,
  939. vEntity, lightDef, shader, lightScissor, false );
  940. } else if ( !lightDef->parms.noShadows && sint->shader->TestMaterialFlag(MF_NOSELFSHADOW) ) {
  941. R_LinkLightSurf( &vLight->localInteractions, lightTris,
  942. vEntity, lightDef, shader, lightScissor, false );
  943. } else {
  944. R_LinkLightSurf( &vLight->globalInteractions, lightTris,
  945. vEntity, lightDef, shader, lightScissor, false );
  946. }
  947. }
  948. }
  949. }
  950. srfTriangles_t *shadowTris = sint->shadowTris;
  951. // the shadows will always have to be added, unless we can tell they
  952. // are from a surface in an unconnected area
  953. if ( shadowTris ) {
  954. // check for view specific shadow suppression (player shadows, etc)
  955. if ( !r_skipSuppress.GetBool() ) {
  956. if ( entityDef->parms.suppressShadowInViewID &&
  957. entityDef->parms.suppressShadowInViewID == tr.viewDef->renderView.viewID ) {
  958. continue;
  959. }
  960. if ( entityDef->parms.suppressShadowInLightID &&
  961. entityDef->parms.suppressShadowInLightID == lightDef->parms.lightId ) {
  962. continue;
  963. }
  964. }
  965. // cull static shadows that have a non-empty bounds
  966. // dynamic shadows that use the turboshadow code will not have valid
  967. // bounds, because the perspective projection extends them to infinity
  968. if ( r_useShadowCulling.GetBool() && !shadowTris->bounds.IsCleared() ) {
  969. if ( R_CullLocalBox( shadowTris->bounds, vEntity->modelMatrix, 5, tr.viewDef->frustum ) ) {
  970. continue;
  971. }
  972. }
  973. // copy the shadow vertexes to the vertex cache if they have been purged
  974. // if we are using shared shadowVertexes and letting a vertex program fix them up,
  975. // get the shadowCache from the parent ambient surface
  976. if ( !shadowTris->shadowVertexes ) {
  977. // the data may have been purged, so get the latest from the "home position"
  978. shadowTris->shadowCache = sint->ambientTris->shadowCache;
  979. }
  980. // if we have been purged, re-upload the shadowVertexes
  981. if ( !shadowTris->shadowCache ) {
  982. if ( shadowTris->shadowVertexes ) {
  983. // each interaction has unique vertexes
  984. R_CreatePrivateShadowCache( shadowTris );
  985. } else {
  986. R_CreateVertexProgramShadowCache( sint->ambientTris );
  987. shadowTris->shadowCache = sint->ambientTris->shadowCache;
  988. }
  989. // if we are out of vertex cache space, skip the interaction
  990. if ( !shadowTris->shadowCache ) {
  991. continue;
  992. }
  993. }
  994. // touch the shadow surface so it won't get purged
  995. vertexCache.Touch( shadowTris->shadowCache );
  996. if ( !shadowTris->indexCache && r_useIndexBuffers.GetBool() ) {
  997. vertexCache.Alloc( shadowTris->indexes, shadowTris->numIndexes * sizeof( shadowTris->indexes[0] ), &shadowTris->indexCache, true );
  998. vertexCache.Touch( shadowTris->indexCache );
  999. }
  1000. // see if we can avoid using the shadow volume caps
  1001. bool inside = R_PotentiallyInsideInfiniteShadow( sint->ambientTris, localViewOrigin, localLightOrigin );
  1002. if ( sint->shader->TestMaterialFlag( MF_NOSELFSHADOW ) ) {
  1003. R_LinkLightSurf( &vLight->localShadows,
  1004. shadowTris, vEntity, lightDef, NULL, shadowScissor, inside );
  1005. } else {
  1006. R_LinkLightSurf( &vLight->globalShadows,
  1007. shadowTris, vEntity, lightDef, NULL, shadowScissor, inside );
  1008. }
  1009. }
  1010. }
  1011. }
  1012. /*
  1013. ===================
  1014. R_ShowInteractionMemory_f
  1015. ===================
  1016. */
  1017. void R_ShowInteractionMemory_f( const idCmdArgs &args ) {
  1018. int total = 0;
  1019. int entities = 0;
  1020. int interactions = 0;
  1021. int deferredInteractions = 0;
  1022. int emptyInteractions = 0;
  1023. int lightTris = 0;
  1024. int lightTriVerts = 0;
  1025. int lightTriIndexes = 0;
  1026. int shadowTris = 0;
  1027. int shadowTriVerts = 0;
  1028. int shadowTriIndexes = 0;
  1029. for ( int i = 0; i < tr.primaryWorld->entityDefs.Num(); i++ ) {
  1030. idRenderEntityLocal *def = tr.primaryWorld->entityDefs[i];
  1031. if ( !def ) {
  1032. continue;
  1033. }
  1034. if ( def->firstInteraction == NULL ) {
  1035. continue;
  1036. }
  1037. entities++;
  1038. for ( idInteraction *inter = def->firstInteraction; inter != NULL; inter = inter->entityNext ) {
  1039. interactions++;
  1040. total += inter->MemoryUsed();
  1041. if ( inter->IsDeferred() ) {
  1042. deferredInteractions++;
  1043. continue;
  1044. }
  1045. if ( inter->IsEmpty() ) {
  1046. emptyInteractions++;
  1047. continue;
  1048. }
  1049. for ( int j = 0; j < inter->numSurfaces; j++ ) {
  1050. surfaceInteraction_t *srf = &inter->surfaces[j];
  1051. if ( srf->lightTris && srf->lightTris != LIGHT_TRIS_DEFERRED ) {
  1052. lightTris++;
  1053. lightTriVerts += srf->lightTris->numVerts;
  1054. lightTriIndexes += srf->lightTris->numIndexes;
  1055. }
  1056. if ( srf->shadowTris ) {
  1057. shadowTris++;
  1058. shadowTriVerts += srf->shadowTris->numVerts;
  1059. shadowTriIndexes += srf->shadowTris->numIndexes;
  1060. }
  1061. }
  1062. }
  1063. }
  1064. common->Printf( "%i entities with %i total interactions totalling %ik\n", entities, interactions, total / 1024 );
  1065. common->Printf( "%i deferred interactions, %i empty interactions\n", deferredInteractions, emptyInteractions );
  1066. common->Printf( "%5i indexes %5i verts in %5i light tris\n", lightTriIndexes, lightTriVerts, lightTris );
  1067. common->Printf( "%5i indexes %5i verts in %5i shadow tris\n", shadowTriIndexes, shadowTriVerts, shadowTris );
  1068. }