Monthly Archives: April 2012

Professional Work Using mental ray

On the nVidia mental ray forums they’re looking to showcase the work you’ve done at studios using mental ray. Specifically modern techniques like Unified Sampling or IBL work.

For example, Lexus – Beast produced at The Mill: Lexus – Beast

Take a look here and show off your hard work! mental ray Clips Wanted

Lexus "Beast" © produced at The Mill

Texture Publishing to mental ray

Nailing down a good texture pipeline can be confusing!  Hopefully this clears up some of the whys and hows for efficiently handling texture of images with mental ray, providing a straightforward solution to a complicated problem.

[You can also find a test example on the ARC forum here: ]

Texture Filtering

Non-filtered images may result in artifacts such as moire patterns (click to view full size).

When sampling a textured object, color information is lost between sampling points.  This is because a single sample only calls a single texture pixel (texel).  Information for texels that don’t get sampled is lost – which can be a real problem if that information was important.  For highly detailed textures or for textures with regular patterns this sampling limitation may manifest itself as an artifact such as a moiré pattern.

Aliasing-free texture sampling is restricted by a mathematical limitation known as the Nyquist rate which states that the number of samples required to adequately estimate a signal must be at least twice the noise frequency.  This means there are only two options to remove rendering artifacts caused by noisy textures: sample more or filter the texture.

Filtering a texture removes high frequency noise from images making them easier to sample.  While this does away with many potential aliasing artifacts, it also removes detail leaving your textures looking fuzzy.  This is not a problem for textures that occupy limited screen space because that is detail that would have been lost anyways.  However it is a problem for textures that occupy lots of screen space because that detail might have been visually important.  Luckily for us, texture mipmapping provides a convenient solution for both situations.


Mipmapped images reduce texture artifacts by filtering (click to view full size).

Mipmapping (from the Latin phrase multum in parvo meaning “much in little”) is the process of pre-filtering one large image into many smaller images of progressively decreasing resolutions. The largest images retain detail that the smallest images lose to filtering.  These mipmap images are generally organized into a multi-resolution file sometimes known as a pyramid image.

Mipmapped “Pyramid Image”

The advantage of multi-resolution images is that they provide mental ray with the ability to read from appropriately sized pre-filtered images based on the amount of screen space that the textured object occupies.  This allows superior anti-aliasing performance while using less samples.

For the above renders, mental ray read from higher and higher mipmap levels as the checkered texture receded into the background.  The highest mipmap levels have lost virtually all detail and appear as a middle grey.  As a result of filtering, mipmapping reduced the total number of eye rays from 5552111 to 5144177!

It is advisable to use mipmapped textures.  Generally, mipmapped textures should be used with the filter declaration (see miFilter attribute below).  The purpose of the filter declaration is two fold:

  1. filter tells mental ray to mipmap images on demand if they are not already so.  When computed on demand, full upper pyramid layers are computed and kept in memory, totaling 1/3 of the base (largest resolution) image.
  2. You can control how selective mental ray is when choosing mipmap levels (and thus the blur of the textures) by setting filter size (see miFilterSize attribute below).  A filter size above 1.0 increases the texture blur by reading from a higher level (lower resolution) mipmap.  A filter size below 1.0 reduces texture blur by reading from a lower (higher resolution) mipmap.  Generally filter size should correspond to the number of times a texture repeats itself in UV space, i.e. an image that repeats itself 10 times should use a filter size value of 10.0.

Elliptical Lookup

Elliptical lookup has removed practically all texture artifacts (click to view full size).

Texels are square when viewed with photoshop or some other 2D application, but this is not always the case when rendering in a 3D environment.  When textures are rendered at glancing angles, the screen space each texel occupies become distorted – i.e. more texels may fit into a screen pixel in one direction than another.

To avoid artifacts caused by symmetric filtering for textures viewed non-symmetrically, some textures require an additional elliptical lookup on top of mipmapping.  Elliptical lookup works by projecting the circular area surrounding a sample onto the texture image, resulting in an ellipse.  The texels within the ellipse are then averaged resulting in a more accurate return value.

The above render shows the artifact-free result elliptical lookups provide.  Not only is the strange middle grey circle gone, but the number of eye rays has been further reduced to 4705444!

While elliptical lookup is relatively fast, not every textured object requires it.  To avoid unnecessary render time, elliptical lookup should be enabled on a per-texture basis.  The default lookup size of 8.0 is generally a good starting point for most situations.  Reducing this value will speed up render times, but may reintroduce artifacts.

Note: mental ray will mipmap on demand if elliptical filtering is specified for non-pyramidal images.

Tiled and Cached Textures

Keeping all texture information in memory can become quite expensive for large scene, especially when using pyramid images.  mental ray supports texture caching for certain tiled texture formats.  When texture caching, only certain tiles of a texture are loaded into memory.  These tiles are automatically removed or replaced by recently accessed ones, dramatically reducing the memory consumption during rendering.

Starting with mental ray 3.9, texture caching can be specified globally using the “{_MI_REG_TEXTURE_CACHE}” registry variable set to “on” / “off” / “local”.  The defualt “local” means that only local textures are cached (see miLocal attribute below).  Native .map files are always considered local.  It is important that local textures be saved locally or on a caching file server where images can be shared across multiple machines (on a render farm) to avoid network overhead.

Another registry variable, “{_MI_REG_TEXTURE_CACHE_SIZE}”, exists to set a maximum size for the texture cache in megabytes (e.g. “512” corresponds to 512MB).  mental ray will dynamically determine the cache size for the default value of “0”.

It is advisable to use tiled/cacheable textures to reduce memory consumption.

Image Formats and Conversion

  • OpenEXR (.exr)
    • Can be saved as mipmap pyramid
    • Can be tiled/cached
    • Personally recommended by me!
  • mental ray’s native memory-mapped image (.map)
    • Can be saved as mipmap pyramid
    • Can be tiled/cached
  • TIFF (.tif)
    • Can be saved as mipmap pyramid
    • Can be tiled/cached
    • note: Photoshop has been known to do some odd things to tifs (like layering)
  • Bitmap Image File (.bmp, .dib)
    • Can be tiled/cached
  • Maya IFF (.iff)
    • Can be tiled/cached

imf_copy (which ships with mental ray and Maya) is a convenient command-line tool that can be used to create mipmapped-tiled images. For example, I could run this command if I wanted to publish my working TIFF texture to an mipmapped-tiled zip-compressed OpenEXR texture:

imf_copy -p -r -k zip working_texture.tif published_texture.exr

Alternatively, exrmaketiled and/or the openimageIO toolkit can provide increased flexibility for publishing textures.

Setting up the Maya file node for mental ray

Maya file node set up for elliptical lookup

If using a pre-mipmapped image (which you should be):

  1. File Attributes > Filter Type → Mipmap (This enables the miFilter option)
  2. File Attributes > Pre Filter → off (we are using mipmapping with the option of elliptical lookup instead)
  3. mental ray > Override Global Auto-Conversion Settings → on (I don’t trust any global Maya auto settings)
  4. mental ray > Convert File To Optimized Format → off (texture has been already mipmapped!)
  5. mental ray > Advanced Elliptical Filtering → generally off, on if this texture is producing rendering artefacts
  6. Extra Attributes > Mi Local → preferably on for texture caching, otherwise off.
  7. Extra Attributes > Mi Filter Size → generally 1.0, higher for repeated textures.
Note: The last two are dynamic attributes so they might not be present on all file nodes.  See below on how to add them.

Python script to add texture attributes to all Maya file nodes in a scene:

import pymel.core as pm

# Add Extension Attributes to all file nodes.
pm.addExtension(nt="file", ln="miLocal", at="bool")
pm.addExtension(nt="file", ln="miFilterSize", at="float", dv=1.0)

# Delete Extension Attributes from all file nodes.
pm.deleteExtension(nt="file", at="miLocal", fd=True)
pm.deleteExtension(nt="file", at="miFilterSize", fd=True)

Last Notes

As a rule-of-thumb, I recommend using mipmapped-tiled OpenEXRs for all textures and to declare them local.  The one exception is the environment map which will only benefit from tiling, not mipmapping.  To pre-blur the environment, try using the mia_envblur shader combined with “Single Env Sample” on material shaders.

A formalized texture publishing step between texture creation and rendering provides an excellent opportunity to both optimize image formats and linearize data.  For more information on colorspaces and linear color workflow, see Linear Color Workflow(s) in Maya.

Unified Sampling in 3.10 (and other changes)

Autodesk released their 2013 products this last week. This is the first public release of mental ray 3.10.

You will find this release focuses mostly on bug fixes and enhancements to existing features. The “What’s New” section leaves out the details. You can find the details in the Release Notes section. This is the best place to find fixes and enhancements you may need to know about.

The majority of your performance increase will be seen with Unified Sampling. Things to note for Unified Sampling in 3.10:

  • Previous scene’s Quality settings will generate fewer eye rays since they are unnecessary
  • Unified Sampling will sample dark areas of an image less
  • Unified Sampling produces smoother grain in areas with insufficient Quality
  • Framebuffers no longer have artifacts
  • Edges and thin objects (like hair) have improved sharpness
  • Motion Blur is smoother than before

Many of the previous caveats that may have kept you from using Unified Sampling before have been fixed in Maya now. More improvements are to come. We’ve been told from Autodesk that Hotfixes will be more often and hopefully provide more opportunity to upgrade and replace mental ray since it is now a separate plug-in.

For same-scene renders you can expect complex scenes will render 10-15% faster than before.

Below are some examples. I was kindly given a scene from a current intern at Full Sail University in Orlando, Florida. I cannot experiment or release my current work on the blog so it’s great when I have some nice projects to play with and share!

You can find more of Jiayu’s work on: Jayuliu

Previously for this scene it was taking multiple hours a frame using traditional techniques. I have since updated the sampling on the scene to use Unified Sampling and area lights. For a 720HD render it now takes about an hour for the night time scenes you will see shortly. 10 minutes a frame for the close-up Bedroom scene. There could be some more tweaking done to these using advanced lighting (covered later) but one thing at a time. 😉

Daylight Bedroom:

Jiayu's Bedroom Model Render

These scenes are rendered following the guides in the Unified Sampling for Artists Post and the Area Light Post.

In mental ray 3.9 I rendered the above scene with these settings:

  • samples min 1
  • samples max 500
  • samples quality 4.
  • error cutoff 0.04
  • Gaussian filter 2. 2.

For mental ray 3.10 I used the settings that gave me the nearest result without going overboard:

  • samples min 1
  • samples max 500
  • samples quality 2.5
  • error cutoff 0.02
  • Gaussian filter 2. 2.

The time saved for a quick render are minimal for a trivial scene:

3.9:       0:12:11.04

3.10:     0:11:12.86

What is of note is how Unified Sampling sees the scene. Below are the Sampling Diagnostic Framebuffers, 3.9 first, 3.10 next. Brighter areas are more samples.

3.9 Samples Diagnostic

3.10 Samples Diagnostic

Notice how dark areas sample much less than before (in the painting above the bed and the foot of the bed for example). Below is the visual difference from imf_diff utility, somewhat exaggerated to see better.

Image Difference from 3.9 to 3.10

Things to notice here are:

  • Edges are cleaner than in 3.9.1 (this change was introduced in 3.9.2 and improved in 3.10)
  • Area Light grain is less noticeable/clumpy than before (overall sampling pattern is smoother)
Pertinent diagnostics are below:
mental ray 3.9

JOB 0.2 progr: 100.0% rendered on SIAB.2
RC 0.10 info : rendering statistics
RC 0.10 info : type                             number     per eye ray
RC 0.10 info : eye rays                       12597442     1.00
RC 0.10 info : transparent rays                 278449     0.02
RC 0.10 info : reflection rays                 8661007     0.69
RC 0.10 info : refraction rays                  643795     0.05
RC 0.10 info : shadow rays                   126100094    10.01
RC 0.10 info : environment rays                 146106     0.01
RC 0.10 info : probe rays                     34008648     2.70
RC 0.10 info : fg points interpolated         20323641     1.61
RC 0.10 info : on average 86.51 finalgather points used per interpolation
RC 0.10 progr: rendering finished
RC 0.10 info : wallclock 0:12:11.04 for rendering
RC 0.10 info : allocated 353 MB, max resident 413 MB
GAPM 0.10 info : triangle count (including retessellation) : 994312
PHEN 0.10 info : Reflection rays skipped by threshold: 4405980
PHEN 0.10 info : Refraction rays skipped by threshold: 22742

mental ray 3.10

JOB 0.7 398 MB progr: 100.0% rendered on SIAB.7
RC 0.3 398 MB info : rendering statistics
RC 0.3 398 MB info : type                        number    per eye ray
RC 0.3 398 MB info : eye rays                   9192639    1.00
RC 0.3 398 MB info : transparent rays            275289    0.03
RC 0.3 398 MB info : reflection rays            6560278    0.71
RC 0.3 398 MB info : refraction rays             553186    0.06
RC 0.3 398 MB info : shadow rays               95549199   10.39
RC 0.3 398 MB info : environment rays            142848    0.02
RC 0.3 398 MB info : probe rays                26833006    2.92
RC 0.3 398 MB info : fg points interpolated    14966173    1.63
RC 0.3 398 MB info : on average 87.47 finalgather points used per interpolation
RC 0.3 352 MB info : wallclock 0:11:12.86 for rendering
RC 0.3 352 MB info : current mem usage 352 MB, max mem usage 411 MB
GAPM 0.3 352 MB info : triangle count (including retessellation) : 994312
PHEN 0.3 352 MB info : Reflection rays skipped by threshold: 3560987
PHEN 0.3 352 MB info : Refraction rays skipped by threshold: 19657

Below is the night image. 12 area lights of different sizes/types. As above I am using the ambient occlusion in the mia_material set to 4 samples. The wall behind the bed has the color bleed option turned on to improve the light from the glowing mushroom night light.

Night image, time for render: 1:09:13

The more important diagnostics can be seen here from 3.9 to 3.10. This scene did not have a change in any settings from 3.9 to 3.10. Exactly the same settings were used.

mental ray 3.9

RC 0.10 info : rendering statistics
RC 0.10 info : type                           number     per eye ray
RC 0.10 info : eye rays                     18624542     1.00
RC 0.10 info : wallclock 1:34:57.79 for rendering
RC 0.10 info : allocated 1132 MB, max resident 1302 MB
GAPM 0.10 info : triangle count (including retessellation) : 1882529

mental ray 3.10

RC 0.3 764 MB info : rendering statistics
RC 0.3 764 MB info : type                     number     per eye ray
RC 0.3 764 MB info : eye rays               11500119     1.00
RC 0.3 679 MB info : wallclock 1:09:09.39 for rendering
RC 0.3 679 MB info : current mem usage 679 MB, max mem usage 850 MB
GAPM 0.3 679 MB info : triangle count (including retessellation) : 1882529

The time saved is 23%! A reduction in 7 million eye rays and nearly 500MB for memory consumption for the same image.

Below is a day scene based on the original scene I was given that can now be rendered in a reasonable amount of time as an animation:

Jiayu's Original Day scene, no changes from file other than technical.

Additional Notes:

  • Complex scenes will now render faster than before with no changes and consume less memory.
  • Greater complexity sees more benefit.
  • You should be able to reduce your Quality for Unified Sampling and still see a better quality image than 3.9 with a shorter render time.
  • 3.10 now allows you to mix Final Gathering with Irradiance Particles without interpolation artifacts. It also exposes new ways to combine FG + Importons and/or IP, see the Release Notes for the most information.
  • Texture caching is improved and will allow you to render more textures at once with much less memory. The mechanism is also faster. In a future post we will show you how to use this with your Maya installation.
  • user_ibl shaders increase how quickly and easily you can light using mapped textures. user_ibl_env allows you to light in the same way as you would with the Native IBL, but has simpler controls and preserves texture details better. Both follow the same guidelines for samples as the Area Light post (4-8 for samples in the area light) and must match the samples on the light shader itself for best results. More samples will be necessary for more complex HDR images.
  • Raytracing speed was improved but the emphasis was hair and fur. Unified Sampling and better raytrace speed should increase the speed of scenes with hair and fur without needed rasterization.
  • New shaders for hair and fur can be written to make brute force sampling for hair and fur a reality with Final Gathering.
  • Framebuffers with Unified Sampling no longer produce artifacts.