Optimization: FindHull spatial hashing, itemcomponent sounds in a dictionary, got rid of Item.Updated, rendering fixes, disposing shadow vertex buffers

This commit is contained in:
Regalis11
2015-12-21 11:01:35 +02:00
parent a62c6d6711
commit 2ff8643c02
16 changed files with 354 additions and 136 deletions
+43 -39
View File
@@ -1,16 +1,19 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Barotrauma.Lights
{
class CachedShadow
class CachedShadow : IDisposable
{
public VertexPositionColor[] ShadowVertices;
public VertexPositionTexture[] PenumbraVertices;
//public VertexPositionColor[] ShadowVertices;
//public VertexPositionTexture[] PenumbraVertices;
public VertexBuffer ShadowBuffer;
public Vector2 LightPos;
@@ -18,14 +21,29 @@ namespace Barotrauma.Lights
public CachedShadow(VertexPositionColor[] shadowVertices, VertexPositionTexture[] penumbraVertices, Vector2 lightPos, int shadowVertexCount, int penumbraVertexCount)
{
ShadowVertices = shadowVertices;
PenumbraVertices = penumbraVertices;
//var ShadowVertices = new VertexPositionColor [shadowVertices.Count()];
//shadowVertices.CopyTo(ShadowVertices, 0);
ShadowBuffer = new VertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionColor.VertexDeclaration, 6*2, BufferUsage.None);
ShadowBuffer.SetData(shadowVertices, 0, shadowVertices.Length);
ShadowVertexCount = shadowVertexCount;
PenumbraVertexCount = penumbraVertexCount;
LightPos = lightPos;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
ShadowBuffer.Dispose();
}
}
class ConvexHull
@@ -43,9 +61,7 @@ namespace Barotrauma.Lights
private VertexPositionColor[] shadowVertices;
private VertexPositionTexture[] penumbraVertices;
private VertexBuffer shadowBuffer, penumbraBuffer;
int shadowVertexCount;
private Entity parentEntity;
@@ -85,9 +101,6 @@ namespace Barotrauma.Lights
shadowVertices = new VertexPositionColor[6 * 2];
penumbraVertices = new VertexPositionTexture[6];
shadowBuffer = new VertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionColor.VertexDeclaration, 6*2, BufferUsage.WriteOnly);
vertices = points;
primitiveCount = vertices.Length;
@@ -100,7 +113,7 @@ namespace Barotrauma.Lights
list.Add(this);
}
private void CalculateDimensions()
{
Vector2 center = Vector2.Zero;
@@ -225,10 +238,6 @@ namespace Barotrauma.Lights
{
CalculatePenumbraVertices(startingIndex, endingIndex, lightSourcePos, los);
}
else
{
shadowBuffer.SetData(shadowVertices);
}
}
private void CalculatePenumbraVertices(int startingIndex, int endingIndex, Vector2 lightSourcePos, bool los)
@@ -283,21 +292,9 @@ namespace Barotrauma.Lights
(light.Position == cachedShadow.LightPos || Vector2.DistanceSquared(light.Position, cachedShadow.LightPos) < 1.0f))
{
//{
shadowVertices = cachedShadow.ShadowVertices;
penumbraVertices = cachedShadow.PenumbraVertices;
graphicsDevice.SetVertexBuffer(cachedShadow.ShadowBuffer);
shadowVertexCount = cachedShadow.ShadowVertexCount;
//}
//else
//CalculateShadowVertices(light.Position, los);
//cachedShadow.LightPos = light.Position;
//cachedShadow.ShadowVertices = shadowVertices;
//cachedShadow.PenumbraVertices = penumbraVertices;
}
else
{
@@ -309,7 +306,11 @@ namespace Barotrauma.Lights
CalculateShadowVertices(lightPos, los);
if (cachedShadows.ContainsKey(light)) cachedShadows.Remove(light);
if (cachedShadow != null)
{
cachedShadow.Dispose();
cachedShadows.Remove(light);
}
cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, light.Position, shadowVertexCount, 0);
cachedShadows.Add(light, cachedShadow);
}
@@ -339,20 +340,17 @@ namespace Barotrauma.Lights
}
if (shadowVertexCount>0)
{
{
shadowEffect.World = Matrix.CreateTranslation(offset) * transform;
shadowEffect.World = Matrix.CreateTranslation(offset) * transform;
shadowEffect.CurrentTechnique.Passes[0].Apply();
if (los || true)
if (los)
{
shadowEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, shadowVertices, 0, shadowVertexCount * 2 - 2);
}
else
{
graphicsDevice.SetVertexBuffer(shadowBuffer);
{
shadowEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, shadowVertexCount);
}
@@ -373,6 +371,12 @@ namespace Barotrauma.Lights
public void Remove()
{
foreach (KeyValuePair<LightSource, CachedShadow> cachedShadow in cachedShadows)
{
cachedShadow.Value.Dispose();
}
cachedShadows.Clear();
list.Remove(this);
}
+4 -3
View File
@@ -63,10 +63,11 @@ namespace Barotrauma.Lights
get { return range; }
set
{
float newRange = MathHelper.Clamp(value, 0.0f, 2048.0f);
if (range == newRange) return;
range = newRange;
float prevRange = range;
range = MathHelper.Clamp(value, 0.0f, 2048.0f);
if (Math.Abs(prevRange - range)<5.0f) return;
UpdateHullsInRange();
}
}