What is the proper way to compile OSL shaders in different threads at the same time?

Skip to first unread message

Jiayin Cao

Apr 2, 2020, 1:27:52 PM4/2/20
to OSL Developers

I'm wondering if there is a way to compile thread in muli-threads.


Larry Gritz

Apr 4, 2020, 9:51:08 PM4/4/20
to OSL Developers List
There is no trick to it, the ShadingSystem is thread-safe for shader execution.

If you are buliding from master, then for declarations of shaders (i.e., ShadingGroupBegin, Shader, ConnectShaders, Parameter, ShaderGroupEnd), just be careful to use the new, thread-safe versions, not the old ones that are not thread safe. Read the oslexec.h header, it's pretty clear about which are which.

If you are using 1.10 or older, then the declaring of shaders is not thread-safe, it only has the old versions. So you want to declare your shader groups from one thread (or at least have a mutex to make sure two threads aren't declaring different shaders at the same time).

In both master and release branches, though, the execution of shaders is already totally thread-safe.

Basically, you just call shadingsys->execute(ctx, group, globals). The ctx is a pointer to a ShadingContext (which you acquire via get_context(), and is thread-specific). Don't share a context between different threads -- have each thread get its own context and then keep using it.

That's all there is to it.

Both the testshade and testrender programs that we use extensively in the unit tests are multithreaded, so you can also look at those for examples. 

You received this message because you are subscribed to the Google Groups "OSL Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osl-dev+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/osl-dev/382771d2-7f52-4672-b011-51220e9f9820%40googlegroups.com.

Larry Gritz

Reply all
Reply to author
0 new messages