Changeset 331
- Timestamp:
- Sep 6, 2018, 1:46:44 AM (6 years ago)
- Location:
- binary-improvements
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/FileCache/MapTileCache.cs
r329 r331 46 46 47 47 Profiler.BeginSample ("ReadPng"); 48 cacheEntry.pngData = File.ReadAllBytes (filename);48 cacheEntry.pngData = ReadAllBytes (filename); 49 49 Profiler.EndSample (); 50 50 } … … 63 63 lock (cache) { 64 64 CurrentZoomFile cacheEntry = cache [zoomlevel]; 65 66 if (string.IsNullOrEmpty (cacheEntry.filename)) { 65 66 string file = cacheEntry.filename; 67 if (string.IsNullOrEmpty (file)) { 67 68 return; 68 69 } … … 71 72 72 73 Profiler.BeginSample ("WritePng"); 73 File.WriteAllBytes (cacheEntry.filename, contentPng); 74 using (Stream stream = new FileStream (file, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 75 4096)) { 76 stream.Write (contentPng, 0, contentPng.Length); 77 } 74 78 Profiler.EndSample (); 75 79 } … … 92 96 } 93 97 94 return File.ReadAllBytes (filename);98 return ReadAllBytes (filename); 95 99 } 96 100 } catch (Exception e) { … … 101 105 } 102 106 107 private static byte[] ReadAllBytes (string _path) { 108 using (FileStream fileStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096)) { 109 int bytesRead = 0; 110 int bytesLeft = (int) fileStream.Length; 111 byte[] result = new byte[bytesLeft]; 112 while (bytesLeft > 0) { 113 int readThisTime = fileStream.Read (result, bytesRead, bytesLeft); 114 if (readThisTime == 0) { 115 throw new IOException ("Unexpected end of stream"); 116 } 117 118 bytesRead += readThisTime; 119 bytesLeft -= readThisTime; 120 } 121 122 return result; 123 } 124 } 125 126 103 127 private class CurrentZoomFile { 104 128 public string filename; -
binary-improvements/MapRendering/API.cs
r326 r331 2 2 using AllocsFixes.NetConnections.Servers.Web.Handlers; 3 3 4 namespace MapRendering{4 namespace AllocsFixes { 5 5 public class API : IModApi { 6 6 public void InitMod () { -
binary-improvements/MapRendering/MapRendering/Constants.cs
r325 r331 1 using UnityEngine; 2 1 3 namespace AllocsFixes.MapRendering { 2 4 public class Constants { 5 public static TextureFormat DEFAULT_TEX_FORMAT = TextureFormat.ARGB32; 3 6 public static int MAP_BLOCK_SIZE = 128; 4 7 public static int MAP_CHUNK_SIZE = 16; -
binary-improvements/MapRendering/MapRendering/MapRenderBlockBuffer.cs
r329 r331 8 8 namespace AllocsFixes.MapRendering { 9 9 public class MapRenderBlockBuffer { 10 private readonly Texture2D blockMap = new Texture2D (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE, TextureFormat.ARGB32, false);10 private readonly Texture2D blockMap = new Texture2D (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE, Constants.DEFAULT_TEX_FORMAT, false); 11 11 private readonly MapTileCache cache; 12 private readonly Color nullColor = new Color (0, 0, 0, 0);13 private readonly Texture2D zoomBuffer = new Texture2D ( 1, 1, TextureFormat.ARGB32, false);12 private readonly NativeArray<int> emptyImageData; 13 private readonly Texture2D zoomBuffer = new Texture2D (Constants.MAP_BLOCK_SIZE / 2, Constants.MAP_BLOCK_SIZE / 2, Constants.DEFAULT_TEX_FORMAT, false); 14 14 private readonly int zoomLevel; 15 15 private readonly string folderBase; … … 23 23 this.cache = cache; 24 24 folderBase = Constants.MAP_DIRECTORY + "/" + zoomLevel + "/"; 25 26 { 27 // Initialize empty tile data 28 Color nullColor = new Color (0, 0, 0, 0); 29 for (int x = 0; x < Constants.MAP_BLOCK_SIZE; x++) { 30 for (int y = 0; y < Constants.MAP_BLOCK_SIZE; y++) { 31 blockMap.SetPixel (x, y, nullColor); 32 } 33 } 34 35 NativeArray<int> blockMapData = blockMap.GetRawTextureData<int> (); 36 emptyImageData = new NativeArray<int> (blockMapData.Length, Allocator.Persistent, 37 NativeArrayOptions.UninitializedMemory); 38 blockMapData.CopyTo (emptyImageData); 39 } 25 40 } 26 41 27 42 public TextureFormat FormatSelf { 28 43 get { return blockMap.format; } 29 }30 31 public TextureFormat FormatScaled {32 get { return zoomBuffer.format; }33 44 } 34 45 … … 40 51 41 52 public void SaveBlock () { 53 Profiler.BeginSample ("SaveBlock"); 42 54 try { 43 if (currentBlockMapFilename.Length > 0) { 44 saveTextureToFile (currentBlockMapFilename); 45 } 55 saveTextureToFile (); 46 56 } catch (Exception e) { 47 57 Log.Warning ("Exception in MapRenderBlockBuffer.SaveBlock(): " + e); 48 58 } 59 Profiler.EndSample (); 49 60 } 50 61 51 62 public bool LoadBlock (Vector2i block) { 52 bool res = false;63 Profiler.BeginSample ("LoadBlock"); 53 64 lock (blockMap) { 54 65 if (currentBlockMapPos != block) { … … 56 67 string folder; 57 68 if (currentBlockMapPos.x != block.x) { 58 folder = folderBase + block.x ;69 folder = folderBase + block.x + '/'; 59 70 60 71 Profiler.BeginSample ("LoadBlock.Directory"); … … 65 76 } 66 77 67 string fileName = folder + "/" +block.y + ".png";78 string fileName = folder + block.y + ".png"; 68 79 Profiler.EndSample (); 69 80 70 if (!fileName.Equals (currentBlockMapFilename)) { 71 res = true; 72 SaveBlock (); 73 loadTextureFromFile (fileName); 74 } 81 SaveBlock (); 82 loadTextureFromFile (fileName); 75 83 76 84 currentBlockMapFolder = folder; 77 85 currentBlockMapPos = block; 78 86 currentBlockMapFilename = fileName; 79 } 80 } 81 82 return res; 87 88 Profiler.EndSample (); 89 return true; 90 } 91 } 92 93 Profiler.EndSample (); 94 return false; 83 95 } 84 96 … … 147 159 public NativeArray<int> GetHalfScaledNative () { 148 160 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 (); 161 if (zoomBuffer.format != blockMap.format || zoomBuffer.height != Constants.MAP_BLOCK_SIZE / 2 || zoomBuffer.width != Constants.MAP_BLOCK_SIZE / 2) { 162 zoomBuffer.Resize (Constants.MAP_BLOCK_SIZE / 2, Constants.MAP_BLOCK_SIZE / 2, blockMap.format, false); 163 163 } 164 164 Profiler.EndSample (); 165 165 166 166 Profiler.BeginSample ("HalfScaledNative.Scale"); 167 TextureScale.Point (zoomBuffer, Constants.MAP_BLOCK_SIZE / 2, Constants.MAP_BLOCK_SIZE / 2);167 ScaleNative (blockMap, zoomBuffer); 168 168 Profiler.EndSample (); 169 169 170 170 return zoomBuffer.GetRawTextureData<int> (); 171 } 172 173 private static void ScaleNative (Texture2D _sourceTex, Texture2D _targetTex) { 174 NativeArray<int> srcData = _sourceTex.GetRawTextureData<int> (); 175 NativeArray<int> targetData = _targetTex.GetRawTextureData<int> (); 176 177 int oldWidth = _sourceTex.width; 178 int oldHeight = _sourceTex.height; 179 int newWidth = _targetTex.width; 180 int newHeight = _targetTex.height; 181 182 float ratioX = ((float) oldWidth) / newWidth; 183 float ratioY = ((float) oldHeight) / newHeight; 184 185 for (var y = 0; y < newHeight; y++) { 186 var oldLineStart = (int) (ratioY * y) * oldWidth; 187 var newLineStart = y * newWidth; 188 for (var x = 0; x < newWidth; x++) { 189 targetData [newLineStart + x] = srcData [(int) (oldLineStart + ratioX * x)]; 190 } 191 } 171 192 } 172 193 … … 192 213 } 193 214 194 if (blockMap.height != Constants.MAP_BLOCK_SIZE || blockMap.width != Constants.MAP_BLOCK_SIZE) { 195 blockMap.Resize (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE, TextureFormat.ARGB32, false); 196 } 197 198 for (int x = 0; x < Constants.MAP_BLOCK_SIZE; x++) { 199 for (int y = 0; y < Constants.MAP_BLOCK_SIZE; y++) { 200 blockMap.SetPixel (x, y, nullColor); 201 } 202 } 203 Profiler.EndSample (); 204 } 205 206 private void saveTextureToFile (string _fileName) { 207 Profiler.BeginSample ("SaveTexture"); 208 215 if (blockMap.format != Constants.DEFAULT_TEX_FORMAT || blockMap.height != Constants.MAP_BLOCK_SIZE || 216 blockMap.width != Constants.MAP_BLOCK_SIZE) { 217 blockMap.Resize (Constants.MAP_BLOCK_SIZE, Constants.MAP_BLOCK_SIZE, Constants.DEFAULT_TEX_FORMAT, 218 false); 219 } 220 221 blockMap.LoadRawTextureData (emptyImageData); 222 223 Profiler.EndSample (); 224 } 225 226 private void saveTextureToFile () { 209 227 Profiler.BeginSample ("EncodePNG"); 210 228 byte[] array = blockMap.EncodeToPNG (); … … 212 230 213 231 cache.SaveTile (zoomLevel, array); 214 Profiler.EndSample ();215 232 } 216 233 } -
binary-improvements/MapRendering/MapRendering/MapRendering.cs
r329 r331 246 246 247 247 Profiler.BeginSample ("RenderDirtyChunks.Work"); 248 // Write all chunks that are in the same image tile of the highest zoom level 248 249 Vector2i v_block, v_blockOffset; 249 250 foreach (Vector2i v in chunksToRender) { … … 268 269 } 269 270 271 // Update lower zoom levels affected by the change of the highest one 270 272 RenderZoomLevel (block); 271 273 … … 285 287 286 288 Profiler.BeginSample ("RenderZoomLevel.Transfer"); 287 if ((zoomLevelBuffers [level].FormatS caled== TextureFormat.ARGB32 ||288 zoomLevelBuffers [level].FormatS caled== TextureFormat.RGBA32) &&289 zoomLevelBuffers [level].FormatS caled== zoomLevelBuffers [level - 1].FormatSelf) {289 if ((zoomLevelBuffers [level].FormatSelf == TextureFormat.ARGB32 || 290 zoomLevelBuffers [level].FormatSelf == TextureFormat.RGBA32) && 291 zoomLevelBuffers [level].FormatSelf == zoomLevelBuffers [level - 1].FormatSelf) { 290 292 zoomLevelBuffers [level - 1].SetPartNative (blockOffset, Constants.MAP_BLOCK_SIZE / 2, zoomLevelBuffers [level].GetHalfScaledNative ()); 291 293 } else { -
binary-improvements/MapRendering/WebAndMapRendering.csproj
r330 r331 70 70 <Compile Include="Commands\EnableRendering.cs" /> 71 71 <Compile Include="API.cs" /> 72 <Compile Include="MapRendering\TextureScale.cs" />73 72 <Compile Include="Web\API\GetAnimalsLocation.cs" /> 74 73 <Compile Include="Web\API\GetHostileLocation.cs" />
Note:
See TracChangeset
for help on using the changeset viewer.