Skip to content

Commit c21e8ae

Browse files
authored
Merge pull request #545 from Sharpe49/fix-vram-usage
Fix VRAM usage
2 parents 4b00b10 + 3f57012 commit c21e8ae

File tree

9 files changed

+279
-135
lines changed

9 files changed

+279
-135
lines changed

Source/RunActivity/Viewer3D/Materials.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class SharedTextureManager
3737
readonly Viewer Viewer;
3838
readonly GraphicsDevice GraphicsDevice;
3939
Dictionary<string, Texture2D> Textures = new Dictionary<string, Texture2D>();
40-
Dictionary<string, bool> TextureMarks;
40+
Dictionary<string, bool> TextureMarks = new Dictionary<string, bool>();
4141

4242
[CallOnThread("Render")]
4343
internal SharedTextureManager(Viewer viewer, GraphicsDevice graphicsDevice)
@@ -198,7 +198,7 @@ public static Texture2D Get(GraphicsDevice graphicsDevice, string path)
198198

199199
public void Mark()
200200
{
201-
TextureMarks = new Dictionary<string, bool>(Textures.Count);
201+
TextureMarks.Clear();
202202
foreach (var path in Textures.Keys)
203203
TextureMarks.Add(path, false);
204204
}
@@ -212,7 +212,10 @@ public void Mark(Texture2D texture)
212212
public void Sweep()
213213
{
214214
foreach (var path in TextureMarks.Where(kvp => !kvp.Value).Select(kvp => kvp.Key))
215+
{
216+
Textures[path].Dispose();
215217
Textures.Remove(path);
218+
}
216219
}
217220

218221
[CallOnThread("Updater")]

Source/RunActivity/Viewer3D/Processes/GameStateRunActivity.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// COPYRIGHT 2009, 2010, 2011, 2012, 2013 by the Open Rails project.
1+
// COPYRIGHT 2021 by the Open Rails project.
22
//
33
// This file is part of Open Rails.
44
//
@@ -36,7 +36,6 @@
3636
using System.Security.Cryptography;
3737
using System.Text;
3838
using System.Text.RegularExpressions;
39-
using System.Threading;
4039
using System.Windows.Forms;
4140

4241
namespace Orts.Viewer3D.Processes
@@ -79,6 +78,14 @@ public GameStateRunActivity(string[] args)
7978
Arguments = args;
8079
}
8180

81+
internal override void Dispose()
82+
{
83+
Loading.Dispose();
84+
LoadingScreen.Dispose();
85+
LoadingBar.Dispose();
86+
base.Dispose();
87+
}
88+
8289
internal override void Update(RenderFrame frame, double totalRealSeconds)
8390
{
8491
UpdateLoading();
@@ -1240,12 +1247,11 @@ long GetProcessBytesLoaded()
12401247
return 0;
12411248
}
12421249

1243-
class LoadingPrimitive : RenderPrimitive
1250+
class LoadingPrimitive : RenderPrimitive, IDisposable
12441251
{
12451252
public readonly LoadingMaterial Material;
12461253
readonly VertexBuffer VertexBuffer;
12471254

1248-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
12491255
public LoadingPrimitive(Game game)
12501256
{
12511257
Material = GetMaterial(game);
@@ -1269,6 +1275,11 @@ virtual protected VertexPositionTexture[] GetVerticies(Game game)
12691275
new VertexPositionTexture(new Vector3(+dd - 0.5f, -dd + 0.5f, -3), new Vector2(1, 1)),
12701276
};
12711277
}
1278+
1279+
public void Dispose()
1280+
{
1281+
Material.Dispose();
1282+
}
12721283

12731284
public override void Draw(GraphicsDevice graphicsDevice)
12741285
{
@@ -1343,19 +1354,23 @@ protected override VertexPositionTexture[] GetVerticies(Game game)
13431354
}
13441355
}
13451356

1346-
class LoadingMaterial : Material
1357+
class LoadingMaterial : Material, IDisposable
13471358
{
13481359
public readonly LoadingShader Shader;
13491360
public readonly Texture2D Texture;
13501361

1351-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
13521362
public LoadingMaterial(Game game)
13531363
: base(null, null)
13541364
{
13551365
Shader = new LoadingShader(game.RenderProcess.GraphicsDevice);
13561366
Texture = GetTexture(game);
13571367
}
13581368

1369+
public void Dispose()
1370+
{
1371+
Texture?.Dispose();
1372+
}
1373+
13591374
virtual protected Texture2D GetTexture(Game game)
13601375
{
13611376
return SharedTextureManager.Get(game.RenderProcess.GraphicsDevice, Path.Combine(game.ContentPath, "Loading.png"));

Source/RunActivity/Viewer3D/RollingStock/MSTSLocomotiveViewer.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,10 @@ internal override void Mark()
355355
foreach (var pdl in ParticleDrawers.Values)
356356
foreach (var pd in pdl)
357357
pd.Mark();
358-
if (_CabRenderer != null)
359-
_CabRenderer.Mark();
358+
359+
_CabRenderer?.Mark();
360+
ThreeDimentionCabViewer?.Mark();
361+
360362
base.Mark();
361363
}
362364

@@ -3129,9 +3131,13 @@ public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
31293131

31303132
internal override void Mark()
31313133
{
3132-
// TODO: This is likely wrong; we should mark textures, shapes and other graphical resources here.
3134+
TrainCarShape?.Mark();
3135+
foreach (ThreeDimCabDigit threeDimCabDigit in DigitParts3D.Values)
3136+
{
3137+
threeDimCabDigit.Mark();
3138+
}
31333139
}
3134-
} // Class ThreeDimentionCabViewer
3140+
}
31353141

31363142
public class ThreeDimCabDigit
31373143
{

Source/RunActivity/Viewer3D/RollingStock/MSTSWagonViewer.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,10 +1398,9 @@ private void LoadTrackSound(string filename)
13981398
internal override void Mark()
13991399
{
14001400
TrainCarShape.Mark();
1401-
if (FreightShape != null)
1402-
FreightShape.Mark();
1403-
if (InteriorShape != null)
1404-
InteriorShape.Mark();
1401+
FreightShape?.Mark();
1402+
InteriorShape?.Mark();
1403+
FreightAnimations?.Mark();
14051404
}
14061405
}
14071406
}

Source/RunActivity/Viewer3D/RollingStock/SubSystems/FreightAnimationsViewer.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public FreightAnimationsViewer(Viewer viewer, MSTSWagon wagon, string wagonFolde
3535
Animations.Add(new FreightAnimationViewer(viewer, wagon, wagonFolderSlash, animation));
3636
}
3737
}
38+
39+
public void Mark()
40+
{
41+
foreach (var animation in Animations)
42+
{
43+
animation.Mark();
44+
}
45+
}
3846
}
3947

4048
public class FreightAnimationViewer
@@ -52,14 +60,14 @@ public FreightAnimationViewer(Viewer viewer, MSTSWagon wagon, string wagonFolder
5260
{
5361
if ( lodControl.DistanceLevels.Length > 0)
5462
{
55-
foreach ( var distanceLevel in lodControl.DistanceLevels)
63+
foreach (var distanceLevel in lodControl.DistanceLevels)
5664
{
5765
if (distanceLevel.SubObjects.Length > 0
5866
&& distanceLevel.SubObjects[0].ShapePrimitives.Length > 0
5967
&& distanceLevel.SubObjects[0].ShapePrimitives[0].Hierarchy.Length > 0)
60-
{
61-
distanceLevel.SubObjects[0].ShapePrimitives[0].Hierarchy[0] = distanceLevel.SubObjects[0].ShapePrimitives[0].Hierarchy.Length;
62-
}
68+
{
69+
distanceLevel.SubObjects[0].ShapePrimitives[0].Hierarchy[0] = distanceLevel.SubObjects[0].ShapePrimitives[0].Hierarchy.Length;
70+
}
6371
}
6472
}
6573
}
@@ -72,5 +80,10 @@ public FreightAnimationViewer(Viewer viewer, MSTSWagon wagon, string wagonFolder
7280
FreightShape.XNAMatrices[0] *= flipper;
7381
}
7482
}
83+
84+
public void Mark()
85+
{
86+
FreightShape.Mark();
87+
}
7588
}
7689
}

Source/RunActivity/Viewer3D/Shapes.cs

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class SharedShapeManager
5151
readonly Viewer Viewer;
5252

5353
Dictionary<string, SharedShape> Shapes = new Dictionary<string, SharedShape>();
54-
Dictionary<string, bool> ShapeMarks;
54+
Dictionary<string, bool> ShapeMarks = new Dictionary<string, bool>();
5555
SharedShape EmptyShape;
5656

5757
[CallOnThread("Render")]
@@ -87,7 +87,7 @@ public SharedShape Get(string path)
8787

8888
public void Mark()
8989
{
90-
ShapeMarks = new Dictionary<string, bool>(Shapes.Count);
90+
ShapeMarks.Clear();
9191
foreach (var path in Shapes.Keys)
9292
ShapeMarks.Add(path, false);
9393
}
@@ -101,7 +101,10 @@ public void Mark(SharedShape shape)
101101
public void Sweep()
102102
{
103103
foreach (var path in ShapeMarks.Where(kvp => !kvp.Value).Select(kvp => kvp.Key))
104+
{
105+
Shapes[path].Dispose();
104106
Shapes.Remove(path);
107+
}
105108
}
106109

107110
[CallOnThread("Updater")]
@@ -1343,7 +1346,7 @@ public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
13431346
}
13441347
}
13451348

1346-
public class ShapePrimitive : RenderPrimitive
1349+
public class ShapePrimitive : RenderPrimitive, IDisposable
13471350
{
13481351
public Material Material { get; protected set; }
13491352
public int[] Hierarchy { get; protected set; } // the hierarchy from the sub_object
@@ -1394,6 +1397,13 @@ public virtual void Mark()
13941397
{
13951398
Material.Mark();
13961399
}
1400+
1401+
public void Dispose()
1402+
{
1403+
VertexBuffer.Dispose();
1404+
IndexBuffer.Dispose();
1405+
PrimitiveCount = 0;
1406+
}
13971407
}
13981408

13991409
/// <summary>
@@ -1535,7 +1545,7 @@ public virtual void Mark()
15351545
}
15361546
#endif
15371547

1538-
public class SharedShape
1548+
public class SharedShape : IDisposable
15391549
{
15401550
static List<string> ShapeWarnings = new List<string>();
15411551

@@ -1668,7 +1678,7 @@ void LoadContent()
16681678
}
16691679
}
16701680

1671-
public class LodControl
1681+
public class LodControl : IDisposable
16721682
{
16731683
public DistanceLevel[] DistanceLevels;
16741684

@@ -1687,11 +1697,21 @@ public LodControl(lod_control MSTSlod_control, Helpers.TextureFlags textureFlags
16871697
internal void Mark()
16881698
{
16891699
foreach (var dl in DistanceLevels)
1700+
{
16901701
dl.Mark();
1702+
}
1703+
}
1704+
1705+
public void Dispose()
1706+
{
1707+
foreach (var dl in DistanceLevels)
1708+
{
1709+
dl.Dispose();
1710+
}
16911711
}
16921712
}
16931713

1694-
public class DistanceLevel
1714+
public class DistanceLevel : IDisposable
16951715
{
16961716
public float ViewingDistance;
16971717
public float ViewSphereRadius;
@@ -1726,11 +1746,21 @@ public DistanceLevel(distance_level MSTSdistance_level, Helpers.TextureFlags tex
17261746
internal void Mark()
17271747
{
17281748
foreach (var so in SubObjects)
1749+
{
17291750
so.Mark();
1751+
}
1752+
}
1753+
1754+
public void Dispose()
1755+
{
1756+
foreach (var so in SubObjects)
1757+
{
1758+
so.Dispose();
1759+
}
17301760
}
17311761
}
17321762

1733-
public class SubObject
1763+
public class SubObject : IDisposable
17341764
{
17351765
static readonly SceneryMaterialOptions[] UVTextureAddressModeMap = new[] {
17361766
SceneryMaterialOptions.TextureAddressModeWrap,
@@ -1951,7 +1981,17 @@ public SubObject(sub_object sub_object, ref int totalPrimitiveIndex, int[] hiera
19511981
internal void Mark()
19521982
{
19531983
foreach (var prim in ShapePrimitives)
1984+
{
19541985
prim.Mark();
1986+
}
1987+
}
1988+
1989+
public void Dispose()
1990+
{
1991+
foreach (var prim in ShapePrimitives)
1992+
{
1993+
prim.Dispose();
1994+
}
19551995
}
19561996
}
19571997

@@ -2180,7 +2220,17 @@ internal void Mark()
21802220
{
21812221
Viewer.ShapeManager.Mark(this);
21822222
foreach (var lod in LodControls)
2223+
{
21832224
lod.Mark();
2225+
}
2226+
}
2227+
2228+
public void Dispose()
2229+
{
2230+
foreach (var lod in LodControls)
2231+
{
2232+
lod.Dispose();
2233+
}
21842234
}
21852235
}
21862236

0 commit comments

Comments
 (0)