BVB Source Codes

CRYENGINE Show TerrainTextureCache.cpp Source code

Return Download CRYENGINE: download TerrainTextureCache.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  File name:   TerrainTextureCache.cpp
  5. //  Version:     v1.00
  6. //  Created:     28/1/2007 by Vladimir Kajalin
  7. //  Compilers:   Visual Studio.NET
  8. //  Description: terrain texture management
  9. // -------------------------------------------------------------------------
  10. //  History:
  11. //
  12. ////////////////////////////////////////////////////////////////////////////
  13.  
  14. #include "StdAfx.h"
  15. #include "terrain_sector.h"
  16. #include "terrain.h"
  17.  
  18. CTextureCache::CTextureCache()
  19. {
  20.         m_eTexFormat = eTF_Unknown;
  21.         m_nDim = 0;
  22.         m_nPoolTexId = 0;
  23.         m_nPoolDim = 0;
  24. }
  25.  
  26. CTextureCache::~CTextureCache()
  27. {
  28.         if (GetPoolSize())
  29.                 assert(m_FreeTextures.Count() + m_Quarantine.Count() + m_UsedTextures.Count() == GetCVars()->e_TerrainTextureStreamingPoolItemsNum);
  30.  
  31.         ResetTexturePool();
  32. }
  33.  
  34. void CTextureCache::ResetTexturePool()
  35. {
  36.         m_FreeTextures.AddList(m_UsedTextures);
  37.         m_UsedTextures.Clear();
  38.         m_FreeTextures.AddList(m_Quarantine);
  39.         m_Quarantine.Clear();
  40.         if (GetRenderer())
  41.         {
  42.                 GetRenderer()->RemoveTexture(m_nPoolTexId);
  43.         }
  44.         m_FreeTextures.Clear();
  45.         m_eTexFormat = eTF_Unknown;
  46.         m_nDim = 0;
  47.         m_nPoolTexId = 0;
  48.         m_nPoolDim = 0;
  49. }
  50.  
  51. uint16 CTextureCache::GetTexture(byte* pData, uint16& nSlotId)
  52. {
  53.         if (!m_FreeTextures.Count())
  54.                 Update();
  55.  
  56.         if (!m_FreeTextures.Count())
  57.         {
  58.                 Error("CTextureCache::GetTexture: !m_FreeTextures.Count()");
  59.                 return 0;
  60.         }
  61.  
  62.         nSlotId = m_FreeTextures.Last();
  63.  
  64.         m_FreeTextures.DeleteLast();
  65.  
  66.         RectI region;
  67.         GetSlotRegion(region, nSlotId);
  68.  
  69.         GetRenderer()->DownLoadToVideoMemory(pData, m_nDim, m_nDim, m_eTexFormat, m_eTexFormat, 0, false, FILTER_NONE, m_nPoolTexId,
  70.                                              NULL, FT_USAGE_ALLOWREADSRGB, GetTerrain()->GetEndianOfTexture(), &region);
  71.  
  72.         m_UsedTextures.Add(nSlotId);
  73.  
  74.         assert(m_FreeTextures.Count() + m_Quarantine.Count() + m_UsedTextures.Count() == GetCVars()->e_TerrainTextureStreamingPoolItemsNum);
  75.  
  76.         return m_nPoolTexId;
  77. }
  78.  
  79. void CTextureCache::UpdateTexture(byte* pData, const uint16& nSlotId)
  80. {
  81.         FUNCTION_PROFILER_3DENGINE;
  82.  
  83.         assert(nSlotId >= 0 && nSlotId < GetPoolSize());
  84.  
  85.         RectI region;
  86.         GetSlotRegion(region, nSlotId);
  87.  
  88.         GetRenderer()->DownLoadToVideoMemory(pData, m_nDim, m_nDim, m_eTexFormat, m_eTexFormat, 0, false, FILTER_NONE, m_nPoolTexId,
  89.                                              NULL, FT_USAGE_ALLOWREADSRGB, GetTerrain()->GetEndianOfTexture(), &region);
  90. }
  91.  
  92. void CTextureCache::ReleaseTexture(uint16& nTexId, uint16& nTexSlotId)
  93. {
  94.         assert(nTexId == m_nPoolTexId);
  95.  
  96.         if (m_UsedTextures.Delete(nTexSlotId))
  97.         {
  98.                 m_Quarantine.Add(nTexSlotId);
  99.  
  100.                 nTexId = 0;
  101.                 nTexSlotId = ~0;
  102.         }
  103.         else
  104.                 assert(!"Attempt to release non pooled texture");
  105.  
  106.         assert(m_FreeTextures.Count() + m_Quarantine.Count() + m_UsedTextures.Count() == GetCVars()->e_TerrainTextureStreamingPoolItemsNum);
  107. }
  108.  
  109. bool CTextureCache::Update()
  110. {
  111.         m_FreeTextures.AddList(m_Quarantine);
  112.         m_Quarantine.Clear();
  113.  
  114.         return GetPoolSize() == GetCVars()->e_TerrainTextureStreamingPoolItemsNum;
  115. }
  116.  
  117. int CTextureCache::GetPoolSize()
  118. {
  119.         return (m_FreeTextures.Count() + m_Quarantine.Count() + m_UsedTextures.Count());
  120. }
  121.  
  122. void CTextureCache::InitPool(byte* pData, int nDim, ETEX_Format eTexFormat)
  123. {
  124.         MEMSTAT_CONTEXT(EMemStatContextTypes::MSC_Texture, 0, "Terrain texture cache");
  125.  
  126.         ResetTexturePool();
  127.  
  128.         m_eTexFormat = eTexFormat;
  129.         m_nDim = nDim;
  130.  
  131.         m_nPoolDim = (int)sqrt(GetCVars()->e_TerrainTextureStreamingPoolItemsNum);
  132.         int nPoolTexDim = m_nPoolDim * m_nDim;
  133.  
  134.         stack_string sPoolTextureName;
  135.         sPoolTextureName.Format("$TERRAIN_TEX_POOL_%p", this);
  136.  
  137.         m_nPoolTexId = GetRenderer()->DownLoadToVideoMemory(NULL, nPoolTexDim, nPoolTexDim, eTexFormat, eTexFormat, 0, false, FILTER_NONE, 0,
  138.                                                                   sPoolTextureName, (eTexFormat == eTF_R32F) ? 0 : FT_USAGE_ALLOWREADSRGB, GetPlatformEndian(), NULL, false);
  139.  
  140.         if (m_nPoolTexId <= 0)
  141.         {
  142.                 if (!gEnv->IsDedicated())
  143.                         Error("Debug: CTextureCache::InitPool: GetRenderer()->DownLoadToVideoMemory returned %d", m_nPoolTexId);
  144.  
  145.                 if (!gEnv->IsDedicated())
  146.                         Error("Debug: DownLoadToVideoMemory() params: dim=%d, eTexFormat=%d", nDim, eTexFormat);
  147.         }
  148.  
  149.         for (int i = GetCVars()->e_TerrainTextureStreamingPoolItemsNum - 1; i >= 0; i--)
  150.         {
  151.                 m_FreeTextures.Add(i);
  152.         }
  153.  
  154.         assert(m_FreeTextures.Count() + m_Quarantine.Count() + m_UsedTextures.Count() == GetCVars()->e_TerrainTextureStreamingPoolItemsNum);
  155. }
  156.  
  157. void CTextureCache::GetSlotRegion(RectI& region, int nSlotId)
  158. {
  159.         region.w = region.h = m_nDim;
  160.         region.x = (nSlotId / m_nPoolDim) * m_nDim;
  161.         region.y = (nSlotId % m_nPoolDim) * m_nDim;
  162. }
  163.  
downloadTerrainTextureCache.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top