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.

169 lines
5.0 KiB

  1. //========= Copyright 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 "tier0/dbg.h"
  10. #include "materialsystem/imaterialsystemhardwareconfig.h"
  11. #include "materialsystem/materialsystem_config.h"
  12. #include "IShaderSystem.h"
  13. #include "materialsystem/ishaderapi.h"
  14. #include "shaderlib_cvar.h"
  15. #include "mathlib/mathlib.h"
  16. #include "tier1/tier1.h"
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. //-----------------------------------------------------------------------------
  20. // The standard implementation of CShaderDLL
  21. //-----------------------------------------------------------------------------
  22. class CShaderDLL : public IShaderDLLInternal, public IShaderDLL
  23. {
  24. public:
  25. CShaderDLL();
  26. // methods of IShaderDLL
  27. virtual bool Connect( CreateInterfaceFn factory );
  28. virtual void Disconnect();
  29. virtual int ShaderCount() const;
  30. virtual IShader *GetShader( int nShader );
  31. // methods of IShaderDLLInternal
  32. virtual bool Connect( CreateInterfaceFn factory, bool bIsMaterialSystem );
  33. virtual void Disconnect( bool bIsMaterialSystem );
  34. virtual void InsertShader( IShader *pShader );
  35. private:
  36. CUtlVector< IShader * > m_ShaderList;
  37. };
  38. //-----------------------------------------------------------------------------
  39. // Global interfaces/structures
  40. //-----------------------------------------------------------------------------
  41. IMaterialSystemHardwareConfig* g_pHardwareConfig;
  42. const MaterialSystem_Config_t *g_pConfig;
  43. //-----------------------------------------------------------------------------
  44. // Interfaces/structures local to shaderlib
  45. //-----------------------------------------------------------------------------
  46. IShaderSystem* g_pSLShaderSystem;
  47. // Pattern necessary because shaders register themselves in global constructors
  48. static CShaderDLL *s_pShaderDLL;
  49. //-----------------------------------------------------------------------------
  50. // Global accessor
  51. //-----------------------------------------------------------------------------
  52. IShaderDLL *GetShaderDLL()
  53. {
  54. // Pattern necessary because shaders register themselves in global constructors
  55. if ( !s_pShaderDLL )
  56. {
  57. s_pShaderDLL = new CShaderDLL;
  58. }
  59. return s_pShaderDLL;
  60. }
  61. IShaderDLLInternal *GetShaderDLLInternal()
  62. {
  63. // Pattern necessary because shaders register themselves in global constructors
  64. if ( !s_pShaderDLL )
  65. {
  66. s_pShaderDLL = new CShaderDLL;
  67. }
  68. return static_cast<IShaderDLLInternal*>( s_pShaderDLL );
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Singleton interface
  72. //-----------------------------------------------------------------------------
  73. EXPOSE_INTERFACE_FN( (InstantiateInterfaceFn)GetShaderDLLInternal, IShaderDLLInternal, SHADER_DLL_INTERFACE_VERSION );
  74. //-----------------------------------------------------------------------------
  75. // Connect, disconnect...
  76. //-----------------------------------------------------------------------------
  77. CShaderDLL::CShaderDLL()
  78. {
  79. MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f );
  80. }
  81. //-----------------------------------------------------------------------------
  82. // Connect, disconnect...
  83. //-----------------------------------------------------------------------------
  84. bool CShaderDLL::Connect( CreateInterfaceFn factory, bool bIsMaterialSystem )
  85. {
  86. g_pHardwareConfig = (IMaterialSystemHardwareConfig*)factory( MATERIALSYSTEM_HARDWARECONFIG_INTERFACE_VERSION, NULL );
  87. g_pConfig = (const MaterialSystem_Config_t*)factory( MATERIALSYSTEM_CONFIG_VERSION, NULL );
  88. g_pSLShaderSystem = (IShaderSystem*)factory( SHADERSYSTEM_INTERFACE_VERSION, NULL );
  89. if ( !bIsMaterialSystem )
  90. {
  91. ConnectTier1Libraries( &factory, 1 );
  92. InitShaderLibCVars( factory );
  93. }
  94. return ( g_pConfig != NULL ) && (g_pHardwareConfig != NULL) && ( g_pSLShaderSystem != NULL );
  95. }
  96. void CShaderDLL::Disconnect( bool bIsMaterialSystem )
  97. {
  98. if ( !bIsMaterialSystem )
  99. {
  100. ConVar_Unregister();
  101. DisconnectTier1Libraries();
  102. }
  103. g_pHardwareConfig = NULL;
  104. g_pConfig = NULL;
  105. g_pSLShaderSystem = NULL;
  106. }
  107. bool CShaderDLL::Connect( CreateInterfaceFn factory )
  108. {
  109. return Connect( factory, false );
  110. }
  111. void CShaderDLL::Disconnect()
  112. {
  113. Disconnect( false );
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Iterates over all shaders
  117. //-----------------------------------------------------------------------------
  118. int CShaderDLL::ShaderCount() const
  119. {
  120. return m_ShaderList.Count();
  121. }
  122. IShader *CShaderDLL::GetShader( int nShader )
  123. {
  124. if ( ( nShader < 0 ) || ( nShader >= m_ShaderList.Count() ) )
  125. return NULL;
  126. return m_ShaderList[nShader];
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Adds to the shader lists
  130. //-----------------------------------------------------------------------------
  131. void CShaderDLL::InsertShader( IShader *pShader )
  132. {
  133. Assert( pShader );
  134. m_ShaderList.AddToTail( pShader );
  135. }