Skip to content

Commit

Permalink
Revert ConditionEvaluatorBuilder + Propagate imagery async processing
Browse files Browse the repository at this point in the history
  • Loading branch information
jetelain committed Aug 24, 2024
1 parent a1ec325 commit b73b0a6
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 97 deletions.
6 changes: 3 additions & 3 deletions GameRealisticMap.Arma3/Arma3MapGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected virtual BuildContext CreateBuildContext(IProgressScope progress, Arma3

// Generate content
var context = CreateBuildContext(progress, a3config, osmSource);
var results = GenerateWrp(progress, a3config, context, a3config.TerrainArea, generators);
var results = await GenerateWrp(progress, a3config, context, a3config.TerrainArea, generators);
context.DisposeHugeImages();
if (progress.CancellationToken.IsCancellationRequested)
{
Expand All @@ -117,7 +117,7 @@ protected virtual async Task<IOsmDataSource> LoadOsmData(IProgressScope progress
return await loader.Load(a3config.TerrainArea);
}

public WrpAndContextResults? GenerateWrp(IProgressScope progress, Arma3MapConfig config, IContext context, ITerrainArea area, Arma3LayerGeneratorCatalog generators)
public async ValueTask<WrpAndContextResults?> GenerateWrp(IProgressScope progress, Arma3MapConfig config, IContext context, ITerrainArea area, Arma3LayerGeneratorCatalog generators)
{
// Game config
new GameConfigGenerator(assets, projectDrive).Generate(config, context, area);
Expand All @@ -135,7 +135,7 @@ protected virtual async Task<IOsmDataSource> LoadOsmData(IProgressScope progress
var imageryCompiler = new ImageryCompiler(assets.Materials, progress, projectDrive);

var gridTask = context.GetDataAsync<ElevationData>();
var tiles = imageryCompiler.Compile(config, CreateImagerySource(progress, config, context));
var tiles = await imageryCompiler.Compile(config, CreateImagerySource(progress, config, context));
if (progress.CancellationToken.IsCancellationRequested)
{
return null;
Expand Down
4 changes: 2 additions & 2 deletions GameRealisticMap.Arma3/Arma3TerrainBuilderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ private async Task<List<ImageryPart>> ExportImagery(IProgressScope progress, Arm
var parts = GenerateParts(config);

var source = new ImagerySource(assets.Materials, progress, projectDrive, config, context);
using (var idMap = source.CreateIdMap())
using (var idMap = await source.CreateIdMap())
{
await WriteImage(progress, idMap, Path.Combine(targetDirectory, "idmap.png"), parts).ConfigureAwait(false);
}
ImageryCompiler.CreateConfigCppImages(projectDrive, config, source);
using (var satMap = source.CreateSatMap())
using (var satMap = await source.CreateSatMap())
{
await WriteImage(progress, satMap, Path.Combine(targetDirectory, "satmap.png"), parts).ConfigureAwait(false);
}
Expand Down
16 changes: 8 additions & 8 deletions GameRealisticMap.Arma3/Demo/Arma3GdtDemoImagerySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Arma3GdtDemoImagerySource(IProgressScope progress, Arma3MapConfig config,
this.defaultMaterial = materials.GetMaterialByUsage(TerrainMaterialUsage.Default);
}

public HugeImage<Rgba32> CreateIdMap()
public async Task<HugeImage<Rgba32>> CreateIdMap()
{
Rgba32 defaultColor = default;
defaultMaterial.Id.ToRgba32(ref defaultColor);
Expand All @@ -38,7 +38,7 @@ public HugeImage<Rgba32> CreateIdMap()
var x = 0;
var y = 0;
var opt = new DrawingOptions() { GraphicsOptions = new GraphicsOptions() { Antialias = false } };
image.MutateAllAsync(p =>
await image.MutateAllAsync(p =>
{
p.Fill(defaultColor);
foreach (var def in materials.Definitions)
Expand All @@ -51,24 +51,24 @@ public HugeImage<Rgba32> CreateIdMap()
y += SquareSizeInPixels;
}
}
}).GetAwaiter().GetResult();
});
return image;
}

public Image CreatePictureMap()
public Task<Image> CreatePictureMap()
{
var img = new Image<Rgba32>(256, 256, new Rgba32(255,255,255,255));
return img;
return Task.FromResult((Image)img);
}

public HugeImage<Rgba32> CreateSatMap()
public async Task<HugeImage<Rgba32>> CreateSatMap()
{
var size = config.GetSatMapSize();
var image = new HugeImage<Rgba32>(context.HugeImageStorage, GetType().Name, new Size(size.Width, size.Height));
var x = 0;
var y = 0;
var defaultBrush = new ImageBrush(Image.Load(defaultMaterial.FakeSatPngImage));
image.MutateAllAsync(p =>
await image.MutateAllAsync(p =>
{
p.Fill(defaultBrush);
foreach (var def in materials.Definitions)
Expand All @@ -82,7 +82,7 @@ public HugeImage<Rgba32> CreateSatMap()
y += SquareSizeInPixels;
}
}
}).GetAwaiter().GetResult();
});
return image;

}
Expand Down
6 changes: 3 additions & 3 deletions GameRealisticMap.Arma3/GameEngine/IImagerySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace GameRealisticMap.Arma3.GameEngine
{
public interface IImagerySource
{
HugeImage<Rgba32> CreateIdMap();
Task<HugeImage<Rgba32>> CreateIdMap();

Image CreatePictureMap();
Task<Image> CreatePictureMap();

HugeImage<Rgba32> CreateSatMap();
Task<HugeImage<Rgba32>> CreateSatMap();

Image CreateSatOut();
}
Expand Down
20 changes: 10 additions & 10 deletions GameRealisticMap.Arma3/GameEngine/ImageryCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,46 +47,46 @@ public ImageryCompiler(TerrainMaterialLibrary materialLibrary, IProgressScope pr
this.gameFileSystemWriter = gameFileSystemWriter;
}

public ImageryTiler Compile(IArma3MapConfig config, IImagerySource source)
public async ValueTask<ImageryTiler> Compile(IArma3MapConfig config, IImagerySource source)
{
gameFileSystemWriter.CreateDirectory($"{config.PboPrefix}\\data\\layers");

TerrainMaterialHelper.UnpackEmbeddedFiles(materialLibrary, progress, gameFileSystemWriter, config);

var tiler = new ImageryTiler(config);

var idTask = Task.Run(() =>
var idTask = Task.Run(async () =>
{
using (var idMap = source.CreateIdMap())
using (var idMap = await source.CreateIdMap())
{
// idMap.SaveAsPng("idmap.png");
GenerateIdMapTilesAndRvMat(config, idMap, tiler);
}
});

CreateConfigCppImages(gameFileSystemWriter, config, source);
await CreateConfigCppImages(gameFileSystemWriter, config, source);

var satTask = Task.Run(() =>
var satTask = Task.Run(async () =>
{
using (var satMap = source.CreateSatMap())
using (var satMap = await source.CreateSatMap())
{
// satMap.SaveAsPng("satmap.png");
GenerateSatMapTiles(config, satMap, tiler);
}
});

idTask.Wait();
satTask.Wait();
await idTask;
await satTask;

return tiler;
}

public static void CreateConfigCppImages(IGameFileSystemWriter gameFileSystemWriter, IArma3MapConfig config, IImagerySource source)
public static async ValueTask CreateConfigCppImages(IGameFileSystemWriter gameFileSystemWriter, IArma3MapConfig config, IImagerySource source)
{
var picturemapFile = $"{config.PboPrefix}\\data\\picturemap_ca.png";
//if (!gameFileSystemWriter.FileExists(picturemapFile))
{
using (var satMapOut = source.CreatePictureMap())
using (var satMapOut = await source.CreatePictureMap())
{
gameFileSystemWriter.WritePngImage(picturemapFile, satMapOut);
}
Expand Down
6 changes: 3 additions & 3 deletions GameRealisticMap.Arma3/Imagery/FakeSatRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public FakeSatRender(TerrainMaterialLibrary materialLibrary, IProgressScope prog
this.gameFileSystem = gameFileSystem;
}

public override HugeImage<Rgba32> Render(IArma3MapConfig config, IContext context)
public override async Task<HugeImage<Rgba32>> Render(IArma3MapConfig config, IContext context)
{
var image = base.Render(config, context);
var image = await base.Render(config, context);
using var step = scope.CreateSingle("FakeSatBlur");
image.MutateAllAsync(d => d.GaussianBlur(1.5f)).GetAwaiter().GetResult();
await image.MutateAllAsync(d => d.GaussianBlur(1.5f));
return image;
}

Expand Down
6 changes: 3 additions & 3 deletions GameRealisticMap.Arma3/Imagery/IdMapRenderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public IdMapRenderBase(TerrainMaterialLibrary materialLibrary, IProgressScope pr
drawingOptions = new DrawingOptions();
}

public virtual HugeImage<TPixel> Render(IArma3MapConfig config, IContext context)
public virtual async Task<HugeImage<TPixel>> Render(IArma3MapConfig config, IContext context)
{
var size = GetImageSize(config);

var image = new HugeImage<TPixel>(context.HugeImageStorage, GetType().Name, new Size(size.Width, size.Height));

image.MutateAllAsync(d =>
await image.MutateAllAsync(d =>
{
d.Fill(GetBrush(materialLibrary.GetMaterialByUsage(TerrainMaterialUsage.Default)));
Expand Down Expand Up @@ -82,7 +82,7 @@ public virtual HugeImage<TPixel> Render(IArma3MapConfig config, IContext context
DrawPolygons(config, d, TerrainMaterialUsage.Asphalt, context.GetData<AsphaltData>().Polygons);
}).GetAwaiter().GetResult();
});

return image;
}
Expand Down
8 changes: 4 additions & 4 deletions GameRealisticMap.Arma3/Imagery/ImagerySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ public ImagerySource(TerrainMaterialLibrary materialLibrary, IProgressScope prog
this.context = context;
}

public HugeImage<Rgba32> CreateIdMap()
public async Task<HugeImage<Rgba32>> CreateIdMap()
{
using var step = progress.CreateScope("Draw IdMap");
return new IdMapRender(materialLibrary, step).Render(config, context);
return await new IdMapRender(materialLibrary, step).Render(config, context);
}

public Image CreatePictureMap()
public Task<Image> CreatePictureMap()
{
return satMapRender.RenderPictureMap(config, context, 2048);
}

public HugeImage<Rgba32> CreateSatMap()
public Task<HugeImage<Rgba32>> CreateSatMap()
{
return satMapRender.Render(config, context);
}
Expand Down
22 changes: 11 additions & 11 deletions GameRealisticMap.Arma3/Imagery/SatMapRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public Image RenderSatOut(IArma3MapConfig config, IContext context, int size)
return new FakeSatRender(materialLibrary, progress, gameFileSystem).RenderSatOut(config, context, size);
}

public Image RenderPictureMap(IArma3MapConfig config, IContext context, int size)
public async Task<Image> RenderPictureMap(IArma3MapConfig config, IContext context, int size)
{
HugeImage<Rgba32> satMap;

if (config.FakeSatBlend == 1)
{
using var step = progress.CreateScope("Draw PictureMap");
satMap = new FakeSatRender(materialLibrary, step, gameFileSystem).Render(config, context);
satMap = await new FakeSatRender(materialLibrary, step, gameFileSystem).Render(config, context);
}
else
{
Expand All @@ -50,27 +50,27 @@ public Image RenderPictureMap(IArma3MapConfig config, IContext context, int size

// TODO: Add shadows based on elevation data

return satMap.ToScaledImageAsync(size, size).GetAwaiter().GetResult();
return await satMap.ToScaledImageAsync(size, size);
}

public HugeImage<Rgba32> Render(IArma3MapConfig config, IContext context)
public async Task<HugeImage<Rgba32>> Render(IArma3MapConfig config, IContext context)
{
var result = RenderBaseImageAsync(config, context).GetAwaiter().GetResult();
var result = await RenderBaseImageAsync(config, context);

// TODO: Add perlin noise ? (in natural areas ?)

// TODO: Maybe also add shadows based on elevation data ?

DrawRoads(config, context, result);
await DrawRoads(config, context, result);

return result;
}

private void DrawRoads(IArma3MapConfig config, IContext context, HugeImage<Rgba32> result)
private async Task DrawRoads(IArma3MapConfig config, IContext context, HugeImage<Rgba32> result)
{
var roads = context.GetData<RoadsData>().Roads;
using var report = progress.CreateSingle("DrawRoads");
result.MutateAllAsync(d =>
await result.MutateAllAsync(d =>
{
foreach (var road in roads.Where(r => r.SpecialSegment != WaySpecialSegment.Bridge))
{
Expand All @@ -79,15 +79,15 @@ private void DrawRoads(IArma3MapConfig config, IContext context, HugeImage<Rgba3
PolygonDrawHelper.DrawPolygon(d, polygon, GetBrush((Arma3RoadTypeInfos)road.RoadTypeInfos), config.TerrainToSatMapPixel);
}
}
}).GetAwaiter().GetResult();
});
}

private async Task<HugeImage<Rgba32>> RenderBaseImageAsync(IArma3MapConfig config, IContext context)
{
if (config.FakeSatBlend == 1)
{
using var scope = progress.CreateScope("Draw FakeSat");
return new FakeSatRender(materialLibrary, scope, gameFileSystem).Render(config, context);
return await new FakeSatRender(materialLibrary, scope, gameFileSystem).Render(config, context);
}

var satMap = context.GetData<RawSatelliteImageData>().Image;
Expand All @@ -109,7 +109,7 @@ await Parallel.ForEachAsync(satMap.PartsLoadedFirst, async (part, _) =>
{
using var scope = progress.CreateScope("Draw FakeSat");

using (var fakeSat = new FakeSatRender(materialLibrary, scope, gameFileSystem).Render(config, context))
using (var fakeSat = await new FakeSatRender(materialLibrary, scope, gameFileSystem).Render(config, context))
{
using var report = scope.CreateSingle("FakeSatBlend");
await satMap.MutateAllAsync(async d =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ private async Task<Arma3Assets> GetAssets(ModelInfoLibrary library, Arma3MapConf
public Task GenerateSatMap()
{
IoC.Get<IProgressTool>()
.RunTask(Labels.GenerateSatelliteImage, (t) => DoImagery(t, s => s.CreateSatMap().OffloadAsync()));
.RunTask(Labels.GenerateSatelliteImage, (t) => DoImagery(t, async s => await (await s.CreateSatMap()).OffloadAsync()));
return Task.CompletedTask;
}
public Task GenerateRawSatMap()
Expand All @@ -487,7 +487,7 @@ public Task GenerateRawSatMap()
public Task GenerateIdMap()
{
IoC.Get<IProgressTool>()
.RunTask(Labels.GenerateIdMapImage, (t) => DoImagery(t, s => s.CreateIdMap().OffloadAsync()));
.RunTask(Labels.GenerateIdMapImage, (t) => DoImagery(t, async s => await (await s.CreateIdMap()).OffloadAsync()));
return Task.CompletedTask;
}

Expand Down
39 changes: 9 additions & 30 deletions GameRealisticMap/Conditions/ConditionEvaluator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel;
using System.Numerics;
using System.Numerics;
using GameRealisticMap.ElevationModel;
using GameRealisticMap.Geometries;
using GameRealisticMap.ManMade;
Expand All @@ -23,34 +22,14 @@ public class ConditionEvaluator : IConditionEvaluator
internal const float MaxRoadBoxSearch = 75f;
internal const float MaxRoadDistance = MaxRoadBoxSearch * 1.414f; // MaxRoadBoxSearch * sqrt(2)

//public ConditionEvaluator(IBuildContext context)
//{
// this.areas = context.GetData<CategoryAreaData>();
// this.cities = context.GetData<CitiesData>();
// this.elevation = context.GetData<ElevationData>();
// this.ocean = context.GetData<OceanData>();
// this.roads = new TerrainSpacialIndex<Road>(context.Area);
// roads.AddRange(context.GetData<RoadsData>().Roads);
//}

public ConditionEvaluator(ITerrainArea aera, CategoryAreaData areas, CitiesData cities, ElevationData elevation, OceanData ocean, RoadsData roads)
{
this.areas = areas;
this.cities = cities;
this.elevation = elevation;
this.ocean = ocean;
this.roads = new TerrainSpacialIndex<Road>(aera);
this.roads.AddRange(roads.Roads);
}

public static async Task<ConditionEvaluator> CreateAsync(IBuildContext context)
{
var areas = context.GetDataAsync<CategoryAreaData>();
var cities = context.GetDataAsync<CitiesData>();
var elevation = context.GetDataAsync<ElevationData>();
var ocean = context.GetDataAsync<OceanData>();
var roads = context.GetDataAsync<RoadsData>();
return new ConditionEvaluator(context.Area, await areas, await cities, await elevation, await ocean, await roads);
public ConditionEvaluator(IBuildContext context)
{
this.areas = context.GetData<CategoryAreaData>();
this.cities = context.GetData<CitiesData>();
this.elevation = context.GetData<ElevationData>();
this.ocean = context.GetData<OceanData>();
this.roads = new TerrainSpacialIndex<Road>(context.Area);
roads.AddRange(context.GetData<RoadsData>().Roads);
}

public IPointConditionContext GetPointContext(TerrainPoint point, Road? road = null)
Expand Down
Loading

0 comments on commit b73b0a6

Please sign in to comment.