From ad2ae8b7639fb568068cf8c111fa8ff2cc66b1f1 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Wed, 23 Feb 2011 07:32:56 +1300 Subject: [PATCH] report memory usage for textures --- .../Graphics/IGraphicsDevice.cs | 3 +- OpenRA.Renderer.Cg/GraphicsDevice.cs | 5 ++- OpenRA.Renderer.Gl/GraphicsDevice.cs | 4 ++- OpenRA.Renderer.Gl/Texture.cs | 36 +++++++++++++++---- OpenRA.Renderer.Null/NullGraphicsDevice.cs | 4 ++- 5 files changed, 42 insertions(+), 10 deletions(-) diff --git a/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs b/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs index 8158a0e26d..95ebb25387 100755 --- a/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs +++ b/OpenRA.FileFormats/Graphics/IGraphicsDevice.cs @@ -35,7 +35,8 @@ namespace OpenRA.FileFormats.Graphics ITexture CreateTexture(); IShader CreateShader( string name ); - Size WindowSize { get; } + Size WindowSize { get; } + int GpuMemoryUsed { get; } void Clear( Color color ); void Present( IInputHandler inputHandler ); diff --git a/OpenRA.Renderer.Cg/GraphicsDevice.cs b/OpenRA.Renderer.Cg/GraphicsDevice.cs index b52464b636..b2bf86c2d0 100755 --- a/OpenRA.Renderer.Cg/GraphicsDevice.cs +++ b/OpenRA.Renderer.Cg/GraphicsDevice.cs @@ -317,6 +317,9 @@ namespace OpenRA.Renderer.Cg public IIndexBuffer CreateIndexBuffer( int size ) { return new IndexBuffer( this, size ); } public ITexture CreateTexture() { return new Texture( this ); } public ITexture CreateTexture( Bitmap bitmap ) { return new Texture( this, bitmap ); } - public IShader CreateShader( string name ) { return new Shader( this, name ); } + public IShader CreateShader( string name ) { return new Shader( this, name ); } + + + public int GpuMemoryUsed { get { return 0; } } } } diff --git a/OpenRA.Renderer.Gl/GraphicsDevice.cs b/OpenRA.Renderer.Gl/GraphicsDevice.cs index de0e0e28d5..2dae7ae3b8 100755 --- a/OpenRA.Renderer.Gl/GraphicsDevice.cs +++ b/OpenRA.Renderer.Gl/GraphicsDevice.cs @@ -310,6 +310,8 @@ namespace OpenRA.Renderer.Glsl public IIndexBuffer CreateIndexBuffer( int size ) { return new IndexBuffer( this, size ); } public ITexture CreateTexture() { return new Texture( this ); } public ITexture CreateTexture( Bitmap bitmap ) { return new Texture( this, bitmap ); } - public IShader CreateShader( string name ) { return new Shader( this, name ); } + public IShader CreateShader( string name ) { return new Shader( this, name ); } + + public int GpuMemoryUsed { get; internal set; } } } diff --git a/OpenRA.Renderer.Gl/Texture.cs b/OpenRA.Renderer.Gl/Texture.cs index d320f69a51..81e07055a8 100644 --- a/OpenRA.Renderer.Gl/Texture.cs +++ b/OpenRA.Renderer.Gl/Texture.cs @@ -19,16 +19,22 @@ namespace OpenRA.Renderer.Glsl { public class Texture : ITexture { - internal int texture; + internal int texture; + internal int memory; + GraphicsDevice dev; public Texture(GraphicsDevice dev) - { + { + this.dev = dev; + Gl.glGenTextures(1, out texture); GraphicsDevice.CheckGlError(); } public Texture(GraphicsDevice dev, Bitmap bitmap) - { + { + this.dev = dev; + Gl.glGenTextures(1, out texture); GraphicsDevice.CheckGlError(); SetData(bitmap); @@ -48,6 +54,18 @@ namespace OpenRA.Renderer.Glsl GraphicsDevice.CheckGlError(); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAX_LEVEL, 0); GraphicsDevice.CheckGlError(); + } + + void UpdateMemoryUsage( int newSize ) + { + if (newSize == memory) + return; + + dev.GpuMemoryUsed -= memory; + memory = newSize; + dev.GpuMemoryUsed += memory; + + Log.Write("debug", "GPU Memory: {0:F2} MiB", dev.GpuMemoryUsed / 1024 / 1024f); } public void SetData(byte[] colors, int width, int height) @@ -65,7 +83,9 @@ namespace OpenRA.Renderer.Glsl 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, intPtr); GraphicsDevice.CheckGlError(); } - } + } + + UpdateMemoryUsage(colors.Length); } // An array of RGBA @@ -87,7 +107,9 @@ namespace OpenRA.Renderer.Glsl 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, intPtr); GraphicsDevice.CheckGlError(); } - } + } + + UpdateMemoryUsage(width * height * sizeof(uint)); } public void SetData(Bitmap bitmap) @@ -107,7 +129,9 @@ namespace OpenRA.Renderer.Glsl Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA8, bits.Width, bits.Height, 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, bits.Scan0); // todo: weird strides GraphicsDevice.CheckGlError(); - bitmap.UnlockBits(bits); + bitmap.UnlockBits(bits); + + UpdateMemoryUsage(bitmap.Width * bitmap.Height * sizeof(uint)); } bool IsPowerOf2(int v) diff --git a/OpenRA.Renderer.Null/NullGraphicsDevice.cs b/OpenRA.Renderer.Null/NullGraphicsDevice.cs index ab462a7be1..3665ef9739 100644 --- a/OpenRA.Renderer.Null/NullGraphicsDevice.cs +++ b/OpenRA.Renderer.Null/NullGraphicsDevice.cs @@ -46,7 +46,9 @@ namespace OpenRA.Renderer.Null public IIndexBuffer CreateIndexBuffer(int size) { return new NullIndexBuffer(); } public ITexture CreateTexture() { return new NullTexture(); } public ITexture CreateTexture(Bitmap bitmap) { return new NullTexture(); } - public IShader CreateShader(string name) { return new NullShader(); } + public IShader CreateShader(string name) { return new NullShader(); } + + public int GpuMemoryUsed { get { return 0; } } } public class NullIndexBuffer : IIndexBuffer