From 63d597e4ad41a842513dcce5726f950bcc703599 Mon Sep 17 00:00:00 2001 From: Vapre Date: Fri, 23 Oct 2020 23:24:43 +0200 Subject: [PATCH] ShroudRenderer UpdateShroud only when at least one cell is dirty. UpdateShroud shows up in profile reports as one of the most active methods (2.3% CPU time, main mono thread). This commit introduces `anyCellDirty` to indicate that at lease one of the cells was marked as dirty. Avoiding the need to traverse all projected cells of the map to find any dirty cell. This reduces the number of shroud updates by at least 50% during a test game. I assume this is related to renders occurring more often than logic ticks(?). --- OpenRA.Mods.Common/Traits/World/ShroudRenderer.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/OpenRA.Mods.Common/Traits/World/ShroudRenderer.cs b/OpenRA.Mods.Common/Traits/World/ShroudRenderer.cs index 50c2e47cbb..21867ac33e 100644 --- a/OpenRA.Mods.Common/Traits/World/ShroudRenderer.cs +++ b/OpenRA.Mods.Common/Traits/World/ShroudRenderer.cs @@ -100,6 +100,7 @@ namespace OpenRA.Mods.Common.Traits readonly CellLayer tileInfos; readonly CellLayer cellsDirty; + bool anyCellDirty; readonly Sprite[] fogSprites, shroudSprites; Shroud shroud; @@ -128,6 +129,7 @@ namespace OpenRA.Mods.Common.Traits tileInfos = new CellLayer(map); cellsDirty = new CellLayer(map); + anyCellDirty = true; // Load sprite variants var variantCount = info.ShroudVariants.Length; @@ -265,6 +267,7 @@ namespace OpenRA.Mods.Common.Traits // Dirty the full projected space so the cells outside // the map bounds can be initialized as fully shrouded. cellsDirty.Clear(true); + anyCellDirty = true; var tl = new PPos(0, 0); var br = new PPos(map.MapSize.X - 1, map.MapSize.Y - 1); UpdateShroud(new ProjectedCellRegion(map, tl, br)); @@ -272,6 +275,9 @@ namespace OpenRA.Mods.Common.Traits void UpdateShroud(IEnumerable region) { + if (!anyCellDirty) + return; + foreach (var puv in region) { var uv = (MPos)puv; @@ -294,6 +300,8 @@ namespace OpenRA.Mods.Common.Traits shroudLayer.Update(uv, shroudSprite, shroudPos, true); fogLayer.Update(uv, fogSprite, fogPos, true); } + + anyCellDirty = false; } void IRenderShroud.RenderShroud(WorldRenderer wr) @@ -307,6 +315,7 @@ namespace OpenRA.Mods.Common.Traits { var uv = (MPos)puv; cellsDirty[uv] = true; + anyCellDirty = true; var cell = uv.ToCPos(map); foreach (var direction in CVec.Directions) if (map.Contains((PPos)(cell + direction).ToMPos(map)))