// Add cell into cache and returns reference to it. Cache item with lowest priority is choosen. // Call this method when you want allocate new item in the cache. static MyVoxelCacheCellRender AddCell(int voxelMapId, ref MyMwcVector3Int cellCoord, MyLodTypeEnum cellHashType) { Int64 key = MyVoxelMaps.GetCellHashCode(voxelMapId, ref cellCoord, cellHashType); // Cache item with lowest priority is choosen. LinkedListNode <MyVoxelCacheCellRender> first = m_priority.First; lock (m_priorityLocker) { m_priority.RemoveFirst(); m_priority.AddLast(first); } // If this object already contained some vertex buffers (and of course some render cell), we need to dispose its vertex buffers and // remove from hash table, so that render cell will no longer be in the render cell cache if (first.Value.Contains == true) { System.Diagnostics.Debug.Assert(false, "Cache is full - increase it atm"); Int64 keyForRemoving = MyVoxelMaps.GetCellHashCode(first.Value.VoxelMap.VoxelMapId, ref first.Value.CellCoord, first.Value.CellHashType); m_cellsByCoordinate.Remove(keyForRemoving); first.Value.Reset(); } // Remember where is render cell cache for this render cell m_cellsByCoordinate.Add(key, first); // You have reached the capacity of RENDER cells cache. Consider increasing it. MyCommonDebugUtils.AssertDebug(m_cellsByCoordinate.Count <= m_capacity); return(first.Value); }
public static void SaveAllVoxels() { foreach (var voxelMap in MyVoxelMaps.GetVoxelMaps()) { SaveVoxel(voxelMap); } }
// Initialized new voxel cell cache. Capacity specified count of voxel cells in the cache. public static void LoadContent() { MyMwcLog.WriteLine("MyVoxelCacheRender.LoadContent() - START"); MyMwcLog.IncreaseIndent(); MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().StartProfilingBlock("MyVoxelCacheRender::LoadContent"); foreach (KeyValuePair <Int64, LinkedListNode <MyVoxelCacheCellRender> > kvp in m_cellsByCoordinate) { //kvp.Value.Value.Reset(); m_priority.Remove(kvp.Value.Value); m_priority.AddFirst(kvp.Value.Value); } m_cellsByCoordinate.Clear();//forces to recreate all rendering cells on next call //this is here becouse it worked and is probably right way how to invalidate this stuff (really dont know why) foreach (MyVoxelMap map in MyVoxelMaps.GetVoxelMaps()) { map.InvalidateCache(new MyMwcVector3Int(-1000, -1000, -1000), new MyMwcVector3Int(1000, 1000, 1000)); } MinerWars.AppCode.Game.Render.MyRender.GetRenderProfiler().EndProfilingBlock(); MyMwcLog.DecreaseIndent(); MyMwcLog.WriteLine("MyVoxelCacheRender.LoadContent() - END"); }
public void Close(bool needLock) { if (needLock) { MyEntities.EntityCloseLock.AcquireExclusive(); } MyVoxelMaps.RemoveVoxelMapOreDepositCell(this); m_voxelMap = null; if (m_oreWithContent != null) { m_oreWithContent.Clear(); m_oreWithContent = null; } m_allMaterialsContent = null; m_allMaterialsPositions = null; m_helpersMaxContentForMaterial = null; m_totalSumOfOreContent = 0; if (OnClose != null) { OnClose(this); OnClose = null; } if (needLock) { MyEntities.EntityCloseLock.ReleaseExclusive(); } }
// Update cell - immediately after it was last time used. It will get higher priority and won't be flushed when AddCell() called next time. public static void UpdateCell(int voxelMapId, ref MyMwcVector3Int cellCoord, MyLodTypeEnum cellHashType) { Int64 key = MyVoxelMaps.GetCellHashCode(voxelMapId, ref cellCoord, cellHashType); LinkedListNode <MyVoxelCacheCellRender> ret = m_cellsByCoordinate[key]; // Move it to the end of priority linked list lock (m_priorityLocker) { m_priority.Remove(ret); m_priority.AddLast(ret); } }
public static bool LoadAllVoxels() { bool foundAll = true; foreach (var voxelMap in MyVoxelMaps.GetVoxelMaps()) { var foundVoxelMap = LoadVoxel(voxelMap); foundAll &= foundVoxelMap; } return(foundAll); }
// Gets cell from the cache. If cell isn't in the cache, null is returned. // This is only lookup into hashtable. No precalc is made here. static MyVoxelCacheCellRender GetCellFromCache(int voxelMapId, ref MyMwcVector3Int cellCoord, MyLodTypeEnum cellHashType) { Int64 key = MyVoxelMaps.GetCellHashCode(voxelMapId, ref cellCoord, cellHashType); LinkedListNode <MyVoxelCacheCellRender> ret; if (m_cellsByCoordinate.TryGetValue(key, out ret) == true) { return(ret.Value); } return(null); }
private static void TryLoadCellInBackground(MyVoxelMap voxelMap, ref MyMwcVector3Int renderCellCoord, MyLodTypeEnum cellHashType) { long key = MyVoxelMaps.GetCellHashCode(voxelMap.VoxelMapId, ref renderCellCoord, cellHashType); bool loadingCell = m_cellsBeingLoaded.Contains(key); if (loadingCell) { return; // already loading the cell } m_cellsBeingLoaded.Add(key); LoadCellInBackground(voxelMap, ref renderCellCoord, cellHashType); }
public static bool IsCollidingWithVoxelMap(MyMwcVoxelFilesEnum voxelFileEnum, Vector3 voxelPosition) { MyVoxelFile voxelFile = MyVoxelFiles.Get(voxelFileEnum); Vector3 sizeInMeters = MyVoxelMap.GetVoxelSizeInMetres(ref voxelFile.SizeInVoxels); BoundingBox newBoundingBox = MyUtils.GetNewBoundingBox(voxelPosition, sizeInMeters); MyVoxelMap intersectingVoxelMap = MyVoxelMaps.GetVoxelMapWhoseBoundingBoxIntersectsBox(ref newBoundingBox, null); if (intersectingVoxelMap != null) { return(true); } return(false); }
// Remove cell - after voxels were changed, etc. public static void RemoveCell(int voxelMapId, ref MyMwcVector3Int cellCoord, MyLodTypeEnum cellHashType) { Int64 key = MyVoxelMaps.GetCellHashCode(voxelMapId, ref cellCoord, cellHashType); // If cell is in cache, we remove it from dictionary and move it to the beginning of priority linked list LinkedListNode <MyVoxelCacheCellRender> ret; if (m_cellsByCoordinate.TryGetValue(key, out ret) == true) { m_cellsByCoordinate.Remove(key); // Dispose vertex buffers ret.Value.Reset(); // Move it to the beginning of priority linked list lock (m_priorityLocker) { m_priority.Remove(ret); m_priority.AddFirst(ret); } } }
// Update cell - immediately after it was last time used. It will get higher priority and won't be flushed when AddCell() called next time. public static void UpdateCell(int voxelMapId, ref MyMwcVector3Int cellCoord) { //bool lockTaken = false; //try //{ // m_spinLock.Enter(ref lockTaken); lock (Locker) { Int64 key = MyVoxelMaps.GetCellHashCode(voxelMapId, ref cellCoord, MyLodTypeEnum.LOD0); LinkedListNode <MyVoxelCacheCellData> cell = m_cellsByCoordinate[key]; // Move it to the end of priority linked list m_priority.Remove(cell); m_priority.AddLast(cell); } //} //finally //{ // if (lockTaken) m_spinLock.Exit(); //} }
// Add cell into cache and returns reference to it. Cache item with lowest priority is choosen. // Call this method when you want allocate new item in the cache. public static MyVoxelCacheCellData AddCell(int voxelMapId, ref MyMwcVector3Int cellCoord) { Int64 key = MyVoxelMaps.GetCellHashCode(voxelMapId, ref cellCoord, MyLodTypeEnum.LOD0); // Cache item with lowest priority is choosen. LinkedListNode <MyVoxelCacheCellData> first = m_priority.First; m_priority.RemoveFirst(); if (first.Value.Key != MyVoxelCacheCellData.INVALID_KEY) { m_cellsByCoordinate.Remove(first.Value.Key); } m_priority.AddLast(first); m_cellsByCoordinate.Add(key, first); first.Value.Key = key; // You have exceeded the capacity of DATA cells cache. Thats serious bug! MyCommonDebugUtils.AssertDebug(m_cellsByCoordinate.Count <= m_capacity); return(first.Value); }