Saturday, May 3, 2008

Frame buffers in maya 2008

Passes in mentalray 3.6 maya 2008

In mentalray there is a different definition for passes. They use it to define several parts of a very large scene to split it up into different layers, here “passes”. I’ll use the normal definition that means a special shader attribute or channel, e.g. a reflection pass, an diffuse pass etc. So how are passes defined in mr?

First you have to define an framebuffer in the options.

There is always a primary buffer and all buffers you define are additional framebuffers. Framebuffer will contain the samples, not pixels. They are converted into pixels before calling an output shader.

options "opt"
object space
contrast .1 .1 .1 1
samples -1 2
frame buffer 0 "+rgba"
frame buffer 1 "+rgba"
end options

This enables mentalray to allocate memory for the buffer and sets the type of the buffer, the plus sign means the buffer samples will be interpolated. That is not useful e.g. for a z depth buffer.

Next mentalray needs to know what it has to do with the framebuffer after rendering. All after-render-operations are calculated in an output shader. So you will define something like this:

camera "cam"
output "fb0" "tif" "somefilename.tif"
output "fb1" "tif" "someotherfilename.tif"
output "rgba" "tif" "primaryBuffer.tif"
...
end camera

Now mr knows to save the buffer as an rgba image in tif format. “output rgba” represents the primary buffer that is calculated always. The important thing are the “fb0” or “fb1”. With these shortcuts you identify the buffers allocated in the options sections “frame buffer 0...”.

Fine, now we have the allocated buffers and we know what to do with it. But they are still empty. How can we fill them. For this the shaders itself are responsible. There exist no default spec pass variable or diffuse or something like that. In the shader there has to be an statement like this one:

miColor color;

do something with the color

mi_fb_put(state, 0, &color);

This means that for this sample the color should be saved in buffer 0. This is the reason why this does not work with the maya shaders because the shaders dont contain these mi_fb_put() statements.

Fortunatly there exist some mentalray shaders out there in the www that enable you to save shader outputs into differnt buffers. e.g the puppet shaders from Pavel Ledin. He also offers the source code of his shaders. e.g. in his p_MegaTK he offers the possiblity to save several passes like the diffuse one or specular one into the buffers. Another great one is ctrl_buffers from ctrl studions, you can find them on cgtalk. great, are there any problems?

Yes, indeed. There are a few problems. Most of them are now solved with the release of maya2008 or 3dsmax2008.

  • sampling: before mentalray version 3.6 all buffers were sampled with the sampling of the primary buffer. So what? Where is the problem? For example, if you want to render a low contrast primary buffer and add contrast and detail with the occlusion pass, you may run into that problem.

passes_lowcontrast.jpgpasses_occlusion.jpg

Yes, I now these examples are really unrealistic and extreme. But they illustrate what I mean. The left one ist the low contrast primary buffer and the other one is the occlusion pass that is sampled with the same sampling like the first one 0/2. Because there is no contrast everything is calculated with sampling 0 even the occlusion pass. If you have a look at the occlusion rendering you will see aliasing artefacts at the diagonal edge of the occlusion area.

Of course you can fix it if you combine your buffers into the primary buffers and write you lowcontrast image as a seperate buffer. But it can lead to serious problems. Fortunatly mentalimages heard the complaints and implemented a new option in 3.6: “contrast all buffers” that enables a different sampling on all buffers.

  • buffer system: before maya2008, it was quite complicated to get the correct output. The main problem was that you could define additional output buffers, but there was no easy way to let the camera know which buffer should be written correctly.

This is exact the reason why all buffer shaders need an geometry shader.

What????

To use buffers correctly with maya releases before maya2008 you had to take care that the connection between the ouput options “frame buffer 0 rgba” and the camera output option “output fb0 ....” was correct. The simples way to do that is to create an geometry shader. An geometry shader is executed in the pre render phase and can manipulate almost everything in the mr scene database. An geometry shader can automatically create the frame buffer statements and the output statements to the camera. With maya2008 you are finally able to connect the output statement directly to your created frame buffer so that there is no geometry shader needed any more. solutions: use maya2008

Here is how a simple passes can be used in maya2008:

First you have to create the frambuffers statements for the mr options. This can be done in the mentalray globals. Open the framebuffer section and click on the “Open Editor” button in the “User Framebuffer section”. This opens the attributeEditor for the miDefaultOptions node. Here under “Frame Buffers” you can add an framebuffer with the “Create” button (see right side).

passes_maya2008_fb_01_sm.jpg

In the appearing node, you can set the data type. For 8bit images the default is okay. The “interpolate samples” will add filtering to the samples, this means the little + sign mentioned above.

Okay very fine, you now have defined an framebuffer. If you open again the editor, you will see an new entry in the framebuffer list.

passes_maya2008_fb_02_sm.jpg

Great, the first step is done. Now we need a possibility to output the buffer to an image file. This has to be done in the camera. Select the camera an open its attribute editor. Go to the mentalray section, open it an do the same with primary output passes. Automatically the secondary output passes appears. Use the “Create” button to create a new ouptut element. Here you can turn on “Use user buffer”. In the popup menu below the checkbox, you should be able to select your previous created framebuffer. Do it. Then turn on “File mode”. Below you can enter an extension to your default file name. The file will be named like all other images, but with your new extension. e.g. sceneName_extension.001.sgi

passes_maya2008_fb_03_sm.jpg

Cool! Isnt it? So now you can render framebuffers. Of course you will only see black images. Why of course? Well, you have defined the buffers, but you didnt fill them yet. How can that be done? You can do it with any shader that is able to fill buffers. You could use the ctrl_buffers, or p_MegaTK shader. They will simply write your infos to a buffer. Or you can use this one: simplePasses. The archive contains the mi file as well as the shader dll compiled for win32 and win64(I couldnt test the 64bit shader yet but I hope it works). But I have added the source code, so if you want you can use it to enhance it as you like it.

It is an extemly simple shader that puts your input into the framebuffers. The first input will be used as primary buffer.

passes_maya2008_fb_sp.jpg

The first shader will be written into the first buffer, the second into the second one etc... be careful not to use more shaders than previously created buffers.

And you should avoid buffer 0. It is used as glow channel by maya itself. That means every color written to the buffer 0, the first one in the simplePasses shader, will create a glow effect. You can avoid it by disabling the glow calculation in the mr globals.

The shader is compiled for mr 3.6 and will not work with any maya version below maya2008 because none of them is able to export color arrays correctly.

Source:- http://www.wiki.render3d.de/doku.php?id=renderwiki:techniques:passes:mr

No comments: