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.

216 lines
6.5 KiB

  1. //===== Copyright 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "shaderlib/ShaderDLL.h"
  7. #include "materialsystem/IShader.h"
  8. #include "tier1/utlvector.h"
  9. #include "tier1/utllinkedlist.h"
  10. #include "tier0/dbg.h"
  11. #include "materialsystem/imaterialsystemhardwareconfig.h"
  12. #include "materialsystem/materialsystem_config.h"
  13. #include "materialsystem/ishadersystem.h"
  14. #include "materialsystem/ishaderapi.h"
  15. #include "shaderlib_cvar.h"
  16. #include "mathlib/mathlib.h"
  17. #include "tier2/tier2.h"
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include "tier0/memdbgon.h"
  20. //-----------------------------------------------------------------------------
  21. // The standard implementation of CShaderDLL
  22. //-----------------------------------------------------------------------------
  23. class CShaderDLL : public IShaderDLLInternal, public IShaderDLL
  24. {
  25. public:
  26. CShaderDLL();
  27. // methods of IShaderDLL
  28. virtual bool Connect( CreateInterfaceFn factory );
  29. virtual void Disconnect();
  30. // methods of IShaderDLLInternal
  31. virtual int ShaderCount() const;
  32. virtual IShader *GetShader( int nShader );
  33. int ShaderComboSemanticsCount() const;
  34. const ShaderComboSemantics_t *GetComboSemantics( int n );
  35. virtual bool Connect( CreateInterfaceFn factory, bool bIsMaterialSystem );
  36. virtual void Disconnect( bool bIsMaterialSystem );
  37. virtual void InsertShader( IShader *pShader );
  38. virtual void AddShaderComboInformation( const ShaderComboSemantics_t *pSemantics );
  39. private:
  40. CUtlVector< IShader * > m_ShaderList;
  41. CUtlLinkedList< const ShaderComboSemantics_t * > m_ShaderComboSemantics;
  42. };
  43. //-----------------------------------------------------------------------------
  44. // Global interfaces/structures
  45. //-----------------------------------------------------------------------------
  46. #if !defined( _PS3 ) && !defined( _OSX )
  47. IMaterialSystemHardwareConfig* g_pHardwareConfig;
  48. const MaterialSystem_Config_t *g_pConfig;
  49. #else
  50. extern MaterialSystem_Config_t g_config;
  51. const MaterialSystem_Config_t *g_pConfig = &g_config;
  52. #endif
  53. //-----------------------------------------------------------------------------
  54. // Interfaces/structures local to shaderlib
  55. //-----------------------------------------------------------------------------
  56. #ifndef _PS3
  57. IShaderSystem* g_pSLShaderSystem;
  58. #endif
  59. // Pattern necessary because shaders register themselves in global constructors
  60. static CShaderDLL *s_pShaderDLL;
  61. //-----------------------------------------------------------------------------
  62. // Global accessor
  63. //-----------------------------------------------------------------------------
  64. IShaderDLL *GetShaderDLL()
  65. {
  66. // Pattern necessary because shaders register themselves in global constructors
  67. if ( !s_pShaderDLL )
  68. {
  69. s_pShaderDLL = new CShaderDLL;
  70. }
  71. return s_pShaderDLL;
  72. }
  73. IShaderDLLInternal *GetShaderDLLInternal()
  74. {
  75. // Pattern necessary because shaders register themselves in global constructors
  76. if ( !s_pShaderDLL )
  77. {
  78. s_pShaderDLL = new CShaderDLL;
  79. }
  80. return static_cast<IShaderDLLInternal*>( s_pShaderDLL );
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Singleton interface
  84. //-----------------------------------------------------------------------------
  85. EXPOSE_INTERFACE_FN( (InstantiateInterfaceFn)GetShaderDLLInternal, IShaderDLLInternal, SHADER_DLL_INTERFACE_VERSION );
  86. //-----------------------------------------------------------------------------
  87. // Connect, disconnect...
  88. //-----------------------------------------------------------------------------
  89. CShaderDLL::CShaderDLL()
  90. {
  91. MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f );
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Connect, disconnect...
  95. //-----------------------------------------------------------------------------
  96. bool CShaderDLL::Connect( CreateInterfaceFn factory, bool bIsMaterialSystem )
  97. {
  98. #if defined( _PS3 )
  99. return true;
  100. #elif defined( OSX )
  101. g_pSLShaderSystem = (IShaderSystem*)factory( SHADERSYSTEM_INTERFACE_VERSION, NULL );
  102. return ( g_pSLShaderSystem != NULL );
  103. #else
  104. g_pHardwareConfig = (IMaterialSystemHardwareConfig*)factory( MATERIALSYSTEM_HARDWARECONFIG_INTERFACE_VERSION, NULL );
  105. g_pConfig = (const MaterialSystem_Config_t*)factory( MATERIALSYSTEM_CONFIG_VERSION, NULL );
  106. g_pSLShaderSystem = (IShaderSystem*)factory( SHADERSYSTEM_INTERFACE_VERSION, NULL );
  107. if ( !bIsMaterialSystem )
  108. {
  109. ConnectTier1Libraries( &factory, 1 );
  110. InitShaderLibCVars( factory );
  111. }
  112. return ( g_pConfig != NULL ) && (g_pHardwareConfig != NULL) && ( g_pSLShaderSystem != NULL );
  113. #endif
  114. }
  115. void CShaderDLL::Disconnect( bool bIsMaterialSystem )
  116. {
  117. #if defined( OSX )
  118. g_pSLShaderSystem = NULL;
  119. #elif !defined( _PS3 )
  120. if ( !bIsMaterialSystem )
  121. {
  122. ConVar_Unregister();
  123. DisconnectTier1Libraries();
  124. }
  125. g_pHardwareConfig = NULL;
  126. g_pConfig = NULL;
  127. g_pSLShaderSystem = NULL;
  128. #endif
  129. }
  130. bool CShaderDLL::Connect( CreateInterfaceFn factory )
  131. {
  132. return Connect( factory, false );
  133. }
  134. void CShaderDLL::Disconnect()
  135. {
  136. Disconnect( false );
  137. }
  138. //-----------------------------------------------------------------------------
  139. // Iterates over all shaders
  140. //-----------------------------------------------------------------------------
  141. int CShaderDLL::ShaderCount() const
  142. {
  143. return m_ShaderList.Count();
  144. }
  145. IShader *CShaderDLL::GetShader( int nShader )
  146. {
  147. if ( ( nShader < 0 ) || ( nShader >= m_ShaderList.Count() ) )
  148. return NULL;
  149. return m_ShaderList[nShader];
  150. }
  151. //-----------------------------------------------------------------------------
  152. // Adds to the shader lists
  153. //-----------------------------------------------------------------------------
  154. void CShaderDLL::InsertShader( IShader *pShader )
  155. {
  156. Assert( pShader );
  157. m_ShaderList.AddToTail( pShader );
  158. }
  159. //-----------------------------------------------------------------------------
  160. // Iterates over all shader semantics
  161. //-----------------------------------------------------------------------------
  162. int CShaderDLL::ShaderComboSemanticsCount() const
  163. {
  164. return m_ShaderComboSemantics.Count();
  165. }
  166. const ShaderComboSemantics_t *CShaderDLL::GetComboSemantics( int n )
  167. {
  168. if ( ( n < 0 ) || ( n >= m_ShaderComboSemantics.Count() ) )
  169. return NULL;
  170. return m_ShaderComboSemantics[n];
  171. }
  172. //-----------------------------------------------------------------------------
  173. // Adds to the shader combo semantics list
  174. //-----------------------------------------------------------------------------
  175. void CShaderDLL::AddShaderComboInformation( const ShaderComboSemantics_t *pSemantics )
  176. {
  177. m_ShaderComboSemantics.AddToTail( pSemantics );
  178. }