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.

199 lines
6.9 KiB

  1. //========== Copyright � 2005, Valve Corporation, All rights reserved. ========
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef CMATERIALLIGHTMAPS_H
  7. #define CMATERIALLIGHTMAPS_H
  8. #include "tier1/utlvector.h"
  9. #include "imagepacker.h"
  10. #ifndef MATSYS_INTERNAL
  11. #error "This file is private to the implementation of IMaterialSystem/IMaterialSystemInternal"
  12. #endif
  13. #if defined( _WIN32 )
  14. #pragma once
  15. #endif
  16. //-----------------------------------------------------------------------------
  17. class CMaterialSystem;
  18. class IMatRenderContextInternal;
  19. class CMaterialDict;
  20. class IMaterial;
  21. class IMaterialInternal;
  22. class FloatBitMap_t;
  23. typedef intp ShaderAPITextureHandle_t;
  24. struct MaterialSystem_SortInfo_t;
  25. typedef unsigned short MaterialHandle_t;
  26. //-----------------------------------------------------------------------------
  27. // Manager of material system lightmaps
  28. //-----------------------------------------------------------------------------
  29. const int COUNT_DYNAMIC_LIGHTMAP_PAGES = 1;
  30. class CMatLightmaps
  31. {
  32. public:
  33. CMatLightmaps();
  34. void Shutdown( );
  35. //------------------------------------------------------------
  36. // Methods exposed in IMaterialSystem
  37. //------------------------------------------------------------
  38. void BeginLightmapAllocation( void );
  39. void EndLightmapAllocation( void );
  40. int AllocateLightmap( int width, int height,
  41. int offsetIntoLightmapPage[2],
  42. IMaterial *pMaterial );
  43. int AllocateWhiteLightmap( IMaterial *pMaterial );
  44. // NOTE: This returns a lightmap page ID, not a sortID like AllocateLightmap!!!!
  45. int AllocateDynamicLightmap( int lightmapSize[2], int *pOutOffsetIntoPage, int frameID );
  46. int GetNumSortIDs( void );
  47. void GetSortInfo( MaterialSystem_SortInfo_t *sortInfoArray );
  48. void UpdateLightmap( int lightmapPageID, int lightmapSize[2],
  49. int offsetIntoLightmapPage[2],
  50. float *pFloatImage, float *pFloatImageBump1,
  51. float *pFloatImageBump2, float *pFloatImageBump3 );
  52. void GetLightmapPageSize( int lightmapPageID, int *width, int *height ) const;
  53. void ResetMaterialLightmapPageInfo( void );
  54. //------------------------------------------------------------
  55. // Methods exposed in IMaterialSystemInternal
  56. //------------------------------------------------------------
  57. int GetLightmapWidth( int lightmap ) const;
  58. int GetLightmapHeight( int lightmap ) const;
  59. //------------------------------------------------------------
  60. // Methods used by other material system components
  61. //------------------------------------------------------------
  62. // Releases/restores lightmap pages
  63. void ReleaseLightmapPages();
  64. void RestoreLightmapPages();
  65. void EnableLightmapFiltering( bool enabled );
  66. int GetNumLightmapPages() const { return m_NumLightmapPages;}
  67. ShaderAPITextureHandle_t GetLightmapPageTextureHandle( int lightmap ) { return m_LightmapPageTextureHandles[lightmap]; }
  68. bool IsDynamicLightmap( int lightmap ) const { return (lightmap >= m_firstDynamicLightmap ) ? true : false; }
  69. CMaterialSystem *GetMaterialSystem() const;
  70. void BeginUpdateLightmaps();
  71. void EndUpdateLightmaps();
  72. void CleanupLightmaps();
  73. private:
  74. // Gets the maximum lightmap page size...
  75. int GetMaxLightmapPageWidth() const;
  76. int GetMaxLightmapPageHeight() const;
  77. // Allocate lightmap textures in D3D
  78. void AllocateLightmapTexture( int lightmap );
  79. // Initializes lightmap bits
  80. void InitLightmapBits( int lightmap );
  81. // assumes m_LightmapPixelWriter is already set up - results written there
  82. void BumpedLightmapBitsToPixelWriter_LDR( float* pFloatImage, float *pFloatImageBump1, float *pFloatImageBump2,
  83. float *pFloatImageBump3, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t *pfmOut );
  84. void BumpedLightmapBitsToPixelWriter_HDRF( float* pFloatImage, float *pFloatImageBump1, float *pFloatImageBump2,
  85. float *pFloatImageBump3, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t *pfmOut );
  86. void BumpedLightmapBitsToPixelWriter_HDRI( float* pFloatImage, float *pFloatImageBump1, float *pFloatImageBump2,
  87. float *pFloatImageBump3, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t *pfmOut ) RESTRICT;
  88. void LightmapBitsToPixelWriter_LDR( float* pFloatImage, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t *pfmOut );
  89. void LightmapBitsToPixelWriter_HDRF( float* pFloatImage, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t *pfmOut );
  90. void LightmapBitsToPixelWriter_HDRI( float* pFloatImage, int pLightmapSize[2], int pOffsetIntoLightmapPage[2], FloatBitMap_t *pfmOut );
  91. // For computing sort info
  92. void ComputeSortInfo( MaterialSystem_SortInfo_t* pInfo, int& sortId, bool alpha );
  93. void ComputeWhiteLightmappedSortInfo( MaterialSystem_SortInfo_t* pInfo, int& sortId, bool alpha );
  94. void EnumerateMaterials( void );
  95. // Lock a lightmap for update.
  96. bool LockLightmap( int lightmap );
  97. //------------------------------------------------------------
  98. // Accessor helpers
  99. //------------------------------------------------------------
  100. IMaterialInternal* GetCurrentMaterialInternal() const;
  101. void SetCurrentMaterialInternal(IMaterialInternal* pCurrentMaterial);
  102. IMaterialInternal* GetMaterialInternal( MaterialHandle_t idx ) const;
  103. const IMatRenderContextInternal *GetRenderContextInternal() const;
  104. IMatRenderContextInternal *GetRenderContextInternal();
  105. const CMaterialDict *GetMaterialDict() const;
  106. CMaterialDict *GetMaterialDict();
  107. //------------------------------------------------------------
  108. struct LightmapPageInfo_t
  109. {
  110. unsigned short m_Width;
  111. unsigned short m_Height;
  112. int m_Flags;
  113. };
  114. struct dynamiclightmap_t
  115. {
  116. void Init()
  117. {
  118. lightmapLocked = -1;
  119. frameID = 0;
  120. currentDynamicIndex = 0;
  121. for ( int i = 0; i < COUNT_DYNAMIC_LIGHTMAP_PAGES; i++ )
  122. {
  123. lightmapLockFrame[i] = 0;
  124. }
  125. }
  126. int lightmapLocked;
  127. int frameID;
  128. int currentDynamicIndex;
  129. int lightmapLockFrame[COUNT_DYNAMIC_LIGHTMAP_PAGES];
  130. CImagePacker imagePackers[COUNT_DYNAMIC_LIGHTMAP_PAGES];
  131. };
  132. CUtlVector<CImagePacker> m_ImagePackers;
  133. int m_numSortIDs;
  134. IMaterialInternal *m_currentWhiteLightmapMaterial;
  135. LightmapPageInfo_t *m_pLightmapPages;
  136. CUtlVector<ShaderAPITextureHandle_t> m_LightmapPageTextureHandles;
  137. int m_NumLightmapPages;
  138. int m_nUpdatingLightmapsStackDepth;
  139. int m_firstDynamicLightmap;
  140. CPixelWriter m_LightmapPixelWriter;
  141. int m_nLockedLightmap; // -1 for nothing locked.
  142. dynamiclightmap_t m_dynamic;
  143. FloatBitMap_t **m_pLightmapDataPtrArray;
  144. enum LightmapsState
  145. {
  146. STATE_DEFAULT, // Lightmaps in default state - all operations allowed
  147. STATE_RELEASED // Lightmaps in released state - usually due to lost/released D3D device, D3D operations disallowed, Restore() call to follow
  148. };
  149. LightmapsState m_eLightmapsState;
  150. };
  151. //-----------------------------------------------------------------------------
  152. #endif // CMATERIALLIGHTMAPS_H