From 63f5a501e8bb0857420810e721d202f9e74f506f Mon Sep 17 00:00:00 2001 From: Regalis11 Date: Mon, 21 Dec 2015 15:32:42 +0200 Subject: [PATCH] Optimization: Connection recipient caching, resetting cachedshadow data instead of creating a new one --- .vs/Subsurface_Solution/v14/.suo | Bin 748544 -> 748544 bytes .../Items/Components/Signal/Connection.cs | 32 ++++++++++++------ .../Components/Signal/ConnectionPanel.cs | 1 + .../Source/Items/Components/Signal/Wire.cs | 19 ++++++----- Subsurface/Source/Map/EntityGrid.cs | 14 ++------ Subsurface/Source/Map/Levels/LevelRenderer.cs | 19 +++-------- Subsurface/Source/Map/Lights/ConvexHull.cs | 25 +++++++------- Subsurface/Source/Map/Lights/LightManager.cs | 19 ++--------- 8 files changed, 55 insertions(+), 74 deletions(-) diff --git a/.vs/Subsurface_Solution/v14/.suo b/.vs/Subsurface_Solution/v14/.suo index 26232258cb68a09c611923fb6d8bbb1da57657f8..37658a661bf39f8dfcc729cf7ec2f60810a6df46 100644 GIT binary patch delta 3127 zcmb_e4Nz6b6@I(-JnnsfyvL7#_(a49T16oQECqx&NYogp#6e6E8f+A65)}-oL{uJ0 zZLC55Zs#qLwqXc!B{9w5kl`lQQNT76ZIaf+TJ0c1l(u6`(wZ6v8HIieDr{3%}V*L9uyGIpQNC^&oy=8jE1zL>QLh=)&)qipLi0&mx!r1#k^{eIhawm|Q- z#zVhqt+IRS{x!zy@l902y=UElCirgv>Ya*+A_UP)j4agpT+W1pbmHr zs0H=|qX;q%vACNqH8yBsu1RM|rhOOd^qW&sSfHL9@K}I`YWtB$4=|hSuR0zLl|fVC zejKhkU=OgL=?m?PSctyCzH0`pvueeNC0`;?|N1}CCi?b^uylbJ=zq{V%$=-2%oyfrJco(G zI_2y3)T>nVx;{s8PtGs^t<5%R_fSk7#w%h`FgdFfm{U!Lm1^o)rwn3_}mq91M4f_n_!;qcW-Z;oPKsx43 zgZvJ+R9j|3sX@KV;5327utQ}4NCh|#UkB_`%y|Y_0pC{GGKozX^M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x8B>4%##OGyV_*I6{sv^j5kkZgZY%RCWo36WIhr*J9CjC zyK+_Xw3ojQ)C~6UWQJx-0wW8;XmcEzFlKODdQt2U*xkv)+53STgnB=D8LWqyA6ebY!@7dBb= zeO{wOEn3NTn}f{QWNm&@zr|MSnDou|tNlB_o<4wY?l7d87V1{%%5`Nr-qc|z!ta_n z9foV74&rdcBOimVfiEx^@8Fq#Hrka6#sXE&{*NMAvE^GJt2I5)dkqe$g}GI8aZQTQ z-)Z`G$Q|N(96aP%sEX#^tw|9oo;|P|K&bcjAH5&yOW*)d!7D(79fUjtUInj#KY=D7 z+TQ3*X!&1YM&W(F2YC|T^%2N-z;yWDhx{uz3I@S$f&2h`2#$e|z;W;~H~~(APrxbA z3Oc}P@F^IN{tiwgmTEf(Q)JNh{3OGq(3r9Pvl|H&MWIU?xBt63Fy+4KYXBp-DL{MoW^Lkjo�H8V)6XV5)!b@!Sc)`= zQj2eWdNo?mP=XM74zyOTE1k9>)yXA|Pe*uJpW_tht diff --git a/Subsurface/Source/Items/Components/Signal/Connection.cs b/Subsurface/Source/Items/Components/Signal/Connection.cs index 1f41d7b6a..60bde8f79 100644 --- a/Subsurface/Source/Items/Components/Signal/Connection.cs +++ b/Subsurface/Source/Items/Components/Signal/Connection.cs @@ -30,6 +30,8 @@ namespace Barotrauma.Items.Components public readonly ushort[] wireId; + private List recipients; + public bool IsPower { get; @@ -38,17 +40,7 @@ namespace Barotrauma.Items.Components public List Recipients { - get - { - List recipients = new List(); - for (int i = 0; i(); + 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; } diff --git a/Subsurface/Source/Items/Components/Signal/ConnectionPanel.cs b/Subsurface/Source/Items/Components/Signal/ConnectionPanel.cs index 06a5cb5cb..4f1b81a86 100644 --- a/Subsurface/Source/Items/Components/Signal/ConnectionPanel.cs +++ b/Subsurface/Source/Items/Components/Signal/ConnectionPanel.cs @@ -162,6 +162,7 @@ namespace Barotrauma.Items.Components c.Wires[i] = wireComponent; wireComponent.Connect(c, false); } + c.UpdateRecipients(); } } } diff --git a/Subsurface/Source/Items/Components/Signal/Wire.cs b/Subsurface/Source/Items/Components/Signal/Wire.cs index eb0fb6f54..593668fbd 100644 --- a/Subsurface/Source/Items/Components/Signal/Wire.cs +++ b/Subsurface/Source/Items/Components/Signal/Wire.cs @@ -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; diff --git a/Subsurface/Source/Map/EntityGrid.cs b/Subsurface/Source/Map/EntityGrid.cs index 0f433902a..679ee9098 100644 --- a/Subsurface/Source/Map/EntityGrid.cs +++ b/Subsurface/Source/Map/EntityGrid.cs @@ -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); } } } diff --git a/Subsurface/Source/Map/Levels/LevelRenderer.cs b/Subsurface/Source/Map/Levels/LevelRenderer.cs index d58ecafa9..7e07bc8d3 100644 --- a/Subsurface/Source/Map/Levels/LevelRenderer.cs +++ b/Subsurface/Source/Map/Levels/LevelRenderer.cs @@ -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() diff --git a/Subsurface/Source/Map/Lights/ConvexHull.cs b/Subsurface/Source/Map/Lights/ConvexHull.cs index f009f7f9d..adba60532 100644 --- a/Subsurface/Source/Map/Lights/ConvexHull.cs +++ b/Subsurface/Source/Map/Lights/ConvexHull.cs @@ -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); } diff --git a/Subsurface/Source/Map/Lights/LightManager.cs b/Subsurface/Source/Map/Lights/LightManager.cs index 5dd00f0ce..f69cff20c 100644 --- a/Subsurface/Source/Map/Lights/LightManager.cs +++ b/Subsurface/Source/Map/Lights/LightManager.cs @@ -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;