Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Maya 2011 and mental ray with MetaSL

  1. #1
    Join Date
    Jan 2009
    Location
    Berlin/Germany
    Posts
    1,148

    Exclamation Maya 2011 and mental ray with MetaSL

    Hello mental ray for Maya users.

    The new edition of Autodesk Maya 2011 comes with the latest version of mental ray 3.8 integrated. This version of mental ray provides the capabilities to use MetaSL shaders. MetaSL is a meta shading language that allows to write shaders once and use them with different render engines, both hardware (like DirectX and OpenGL) and software (like mental ray) renderers, on any supported platform. But there is more.

    For the first time in this version of mental ray, the MetaSL shader code will be compiled and executed automatically inside the renderer without the need for external compiler tools. This technology is based on a virtual machine component called LLVM. This means, that shaders can be authored by anyone who is primarily interested in rendering algorithms, but was previously hindered by the hurdles of C/C++ software development or platform specific problems.

    Please note, that the support for MetaSL in Maya 2011 is limited, because the current version of Maya can not handle MetaSL nodes directly. However, the embedded mental ray comes with all the required functionality to take advantage of MetaSL shader code for rendering. Fortunately, this functionality can be used through the existing mental ray for Maya customization controls, like the Shader Manager.

    For those of you who like to get hands on MetaSL today I'm collecting helpful information and tips in this thread.


    Happy shading

    Steffen Roemer
    Product Manager mental ray
    Last edited by steve; October 7th, 2010 at 12:57. Reason: title

  2. #2
    Join Date
    Jan 2009
    Location
    Berlin/Germany
    Posts
    1,148

    Lightbulb Use MetaSL in Maya 2011

    Introduction

    The mental ray for Maya 2011 integration is based on mental ray version 3.8. This version allows to use MetaSL shaders for software rendering. Maya 2011 provides the LLVM back-end required for automatic deployment only for the Windows platform, where LLVM is enabled by default. Linux and MacOSX may also use LLVM as well if the back-end plug-in is available. By default, those platforms will use the C/C++ back-end, which depends on external compiler tools and, therefore, is not recommended for use inside applications like Maya.

    Prerequisites

    The MetaSL compiler component is integral part of mental ray, while the LLVM back-end is implemented in shared library extension, called gen_llvm. This extension is only loaded on demand, like the first time a MetaSL shader is actually used. No other external tools or extensions are required to implement and render the following example, since we are going to write the MetaSL shader code ourselves. Especially, it is not necessary to have mental mill installed, the standalone shader authoring application for interactive shader development. Although, this program comes with a lot of example MetaSL nodes in source code that can help shader developers to get started much more quickly.

    Load MetaSL shader in Maya 2011

    The following steps need to be performed to use a MetaSL shader in Maya. The example was implemented in Maya on the Windows platform.

    The idea is to load MetaSL shader code into mental ray for Maya 2011 using the Shader Manager. This requires to write a .mi file wrapper for the MetaSL shader, which is driving the creation of a Maya node.

    Write a MetaSL shader

    In order to demonstrate what MetaSL can do right now, we are going to write a useful node that helps to resolve a common problem in Maya: displacement workflow. We like to keep displacement related settings in a separate node, so that we don't need to touch properties of attached texture nodes like Color Effects settings Alpha Gain and Alpha Offset. That would allow us to share that same texture node with different objects or effects, assuming that Color Effects settings have been finalized for that texture and can not be touched.

    Here is the example shader. It is supposed to return a scalar value to be attached to the displacement slot of a mental ray material. It expects a color type input for connection of any other shader, and converts this color into the scalar output according to a mode and a scale parameter.

    --- displace_adapter.msl ---

    Code:
    // MetaSL 1.0
    // steve, 19 Aug 2010
    // Example displacement adapter shader to
    // convert color input to scalar displacement values
    
    shader displace_adapter
    {
    /*
    	enum Mode {
    		alpha	= 0,
    		average	= 1,
    		red	= 2
    		};
    */
    	input:
    		Color	color = Color(0);
    		Scalar	scale = 1.0;
    		int	mode  = 0;
    
    	output:
    		Scalar	result;
    
    	void main()
    	{
    	result = 0.0;
    	switch(mode) {
    		case 0:
    			result = color.a;
    			break;
    		case 1:
    			result = (color.r + color.g + color.b) / 3.0;
    			break;
    		case 2:
    			result = color.r;
    			break;
    		default:
    			result = (color.r + color.g + color.b) / 3.0;
    			break;
    		}
    
    	result *= scale;
    	}
    };
    Write a Phenomenon wrapper

    The MetaSL shader can not be loaded directly into Maya 2011. But, using the standard .mi parsing capabilities for traditional shaders, it is possible to force to load it indirectly. That requires to write a simple wrapper which exposes the interface of the original shader and connects the interface parameters to the MetaSL shader inputs.

    --- msl_displace_adapter.mi ---

    Code:
    $include "displace_adapter.msl"
    
    declare phenomenon scalar
    	"displace_adapter_msl" (
    		color	"color",
    		integer	"mode",
    		scalar	"scale" default 1.0
    		)
    	shader "shd" "displace_adapter" (
    		"color"	= interface "color",
    		"mode"	= interface "mode",
    		"scale"	= interface "scale"
    		)
    	root = "shd"
    end declare
    It is important to mention, that this step is only needed for Maya to generate a new node type for the MetaSL shader. It is not required for mental ray, which automatically generates a node description, including input and output parameters, just from the MetaSL code. Future Maya versions may be able to create their node types from reading the mental ray node repository directly.

    Announce MetaSL directory to Maya

    The MetaSL source code and the .mi declaration files may reside in the same directory, or in separate folders. Both directories need to be announced to Maya. In our example, I create a single new directory

    C:/Users/steve/Documents/metasl

    and put the files

    displace_adapter.msl
    msl_displace_adapter.mi

    there.

    The .msl files should be located in the mental ray path MI_RAY_INCPATH,
    the .mi files in the Maya path MI_CUSTOM_SHADER_PATH.

    To achieve this, create/edit file Maya.env in the sub-directory 2011 (or 2011-x64) of the Maya user preferences location, and add statements similar to the following:

    Code:
    MI_RAY_INCPATH = C:/Users/steve/Documents/metasl
    MI_CUSTOM_SHADER_PATH = C:/Users/steve/Documents/metasl
    This will define those environment variables for that specific version of Maya. It is also possible to set those variables in the user environment of the operating system, which would then apply to all Maya versions that may be running on this machine. I guess that is OK for most users. Personally, I prefer to keep settings local to a certain Maya version.

    The Maya Script Editor should report if the new .mi declarations were found and parsed. For our example, there may be some warnings printed because Maya needs to assign a node ID automatically from its pool of free node IDs. This means, that the Maya scene containing MetaSL wrapper nodes should be saved in ASCII format rather than BINARY, to store the node information in a form that does not depend on assigned node IDs.

    Load the .mi Declaration in Maya

    The mental ray for Maya plug-in will be searching for all .mi files in the mentioned directory, and generate Maya nodes out of them. And because the .mi files reference MetaSL code using the $include statement, it forces mental ray to initialize MetaSL and to load the LLVM back-end plug-in. If the user setup was successful, the following lines should be printed in the Maya Output Window as a confirmation:

    Code:
    mental ray for Maya 2011 
    mental ray: version 3.8.1.26, Feb  9 2010, revision 110437
    mental ray: attempting to load library "C:/Program Files/Autodesk/Maya2011/bin\gen_llvm.dll".
    mental ray: library "C:/Program Files/Autodesk/Maya2011/bin\gen_llvm.dll": loaded Code_generator12_plugin plugin "gen_llvm"
    Once this is all done it is possible to use MetaSL nodes in Maya for rendering with mental ray like any other node. See next post for details.

    Have fun, Steffen.
    Attached Images Attached Images
    Last edited by steve; October 12th, 2010 at 10:36. Reason: wording

  3. #3
    Join Date
    Jan 2009
    Location
    Berlin/Germany
    Posts
    1,148

    Lightbulb Run MetaSL shaders within Maya 2011

    Introduction

    After the MetaSL functionality has been enabled and the LLVM back-end was set up successfully, new node types for the MetaSL shaders will appear in Maya. Those nodes can now be used like any other Maya or custom mental ray shader node, like connected in node graphs. It is very well supported to mix nodes of different sources, like those generated from C/C++ shaders with the MetaSL ones.

    Create Maya nodes for MetaSL shaders

    • Create a geometry shape, like a poly sphere.
    • Attach a new material, like mia_material_x.
    • Open the Attribute Editor.
    • Navigate back to the Shading Engine node.
    • Click the connect button on displacement slot in mental ray section.
    • Find and select the displace_adapter_msl node, like
      • type msl in the search field, or
      • go to mental ray / Miscellaneous.
    • Connect a Maya texture node to the color parameter, like cloth in our example.


    In addition, the existing cloth texture node is attached to the color input of the mia_material_x node. For convenience, the Shading Engine node in our example got a regular Maya surface node lambert attached with the same cloth texture as input, to get swatches rendered properly.

    Once done, the Attribute Editor should show something like this:



    The Shading Engine connections will look like this:



    And finally, the Hypergraph may look like this:



    Render Scene with MetaSL shaders

    When rendering a preview of this scene like with Render Current Frame the following picture may be generated. Tuning the scale parameter of the displace adapter node results in different displacement effect.



    Voila!

    The rendering should work similarly with IPR, the interactive rendering mode in Maya.

    This image shows a snapshot with some color adjustments of the cloth texture and the displacement scale set to -1.0.



    I hope you are able to reproduce this workflow successfully in your copy of Maya 2011. I appreciate any feedback on this tutorial.

    Have fun, Steffen.
    Last edited by steve; October 12th, 2010 at 10:24. Reason: layout

  4. #4
    Join Date
    May 2009
    Posts
    50

    Default

    Hi Steffen,

    For linux it would be good if you could share some info on what is needed in order to use the llvm compiling method. I'm running maya 2011 hotfix3. I added the:
    registry "{_MI_REG_METASL_BACKEND}" value "llvm" end registry and I'm getting bunch of errors log like:

    LIB 0.0 debug: {_MI_REG_METASL_AUTOINCLUDE}: registry entry {_MI_REG_METASL_AUTOINCLUDE} not found

    Any check list you could share for those who find windows as no option.

    Thanks,
    Rob

  5. #5
    Join Date
    Jun 2007
    Location
    Berlin
    Posts
    754

    Default

    Steve put together a great tutorial on how to use plain MetaSL shaders in Maya 2011. For those who are using mental mill, here are two more threads that deal with MetaSL shaders and Phenomena in Maya 2011:

    • MetaSL shaders in Maya 2011
      This exampe comes with a fractal shader that you can try in mental mill and Maya 2011.
    • Using mental mill Phenomena in Maya 2011
      Mental mill allows you to wrap your shaders in what we call a Phenomenon. A Phenomenon hides underlying complexity of the shader and facilitates working considerably. If you want to get your Phenomenon that you authored in mental mill working with mental ray, please check out the link above.


    Best Regards,
    Ruediger

  6. #6
    Join Date
    May 2009
    Posts
    50

    Default

    Hello and thank you for your reply. Trying further Steffen tutorial on Linux I'm getting some result but not fully working. I've got the two files, displace_adapter.msl and msl_displace_adapter.mi made in a path location. When I launch maya I get this message:

    mental ray: Writing loaded shader tree as xmsl to file /usr/tmp/user-29791-mr_msl001_shader_inst_added_.xmsl
    mental ray: Writing loaded shader tree as xmsl to file /usr/tmp/user-29791-mr_msl002_phen_added_.xmsl

    I've configured my maya.rayrc:

    registry "{_MI_REG_METASL_BACKEND}" value "llvm" end registry
    registry

    registry "{_MI_REG_METASL_AUTOINCLUDE}" value "true" end registry
    registry "{_MI_REG_METASL_TRACE}" value "true" end registry
    registry "{_MI_REG_METASL_SUPPRESS_COMPILATION}" value "false" end registry
    #registry "{_MI_REG_METASL_EMULATE_MI_PHEN}" value "true" end registry
    #registry "{_MI_REG_METASL_FLATTEN}" value "true" end registry
    registry "{_MI_REG_METASL_CIL}" value "false" end registry

    I don't get any similar shell message like:
    mental ray: library "C:/Program Files/Autodesk/Maya2011/bin\gen_llvm.dll": loaded Code_generator12_plugin plugin "gen_llvm"

    when I render I get :
    GAPM 0.3 progr: starting displacement sampling for visible trace shadow part of object pPlaneShape1
    PHEN 0.3 error 051011: shader "displace_adapter" not found

    Steffen says:
    "Linux and MacOSX may also use LLVM as well if the back-end plug-in is available."
    Would that be setting up llvm from the http://llvm.org project page? or is it having the gen_llvm.so plugin loaded in maya?

    what should happen after the two xmsl files have been automatically generated while putting _MI_REG_METASL_TRACE to false?

    Kind Regards,
    Robert

  7. #7
    Join Date
    Jan 2009
    Location
    Berlin/Germany
    Posts
    1,148

    Default

    Hi Robert,

    unfortunately, Linux and MacOSX do not support automatic LLVM back-end loading in mental ray for Maya 2011.

    This is caused by the fact that LLVM is not the default back-end as on Windows.
    Enabling it with the LLVM registry key as in your example does not really help.

    You should get LLVM to work with mental ray Standalone on these platforms though.

  8. #8
    Join Date
    Dec 2005
    Location
    Wherever The Computer Says
    Posts
    2,853

    Default

    This part is fairly straightforward, however, I am having issues with the MetaSL library on the site.

    Do I load the .msl under Shaders or the .xmsl with the material name I downloaded? There are 3 .msl shaders in these packages.

    I can get something to load but rendering causes it to crash.

    The examples included in the mental mill forum render correctly but are only 2 files, the .msl and the .mi
    Attached Images Attached Images
    "Don't argue with an idiot, they will drag you down to their level and beat you over the head with experience."

  9. #9
    Join Date
    Feb 2006
    Location
    France
    Posts
    87

    Default

    It sounds great that within mayatomr there is mechnism of "on-fly" msl code compilation.
    I would write a carpaint using some base msl library from iray_illumination_coat.msl for instance.
    Try some lines but debugging is not easy cause it crashs maya
    But i am quite a novice with testing experiment, anybody succeed ?
    Visualization Team | Digital Rendering Process
    Renault Design

  10. #10
    Join Date
    Dec 2005
    Location
    Wherever The Computer Says
    Posts
    2,853

    Default

    I've not succeeded with a render of a shader I've created (I don't have mental mill) but ones provided seem to work well. (Still trying to get some of the ones from the site installed correctly, see above)

    However, when debugging and creating shaders, it's usually best to do that with Standalone. Once it works well, try it in something like Maya.
    "Don't argue with an idiot, they will drag you down to their level and beat you over the head with experience."

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •