Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Video Memory Problem

Started by MrM Oct 27, 2005 at 11:34 AM 1 replies 400+ views
Original Post
MrM
MrM
Hi! i have a problem running out of video memory! I'm just working in 2d and have at max 10 textures that are drawn.. so o don't really get what happens! here is how i initialize the buttons: I use simple render to texture to get a text on my button.

		public Button(string Resource, int x, int y, string text, int TextSize, Device device): base(Resource, x, y, device) {
		
		
			m_Device = device;
			m_x = x;
			m_y = y;

			ImageInformation info = TextureLoader.ImageInformationFromStream(GetType().Module.Assembly.GetManifestResourceStream(Resource));
			m_height = info.Height;
			m_width = info.Width;
			texture = TextureLoader.FromStream(m_Device, GetType().Module.Assembly.GetManifestResourceStream(Resource));	

			BackBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, m_Device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);

			verts = (CustomVertex.PositionTextured[])BackBuffer.Lock(0, LockFlags.None);
			verts[2] = new CustomVertex.PositionTextured(0, 0, 1, 0, 1); // Top Left
			verts[3] = new CustomVertex.PositionTextured(0, m_height, 1, 0, 0); // Top Right
			verts[0] = new CustomVertex.PositionTextured(m_width, 0, 1, 1, 1); // Bottom Left
			verts[1] = new CustomVertex.PositionTextured(m_width, m_height, 1, 1, 0); // Bottom Right
			BackBuffer.Unlock();

			GraphicBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, m_Device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);

			verts = (CustomVertex.PositionTextured[])GraphicBuffer.Lock(0, LockFlags.None);
			verts[0] = new CustomVertex.PositionTextured(0, 0, 1, 0, 1); // Top Left
			verts[1] = new CustomVertex.PositionTextured(0, m_height, 1, 0, 0); // Top Right
			verts[2] = new CustomVertex.PositionTextured(m_width, 0, 1, 1, 1); // Bottom Left
			verts[3] = new CustomVertex.PositionTextured(m_width, m_height, 1, 1, 0); // Bottom Right
			GraphicBuffer.Unlock();
		
			VertexBuffer TempBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, m_Device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);

			verts = (CustomVertex.PositionTextured[])TempBuffer.Lock(0, LockFlags.None);
			verts[0] = new CustomVertex.PositionTextured(0, 0, 1, 0, 1); // Top Left
			verts[1] = new CustomVertex.PositionTextured(0, 600, 1, 0, 0); // Top Right
			verts[2] = new CustomVertex.PositionTextured(800, 0, 1, 1, 1); // Bottom Left
			verts[3] = new CustomVertex.PositionTextured(800, 600, 1, 1, 0); // Bottom Right
			TempBuffer.Unlock();

		
			testText = new Microsoft.DirectX.Direct3D.Font(m_Device, new System.Drawing.Font("Tahoma", TextSize));
			testtex = new Texture(m_Device, m_width, m_height, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
			Surface surface = testtex.GetSurfaceLevel(0);

			Viewport view = new Viewport();
			view.Width = m_width;
			view.Height = m_height;
			view.MaxZ = 1.0f;
			//Save the current render target so that we can reset it after use.
			Surface oldRenderTarget = m_Device.GetRenderTarget(0);
			//Set the render target to the surface we created
			m_Device.SetRenderTarget(0, surface);
			//Clear the render target (our surface and our depth buffer of the surface)
			m_Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black.ToArgb(), 1.0f, 0);
			m_Device.BeginScene();
			//Set the material and the world transform and render the cube
			m_Device.RenderState.SourceBlend = Blend.SourceAlpha;
			m_Device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
			m_Device.RenderState.AlphaBlendEnable = true;

			m_Device.SetTexture(0, texture);
			m_Device.VertexFormat=CustomVertex.PositionTextured.Format;
			m_Device.SetStreamSource(0, TempBuffer, 0);
			m_Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
			m_Device.Transform.World = Matrix.Identity;
			Rectangle TextRect = testText.MeasureString(null, text, DrawTextFormat.None, Color.Black);
			testText.DrawText(null, text, m_width / 2 - TextRect.Width / 2, m_height / 2 - TextRect.Height / 2, Color.Black);
			//testText.DrawText(null, "huh", m_width / 2 - TextRect.Width / 2, m_height / 2 - TextRect.Height / 2, Color.Red);

			m_Device.EndScene();
			//Reset the render target.
			m_Device.SetRenderTarget(0, oldRenderTarget);
		}

and here comes the rendering:

		public override void Paint()
		{
			//m_Device.SetTexture(0,this.texture);
			
			m_Device.SetTexture(0, testtex);
			
			m_Device.RenderState.SourceBlend = Blend.SourceAlpha;
			m_Device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
			m_Device.RenderState.AlphaBlendEnable = true;

			m_Device.VertexFormat=CustomVertex.PositionTextured.Format;
			m_Device.SetStreamSource(0, GraphicBuffer, 0);
			m_Device.Transform.World = Matrix.Translation(- m_width / 2, -m_height / 2, -1) * Matrix.RotationY(m_alphaY) * Matrix.Translation(m_width / 2, m_height / 2, 1) * Matrix.AffineTransformation2D(1,new Vector2(m_width / 2, m_height / 2), m_alphaZ, new Vector2(m_x, m_y));
			m_Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
			m_Device.SetStreamSource(0, BackBuffer, 0);
			m_Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
			//testText.DrawText(null, text, 100, 100, Color.Red);
			m_Device.Transform.World = Matrix.Identity;
			
		}

and here i draw them:


		private void Render()
		{
			m_Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Black, 1.0f, 0);
			m_Device.BeginScene();

                        Button[0].Paint();
			m_Device.EndScene();
			m_Device.Present();
}

i often redraw the textures of the buttons that means i use the same vaiable Button[] but i ovveride it by using Button[0] = new Button(..);. always after redrawing the textures the video memory drops until i run out of it... ? what memory do i forget to free? Greets m [Edited by - Coder on October 27, 2005 2:07:25 PM]
Armadon
Armadon
I will try my best as to give you tips of reducing the amount of loading you are doing.

#1 you could use 1 vertex buffer for 10 buttons as they all essentially have the size size and even if they don't they are all rectangles which could classify them as being the same...

Setting up the button vertex buffer
CustomVertex.PositionTextured[] button = new CustomVertex.PositionTextured[4];button [0] = new CustomVertex.PositionTextured(-1.0f, 0.0f, 0.0f, 0.0f, 1.0f);button [1] = new CustomVertex.PositionTextured(-1.0f, 2.0f, 0.0f, 0.0f, 0.0f);button [2] = new CustomVertex.PositionTextured( 1.0f, 0.0f, 0.0f, 1.0f, 1.0f);button [3] = new CustomVertex.PositionTextured( 1.0f, 2.0f, 0.0f, 1.0f, 0.0f);vb = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, renderer.Device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Managed);vb.SetData(button , 0, LockFlags.None);//Loading the button's texturetexButton1 = TextureLoader.FromFile(renderer.Device, "button1.jpg");texButton2 = TextureLoader.FromFile(renderer.Device, "button2.jpg");texButton3 = TextureLoader.FromFile(renderer.Device, "button3.jpg");


Essentially you shouldn't have a problem with that and should be fine for you to use.. Try and avoid having to render to texture just to get text ontop of buttons as you could do that with either placing the text in the texture or just using the Font class provided in Direct3d.

I hope this helps.
Take care.
kovacsp
kovacsp
Hi,

I might be wrong, but I suppose that 10 buttons shouldn't take so much video memory that you run out of it. You must be leaking memory somewhere..
Use the debug runtime, which will give you information on the leaked memory blocks. Also, try to pair allocations and Release()-s by hand, and if everything else fails, try commenting out blocks of code and see if leaking disappears (you can do that until your code is not that big, fortunately)..

Sorry if I'm on the wrong way and you are not leaking vidmem, but this was my first thought..

Hope you'll find it soon,
kp
------------------------------------------------------------Neo, the Matrix should be 16-byte aligned for better performance!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.