Optimization: Connection recipient caching, resetting cachedshadow data instead of creating a new one

This commit is contained in:
Regalis11
2015-12-21 15:32:42 +02:00
parent 63dd5e1bf6
commit 63f5a501e8
8 changed files with 55 additions and 74 deletions
Binary file not shown.
@@ -30,6 +30,8 @@ namespace Barotrauma.Items.Components
public readonly ushort[] wireId;
private List<Connection> recipients;
public bool IsPower
{
get;
@@ -38,17 +40,7 @@ namespace Barotrauma.Items.Components
public List<Connection> Recipients
{
get
{
List<Connection> recipients = new List<Connection>();
for (int i = 0; i<MaxLinked; i++)
{
if (Wires[i] == null) continue;
Connection recipient = Wires[i].OtherConnection(this);
if (recipient != null) recipients.Add(recipient);
}
return recipients;
}
get { return recipients; }
}
public Item Item
@@ -72,6 +64,8 @@ namespace Barotrauma.Items.Components
//recipient = new Connection[MaxLinked];
Wires = new Wire[MaxLinked];
recipients = new List<Connection>();
IsOutput = (element.Name.ToString() == "output");
Name = ToolBox.GetAttributeString(element, "name", (IsOutput) ? "output" : "input");
@@ -140,6 +134,18 @@ namespace Barotrauma.Items.Components
//linked[index] = connectedItem;
//recipient[index] = otherConnection;
Wires[index] = wire;
UpdateRecipients();
}
public void UpdateRecipients()
{
recipients.Clear();
for (int i = 0; i < MaxLinked; i++)
{
if (Wires[i] == null) continue;
Connection recipient = Wires[i].OtherConnection(this);
if (recipient != null) recipients.Add(recipient);
}
}
public void SendSignal(string signal, Item sender, float power)
@@ -175,6 +181,8 @@ namespace Barotrauma.Items.Components
Wires[i].RemoveConnection(this);
Wires[i] = null;
}
recipients.Clear();
}
@@ -460,6 +468,8 @@ namespace Barotrauma.Items.Components
}
}
UpdateRecipients();
//wireId = null;
}
@@ -162,6 +162,7 @@ namespace Barotrauma.Items.Components
c.Wires[i] = wireComponent;
wireComponent.Connect(c, false);
}
c.UpdateRecipients();
}
}
}
@@ -41,10 +41,10 @@ namespace Barotrauma.Items.Components
public override void Move(Vector2 amount)
{
for (int i = 0; i < Nodes.Count; i++)
{
Nodes[i] += amount;
}
//for (int i = 0; i < Nodes.Count; i++)
//{
// Nodes[i] += amount;
//}
}
public Connection OtherConnection(Connection connection)
@@ -64,7 +64,10 @@ namespace Barotrauma.Items.Components
for (int n = 0; n< connections[i].Wires.Length; n++)
{
if (connections[i].Wires[n] == this) connections[i].Wires[n] = null;
if (connections[i].Wires[n] != this) continue;
connections[i].Wires[n] = null;
connections[i].UpdateRecipients();
}
connections[i] = null;
}
@@ -290,7 +293,7 @@ namespace Barotrauma.Items.Components
}
public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, bool editing)
public override void Draw(SpriteBatch spriteBatch, bool editing)
{
if (Nodes.Count == 0) return;
@@ -369,8 +372,8 @@ namespace Barotrauma.Items.Components
{
if (item.Submarine!=null)
{
start += item.Submarine.DrawPosition;
end += item.Submarine.DrawPosition;
start += item.Submarine.DrawPosition+Submarine.HiddenSubPosition;
end += item.Submarine.DrawPosition+Submarine.HiddenSubPosition;
}
start.Y = -start.Y;
+3 -11
View File
@@ -50,19 +50,11 @@ namespace Barotrauma
public void RemoveEntity(MapEntity entity)
{
Rectangle indices = GetIndices(entity.Rect);
if (indices.X < 0 || indices.Width >= entities.GetLength(0) ||
indices.Y < 0 || indices.Height >= entities.GetLength(1))
for (int x = 0; x <= entities.GetLength(0); x++)
{
DebugConsole.ThrowError("Error in EntityGrid.RemoveEntity: " + entity + " is outside the grid");
return;
}
for (int x = indices.X; x <= indices.Width; x++)
{
for (int y = indices.Y; y <= indices.Height; y++)
for (int y = 0; y <= entities.GetLength(1); y++)
{
entities[x, y].Remove(entity);
if (entities[x,y].Contains(entity)) entities[x, y].Remove(entity);
}
}
}
+4 -15
View File
@@ -129,7 +129,7 @@ namespace Barotrauma
viewRect.Y = -viewRect.Y;
float multiplier = 0.8f;
for (int i = 1; i < 5; i++)
for (int i = 1; i < 4; i++)
{
spriteBatch.Draw(dustParticles, new Rectangle(0,0,GameMain.GraphicsWidth,GameMain.GraphicsHeight),
new Rectangle((int)((backgroundPos.X * multiplier)), (int)((-backgroundPos.Y * multiplier)), cam.WorldView.Width*2, cam.WorldView.Height*2),
@@ -164,10 +164,7 @@ namespace Barotrauma
public void RenderWalls(GraphicsDevice graphicsDevice, Camera cam)
{
if (wallVertices == null) return;
Stopwatch sw = new Stopwatch();
sw.Start();
basicEffect.World = cam.ShaderTransform
* Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
@@ -202,12 +199,7 @@ namespace Barotrauma
basicEffect.CurrentTechnique = basicEffect.Techniques["BasicEffect_Texture"];
basicEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, (int)Math.Floor(wallVertices.VertexCount / 3.0f));
basicEffect.VertexColorEnabled = false;
basicEffect.TextureEnabled = true;
basicEffect.CurrentTechnique = basicEffect.Techniques["BasicEffect_Texture"];
basicEffect.CurrentTechnique.Passes[0].Apply();
for (int side = 0; side < 2; side++)
{
for (int i = 0; i < 2; i++)
@@ -221,10 +213,7 @@ namespace Barotrauma
}
}
sw.Stop();
Debug.WriteLine("level render: "+sw.ElapsedTicks);
}
public void Dispose()
+12 -13
View File
@@ -9,10 +9,6 @@ namespace Barotrauma.Lights
{
class CachedShadow : IDisposable
{
//public VertexPositionColor[] ShadowVertices;
//public VertexPositionTexture[] PenumbraVertices;
public VertexBuffer ShadowBuffer;
public Vector2 LightPos;
@@ -291,30 +287,33 @@ namespace Barotrauma.Lights
if (cachedShadows.TryGetValue(light, out cachedShadow) &&
(light.Position == cachedShadow.LightPos || Vector2.DistanceSquared(light.Position, cachedShadow.LightPos) < 1.0f))
{
//{
graphicsDevice.SetVertexBuffer(cachedShadow.ShadowBuffer);
shadowVertexCount = cachedShadow.ShadowVertexCount;
}
else
{
Vector2 lightPos = light.Position;
//if (light.Submarine!=null && parentEntity != null && parentEntity.Submarine == light.Submarine)
//{
// lightPos = light.Position;
//}
CalculateShadowVertices(lightPos, los);
CalculateShadowVertices(light.Position, los);
if (cachedShadow != null)
{
cachedShadow.Dispose();
cachedShadows.Remove(light);
cachedShadow.LightPos = light.Position;
cachedShadow.ShadowBuffer.SetData(shadowVertices, 0, shadowVertices.Length);
cachedShadow.ShadowVertexCount = shadowVertexCount;
}
else
{
cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, light.Position, shadowVertexCount, 0);
cachedShadows.Add(light, cachedShadow);
}
cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, light.Position, shadowVertexCount, 0);
cachedShadows.Add(light, cachedShadow);
}
graphicsDevice.SetVertexBuffer(cachedShadow.ShadowBuffer);
shadowVertexCount = cachedShadow.ShadowVertexCount;
DrawShadows(graphicsDevice, cam, transform, los);
}
+3 -16
View File
@@ -59,11 +59,7 @@ namespace Barotrauma.Lights
}
public void DrawLOS(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 pos)
{
Stopwatch sw = new Stopwatch();
sw.Start();
{
if (!LosEnabled) return;
Rectangle camView = new Rectangle(cam.WorldView.X, cam.WorldView.Y - cam.WorldView.Height, cam.WorldView.Width, cam.WorldView.Height);
@@ -78,10 +74,7 @@ namespace Barotrauma.Lights
convexHull.DrawShadows(graphics, cam, pos, shadowTransform);
}
sw.Stop();
Debug.WriteLine("drawlos: " + sw.ElapsedTicks + " (" + sw.ElapsedMilliseconds + ")");
if (!ObstructVision) return;
spriteBatch.Begin(SpriteSortMode.Deferred, CustomBlendStates.Multiplicative);
@@ -102,10 +95,6 @@ namespace Barotrauma.Lights
public void UpdateLightMap(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam)
{
Stopwatch sw = new Stopwatch();
sw.Start();
if (!LightingEnabled) return;
Matrix shadowTransform = cam.ShaderTransform
@@ -162,9 +151,7 @@ namespace Barotrauma.Lights
//clear alpha, to avoid messing stuff up later
ClearAlphaToOne(graphics, spriteBatch);
graphics.SetRenderTarget(null);
Debug.WriteLine("lights: " + sw.ElapsedTicks + " (" + sw.ElapsedMilliseconds + ")");
if (!ObstructVision) return;