Changeset 329
- Timestamp:
- Sep 5, 2018, 11:16:04 PM (6 years ago)
- Location:
- binary-improvements
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/FileCache/MapTileCache.cs
r326 r329 2 2 using System.IO; 3 3 using UnityEngine; 4 using UnityEngine.Profiling; 4 5 using Object = UnityEngine.Object; 5 6 … … 26 27 public void SetZoomCount (int count) { 27 28 cache = new CurrentZoomFile[count]; 29 for (int i = 0; i < cache.Length; i++) { 30 cache [i] = new CurrentZoomFile (); 31 } 28 32 } 29 33 … … 31 35 try { 32 36 lock (cache) { 33 if (cache [zoomlevel].filename == null || !cache [zoomlevel].filename.Equals (filename)) { 34 cache [zoomlevel].filename = filename; 37 CurrentZoomFile cacheEntry = cache [zoomlevel]; 38 39 if (cacheEntry.filename == null || !cacheEntry.filename.Equals (filename)) { 40 cacheEntry.filename = filename; 35 41 36 42 if (!File.Exists (filename)) { 37 cache [zoomlevel].data = null;43 cacheEntry.pngData = null; 38 44 return null; 39 45 } 40 46 41 cache [zoomlevel].data = File.ReadAllBytes (filename); 47 Profiler.BeginSample ("ReadPng"); 48 cacheEntry.pngData = File.ReadAllBytes (filename); 49 Profiler.EndSample (); 42 50 } 43 51 44 return cache [zoomlevel].data;52 return cacheEntry.pngData; 45 53 } 46 54 } catch (Exception e) { 47 Log. Out("Error in MapTileCache.LoadTile: " + e);55 Log.Warning ("Error in MapTileCache.LoadTile: " + e); 48 56 } 49 57 … … 51 59 } 52 60 53 public void SaveTile (int zoomlevel, byte[] content ) {61 public void SaveTile (int zoomlevel, byte[] contentPng) { 54 62 try { 55 63 lock (cache) { 56 if (cache [zoomlevel].filename == null) { 64 CurrentZoomFile cacheEntry = cache [zoomlevel]; 65 66 if (string.IsNullOrEmpty (cacheEntry.filename)) { 57 67 return; 58 68 } 69 70 cacheEntry.pngData = contentPng; 59 71 60 cache [zoomlevel].data = content; 61 File.WriteAllBytes (cache [zoomlevel].filename, content); 72 Profiler.BeginSample ("WritePng"); 73 File.WriteAllBytes (cacheEntry.filename, contentPng); 74 Profiler.EndSample (); 62 75 } 63 76 } catch (Exception e) { 64 Log. Out("Error in MapTileCache.SaveTile: " + e);77 Log.Warning ("Error in MapTileCache.SaveTile: " + e); 65 78 } 66 79 } … … 71 84 foreach (CurrentZoomFile czf in cache) { 72 85 if (czf.filename != null && czf.filename.Equals (filename)) { 73 return czf. data;86 return czf.pngData; 74 87 } 75 88 } … … 82 95 } 83 96 } catch (Exception e) { 84 Log. Out("Error in MapTileCache.GetFileContent: " + e);97 Log.Warning ("Error in MapTileCache.GetFileContent: " + e); 85 98 } 86 99 … … 88 101 } 89 102 90 private structCurrentZoomFile {103 private class CurrentZoomFile { 91 104 public string filename; 92 public byte[] data;105 public byte[] pngData; 93 106 } 94 107 } -
binary-improvements/MapRendering/MapRendering/MapRenderBlockBuffer.cs
r326 r329 2 2 using System.IO; 3 3 using AllocsFixes.FileCache; 4 using Unity.Collections; 4 5 using UnityEngine; 6 using UnityEngine.Profiling; 5 7 6 8 namespace AllocsFixes.MapRendering { 7 9 public class MapRenderBlockBuffer { 8 private readonly Texture2D blockMap = new Texture2D (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE );10 private readonly Texture2D blockMap = new Texture2D (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE, TextureFormat.ARGB32, false); 9 11 private readonly MapTileCache cache; 10 12 private readonly Color nullColor = new Color (0, 0, 0, 0); 11 private readonly Texture2D zoomBuffer = new Texture2D (1, 1 );13 private readonly Texture2D zoomBuffer = new Texture2D (1, 1, TextureFormat.ARGB32, false); 12 14 private readonly int zoomLevel; 13 private string currentBlockMap = string.Empty; 15 private readonly string folderBase; 16 17 private Vector2i currentBlockMapPos = new Vector2i (Int32.MinValue, Int32.MinValue); 18 private string currentBlockMapFolder = string.Empty; 19 private string currentBlockMapFilename = string.Empty; 14 20 15 21 public MapRenderBlockBuffer (int level, MapTileCache cache) { 16 22 zoomLevel = level; 17 23 this.cache = cache; 24 folderBase = Constants.MAP_DIRECTORY + "/" + zoomLevel + "/"; 25 } 26 27 public TextureFormat FormatSelf { 28 get { return blockMap.format; } 29 } 30 31 public TextureFormat FormatScaled { 32 get { return zoomBuffer.format; } 18 33 } 19 34 20 35 public void ResetBlock () { 21 currentBlockMap = string.Empty; 36 currentBlockMapFolder = string.Empty; 37 currentBlockMapFilename = string.Empty; 38 currentBlockMapPos = new Vector2i (Int32.MinValue, Int32.MinValue); 22 39 } 23 40 24 41 public void SaveBlock () { 25 42 try { 26 if (currentBlockMap .Length > 0) {27 saveTextureToFile (currentBlockMap );43 if (currentBlockMapFilename.Length > 0) { 44 saveTextureToFile (currentBlockMapFilename); 28 45 } 29 46 } catch (Exception e) { 30 Log. Out("Exception in MapRenderBlockBuffer.SaveBlock(): " + e);47 Log.Warning ("Exception in MapRenderBlockBuffer.SaveBlock(): " + e); 31 48 } 32 49 } … … 35 52 bool res = false; 36 53 lock (blockMap) { 37 string folder = Constants.MAP_DIRECTORY + "/" + zoomLevel + "/" + block.x; 38 string fileName = folder + "/" + block.y + ".png"; 39 Directory.CreateDirectory (folder); 40 if (!fileName.Equals (currentBlockMap)) { 41 res = true; 42 SaveBlock (); 43 loadTextureFromFile (fileName); 44 } 45 46 currentBlockMap = fileName; 54 if (currentBlockMapPos != block) { 55 Profiler.BeginSample ("LoadBlock.Strings"); 56 string folder; 57 if (currentBlockMapPos.x != block.x) { 58 folder = folderBase + block.x; 59 60 Profiler.BeginSample ("LoadBlock.Directory"); 61 Directory.CreateDirectory (folder); 62 Profiler.EndSample (); 63 } else { 64 folder = currentBlockMapFolder; 65 } 66 67 string fileName = folder + "/" + block.y + ".png"; 68 Profiler.EndSample (); 69 70 if (!fileName.Equals (currentBlockMapFilename)) { 71 res = true; 72 SaveBlock (); 73 loadTextureFromFile (fileName); 74 } 75 76 currentBlockMapFolder = folder; 77 currentBlockMapPos = block; 78 currentBlockMapFilename = fileName; 79 } 47 80 } 48 81 … … 51 84 52 85 public void SetPart (Vector2i offset, int partSize, Color32[] pixels) { 53 if (offset.x + partSize > blockMap.width || offset.y + partSize > blockMap.height) {86 if (offset.x + partSize > Constants.MAP_BLOCK_SIZE || offset.y + partSize > Constants.MAP_BLOCK_SIZE) { 54 87 Log.Error (string.Format ("MapBlockBuffer[{0}].SetPart ({1}, {2}, {3}) has blockMap.size ({4}/{5})", 55 zoomLevel, offset, partSize, pixels.Length, blockMap.width, blockMap.height));88 zoomLevel, offset, partSize, pixels.Length, Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE)); 56 89 return; 57 90 } 58 91 92 Profiler.BeginSample ("SetPart"); 59 93 blockMap.SetPixels32 (offset.x, offset.y, partSize, partSize, pixels); 94 Profiler.EndSample (); 60 95 } 61 96 62 97 public Color32[] GetHalfScaled () { 98 Profiler.BeginSample ("HalfScaled.ResizeBuffer"); 63 99 zoomBuffer.Resize (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE); 64 zoomBuffer.SetPixels32 (blockMap.GetPixels32 ()); 65 100 Profiler.EndSample (); 101 102 Profiler.BeginSample ("HalfScaled.CopyPixels"); 103 if (blockMap.format == zoomBuffer.format) { 104 Profiler.BeginSample ("Native"); 105 NativeArray<byte> dataSrc = blockMap.GetRawTextureData<byte> (); 106 NativeArray<byte> dataZoom = zoomBuffer.GetRawTextureData<byte> (); 107 dataSrc.CopyTo (dataZoom); 108 Profiler.EndSample (); 109 } else { 110 Profiler.BeginSample ("GetSetPixels"); 111 zoomBuffer.SetPixels32 (blockMap.GetPixels32 ()); 112 Profiler.EndSample (); 113 } 114 Profiler.EndSample (); 115 116 Profiler.BeginSample ("HalfScaled.Scale"); 66 117 TextureScale.Point (zoomBuffer, Constants.MAP_BLOCK_SIZE / 2, Constants.MAP_BLOCK_SIZE / 2); 67 68 return zoomBuffer.GetPixels32 (); 118 Profiler.EndSample (); 119 120 Profiler.BeginSample ("HalfScaled.Return"); 121 Color32[] result = zoomBuffer.GetPixels32 (); 122 Profiler.EndSample (); 123 124 return result; 125 } 126 127 public void SetPartNative (Vector2i offset, int partSize, NativeArray<int> pixels) { 128 if (offset.x + partSize > Constants.MAP_BLOCK_SIZE || offset.y + partSize > Constants.MAP_BLOCK_SIZE) { 129 Log.Error (string.Format ("MapBlockBuffer[{0}].SetPart ({1}, {2}, {3}) has blockMap.size ({4}/{5})", 130 zoomLevel, offset, partSize, pixels.Length, Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE)); 131 return; 132 } 133 134 Profiler.BeginSample ("SetPartNative"); 135 NativeArray<int> destData = blockMap.GetRawTextureData<int> (); 136 137 for (int y = 0; y < partSize; y++) { 138 int srcLineStartIdx = partSize * y; 139 int destLineStartIdx = blockMap.width * (offset.y + y) + offset.x; 140 for (int x = 0; x < partSize; x++) { 141 destData [destLineStartIdx + x] = pixels [srcLineStartIdx + x]; 142 } 143 } 144 Profiler.EndSample (); 145 } 146 147 public NativeArray<int> GetHalfScaledNative () { 148 Profiler.BeginSample ("HalfScaledNative.ResizeBuffer"); 149 zoomBuffer.Resize (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE); 150 Profiler.EndSample (); 151 152 Profiler.BeginSample ("HalfScaledNative.CopyPixels"); 153 if (blockMap.format == zoomBuffer.format) { 154 Profiler.BeginSample ("Native"); 155 NativeArray<byte> dataSrc = blockMap.GetRawTextureData<byte> (); 156 NativeArray<byte> dataZoom = zoomBuffer.GetRawTextureData<byte> (); 157 dataSrc.CopyTo (dataZoom); 158 Profiler.EndSample (); 159 } else { 160 Profiler.BeginSample ("GetSetPixels"); 161 zoomBuffer.SetPixels32 (blockMap.GetPixels32 ()); 162 Profiler.EndSample (); 163 } 164 Profiler.EndSample (); 165 166 Profiler.BeginSample ("HalfScaledNative.Scale"); 167 TextureScale.Point (zoomBuffer, Constants.MAP_BLOCK_SIZE / 2, Constants.MAP_BLOCK_SIZE / 2); 168 Profiler.EndSample (); 169 170 return zoomBuffer.GetRawTextureData<int> (); 69 171 } 70 172 71 173 private void loadTextureFromFile (string _fileName) { 174 Profiler.BeginSample ("LoadTexture"); 175 176 Profiler.BeginSample ("LoadFile"); 72 177 byte[] array = cache.LoadTile (zoomLevel, _fileName); 178 Profiler.EndSample (); 179 180 Profiler.BeginSample ("LoadImage"); 73 181 if (array != null && blockMap.LoadImage (array) && blockMap.height == Constants.MAP_BLOCK_SIZE && 74 182 blockMap.width == Constants.MAP_BLOCK_SIZE) { 183 Profiler.EndSample (); 184 185 Profiler.EndSample (); 75 186 return; 76 187 } 188 Profiler.EndSample (); 77 189 78 190 if (array != null) { … … 81 193 82 194 if (blockMap.height != Constants.MAP_BLOCK_SIZE || blockMap.width != Constants.MAP_BLOCK_SIZE) { 83 blockMap.Resize (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE );195 blockMap.Resize (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE, TextureFormat.ARGB32, false); 84 196 } 85 197 … … 89 201 } 90 202 } 203 Profiler.EndSample (); 91 204 } 92 205 93 206 private void saveTextureToFile (string _fileName) { 207 Profiler.BeginSample ("SaveTexture"); 208 209 Profiler.BeginSample ("EncodePNG"); 94 210 byte[] array = blockMap.EncodeToPNG (); 211 Profiler.EndSample (); 212 95 213 cache.SaveTile (zoomLevel, array); 214 Profiler.EndSample (); 96 215 } 97 216 } -
binary-improvements/MapRendering/MapRendering/MapRendering.cs
r326 r329 9 9 using AllocsFixes.JSON; 10 10 using UnityEngine; 11 using UnityEngine.Profiling; 11 12 using Object = UnityEngine.Object; 12 13 … … 67 68 public static void RenderSingleChunk (Chunk chunk) { 68 69 if (renderingEnabled) { 70 // TODO: Replace with regular thread and a blocking queue / set 69 71 ThreadPool.UnsafeQueueUserWorkItem (o => { 70 72 try { … … 194 196 } 195 197 } 198 199 private readonly WaitForSeconds coroutineDelay = new WaitForSeconds (0.2f); 196 200 197 201 private IEnumerator renderCoroutine () { … … 203 207 204 208 if (Time.time > renderTimeout || dirtyChunks.Count > 200) { 209 Profiler.BeginSample ("RenderDirtyChunks"); 205 210 RenderDirtyChunks (); 206 } 207 } 208 209 yield return new WaitForSeconds (0.2f); 210 } 211 } 211 Profiler.EndSample (); 212 } 213 } 214 215 yield return coroutineDelay; 216 } 217 } 218 219 private readonly List<Vector2i> chunksToRender = new List<Vector2i> (); 220 private readonly List<Vector2i> chunksRendered = new List<Vector2i> (); 212 221 213 222 private void RenderDirtyChunks () { … … 218 227 } 219 228 220 List<Vector2i> keys = new List<Vector2i> (dirtyChunks.Keys); 221 List<Vector2i> chunksDone = new List<Vector2i> (); 222 223 Vector2i chunkPos = keys [0]; 224 chunksDone.Add (chunkPos); 229 Profiler.BeginSample ("RenderDirtyChunks.Prepare"); 230 chunksToRender.Clear (); 231 chunksRendered.Clear (); 232 233 dirtyChunks.CopyKeysTo (chunksToRender); 234 235 Vector2i chunkPos = chunksToRender [0]; 236 chunksRendered.Add (chunkPos); 225 237 226 238 //Log.Out ("Start Dirty: " + chunkPos); … … 231 243 232 244 zoomLevelBuffers [Constants.ZOOMLEVELS - 1].LoadBlock (block); 233 245 Profiler.EndSample (); 246 247 Profiler.BeginSample ("RenderDirtyChunks.Work"); 234 248 Vector2i v_block, v_blockOffset; 235 foreach (Vector2i v in keys) {249 foreach (Vector2i v in chunksToRender) { 236 250 getBlockNumber (v, out v_block, out v_blockOffset, Constants.MAP_BLOCK_TO_CHUNK_DIV, 237 251 Constants.MAP_CHUNK_SIZE); 238 252 if (v_block.Equals (block)) { 239 253 //Log.Out ("Dirty: " + v + " render: true"); 240 chunks Done.Add (v);254 chunksRendered.Add (v); 241 255 if (dirtyChunks [v].Length != Constants.MAP_CHUNK_SIZE * Constants.MAP_CHUNK_SIZE) { 242 256 Log.Error (string.Format ("Rendering chunk has incorrect data size of {0} instead of {1}", … … 248 262 } 249 263 } 250 251 foreach (Vector2i v in chunksDone) { 264 Profiler.EndSample (); 265 266 foreach (Vector2i v in chunksRendered) { 252 267 dirtyChunks.Remove (v); 253 268 } … … 255 270 RenderZoomLevel (block); 256 271 272 Profiler.BeginSample ("RenderDirtyChunks.SaveAll"); 257 273 SaveAllBlockMaps (); 274 Profiler.EndSample (); 258 275 } 259 276 260 277 private void RenderZoomLevel (Vector2i innerBlock) { 278 Profiler.BeginSample ("RenderZoomLevel"); 261 279 int level = Constants.ZOOMLEVELS - 1; 262 280 while (level > 0) { … … 265 283 266 284 zoomLevelBuffers [level - 1].LoadBlock (block); 267 zoomLevelBuffers [level - 1].SetPart (blockOffset, Constants.MAP_BLOCK_SIZE / 2, zoomLevelBuffers [level].GetHalfScaled ()); 268 269 level = level - 1; 285 286 Profiler.BeginSample ("RenderZoomLevel.Transfer"); 287 if ((zoomLevelBuffers [level].FormatScaled == TextureFormat.ARGB32 || 288 zoomLevelBuffers [level].FormatScaled == TextureFormat.RGBA32) && 289 zoomLevelBuffers [level].FormatScaled == zoomLevelBuffers [level - 1].FormatSelf) { 290 zoomLevelBuffers [level - 1].SetPartNative (blockOffset, Constants.MAP_BLOCK_SIZE / 2, zoomLevelBuffers [level].GetHalfScaledNative ()); 291 } else { 292 zoomLevelBuffers [level - 1].SetPart (blockOffset, Constants.MAP_BLOCK_SIZE / 2, zoomLevelBuffers [level].GetHalfScaled ()); 293 } 294 Profiler.EndSample (); 295 296 level--; 270 297 innerBlock = block; 271 298 } 299 Profiler.EndSample (); 272 300 } 273 301 … … 374 402 } 375 403 376 private static Color shortColorToColor (ushort col) {377 return new Color (((col >> 10) & 31) / 31f, ((col >> 5) & 31) / 31f, (col & 31) / 31f, 255);378 }379 380 404 private static Color32 shortColorToColor32 (ushort col) { 381 405 byte r = (byte) (256 * ((col >> 10) & 31) / 32);
Note:
See TracChangeset
for help on using the changeset viewer.