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.

170 lines
4.5 KiB

  1. //===== Copyright � 1996-2008, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: Manager for texture containing subd data
  4. //
  5. //===========================================================================//
  6. #include "isubdinternal.h"
  7. #include "tier0/dbg.h"
  8. #include "materialsystem/imaterialsystem.h"
  9. #include "materialsystem/MaterialSystemUtil.h"
  10. #include "materialsystem/itexture.h"
  11. #include "materialsystem/imesh.h"
  12. #include "utlsortvector.h"
  13. #include "materialsystem_global.h"
  14. #include "IHardwareConfigInternal.h"
  15. #include "pixelwriter.h"
  16. #include "itextureinternal.h"
  17. #include "tier1/keyvalues.h"
  18. #include "texturemanager.h"
  19. #include "imaterialsysteminternal.h"
  20. #include "imatrendercontextinternal.h"
  21. #include "studio.h"
  22. #include "tier0/vprof.h"
  23. #include "renderparm.h"
  24. #include "tier2/renderutils.h"
  25. #include "bitmap/imageformat.h"
  26. #include "materialsystem/IShader.h"
  27. #include "imaterialinternal.h"
  28. #include "tier0/memdbgon.h"
  29. #define SUBD_TEXTURE_FORMAT IMAGE_FORMAT_RGBA32323232F
  30. #define SUBD_TEXTURE_WIDTH 30
  31. #define SUBD_TEXTURE_HEIGHT 8192 // If we need more than 8192 patches, we may be in trouble
  32. //-----------------------------------------------------------------------------
  33. // Subdivision surface manager class
  34. //-----------------------------------------------------------------------------
  35. class CSubDMgr : public ISubDMgr
  36. {
  37. public:
  38. CSubDMgr();
  39. virtual bool ShouldAllocateTextures();
  40. virtual void AllocateTextures();
  41. virtual void FreeTextures();
  42. virtual void ReacquireResources();
  43. virtual void ReleaseResources();
  44. virtual int GetWidth();
  45. virtual int GetHeight();
  46. virtual int GetOffsetGeometry();
  47. virtual int GetOffsetTangents();
  48. virtual float* Lock( int nNumRows );
  49. virtual void Unlock();
  50. virtual ShaderAPITextureHandle_t SubDTexture();
  51. private:
  52. ShaderAPITextureHandle_t m_hSysMem;
  53. ShaderAPITextureHandle_t m_hDefaultPool;
  54. int m_nRowsLocked;
  55. int m_nOffsetGeometry;
  56. int m_nOffsetTangents;
  57. };
  58. //-----------------------------------------------------------------------------
  59. // Singleton
  60. //-----------------------------------------------------------------------------
  61. static CSubDMgr s_SubDMgr;
  62. ISubDMgr *g_pSubDMgr = &s_SubDMgr;
  63. //-----------------------------------------------------------------------------
  64. // Constructor
  65. //-----------------------------------------------------------------------------
  66. CSubDMgr::CSubDMgr()
  67. {
  68. m_nRowsLocked = 0;
  69. m_nOffsetGeometry = 0;
  70. m_nOffsetTangents = 0;
  71. m_hSysMem = INVALID_SHADERAPI_TEXTURE_HANDLE;
  72. m_hDefaultPool = INVALID_SHADERAPI_TEXTURE_HANDLE;
  73. }
  74. bool CSubDMgr::ShouldAllocateTextures()
  75. {
  76. return true;
  77. // return g_pMaterialSystemHardwareConfig->SupportsSubdivisionSurfaces();
  78. }
  79. int CSubDMgr::GetWidth()
  80. {
  81. return SUBD_TEXTURE_WIDTH;
  82. }
  83. int CSubDMgr::GetHeight()
  84. {
  85. return SUBD_TEXTURE_HEIGHT;
  86. }
  87. int CSubDMgr::GetOffsetGeometry()
  88. {
  89. return m_nOffsetGeometry;
  90. }
  91. int CSubDMgr::GetOffsetTangents()
  92. {
  93. return m_nOffsetTangents;
  94. }
  95. void CSubDMgr::AllocateTextures()
  96. {
  97. ReacquireResources();
  98. }
  99. void CSubDMgr::FreeTextures()
  100. {
  101. ReleaseResources();
  102. }
  103. void CSubDMgr::ReleaseResources()
  104. {
  105. if ( g_pShaderAPI->IsTexture( m_hSysMem ) )
  106. {
  107. g_pShaderAPI->DeleteTexture( m_hSysMem );
  108. m_hSysMem = INVALID_SHADERAPI_TEXTURE_HANDLE;
  109. }
  110. if ( g_pShaderAPI->IsTexture( m_hDefaultPool ) )
  111. {
  112. g_pShaderAPI->DeleteTexture( m_hDefaultPool );
  113. m_hDefaultPool = INVALID_SHADERAPI_TEXTURE_HANDLE;
  114. }
  115. }
  116. void CSubDMgr::ReacquireResources()
  117. {
  118. Assert( m_hSysMem == INVALID_SHADERAPI_TEXTURE_HANDLE );
  119. Assert( m_hDefaultPool == INVALID_SHADERAPI_TEXTURE_HANDLE );
  120. // These scratch textures are ~4mb (30*8192*16 bytes) and can
  121. // accommodate data for 8192 patches
  122. g_pShaderAPI->CreateTextures( &m_hSysMem, 1, SUBD_TEXTURE_WIDTH, SUBD_TEXTURE_HEIGHT, 0, SUBD_TEXTURE_FORMAT, 1, 1, TEXTURE_CREATE_SYSMEM, "_SubDSysMemTexture", "SubD Textures" );
  123. g_pShaderAPI->CreateTextures( &m_hDefaultPool, 1, SUBD_TEXTURE_WIDTH, SUBD_TEXTURE_HEIGHT, 0, SUBD_TEXTURE_FORMAT, 1, 1, TEXTURE_CREATE_DEFAULT_POOL | TEXTURE_CREATE_VERTEXTEXTURE, "_SubDVidMemTexture", "SubD Textures" );
  124. g_pShaderAPI->ModifyTexture( m_hDefaultPool );
  125. g_pShaderAPI->TexMinFilter( SHADER_TEXFILTERMODE_NEAREST );
  126. g_pShaderAPI->TexMagFilter( SHADER_TEXFILTERMODE_NEAREST );
  127. }
  128. float *CSubDMgr::Lock( int nRows )
  129. {
  130. m_nRowsLocked = nRows;
  131. return (float *) g_pShaderAPI->LockTex( m_hSysMem );
  132. }
  133. void CSubDMgr::Unlock()
  134. {
  135. g_pShaderAPI->UnlockTex( m_hSysMem );
  136. g_pShaderAPI->UpdateTexture( 0, 0, SUBD_TEXTURE_WIDTH, m_nRowsLocked, m_hDefaultPool, m_hSysMem );
  137. }
  138. ShaderAPITextureHandle_t CSubDMgr::SubDTexture()
  139. {
  140. return m_hDefaultPool;
  141. }