blenderraytracingshadingpbr

What exactly are the limits of Open Shading Language (OSL) in terms of new materials/BSDFs?


I'm doing research on BRDF description and implementation techniques, and OSL is one of the main languages to do so. I'm curious about the way one could implement a new BRDF using OSL, or if it's even possible to do so without messing around with its source code.

The OSL documentation gives a set of materials that are to be expected of any renderer. Blender, for examples, provides an an extended set of implemented materials ready to be used in OSL (these materials are actually already available as nodes in Cycles). I'm interested in creating new materials (BRDFs). According to this thread, OSL is not meant to be used like this, instead the users are supposed to make use of the already available material closures (BSDFs) to create new materials. The OP in that thread was trying to implement a BRDF but couldn't progress because he couldn't find a way to obtain certain vectors needed.

Finally, my question is: in order to create new materials (BRDFs) to be used in OSL, is it necessary to implement them first in C++ and recompile OSL, to finally make them usable? I wasn't able to find a definitive answer to this question.


Solution

  • Starting from the Introduction in the OSL spec,

    OSL’s surface and volume shaders compute an explicit symbolic description, called a ”closure”, of the way a surface or volume scatters light, in units of radiance.

    In blender, the cycles nodes with a green output socket are the "closure" nodes, they provide the BRDF/BSDF calculations that give the appearance in the render. Cycles provides the type of closures that are available, OSL cannot implement new closure types.

    So yes, you would have to alter blenders code to offer different closure types, but an OSL script can add plenty of customisation to a cycles node tree without you having to alter blenders code. The OSL script is compiled using LLVM's JIT compiler to run on the current CPU being used. So an OSL script does "inject" new code into the render engine, it is just limited in how it can alter the final result.

    Note that an OSL script does not have to provide a closure output, it may output float or colour values which allows it to provide intermediate nodes within a node tree.

    As example OSL scripts, the LGHexTiles.osl provides a complete node that can be used by itself as a material. While the MAscales.osl provides colour and float outputs that make only one node in the material.

    And like many things, OSL scripts can be abused. Here you will find info about the old Amiga Juggler being implemented in an OSL script. Yes, a simple render engine, written in OSL. I'm pretty sure he continued to develop the raytracer further.