Mental Ray Sampling


Sampling is one of the most delicate features of Mental Ray (and most of the other renderers) rendering-time and quality-wise. Some Maya-renderer users tend to use only the default preset render settings of Maya and this technique leads to very drastic rendertime increases when dealing with Mental Ray. To properly tune rendetimes and image quality to your taste you have to understand a few fundamental concepts and techniques.


Sampling itself is basically the process of shooting rays for every sample from the camera (eye rays) into the scene and where these rays intersect geometry they query the shading information for that point. From that shading information will the renderer compute the colour of the pixel for the final image. This process can take quite some time because it depends on the complexity of the shaders used, so the number of samples inspected does have a really solid hit on rendertime. (And material and volumetrical shaders can send even more rays into the scene for example in case of transparency, reflection or refraction calculations.)

When sampling happens less frequently than the number of pixels of the image (this is called infrasampling or undersampling) the rendered image will complete faster for sure, but it will be incorrect: for certain pixels the renderer doesn’t have real information and it calculates them by interpolating the colour of surrounding pixels. This way the renderer can miss details or interpolate “extra” detail that is in reality not present in the scene.

If you sample each and every pixel once than theoretically you do have precise information for the image, but in reality you encounter many sampling failures, artifacts. For example the outline of the objects will be sharp (an artifact widely called „jaggies” or “staircases”), because on a specific pixel the object is either present or not, there is no inbetween value.
To get rid of this and some other sampling failures called aliasing (this name has a long history in the field of signal processing, and it is the result of bad signal reconstruction) you have to take more than one sample for every pixel (called supersampling or oversampling), each of them from a slightly different position, and the rendered colour of the pixel will be the average of these samples. This definitely increases rendering time but you can avoid jagged outlines and other artifacts.

Adaptive sampling

If you take a closer look at the sampling settings of Mental Ray you can see that sampling is not defined by one parameter, but three: Min samples / Max samples / Contrast threshold (by colour channels).
Thats because Mental Ray uses a sampling technique called adaptive sampling (so does Maya and most of the modern renderers). The concept of adaptive sampling is to use as few samples as possible to achieve good image quality by increasing sample count locally at certain parts of the image. The process is the following:

In the first place the renderer takes as much samples as the min samples parameter requires and compares these value to each other and the surrounding pixel`s samples, and if the difference is greater than the contrast threshold value than takes additional samples, repeating this until the limit defined by max samples is reached. Max Samples is the upper limit of the possible sampling count, to avoid calculating samples for infinite time.
In other words it simply means that the renderer takes more samples at areas where there is more visual information in the image and therefore it calculates more accurate results.

So Min and Max values are basically a frame, the lower and upper limit of sampling, and it is the contrast setting that defines the sensitivity of additional sampling. When the contrast setting is high it might happen that the renderer does not take a single additional sample (no matter how high the Max Samples value is), while a low contrast setting would trace every pixel forever, or at least until the max samples limit shuts it down to avoid calculating until the end of days.

Samples settings and eye ray count

However sampling values are surprisingly don’t change in a linear fashion, they change exponentially. Although it might sound complicated, the exact number of rays cast can be calculated by taking two to the power of twice the number of samples set:

eye rays = 2^(samples*2)

There is not much need to remember this equation but the results of this are very important to remember to, because the effect is very drastic:

Setting a value of 0 for Min Samples results in 1 ray per pixel:
2^(0*2) = 1
Value 1 means 4 rays
2^ (1*2) = 4
Value 2 means 16 rays
2^(2*2) = 16
… and so on

So every single increase of the min samples value quadruples the number of rays shot, basically quadrupling the shading calculation and therefore the rendertime too. The quadratic change is the result of subdividing the current pixel area to four equal sub-parts. By shading these new, smaller sub-parts the renderer gets more precise information about that area only.

You have to handle the Min and Max Samples values very gently because raising them too high can lead to astronomical rendertimes. (Since every cast ray does a new shading calculation, calling every relevant shader, potentially sending even more rays into the scene.) Negative values have the opposite effect on rendertime.

In the next chart you can see how the sample settings effect the number of cast eye rays:

Eye rays per pixel
1/16 (1 sample per 4×4 pixels, so one sample defines 16 pixels)
1/4 (1 sample per 2×2 pixels)
1 (1 sample for every pixel)
4 (four samples used for one pixel)


The samples taken for a pixel are not simply averaged together, they are summed using a specified filtering method. Different filters produce different image characteristics, mainly more or less blurry images. This can be important, because there is no need to do extensive sampling when the result is blurred. Its very tempting to setup hyper-real crisp, awfully slow renders, but they are usually far sharper than most photo, film or broadcast material.
The Filter Width and Height parameters define the strength of the filtering in the horizontal and vertical directions. (Usually the two values should be the same, but assymetric values can be set for example to reduce vertical field artifacts for animations for broadcast.)

The characteristics of different filters:

Box: Simple mathematical average of the samples. This is the fastest and most blurry filter, so fine detail is lost.
Triangle: Sharper than Box, but slower. Samples farther from the center have less effect on the pixel.
Gauss: Soft image, intermediate quality and speed.
Lanczos: Pretty slow filter, but the result is very sharp.
Mitchell: The most precise but slowest filter. This is the closest to the perfect, ideal filter: it filters without blurring.

In general the more complex filtering methods need higher filter size values to work properly. (box 1, triangle 2, Gauss 3, Lanczos and Mitchell 4 by default) Large values result in softer images and values below 1 cause artifacts to appear.

Sampling techniques

Before starting a render you have to think for a while about your goal and the spare time you have. For a quick test you can use fairly high contrast values (0.3-0.6) and low sampling (min -2 max 0), and even for production quality you don’t always need higher values than min 0 and max 2. When using depth of field or area light soft shadows this can go up to min 1 max 3 (or a nightmarish max 4 in very special cases), but the contrast values have more effect on these calculations than max samples.

The writers of Mental Ray (or at least the writers of the documentation) suggest to use min samples plus two or three for the max samples, to let the adaptive algorithm some freedom to work. (No big magic here but good for starters.)
You should very rarely use higher max samples value than 3. When rendering a still image it is acceptable to wait, but for an animation it can be very dramatic to sample every pixel potentialy 256 times, even 64 is pretty decent.

At first glance it might be tempting to use a big negative Min Samples value to sparsely evaluate the image first and than let the adaptive sampling kick in when the contrast is over the threshold. This way some small objects or textures might dissappear between the cast samples. To evaluate every pixel at least once you have to use a min samples of 0, but if it is higher than 0 you ask the renderer to do four times more calculation for every pixel by default.

The Min and Max Samples parameters are pretty generous when dealing with shooting rays, without really affecting the “sensitivity” of the rendering. Of course if you sling a lot of rays around the scene you’ll get better quality in the end, but usually you don’t want to quadruple the number of rays just to increase render quality a bit. The Contrast parameter has much greater impact on render quality, mainly when you have to deal with multiple sampling (depth of field, motion blur, blurred reflection, area light shadows…). Decreasing the contrast threshold is much more useful in these situations than simply raising the max samples. (Of course you must have a fairly high max sample count for this to work. But 3 is usually enough.)

You should always decrease the contrast first and only increase max samples when the result is not sufficient. There is no point of using values less then 0.05 for contrast, because it does not really make a difference. Values above 0.3 are pretty gross so they can only be used for quick test renders or animation tests.
If you raise Max Samples by one you can try to set contrast to a higher value (try a value around 0.2), to check if the use of more samples is enough for only the problematic, high-contrast areas.

Jitter – Reducing straight line, repeating pattern and banding artifacts

When rendering straight lines or repeating tiles and similar patterns you might encounter sampling artifacts that can be eliminated only by using extreme sampling count. The reason of these artifacts is that the sampling happens in perfectly regular intervals and from points that lie on straight lines. By turning on the jitter switch, most of these problems are eliminated because every sample ray is fired from a (tiny-bit) randomly altered position. The effect of jitter is pretty small on rendertime, at least compared to shading calculations.

Texture sampling and different artifacts

Sometimes sampling quality is good for objects and shadows but it fails at certain textures. This usually happens at high frequency textures (for example regularly repeating patterns disappearing in infinity, or detailed fractal noise) and although very small contrast values can fix these problems, this also has a great and unnecessary impact on rendertimes. Changing the texture filtering is a more elegant and far more efficient solution to these problems. If the standard maya filtering does not solve the problems, you can create an elliptical filtering setup for the texture using custom mental ray nodes and textures. Changing contrast threshold always effects the rendering of the whole image.

Final Gathering calculations, area light shadows and motion blur can cause artifacts (flickering, dark spots) too, that can be reduced by finer sampling. But like filtering textures, these can be corrected by the tools` own settings. The Final Gather rays and radius parameters, area light and depth map shadow sampling settings only affect the problematic region, and does not slow down the rendering of the whole image. Motion blur has separate contrast values that in conjunction with the sampling contrast helps fine-tuning the rendertime and image quality.

Diagnose Sampling

The Diagnose Samples render mode can be a very useful tool to check the effect of sampling settings. (Render Globals / Mental Ray / Diagnostics / Samples) This tool renders the image normally and displays the number of cast rays as a greyscale image:
the black colour means 1 ray, the white colour is the maximum number of rays specified by max samples, and the inbetween colours display the ray count between these two numbers.
Ideally the image should be as dark as possible and only brighten where object and texture changes are present. If the image seems to be too bright and not only the contours are white, it usually means that either the min samples setting is too high (and therefore it took too many samples by default), or the contrast setting is too low (therefore every tiny change of colour results in new samples). If the image looks too dark and not even the detailed areas are bright, than probably the contrast threshold is too high.


· When the Min and Max Samples settings are the same, the whole adaptive sampling process is turned off and standard sampling is used. This usually produces nice, precise images, but the rendertime can be way longer than by using adaptive sampling.

· Contrast values can be different for colour channels and alpha. By using different contrast values for the RGB channels, you can simulate the colour sensitivity of the human eye or a specific film stock. By decreasing only the alpha value you can create even more precise alpha masks.

· The contrast threshold values are automatically multiplied by two for every extra sampling level. So when crawling from min to max sampling it requires higher and higher changes to cast new rays. This process can not (and should not) be changed but it is theoretically interesting.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s