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.

235 lines
7.4 KiB

  1. //========= Copyright � 1996-2013, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Provide custom texture generation (compositing)
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef COMPOSITE_TEXTURE_H
  8. #define COMPOSITE_TEXTURE_H
  9. #include "materialsystem/MaterialSystemUtil.h"
  10. #include "materialsystem/itexture.h"
  11. #include "materialsystem/icompositetexturegenerator.h"
  12. #include "utllinkedlist.h"
  13. #include "utlbuffer.h"
  14. class IVisualsDataProcessor;
  15. class ITextureInternal;
  16. //
  17. // Render Targets for Custom Materials
  18. //
  19. class IMaterialSystem;
  20. class IMaterialSystemHardwareConfig;
  21. struct SCompositeTextureRTData_t
  22. {
  23. const char* pName;
  24. int nSize;
  25. bool bSRGB;
  26. bool bAvailable; // true if the RT was created and can be used.
  27. IVTFTexture *pScratch;
  28. };
  29. // this should match the s_compositeTextureRTData array in composite_texture.cpp
  30. enum CompositeTextureRTSizes_t
  31. {
  32. #if !defined( PLATFORM_OSX )
  33. COMPOSITE_TEXTURE_RT_2048,
  34. #endif
  35. COMPOSITE_TEXTURE_RT_1024,
  36. COMPOSITE_TEXTURE_RT_512,
  37. COMPOSITE_TEXTURE_RT_256,
  38. COMPOSITE_TEXTURE_RT_128,
  39. COMPOSITE_TEXTURE_RT_COUNT
  40. };
  41. //
  42. // Texture Regenerator for Custom Materials
  43. //
  44. // This is where the compositing material is created and used to make the procedural textures
  45. // used with custom materials. It also holds the result vtfs that are copied into the
  46. // actual textures used for rendering.
  47. //
  48. enum CompositeTextureStages_t
  49. {
  50. COMPOSITE_TEXTURE_STATE_NOT_STARTED = 0,
  51. COMPOSITE_TEXTURE_STATE_ASYNC_TEXTURE_LOAD,
  52. COMPOSITE_TEXTURE_STATE_WAITING_FOR_ASYNC_TEXTURE_LOAD_FINISH,
  53. COMPOSITE_TEXTURE_STATE_NEEDS_INIT,
  54. COMPOSITE_TEXTURE_STATE_WAITING_FOR_RENDER_TO_RT,
  55. COMPOSITE_TEXTURE_STATE_RENDERED_TO_RT,
  56. COMPOSITE_TEXTURE_STATE_WAITING_FOR_READ_RT,
  57. COMPOSITE_TEXTURE_STATE_REQUESTED_READ,
  58. COMPOSITE_TEXTURE_STATE_WAITING_FOR_GETRESULT,
  59. COMPOSITE_TEXTURE_STATE_REQUESTED_GETRESULT,
  60. COMPOSITE_TEXTURE_STATE_COPY_TO_VTF_COMPLETE,
  61. COMPOSITE_TEXTURE_STATE_WAITING_FOR_MATERIAL_CLEANUP,
  62. COMPOSITE_TEXTURE_STATE_COMPLETE
  63. };
  64. #define NUM_PRELOAD_TEXTURES 20
  65. class CCompositeTexture;
  66. class CCompositeTextureResult : public ITextureRegenerator
  67. {
  68. public:
  69. CCompositeTextureResult( CCompositeTexture *pOwner ) : m_pOwner( pOwner ), m_pTexture( NULL ) {}
  70. virtual void RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pRect );
  71. virtual void Release();
  72. CCompositeTexture *m_pOwner;
  73. ITexture *m_pTexture;
  74. };
  75. class CCompositeTexture : public ICompositeTexture, public CRefCounted<>
  76. {
  77. public:
  78. CCompositeTexture( const CUtlBuffer &compareBlob, KeyValues *pCompositingMaterialKeyValues, CompositeTextureSize_t size, CompositeTextureFormat_t format, int nMaterialParamNameId, bool bSRGB, bool bIgnorePicMip );
  79. bool Init();
  80. void Refresh()
  81. {
  82. m_nRegenerateStage = COMPOSITE_TEXTURE_STATE_NOT_STARTED;
  83. m_bNeedsRegenerate = false;
  84. m_bNeedsFinalize = true;
  85. }
  86. bool IsReady() const { return ( !NeedsFinalize() && GenerationComplete() ); }
  87. bool GenerationComplete() const { return ( m_nRegenerateStage == COMPOSITE_TEXTURE_STATE_COMPLETE ); }
  88. void Usage( int &nTextures, int &nBackingTextures );
  89. bool NeedsAsyncTextureLoad() const { return m_nRegenerateStage == COMPOSITE_TEXTURE_STATE_ASYNC_TEXTURE_LOAD; }
  90. void DoAsyncTextureLoad();
  91. bool NeedsCompositingMaterial() const { return m_nRegenerateStage == COMPOSITE_TEXTURE_STATE_NEEDS_INIT; }
  92. void CreateCompositingMaterial();
  93. bool NeedsRender() const { return m_nRegenerateStage == COMPOSITE_TEXTURE_STATE_WAITING_FOR_RENDER_TO_RT; }
  94. void RenderToRT();
  95. bool HasRendered() const { return ( m_nRegenerateStage == COMPOSITE_TEXTURE_STATE_RENDERED_TO_RT ); }
  96. void AdvanceToReadRT();
  97. bool NeedsReadRT() const { return m_nRegenerateStage == COMPOSITE_TEXTURE_STATE_WAITING_FOR_READ_RT; }
  98. void ReadRT();
  99. bool NeedsGetResult() const { return m_nRegenerateStage == COMPOSITE_TEXTURE_STATE_WAITING_FOR_GETRESULT; }
  100. void GetReadRTResult();
  101. bool NeedsMaterialCleanup() const { return m_nRegenerateStage == COMPOSITE_TEXTURE_STATE_WAITING_FOR_MATERIAL_CLEANUP; }
  102. void CleanupCompositingMaterial();
  103. void GenerationStep();
  104. bool NeedsRegenerate() const { return m_bNeedsRegenerate; }
  105. void ForceRegenerate();
  106. CompositeTextureFormat_t Format() const { return m_format; }
  107. CompositeTextureSize_t Size() const { return m_size; }
  108. int ActualSize() const { return m_nActualSize; }
  109. int GetMaterialParamNameId() const { return m_nMaterialParamNameId; }
  110. const char *GetName() { return m_szTextureName; }
  111. bool NeedsFinalize() const { return m_bNeedsFinalize; }
  112. void Finalize();
  113. IVTFTexture *GetResultVTF() { return m_pResultVTF; }
  114. bool ShouldRelease() { return ( ( GetRefCount() == 1 ) && IsReady() ); }
  115. const CUtlBuffer &GetVisualsDataCompareBlob() { return m_compareBlob; }
  116. void ReleaseResult() { m_ResultTexture.Release(); }
  117. bool IsSRGB() const { return m_bSRGB; }
  118. bool Compare( const SCompositeTextureInfo &textureInfo );
  119. protected:
  120. virtual ~CCompositeTexture();
  121. void GenerateComposite();
  122. void ReleasePreloadedTextures();
  123. char m_szTextureName[ MAX_PATH ];
  124. CompositeTextureSize_t m_size;
  125. CompositeTextureFormat_t m_format; // DXT1 or DXT5 only
  126. int m_nMaterialParamNameId; // the material parameter that this texture will fill/replace in the eventual Custom Material, also used to interact with the VisualsDataProcessor
  127. bool m_bSRGB;
  128. CompositeTextureStages_t m_nRegenerateStage;
  129. CUtlBuffer m_compareBlob;
  130. IVTFTexture *m_pResultVTF;
  131. CCompositeTextureResult m_ResultTexture;
  132. int m_nActualSize;
  133. bool m_bNeedsRegenerate;
  134. bool m_bNeedsFinalize;
  135. IVTFTexture *m_pScratchVTF;
  136. ITexture *m_pCustomMaterialRT;
  137. KeyValues *m_pCompositingMaterialKeyValues;
  138. IMaterial *m_pCompositingMaterial;
  139. int m_nLastFrameCount;
  140. CThreadEvent *m_pPixelsReadEvent;
  141. bool m_bIgnorePicMip;
  142. static int m_nTextureCount;
  143. private:
  144. CCompositeTexture( CCompositeTexture &);
  145. ITextureInternal* m_pPreLoadTextures[ NUM_PRELOAD_TEXTURES ];
  146. };
  147. //
  148. // Composite Texture Generator
  149. //
  150. // This is the class that the game talks to in order to make composite textures
  151. // You need a IVisualsDataProcessor based class to feed this.
  152. //
  153. class CCompositeTextureGenerator : public ICompositeTextureGenerator
  154. {
  155. public:
  156. CCompositeTextureGenerator( void );
  157. virtual ~CCompositeTextureGenerator( void );
  158. bool Init( void );
  159. void Shutdown( void );
  160. virtual bool Process( void );
  161. virtual ICompositeTexture *GetCompositeTexture( IVisualsDataProcessor *pVisualsDataProcessor, int nMaterialParamNameId, CompositeTextureSize_t size, CompositeTextureFormat_t format, bool bSRGB, bool bIgnorePicMip = false , bool bAllowCreate = true );
  162. virtual ICompositeTexture *GetCompositeTexture( const SCompositeTextureInfo &textureInfo, bool bIgnorePicMip = false , bool bAllowCreate = true );
  163. virtual bool ForceRegenerate( ICompositeTexture *pTexture );
  164. private:
  165. CUtlVector< CCompositeTexture * > m_pCompositeTextures;
  166. CUtlVector< CCompositeTexture * > m_pPendingCompositeTextures;
  167. void DestroyTextures( void );
  168. void GenerateThread(); // never call this directly, it's running as another thread
  169. void CreateGenerateThread();
  170. void DestroyGenerateThread();
  171. CUtlLinkedList< CCompositeTexture * > m_pGenerateQueue;
  172. CThreadFastMutex m_GenerateQueueMutex;
  173. ThreadHandle_t m_hGenerateThread;
  174. bool m_bGenerateThreadExit;
  175. CTextureReference m_CompositeTextureManagerRTs[COMPOSITE_TEXTURE_RT_COUNT];
  176. ITextureInternal* m_pGunGrimeTexture;
  177. ITextureInternal* m_pPaintWearTexture;
  178. };
  179. #endif // COMPOSITE_TEXTURE_H