OSL general questions

47 views
Skip to first unread message

haggi

unread,
Jul 21, 2015, 4:30:14 AM7/21/15
to apples...@googlegroups.com
Until now I only created shaders in OSL, no surface shaders so I'm not very familiar with the closure concept.
e.g. I want to implement the maya lambert shader for testing. Normally I would do the following in pseudocode:

color shader (color Color, color incandescence, color transparency, float diffval)
{
    color diff
= Color * diffuse() * diffval;
   
float transp = grayscale(transparency);
   
return (diff * (1.0 - transp)) + transparent() +  incandescence;
}


This will not work with osl because of the usage of closures. If I understand it correctly then transparent(), diffuse() are closures wich can be multiplied but not added. So e.g. how can I implemnt the example shader above in OSL?



Est

unread,
Jul 21, 2015, 8:48:23 AM7/21/15
to apples...@googlegroups.com, haggi...@gmail.com
Untested:

as a surface shader:

surface maya_lambert(color Color, color ...)
{
    Ci = (Color * diffval * diffuse(N)) + (incandescence * emission()) + (transparency * transparent());
}

as a shader:

shader maya_lambert(color Color, ..., ..., output closure color Cout)
{
    Cout = (Color * diffval * diffuse(N)) + (incandescence * emission()) + (transparency * transparent());
}

in this second case, you need a final surface shader that writes the result to Ci

surface my_result(closure color Cinput)
{
   Ci = Cinput;
}

and connect Cout to Cinput.

Est.

haggi

unread,
Jul 25, 2015, 9:08:06 AM7/25/15
to appleseed-dev, rame...@gmail.com
Great! Works.

Can you tell me if there is a way to get the current shading network as a string for debug purposes?
In my other project I did it with:

std::string serialized;
oslRenderer
->shadingsys->getattribute(shaderGroup.get(), "pickle", serialized);



what was really helpful for debugging.

haggi

unread,
Jul 25, 2015, 9:45:07 AM7/25/15
to appleseed-dev, rame...@gmail.com, haggi...@gmail.com
It seems that I'm doing something wrong if I try to assign paramaters to my shaders because I get these errors:
2015-07-25T13:37:26.666081Z <001>   543 MB error   | error parsing osl param value, param = offsetU, value = 0; will use the default value.
2015-07-25T13:37:26.666081Z <001>   543 MB error   | error parsing osl param value, param = offsetV, value = 0; will use the default value.
2015-07-25T13:37:26.667081Z <001>   543 MB error   | error parsing osl param value, param = repeatU, value = 1; will use the default value.
2015-07-25T13:37:26.667081Z <001>   543 MB error   | error parsing osl param value, param = repeatV, value = 1; will use the default value.
2015-07-25T13:37:26.667081Z <001>   543 MB error   | error parsing osl param value, param = rotateUV, value = 0; will use the default value.
2015-07-25T13:37:26.668081Z <001>   543 MB error   | error parsing osl param value, param = uvCoord, value = 00000000571CDB58; will use the default value.
2015-07-25T13:37:26.668081Z <001>   543 MB debug   | created shader place2dTexture, layer = place2dTexture1.


The place2dTexture is the shader name and the inputs are defined as follows:

        float vec[3];
       
float m[4][4];
       
float fv = 0.0f;
       
int intv = 0;
        std
::string sv;
       
if (param.type == OSL::TypeDesc::TypeFloat)
       
{
            fv
= boost::get<float>(param.value);
           
Logging::debug(MString("MAYATO_OSL::createOSLShader creating param ") + pname + " : " + fv);
            asParamArray
.insert(pname.asChar(), fv);
       
}
       
if (param.type == OSL::TypeDesc::TypeInt)
       
{
            intv
= boost::get<int>(param.value);
            asParamArray
.insert(pname.asChar(), intv);
       
}
       
if (param.type == OSL::TypeDesc::TypeVector)
       
{
            MAYATO_OSL
::SimpleVector &v = boost::get<MAYATO_OSL::SimpleVector>(param.value);
            vec
[0] = v.f[0];
            vec
[1] = v.f[1];
            vec
[2] = v.f[2];
            asParamArray
.insert(pname.asChar(), vec);
       
}
       
if (param.type == OSL::TypeDesc::TypeString)
       
{
            sv
= boost::get<std::string>(param.value);
            asParamArray
.insert(pname.asChar(), sv);
       
}


Any ideas?

Esteban Tovagliari

unread,
Jul 26, 2015, 8:40:07 AM7/26/15
to apples...@googlegroups.com, haggi krey
On 25 July 2015 at 16:05, Esteban Tovagliari <rame...@gmail.com> wrote:
Hi Haggi,

About your first question, it's not possible now. Could you create a new issue for it?

Actually, there's something you can try:

in ShaderGroup:

    // Create OSL shader group.
    bool create_optimized_osl_shader_group(
        OSL::ShadingSystem&         shading_system,
        foundation::IAbortSwitch*   abort_switch = 0);

    // Release internal OSL shader group.
    void release_optimized_osl_shader_group();

are both public. Can you try creating an OSL ShadingSystem (you can probably pass null to all constructor params if you are not going to do any rendering).
Then with your shading system, you can call  create_optimized_osl_shader_group (pass null for the abort switch). After that, I think you can call methods on your shading system, including serializing shader groups to strings.
Do not forget to call release_optimized_osl_shader_group() before destroying your shading system.

Use this only for debugging. There will be some changes and it's very likely that these methods will become private in the future.

Regarding the error specifying shaders, I think you are missing the param type.

param_array.insert("param_name_1", "float 0.7");
param_array.insert("param_name_2", "int 4");
param_array.insert("string_param_name", "string something");

for multi-value params (colors, vectors, ...) use spaces as the separator between values:

param_array.insert("color_param_name", "color 0.2 0.5 0.7);

you can see the supported param types here:

if you need a type that's not supported, open an issue and I'll add it.

I made a PR for matrix params:

Est.

haggi

unread,
Jul 26, 2015, 10:15:57 AM7/26/15
to appleseed-dev, rame...@gmail.com

This is the first image I created with complete OSL background and OSL shaders. Nothing special, but now I can implement a whole bunch of features. Will be fun!!!




Esteban Tovagliari

unread,
Jul 26, 2015, 1:02:32 PM7/26/15
to haggi, appleseed-dev
Hi,

That's great!

Please, post screenshots from time to time with your progress.

Btw, have you considered switching the build system to CMake?
It will make it easier to build OpenMaya for people not using Windows 
and maybe contribute too.

​Est.

haggi

unread,
Jul 26, 2015, 4:59:28 PM7/26/15
to appleseed-dev, rame...@gmail.com
Not yet. I did not find the time to dig deeper into the cmake build process. But you are right, I should try it as soon as possible.

François Beaune

unread,
Aug 6, 2015, 2:53:10 AM8/6/15
to apples...@googlegroups.com, Esteban Tovagliari
Very cool. I second Esteban request about WIP screenshots!

Franz

On Sun, Jul 26, 2015 at 4:15 PM, haggi <haggi...@gmail.com> wrote:

This is the first image I created with complete OSL background and OSL shaders. Nothing special, but now I can implement a whole bunch of features. Will be fun!!!




--
You received this message because you are subscribed to the Google Groups "appleseed-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to appleseed-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages