Sign in to follow this  
dave

Double Buffering A SceneGraph

Recommended Posts

dave    2187
I'm trying to find a good solution to the following problem. * I have a scenegraph which sits on the same thread as the renderer. Everything is fairly light weight at the moment so i've not needed to divide those two up. This thread updates the scenegraph and renders it sequentially. There should never be any issues here, there havn't been so far. * I also have an editor that while this is all happening is capable of adding nodes, changing properties of nodes in the scenegraph and adding or removing resources from the renderer. It does this by issuing commands to either the renderer or scenegraph and these are cached in a lock-free queue and executed at a deferred time. This time is after the thread that updates the SG and renders it is done doing those things. This all seems to work. A problem has arisen recently though when implementing a new feature in the editor which needs to be able to read the current position of a node from the scenegraph. So far this functionality hasn't been needed anywhere. It occurs when dragging a model in the scene around. When i drag a model i add a command to the scenegraph to update that models position. This is ofcourse deferred to a later time. The editor requests the models position from the editors GUI thread and at present this is implemented by calling the GetWorldTransform() method on the node in question. This reverses up the graph accumulating the local positions into the world one. The problem is occasionally it fails to find the correct transform (even though it is actually there) and if i move the current line up to the start of the code and step through it succeeds. It looks to me like the position is being got whilst it's being updated by a MoveNode command and therefore i get some strange results. Presently there is alot of potential for this kind of problem to occur. * So any suggestions about how to solve this kind of scenario? * Do i double buffer the properties of the scenegraph nodes so i am always reading from the one being rendered and updating the one that isn't being rendered? * If this is the correct approach, do i double buffer the child nodes (this will double buffer all the children again and explode the data stored for the SG). Thanks,

Share this post


Link to post
Share on other sites
ApochPiQ    23003
If you're accessing the scenegraph from more than one thread, you almost definitely need to be locking on each access, both read and write. It sounds like you're running into cases where a write operation isn't finished when the read is executed.

While you can solve this by double buffering the scenegraph, that gets expensive very quickly, especially if your nodes have non-trivial deep copy semantics. A better approach would be to implement a multiple-read, single-write lock that allows your external threads to read from the scenegraph as desired, while also locking correctly when a write operation is being executed. This approach works quite well, and if you can arrange your work to be done in batches, the overhead of the locks can be almost completely erased by load-balancing across frames.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this