imageworks/OpenShadingLanguage — pull request #381 by lgritz (2014-06-19 20:53:35+00:00)
Title: Serialize/deserialize shader groups

I'll introduce this set of changes by illustrating the "killer app":

$ myrenderer myscene.scene
...weird shader behavior or an error...

$ OSL_OPTIONS="archive_groupname=mygroup" myrenderer myscene.scene
$ ls mygroup*
mygroup.tar.gz
$ tar tf mygroup.tar.gz
shadergroup
a.oso
b.oso

This tar file contains a serialized description (in "shadergroup") of the shader group network -- shaders, instance parameter values, connections -- and the oso of every shader referenced by the group. These can be reconstituted to debug the shader from testshade or any other debugging-conducive environment, which does not have to be the original renderer.

This involves three parts:

Part 1:

Added a new variety of ShaderGroupBegin() that accepts a string containing "commands" that easily and human-readably describe the entire setup of a ShaderGroup, equivalent to a set of Parameter, Shader, and ConnectShaders calls. Here is an example:

ShaderGroupBegin ("groupname", "surface",
/*concatenated string follows: */
"param float fin 3.14; "
"param float val 42.0 [[int lockgeom=0]]; "
"shader a alayer;"
"shader b blayer;"
"connect alayer.f_out blayer.f_in;");

Added --group command to testshade so this can be tested and used from the
command line. Here's an example:

testshade -v -group "param float fin 3.14, shader a alayer, shader b blayer, connect alayer.f_out blayer.f_in"

(Commas are an allowed substitute for semicolons for statement separators, and the last statement separator is optional if it's at the end of the string; this makes it easier for command line use, where semicolons have special meaning in some contexts like our test scripts.)

Part 2:

The mirror image of this, where we can serialize (turn into a command string) an existing ShaderGroup. This is done by retrieving the group attribute "pickle":

std::string groupstring;
shadingsys->getattribute (groupptr, "pickle", groupstring);
std::cout << groupstring << "\n";

will print

param float fin 3.14 ;
param float val 42.0 [[int lockgeom=0]] ;
shader a alayer ;
shader b blayer ;
connect alayer.f_out blayer.f_in ;

Part 3:

shadingsys->archive_shadergroup(group,filename) will create an archive file (tar, tar.gz, or zip, depending on the file extension) containing a serialized description of the shader group as well as oso files for every shader referenced in the group.

Also, new ShadingSystem attributes "archive_groupname" and "archive_filename" will cause the named group to be automatically archived to the given filename, right after it is set up.