RenderSystem.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  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() {
  33. if ( r_showPrimitives.GetInteger() != 0 ) {
  34. common->Printf( "views:%i draws:%i tris:%i (shdw:%i)\n",
  35. tr.pc.c_numViews,
  36. backEnd.pc.c_drawElements + backEnd.pc.c_shadowElements,
  37. ( backEnd.pc.c_drawIndexes + backEnd.pc.c_shadowIndexes ) / 3,
  38. backEnd.pc.c_shadowIndexes / 3
  39. );
  40. }
  41. if ( r_showDynamic.GetBool() ) {
  42. common->Printf( "callback:%i md5:%i dfrmVerts:%i dfrmTris:%i tangTris:%i guis:%i\n",
  43. tr.pc.c_entityDefCallbacks,
  44. tr.pc.c_generateMd5,
  45. tr.pc.c_deformedVerts,
  46. tr.pc.c_deformedIndexes/3,
  47. tr.pc.c_tangentIndexes/3,
  48. tr.pc.c_guiSurfs
  49. );
  50. }
  51. if ( r_showCull.GetBool() ) {
  52. common->Printf( "%i box in %i box out\n",
  53. tr.pc.c_box_cull_in, tr.pc.c_box_cull_out );
  54. }
  55. if ( r_showAddModel.GetBool() ) {
  56. common->Printf( "callback:%i createInteractions:%i createShadowVolumes:%i\n",
  57. tr.pc.c_entityDefCallbacks, tr.pc.c_createInteractions, tr.pc.c_createShadowVolumes );
  58. common->Printf( "viewEntities:%i shadowEntities:%i viewLights:%i\n", tr.pc.c_visibleViewEntities,
  59. tr.pc.c_shadowViewEntities, tr.pc.c_viewLights );
  60. }
  61. if ( r_showUpdates.GetBool() ) {
  62. common->Printf( "entityUpdates:%i entityRefs:%i lightUpdates:%i lightRefs:%i\n",
  63. tr.pc.c_entityUpdates, tr.pc.c_entityReferences,
  64. tr.pc.c_lightUpdates, tr.pc.c_lightReferences );
  65. }
  66. if ( r_showMemory.GetBool() ) {
  67. common->Printf( "frameData: %i (%i)\n", frameData->frameMemoryAllocated.GetValue(), frameData->highWaterAllocated );
  68. }
  69. memset( &tr.pc, 0, sizeof( tr.pc ) );
  70. memset( &backEnd.pc, 0, sizeof( backEnd.pc ) );
  71. }
  72. /*
  73. ====================
  74. RenderCommandBuffers
  75. ====================
  76. */
  77. void idRenderSystemLocal::RenderCommandBuffers( const emptyCommand_t * const cmdHead ) {
  78. // if there isn't a draw view command, do nothing to avoid swapping a bad frame
  79. bool hasView = false;
  80. for ( const emptyCommand_t * cmd = cmdHead ; cmd ; cmd = (const emptyCommand_t *)cmd->next ) {
  81. if ( cmd->commandId == RC_DRAW_VIEW_3D || cmd->commandId == RC_DRAW_VIEW_GUI ) {
  82. hasView = true;
  83. break;
  84. }
  85. }
  86. if ( !hasView ) {
  87. return;
  88. }
  89. // r_skipBackEnd allows the entire time of the back end
  90. // to be removed from performance measurements, although
  91. // nothing will be drawn to the screen. If the prints
  92. // are going to a file, or r_skipBackEnd is later disabled,
  93. // usefull data can be received.
  94. // r_skipRender is usually more usefull, because it will still
  95. // draw 2D graphics
  96. if ( !r_skipBackEnd.GetBool() ) {
  97. if ( glConfig.timerQueryAvailable ) {
  98. if ( tr.timerQueryId == 0 ) {
  99. qglGenQueriesARB( 1, & tr.timerQueryId );
  100. }
  101. qglBeginQueryARB( GL_TIME_ELAPSED_EXT, tr.timerQueryId );
  102. RB_ExecuteBackEndCommands( cmdHead );
  103. qglEndQueryARB( GL_TIME_ELAPSED_EXT );
  104. qglFlush();
  105. } else {
  106. RB_ExecuteBackEndCommands( cmdHead );
  107. }
  108. }
  109. // pass in null for now - we may need to do some map specific hackery in the future
  110. resolutionScale.InitForMap( NULL );
  111. }
  112. /*
  113. ============
  114. R_GetCommandBuffer
  115. Returns memory for a command buffer (stretchPicCommand_t,
  116. drawSurfsCommand_t, etc) and links it to the end of the
  117. current command chain.
  118. ============
  119. */
  120. void *R_GetCommandBuffer( int bytes ) {
  121. emptyCommand_t *cmd;
  122. cmd = (emptyCommand_t *)R_FrameAlloc( bytes, FRAME_ALLOC_DRAW_COMMAND );
  123. cmd->next = NULL;
  124. frameData->cmdTail->next = &cmd->commandId;
  125. frameData->cmdTail = cmd;
  126. return (void *)cmd;
  127. }
  128. /*
  129. =================
  130. R_ViewStatistics
  131. =================
  132. */
  133. static void R_ViewStatistics( viewDef_t *parms ) {
  134. // report statistics about this view
  135. if ( !r_showSurfaces.GetBool() ) {
  136. return;
  137. }
  138. common->Printf( "view:%p surfs:%i\n", parms, parms->numDrawSurfs );
  139. }
  140. /*
  141. =============
  142. R_AddDrawViewCmd
  143. This is the main 3D rendering command. A single scene may
  144. have multiple views if a mirror, portal, or dynamic texture is present.
  145. =============
  146. */
  147. void R_AddDrawViewCmd( viewDef_t *parms, bool guiOnly ) {
  148. drawSurfsCommand_t *cmd;
  149. cmd = (drawSurfsCommand_t *)R_GetCommandBuffer( sizeof( *cmd ) );
  150. cmd->commandId = ( guiOnly ) ? RC_DRAW_VIEW_GUI : RC_DRAW_VIEW_3D;
  151. cmd->viewDef = parms;
  152. tr.pc.c_numViews++;
  153. R_ViewStatistics( parms );
  154. }
  155. /*
  156. =============
  157. R_AddPostProcess
  158. This issues the command to do a post process after all the views have
  159. been rendered.
  160. =============
  161. */
  162. void R_AddDrawPostProcess( viewDef_t * parms ) {
  163. postProcessCommand_t * cmd = (postProcessCommand_t *)R_GetCommandBuffer( sizeof( *cmd ) );
  164. cmd->commandId = RC_POST_PROCESS;
  165. cmd->viewDef = parms;
  166. }
  167. //=================================================================================
  168. /*
  169. =============
  170. R_CheckCvars
  171. See if some cvars that we watch have changed
  172. =============
  173. */
  174. static void R_CheckCvars() {
  175. // gamma stuff
  176. if ( r_gamma.IsModified() || r_brightness.IsModified() ) {
  177. r_gamma.ClearModified();
  178. r_brightness.ClearModified();
  179. R_SetColorMappings();
  180. }
  181. // filtering
  182. if ( r_maxAnisotropicFiltering.IsModified() || r_useTrilinearFiltering.IsModified() || r_lodBias.IsModified() ) {
  183. idLib::Printf( "Updating texture filter parameters.\n" );
  184. r_maxAnisotropicFiltering.ClearModified();
  185. r_useTrilinearFiltering.ClearModified();
  186. r_lodBias.ClearModified();
  187. for ( int i = 0 ; i < globalImages->images.Num() ; i++ ) {
  188. if ( globalImages->images[i] ) {
  189. globalImages->images[i]->Bind();
  190. globalImages->images[i]->SetTexParameters();
  191. }
  192. }
  193. }
  194. extern idCVar r_useSeamlessCubeMap;
  195. if ( r_useSeamlessCubeMap.IsModified() ) {
  196. r_useSeamlessCubeMap.ClearModified();
  197. if ( glConfig.seamlessCubeMapAvailable ) {
  198. if ( r_useSeamlessCubeMap.GetBool() ) {
  199. qglEnable( GL_TEXTURE_CUBE_MAP_SEAMLESS );
  200. } else {
  201. qglDisable( GL_TEXTURE_CUBE_MAP_SEAMLESS );
  202. }
  203. }
  204. }
  205. extern idCVar r_useSRGB;
  206. if ( r_useSRGB.IsModified() ) {
  207. r_useSRGB.ClearModified();
  208. if ( glConfig.sRGBFramebufferAvailable ) {
  209. if ( r_useSRGB.GetBool() ) {
  210. qglEnable( GL_FRAMEBUFFER_SRGB );
  211. } else {
  212. qglDisable( GL_FRAMEBUFFER_SRGB );
  213. }
  214. }
  215. }
  216. if ( r_multiSamples.IsModified() ) {
  217. if ( r_multiSamples.GetInteger() > 0 ) {
  218. qglEnable( GL_MULTISAMPLE_ARB );
  219. } else {
  220. qglDisable( GL_MULTISAMPLE_ARB );
  221. }
  222. }
  223. // check for changes to logging state
  224. GLimp_EnableLogging( r_logFile.GetInteger() != 0 );
  225. }
  226. /*
  227. =============
  228. idRenderSystemLocal::idRenderSystemLocal
  229. =============
  230. */
  231. idRenderSystemLocal::idRenderSystemLocal() :
  232. unitSquareTriangles( NULL ),
  233. zeroOneCubeTriangles( NULL ),
  234. testImageTriangles( NULL ) {
  235. Clear();
  236. }
  237. /*
  238. =============
  239. idRenderSystemLocal::~idRenderSystemLocal
  240. =============
  241. */
  242. idRenderSystemLocal::~idRenderSystemLocal() {
  243. }
  244. /*
  245. =============
  246. idRenderSystemLocal::SetColor
  247. =============
  248. */
  249. void idRenderSystemLocal::SetColor( const idVec4 & rgba ) {
  250. currentColorNativeBytesOrder = LittleLong( PackColor( rgba ) );
  251. }
  252. /*
  253. =============
  254. idRenderSystemLocal::GetColor
  255. =============
  256. */
  257. uint32 idRenderSystemLocal::GetColor() {
  258. return LittleLong( currentColorNativeBytesOrder );
  259. }
  260. /*
  261. =============
  262. idRenderSystemLocal::SetGLState
  263. =============
  264. */
  265. void idRenderSystemLocal::SetGLState( const uint64 glState ) {
  266. currentGLState = glState;
  267. }
  268. /*
  269. =============
  270. idRenderSystemLocal::DrawFilled
  271. =============
  272. */
  273. void idRenderSystemLocal::DrawFilled( const idVec4 & color, float x, float y, float w, float h ) {
  274. SetColor( color );
  275. DrawStretchPic( x, y, w, h, 0.0f, 0.0f, 1.0f, 1.0f, whiteMaterial );
  276. }
  277. /*
  278. =============
  279. idRenderSystemLocal::DrawStretchPic
  280. =============
  281. */
  282. void idRenderSystemLocal::DrawStretchPic( float x, float y, float w, float h, float s1, float t1, float s2, float t2, const idMaterial *material ) {
  283. DrawStretchPic( idVec4( x, y, s1, t1 ), idVec4( x+w, y, s2, t1 ), idVec4( x+w, y+h, s2, t2 ), idVec4( x, y+h, s1, t2 ), material );
  284. }
  285. /*
  286. =============
  287. idRenderSystemLocal::DrawStretchPic
  288. =============
  289. */
  290. static triIndex_t quadPicIndexes[6] = { 3, 0, 2, 2, 0, 1 };
  291. void idRenderSystemLocal::DrawStretchPic( const idVec4 & topLeft, const idVec4 & topRight, const idVec4 & bottomRight, const idVec4 & bottomLeft, const idMaterial * material ) {
  292. if ( !R_IsInitialized() ) {
  293. return;
  294. }
  295. if ( material == NULL ) {
  296. return;
  297. }
  298. idDrawVert * verts = guiModel->AllocTris( 4, quadPicIndexes, 6, material, currentGLState, STEREO_DEPTH_TYPE_NONE );
  299. if ( verts == NULL ) {
  300. return;
  301. }
  302. ALIGNTYPE16 idDrawVert localVerts[4];
  303. localVerts[0].Clear();
  304. localVerts[0].xyz[0] = topLeft.x;
  305. localVerts[0].xyz[1] = topLeft.y;
  306. localVerts[0].SetTexCoord( topLeft.z, topLeft.w );
  307. localVerts[0].SetNativeOrderColor( currentColorNativeBytesOrder );
  308. localVerts[0].ClearColor2();
  309. localVerts[1].Clear();
  310. localVerts[1].xyz[0] = topRight.x;
  311. localVerts[1].xyz[1] = topRight.y;
  312. localVerts[1].SetTexCoord( topRight.z, topRight.w );
  313. localVerts[1].SetNativeOrderColor( currentColorNativeBytesOrder );
  314. localVerts[1].ClearColor2();
  315. localVerts[2].Clear();
  316. localVerts[2].xyz[0] = bottomRight.x;
  317. localVerts[2].xyz[1] = bottomRight.y;
  318. localVerts[2].SetTexCoord( bottomRight.z, bottomRight.w );
  319. localVerts[2].SetNativeOrderColor( currentColorNativeBytesOrder );
  320. localVerts[2].ClearColor2();
  321. localVerts[3].Clear();
  322. localVerts[3].xyz[0] = bottomLeft.x;
  323. localVerts[3].xyz[1] = bottomLeft.y;
  324. localVerts[3].SetTexCoord( bottomLeft.z, bottomLeft.w );
  325. localVerts[3].SetNativeOrderColor( currentColorNativeBytesOrder );
  326. localVerts[3].ClearColor2();
  327. WriteDrawVerts16( verts, localVerts, 4 );
  328. }
  329. /*
  330. =============
  331. idRenderSystemLocal::DrawStretchTri
  332. =============
  333. */
  334. void idRenderSystemLocal::DrawStretchTri( const idVec2 & p1, const idVec2 & p2, const idVec2 & p3, const idVec2 & t1, const idVec2 & t2, const idVec2 & t3, const idMaterial *material ) {
  335. if ( !R_IsInitialized() ) {
  336. return;
  337. }
  338. if ( material == NULL ) {
  339. return;
  340. }
  341. triIndex_t tempIndexes[3] = { 1, 0, 2 };
  342. idDrawVert * verts = guiModel->AllocTris( 3, tempIndexes, 3, material, currentGLState, STEREO_DEPTH_TYPE_NONE );
  343. if ( verts == NULL ) {
  344. return;
  345. }
  346. ALIGNTYPE16 idDrawVert localVerts[3];
  347. localVerts[0].Clear();
  348. localVerts[0].xyz[0] = p1.x;
  349. localVerts[0].xyz[1] = p1.y;
  350. localVerts[0].SetTexCoord( t1 );
  351. localVerts[0].SetNativeOrderColor( currentColorNativeBytesOrder );
  352. localVerts[0].ClearColor2();
  353. localVerts[1].Clear();
  354. localVerts[1].xyz[0] = p2.x;
  355. localVerts[1].xyz[1] = p2.y;
  356. localVerts[1].SetTexCoord( t2 );
  357. localVerts[1].SetNativeOrderColor( currentColorNativeBytesOrder );
  358. localVerts[1].ClearColor2();
  359. localVerts[2].Clear();
  360. localVerts[2].xyz[0] = p3.x;
  361. localVerts[2].xyz[1] = p3.y;
  362. localVerts[2].SetTexCoord( t3 );
  363. localVerts[2].SetNativeOrderColor( currentColorNativeBytesOrder );
  364. localVerts[2].ClearColor2();
  365. WriteDrawVerts16( verts, localVerts, 3 );
  366. }
  367. /*
  368. =============
  369. idRenderSystemLocal::AllocTris
  370. =============
  371. */
  372. idDrawVert * idRenderSystemLocal::AllocTris( int numVerts, const triIndex_t * indexes, int numIndexes, const idMaterial * material, const stereoDepthType_t stereoType ) {
  373. return guiModel->AllocTris( numVerts, indexes, numIndexes, material, currentGLState, stereoType );
  374. }
  375. /*
  376. =====================
  377. idRenderSystemLocal::DrawSmallChar
  378. small chars are drawn at native screen resolution
  379. =====================
  380. */
  381. void idRenderSystemLocal::DrawSmallChar( int x, int y, int ch ) {
  382. int row, col;
  383. float frow, fcol;
  384. float size;
  385. ch &= 255;
  386. if ( ch == ' ' ) {
  387. return;
  388. }
  389. if ( y < -SMALLCHAR_HEIGHT ) {
  390. return;
  391. }
  392. row = ch >> 4;
  393. col = ch & 15;
  394. frow = row * 0.0625f;
  395. fcol = col * 0.0625f;
  396. size = 0.0625f;
  397. DrawStretchPic( x, y, SMALLCHAR_WIDTH, SMALLCHAR_HEIGHT,
  398. fcol, frow,
  399. fcol + size, frow + size,
  400. charSetMaterial );
  401. }
  402. /*
  403. ==================
  404. idRenderSystemLocal::DrawSmallStringExt
  405. Draws a multi-colored string with a drop shadow, optionally forcing
  406. to a fixed color.
  407. Coordinates are at 640 by 480 virtual resolution
  408. ==================
  409. */
  410. void idRenderSystemLocal::DrawSmallStringExt( int x, int y, const char *string, const idVec4 &setColor, bool forceColor ) {
  411. idVec4 color;
  412. const unsigned char *s;
  413. int xx;
  414. // draw the colored text
  415. s = (const unsigned char*)string;
  416. xx = x;
  417. SetColor( setColor );
  418. while ( *s ) {
  419. if ( idStr::IsColor( (const char*)s ) ) {
  420. if ( !forceColor ) {
  421. if ( *(s+1) == C_COLOR_DEFAULT ) {
  422. SetColor( setColor );
  423. } else {
  424. color = idStr::ColorForIndex( *(s+1) );
  425. color[3] = setColor[3];
  426. SetColor( color );
  427. }
  428. }
  429. s += 2;
  430. continue;
  431. }
  432. DrawSmallChar( xx, y, *s );
  433. xx += SMALLCHAR_WIDTH;
  434. s++;
  435. }
  436. SetColor( colorWhite );
  437. }
  438. /*
  439. =====================
  440. idRenderSystemLocal::DrawBigChar
  441. =====================
  442. */
  443. void idRenderSystemLocal::DrawBigChar( int x, int y, int ch ) {
  444. int row, col;
  445. float frow, fcol;
  446. float size;
  447. ch &= 255;
  448. if ( ch == ' ' ) {
  449. return;
  450. }
  451. if ( y < -BIGCHAR_HEIGHT ) {
  452. return;
  453. }
  454. row = ch >> 4;
  455. col = ch & 15;
  456. frow = row * 0.0625f;
  457. fcol = col * 0.0625f;
  458. size = 0.0625f;
  459. DrawStretchPic( x, y, BIGCHAR_WIDTH, BIGCHAR_HEIGHT,
  460. fcol, frow,
  461. fcol + size, frow + size,
  462. charSetMaterial );
  463. }
  464. /*
  465. ==================
  466. idRenderSystemLocal::DrawBigStringExt
  467. Draws a multi-colored string with a drop shadow, optionally forcing
  468. to a fixed color.
  469. Coordinates are at 640 by 480 virtual resolution
  470. ==================
  471. */
  472. void idRenderSystemLocal::DrawBigStringExt( int x, int y, const char *string, const idVec4 &setColor, bool forceColor ) {
  473. idVec4 color;
  474. const char *s;
  475. int xx;
  476. // draw the colored text
  477. s = string;
  478. xx = x;
  479. SetColor( setColor );
  480. while ( *s ) {
  481. if ( idStr::IsColor( s ) ) {
  482. if ( !forceColor ) {
  483. if ( *(s+1) == C_COLOR_DEFAULT ) {
  484. SetColor( setColor );
  485. } else {
  486. color = idStr::ColorForIndex( *(s+1) );
  487. color[3] = setColor[3];
  488. SetColor( color );
  489. }
  490. }
  491. s += 2;
  492. continue;
  493. }
  494. DrawBigChar( xx, y, *s );
  495. xx += BIGCHAR_WIDTH;
  496. s++;
  497. }
  498. SetColor( colorWhite );
  499. }
  500. //======================================================================================
  501. /*
  502. ====================
  503. idRenderSystemLocal::SwapCommandBuffers
  504. Performs final closeout of any gui models being defined.
  505. Waits for the previous GPU rendering to complete and vsync.
  506. Returns the head of the linked command list that was just closed off.
  507. Returns timing information from the previous frame.
  508. After this is called, new command buffers can be built up in parallel
  509. with the rendering of the closed off command buffers by RenderCommandBuffers()
  510. ====================
  511. */
  512. const emptyCommand_t * idRenderSystemLocal::SwapCommandBuffers(
  513. uint64 * frontEndMicroSec,
  514. uint64 * backEndMicroSec,
  515. uint64 * shadowMicroSec,
  516. uint64 * gpuMicroSec ) {
  517. SwapCommandBuffers_FinishRendering( frontEndMicroSec, backEndMicroSec, shadowMicroSec, gpuMicroSec );
  518. return SwapCommandBuffers_FinishCommandBuffers();
  519. }
  520. /*
  521. =====================
  522. idRenderSystemLocal::SwapCommandBuffers_FinishRendering
  523. =====================
  524. */
  525. void idRenderSystemLocal::SwapCommandBuffers_FinishRendering(
  526. uint64 * frontEndMicroSec,
  527. uint64 * backEndMicroSec,
  528. uint64 * shadowMicroSec,
  529. uint64 * gpuMicroSec ) {
  530. SCOPED_PROFILE_EVENT( "SwapCommandBuffers" );
  531. if ( gpuMicroSec != NULL ) {
  532. *gpuMicroSec = 0; // until shown otherwise
  533. }
  534. if ( !R_IsInitialized() ) {
  535. return;
  536. }
  537. // After coming back from an autoswap, we won't have anything to render
  538. if ( frameData->cmdHead->next != NULL ) {
  539. // wait for our fence to hit, which means the swap has actually happened
  540. // We must do this before clearing any resources the GPU may be using
  541. void GL_BlockingSwapBuffers();
  542. GL_BlockingSwapBuffers();
  543. }
  544. // read back the start and end timer queries from the previous frame
  545. if ( glConfig.timerQueryAvailable ) {
  546. uint64 drawingTimeNanoseconds = 0;
  547. if ( tr.timerQueryId != 0 ) {
  548. qglGetQueryObjectui64vEXT( tr.timerQueryId, GL_QUERY_RESULT, &drawingTimeNanoseconds );
  549. }
  550. if ( gpuMicroSec != NULL ) {
  551. *gpuMicroSec = drawingTimeNanoseconds / 1000;
  552. }
  553. }
  554. //------------------------------
  555. // save out timing information
  556. if ( frontEndMicroSec != NULL ) {
  557. *frontEndMicroSec = pc.frontEndMicroSec;
  558. }
  559. if ( backEndMicroSec != NULL ) {
  560. *backEndMicroSec = backEnd.pc.totalMicroSec;
  561. }
  562. if ( shadowMicroSec != NULL ) {
  563. *shadowMicroSec = backEnd.pc.shadowMicroSec;
  564. }
  565. // print any other statistics and clear all of them
  566. R_PerformanceCounters();
  567. // check for dynamic changes that require some initialization
  568. R_CheckCvars();
  569. // check for errors
  570. GL_CheckErrors();
  571. }
  572. /*
  573. =====================
  574. idRenderSystemLocal::SwapCommandBuffers_FinishCommandBuffers
  575. =====================
  576. */
  577. const emptyCommand_t * idRenderSystemLocal::SwapCommandBuffers_FinishCommandBuffers() {
  578. if ( !R_IsInitialized() ) {
  579. return NULL;
  580. }
  581. // close any gui drawing
  582. guiModel->EmitFullScreen();
  583. guiModel->Clear();
  584. // unmap the buffer objects so they can be used by the GPU
  585. vertexCache.BeginBackEnd();
  586. // save off this command buffer
  587. const emptyCommand_t * commandBufferHead = frameData->cmdHead;
  588. // copy the code-used drawsurfs that were
  589. // allocated at the start of the buffer memory to the backEnd referenced locations
  590. backEnd.unitSquareSurface = tr.unitSquareSurface_;
  591. backEnd.zeroOneCubeSurface = tr.zeroOneCubeSurface_;
  592. backEnd.testImageSurface = tr.testImageSurface_;
  593. // use the other buffers next frame, because another CPU
  594. // may still be rendering into the current buffers
  595. R_ToggleSmpFrame();
  596. // possibly change the stereo3D mode
  597. // PC
  598. if ( glConfig.nativeScreenWidth == 1280 && glConfig.nativeScreenHeight == 1470 ) {
  599. glConfig.stereo3Dmode = STEREO3D_HDMI_720;
  600. } else {
  601. glConfig.stereo3Dmode = GetStereoScopicRenderingMode();
  602. }
  603. // prepare the new command buffer
  604. guiModel->BeginFrame();
  605. //------------------------------
  606. // Make sure that geometry used by code is present in the buffer cache.
  607. // These use frame buffer cache (not static) because they may be used during
  608. // map loads.
  609. //
  610. // It is important to do this first, so if the buffers overflow during
  611. // scene generation, the basic surfaces needed for drawing the buffers will
  612. // always be present.
  613. //------------------------------
  614. R_InitDrawSurfFromTri( tr.unitSquareSurface_, *tr.unitSquareTriangles );
  615. R_InitDrawSurfFromTri( tr.zeroOneCubeSurface_, *tr.zeroOneCubeTriangles );
  616. R_InitDrawSurfFromTri( tr.testImageSurface_, *tr.testImageTriangles );
  617. // Reset render crop to be the full screen
  618. renderCrops[0].x1 = 0;
  619. renderCrops[0].y1 = 0;
  620. renderCrops[0].x2 = GetWidth() - 1;
  621. renderCrops[0].y2 = GetHeight() - 1;
  622. currentRenderCrop = 0;
  623. // this is the ONLY place this is modified
  624. frameCount++;
  625. // just in case we did a common->Error while this
  626. // was set
  627. guiRecursionLevel = 0;
  628. // the first rendering will be used for commands like
  629. // screenshot, rather than a possible subsequent remote
  630. // or mirror render
  631. // primaryWorld = NULL;
  632. // set the time for shader effects in 2D rendering
  633. frameShaderTime = Sys_Milliseconds() * 0.001;
  634. setBufferCommand_t * cmd2 = (setBufferCommand_t *)R_GetCommandBuffer( sizeof( *cmd2 ) );
  635. cmd2->commandId = RC_SET_BUFFER;
  636. cmd2->buffer = (int)GL_BACK;
  637. // the old command buffer can now be rendered, while the new one can
  638. // be built in parallel
  639. return commandBufferHead;
  640. }
  641. /*
  642. =====================
  643. idRenderSystemLocal::WriteDemoPics
  644. =====================
  645. */
  646. void idRenderSystemLocal::WriteDemoPics() {
  647. common->WriteDemo()->WriteInt( DS_RENDER );
  648. common->WriteDemo()->WriteInt( DC_GUI_MODEL );
  649. }
  650. /*
  651. =====================
  652. idRenderSystemLocal::DrawDemoPics
  653. =====================
  654. */
  655. void idRenderSystemLocal::DrawDemoPics() {
  656. }
  657. /*
  658. =====================
  659. idRenderSystemLocal::GetCroppedViewport
  660. Returns the current cropped pixel coordinates
  661. =====================
  662. */
  663. void idRenderSystemLocal::GetCroppedViewport( idScreenRect * viewport ) {
  664. *viewport = renderCrops[currentRenderCrop];
  665. }
  666. /*
  667. ========================
  668. idRenderSystemLocal::PerformResolutionScaling
  669. The 3D rendering size can be smaller than the full window resolution to reduce
  670. fill rate requirements while still allowing the GUIs to be full resolution.
  671. In split screen mode the rendering size is also smaller.
  672. ========================
  673. */
  674. void idRenderSystemLocal::PerformResolutionScaling( int& newWidth, int& newHeight ) {
  675. float xScale = 1.0f;
  676. float yScale = 1.0f;
  677. resolutionScale.GetCurrentResolutionScale( xScale, yScale );
  678. newWidth = idMath::Ftoi( GetWidth() * xScale );
  679. newHeight = idMath::Ftoi( GetHeight() * yScale );
  680. }
  681. /*
  682. ================
  683. idRenderSystemLocal::CropRenderSize
  684. ================
  685. */
  686. void idRenderSystemLocal::CropRenderSize( int width, int height ) {
  687. if ( !R_IsInitialized() ) {
  688. return;
  689. }
  690. // close any gui drawing before changing the size
  691. guiModel->EmitFullScreen();
  692. guiModel->Clear();
  693. if ( width < 1 || height < 1 ) {
  694. common->Error( "CropRenderSize: bad sizes" );
  695. }
  696. if ( common->WriteDemo() ) {
  697. common->WriteDemo()->WriteInt( DS_RENDER );
  698. common->WriteDemo()->WriteInt( DC_CROP_RENDER );
  699. common->WriteDemo()->WriteInt( width );
  700. common->WriteDemo()->WriteInt( height );
  701. if ( r_showDemo.GetBool() ) {
  702. common->Printf( "write DC_CROP_RENDER\n" );
  703. }
  704. }
  705. idScreenRect & previous = renderCrops[currentRenderCrop];
  706. currentRenderCrop++;
  707. idScreenRect & current = renderCrops[currentRenderCrop];
  708. current.x1 = previous.x1;
  709. current.x2 = previous.x1 + width - 1;
  710. current.y1 = previous.y2 - height + 1;
  711. current.y2 = previous.y2;
  712. }
  713. /*
  714. ================
  715. idRenderSystemLocal::UnCrop
  716. ================
  717. */
  718. void idRenderSystemLocal::UnCrop() {
  719. if ( !R_IsInitialized() ) {
  720. return;
  721. }
  722. if ( currentRenderCrop < 1 ) {
  723. common->Error( "idRenderSystemLocal::UnCrop: currentRenderCrop < 1" );
  724. }
  725. // close any gui drawing
  726. guiModel->EmitFullScreen();
  727. guiModel->Clear();
  728. currentRenderCrop--;
  729. if ( common->WriteDemo() ) {
  730. common->WriteDemo()->WriteInt( DS_RENDER );
  731. common->WriteDemo()->WriteInt( DC_UNCROP_RENDER );
  732. if ( r_showDemo.GetBool() ) {
  733. common->Printf( "write DC_UNCROP\n" );
  734. }
  735. }
  736. }
  737. /*
  738. ================
  739. idRenderSystemLocal::CaptureRenderToImage
  740. ================
  741. */
  742. void idRenderSystemLocal::CaptureRenderToImage( const char *imageName, bool clearColorAfterCopy ) {
  743. if ( !R_IsInitialized() ) {
  744. return;
  745. }
  746. guiModel->EmitFullScreen();
  747. guiModel->Clear();
  748. if ( common->WriteDemo() ) {
  749. common->WriteDemo()->WriteInt( DS_RENDER );
  750. common->WriteDemo()->WriteInt( DC_CAPTURE_RENDER );
  751. common->WriteDemo()->WriteHashString( imageName );
  752. if ( r_showDemo.GetBool() ) {
  753. common->Printf( "write DC_CAPTURE_RENDER: %s\n", imageName );
  754. }
  755. }
  756. idImage * image = globalImages->GetImage( imageName );
  757. if ( image == NULL ) {
  758. image = globalImages->AllocImage( imageName );
  759. }
  760. idScreenRect & rc = renderCrops[currentRenderCrop];
  761. copyRenderCommand_t *cmd = (copyRenderCommand_t *)R_GetCommandBuffer( sizeof( *cmd ) );
  762. cmd->commandId = RC_COPY_RENDER;
  763. cmd->x = rc.x1;
  764. cmd->y = rc.y1;
  765. cmd->imageWidth = rc.GetWidth();
  766. cmd->imageHeight = rc.GetHeight();
  767. cmd->image = image;
  768. cmd->clearColorAfterCopy = clearColorAfterCopy;
  769. guiModel->Clear();
  770. }
  771. /*
  772. ==============
  773. idRenderSystemLocal::CaptureRenderToFile
  774. ==============
  775. */
  776. void idRenderSystemLocal::CaptureRenderToFile( const char *fileName, bool fixAlpha ) {
  777. if ( !R_IsInitialized() ) {
  778. return;
  779. }
  780. idScreenRect & rc = renderCrops[currentRenderCrop];
  781. guiModel->EmitFullScreen();
  782. guiModel->Clear();
  783. RenderCommandBuffers( frameData->cmdHead );
  784. qglReadBuffer( GL_BACK );
  785. // include extra space for OpenGL padding to word boundaries
  786. int c = ( rc.GetWidth() + 3 ) * rc.GetHeight();
  787. byte *data = (byte *)R_StaticAlloc( c * 3 );
  788. qglReadPixels( rc.x1, rc.y1, rc.GetWidth(), rc.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, data );
  789. byte *data2 = (byte *)R_StaticAlloc( c * 4 );
  790. for ( int i = 0 ; i < c ; i++ ) {
  791. data2[ i * 4 ] = data[ i * 3 ];
  792. data2[ i * 4 + 1 ] = data[ i * 3 + 1 ];
  793. data2[ i * 4 + 2 ] = data[ i * 3 + 2 ];
  794. data2[ i * 4 + 3 ] = 0xff;
  795. }
  796. R_WriteTGA( fileName, data2, rc.GetWidth(), rc.GetHeight(), true );
  797. R_StaticFree( data );
  798. R_StaticFree( data2 );
  799. }
  800. /*
  801. ==============
  802. idRenderSystemLocal::AllocRenderWorld
  803. ==============
  804. */
  805. idRenderWorld *idRenderSystemLocal::AllocRenderWorld() {
  806. idRenderWorldLocal *rw;
  807. rw = new (TAG_RENDER) idRenderWorldLocal;
  808. worlds.Append( rw );
  809. return rw;
  810. }
  811. /*
  812. ==============
  813. idRenderSystemLocal::FreeRenderWorld
  814. ==============
  815. */
  816. void idRenderSystemLocal::FreeRenderWorld( idRenderWorld *rw ) {
  817. if ( primaryWorld == rw ) {
  818. primaryWorld = NULL;
  819. }
  820. worlds.Remove( static_cast<idRenderWorldLocal *>(rw) );
  821. delete rw;
  822. }
  823. /*
  824. ==============
  825. idRenderSystemLocal::PrintMemInfo
  826. ==============
  827. */
  828. void idRenderSystemLocal::PrintMemInfo( MemInfo_t *mi ) {
  829. // sum up image totals
  830. globalImages->PrintMemInfo( mi );
  831. // sum up model totals
  832. renderModelManager->PrintMemInfo( mi );
  833. // compute render totals
  834. }
  835. /*
  836. ===============
  837. idRenderSystemLocal::UploadImage
  838. ===============
  839. */
  840. bool idRenderSystemLocal::UploadImage( const char *imageName, const byte *data, int width, int height ) {
  841. idImage *image = globalImages->GetImage( imageName );
  842. if ( !image ) {
  843. return false;
  844. }
  845. image->UploadScratch( data, width, height );
  846. return true;
  847. }