DirectX Freezes During Resize

Started by
5 comments, last by andy1 17 years, 10 months ago
My program, created with C# and DirectX 9, has a GUI based on forms (created with DirectX). When the user opens a modal form, the background is copied to a texture and then painted under the form to increase speed when many modal forms are open at the same time. Then, when the user closes the form, the texture is disposed because it is no longer needed. Here is the problem: When I call the Dispose method on the texture, my program usually freezes for several seconds to minutes. Fortunately, I can work around this by disposing the texture in a new thread. However, when I resize the main window, my program freezes similarly, even when I do not manually dispose textures. It is interesting to note that during the resize process, my program freezes only after I have opened and closed several modal forms. Once I begin resizing the main form and my program has finished the initial freezing, I can continue to resize normally until I open and close more modal forms. I have three methods, OnDeviceResizing, OnDeviceLost, and OnDeviceReset, that handle the deivce events. I placed breakpoints at the first and last lines of each method. During the resize process, execution enters OnDeviceResizing, exits, enters OnDeviceLost, exits, and then freezes. When I break while the program is frozen, I see this in the Call Stack: System.MulticastDelegate::RemoveImpl(System.Delegate value = {System.EventHandler}) + 0xfa bytes System.Delegate::Remove(System.Delegate source = {System.EventHandler}, System.Delegate value = {System.EventHandler}) + 0x48 bytes Microsoft.DirectX.Direct3D.Device::remove_Disposing(System.EventHandler eh = {System.EventHandler}) + 0x22 bytes Microsoft.DirectX.Direct3D.Surface::Dispose() + 0x101 bytes Microsoft.DirectX.Direct3D.Surface::OnParentLost(System.Object sender = {Microsoft.DirectX.Direct3D.Device}, System.EventArgs e = {System.EventArgs}) + 0x16 bytes Microsoft.DirectX.Direct3D.Device::raise_DeviceLost(System.Object i1 = {Microsoft.DirectX.Direct3D.Device}, System.EventArgs i2 = {System.EventArgs}) + 0x2b bytes Microsoft.DirectX.Direct3D.Device::Reset(Microsoft.DirectX.Direct3D.PresentParameters[] presentationParameters = {Length=1}) + 0x9a bytes Microsoft.DirectX.Direct3D.Device::OnParentResized(System.Object sender = {Power2D.Canvas}, System.EventArgs e = {System.EventArgs}) + 0x16c bytes System.Windows.Forms.Control::OnResize(System.EventArgs e = {System.EventArgs}) + 0xa1 bytes ... Any ideas on how to solve this? This problem has been bugging me for a long time.
—————————————————————————Platform Studio - The Ultimate Platform Game Creator. A free download.
Advertisement
Hmm. It sounds like you are doing things pretty straighfoward (besides trying to get around it by throwing it into another thread), so perhaps it could be something system-oriented. Have you tried:

- The application on a different machine?
- Updating D3D and MDX components?
- Updating drivers?

If it does the same on a different machine, chances are you made a boo-boo at some point, and we can go from there.
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )
Thanks for your reply. I tested it on two other machines and they both have the same problem.
—————————————————————————Platform Studio - The Ultimate Platform Game Creator. A free download.
Any suggestions? When I comment out my code for storing and destroying the background, the delays are eliminated.

This is the code for storing, drawing, and disposing the backbuffer as a texture:

private Stack backBuffTex = new Stack();internal void StoreBackbuffer(){	// Texture sizes must be powers of 2	int tmpW = TextureSprite.expandToPowerOf2(canvas.Width);	int tmpH = TextureSprite.expandToPowerOf2(canvas.Height);	// (Copies the backbuffer to a new texture):	backBuffTex.Push(new D3D.Texture(		dev, tmpW, tmpH, 0, Usage.RenderTarget,		dev.PresentationParameters.BackBufferFormat,		Pool.Default));	dev.StretchRectangle(dev.GetBackBuffer(0, 0, BackBufferType.Mono),		new Rectangle(0, 0, canvas.Width, canvas.Height),		((D3D.Texture)backBuffTex.Peek()).GetSurfaceLevel(0),		new Rectangle(0, 0, canvas.Width, canvas.Height),		TextureFilter.None);}internal void DrawBackbuffer(){	dev.StretchRectangle(((D3D.Texture)backBuffTex.Peek()).GetSurfaceLevel(0),		new Rectangle(0, 0, canvas.Width, canvas.Height),		dev.GetBackBuffer(0, 0, BackBufferType.Mono),		new Rectangle(0, 0, canvas.Width, canvas.Height),		TextureFilter.None);}internal void ReleaseBackbuffer(){	System.Threading.Thread thread = new System.Threading.Thread(		new System.Threading.ThreadStart(disposeTexture));	thread.Start();}private void disposeTexture(){	D3D.Texture tex = (D3D.Texture)backBuffTex.Pop();	tex.Dispose();}
—————————————————————————Platform Studio - The Ultimate Platform Game Creator. A free download.
I am not super-familiar with MDX, but a few suggestions:

(1) In ReleaseBackBuffer(), you start up a new thread, which then modifies D3D resources. Unless you specified the D3DCREATE_MULTITHREADED flag upon device creation, the D3D Device is not thread-safe. It is not recommended to specify this flags, due to severe performance penalties. In short, just keep all of you resources in one thread.

(2) Check the output from the D3D runtime. If you are doing something wrong in a function call or something, it will tell you about it.

(3) Before reseting your D3D device (when the app resizes), you need to release your buffer, since it was created in the default pool. If you follow (2), you should see a message about this.
Dustin Franklin ( circlesoft :: KBase :: Mystic GD :: ApolloNL )
1. The creation of a new thread was only a temporary workaround to part of the problem (fixes delays while closing modal forms, but not while resizing the main form), but I reverted back to single-threading to make sure nothing else would go wrong during debugging.

2. I got a few warnings such as redundant SetRenderStates, etc, but no errors appeared in the output window. However, the texture behind the modal dialog now appears as either solid magenta or green (I think it is random which one it is), which I think indicates a problem of some sort. I must not be creating the backbuffer copy correctly. Any thoughts?

3. I only resize the form when all modal windows are closed, so all temporary textures should have been destroyed.
—————————————————————————Platform Studio - The Ultimate Platform Game Creator. A free download.
(bump) Any thoughts?
—————————————————————————Platform Studio - The Ultimate Platform Game Creator. A free download.

This topic is closed to new replies.

Advertisement