SlimDX Application Design Question (Threading issues and how to avoid them?)$$REWARD$$

Started by
5 comments, last by vanattab 11 years, 6 months ago
NOTE: For technical reasons I need to target DX9 (not 10/11), I am offering $25 total via PayPal to any poster(s) who provide help leading to a resolution of this issue. I know that's not much but I am very poor and hopefully that is at least enough incentive to read my ridiculously long post.

I am having some trouble with figuring out how I should structure my program. I will first describe the general description of my program and then go into the things I have tried and the issues I have run into. I am not developing a game but rather a suite of computerized tests to test human vision. I am writing the program in VB.Net but converting between .Net languages is relativity trivial so if your going to post some code don't go out of your way to post it as VB if your better with C#. Even examples in other languages would be appreciated. I know the post is a little long but I want to be as clear as possible about the issues I am having. Thanks in advance!


Design: The program lunches a “Main Menu” VB.Net form that has lots of options to select different types of test and configure different options for the tests. When the user clicks the RunTest button for a specific test a TestObject representing that test is created. The TestObject constructor creates a StimulusDisplayObject(SDO) which is derived from the SlimDX RenderForm class. The job of the SDO class is to manage the SlimDX device object and render the different stimuli to the screen. It contains a list of StimulusObjects(SO) which are essentially just a DX surface that was loaded from an .bmp image and some location information that to tell the SDO class where to draw the surface on the screen. After the SDO is created the TestObject.BeginTest() method is called that is just a loop that presents a number of trials to the observer. The loop creates some SO objects and adds them to the list of SO objects of the SDO class to be displayed.

Problems: Because the stimuli I preset for the tests is fairly static and I usually only had to change the image that was displayed to the screen once every sec or two at the most, I did not have the scene rendering continually at as high of an FPS as possible. I simply had a SDO.RenderScene() that would get called from the TestObject whenever something need to be changed on the screen. This worked pretty well until I tried to handle DeviceLostExceptions and similar issues I arise from when the user ALT-TABs, CTRL-ALT-Delete, Covers the window (when in windowed mode), etc..) I have not found an effective way to handle these types of errors without having the scene continually rendering.

So I decided to try rewriting the SDO class to use SlimDXs MessagePump.Run() functionality to render the scene constantly, however this causes exceptions because The MessagePump.Run() conflicts with the MessagePump of the “Main Menu” Form (At least I think that is what this error message means: “Additional information: Starting a second message loop on a single thread is not a valid operation.”). I decided to try creating the SDO object in a new thread but then I can't communicate between the the TestObject and the SDO Object to add/remove/change the SO objects. I am not 100% sure that creating a multithreaded application is the best approach because I have read that DX and Threading leads to programing nightmares but I don't see any other solutions. I think I need to learn to use some kind of thread synchronization but I am very very new to threading and I want to make sure I am not heading down a road that is doomed to fail before I invest a long period of time learning to synchronized threads. If creating a multithreaded application is the best choice is there any DX/SlimDX threading issues I should be aware of? Any examples of simple SlimDX threaded programs that synchronize data across threads? Thanks for taking the time to read this post and I will extremely grateful to any advice you can give.
Advertisement
Is the main menu continuing to run while the test takes place? You could potentially include the update and/or render call in the main menu's messagePump, wrapped in a boolean check to see if "test_is_running" is true?

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Yes it is. At least I think it is. When I am running the test one monitor and have the mainMenu form opened on the other monitor the mainMenu buttons and textboxes are still responsive. How would I go about adding the update call to the forms messagePump.
Wait, is the main menu another SlimDX Form, or just a regular winform?

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Its a regular winform.
There's nothing inherently wrong with only rendering when you need to. The reason games do it is because there are typical objects animating and camera movement constantly, so the changes have to be rendered anyway. If you give more information on what was going wrong with device lost scenarios, it might be a better approach to try to fix those and continue going the way you were before.

If you want to render continuously though, that's also viable. The idea is that you *replace* the Application.Run() method with MessagePump.Run(). You can run as many forms as you want on the same message loop. MessagePump.Run() internally hooks the application "Idle" event and constantly calls your provided callback whenever there are no window messages in the queue. You can put your rendering code inside that loop, and then you can add objects to be rendered to some shared list whenever you need to run one of your tests.
Mike Popoloski | Journal | SlimDX

There's nothing inherently wrong with only rendering when you need to. The reason games do it is because there are typical objects animating and camera movement constantly, so the changes have to be rendered anyway. If you give more information on what was going wrong with device lost scenarios, it might be a better approach to try to fix those and continue going the way you were before.

If you want to render continuously though, that's also viable. The idea is that you *replace* the Application.Run() method with MessagePump.Run(). You can run as many forms as you want on the same message loop. MessagePump.Run() internally hooks the application "Idle" event and constantly calls your provided callback whenever there are no window messages in the queue. You can put your rendering code inside that loop, and then you can add objects to be rendered to some shared list whenever you need to run one of your tests.


Thanks for the help Mike. I am working on rewritting some of the code now. I was haveing trouble trying to handle the DeviceLostExceptions when not rendering on demand. I tried to have some render code that looked something like:

[source lang="vb"]Sub RenderScene()
...Some Code
Try
device.present()
Catch e as devLostEx
DevLost = true
recover()
RenderScene()
End Try
End Sub

Sub recover()
While DevLost
Try
device.testCoop
Catch DevLostEx
'Do nothing
Catch DevNotReset
...Dispose defult pool stuff
device.reset()
recreate defult pool stuff
DevLost = false
End Try
End While
End Sub[/source]
That was with MDX9 not SlimDX. It just seemed to hang and never recover. I am not sure why. Anyway I have got a SlimDX basic class rendering contiually and handleing device lost exceptions fine. I am running it in its own thread and am just using synclock to protect data when I pass new objects to the display. I think the code would be a little slow if I need to render really fast because I synclock most of the SlimDX code but I don't think it will be a big issue for me. I am however running in to some trouble. I can't find a SlimDX replacment for the MDX9 Surface.FromImage() method. Also someone said that the DeviceEx class as some better features for passing data between threads so should I be using that class? Thanks for any help you can provide.

This topic is closed to new replies.

Advertisement