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.

255 lines
7.5 KiB

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