Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

491 lines
13 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // The copyright to the contents herein is the property of Valve, L.L.C.
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. // $Header: $
  9. // $NoKeywords: $
  10. //
  11. // Material editor
  12. //=============================================================================
  13. #include <windows.h>
  14. #include "appframework/tier2app.h"
  15. #include "materialsystem/materialsystem_config.h"
  16. #include "materialsystem/imaterialsystemhardwareconfig.h"
  17. #include "materialsystem/imaterialsystem.h"
  18. #include "materialsystem/MaterialSystemUtil.h"
  19. #include "vstdlib/random.h"
  20. #include "filesystem.h"
  21. #include "filesystem_init.h"
  22. #include "tier0/icommandline.h"
  23. #include "tier1/KeyValues.h"
  24. #include "tier1/utlbuffer.h"
  25. #include "materialsystem/imesh.h"
  26. //-----------------------------------------------------------------------------
  27. // Purpose: Warning/Msg call back through this API
  28. // Input : type -
  29. // *pMsg -
  30. // Output : SpewRetval_t
  31. //-----------------------------------------------------------------------------
  32. SpewRetval_t SpewFunc( SpewType_t type, const char *pMsg )
  33. {
  34. if ( Plat_IsInDebugSession() )
  35. {
  36. OutputDebugString( pMsg );
  37. if ( type == SPEW_ASSERT )
  38. return SPEW_DEBUGGER;
  39. }
  40. return SPEW_CONTINUE;
  41. }
  42. //-----------------------------------------------------------------------------
  43. // The application object
  44. //-----------------------------------------------------------------------------
  45. class CMaterialSystemTestApp : public CTier2SteamApp
  46. {
  47. typedef CTier2SteamApp BaseClass;
  48. public:
  49. // Methods of IApplication
  50. virtual bool Create();
  51. virtual bool PreInit( );
  52. virtual int Main();
  53. virtual void PostShutdown( );
  54. virtual void Destroy();
  55. virtual const char *GetAppName() { return "MaterialSystemTest"; }
  56. virtual bool AppUsesReadPixels() { return false; }
  57. private:
  58. // Window management
  59. bool CreateAppWindow( const char *pTitle, bool bWindowed, int w, int h );
  60. // Sets up the game path
  61. bool SetupSearchPaths();
  62. // Waits for a keypress
  63. bool WaitForKeypress();
  64. // Sets the video mode
  65. bool SetMode();
  66. // Tests dynamic buffers
  67. void TestDynamicBuffers( IMatRenderContext *pRenderContext, bool bBuffered );
  68. // Creates, destroys a test material
  69. void CreateWireframeMaterial();
  70. void DestroyMaterial();
  71. CMaterialReference m_pMaterial;
  72. HWND m_HWnd;
  73. };
  74. DEFINE_WINDOWED_STEAM_APPLICATION_OBJECT( CMaterialSystemTestApp );
  75. //-----------------------------------------------------------------------------
  76. // Create all singleton systems
  77. //-----------------------------------------------------------------------------
  78. bool CMaterialSystemTestApp::Create()
  79. {
  80. SpewOutputFunc( SpewFunc );
  81. AppSystemInfo_t appSystems[] =
  82. {
  83. { "materialsystem.dll", MATERIAL_SYSTEM_INTERFACE_VERSION },
  84. // Required to terminate the list
  85. { "", "" }
  86. };
  87. if ( !AddSystems( appSystems ) )
  88. return false;
  89. IMaterialSystem *pMaterialSystem = (IMaterialSystem*)FindSystem( MATERIAL_SYSTEM_INTERFACE_VERSION );
  90. if ( !pMaterialSystem )
  91. {
  92. Warning( "CMaterialSystemTestApp::Create: Unable to connect to necessary interface!\n" );
  93. return false;
  94. }
  95. bool bIsVistaOrHigher = false;
  96. OSVERSIONINFO info;
  97. info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  98. if ( GetVersionEx( &info ) )
  99. {
  100. bIsVistaOrHigher = info.dwMajorVersion >= 6;
  101. }
  102. const char *pShaderDLL = CommandLine()->ParmValue( "-shaderdll" );
  103. if ( !pShaderDLL )
  104. {
  105. pShaderDLL = "shaderapidx10.dll";
  106. }
  107. if ( !bIsVistaOrHigher && !Q_stricmp( pShaderDLL, "shaderapidx10.dll" ) )
  108. {
  109. pShaderDLL = "shaderapidx9.dll";
  110. }
  111. pMaterialSystem->SetShaderAPI( pShaderDLL );
  112. return true;
  113. }
  114. void CMaterialSystemTestApp::Destroy()
  115. {
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Window callback
  119. //-----------------------------------------------------------------------------
  120. static LRESULT CALLBACK MaterialSystemTestWndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
  121. {
  122. switch( message )
  123. {
  124. case WM_DESTROY:
  125. PostQuitMessage( 0 );
  126. break;
  127. default:
  128. return DefWindowProc( hWnd, message, wParam, lParam );
  129. }
  130. return 0;
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Window management
  134. //-----------------------------------------------------------------------------
  135. bool CMaterialSystemTestApp::CreateAppWindow( const char *pTitle, bool bWindowed, int w, int h )
  136. {
  137. WNDCLASSEX wc;
  138. memset( &wc, 0, sizeof( wc ) );
  139. wc.cbSize = sizeof( wc );
  140. wc.style = CS_OWNDC | CS_DBLCLKS;
  141. wc.lpfnWndProc = MaterialSystemTestWndProc;
  142. wc.hInstance = (HINSTANCE)GetAppInstance();
  143. wc.lpszClassName = "Valve001";
  144. wc.hIcon = NULL; //LoadIcon( s_HInstance, MAKEINTRESOURCE( IDI_LAUNCHER ) );
  145. wc.hIconSm = wc.hIcon;
  146. RegisterClassEx( &wc );
  147. // Note, it's hidden
  148. DWORD style = WS_POPUP | WS_CLIPSIBLINGS;
  149. if ( bWindowed )
  150. {
  151. // Give it a frame
  152. style |= WS_OVERLAPPEDWINDOW;
  153. style &= ~WS_THICKFRAME;
  154. }
  155. // Never a max box
  156. style &= ~WS_MAXIMIZEBOX;
  157. RECT windowRect;
  158. windowRect.top = 0;
  159. windowRect.left = 0;
  160. windowRect.right = w;
  161. windowRect.bottom = h;
  162. // Compute rect needed for that size client area based on window style
  163. AdjustWindowRectEx(&windowRect, style, FALSE, 0);
  164. // Create the window
  165. m_HWnd = CreateWindow( wc.lpszClassName, pTitle, style, 0, 0,
  166. windowRect.right - windowRect.left, windowRect.bottom - windowRect.top,
  167. NULL, NULL, (HINSTANCE)GetAppInstance(), NULL );
  168. if (!m_HWnd)
  169. return false;
  170. int CenterX, CenterY;
  171. CenterX = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
  172. CenterY = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
  173. CenterX = (CenterX < 0) ? 0: CenterX;
  174. CenterY = (CenterY < 0) ? 0: CenterY;
  175. // In VCR modes, keep it in the upper left so mouse coordinates are always relative to the window.
  176. SetWindowPos (m_HWnd, NULL, CenterX, CenterY, 0, 0,
  177. SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
  178. return true;
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Sets up the game path
  182. //-----------------------------------------------------------------------------
  183. bool CMaterialSystemTestApp::SetupSearchPaths()
  184. {
  185. if ( !BaseClass::SetupSearchPaths( NULL, false, true ) )
  186. return false;
  187. g_pFullFileSystem->AddSearchPath( GetGameInfoPath(), "SKIN", PATH_ADD_TO_HEAD );
  188. return true;
  189. }
  190. //-----------------------------------------------------------------------------
  191. // PreInit, PostShutdown
  192. //-----------------------------------------------------------------------------
  193. bool CMaterialSystemTestApp::PreInit( )
  194. {
  195. if ( !BaseClass::PreInit() )
  196. return false;
  197. if ( !g_pFullFileSystem || !g_pMaterialSystem )
  198. return false;
  199. // Add paths...
  200. if ( !SetupSearchPaths() )
  201. return false;
  202. const char *pArg;
  203. int iWidth = 1024;
  204. int iHeight = 768;
  205. bool bWindowed = (CommandLine()->CheckParm( "-fullscreen" ) == NULL);
  206. if (CommandLine()->CheckParm( "-width", &pArg ))
  207. {
  208. iWidth = atoi( pArg );
  209. }
  210. if (CommandLine()->CheckParm( "-height", &pArg ))
  211. {
  212. iHeight = atoi( pArg );
  213. }
  214. if (!CreateAppWindow( "Press a Key To Continue", bWindowed, iWidth, iHeight ))
  215. return false;
  216. // Get the adapter from the command line....
  217. const char *pAdapterString;
  218. int nAdapter = 0;
  219. if ( CommandLine()->CheckParm( "-adapter", &pAdapterString ) )
  220. {
  221. nAdapter = atoi( pAdapterString );
  222. }
  223. int nAdapterFlags = 0;
  224. if ( AppUsesReadPixels() )
  225. {
  226. nAdapterFlags |= MATERIAL_INIT_ALLOCATE_FULLSCREEN_TEXTURE;
  227. }
  228. g_pMaterialSystem->SetAdapter( nAdapter, nAdapterFlags );
  229. return true;
  230. }
  231. void CMaterialSystemTestApp::PostShutdown( )
  232. {
  233. BaseClass::PostShutdown();
  234. }
  235. //-----------------------------------------------------------------------------
  236. // Waits for a keypress
  237. //-----------------------------------------------------------------------------
  238. bool CMaterialSystemTestApp::WaitForKeypress()
  239. {
  240. MSG msg = {0};
  241. while( WM_QUIT != msg.message )
  242. {
  243. if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  244. {
  245. TranslateMessage( &msg );
  246. DispatchMessage( &msg );
  247. }
  248. if ( msg.message == WM_KEYDOWN )
  249. return true;
  250. }
  251. return false;
  252. }
  253. //-----------------------------------------------------------------------------
  254. // Sets the video mode
  255. //-----------------------------------------------------------------------------
  256. bool CMaterialSystemTestApp::SetMode()
  257. {
  258. MaterialSystem_Config_t config;
  259. if ( CommandLine()->CheckParm( "-fullscreen" ) )
  260. {
  261. config.SetFlag( MATSYS_VIDCFG_FLAGS_WINDOWED, false );
  262. }
  263. else
  264. {
  265. config.SetFlag( MATSYS_VIDCFG_FLAGS_WINDOWED, true );
  266. }
  267. if ( CommandLine()->CheckParm( "-resizing" ) )
  268. {
  269. config.SetFlag( MATSYS_VIDCFG_FLAGS_RESIZING, true );
  270. }
  271. if ( CommandLine()->CheckParm( "-mat_vsync" ) )
  272. {
  273. config.SetFlag( MATSYS_VIDCFG_FLAGS_NO_WAIT_FOR_VSYNC, false );
  274. }
  275. config.m_nAASamples = CommandLine()->ParmValue( "-mat_antialias", 1 );
  276. config.m_nAAQuality = CommandLine()->ParmValue( "-mat_aaquality", 0 );
  277. config.m_VideoMode.m_Width = config.m_VideoMode.m_Height = 0;
  278. config.m_VideoMode.m_Format = IMAGE_FORMAT_BGRX8888;
  279. config.m_VideoMode.m_RefreshRate = 0;
  280. bool modeSet = g_pMaterialSystem->SetMode( m_HWnd, config );
  281. if (!modeSet)
  282. {
  283. Error( "Unable to set mode\n" );
  284. return false;
  285. }
  286. g_pMaterialSystem->OverrideConfig( config, false );
  287. return true;
  288. }
  289. //-----------------------------------------------------------------------------
  290. // Creates, destroys a test material
  291. //-----------------------------------------------------------------------------
  292. void CMaterialSystemTestApp::CreateWireframeMaterial()
  293. {
  294. KeyValues *pVMTKeyValues = new KeyValues( "Wireframe" );
  295. pVMTKeyValues->SetInt( "$vertexcolor", 1 );
  296. pVMTKeyValues->SetInt( "$nocull", 1 );
  297. pVMTKeyValues->SetInt( "$ignorez", 1 );
  298. m_pMaterial.Init( "__test", pVMTKeyValues );
  299. }
  300. void CMaterialSystemTestApp::DestroyMaterial()
  301. {
  302. m_pMaterial.Shutdown();
  303. }
  304. //-----------------------------------------------------------------------------
  305. // Tests dynamic buffers
  306. //-----------------------------------------------------------------------------
  307. void CMaterialSystemTestApp::TestDynamicBuffers( IMatRenderContext *pMatRenderContext, bool bBuffered )
  308. {
  309. CreateWireframeMaterial();
  310. g_pMaterialSystem->BeginFrame( 0 );
  311. pMatRenderContext->Bind( m_pMaterial );
  312. IMesh *pMesh = pMatRenderContext->GetDynamicMesh( bBuffered );
  313. // clear (so that we can make sure that we aren't getting results from the previous quad)
  314. pMatRenderContext->ClearColor3ub( RandomInt( 0, 100 ), RandomInt( 0, 100 ), RandomInt( 190, 255 ) );
  315. pMatRenderContext->ClearBuffers( true, true );
  316. static unsigned char s_pColors[4][4] =
  317. {
  318. { 255, 0, 0, 255 },
  319. { 0, 255, 0, 255 },
  320. { 0, 0, 255, 255 },
  321. { 255, 255, 255, 255 },
  322. };
  323. static int nCount = 0;
  324. const int nLoopCount = 8;
  325. float flWidth = 2.0f / nLoopCount;
  326. for ( int i = 0; i < nLoopCount; ++i )
  327. {
  328. CMeshBuilder mb;
  329. mb.Begin( pMesh, MATERIAL_TRIANGLES, 4, 6 );
  330. mb.Position3f( -1.0f + i * flWidth, -1.0f, 0.5f );
  331. mb.Normal3f( 0.0f, 0.0f, 1.0f );
  332. mb.Color4ubv( s_pColors[nCount++ % 4] );
  333. mb.AdvanceVertex();
  334. mb.Position3f( -1.0f + i * flWidth + flWidth, -1.0f, 0.5f );
  335. mb.Normal3f( 0.0f, 0.0f, 1.0f );
  336. mb.Color4ubv( s_pColors[nCount++ % 4] );
  337. mb.AdvanceVertex();
  338. mb.Position3f( -1.0f + i * flWidth + flWidth, 1.0f, 0.5f );
  339. mb.Normal3f( 0.0f, 0.0f, 1.0f );
  340. mb.Color4ubv( s_pColors[nCount++ % 4] );
  341. mb.AdvanceVertex();
  342. mb.Position3f( -1.0f + i * flWidth, 1.0f, 0.5f );
  343. mb.Normal3f( 0.0f, 0.0f, 1.0f );
  344. mb.Color4ubv( s_pColors[nCount++ % 4] );
  345. mb.AdvanceVertex();
  346. ++nCount;
  347. mb.FastIndex( 0 );
  348. mb.FastIndex( 2 );
  349. mb.FastIndex( 1 );
  350. mb.FastIndex( 0 );
  351. mb.FastIndex( 3 );
  352. mb.FastIndex( 2 );
  353. mb.End( true );
  354. pMesh->Draw( );
  355. }
  356. ++nCount;
  357. g_pMaterialSystem->EndFrame();
  358. g_pMaterialSystem->SwapBuffers();
  359. DestroyMaterial();
  360. }
  361. //-----------------------------------------------------------------------------
  362. // main application
  363. //-----------------------------------------------------------------------------
  364. int CMaterialSystemTestApp::Main()
  365. {
  366. if ( !SetMode() )
  367. return 0;
  368. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  369. // Sets up a full-screen viewport
  370. int w, h;
  371. pRenderContext->GetWindowSize( w, h );
  372. pRenderContext->Viewport( 0, 0, w, h );
  373. pRenderContext->DepthRange( 0.0f, 1.0f );
  374. // Clears the screen
  375. g_pMaterialSystem->BeginFrame( 0 );
  376. pRenderContext->ClearColor4ub( 76, 88, 68, 255 );
  377. pRenderContext->ClearBuffers( true, true );
  378. g_pMaterialSystem->EndFrame();
  379. g_pMaterialSystem->SwapBuffers();
  380. SetWindowText( m_HWnd, "Buffer clearing . . hit a key" );
  381. if ( !WaitForKeypress() )
  382. return 1;
  383. SetWindowText( m_HWnd, "Dynamic buffer test.. hit a key" );
  384. TestDynamicBuffers( pRenderContext, false );
  385. if ( !WaitForKeypress() )
  386. return 1;
  387. SetWindowText( m_HWnd, "Buffered dynamic buffer test.. hit a key" );
  388. TestDynamicBuffers( pRenderContext, true );
  389. if ( !WaitForKeypress() )
  390. return 1;
  391. return 1;
  392. }