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.

316 lines
10 KiB

  1. #include "cbase.h"
  2. #include "keyvalues.h"
  3. #include "cdll_client_int.h"
  4. #include "view_scene.h"
  5. #include "viewrender.h"
  6. #include "tier0/icommandline.h"
  7. #include "materialsystem/imesh.h"
  8. #include "materialsystem/imaterial.h"
  9. #include "materialsystem/imaterialsystemhardwareconfig.h"
  10. #include "materialsystem/imaterialvar.h"
  11. #include "ScreenSpaceEffects.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. //------------------------------------------------------------------------------
  15. // CScreenSpaceEffectRegistration code
  16. // Used to register and effect with the IScreenSpaceEffectManager
  17. //------------------------------------------------------------------------------
  18. CScreenSpaceEffectRegistration *CScreenSpaceEffectRegistration::s_pHead = NULL;
  19. CScreenSpaceEffectRegistration::CScreenSpaceEffectRegistration( const char *pName, IScreenSpaceEffect *pEffect )
  20. {
  21. m_pEffectName = pName;
  22. m_pEffect = pEffect;
  23. m_pNext = s_pHead;
  24. s_pHead = this;
  25. }
  26. //------------------------------------------------------------------------------
  27. // CScreenSpaceEffectManager - Implementation of IScreenSpaceEffectManager
  28. //------------------------------------------------------------------------------
  29. class CScreenSpaceEffectManager : public IScreenSpaceEffectManager
  30. {
  31. public:
  32. virtual void InitScreenSpaceEffects( );
  33. virtual void ShutdownScreenSpaceEffects( );
  34. virtual IScreenSpaceEffect *GetScreenSpaceEffect( const char *pEffectName );
  35. virtual void SetScreenSpaceEffectParams( const char *pEffectName, KeyValues *params );
  36. virtual void SetScreenSpaceEffectParams( IScreenSpaceEffect *pEffect, KeyValues *params );
  37. virtual void EnableScreenSpaceEffect( const char *pEffectName );
  38. virtual void EnableScreenSpaceEffect( IScreenSpaceEffect *pEffect );
  39. virtual void DisableScreenSpaceEffect( const char *pEffectName );
  40. virtual void DisableScreenSpaceEffect( IScreenSpaceEffect *pEffect );
  41. virtual void DisableAllScreenSpaceEffects( );
  42. virtual void RenderEffects( int x, int y, int w, int h );
  43. };
  44. CScreenSpaceEffectManager g_ScreenSpaceEffectManager;
  45. IScreenSpaceEffectManager *g_pScreenSpaceEffects = &g_ScreenSpaceEffectManager;
  46. //---------------------------------------------------------------------------------------
  47. // CScreenSpaceEffectManager::InitScreenSpaceEffects - Initialise all registered effects
  48. //---------------------------------------------------------------------------------------
  49. void CScreenSpaceEffectManager::InitScreenSpaceEffects( )
  50. {
  51. if ( CommandLine()->FindParm( "-filmgrain" ) )
  52. {
  53. GetScreenSpaceEffect( "filmgrain" )->Enable( true );
  54. }
  55. for( CScreenSpaceEffectRegistration *pReg=CScreenSpaceEffectRegistration::s_pHead; pReg!=NULL; pReg=pReg->m_pNext )
  56. {
  57. IScreenSpaceEffect *pEffect = pReg->m_pEffect;
  58. if( pEffect )
  59. {
  60. bool bIsEnabled = pEffect->IsEnabled( );
  61. pEffect->Init( );
  62. pEffect->Enable( bIsEnabled );
  63. }
  64. }
  65. }
  66. //----------------------------------------------------------------------------------------
  67. // CScreenSpaceEffectManager::ShutdownScreenSpaceEffects - Shutdown all registered effects
  68. //----------------------------------------------------------------------------------------
  69. void CScreenSpaceEffectManager::ShutdownScreenSpaceEffects( )
  70. {
  71. for( CScreenSpaceEffectRegistration *pReg=CScreenSpaceEffectRegistration::s_pHead; pReg!=NULL; pReg=pReg->m_pNext )
  72. {
  73. IScreenSpaceEffect *pEffect = pReg->m_pEffect;
  74. if( pEffect )
  75. {
  76. pEffect->Shutdown( );
  77. }
  78. }
  79. }
  80. //---------------------------------------------------------------------------------------
  81. // CScreenSpaceEffectManager::GetScreenSpaceEffect - Returns a point to the named effect
  82. //---------------------------------------------------------------------------------------
  83. IScreenSpaceEffect *CScreenSpaceEffectManager::GetScreenSpaceEffect( const char *pEffectName )
  84. {
  85. for( CScreenSpaceEffectRegistration *pReg=CScreenSpaceEffectRegistration::s_pHead; pReg!=NULL; pReg=pReg->m_pNext )
  86. {
  87. if( !Q_stricmp( pReg->m_pEffectName, pEffectName ) )
  88. {
  89. IScreenSpaceEffect *pEffect = pReg->m_pEffect;
  90. return pEffect;
  91. }
  92. }
  93. Warning( "Could not find screen space effect %s\n", pEffectName );
  94. return NULL;
  95. }
  96. //---------------------------------------------------------------------------------------
  97. // CScreenSpaceEffectManager::SetScreenSpaceEffectParams
  98. // - Assign parameters to the specified effect
  99. //---------------------------------------------------------------------------------------
  100. void CScreenSpaceEffectManager::SetScreenSpaceEffectParams( const char *pEffectName, KeyValues *params )
  101. {
  102. IScreenSpaceEffect *pEffect = GetScreenSpaceEffect( pEffectName );
  103. if( pEffect )
  104. SetScreenSpaceEffectParams( pEffect, params );
  105. }
  106. void CScreenSpaceEffectManager::SetScreenSpaceEffectParams( IScreenSpaceEffect *pEffect, KeyValues *params )
  107. {
  108. if( pEffect )
  109. pEffect->SetParameters( params );
  110. }
  111. //---------------------------------------------------------------------------------------
  112. // CScreenSpaceEffectManager::EnableScreenSpaceEffect
  113. // - Enables the specified effect
  114. //---------------------------------------------------------------------------------------
  115. void CScreenSpaceEffectManager::EnableScreenSpaceEffect( const char *pEffectName )
  116. {
  117. IScreenSpaceEffect *pEffect = GetScreenSpaceEffect( pEffectName );
  118. if( pEffect )
  119. EnableScreenSpaceEffect( pEffect );
  120. }
  121. void CScreenSpaceEffectManager::EnableScreenSpaceEffect( IScreenSpaceEffect *pEffect )
  122. {
  123. if( pEffect )
  124. pEffect->Enable( true );
  125. }
  126. //---------------------------------------------------------------------------------------
  127. // CScreenSpaceEffectManager::DisableScreenSpaceEffect
  128. // - Disables the specified effect
  129. //---------------------------------------------------------------------------------------
  130. void CScreenSpaceEffectManager::DisableScreenSpaceEffect( const char *pEffectName )
  131. {
  132. IScreenSpaceEffect *pEffect = GetScreenSpaceEffect( pEffectName );
  133. if( pEffect )
  134. DisableScreenSpaceEffect( pEffect );
  135. }
  136. void CScreenSpaceEffectManager::DisableScreenSpaceEffect( IScreenSpaceEffect *pEffect )
  137. {
  138. if( pEffect )
  139. pEffect->Enable( false );
  140. }
  141. //---------------------------------------------------------------------------------------
  142. // CScreenSpaceEffectManager::DisableAllScreenSpaceEffects
  143. // - Disables all registered screen space effects
  144. //---------------------------------------------------------------------------------------
  145. void CScreenSpaceEffectManager::DisableAllScreenSpaceEffects( )
  146. {
  147. for( CScreenSpaceEffectRegistration *pReg=CScreenSpaceEffectRegistration::s_pHead; pReg!=NULL; pReg=pReg->m_pNext )
  148. {
  149. IScreenSpaceEffect *pEffect = pReg->m_pEffect;
  150. if( pEffect )
  151. {
  152. pEffect->Enable( false );
  153. }
  154. }
  155. }
  156. //---------------------------------------------------------------------------------------
  157. // CScreenSpaceEffectManager::RenderEffects
  158. // - Renders all registered screen space effects
  159. //---------------------------------------------------------------------------------------
  160. void CScreenSpaceEffectManager::RenderEffects( int x, int y, int w, int h )
  161. {
  162. for( CScreenSpaceEffectRegistration *pReg=CScreenSpaceEffectRegistration::s_pHead; pReg!=NULL; pReg=pReg->m_pNext )
  163. {
  164. IScreenSpaceEffect *pEffect = pReg->m_pEffect;
  165. if( pEffect )
  166. {
  167. pEffect->Render( x, y, w, h );
  168. }
  169. }
  170. }
  171. //------------------------------------------------------------------------------
  172. // Example post-processing effect
  173. //------------------------------------------------------------------------------
  174. class CExampleEffect : public IScreenSpaceEffect
  175. {
  176. public:
  177. CExampleEffect( );
  178. ~CExampleEffect( );
  179. void Init( );
  180. void Shutdown( );
  181. void SetParameters( KeyValues *params );
  182. void Render( int x, int y, int w, int h );
  183. void Enable( bool bEnable );
  184. bool IsEnabled( );
  185. private:
  186. bool m_bEnable;
  187. CMaterialReference m_Material;
  188. };
  189. ADD_SCREENSPACE_EFFECT( CExampleEffect, exampleeffect );
  190. //------------------------------------------------------------------------------
  191. // CExampleEffect constructor
  192. //------------------------------------------------------------------------------
  193. CExampleEffect::CExampleEffect( )
  194. {
  195. m_bEnable = false;
  196. }
  197. //------------------------------------------------------------------------------
  198. // CExampleEffect destructor
  199. //------------------------------------------------------------------------------
  200. CExampleEffect::~CExampleEffect( )
  201. {
  202. }
  203. //------------------------------------------------------------------------------
  204. // CExampleEffect init
  205. //------------------------------------------------------------------------------
  206. void CExampleEffect::Init( )
  207. {
  208. // This is just example code, init your effect material here
  209. //m_Material.Init( "engine/exampleeffect", TEXTURE_GROUP_OTHER );
  210. m_bEnable = false;
  211. }
  212. //------------------------------------------------------------------------------
  213. // CExampleEffect shutdown
  214. //------------------------------------------------------------------------------
  215. void CExampleEffect::Shutdown( )
  216. {
  217. m_Material.Shutdown();
  218. }
  219. //------------------------------------------------------------------------------
  220. // CExampleEffect enable
  221. //------------------------------------------------------------------------------
  222. void CExampleEffect::Enable( bool bEnable )
  223. {
  224. // This is just example code, don't enable it
  225. // m_bEnable = bEnable;
  226. }
  227. bool CExampleEffect::IsEnabled( )
  228. {
  229. return m_bEnable;
  230. }
  231. //------------------------------------------------------------------------------
  232. // CExampleEffect SetParameters
  233. //------------------------------------------------------------------------------
  234. void CExampleEffect::SetParameters( KeyValues *params )
  235. {
  236. if( params->GetDataType( "example_param" ) == KeyValues::TYPE_STRING )
  237. {
  238. // ...
  239. }
  240. }
  241. //------------------------------------------------------------------------------
  242. // CExampleEffect render
  243. //------------------------------------------------------------------------------
  244. void CExampleEffect::Render( int x, int y, int w, int h )
  245. {
  246. if ( !IsEnabled() )
  247. return;
  248. // Render Effect
  249. Rect_t actualRect;
  250. UpdateScreenEffectTexture( 0, x, y, w, h, false, &actualRect );
  251. ITexture *pTexture = GetFullFrameFrameBufferTexture( 0 );
  252. CMatRenderContextPtr pRenderContext( materials );
  253. pRenderContext->DrawScreenSpaceRectangle( m_Material, x, y, w, h,
  254. actualRect.x, actualRect.y, actualRect.x+actualRect.width-1, actualRect.y+actualRect.height-1,
  255. pTexture->GetActualWidth(), pTexture->GetActualHeight() );
  256. }