RenderSystem.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. idRenderSystemLocal tr;
  24. idRenderSystem *renderSystem = &tr;
  25. /*
  26. =====================
  27. R_PerformanceCounters
  28. This prints both front and back end counters, so it should
  29. only be called when the back end thread is idle.
  30. =====================
  31. */
  32. static void R_PerformanceCounters( void ) {
  33. if ( r_showPrimitives.GetInteger() != 0 ) {
  34. float megaBytes = globalImages->SumOfUsedImages() / ( 1024*1024.0 );
  35. if ( r_showPrimitives.GetInteger() > 1 ) {
  36. common->Printf( "v:%i ds:%i t:%i/%i v:%i/%i st:%i sv:%i image:%5.1f MB\n",
  37. tr.pc.c_numViews,
  38. backEnd.pc.c_drawElements + backEnd.pc.c_shadowElements,
  39. backEnd.pc.c_drawIndexes / 3,
  40. ( backEnd.pc.c_drawIndexes - backEnd.pc.c_drawRefIndexes ) / 3,
  41. backEnd.pc.c_drawVertexes,
  42. ( backEnd.pc.c_drawVertexes - backEnd.pc.c_drawRefVertexes ),
  43. backEnd.pc.c_shadowIndexes / 3,
  44. backEnd.pc.c_shadowVertexes,
  45. megaBytes
  46. );
  47. } else {
  48. common->Printf( "views:%i draws:%i tris:%i (shdw:%i) (vbo:%i) image:%5.1f MB\n",
  49. tr.pc.c_numViews,
  50. backEnd.pc.c_drawElements + backEnd.pc.c_shadowElements,
  51. ( backEnd.pc.c_drawIndexes + backEnd.pc.c_shadowIndexes ) / 3,
  52. backEnd.pc.c_shadowIndexes / 3,
  53. backEnd.pc.c_vboIndexes / 3,
  54. megaBytes
  55. );
  56. }
  57. }
  58. if ( r_showDynamic.GetBool() ) {
  59. common->Printf( "callback:%i md5:%i dfrmVerts:%i dfrmTris:%i tangTris:%i guis:%i\n",
  60. tr.pc.c_entityDefCallbacks,
  61. tr.pc.c_generateMd5,
  62. tr.pc.c_deformedVerts,
  63. tr.pc.c_deformedIndexes/3,
  64. tr.pc.c_tangentIndexes/3,
  65. tr.pc.c_guiSurfs
  66. );
  67. }
  68. if ( r_showCull.GetBool() ) {
  69. common->Printf( "%i sin %i sclip %i sout %i bin %i bout\n",
  70. tr.pc.c_sphere_cull_in, tr.pc.c_sphere_cull_clip, tr.pc.c_sphere_cull_out,
  71. tr.pc.c_box_cull_in, tr.pc.c_box_cull_out );
  72. }
  73. if ( r_showAlloc.GetBool() ) {
  74. common->Printf( "alloc:%i free:%i\n", tr.pc.c_alloc, tr.pc.c_free );
  75. }
  76. if ( r_showInteractions.GetBool() ) {
  77. common->Printf( "createInteractions:%i createLightTris:%i createShadowVolumes:%i\n",
  78. tr.pc.c_createInteractions, tr.pc.c_createLightTris, tr.pc.c_createShadowVolumes );
  79. }
  80. if ( r_showDefs.GetBool() ) {
  81. common->Printf( "viewEntities:%i shadowEntities:%i viewLights:%i\n", tr.pc.c_visibleViewEntities,
  82. tr.pc.c_shadowViewEntities, tr.pc.c_viewLights );
  83. }
  84. if ( r_showUpdates.GetBool() ) {
  85. common->Printf( "entityUpdates:%i entityRefs:%i lightUpdates:%i lightRefs:%i\n",
  86. tr.pc.c_entityUpdates, tr.pc.c_entityReferences,
  87. tr.pc.c_lightUpdates, tr.pc.c_lightReferences );
  88. }
  89. if ( r_showMemory.GetBool() ) {
  90. int m1 = frameData ? frameData->memoryHighwater : 0;
  91. common->Printf( "frameData: %i (%i)\n", R_CountFrameData(), m1 );
  92. }
  93. if ( r_showLightScale.GetBool() ) {
  94. common->Printf( "lightScale: %f\n", backEnd.pc.maxLightValue );
  95. }
  96. memset( &tr.pc, 0, sizeof( tr.pc ) );
  97. memset( &backEnd.pc, 0, sizeof( backEnd.pc ) );
  98. }
  99. /*
  100. ====================
  101. R_IssueRenderCommands
  102. Called by R_EndFrame each frame
  103. ====================
  104. */
  105. static void R_IssueRenderCommands( void ) {
  106. if ( frameData->cmdHead->commandId == RC_NOP
  107. && !frameData->cmdHead->next ) {
  108. // nothing to issue
  109. return;
  110. }
  111. // r_skipBackEnd allows the entire time of the back end
  112. // to be removed from performance measurements, although
  113. // nothing will be drawn to the screen. If the prints
  114. // are going to a file, or r_skipBackEnd is later disabled,
  115. // usefull data can be received.
  116. // r_skipRender is usually more usefull, because it will still
  117. // draw 2D graphics
  118. if ( !r_skipBackEnd.GetBool() ) {
  119. RB_ExecuteBackEndCommands( frameData->cmdHead );
  120. }
  121. R_ClearCommandChain();
  122. }
  123. /*
  124. ============
  125. R_GetCommandBuffer
  126. Returns memory for a command buffer (stretchPicCommand_t,
  127. drawSurfsCommand_t, etc) and links it to the end of the
  128. current command chain.
  129. ============
  130. */
  131. void *R_GetCommandBuffer( int bytes ) {
  132. emptyCommand_t *cmd;
  133. cmd = (emptyCommand_t *)R_FrameAlloc( bytes );
  134. cmd->next = NULL;
  135. frameData->cmdTail->next = &cmd->commandId;
  136. frameData->cmdTail = cmd;
  137. return (void *)cmd;
  138. }
  139. /*
  140. ====================
  141. R_ClearCommandChain
  142. Called after every buffer submission
  143. and by R_ToggleSmpFrame
  144. ====================
  145. */
  146. void R_ClearCommandChain( void ) {
  147. // clear the command chain
  148. frameData->cmdHead = frameData->cmdTail = (emptyCommand_t *)R_FrameAlloc( sizeof( *frameData->cmdHead ) );
  149. frameData->cmdHead->commandId = RC_NOP;
  150. frameData->cmdHead->next = NULL;
  151. }
  152. /*
  153. =================
  154. R_ViewStatistics
  155. =================
  156. */
  157. static void R_ViewStatistics( viewDef_t *parms ) {
  158. // report statistics about this view
  159. if ( !r_showSurfaces.GetBool() ) {
  160. return;
  161. }
  162. common->Printf( "view:%p surfs:%i\n", parms, parms->numDrawSurfs );
  163. }
  164. /*
  165. =============
  166. R_AddDrawViewCmd
  167. This is the main 3D rendering command. A single scene may
  168. have multiple views if a mirror, portal, or dynamic texture is present.
  169. =============
  170. */
  171. void R_AddDrawViewCmd( viewDef_t *parms ) {
  172. drawSurfsCommand_t *cmd;
  173. cmd = (drawSurfsCommand_t *)R_GetCommandBuffer( sizeof( *cmd ) );
  174. cmd->commandId = RC_DRAW_VIEW;
  175. cmd->viewDef = parms;
  176. if ( parms->viewEntitys ) {
  177. // save the command for r_lockSurfaces debugging
  178. tr.lockSurfacesCmd = *cmd;
  179. }
  180. tr.pc.c_numViews++;
  181. R_ViewStatistics( parms );
  182. }
  183. //=================================================================================
  184. /*
  185. ======================
  186. R_LockSurfaceScene
  187. r_lockSurfaces allows a developer to move around
  188. without changing the composition of the scene, including
  189. culling. The only thing that is modified is the
  190. view position and axis, no front end work is done at all
  191. Add the stored off command again, so the new rendering will use EXACTLY
  192. the same surfaces, including all the culling, even though the transformation
  193. matricies have been changed. This allow the culling tightness to be
  194. evaluated interactively.
  195. ======================
  196. */
  197. void R_LockSurfaceScene( viewDef_t *parms ) {
  198. drawSurfsCommand_t *cmd;
  199. viewEntity_t *vModel;
  200. // set the matrix for world space to eye space
  201. R_SetViewMatrix( parms );
  202. tr.lockSurfacesCmd.viewDef->worldSpace = parms->worldSpace;
  203. // update the view origin and axis, and all
  204. // the entity matricies
  205. for( vModel = tr.lockSurfacesCmd.viewDef->viewEntitys ; vModel ; vModel = vModel->next ) {
  206. myGlMultMatrix( vModel->modelMatrix,
  207. tr.lockSurfacesCmd.viewDef->worldSpace.modelViewMatrix,
  208. vModel->modelViewMatrix );
  209. }
  210. // add the stored off surface commands again
  211. cmd = (drawSurfsCommand_t *)R_GetCommandBuffer( sizeof( *cmd ) );
  212. *cmd = tr.lockSurfacesCmd;
  213. }
  214. /*
  215. =============
  216. R_CheckCvars
  217. See if some cvars that we watch have changed
  218. =============
  219. */
  220. static void R_CheckCvars( void ) {
  221. globalImages->CheckCvars();
  222. // gamma stuff
  223. if ( r_gamma.IsModified() || r_brightness.IsModified() ) {
  224. r_gamma.ClearModified();
  225. r_brightness.ClearModified();
  226. R_SetColorMappings();
  227. }
  228. // check for changes to logging state
  229. GLimp_EnableLogging( r_logFile.GetInteger() != 0 );
  230. }
  231. /*
  232. =============
  233. idRenderSystemLocal::idRenderSystemLocal
  234. =============
  235. */
  236. idRenderSystemLocal::idRenderSystemLocal( void ) {
  237. Clear();
  238. }
  239. /*
  240. =============
  241. idRenderSystemLocal::~idRenderSystemLocal
  242. =============
  243. */
  244. idRenderSystemLocal::~idRenderSystemLocal( void ) {
  245. }
  246. /*
  247. =============
  248. SetColor
  249. This can be used to pass general information to the current material, not
  250. just colors
  251. =============
  252. */
  253. void idRenderSystemLocal::SetColor( const idVec4 &rgba ) {
  254. guiModel->SetColor( rgba[0], rgba[1], rgba[2], rgba[3] );
  255. }
  256. /*
  257. =============
  258. SetColor4
  259. =============
  260. */
  261. void idRenderSystemLocal::SetColor4( float r, float g, float b, float a ) {
  262. guiModel->SetColor( r, g, b, a );
  263. }
  264. /*
  265. =============
  266. DrawStretchPic
  267. =============
  268. */
  269. void idRenderSystemLocal::DrawStretchPic( const idDrawVert *verts, const glIndex_t *indexes, int vertCount, int indexCount, const idMaterial *material,
  270. bool clip, float min_x, float min_y, float max_x, float max_y ) {
  271. guiModel->DrawStretchPic( verts, indexes, vertCount, indexCount, material,
  272. clip, min_x, min_y, max_x, max_y );
  273. }
  274. /*
  275. =============
  276. DrawStretchPic
  277. x/y/w/h are in the 0,0 to 640,480 range
  278. =============
  279. */
  280. void idRenderSystemLocal::DrawStretchPic( float x, float y, float w, float h, float s1, float t1, float s2, float t2, const idMaterial *material ) {
  281. guiModel->DrawStretchPic( x, y, w, h, s1, t1, s2, t2, material );
  282. }
  283. /*
  284. =============
  285. DrawStretchTri
  286. x/y/w/h are in the 0,0 to 640,480 range
  287. =============
  288. */
  289. void idRenderSystemLocal::DrawStretchTri( idVec2 p1, idVec2 p2, idVec2 p3, idVec2 t1, idVec2 t2, idVec2 t3, const idMaterial *material ) {
  290. tr.guiModel->DrawStretchTri( p1, p2, p3, t1, t2, t3, material );
  291. }
  292. /*
  293. =============
  294. GlobalToNormalizedDeviceCoordinates
  295. =============
  296. */
  297. void idRenderSystemLocal::GlobalToNormalizedDeviceCoordinates( const idVec3 &global, idVec3 &ndc ) {
  298. R_GlobalToNormalizedDeviceCoordinates( global, ndc );
  299. }
  300. /*
  301. =============
  302. GlobalToNormalizedDeviceCoordinates
  303. =============
  304. */
  305. void idRenderSystemLocal::GetGLSettings( int& width, int& height ) {
  306. width = glConfig.vidWidth;
  307. height = glConfig.vidHeight;
  308. }
  309. /*
  310. =====================
  311. idRenderSystemLocal::DrawSmallChar
  312. small chars are drawn at native screen resolution
  313. =====================
  314. */
  315. void idRenderSystemLocal::DrawSmallChar( int x, int y, int ch, const idMaterial *material ) {
  316. int row, col;
  317. float frow, fcol;
  318. float size;
  319. ch &= 255;
  320. if ( ch == ' ' ) {
  321. return;
  322. }
  323. if ( y < -SMALLCHAR_HEIGHT ) {
  324. return;
  325. }
  326. row = ch >> 4;
  327. col = ch & 15;
  328. frow = row * 0.0625f;
  329. fcol = col * 0.0625f;
  330. size = 0.0625f;
  331. DrawStretchPic( x, y, SMALLCHAR_WIDTH, SMALLCHAR_HEIGHT,
  332. fcol, frow,
  333. fcol + size, frow + size,
  334. material );
  335. }
  336. /*
  337. ==================
  338. idRenderSystemLocal::DrawSmallString[Color]
  339. Draws a multi-colored string with a drop shadow, optionally forcing
  340. to a fixed color.
  341. Coordinates are at 640 by 480 virtual resolution
  342. ==================
  343. */
  344. void idRenderSystemLocal::DrawSmallStringExt( int x, int y, const char *string, const idVec4 &setColor, bool forceColor, const idMaterial *material ) {
  345. idVec4 color;
  346. const unsigned char *s;
  347. int xx;
  348. // draw the colored text
  349. s = (const unsigned char*)string;
  350. xx = x;
  351. SetColor( setColor );
  352. while ( *s ) {
  353. if ( idStr::IsColor( (const char*)s ) ) {
  354. if ( !forceColor ) {
  355. if ( *(s+1) == C_COLOR_DEFAULT ) {
  356. SetColor( setColor );
  357. } else {
  358. color = idStr::ColorForIndex( *(s+1) );
  359. color[3] = setColor[3];
  360. SetColor( color );
  361. }
  362. }
  363. s += 2;
  364. continue;
  365. }
  366. DrawSmallChar( xx, y, *s, material );
  367. xx += SMALLCHAR_WIDTH;
  368. s++;
  369. }
  370. SetColor( colorWhite );
  371. }
  372. /*
  373. =====================
  374. idRenderSystemLocal::DrawBigChar
  375. =====================
  376. */
  377. void idRenderSystemLocal::DrawBigChar( int x, int y, int ch, const idMaterial *material ) {
  378. int row, col;
  379. float frow, fcol;
  380. float size;
  381. ch &= 255;
  382. if ( ch == ' ' ) {
  383. return;
  384. }
  385. if ( y < -BIGCHAR_HEIGHT ) {
  386. return;
  387. }
  388. row = ch >> 4;
  389. col = ch & 15;
  390. frow = row * 0.0625f;
  391. fcol = col * 0.0625f;
  392. size = 0.0625f;
  393. DrawStretchPic( x, y, BIGCHAR_WIDTH, BIGCHAR_HEIGHT,
  394. fcol, frow,
  395. fcol + size, frow + size,
  396. material );
  397. }
  398. /*
  399. ==================
  400. idRenderSystemLocal::DrawBigString[Color]
  401. Draws a multi-colored string with a drop shadow, optionally forcing
  402. to a fixed color.
  403. Coordinates are at 640 by 480 virtual resolution
  404. ==================
  405. */
  406. void idRenderSystemLocal::DrawBigStringExt( int x, int y, const char *string, const idVec4 &setColor, bool forceColor, const idMaterial *material ) {
  407. idVec4 color;
  408. const char *s;
  409. int xx;
  410. // draw the colored text
  411. s = string;
  412. xx = x;
  413. SetColor( setColor );
  414. while ( *s ) {
  415. if ( idStr::IsColor( s ) ) {
  416. if ( !forceColor ) {
  417. if ( *(s+1) == C_COLOR_DEFAULT ) {
  418. SetColor( setColor );
  419. } else {
  420. color = idStr::ColorForIndex( *(s+1) );
  421. color[3] = setColor[3];
  422. SetColor( color );
  423. }
  424. }
  425. s += 2;
  426. continue;
  427. }
  428. DrawBigChar( xx, y, *s, material );
  429. xx += BIGCHAR_WIDTH;
  430. s++;
  431. }
  432. SetColor( colorWhite );
  433. }
  434. //======================================================================================
  435. /*
  436. ==================
  437. SetBackEndRenderer
  438. Check for changes in the back end renderSystem, possibly invalidating cached data
  439. ==================
  440. */
  441. void idRenderSystemLocal::SetBackEndRenderer() {
  442. if ( !r_renderer.IsModified() ) {
  443. return;
  444. }
  445. bool oldVPstate = backEndRendererHasVertexPrograms;
  446. backEndRenderer = BE_BAD;
  447. if ( idStr::Icmp( r_renderer.GetString(), "arb" ) == 0 ) {
  448. backEndRenderer = BE_ARB;
  449. } else if ( idStr::Icmp( r_renderer.GetString(), "arb2" ) == 0 ) {
  450. if ( glConfig.allowARB2Path ) {
  451. backEndRenderer = BE_ARB2;
  452. }
  453. } else if ( idStr::Icmp( r_renderer.GetString(), "nv10" ) == 0 ) {
  454. if ( glConfig.allowNV10Path ) {
  455. backEndRenderer = BE_NV10;
  456. }
  457. } else if ( idStr::Icmp( r_renderer.GetString(), "nv20" ) == 0 ) {
  458. if ( glConfig.allowNV20Path ) {
  459. backEndRenderer = BE_NV20;
  460. }
  461. } else if ( idStr::Icmp( r_renderer.GetString(), "r200" ) == 0 ) {
  462. if ( glConfig.allowR200Path ) {
  463. backEndRenderer = BE_R200;
  464. }
  465. }
  466. // fallback
  467. if ( backEndRenderer == BE_BAD ) {
  468. // choose the best
  469. if ( glConfig.allowARB2Path ) {
  470. backEndRenderer = BE_ARB2;
  471. } else if ( glConfig.allowR200Path ) {
  472. backEndRenderer = BE_R200;
  473. } else if ( glConfig.allowNV20Path ) {
  474. backEndRenderer = BE_NV20;
  475. } else if ( glConfig.allowNV10Path ) {
  476. backEndRenderer = BE_NV10;
  477. } else {
  478. // the others are considered experimental
  479. backEndRenderer = BE_ARB;
  480. }
  481. }
  482. backEndRendererHasVertexPrograms = false;
  483. backEndRendererMaxLight = 1.0;
  484. switch( backEndRenderer ) {
  485. case BE_ARB:
  486. common->Printf( "using ARB renderSystem\n" );
  487. break;
  488. case BE_NV10:
  489. common->Printf( "using NV10 renderSystem\n" );
  490. break;
  491. case BE_NV20:
  492. common->Printf( "using NV20 renderSystem\n" );
  493. backEndRendererHasVertexPrograms = true;
  494. break;
  495. case BE_R200:
  496. common->Printf( "using R200 renderSystem\n" );
  497. backEndRendererHasVertexPrograms = true;
  498. break;
  499. case BE_ARB2:
  500. common->Printf( "using ARB2 renderSystem\n" );
  501. backEndRendererHasVertexPrograms = true;
  502. backEndRendererMaxLight = 999;
  503. break;
  504. default:
  505. common->FatalError( "SetbackEndRenderer: bad back end" );
  506. }
  507. // clear the vertex cache if we are changing between
  508. // using vertex programs and not, because specular and
  509. // shadows will be different data
  510. if ( oldVPstate != backEndRendererHasVertexPrograms ) {
  511. vertexCache.PurgeAll();
  512. if ( primaryWorld ) {
  513. primaryWorld->FreeInteractions();
  514. }
  515. }
  516. r_renderer.ClearModified();
  517. }
  518. /*
  519. ====================
  520. BeginFrame
  521. ====================
  522. */
  523. void idRenderSystemLocal::BeginFrame( int windowWidth, int windowHeight ) {
  524. setBufferCommand_t *cmd;
  525. if ( !glConfig.isInitialized ) {
  526. return;
  527. }
  528. // determine which back end we will use
  529. SetBackEndRenderer();
  530. guiModel->Clear();
  531. // for the larger-than-window tiled rendering screenshots
  532. if ( tiledViewport[0] ) {
  533. windowWidth = tiledViewport[0];
  534. windowHeight = tiledViewport[1];
  535. }
  536. glConfig.vidWidth = windowWidth;
  537. glConfig.vidHeight = windowHeight;
  538. renderCrops[0].x = 0;
  539. renderCrops[0].y = 0;
  540. renderCrops[0].width = windowWidth;
  541. renderCrops[0].height = windowHeight;
  542. currentRenderCrop = 0;
  543. // screenFraction is just for quickly testing fill rate limitations
  544. if ( r_screenFraction.GetInteger() != 100 ) {
  545. int w = SCREEN_WIDTH * r_screenFraction.GetInteger() / 100.0f;
  546. int h = SCREEN_HEIGHT * r_screenFraction.GetInteger() / 100.0f;
  547. CropRenderSize( w, h );
  548. }
  549. // this is the ONLY place this is modified
  550. frameCount++;
  551. // just in case we did a common->Error while this
  552. // was set
  553. guiRecursionLevel = 0;
  554. // the first rendering will be used for commands like
  555. // screenshot, rather than a possible subsequent remote
  556. // or mirror render
  557. // primaryWorld = NULL;
  558. // set the time for shader effects in 2D rendering
  559. frameShaderTime = eventLoop->Milliseconds() * 0.001;
  560. //
  561. // draw buffer stuff
  562. //
  563. cmd = (setBufferCommand_t *)R_GetCommandBuffer( sizeof( *cmd ) );
  564. cmd->commandId = RC_SET_BUFFER;
  565. cmd->frameCount = frameCount;
  566. if ( r_frontBuffer.GetBool() ) {
  567. cmd->buffer = (int)GL_FRONT;
  568. } else {
  569. cmd->buffer = (int)GL_BACK;
  570. }
  571. }
  572. void idRenderSystemLocal::WriteDemoPics() {
  573. session->writeDemo->WriteInt( DS_RENDER );
  574. session->writeDemo->WriteInt( DC_GUI_MODEL );
  575. guiModel->WriteToDemo( session->writeDemo );
  576. }
  577. void idRenderSystemLocal::DrawDemoPics() {
  578. demoGuiModel->EmitFullScreen();
  579. }
  580. /*
  581. =============
  582. EndFrame
  583. Returns the number of msec spent in the back end
  584. =============
  585. */
  586. void idRenderSystemLocal::EndFrame( int *frontEndMsec, int *backEndMsec ) {
  587. emptyCommand_t *cmd;
  588. if ( !glConfig.isInitialized ) {
  589. return;
  590. }
  591. // close any gui drawing
  592. guiModel->EmitFullScreen();
  593. guiModel->Clear();
  594. // save out timing information
  595. if ( frontEndMsec ) {
  596. *frontEndMsec = pc.frontEndMsec;
  597. }
  598. if ( backEndMsec ) {
  599. *backEndMsec = backEnd.pc.msec;
  600. }
  601. // print any other statistics and clear all of them
  602. R_PerformanceCounters();
  603. // check for dynamic changes that require some initialization
  604. R_CheckCvars();
  605. // check for errors
  606. GL_CheckErrors();
  607. // add the swapbuffers command
  608. cmd = (emptyCommand_t *)R_GetCommandBuffer( sizeof( *cmd ) );
  609. cmd->commandId = RC_SWAP_BUFFERS;
  610. // start the back end up again with the new command list
  611. R_IssueRenderCommands();
  612. // use the other buffers next frame, because another CPU
  613. // may still be rendering into the current buffers
  614. R_ToggleSmpFrame();
  615. // we can now release the vertexes used this frame
  616. vertexCache.EndFrame();
  617. if ( session->writeDemo ) {
  618. session->writeDemo->WriteInt( DS_RENDER );
  619. session->writeDemo->WriteInt( DC_END_FRAME );
  620. if ( r_showDemo.GetBool() ) {
  621. common->Printf( "write DC_END_FRAME\n" );
  622. }
  623. }
  624. }
  625. /*
  626. =====================
  627. RenderViewToViewport
  628. Converts from SCREEN_WIDTH / SCREEN_HEIGHT coordinates to current cropped pixel coordinates
  629. =====================
  630. */
  631. void idRenderSystemLocal::RenderViewToViewport( const renderView_t *renderView, idScreenRect *viewport ) {
  632. renderCrop_t *rc = &renderCrops[currentRenderCrop];
  633. float wRatio = (float)rc->width / SCREEN_WIDTH;
  634. float hRatio = (float)rc->height / SCREEN_HEIGHT;
  635. viewport->x1 = idMath::Ftoi( rc->x + renderView->x * wRatio );
  636. viewport->x2 = idMath::Ftoi( rc->x + floor( ( renderView->x + renderView->width ) * wRatio + 0.5f ) - 1 );
  637. viewport->y1 = idMath::Ftoi( ( rc->y + rc->height ) - floor( ( renderView->y + renderView->height ) * hRatio + 0.5f ) );
  638. viewport->y2 = idMath::Ftoi( ( rc->y + rc->height ) - floor( renderView->y * hRatio + 0.5f ) - 1 );
  639. }
  640. static int RoundDownToPowerOfTwo( int v ) {
  641. int i;
  642. for ( i = 0 ; i < 20 ; i++ ) {
  643. if ( ( 1 << i ) == v ) {
  644. return v;
  645. }
  646. if ( ( 1 << i ) > v ) {
  647. return 1 << ( i-1 );
  648. }
  649. }
  650. return 1<<i;
  651. }
  652. /*
  653. ================
  654. CropRenderSize
  655. This automatically halves sizes until it fits in the current window size,
  656. so if you specify a power of two size for a texture copy, it may be shrunk
  657. down, but still valid.
  658. ================
  659. */
  660. void idRenderSystemLocal::CropRenderSize( int width, int height, bool makePowerOfTwo, bool forceDimensions ) {
  661. if ( !glConfig.isInitialized ) {
  662. return;
  663. }
  664. // close any gui drawing before changing the size
  665. guiModel->EmitFullScreen();
  666. guiModel->Clear();
  667. if ( width < 1 || height < 1 ) {
  668. common->Error( "CropRenderSize: bad sizes" );
  669. }
  670. if ( session->writeDemo ) {
  671. session->writeDemo->WriteInt( DS_RENDER );
  672. session->writeDemo->WriteInt( DC_CROP_RENDER );
  673. session->writeDemo->WriteInt( width );
  674. session->writeDemo->WriteInt( height );
  675. session->writeDemo->WriteInt( makePowerOfTwo );
  676. if ( r_showDemo.GetBool() ) {
  677. common->Printf( "write DC_CROP_RENDER\n" );
  678. }
  679. }
  680. // convert from virtual SCREEN_WIDTH/SCREEN_HEIGHT coordinates to physical OpenGL pixels
  681. renderView_t renderView;
  682. renderView.x = 0;
  683. renderView.y = 0;
  684. renderView.width = width;
  685. renderView.height = height;
  686. idScreenRect r;
  687. RenderViewToViewport( &renderView, &r );
  688. width = r.x2 - r.x1 + 1;
  689. height = r.y2 - r.y1 + 1;
  690. if ( forceDimensions ) {
  691. // just give exactly what we ask for
  692. width = renderView.width;
  693. height = renderView.height;
  694. }
  695. // if makePowerOfTwo, drop to next lower power of two after scaling to physical pixels
  696. if ( makePowerOfTwo ) {
  697. width = RoundDownToPowerOfTwo( width );
  698. height = RoundDownToPowerOfTwo( height );
  699. // FIXME: megascreenshots with offset viewports don't work right with this yet
  700. }
  701. renderCrop_t *rc = &renderCrops[currentRenderCrop];
  702. // we might want to clip these to the crop window instead
  703. while ( width > glConfig.vidWidth ) {
  704. width >>= 1;
  705. }
  706. while ( height > glConfig.vidHeight ) {
  707. height >>= 1;
  708. }
  709. if ( currentRenderCrop == MAX_RENDER_CROPS ) {
  710. common->Error( "idRenderSystemLocal::CropRenderSize: currentRenderCrop == MAX_RENDER_CROPS" );
  711. }
  712. currentRenderCrop++;
  713. rc = &renderCrops[currentRenderCrop];
  714. rc->x = 0;
  715. rc->y = 0;
  716. rc->width = width;
  717. rc->height = height;
  718. }
  719. /*
  720. ================
  721. UnCrop
  722. ================
  723. */
  724. void idRenderSystemLocal::UnCrop() {
  725. if ( !glConfig.isInitialized ) {
  726. return;
  727. }
  728. if ( currentRenderCrop < 1 ) {
  729. common->Error( "idRenderSystemLocal::UnCrop: currentRenderCrop < 1" );
  730. }
  731. // close any gui drawing
  732. guiModel->EmitFullScreen();
  733. guiModel->Clear();
  734. currentRenderCrop--;
  735. if ( session->writeDemo ) {
  736. session->writeDemo->WriteInt( DS_RENDER );
  737. session->writeDemo->WriteInt( DC_UNCROP_RENDER );
  738. if ( r_showDemo.GetBool() ) {
  739. common->Printf( "write DC_UNCROP\n" );
  740. }
  741. }
  742. }
  743. /*
  744. ================
  745. CaptureRenderToImage
  746. ================
  747. */
  748. void idRenderSystemLocal::CaptureRenderToImage( const char *imageName ) {
  749. if ( !glConfig.isInitialized ) {
  750. return;
  751. }
  752. guiModel->EmitFullScreen();
  753. guiModel->Clear();
  754. if ( session->writeDemo ) {
  755. session->writeDemo->WriteInt( DS_RENDER );
  756. session->writeDemo->WriteInt( DC_CAPTURE_RENDER );
  757. session->writeDemo->WriteHashString( imageName );
  758. if ( r_showDemo.GetBool() ) {
  759. common->Printf( "write DC_CAPTURE_RENDER: %s\n", imageName );
  760. }
  761. }
  762. // look up the image before we create the render command, because it
  763. // may need to sync to create the image
  764. idImage *image = globalImages->ImageFromFile(imageName, TF_DEFAULT, true, TR_REPEAT, TD_DEFAULT);
  765. renderCrop_t *rc = &renderCrops[currentRenderCrop];
  766. copyRenderCommand_t *cmd = (copyRenderCommand_t *)R_GetCommandBuffer( sizeof( *cmd ) );
  767. cmd->commandId = RC_COPY_RENDER;
  768. cmd->x = rc->x;
  769. cmd->y = rc->y;
  770. cmd->imageWidth = rc->width;
  771. cmd->imageHeight = rc->height;
  772. cmd->image = image;
  773. guiModel->Clear();
  774. }
  775. /*
  776. ==============
  777. CaptureRenderToFile
  778. ==============
  779. */
  780. void idRenderSystemLocal::CaptureRenderToFile( const char *fileName, bool fixAlpha ) {
  781. if ( !glConfig.isInitialized ) {
  782. return;
  783. }
  784. renderCrop_t *rc = &renderCrops[currentRenderCrop];
  785. guiModel->EmitFullScreen();
  786. guiModel->Clear();
  787. R_IssueRenderCommands();
  788. qglReadBuffer( GL_BACK );
  789. // include extra space for OpenGL padding to word boundaries
  790. int c = ( rc->width + 3 ) * rc->height;
  791. byte *data = (byte *)R_StaticAlloc( c * 3 );
  792. qglReadPixels( rc->x, rc->y, rc->width, rc->height, GL_RGB, GL_UNSIGNED_BYTE, data );
  793. byte *data2 = (byte *)R_StaticAlloc( c * 4 );
  794. for ( int i = 0 ; i < c ; i++ ) {
  795. data2[ i * 4 ] = data[ i * 3 ];
  796. data2[ i * 4 + 1 ] = data[ i * 3 + 1 ];
  797. data2[ i * 4 + 2 ] = data[ i * 3 + 2 ];
  798. data2[ i * 4 + 3 ] = 0xff;
  799. }
  800. R_WriteTGA( fileName, data2, rc->width, rc->height, true );
  801. R_StaticFree( data );
  802. R_StaticFree( data2 );
  803. }
  804. /*
  805. ==============
  806. AllocRenderWorld
  807. ==============
  808. */
  809. idRenderWorld *idRenderSystemLocal::AllocRenderWorld() {
  810. idRenderWorldLocal *rw;
  811. rw = new idRenderWorldLocal;
  812. worlds.Append( rw );
  813. return rw;
  814. }
  815. /*
  816. ==============
  817. FreeRenderWorld
  818. ==============
  819. */
  820. void idRenderSystemLocal::FreeRenderWorld( idRenderWorld *rw ) {
  821. if ( primaryWorld == rw ) {
  822. primaryWorld = NULL;
  823. }
  824. worlds.Remove( static_cast<idRenderWorldLocal *>(rw) );
  825. delete rw;
  826. }
  827. /*
  828. ==============
  829. PrintMemInfo
  830. ==============
  831. */
  832. void idRenderSystemLocal::PrintMemInfo( MemInfo_t *mi ) {
  833. // sum up image totals
  834. globalImages->PrintMemInfo( mi );
  835. // sum up model totals
  836. renderModelManager->PrintMemInfo( mi );
  837. // compute render totals
  838. }
  839. /*
  840. ===============
  841. idRenderSystemLocal::UploadImage
  842. ===============
  843. */
  844. bool idRenderSystemLocal::UploadImage( const char *imageName, const byte *data, int width, int height ) {
  845. idImage *image = globalImages->GetImage( imageName );
  846. if ( !image ) {
  847. return false;
  848. }
  849. image->UploadScratch( data, width, height );
  850. image->SetImageFilterAndRepeat();
  851. return true;
  852. }