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.

259 lines
7.8 KiB

  1. //===== Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #include "materialsystem/MaterialSystemUtil.h"
  9. #include "materialsystem/imaterial.h"
  10. #include "materialsystem/itexture.h"
  11. #include "materialsystem/imaterialsystem.h"
  12. #include "tier1/keyvalues.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. //-----------------------------------------------------------------------------
  16. // Little utility class to deal with material references
  17. //-----------------------------------------------------------------------------
  18. //-----------------------------------------------------------------------------
  19. // constructor, destructor
  20. //-----------------------------------------------------------------------------
  21. CMaterialReference::CMaterialReference( char const* pMaterialName, const char *pTextureGroupName, bool bComplain ) : m_pMaterial( 0 )
  22. {
  23. if ( pMaterialName )
  24. {
  25. Assert( pTextureGroupName );
  26. Init( pMaterialName, pTextureGroupName, bComplain );
  27. }
  28. }
  29. const CMaterialReference& CMaterialReference::operator=( const CMaterialReference &ref )
  30. {
  31. Init( ref.m_pMaterial );
  32. return *this;
  33. }
  34. CMaterialReference::~CMaterialReference()
  35. {
  36. Shutdown();
  37. }
  38. //-----------------------------------------------------------------------------
  39. // Attach to a material
  40. //-----------------------------------------------------------------------------
  41. void CMaterialReference::Init( char const* pMaterialName, const char *pTextureGroupName, bool bComplain )
  42. {
  43. IMaterial *pMaterial = materials->FindMaterial( pMaterialName, pTextureGroupName, bComplain);
  44. if( IsErrorMaterial( pMaterial ) )
  45. {
  46. if (IsOSX())
  47. {
  48. printf("\n ##### CMaterialReference::Init got error material for %s in tex group %s", pMaterialName, pTextureGroupName );
  49. }
  50. }
  51. Assert( pMaterial );
  52. Init( pMaterial );
  53. }
  54. void CMaterialReference::Init( const char *pMaterialName, KeyValues *pVMTKeyValues )
  55. {
  56. // CreateMaterial has a refcount of 1
  57. Shutdown();
  58. m_pMaterial = materials->CreateMaterial( pMaterialName, pVMTKeyValues );
  59. }
  60. void CMaterialReference::Init( const char *pMaterialName, const char *pTextureGroupName, KeyValues *pVMTKeyValues )
  61. {
  62. IMaterial *pMaterial = materials->FindProceduralMaterial( pMaterialName, pTextureGroupName, pVMTKeyValues );
  63. Assert( pMaterial );
  64. Init( pMaterial );
  65. }
  66. void CMaterialReference::Init( IMaterial* pMaterial )
  67. {
  68. if ( m_pMaterial != pMaterial )
  69. {
  70. Shutdown();
  71. m_pMaterial = pMaterial;
  72. if ( m_pMaterial )
  73. {
  74. m_pMaterial->IncrementReferenceCount();
  75. }
  76. }
  77. }
  78. void CMaterialReference::Init( CMaterialReference& ref )
  79. {
  80. if ( m_pMaterial != ref.m_pMaterial )
  81. {
  82. Shutdown();
  83. m_pMaterial = ref.m_pMaterial;
  84. if (m_pMaterial)
  85. {
  86. m_pMaterial->IncrementReferenceCount();
  87. }
  88. }
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Detach from a material
  92. //-----------------------------------------------------------------------------
  93. void CMaterialReference::Shutdown( bool bDeleteIfUnreferenced /*=false*/ )
  94. {
  95. if ( m_pMaterial && materials )
  96. {
  97. m_pMaterial->DecrementReferenceCount();
  98. if ( bDeleteIfUnreferenced )
  99. {
  100. m_pMaterial->DeleteIfUnreferenced();
  101. }
  102. m_pMaterial = NULL;
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Little utility class to deal with texture references
  107. //-----------------------------------------------------------------------------
  108. //-----------------------------------------------------------------------------
  109. // constructor, destructor
  110. //-----------------------------------------------------------------------------
  111. CTextureReference::CTextureReference( ) : m_pTexture(NULL)
  112. {
  113. }
  114. CTextureReference::CTextureReference( const CTextureReference &ref ) : m_pTexture( NULL )
  115. {
  116. Init( ref.m_pTexture );
  117. }
  118. const CTextureReference& CTextureReference::operator=( CTextureReference &ref )
  119. {
  120. Init( ref.m_pTexture );
  121. return *this;
  122. }
  123. CTextureReference::~CTextureReference( )
  124. {
  125. Shutdown();
  126. }
  127. //-----------------------------------------------------------------------------
  128. // Attach to a texture
  129. //-----------------------------------------------------------------------------
  130. void CTextureReference::Init( char const* pTextureName, const char *pTextureGroupName, bool bComplain /* = false */, int nAdditionalCreationFlags /* = 0 */ )
  131. {
  132. Shutdown();
  133. m_pTexture = materials->FindTexture( pTextureName, pTextureGroupName, bComplain, nAdditionalCreationFlags );
  134. if ( m_pTexture )
  135. {
  136. m_pTexture->IncrementReferenceCount();
  137. }
  138. }
  139. void CTextureReference::Init( ITexture* pTexture )
  140. {
  141. if ( m_pTexture != pTexture )
  142. {
  143. Shutdown();
  144. m_pTexture = pTexture;
  145. if (m_pTexture)
  146. {
  147. m_pTexture->IncrementReferenceCount();
  148. }
  149. }
  150. }
  151. void CTextureReference::InitProceduralTexture( const char *pTextureName, const char *pTextureGroupName, int w, int h, ImageFormat fmt, int nFlags )
  152. {
  153. Shutdown();
  154. m_pTexture = materials->CreateProceduralTexture( pTextureName, pTextureGroupName, w, h, fmt, nFlags );
  155. // NOTE: The texture reference is already incremented internally above!
  156. /*
  157. if ( m_pTexture )
  158. {
  159. m_pTexture->IncrementReferenceCount();
  160. }
  161. */
  162. }
  163. void CTextureReference::InitRenderTarget( int w, int h, RenderTargetSizeMode_t sizeMode, ImageFormat fmt, MaterialRenderTargetDepth_t depth, bool bHDR, char *pStrOptionalName /* = NULL */ )
  164. {
  165. Shutdown();
  166. int textureFlags = TEXTUREFLAGS_CLAMPS | TEXTUREFLAGS_CLAMPT;
  167. if ( depth == MATERIAL_RT_DEPTH_ONLY )
  168. textureFlags |= TEXTUREFLAGS_POINTSAMPLE;
  169. int renderTargetFlags = bHDR ? CREATERENDERTARGETFLAGS_HDR : 0;
  170. // NOTE: Refcount returned by CreateRenderTargetTexture is 1
  171. m_pTexture = materials->CreateNamedRenderTargetTextureEx( pStrOptionalName, w, h, sizeMode, fmt,
  172. depth, textureFlags, renderTargetFlags );
  173. Assert( m_pTexture );
  174. }
  175. //-----------------------------------------------------------------------------
  176. // Detach from a texture
  177. //-----------------------------------------------------------------------------
  178. void CTextureReference::Shutdown( bool bDeleteIfUnReferenced )
  179. {
  180. if ( m_pTexture && materials )
  181. {
  182. m_pTexture->DecrementReferenceCount();
  183. if ( bDeleteIfUnReferenced )
  184. {
  185. m_pTexture->DeleteIfUnreferenced();
  186. }
  187. m_pTexture = NULL;
  188. }
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Builds ONLY the system ram render target. Used when caller is explicitly managing.
  192. // The paired EDRAM surface can be built in an alternate format.
  193. //-----------------------------------------------------------------------------
  194. #if defined( _X360 )
  195. void CTextureReference::InitRenderTargetTexture( int w, int h, RenderTargetSizeMode_t sizeMode, ImageFormat fmt, MaterialRenderTargetDepth_t depth, bool bHDR, char *pStrOptionalName, int nRenderTargetFlags )
  196. {
  197. // other variants not implemented yet
  198. Assert( depth == MATERIAL_RT_DEPTH_NONE || depth == MATERIAL_RT_DEPTH_SHARED );
  199. Assert( !bHDR );
  200. m_pTexture = materials->CreateNamedRenderTargetTextureEx(
  201. pStrOptionalName,
  202. w,
  203. h,
  204. sizeMode,
  205. fmt,
  206. depth,
  207. TEXTUREFLAGS_CLAMPS | TEXTUREFLAGS_CLAMPT,
  208. CREATERENDERTARGETFLAGS_NOEDRAM | nRenderTargetFlags );
  209. Assert( m_pTexture );
  210. }
  211. #endif
  212. //-----------------------------------------------------------------------------
  213. // Builds ONLY the EDRAM render target surface. Used when caller is explicitly managing.
  214. // The paired system memory texture can be built in an alternate format.
  215. //-----------------------------------------------------------------------------
  216. #if defined( _X360 )
  217. void CTextureReference::InitRenderTargetSurface( int width, int height, ImageFormat fmt, bool bSameAsTexture, RTMultiSampleCount360_t multiSampleCount )
  218. {
  219. // texture has to be created first
  220. Assert( m_pTexture && m_pTexture->IsRenderTarget() );
  221. m_pTexture->CreateRenderTargetSurface( width, height, fmt, bSameAsTexture, multiSampleCount );
  222. }
  223. #endif