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:
@@ -0,0 +1,115 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class EntityGrid
|
||||
{
|
||||
private List<MapEntity>[,] entities;
|
||||
|
||||
private Rectangle limits;
|
||||
|
||||
private float cellSize;
|
||||
|
||||
public EntityGrid(Rectangle limits, float cellSize)
|
||||
{
|
||||
this.limits = limits;
|
||||
this.cellSize = cellSize;
|
||||
|
||||
entities = new List<MapEntity>[(int)Math.Ceiling(limits.Width / cellSize),(int)Math.Ceiling(limits.Height / cellSize)];
|
||||
for (int x = 0; x<entities.GetLength(0); x++)
|
||||
{
|
||||
for (int y=0; y<entities.GetLength(1); y++)
|
||||
{
|
||||
entities[x, y] = new List<MapEntity>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void InsertEntity(MapEntity entity)
|
||||
{
|
||||
Rectangle rect = entity.Rect;
|
||||
//if (Submarine.Loaded != null) rect.Offset(-Submarine.HiddenSubPosition);
|
||||
Rectangle indices = GetIndices(rect);
|
||||
if (indices.X<0 || indices.Width>=entities.GetLength(0) ||
|
||||
indices.Y<0 || indices.Height>=entities.GetLength(1))
|
||||
{
|
||||
DebugConsole.ThrowError("Error in EntityGrid.InsertEntity: "+entity+" is outside the grid");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int x=indices.X; x<=indices.Width; x++)
|
||||
{
|
||||
for (int y = indices.Y; y<=indices.Height; y++)
|
||||
{
|
||||
entities[x, y].Add(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
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++)
|
||||
{
|
||||
entities[x, y].Remove(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
for (int x = 0; x < entities.GetLength(0); x++)
|
||||
{
|
||||
for (int y = 0; y < entities.GetLength(1); y++)
|
||||
{
|
||||
entities[x, y].Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<MapEntity> GetEntities(Vector2 position)
|
||||
{
|
||||
if (Submarine.Loaded != null) position -= Submarine.HiddenSubPosition;
|
||||
|
||||
if (position.X < limits.X || position.Y > limits.Y ||
|
||||
position.X > limits.Right || position.Y < limits.Y - limits.Height)
|
||||
{
|
||||
return new List<MapEntity>();
|
||||
}
|
||||
|
||||
Point indices = GetIndices(position);
|
||||
|
||||
return entities[indices.X, indices.Y];
|
||||
}
|
||||
|
||||
public Rectangle GetIndices(Rectangle rect)
|
||||
{
|
||||
Rectangle indices = Rectangle.Empty;
|
||||
indices.X = (int)Math.Floor((rect.X - limits.X) / cellSize);
|
||||
indices.Y = (int)Math.Floor((limits.Y - rect.Y)/cellSize);
|
||||
|
||||
indices.Width = (int)Math.Floor((rect.Right - limits.X) / cellSize);
|
||||
indices.Height = (int)Math.Floor((limits.Y - (rect.Y-rect.Height)) / cellSize);
|
||||
|
||||
return indices;
|
||||
}
|
||||
|
||||
public Point GetIndices(Vector2 position)
|
||||
{
|
||||
return new Point(
|
||||
(int)Math.Floor((position.X - limits.X) / cellSize),
|
||||
(int)Math.Floor((limits.Y - position.Y) / cellSize));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ namespace Barotrauma
|
||||
class Hull : MapEntity
|
||||
{
|
||||
public static List<Hull> hullList = new List<Hull>();
|
||||
private static EntityGrid entityGrid;
|
||||
|
||||
public static bool EditWater, EditFire;
|
||||
|
||||
@@ -165,9 +166,21 @@ namespace Barotrauma
|
||||
|
||||
Volume = 0.0f;
|
||||
|
||||
|
||||
InsertToList();
|
||||
}
|
||||
|
||||
public override void OnMapLoaded()
|
||||
{
|
||||
|
||||
if (entityGrid == null)
|
||||
{
|
||||
entityGrid = new EntityGrid(Submarine.Borders, 200.0f);
|
||||
}
|
||||
|
||||
entityGrid.InsertEntity(this);
|
||||
}
|
||||
|
||||
public override bool Contains(Vector2 position)
|
||||
{
|
||||
return (Submarine.RectContains(WorldRect, position) &&
|
||||
@@ -210,6 +223,8 @@ namespace Barotrauma
|
||||
|
||||
//renderer.Dispose();
|
||||
|
||||
entityGrid.RemoveEntity(this);
|
||||
|
||||
hullList.Remove(this);
|
||||
}
|
||||
|
||||
@@ -481,10 +496,30 @@ namespace Barotrauma
|
||||
//returns the water block which contains the point (or null if it isn't inside any)
|
||||
public static Hull FindHull(Vector2 position, Hull guess = null, bool useWorldCoordinates = true)
|
||||
{
|
||||
return FindHull(position, hullList, guess, useWorldCoordinates);
|
||||
if (entityGrid == null) return null;
|
||||
|
||||
if (guess != null)
|
||||
{
|
||||
if (Submarine.RectContains(useWorldCoordinates ? guess.WorldRect : guess.rect, position)) return guess;
|
||||
}
|
||||
|
||||
var entities = entityGrid.GetEntities(useWorldCoordinates ? position-Submarine.Loaded.Position : position);
|
||||
|
||||
foreach (Hull hull in entities)
|
||||
{
|
||||
if (Submarine.RectContains(useWorldCoordinates ? hull.WorldRect : hull.rect, position)) return hull;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Hull FindHull(Vector2 position, List<Hull> hulls, Hull guess = null, bool useWorldCoordinates = true)
|
||||
//returns the water block which contains the point (or null if it isn't inside any)
|
||||
public static Hull FindHullOld(Vector2 position, Hull guess = null, bool useWorldCoordinates = true)
|
||||
{
|
||||
return FindHullOld(position, hullList, guess, useWorldCoordinates);
|
||||
}
|
||||
|
||||
public static Hull FindHullOld(Vector2 position, List<Hull> hulls, Hull guess = null, bool useWorldCoordinates = true)
|
||||
{
|
||||
if (guess != null && hulls.Contains(guess))
|
||||
{
|
||||
|
||||
@@ -314,8 +314,8 @@ namespace Barotrauma
|
||||
wrappingWalls[side, i] = new WrappingWall(pathCells, cells, borders.Height * 0.5f,
|
||||
(side == 0 ? -1 : 1) * (i == 0 ? 1 : 2));
|
||||
|
||||
wrappingWalls[side, i].BodyVertices = GeneratePolygons(wrappingWalls[side, i].Cells, new List<VoronoiCell>(), false);
|
||||
wrappingWalls[side, i].WallVertices = GenerateWallShapes(wrappingWalls[side, i].Cells);
|
||||
wrappingWalls[side, i].SetBodyVertices(GeneratePolygons(wrappingWalls[side, i].Cells, new List<VoronoiCell>(), false));
|
||||
wrappingWalls[side, i].SetWallVertices(GenerateWallShapes(wrappingWalls[side, i].Cells));
|
||||
//wrappingWalls[side, i].Cells[0].edges[1].isSolid = false;
|
||||
//wrappingWalls[side, i].Cells[0].edges[3].isSolid = false;
|
||||
|
||||
@@ -1091,10 +1091,11 @@ namespace Barotrauma
|
||||
|
||||
private void Unload()
|
||||
{
|
||||
renderer.Dispose();
|
||||
renderer = null;
|
||||
|
||||
cells = null;
|
||||
|
||||
|
||||
bodies.Clear();
|
||||
bodies = null;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using System.Text;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class LevelRenderer
|
||||
class LevelRenderer : IDisposable
|
||||
{
|
||||
private static BasicEffect basicEffect;
|
||||
|
||||
@@ -182,16 +182,18 @@ namespace Barotrauma
|
||||
|
||||
graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, (int)Math.Floor(bodyVertices.VertexCount / 3.0f));
|
||||
|
||||
//for (int side = 0; side < 2; side++)
|
||||
//{
|
||||
// for (int i = 0; i < 2; i++)
|
||||
// {
|
||||
// graphicsDevice.DrawUserPrimitives(
|
||||
// PrimitiveType.TriangleList, level.WrappingWalls[side, i].BodyVertices, 0,
|
||||
// (int)Math.Floor(level.WrappingWalls[side, i].BodyVertices.Length / 3.0f));
|
||||
for (int side = 0; side < 2; side++)
|
||||
{
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
graphicsDevice.SetVertexBuffer(level.WrappingWalls[side, i].BodyVertices);
|
||||
|
||||
// }
|
||||
//}
|
||||
graphicsDevice.DrawPrimitives(
|
||||
PrimitiveType.TriangleList, 0,
|
||||
(int)Math.Floor(level.WrappingWalls[side, i].BodyVertices.VertexCount / 3.0f));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
graphicsDevice.SetVertexBuffer(wallVertices);
|
||||
@@ -201,25 +203,42 @@ namespace Barotrauma
|
||||
basicEffect.CurrentTechnique.Passes[0].Apply();
|
||||
graphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, (int)Math.Floor(wallVertices.VertexCount / 3.0f));
|
||||
|
||||
//for (int side = 0; side < 2; side++)
|
||||
//{
|
||||
// for (int i = 0; i < 2; i++)
|
||||
// {
|
||||
// basicEffect.VertexColorEnabled = false;
|
||||
// basicEffect.TextureEnabled = true;
|
||||
// basicEffect.CurrentTechnique = basicEffect.Techniques["BasicEffect_Texture"];
|
||||
// basicEffect.CurrentTechnique.Passes[0].Apply();
|
||||
// graphicsDevice.DrawUserPrimitives(
|
||||
// PrimitiveType.TriangleList, level.WrappingWalls[side, i].WallVertices, 0,
|
||||
// (int)Math.Floor(level.WrappingWalls[side, i].WallVertices.Length / 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++)
|
||||
{
|
||||
|
||||
graphicsDevice.SetVertexBuffer(level.WrappingWalls[side, i].WallVertices);
|
||||
|
||||
graphicsDevice.DrawPrimitives(
|
||||
PrimitiveType.TriangleList, 0,
|
||||
(int)Math.Floor(level.WrappingWalls[side, i].WallVertices.VertexCount / 3.0f));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
sw.Stop();
|
||||
|
||||
Debug.WriteLine("level render: "+sw.ElapsedTicks);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
wallVertices.Dispose();
|
||||
bodyVertices.Dispose();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,7 @@ namespace Barotrauma
|
||||
|
||||
public const float WallWidth = 20000.0f;
|
||||
|
||||
public VertexPositionTexture[] WallVertices;
|
||||
|
||||
public VertexPositionColor[] BodyVertices;
|
||||
private VertexBuffer wallVertices, bodyVertices;
|
||||
|
||||
private Vector2 midPos;
|
||||
private int slot;
|
||||
@@ -26,6 +24,16 @@ namespace Barotrauma
|
||||
|
||||
private List<VoronoiCell> cells;
|
||||
|
||||
public VertexBuffer WallVertices
|
||||
{
|
||||
get { return wallVertices; }
|
||||
}
|
||||
|
||||
public VertexBuffer BodyVertices
|
||||
{
|
||||
get { return bodyVertices; }
|
||||
}
|
||||
|
||||
public Vector2 Offset
|
||||
{
|
||||
get { return offset; }
|
||||
@@ -116,6 +124,18 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
public void SetWallVertices(VertexPositionTexture[] vertices)
|
||||
{
|
||||
wallVertices = new VertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionTexture.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
|
||||
wallVertices.SetData(vertices);
|
||||
}
|
||||
|
||||
public void SetBodyVertices(VertexPositionColor[] vertices)
|
||||
{
|
||||
bodyVertices = new VertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionColor.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
|
||||
bodyVertices.SetData(vertices);
|
||||
}
|
||||
|
||||
|
||||
public static void UpdateWallShift(Vector2 pos, WrappingWall[,] walls)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Barotrauma
|
||||
|
||||
//which entities have been selected for editing
|
||||
protected static List<MapEntity> selectedList = new List<MapEntity>();
|
||||
|
||||
|
||||
protected static GUIComponent editingHUD;
|
||||
|
||||
protected static Vector2 selectionPos = Vector2.Zero;
|
||||
@@ -206,11 +206,6 @@ namespace Barotrauma
|
||||
/// </summary>
|
||||
public static void UpdateAll(Camera cam, float deltaTime)
|
||||
{
|
||||
foreach (Item item in Item.ItemList)
|
||||
{
|
||||
item.Updated = false;
|
||||
}
|
||||
|
||||
foreach (Hull hull in Hull.hullList)
|
||||
{
|
||||
hull.Update(cam, deltaTime);
|
||||
|
||||
Reference in New Issue
Block a user