CreatePreview - How does it work?

Hello,

I can not figure out how Render plugin CreatePreview() should work.

Ok, I understand the basic concept, but how can is use it asynchronously?

I use progressive render type
protected override PreviewRenderTypes PreviewRenderType()
{
return PreviewRenderTypes.Progressive;
}

inside CreatePreview I handle only Quality.RefineThirdPass, and I use scene.PreviewNotifier.NotifyIntermediateUpdate(RenderWindow);
to update the preview.

I can do it fine if it is in the same thread as CreatePreview. But if I open a new Thread for preview calculations I can not call NotifyIntermediateUpdate from there because it triggers an exception. I guess after I return from CreatePreview the previewnotifier is not really valid anymore so my parallel calculation can not use it.
But what is the correct process?

Márton

CreatePreview is already called on a thread. There is no need to create a separate thread for your engine in that sense, as it is already non-blocking.

This is the Plugin.cs for the RhinoRenderCycles.rhp plug-in:

At the bottom you can see how PreviewRenderEngine is initialized, then its Renderer function is directly called. It can ‘block’ here, as CreatePreview already runs in a background thread created by the preview render job queue from the RDK. At the end you get your render result as a bitmap and set it on the scene parameter passed into CreatePreview.

Reason: accessing the preview notifier should happen on the same thread it was created.

Thanks Nathan,

This is actually what I was expecting (that CreatePreview runs in a parallel thread).

But in that case something else is wrong on my side, because my calculation blocks the Rhino UI fully.

If it is already on another Thread this (silly test code) shouldn’t block Rhino completely right?
But it blocks Rhino…

scene.SkipInitialisation();
if (scene.Quality == PreviewSceneQuality.Low)
{
scene.PreviewImage = null;
return;
}

while (0 == 0)
{
System.Threading.Thread.Sleep(10);
}

It shouldn’t block. At least in Rhino WIP with Rhino Render set as default renderer (which is Raytraced/Cycles) this works just fine. AFAICR this preview creation code hasn’t fundamentally changed from Rhino 6. While previews are rendering I can use Rhino just fine.

Maybe test with a sleep of say 10 seconds? Or does that block as well for you?

Yes System.Threading.Thread.Sleep(10000); fully blocks it.

Shouldn’t I trigger something?

Márton

Ohhh sorry…

I let it cook for a few days and after I returned back I discovered the locking happened because of my older code which still interfered with my new code…

So now I see the async effect:)

1 Like