27/01/2010

Where to Start When Creating a Custom mental ray Shaders for Maya

Where did January go? I feel like it was only January 1st last week. It’s one of those rainy-snowy weeks in Toronto, which is kind of miserable if you ask me.

However this is a momentous week, as I did get to meet my blog mates Annick and Owen, you might be thinking “WHAT?!? You’re on a blog together…..” Autodesk is very global, and we are based out of different offices, and it’s not that we don’t regularly communicate, it just this is the first face to face meeting, so now I have a face for the name :)

I was digging in the crates (or rather my computer file folders), and I found some notes I made a while back on shader writing from a mental images training and I thought I would share them with you.

Writing Shaders:

This is an introduction to writing mental ray shaders; we want to try to follow these steps, to compile a shader that will run inside mental ray for Maya.

This will introduce writing a shader from scratch going through the principle ideas. The basic steps are written here, before you start you need to think about what the shader will do, what it will compute and return depending on your algorithm that you are going to implement, and which parameters it needs to control the algorithm. When you finish that you create the shader declaration that is the information that mental ray needs to know the inputs and output parameters, and some more options that tell mental ray about the type of the shader so that it can make some assumptions about the shader, and may optimize it. Then you have to write the shader writer code, you can take any language you want, the only interface to mental ray is a shared library that exports some known symbols, so that mental ray will be able to find your functions and call them. Once you are finished that, you can have a binary file in addition to the declaration file. This binary needs to be loaded into mental ray. That is the next step ‘Load into the Application’, once that is done, you can create nodes inside Maya connected and render.

A) Design the interface

Determine type of shader

Specific or general use?

Determine type and count of result values

Single or multiple output parameters?

Determine type and count of input parameters

Follow common scheme for compatibility?

Provide reasonable defaults

Provide UI hints when possible

B) Create Shader Declaration

Write .mi declaration

Set apply property

Set version

Set requirements with care !

Set nodeid property for public shaders

Validate

Load into mental ray

*prefer to do in mental ray for Maya because standalone will not catch the Maya errors.

**Open and load the .mi file you have just created into Maya using Windows > mental ray Shader Manager, if it loads fine you have no syntax or other errors, this is good

C) Writing Shader Code

Write required code

(optional) Use mkmishader tool for prototype to create C code from your .mi file, located in: