added DtTileCacheBuildResult

This commit is contained in:
ikpil 2023-07-01 12:24:23 +09:00
parent a68df35d91
commit f2d2288283
2 changed files with 26 additions and 22 deletions

View File

@ -53,39 +53,24 @@ namespace DotRecast.Detour.TileCache
return layers;
}
private List<byte[]> BuildMultiThread(RcByteOrder order, bool cCompatibility, int tw, int th, int threads)
{
var tasks = new ConcurrentQueue<Task<Tuple<int, int, List<byte[]>>>>();
var results = new List<DtTileCacheBuildResult>();
for (int y = 0; y < th; ++y)
{
for (int x = 0; x < tw; ++x)
{
int tx = x;
int ty = y;
var task = Task.Run(() =>
{
var partial = Build(tx, ty, order, cCompatibility);
return Tuple.Create(tx, ty, partial);
});
tasks.Enqueue(task);
var task = Task.Run(() => Build(tx, ty, order, cCompatibility));
results.Add(new DtTileCacheBuildResult(tx, ty, task));
}
}
var partialResults = tasks
.Select(x => x.Result)
.ToDictionary(x => Tuple.Create(x.Item1, x.Item2), x => x.Item3);
List<byte[]> layers = new List<byte[]>();
for (int y = 0; y < th; ++y)
{
for (int x = 0; x < tw; ++x)
{
var key = Tuple.Create(x, y);
layers.AddRange(partialResults[key]);
}
}
return layers;
return results
.SelectMany(x => x.task.Result)
.ToList();
}
protected abstract List<byte[]> Build(int tx, int ty, RcByteOrder order, bool cCompatibility);

View File

@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace DotRecast.Detour.TileCache
{
public class DtTileCacheBuildResult
{
public readonly int tx;
public readonly int ty;
public readonly Task<List<byte[]>> task;
public DtTileCacheBuildResult(int tx, int ty, Task<List<byte[]>> task)
{
this.tx = tx;
this.ty = ty;
this.task = task;
}
}
}