Team Fortress 2 Source Code as on 22/4/2020

298 lines
7.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //===========================================================================//
  9. // HDRFIXME: reduce the number of include files here.
  10. #include "render_pch.h"
  11. #include "cdll_int.h"
  12. #include "client_class.h"
  13. #include "icliententitylist.h"
  14. #include "traceinit.h"
  15. #include "server.h"
  16. #include "r_decal.h"
  17. #include "r_areaportal.h"
  18. #include "ispatialpartitioninternal.h"
  19. #include "cdll_engine_int.h"
  20. #include "ivtex.h"
  21. #include "materialsystem/itexture.h"
  22. #include "view.h"
  23. #include "tier0/dbg.h"
  24. #include "staticpropmgr.h"
  25. #include "icliententity.h"
  26. #include "gl_drawlights.h"
  27. #include "Overlay.h"
  28. #include "vmodes.h"
  29. #include "tier1/utlbuffer.h"
  30. #include "vtf/vtf.h"
  31. #include "bitmap/imageformat.h"
  32. #include "cbenchmark.h"
  33. #include "ivideomode.h"
  34. #include "materialsystem/imaterialsystemhardwareconfig.h"
  35. #include "../utils/common/bsplib.h"
  36. #include "ibsppack.h"
  37. // memdbgon must be the last include file in a .cpp file!!!
  38. #include "tier0/memdbgon.h"
  39. void Linefile_Read_f(void);
  40. /*
  41. ====================
  42. // if( r_drawtranslucentworld )
  43. // {
  44. // bSaveDrawTranslucentWorld = r_drawtranslucentworld->GetBool();
  45. // NOTE! : We use to set this to 0 for HDR.
  46. // r_drawtranslucentworld->SetValue( 0 );
  47. // }
  48. // if( r_drawtranslucentrenderables )
  49. R_TimeRefresh_f
  50. For program optimization
  51. ====================
  52. */
  53. void R_TimeRefresh_f (void)
  54. {
  55. int i;
  56. float start, stop, time;
  57. CViewSetup view;
  58. materials->Flush( true );
  59. Q_memset(&view, 0, sizeof(view));
  60. view.origin = MainViewOrigin();
  61. view.angles[0] = 0;
  62. view.angles[1] = 0;
  63. view.angles[2] = 0;
  64. view.x = 0;
  65. view.y = 0;
  66. view.width = videomode->GetModeStereoWidth();
  67. view.height = videomode->GetModeStereoHeight();
  68. view.fov = 75;
  69. view.fovViewmodel = 75;
  70. view.m_flAspectRatio = 1.0f;
  71. view.zNear = 4;
  72. view.zFar = MAX_COORD_FLOAT;
  73. view.zNearViewmodel = 4;
  74. view.zFarViewmodel = MAX_COORD_FLOAT;
  75. int savedeveloper = developer.GetInt();
  76. developer.SetValue( 0 );
  77. start = Sys_FloatTime ();
  78. for (i=0 ; i<128 ; i++)
  79. {
  80. view.angles[1] = i/128.0*360.0;
  81. g_ClientDLL->RenderView( view, VIEW_CLEAR_COLOR, RENDERVIEW_DRAWVIEWMODEL | RENDERVIEW_DRAWHUD );
  82. Shader_SwapBuffers();
  83. }
  84. materials->Flush( true );
  85. Shader_SwapBuffers();
  86. stop = Sys_FloatTime ();
  87. time = stop-start;
  88. developer.SetValue( savedeveloper );
  89. ConMsg ("%f seconds (%f fps)\n", time, 128/time);
  90. }
  91. ConCommand timerefresh("timerefresh", R_TimeRefresh_f, "Profile the renderer.", FCVAR_CHEAT );
  92. ConCommand linefile("linefile", Linefile_Read_f, "Parses map leak data from .lin file", FCVAR_CHEAT );
  93. //-----------------------------------------------------------------------------
  94. // Purpose:
  95. //-----------------------------------------------------------------------------
  96. void R_Init (void)
  97. {
  98. extern byte *hunk_base;
  99. extern void InitMathlib( void );
  100. InitMathlib();
  101. UpdateMaterialSystemConfig();
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Purpose:
  105. //-----------------------------------------------------------------------------
  106. void R_Shutdown( void )
  107. {
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Purpose:
  111. //-----------------------------------------------------------------------------
  112. void R_ResetLightStyles( void )
  113. {
  114. for ( int i=0 ; i<256 ; i++ )
  115. {
  116. d_lightstylevalue[i] = 264;
  117. d_lightstyleframe[i] = r_framecount;
  118. }
  119. }
  120. void R_RemoveAllDecalsFromAllModels();
  121. //-----------------------------------------------------------------------------
  122. // Purpose:
  123. //-----------------------------------------------------------------------------
  124. CON_COMMAND_F( r_cleardecals, "Usage r_cleardecals <permanent>.", FCVAR_CLIENTCMD_CAN_EXECUTE | FCVAR_SERVER_CAN_EXECUTE )
  125. {
  126. if ( host_state.worldmodel )
  127. {
  128. bool bPermanent = false;
  129. if ( args.ArgC() == 2 )
  130. {
  131. if ( !Q_stricmp( args[1], "permanent" ) )
  132. {
  133. bPermanent = true;
  134. }
  135. }
  136. R_DecalTerm( host_state.worldmodel->brush.pShared, bPermanent );
  137. }
  138. R_RemoveAllDecalsFromAllModels();
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Loads world geometry. Called when map changes or dx level changes
  142. //-----------------------------------------------------------------------------
  143. void R_LoadWorldGeometry( bool bDXChange )
  144. {
  145. // Recreate the sortinfo arrays ( ack, uses new/delete right now ) because doing it with Hunk_AllocName will
  146. // leak through every connect that doesn't wipe the hunk ( "reconnect" )
  147. MaterialSystem_DestroySortinfo();
  148. MaterialSystem_RegisterLightmapSurfaces();
  149. MaterialSystem_CreateSortinfo();
  150. // UNDONE: This is a really crappy place to do this - shouldn't this stuff be in the modelloader?
  151. // If this is the first time we've tried to render this map, create a few one-time data structures
  152. // These all get cleared out if Map_UnloadModel is ever called by the modelloader interface ( and that happens
  153. // any time we free the Hunk down to the low mark since these things all use the Hunk for their data ).
  154. if ( !bDXChange )
  155. {
  156. if ( !modelloader->Map_GetRenderInfoAllocated() )
  157. {
  158. // create the displacement surfaces for the map
  159. modelloader->Map_LoadDisplacements( host_state.worldmodel, false );
  160. //if( !DispInfo_CreateStaticBuffers( host_state.worldmodel, materialSortInfoArray, false ) )
  161. // Sys_Error( "Can't create static meshes for displacements" );
  162. modelloader->Map_SetRenderInfoAllocated( true );
  163. }
  164. }
  165. else
  166. {
  167. // create the displacement surfaces for the map
  168. modelloader->Map_LoadDisplacements( host_state.worldmodel, true );
  169. }
  170. if ( bDXChange )
  171. {
  172. // Must be done before MarkWaterSurfaces
  173. modelloader->RecomputeSurfaceFlags( host_state.worldmodel );
  174. }
  175. Mod_MarkWaterSurfaces( host_state.worldmodel );
  176. // make sure and rebuild lightmaps when the level gets started.
  177. GL_RebuildLightmaps();
  178. if ( bDXChange )
  179. {
  180. R_BrushBatchInit();
  181. R_DecalReSortMaterials();
  182. OverlayMgr()->ReSortMaterials();
  183. }
  184. }
  185. /*
  186. ===============
  187. R_LevelInit
  188. ===============
  189. */
  190. void R_LevelInit( void )
  191. {
  192. ConDMsg( "Initializing renderer...\n" );
  193. COM_TimestampedLog( "R_LevelInit: Start" );
  194. Assert( g_ClientDLL );
  195. r_framecount = 1;
  196. R_ResetLightStyles();
  197. R_DecalInit();
  198. R_LoadSkys();
  199. R_InitStudio();
  200. // FIXME: Is this the best place to initialize the kd tree when we're client-only?
  201. if ( !sv.IsActive() )
  202. {
  203. g_pShadowMgr->LevelShutdown();
  204. StaticPropMgr()->LevelShutdown();
  205. SpatialPartition()->Init( host_state.worldmodel->mins, host_state.worldmodel->maxs );
  206. StaticPropMgr()->LevelInit();
  207. g_pShadowMgr->LevelInit( host_state.worldbrush->numsurfaces );
  208. }
  209. // We've fully loaded the new level, unload any models that we don't care about any more
  210. modelloader->UnloadUnreferencedModels();
  211. if ( host_state.worldmodel->brush.pShared->numworldlights == 0 )
  212. {
  213. ConDMsg( "Level unlit, setting 'mat_fullbright 1'\n" );
  214. mat_fullbright.SetValue( 1 );
  215. }
  216. UpdateMaterialSystemConfig();
  217. // FIXME: E3 2003 HACK
  218. if ( IsPC() && mat_levelflush.GetBool() )
  219. {
  220. bool bOnLevelShutdown = false;
  221. materials->ResetTempHWMemory( bOnLevelShutdown );
  222. }
  223. // precache any textures that are used in this map.
  224. // this is a no-op for textures that are already cached from the previous map.
  225. materials->CacheUsedMaterials();
  226. // Loads the world geometry
  227. R_LoadWorldGeometry();
  228. R_Surface_LevelInit();
  229. R_Areaportal_LevelInit();
  230. // Build the overlay fragments.
  231. OverlayMgr()->CreateFragments();
  232. #ifdef _XBOX
  233. extern void CompactTextureHeap();
  234. CompactTextureHeap();
  235. #endif
  236. COM_TimestampedLog( "R_LevelInit: Finish" );
  237. }
  238. void R_LevelShutdown()
  239. {
  240. R_Surface_LevelShutdown();
  241. R_Areaportal_LevelShutdown();
  242. g_DispLightmapSamplePositions.Purge();
  243. }