Dispose of graphics resources deterministically.
Textures, FrameBuffers and VertexBuffers allocated by the Sdl2 Renderer were only being released via finalizers. This could lead to OpenGL out of memory errors since resources may not be cleaned up in a timely manner. To avoid this, IDisposable has been implemented and transitively applied to classes that use these resources. As a side-effect some static state is no longer static, particularly in Renderer, in order to facilitate this change and just for nicer design in general. Also dispose some bitmaps.
This commit is contained in:
committed by
RoosterDragon
parent
38b579a081
commit
f0f02dff5c
@@ -16,7 +16,7 @@ using OpenTK.Graphics.OpenGL;
|
||||
|
||||
namespace OpenRA.Renderer.Sdl2
|
||||
{
|
||||
public class Texture : ITexture
|
||||
public sealed class Texture : ITexture
|
||||
{
|
||||
int texture;
|
||||
TextureScaleFilter scaleFilter;
|
||||
@@ -26,6 +26,8 @@ namespace OpenRA.Renderer.Sdl2
|
||||
|
||||
public Size Size { get { return size; } }
|
||||
|
||||
bool disposed;
|
||||
|
||||
public TextureScaleFilter ScaleFilter
|
||||
{
|
||||
get
|
||||
@@ -55,9 +57,6 @@ namespace OpenRA.Renderer.Sdl2
|
||||
SetData(bitmap);
|
||||
}
|
||||
|
||||
void FinalizeInner() { GL.DeleteTextures(1, ref texture); }
|
||||
~Texture() { Game.RunAfterTick(FinalizeInner); }
|
||||
|
||||
void PrepareTexture()
|
||||
{
|
||||
ErrorHandler.CheckGlError();
|
||||
@@ -180,5 +179,24 @@ namespace OpenRA.Renderer.Sdl2
|
||||
0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
|
||||
ErrorHandler.CheckGlError();
|
||||
}
|
||||
|
||||
~Texture()
|
||||
{
|
||||
Game.RunAfterTick(() => Dispose(false));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Game.RunAfterTick(() => Dispose(true));
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
void Dispose(bool disposing)
|
||||
{
|
||||
if (disposed)
|
||||
return;
|
||||
disposed = true;
|
||||
GL.DeleteTextures(1, ref texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user