MLR.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #include "MLRHeaders.hpp"
  5. DWORD gShowClippedPolys=0;
  6. DWORD gShowBirdView=0;
  7. DWORD gEnableDetailTexture=1;
  8. DWORD gEnableTextureSort=1;
  9. DWORD gEnableAlphaSort=1;
  10. DWORD gEnableMultiTexture=1;
  11. DWORD gEnableLightMaps=1;
  12. static bool __stdcall CheckDetailTexture()
  13. {
  14. return gEnableDetailTexture!=0;
  15. }
  16. static bool __stdcall CheckTextureSort()
  17. {
  18. return gEnableTextureSort!=0;
  19. }
  20. static bool __stdcall CheckAlphaSort()
  21. {
  22. return gEnableAlphaSort!=0;
  23. }
  24. static bool __stdcall CheckMultiTexture()
  25. {
  26. return gEnableMultiTexture!=0;
  27. }
  28. static bool __stdcall CheckLightMaps()
  29. {
  30. return gEnableLightMaps!=0;
  31. }
  32. static void __stdcall EnableDetailTexture()
  33. {
  34. gEnableDetailTexture=!gEnableDetailTexture;
  35. }
  36. static void __stdcall EnableTextureSort()
  37. {
  38. gEnableTextureSort=!gEnableTextureSort;
  39. }
  40. static void __stdcall EnableAlphaSort()
  41. {
  42. gEnableAlphaSort=!gEnableAlphaSort;
  43. }
  44. static void __stdcall EnableMultiTexture()
  45. {
  46. gEnableMultiTexture=!gEnableMultiTexture;
  47. }
  48. static void __stdcall EnableLightMaps()
  49. {
  50. gEnableLightMaps=!gEnableLightMaps;
  51. }
  52. extern DWORD gShowClippedPolys;
  53. static bool __stdcall Check_ShowClippedPolys() {return gShowClippedPolys!=0;}
  54. static void __stdcall Toggle_ShowClippedPolys() {gShowClippedPolys=!gShowClippedPolys;}
  55. extern DWORD gShowBirdView;
  56. static bool __stdcall Check_ShowBirdView() {return gShowBirdView!=0;}
  57. static void __stdcall Toggle_ShowBirdView() {gShowBirdView=!gShowBirdView;}
  58. unsigned
  59. Limits::Max_Number_Vertices_Per_Frame,
  60. Limits::Max_Number_Primitives_Per_Frame,
  61. Limits::Max_Number_ScreenQuads_Per_Frame,
  62. Limits::Max_Size_Of_LightMap_MemoryStream;
  63. HGOSHEAP
  64. MidLevelRenderer::Heap = NULL,
  65. MidLevelRenderer::StaticHeap = NULL;
  66. DEFINE_TIMER(MidLevelRenderer, Scene_Draw_Time);
  67. DEFINE_TIMER(MidLevelRenderer, Transform_Time);
  68. DEFINE_TIMER(MidLevelRenderer, Clipping_Time);
  69. DEFINE_TIMER(MidLevelRenderer, GOS_Draw_Time);
  70. DEFINE_TIMER(MidLevelRenderer, Vertex_Light_Time);
  71. DEFINE_TIMER(MidLevelRenderer, LightMap_Light_Time);
  72. DEFINE_TIMER(MidLevelRenderer, Texture_Sorting_Time);
  73. DEFINE_TIMER(MidLevelRenderer, Alpha_Sorting_Time);
  74. DEFINE_TIMER(MidLevelRenderer, Unlock_Texture_Time);
  75. DWORD MidLevelRenderer::Number_Of_Primitives;
  76. DWORD MidLevelRenderer::NumAllIndices;
  77. DWORD MidLevelRenderer::NumAllVertices;
  78. float MidLevelRenderer::Index_Over_Vertex_Ratio;
  79. DWORD MidLevelRenderer::TransformedVertices;
  80. DWORD MidLevelRenderer::NumberOfAlphaSortedTriangles;
  81. DWORD MidLevelRenderer::LitVertices;
  82. DWORD MidLevelRenderer::NonClippedVertices;
  83. DWORD MidLevelRenderer::ClippedVertices;
  84. DWORD MidLevelRenderer::PolysClippedButOutside;
  85. DWORD MidLevelRenderer::PolysClippedButInside;
  86. DWORD MidLevelRenderer::PolysClippedButOnePlane;
  87. DWORD MidLevelRenderer::PolysClippedButGOnePlane;
  88. bool MidLevelRenderer::ConvertToTriangleMeshes = true;
  89. bool MidLevelRenderer::PerspectiveMode = true;
  90. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  91. //
  92. void
  93. MidLevelRenderer::InitializeClasses(
  94. unsigned Max_Number_Vertices_Per_Frame,
  95. unsigned Max_Number_Primitives_Per_Frame,
  96. unsigned Max_Number_ScreenQuads_Per_Frame,
  97. unsigned Max_Size_Of_LightMap_MemoryStream,
  98. bool Convert_To_Triangle_Meshes
  99. )
  100. {
  101. Verify(FirstFreeMLRClassID <= LastMLRClassID);
  102. Verify(!StaticHeap);
  103. StaticHeap = gos_CreateMemoryHeap("MLR Static");
  104. Check_Pointer(StaticHeap);
  105. gos_PushCurrentHeap(StaticHeap);
  106. Verify(!Heap);
  107. Heap = gos_CreateMemoryHeap("MLR");
  108. Check_Pointer(Heap);
  109. Limits::Max_Number_Vertices_Per_Frame = Max_Number_Vertices_Per_Frame;
  110. Limits::Max_Number_Primitives_Per_Frame = Max_Number_Primitives_Per_Frame;
  111. Limits::Max_Number_ScreenQuads_Per_Frame = Max_Number_ScreenQuads_Per_Frame;
  112. Limits::Max_Size_Of_LightMap_MemoryStream = Max_Size_Of_LightMap_MemoryStream;
  113. ConvertToTriangleMeshes = Convert_To_Triangle_Meshes;
  114. MLRLight::InitializeClass();
  115. MLRTexturePool::InitializeClass();
  116. MLRClipper::InitializeClass();
  117. MLRSorter::InitializeClass();
  118. MLRSortByOrder::InitializeClass();
  119. MLRShape::InitializeClass();
  120. MLREffect::InitializeClass();
  121. MLRPointCloud::InitializeClass();
  122. MLRTriangleCloud::InitializeClass();
  123. MLRNGonCloud::InitializeClass();
  124. MLRCardCloud::InitializeClass();
  125. MLRAmbientLight::InitializeClass();
  126. MLRInfiniteLight::InitializeClass();
  127. MLRInfiniteLightWithFalloff::InitializeClass();
  128. MLRPointLight::InitializeClass();
  129. MLRSpotLight::InitializeClass();
  130. // MLRLightMap::InitializeClass();
  131. MLRPrimitiveBase::InitializeClass();
  132. MLRIndexedPrimitiveBase::InitializeClass();
  133. MLR_I_PMesh::InitializeClass();
  134. MLR_I_C_PMesh::InitializeClass();
  135. MLR_I_L_PMesh::InitializeClass();
  136. MLR_I_DT_PMesh::InitializeClass();
  137. MLR_I_C_DT_PMesh::InitializeClass();
  138. MLR_I_L_DT_PMesh::InitializeClass();
  139. MLR_I_MT_PMesh::InitializeClass();
  140. MLR_I_DeT_PMesh::InitializeClass();
  141. MLR_I_C_DeT_PMesh::InitializeClass();
  142. MLR_I_L_DeT_PMesh::InitializeClass();
  143. MLR_I_TMesh::InitializeClass();
  144. MLR_I_DeT_TMesh::InitializeClass();
  145. MLR_I_C_TMesh::InitializeClass();
  146. MLR_I_L_TMesh::InitializeClass();
  147. // MLR_Terrain::InitializeClass();
  148. // MLR_Terrain2::InitializeClass();
  149. MLRLineCloud::InitializeClass();
  150. MLRIndexedTriangleCloud::InitializeClass();
  151. MLR_I_DT_TMesh::InitializeClass();
  152. MLR_I_C_DT_TMesh::InitializeClass();
  153. MLR_I_L_DT_TMesh::InitializeClass();
  154. MLR_I_C_DeT_TMesh::InitializeClass();
  155. MLR_I_L_DeT_TMesh::InitializeClass();
  156. MLRLookUpLight::InitializeClass();
  157. #if FOG_HACK
  158. // for(int i=0;i<Limits::Max_Number_Of_FogStates;i++)
  159. // {
  160. // GOSVertex::SetFogTableEntry(i+1, 700.0f, 1000.0f, 0.0f);
  161. // }
  162. #endif
  163. gos_PopCurrentHeap();
  164. //
  165. //-------------------------
  166. // Setup the debugger menus
  167. //-------------------------
  168. //
  169. AddDebuggerMenuItem(
  170. "Libraries\\MLR\\Show Clipped Polygons",
  171. Check_ShowClippedPolys,
  172. Toggle_ShowClippedPolys,
  173. NULL
  174. );
  175. AddDebuggerMenuItem(
  176. "Libraries\\MLR\\Show Bird View",
  177. Check_ShowBirdView,
  178. Toggle_ShowBirdView,
  179. NULL
  180. );
  181. AddDebuggerMenuItem("Libraries\\MLR\\Texture Sort", CheckTextureSort, EnableTextureSort, NULL );
  182. AddDebuggerMenuItem("Libraries\\MLR\\Enable Detail Texture", CheckDetailTexture, EnableDetailTexture, NULL );
  183. AddDebuggerMenuItem("Libraries\\MLR\\Alpha Sort", CheckAlphaSort, EnableAlphaSort, NULL );
  184. AddDebuggerMenuItem("Libraries\\MLR\\MultiTexture Enabled", CheckMultiTexture, EnableMultiTexture, NULL );
  185. AddDebuggerMenuItem("Libraries\\MLR\\LightMaps Enabled", CheckLightMaps, EnableLightMaps, NULL );
  186. //
  187. //---------------------
  188. // Setup the statistics
  189. //---------------------
  190. //
  191. StatisticFormat( "" );
  192. StatisticFormat( "Mid Level Renderer" );
  193. StatisticFormat( "==================" );
  194. StatisticFormat( "" );
  195. Initialize_Timer(Transform_Time, "Transform Time");
  196. Initialize_Timer(Clipping_Time, "Clipping Time");
  197. Initialize_Timer(GOS_Draw_Time, "GOS Draw Time");
  198. Initialize_Timer(Vertex_Light_Time, "Vertex Light Time");
  199. Initialize_Timer(LightMap_Light_Time, "LightMap Light Time");
  200. Initialize_Timer(Texture_Sorting_Time, "Texture Sorting Time");
  201. Initialize_Timer(Alpha_Sorting_Time, "Alpha Sorting Time");
  202. Initialize_Timer(Unlock_Texture_Time, "Unlock Texture Time");
  203. AddStatistic( "MLR Primitives", "prims", gos_DWORD, &Number_Of_Primitives, Stat_AutoReset );
  204. AddStatistic( "Indices/Vertices", "Ratio", gos_float, &Index_Over_Vertex_Ratio, Stat_AutoReset+Stat_2DP );
  205. AddStatistic( "Transformed vertices", "vertices", gos_DWORD, &TransformedVertices, Stat_AutoReset );
  206. AddStatistic( "Number of alphasorted Tri", "tri", gos_DWORD, &NumberOfAlphaSortedTriangles, Stat_AutoReset );
  207. AddStatistic( "Lit vertices", "vertices", gos_DWORD, &LitVertices, Stat_AutoReset );
  208. AddStatistic( "Unclipped vertices", "vertices", gos_DWORD, &NonClippedVertices, Stat_AutoReset );
  209. AddStatistic( "Clipped vertices", "vertices", gos_DWORD, &ClippedVertices, Stat_AutoReset );
  210. // Polygons in primitives which are clipped but polys are outside the viewing frustrum
  211. AddStatistic( "Clip: Offscreen", "Poly", gos_DWORD, &PolysClippedButOutside, Stat_AutoReset );
  212. // Polygons in primitives which are clipped but polys are inside the viewing frustrum
  213. AddStatistic( "Clip: Onscreen", "Poly", gos_DWORD, &PolysClippedButInside, Stat_AutoReset );
  214. // Polygons in primitives which are clipped, polys clipped against one plain
  215. AddStatistic( "Clip: One Plane", "Poly", gos_DWORD, &PolysClippedButOnePlane, Stat_AutoReset );
  216. // Polygons in primitives which are clipped, polys clipped against more than one plain
  217. AddStatistic( "Clip: > One Plane", "Poly", gos_DWORD, &PolysClippedButGOnePlane, Stat_AutoReset );
  218. }
  219. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  220. //
  221. void
  222. MidLevelRenderer::TerminateClasses()
  223. {
  224. if ( !MLRLookUpLight::DefaultData ) // make sure there is something to termiante
  225. return;
  226. MLRLookUpLight::TerminateClass();
  227. MLR_I_L_DeT_TMesh::TerminateClass();
  228. MLR_I_C_DeT_TMesh::TerminateClass();
  229. MLR_I_L_DT_TMesh::TerminateClass();
  230. MLR_I_C_DT_TMesh::TerminateClass();
  231. MLR_I_DT_TMesh::TerminateClass();
  232. MLRIndexedTriangleCloud::TerminateClass();
  233. MLRLineCloud::TerminateClass();
  234. // MLR_Terrain2::TerminateClass();
  235. // MLR_Terrain::TerminateClass();
  236. MLR_I_L_TMesh::TerminateClass();
  237. MLR_I_C_TMesh::TerminateClass();
  238. MLR_I_DeT_TMesh::TerminateClass();
  239. MLR_I_TMesh::TerminateClass();
  240. MLR_I_L_DeT_PMesh::TerminateClass();
  241. MLR_I_C_DeT_PMesh::TerminateClass();
  242. MLR_I_DeT_PMesh::TerminateClass();
  243. MLR_I_MT_PMesh::TerminateClass();
  244. MLR_I_L_DT_PMesh::TerminateClass();
  245. MLR_I_C_DT_PMesh::TerminateClass();
  246. MLR_I_DT_PMesh::TerminateClass();
  247. MLR_I_L_PMesh::TerminateClass();
  248. MLR_I_C_PMesh::TerminateClass();
  249. MLR_I_PMesh::TerminateClass();
  250. MLRIndexedPrimitiveBase::TerminateClass();
  251. MLRPrimitiveBase::TerminateClass();
  252. // MLRLightMap::TerminateClass();
  253. MLRSpotLight::TerminateClass();
  254. MLRPointLight::TerminateClass();
  255. MLRInfiniteLightWithFalloff::TerminateClass();
  256. MLRInfiniteLight::TerminateClass();
  257. MLRAmbientLight::TerminateClass();
  258. MLRCardCloud::TerminateClass();
  259. MLRNGonCloud::TerminateClass();
  260. MLRTriangleCloud::TerminateClass();
  261. MLRPointCloud::TerminateClass();
  262. MLREffect::TerminateClass();
  263. MLRShape::TerminateClass();
  264. MLRSortByOrder::TerminateClass();
  265. MLRSorter::TerminateClass();
  266. MLRClipper::TerminateClass();
  267. MLRTexturePool::TerminateClass();
  268. MLRLight::TerminateClass();
  269. Check_Pointer(Heap);
  270. gos_DestroyMemoryHeap(Heap);
  271. Heap = NULL;
  272. Check_Pointer(StaticHeap);
  273. gos_DestroyMemoryHeap(StaticHeap);
  274. StaticHeap = NULL;
  275. }
  276. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  277. //
  278. int
  279. MidLevelRenderer::ReadMLRVersion(MemoryStream *erf_stream)
  280. {
  281. Check_Object(erf_stream);
  282. //
  283. //------------------------------------------------------------------------
  284. // See if this file has an erf signature. If so, the next int will be the
  285. // version number. If not, assume it is version 1 and rewind the file
  286. //------------------------------------------------------------------------
  287. //
  288. int version = -1;
  289. int erf_signature;
  290. *erf_stream >> erf_signature;
  291. if (erf_signature == 'MLR#')
  292. *erf_stream >> version;
  293. else
  294. erf_stream->RewindPointer(sizeof(erf_signature));
  295. if (version > Current_MLR_Version)
  296. STOP(("Application must be rebuilt to use this version of MLR data"));
  297. return version;
  298. }
  299. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  300. //
  301. void
  302. MidLevelRenderer::WriteMLRVersion(MemoryStream *erf_stream)
  303. {
  304. Check_Object(erf_stream);
  305. *erf_stream << 'MLR#' << static_cast<int>(Current_MLR_Version);
  306. }