Counter Strike : Global Offensive Source Code
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.

236 lines
5.8 KiB

  1. //========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. // NOTE: This must be the last file included!!!
  9. #include "tier0/memdbgon.h"
  10. using namespace SF::GFx;
  11. using namespace SF::Render;
  12. void ScaleformUIImpl::InitRendererImpl( void )
  13. {
  14. if ( m_pShaderDeviceMgr )
  15. {
  16. m_pDeviceCallbacks = new DeviceCallbacks();
  17. m_pDeviceCallbacks->m_pScaleform = this;
  18. m_pShaderDeviceMgr->AddDeviceDependentObject( m_pDeviceCallbacks );
  19. }
  20. m_iScreenHeight = 768;
  21. m_iScreenWidth = 1024;
  22. }
  23. void ScaleformUIImpl::ShutdownRendererImpl( void )
  24. {
  25. if ( m_pShaderDeviceMgr )
  26. {
  27. if ( m_pDeviceCallbacks )
  28. {
  29. m_pShaderDeviceMgr->RemoveDeviceDependentObject( m_pDeviceCallbacks );
  30. delete m_pDeviceCallbacks;
  31. m_pDeviceCallbacks = NULL;
  32. }
  33. }
  34. #if defined( WIN32 ) && !defined( DX_TO_GL_ABSTRACTION )
  35. if ( m_pD3D9Stateblock )
  36. {
  37. m_pD3D9Stateblock->Release();
  38. m_pD3D9Stateblock = NULL;
  39. }
  40. #endif
  41. if ( m_pRenderHAL )
  42. {
  43. m_pRenderHAL->ShutdownHAL();
  44. m_pRenderer2D.Clear();
  45. m_pRenderHAL.Clear();
  46. }
  47. }
  48. void ScaleformUIImpl::SaveRenderingState( void )
  49. {
  50. #if defined( WIN32 ) && !defined( DX_TO_GL_ABSTRACTION )
  51. if ( m_pD3D9Stateblock )
  52. {
  53. m_pD3D9Stateblock->Capture();
  54. }
  55. // SF does not touch SRGB settings so make sure they are consistent
  56. m_pDevice->GetRenderState( D3DRS_SRGBWRITEENABLE, &m_srgbRenderState );
  57. m_pDevice->SetRenderState( D3DRS_SRGBWRITEENABLE, FALSE );
  58. for ( int i = 0; i < 16; i++ )
  59. {
  60. m_pDevice->GetSamplerState( i, D3DSAMP_SRGBTEXTURE, &m_pSavedSrgbSamplerStates[i] );
  61. m_pDevice->SetSamplerState( i, D3DSAMP_SRGBTEXTURE, FALSE );
  62. }
  63. #elif defined( DX_TO_GL_ABSTRACTION )
  64. if (m_pDevice)
  65. {
  66. m_pDevice->SaveGLState();
  67. }
  68. #endif
  69. }
  70. void ScaleformUIImpl::RestoreRenderingState( void )
  71. {
  72. #if defined( WIN32 ) && !defined( DX_TO_GL_ABSTRACTION )
  73. if ( m_pD3D9Stateblock )
  74. {
  75. m_pD3D9Stateblock->Apply();
  76. }
  77. // Restore srgb render and sampler states
  78. // (Looks like state block capture/apply didn't work, should investigate at a later state)
  79. m_pDevice->SetRenderState( D3DRS_SRGBWRITEENABLE, m_srgbRenderState );
  80. for ( int i = 0; i < 16; i++ )
  81. {
  82. m_pDevice->SetSamplerState( i, D3DSAMP_SRGBTEXTURE, m_pSavedSrgbSamplerStates[i] );
  83. }
  84. #elif defined( DX_TO_GL_ABSTRACTION )
  85. if (m_pDevice)
  86. {
  87. m_pDevice->RestoreGLState();
  88. }
  89. #endif
  90. }
  91. void ScaleformUIImpl::SetRenderingDevice( IDirect3DDevice9 *pDevice, D3DPRESENT_PARAMETERS *pPresentParameters, HWND hWnd )
  92. {
  93. #if defined(DX_TO_GL_ABSTRACTION)
  94. if ( !m_pThreadCommandQueue )
  95. {
  96. m_pThreadCommandQueue = new CScaleFormThreadCommandQueue;
  97. }
  98. #endif
  99. if ( !m_pRenderHAL )
  100. {
  101. // This is startup initialisation
  102. m_pDevice = pDevice;
  103. #if defined( WIN32 ) && !defined( DX_TO_GL_ABSTRACTION )
  104. m_pRenderHAL = *new D3D9::HAL();
  105. m_pRenderHAL->InitHAL( D3D9::HALInitParams( m_pDevice, *pPresentParameters, D3D9::HALConfig_NoSceneCalls ) );
  106. #else
  107. m_pRenderHAL = *new GL::HAL( m_pThreadCommandQueue );
  108. m_pRenderHAL->InitHAL( GL::HALInitParams( GL::HALConfig_DisableBinaryShaders | GL::HALConfig_DisableShaderPipelines, SF::ThreadId(), SF::String(), true ) );
  109. if ( m_pThreadCommandQueue )
  110. {
  111. m_pThreadCommandQueue->pHAL = m_pRenderHAL;
  112. }
  113. #endif
  114. Assert ( !m_pRenderer2D );
  115. m_pRenderer2D = *new Renderer2D( m_pRenderHAL.GetPtr() );
  116. if ( m_pThreadCommandQueue )
  117. {
  118. m_pThreadCommandQueue->pR2D = m_pRenderer2D;
  119. }
  120. InitFonts();
  121. SF::Ptr<CScaleformImageCreator> pImageCreator = *new CScaleformImageCreator( this, (TextureManager*)m_pRenderHAL->GetTextureManager() );
  122. m_pLoader->SetImageCreator( pImageCreator );
  123. }
  124. else
  125. {
  126. #if defined( WIN32 ) && !defined( DX_TO_GL_ABSTRACTION )
  127. m_pRenderHAL->RestoreAfterReset();
  128. #else
  129. m_pRenderHAL->ResetContext();
  130. #endif
  131. }
  132. #if defined( WIN32 ) && !defined( DX_TO_GL_ABSTRACTION )
  133. pDevice->CreateStateBlock( D3DSBT_ALL, &m_pD3D9Stateblock );
  134. #endif
  135. #if defined( SF_ENABLE_IME ) && defined( SF_ENABLE_IME_WIN32 )
  136. if ( !m_pIMEManager )
  137. {
  138. m_pIMEManager = *new ScaleformeUIIMEManager( hWnd, m_pGameEventManager );
  139. if ( m_pIMEManager )
  140. {
  141. m_pIMEManager->Init( m_pLoader->GetLog(), static_cast<Scaleform::GFx::FileOpener*>(m_pLoader->GetFileOpener().GetPtr()), NULL, false );
  142. m_pIMEManager->SetIMEMoviePath( "ime.swf" );
  143. m_pLoader->SetIMEManager( m_pIMEManager );
  144. m_pIMEManager->EnableIME( m_bIMEEnabled );
  145. }
  146. }
  147. #else
  148. NOTE_UNUSED( hWnd );
  149. #endif
  150. D3DVIEWPORT9 viewport;
  151. pDevice->GetViewport( &viewport );
  152. m_iScreenWidth = viewport.Width;
  153. m_iScreenHeight = viewport.Height;
  154. SetScreenSize( m_iScreenWidth, m_iScreenHeight );
  155. }
  156. void ScaleformUIImpl::NotifyRenderingDeviceLost( void )
  157. {
  158. #if defined( WIN32 ) && !defined( DX_TO_GL_ABSTRACTION )
  159. if ( !m_pRenderHAL )
  160. return;
  161. m_pRenderHAL->PrepareForReset();
  162. m_pD3D9Stateblock->Release();
  163. m_pD3D9Stateblock = NULL;
  164. #endif
  165. }
  166. #if defined( _WIN32 ) && !defined( DX_TO_GL_ABSTRACTION )
  167. void DumpMeshBufferSetStats( D3D9::MeshBufferSet *pMBSet )
  168. {
  169. int totalMem = 0;
  170. for ( int i = 0; i < MC_NUM_BUFFER_BUCKETS; i++ )
  171. {
  172. D3D9::MeshBufferBucket *pBucket = &pMBSet->m_buckets[i];
  173. Msg("Bucket %2d: created = %3d, free = %3d, total = %6d bytes (%4d KB)\n",
  174. i, pBucket->m_numCreated, pBucket->m_numFree,
  175. pBucket->m_totalMemUsed, pBucket->m_totalMemUsed / 1024 );
  176. totalMem += pBucket->m_totalMemUsed;
  177. }
  178. Msg( "Total = %d KB\n", totalMem/1024 );
  179. }
  180. #endif
  181. void ScaleformUIImpl::DumpMeshCacheStats( void )
  182. {
  183. #if defined( _WIN32 ) && !defined( DX_TO_GL_ABSTRACTION )
  184. if ( !m_pRenderHAL )
  185. {
  186. Msg( "No renderer, no HAL, no stats.\n" );
  187. return;
  188. }
  189. D3D9::MeshCache *mc = &m_pRenderHAL->GetMeshCache();
  190. // Log stats
  191. Msg( "SF MeshBufferSet, VB mem summary\n" );
  192. DumpMeshBufferSetStats( &mc->VertexBuffers );
  193. Msg( "SF MeshBufferSet, IB mem summary\n" );
  194. DumpMeshBufferSetStats( &mc->IndexBuffers );
  195. #endif
  196. }