How to sync ChangeQueue and renderer geometry update

I am trying slowly to discover the potential of ChangeQueue concept while I am building a simple custom renderer and after succeeding to have the all the scene geometry in a triangular mesh format I came across to a sync issue.
After initializing my scene, the renderer runs in a while loop loading the rendered buffer into the viewport via pChannel->SetValueRect
After rendering the initial scene I create lots of new instances in Rhino. There, I found out that when I do this the ChangeQueue::ApplyMeshInstanceChanges is called again to calculate and feed the new geometry to the renderer (which runs on the while loop).
In this scenario isn’t there a sync issue? If the newly created geometry is big enough the renderer will not be able to access it consistently, should I use a mutex/cv mechanism to avoid this?

std::unique_lock<std::mutex> lock(_scene->mutex);
				_scene->cv.wait(lock, [that]
					{
						return _scene->vertices_ready;
					});
updateRendererGeometry(triangularMeshVertices);
lock.unlock();
_scene->vertices_ready = true;
_scene->vertices_processed = true;
_scene->cv.notify_one();

For Raytraced the Cycles engine is running in a separate thread. On any change in the document the ChangeQueue will start processing that data, as you have noticed, and when done it’ll give you the notification of that.

Depending on when you push data to your renderer you may either first collect all changes through the flush, then interrupt your renderer, upload data and resume rendering. Alternatively you first interrupt your renderer, flush and upload data directly, then continue.

For Raytraced I first flush the changequeue to gather all data, process it where necessary. Only then I tell Cycles to stop, and when it has done so I upload the new data or delete old stuff if necessary. After that Cycles can start rendering again. For this I have a couple of mutexes in play.

Essentially whatever mechanism that helps you ensure you aren’t accessing data that you’re modifying at the same time is going to be good.

1 Like