tags:

views:

79

answers:

3

Hi,

I'm trying to write an editor overtop a multi-threaded game engine. In theory, through the editor, the contents of the scene can be totally changed but I haven't been able to come up with a good way for the engine to cope with the changes. (ie delete an entity while the renderer was drawing it). Additionally, I'm hesitant to write code to manage locks at every instance I use an entity or resource that can be potentially deleted. I imagine there has to be a relatively more elegant solution.

Does anyone have any ideas or strategies I can start looking at?

Thanks!

+1  A: 

If memory is not a problem, you can could have a two-stage process, where the changes are done in one model, and then a snapshot is taken for the renderer, this way the renderer would always see a consistent view of the model.

Lasse V. Karlsen
I agree, that way you only need to lock at one place, the "view".
Groo
Although it depends on how much memory it would take, and the other thing is that you need to have snapshots ready all the time, instead of just passing the data when requested.
Groo
Oh, yes, it's not a good solution, but it's *A* solution :) I rather like the pipe structure that Jorge has posted though.
Lasse V. Karlsen
+1  A: 

In addition to the two-stage process suggested by @lassevk you could use a Pipe structure to "push" commands to the renderer so that these changes gets the form of another work item for the render engine.

For example, say your engine follows a workflow like:

  1. Calculate positions
  2. Process Physics
  3. Process Lights Process Cameras
  4. Render Scene

You could just add a new item to the workflow in the position 0, called Process Changes which pulls out the information from the Pipe and incorporates it to the scene.

Jorge Córdoba
So if I understand correctly, the entity manager would send update messages to the 3dengine (or any number of "observers", I suppose). The 3d engine would have a "parallel" version of the entity manager and would use that instead of the main one.
Steve the Plant
No, no "parallel" versions, just a simple step wich takes the updates messages an update the main scene representation. For example, let's say yo change the texture of a given entity from the editor. The editor sends the update, then the "update manager" just process the commands into the main entity pool. That could be implemented as a Command Pattern so that you can be able to process different kind of updates.
Jorge Córdoba
I like this idea, so I'll mark it as the answer.
Steve the Plant
A: 

Without knowing your exact need, it is hard for me to comment on the details, but i think if a combination of copy-on-write and lock-free data structure may help.

When you're rendering, you need read only access, so there are no problems. When you're editing, say deleting an object from a group of objects, you mark the current object "deleted" using atomic operations and your renderer can skip or remove the object before the next render loop. When an object is changed, you make a copy and change the copy and "commit" the change using lock-free techniques.

Shing Yip