tr_backend.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. frameData_t *frameData;
  24. backEndState_t backEnd;
  25. /*
  26. ======================
  27. RB_SetDefaultGLState
  28. This should initialize all GL state that any part of the entire program
  29. may touch, including the editor.
  30. ======================
  31. */
  32. void RB_SetDefaultGLState( void ) {
  33. int i;
  34. RB_LogComment( "--- R_SetDefaultGLState ---\n" );
  35. qglClearDepth( 1.0f );
  36. qglColor4f (1,1,1,1);
  37. // the vertex array is always enabled
  38. qglEnableClientState( GL_VERTEX_ARRAY );
  39. qglEnableClientState( GL_TEXTURE_COORD_ARRAY );
  40. qglDisableClientState( GL_COLOR_ARRAY );
  41. //
  42. // make sure our GL state vector is set correctly
  43. //
  44. memset( &backEnd.glState, 0, sizeof( backEnd.glState ) );
  45. backEnd.glState.forceGlState = true;
  46. qglColorMask( 1, 1, 1, 1 );
  47. qglEnable( GL_DEPTH_TEST );
  48. qglEnable( GL_BLEND );
  49. qglEnable( GL_SCISSOR_TEST );
  50. qglEnable( GL_CULL_FACE );
  51. qglDisable( GL_LIGHTING );
  52. qglDisable( GL_LINE_STIPPLE );
  53. qglDisable( GL_STENCIL_TEST );
  54. qglPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  55. qglDepthMask( GL_TRUE );
  56. qglDepthFunc( GL_ALWAYS );
  57. qglCullFace( GL_FRONT_AND_BACK );
  58. qglShadeModel( GL_SMOOTH );
  59. if ( r_useScissor.GetBool() ) {
  60. qglScissor( 0, 0, glConfig.vidWidth, glConfig.vidHeight );
  61. }
  62. for ( i = glConfig.maxTextureUnits - 1 ; i >= 0 ; i-- ) {
  63. GL_SelectTexture( i );
  64. // object linear texgen is our default
  65. qglTexGenf( GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
  66. qglTexGenf( GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
  67. qglTexGenf( GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
  68. qglTexGenf( GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
  69. GL_TexEnv( GL_MODULATE );
  70. qglDisable( GL_TEXTURE_2D );
  71. if ( glConfig.texture3DAvailable ) {
  72. qglDisable( GL_TEXTURE_3D );
  73. }
  74. if ( glConfig.cubeMapAvailable ) {
  75. qglDisable( GL_TEXTURE_CUBE_MAP_EXT );
  76. }
  77. }
  78. }
  79. /*
  80. ====================
  81. RB_LogComment
  82. ====================
  83. */
  84. void RB_LogComment( const char *comment, ... ) {
  85. va_list marker;
  86. if ( !tr.logFile ) {
  87. return;
  88. }
  89. fprintf( tr.logFile, "// " );
  90. va_start( marker, comment );
  91. vfprintf( tr.logFile, comment, marker );
  92. va_end( marker );
  93. }
  94. //=============================================================================
  95. /*
  96. ====================
  97. GL_SelectTexture
  98. ====================
  99. */
  100. void GL_SelectTexture( int unit ) {
  101. if ( backEnd.glState.currenttmu == unit ) {
  102. return;
  103. }
  104. if ( unit < 0 || unit >= glConfig.maxTextureUnits && unit >= glConfig.maxTextureImageUnits ) {
  105. common->Warning( "GL_SelectTexture: unit = %i", unit );
  106. return;
  107. }
  108. qglActiveTextureARB( GL_TEXTURE0_ARB + unit );
  109. qglClientActiveTextureARB( GL_TEXTURE0_ARB + unit );
  110. RB_LogComment( "glActiveTextureARB( %i );\nglClientActiveTextureARB( %i );\n", unit, unit );
  111. backEnd.glState.currenttmu = unit;
  112. }
  113. /*
  114. ====================
  115. GL_Cull
  116. This handles the flipping needed when the view being
  117. rendered is a mirored view.
  118. ====================
  119. */
  120. void GL_Cull( int cullType ) {
  121. if ( backEnd.glState.faceCulling == cullType ) {
  122. return;
  123. }
  124. if ( cullType == CT_TWO_SIDED ) {
  125. qglDisable( GL_CULL_FACE );
  126. } else {
  127. if ( backEnd.glState.faceCulling == CT_TWO_SIDED ) {
  128. qglEnable( GL_CULL_FACE );
  129. }
  130. if ( cullType == CT_BACK_SIDED ) {
  131. if ( backEnd.viewDef->isMirror ) {
  132. qglCullFace( GL_FRONT );
  133. } else {
  134. qglCullFace( GL_BACK );
  135. }
  136. } else {
  137. if ( backEnd.viewDef->isMirror ) {
  138. qglCullFace( GL_BACK );
  139. } else {
  140. qglCullFace( GL_FRONT );
  141. }
  142. }
  143. }
  144. backEnd.glState.faceCulling = cullType;
  145. }
  146. /*
  147. ====================
  148. GL_TexEnv
  149. ====================
  150. */
  151. void GL_TexEnv( int env ) {
  152. tmu_t *tmu;
  153. tmu = &backEnd.glState.tmu[backEnd.glState.currenttmu];
  154. if ( env == tmu->texEnv ) {
  155. return;
  156. }
  157. tmu->texEnv = env;
  158. switch ( env ) {
  159. case GL_COMBINE_EXT:
  160. case GL_MODULATE:
  161. case GL_REPLACE:
  162. case GL_DECAL:
  163. case GL_ADD:
  164. qglTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env );
  165. break;
  166. default:
  167. common->Error( "GL_TexEnv: invalid env '%d' passed\n", env );
  168. break;
  169. }
  170. }
  171. /*
  172. =================
  173. GL_ClearStateDelta
  174. Clears the state delta bits, so the next GL_State
  175. will set every item
  176. =================
  177. */
  178. void GL_ClearStateDelta( void ) {
  179. backEnd.glState.forceGlState = true;
  180. }
  181. /*
  182. ====================
  183. GL_State
  184. This routine is responsible for setting the most commonly changed state
  185. ====================
  186. */
  187. void GL_State( int stateBits ) {
  188. int diff;
  189. if ( !r_useStateCaching.GetBool() || backEnd.glState.forceGlState ) {
  190. // make sure everything is set all the time, so we
  191. // can see if our delta checking is screwing up
  192. diff = -1;
  193. backEnd.glState.forceGlState = false;
  194. } else {
  195. diff = stateBits ^ backEnd.glState.glStateBits;
  196. if ( !diff ) {
  197. return;
  198. }
  199. }
  200. //
  201. // check depthFunc bits
  202. //
  203. if ( diff & ( GLS_DEPTHFUNC_EQUAL | GLS_DEPTHFUNC_LESS | GLS_DEPTHFUNC_ALWAYS ) ) {
  204. if ( stateBits & GLS_DEPTHFUNC_EQUAL ) {
  205. qglDepthFunc( GL_EQUAL );
  206. } else if ( stateBits & GLS_DEPTHFUNC_ALWAYS ) {
  207. qglDepthFunc( GL_ALWAYS );
  208. } else {
  209. qglDepthFunc( GL_LEQUAL );
  210. }
  211. }
  212. //
  213. // check blend bits
  214. //
  215. if ( diff & ( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS ) ) {
  216. GLenum srcFactor, dstFactor;
  217. switch ( stateBits & GLS_SRCBLEND_BITS ) {
  218. case GLS_SRCBLEND_ZERO:
  219. srcFactor = GL_ZERO;
  220. break;
  221. case GLS_SRCBLEND_ONE:
  222. srcFactor = GL_ONE;
  223. break;
  224. case GLS_SRCBLEND_DST_COLOR:
  225. srcFactor = GL_DST_COLOR;
  226. break;
  227. case GLS_SRCBLEND_ONE_MINUS_DST_COLOR:
  228. srcFactor = GL_ONE_MINUS_DST_COLOR;
  229. break;
  230. case GLS_SRCBLEND_SRC_ALPHA:
  231. srcFactor = GL_SRC_ALPHA;
  232. break;
  233. case GLS_SRCBLEND_ONE_MINUS_SRC_ALPHA:
  234. srcFactor = GL_ONE_MINUS_SRC_ALPHA;
  235. break;
  236. case GLS_SRCBLEND_DST_ALPHA:
  237. srcFactor = GL_DST_ALPHA;
  238. break;
  239. case GLS_SRCBLEND_ONE_MINUS_DST_ALPHA:
  240. srcFactor = GL_ONE_MINUS_DST_ALPHA;
  241. break;
  242. case GLS_SRCBLEND_ALPHA_SATURATE:
  243. srcFactor = GL_SRC_ALPHA_SATURATE;
  244. break;
  245. default:
  246. srcFactor = GL_ONE; // to get warning to shut up
  247. common->Error( "GL_State: invalid src blend state bits\n" );
  248. break;
  249. }
  250. switch ( stateBits & GLS_DSTBLEND_BITS ) {
  251. case GLS_DSTBLEND_ZERO:
  252. dstFactor = GL_ZERO;
  253. break;
  254. case GLS_DSTBLEND_ONE:
  255. dstFactor = GL_ONE;
  256. break;
  257. case GLS_DSTBLEND_SRC_COLOR:
  258. dstFactor = GL_SRC_COLOR;
  259. break;
  260. case GLS_DSTBLEND_ONE_MINUS_SRC_COLOR:
  261. dstFactor = GL_ONE_MINUS_SRC_COLOR;
  262. break;
  263. case GLS_DSTBLEND_SRC_ALPHA:
  264. dstFactor = GL_SRC_ALPHA;
  265. break;
  266. case GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA:
  267. dstFactor = GL_ONE_MINUS_SRC_ALPHA;
  268. break;
  269. case GLS_DSTBLEND_DST_ALPHA:
  270. dstFactor = GL_DST_ALPHA;
  271. break;
  272. case GLS_DSTBLEND_ONE_MINUS_DST_ALPHA:
  273. dstFactor = GL_ONE_MINUS_DST_ALPHA;
  274. break;
  275. default:
  276. dstFactor = GL_ONE; // to get warning to shut up
  277. common->Error( "GL_State: invalid dst blend state bits\n" );
  278. break;
  279. }
  280. qglBlendFunc( srcFactor, dstFactor );
  281. }
  282. //
  283. // check depthmask
  284. //
  285. if ( diff & GLS_DEPTHMASK ) {
  286. if ( stateBits & GLS_DEPTHMASK ) {
  287. qglDepthMask( GL_FALSE );
  288. } else {
  289. qglDepthMask( GL_TRUE );
  290. }
  291. }
  292. //
  293. // check colormask
  294. //
  295. if ( diff & (GLS_REDMASK|GLS_GREENMASK|GLS_BLUEMASK|GLS_ALPHAMASK) ) {
  296. GLboolean r, g, b, a;
  297. r = ( stateBits & GLS_REDMASK ) ? 0 : 1;
  298. g = ( stateBits & GLS_GREENMASK ) ? 0 : 1;
  299. b = ( stateBits & GLS_BLUEMASK ) ? 0 : 1;
  300. a = ( stateBits & GLS_ALPHAMASK ) ? 0 : 1;
  301. qglColorMask( r, g, b, a );
  302. }
  303. //
  304. // fill/line mode
  305. //
  306. if ( diff & GLS_POLYMODE_LINE ) {
  307. if ( stateBits & GLS_POLYMODE_LINE ) {
  308. qglPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
  309. } else {
  310. qglPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
  311. }
  312. }
  313. //
  314. // alpha test
  315. //
  316. if ( diff & GLS_ATEST_BITS ) {
  317. switch ( stateBits & GLS_ATEST_BITS ) {
  318. case 0:
  319. qglDisable( GL_ALPHA_TEST );
  320. break;
  321. case GLS_ATEST_EQ_255:
  322. qglEnable( GL_ALPHA_TEST );
  323. qglAlphaFunc( GL_EQUAL, 1 );
  324. break;
  325. case GLS_ATEST_LT_128:
  326. qglEnable( GL_ALPHA_TEST );
  327. qglAlphaFunc( GL_LESS, 0.5 );
  328. break;
  329. case GLS_ATEST_GE_128:
  330. qglEnable( GL_ALPHA_TEST );
  331. qglAlphaFunc( GL_GEQUAL, 0.5 );
  332. break;
  333. default:
  334. assert( 0 );
  335. break;
  336. }
  337. }
  338. backEnd.glState.glStateBits = stateBits;
  339. }
  340. /*
  341. ============================================================================
  342. RENDER BACK END THREAD FUNCTIONS
  343. ============================================================================
  344. */
  345. /*
  346. =============
  347. RB_SetGL2D
  348. This is not used by the normal game paths, just by some tools
  349. =============
  350. */
  351. void RB_SetGL2D( void ) {
  352. // set 2D virtual screen size
  353. qglViewport( 0, 0, glConfig.vidWidth, glConfig.vidHeight );
  354. if ( r_useScissor.GetBool() ) {
  355. qglScissor( 0, 0, glConfig.vidWidth, glConfig.vidHeight );
  356. }
  357. qglMatrixMode( GL_PROJECTION );
  358. qglLoadIdentity();
  359. qglOrtho( 0, 640, 480, 0, 0, 1 ); // always assume 640x480 virtual coordinates
  360. qglMatrixMode( GL_MODELVIEW );
  361. qglLoadIdentity();
  362. GL_State( GLS_DEPTHFUNC_ALWAYS |
  363. GLS_SRCBLEND_SRC_ALPHA |
  364. GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA );
  365. GL_Cull( CT_TWO_SIDED );
  366. qglDisable( GL_DEPTH_TEST );
  367. qglDisable( GL_STENCIL_TEST );
  368. }
  369. /*
  370. =============
  371. RB_SetBuffer
  372. =============
  373. */
  374. static void RB_SetBuffer( const void *data ) {
  375. const setBufferCommand_t *cmd;
  376. // see which draw buffer we want to render the frame to
  377. cmd = (const setBufferCommand_t *)data;
  378. backEnd.frameCount = cmd->frameCount;
  379. qglDrawBuffer( cmd->buffer );
  380. // clear screen for debugging
  381. // automatically enable this with several other debug tools
  382. // that might leave unrendered portions of the screen
  383. if ( r_clear.GetFloat() || idStr::Length( r_clear.GetString() ) != 1 || r_lockSurfaces.GetBool() || r_singleArea.GetBool() || r_showOverDraw.GetBool() ) {
  384. float c[3];
  385. if ( sscanf( r_clear.GetString(), "%f %f %f", &c[0], &c[1], &c[2] ) == 3 ) {
  386. qglClearColor( c[0], c[1], c[2], 1 );
  387. } else if ( r_clear.GetInteger() == 2 ) {
  388. qglClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
  389. } else if ( r_showOverDraw.GetBool() ) {
  390. qglClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
  391. } else {
  392. qglClearColor( 0.4f, 0.0f, 0.25f, 1.0f );
  393. }
  394. qglClear( GL_COLOR_BUFFER_BIT );
  395. }
  396. }
  397. /*
  398. ===============
  399. RB_ShowImages
  400. Draw all the images to the screen, on top of whatever
  401. was there. This is used to test for texture thrashing.
  402. ===============
  403. */
  404. void RB_ShowImages( void ) {
  405. int i;
  406. idImage *image;
  407. float x, y, w, h;
  408. int start, end;
  409. RB_SetGL2D();
  410. //qglClearColor( 0.2, 0.2, 0.2, 1 );
  411. //qglClear( GL_COLOR_BUFFER_BIT );
  412. qglFinish();
  413. start = Sys_Milliseconds();
  414. for ( i = 0 ; i < globalImages->images.Num() ; i++ ) {
  415. image = globalImages->images[i];
  416. if ( image->texnum == idImage::TEXTURE_NOT_LOADED && image->partialImage == NULL ) {
  417. continue;
  418. }
  419. w = glConfig.vidWidth / 20;
  420. h = glConfig.vidHeight / 15;
  421. x = i % 20 * w;
  422. y = i / 20 * h;
  423. // show in proportional size in mode 2
  424. if ( r_showImages.GetInteger() == 2 ) {
  425. w *= image->uploadWidth / 512.0f;
  426. h *= image->uploadHeight / 512.0f;
  427. }
  428. image->Bind();
  429. qglBegin (GL_QUADS);
  430. qglTexCoord2f( 0, 0 );
  431. qglVertex2f( x, y );
  432. qglTexCoord2f( 1, 0 );
  433. qglVertex2f( x + w, y );
  434. qglTexCoord2f( 1, 1 );
  435. qglVertex2f( x + w, y + h );
  436. qglTexCoord2f( 0, 1 );
  437. qglVertex2f( x, y + h );
  438. qglEnd();
  439. }
  440. qglFinish();
  441. end = Sys_Milliseconds();
  442. common->Printf( "%i msec to draw all images\n", end - start );
  443. }
  444. /*
  445. =============
  446. RB_SwapBuffers
  447. =============
  448. */
  449. const void RB_SwapBuffers( const void *data ) {
  450. // texture swapping test
  451. if ( r_showImages.GetInteger() != 0 ) {
  452. RB_ShowImages();
  453. }
  454. // force a gl sync if requested
  455. if ( r_finish.GetBool() ) {
  456. qglFinish();
  457. }
  458. RB_LogComment( "***************** RB_SwapBuffers *****************\n\n\n" );
  459. // don't flip if drawing to front buffer
  460. if ( !r_frontBuffer.GetBool() ) {
  461. GLimp_SwapBuffers();
  462. }
  463. }
  464. /*
  465. =============
  466. RB_CopyRender
  467. Copy part of the current framebuffer to an image
  468. =============
  469. */
  470. const void RB_CopyRender( const void *data ) {
  471. const copyRenderCommand_t *cmd;
  472. cmd = (const copyRenderCommand_t *)data;
  473. if ( r_skipCopyTexture.GetBool() ) {
  474. return;
  475. }
  476. RB_LogComment( "***************** RB_CopyRender *****************\n" );
  477. if (cmd->image) {
  478. cmd->image->CopyFramebuffer( cmd->x, cmd->y, cmd->imageWidth, cmd->imageHeight, false );
  479. }
  480. }
  481. /*
  482. ====================
  483. RB_ExecuteBackEndCommands
  484. This function will be called syncronously if running without
  485. smp extensions, or asyncronously by another thread.
  486. ====================
  487. */
  488. int backEndStartTime, backEndFinishTime;
  489. void RB_ExecuteBackEndCommands( const emptyCommand_t *cmds ) {
  490. // r_debugRenderToTexture
  491. int c_draw3d = 0, c_draw2d = 0, c_setBuffers = 0, c_swapBuffers = 0, c_copyRenders = 0;
  492. if ( cmds->commandId == RC_NOP && !cmds->next ) {
  493. return;
  494. }
  495. backEndStartTime = Sys_Milliseconds();
  496. // needed for editor rendering
  497. RB_SetDefaultGLState();
  498. // upload any image loads that have completed
  499. globalImages->CompleteBackgroundImageLoads();
  500. for ( ; cmds ; cmds = (const emptyCommand_t *)cmds->next ) {
  501. switch ( cmds->commandId ) {
  502. case RC_NOP:
  503. break;
  504. case RC_DRAW_VIEW:
  505. RB_DrawView( cmds );
  506. if ( ((const drawSurfsCommand_t *)cmds)->viewDef->viewEntitys ) {
  507. c_draw3d++;
  508. }
  509. else {
  510. c_draw2d++;
  511. }
  512. break;
  513. case RC_SET_BUFFER:
  514. RB_SetBuffer( cmds );
  515. c_setBuffers++;
  516. break;
  517. case RC_SWAP_BUFFERS:
  518. RB_SwapBuffers( cmds );
  519. c_swapBuffers++;
  520. break;
  521. case RC_COPY_RENDER:
  522. RB_CopyRender( cmds );
  523. c_copyRenders++;
  524. break;
  525. default:
  526. common->Error( "RB_ExecuteBackEndCommands: bad commandId" );
  527. break;
  528. }
  529. }
  530. // go back to the default texture so the editor doesn't mess up a bound image
  531. qglBindTexture( GL_TEXTURE_2D, 0 );
  532. backEnd.glState.tmu[0].current2DMap = -1;
  533. // stop rendering on this thread
  534. backEndFinishTime = Sys_Milliseconds();
  535. backEnd.pc.msec = backEndFinishTime - backEndStartTime;
  536. if ( r_debugRenderToTexture.GetInteger() == 1 ) {
  537. common->Printf( "3d: %i, 2d: %i, SetBuf: %i, SwpBuf: %i, CpyRenders: %i, CpyFrameBuf: %i\n", c_draw3d, c_draw2d, c_setBuffers, c_swapBuffers, c_copyRenders, backEnd.c_copyFrameBuffer );
  538. backEnd.c_copyFrameBuffer = 0;
  539. }
  540. }