Tuesday, 12 January 2010

3ds max: Decoding mental ray BSP tree




After few requests for an insight into the BSP parameters; I have managed to find time to put some thoughts together.

Please note: this subject, although in our latest book, does not give such detail as seen here, due to the final page count and the targeted audience.




The Theory

BSP tree stands for Binary Space Partition tree.

It is worth noting that although the BSP parameters are under the ray tracing group, it only affects the geometry, as oppose to reflections, etc.

This ray trace acceleration method essentially helps mental ray to cast rays in a speedy matter by creating an imaginary bounding box around the entire scene, with subdivisions.
These subdivided patches/cells inside the bounding box are technically designated as voxels.
Mental ray usually splits all voxels of the scene in three axes (i.e. X; Y; Z); in almost equal number of triangles, until depth is reached.
The "Size" and "Depth" parameters help mental ray to determine the total number of triangles(i.e.leafs)to be processed for ray casting/testing.
The higher the depth values, the fewer the voxels will be.
Fewer voxels equals faster rendering times, as mental ray will use fewer voxels to test the rays against.


When shooting a ray there are 2 phases

a) Moving down the BSP tree depth whilst checking/hitting all axis of each voxel.


b) Whilst checking/hitting voxels, it will touch triangles(i.e. leafs) in the process. If perchance there are 1000 triangles(i.e. leafs) in a voxel; each will be tested 40 times (i.e. default depth value).
Subsequently the rendering times will be slow.
If there are only 10 triangles(i.e. leafs), the process will be faster.
With this in mind, the user’s goal should be to reduce the number of average and maximum leafs in the BSP tree.

The total rendering time is a combination of the time it takes to create the voxels, move down the tree depth(i.e. pre processing/translation); and the final time to check/split the triangles (i.e. leafs) during the rendering time.




The default BSP ray trace acceleration method is often used for small/medium Max scenes (i.e. less than one million triangles/polygons).
Users often press the hotkey 7 to determine the number of polygons/faces/triangles in the scene.

The default Size value of 10 sets the minimum number of objects to be found in the scene before a voxel is split (in all three axes (i.e. X; Y; Z).
Smaller values equates to more voxels and slower rendering times.



The default BSP Depth value of 40 sets the maximum number of subdivisions/splits (i.e. tree levels) per triangle(i.e lesf) in the voxel of the BSP tree.
These divisions are technically designated as levels (i.e. tree levels).

Each Depth value is considered to be one level (i.e. level 1= trunk; level 2= branch; level 3= branch; etc).

In general, increasing the depth values reduces the average leaf size(i.e. triangles); which is very beneficial for high resolution renders.
The leafnodes and the bsp size(kb)may also increase in size subsequently.

If using displacement maps this theory may not work, as displacement materials will generate more triangles at render time.
The higher the resolution,the more triangles will be generated. If this is the case, simply switch it to BSP2 type.

Animated scenes are also quite difficult to control the max and average leaf size, as objects appear and disappear in the camera view.
Perhaps it’s best to place the camera in position where it captures all the objects in scene; followed by working out the best(i.e. lowest number) max and average leaf size of the BSP tree.


The BSP Depth values seem to generally have more impact in the rendering times than the Size values.
Increasing the Depth value (to 50 or above) may reduce the final rendering times (i.e. faster).
This is more beneficial for high resolution renders (i.e. 3500 pixles, or higher resolutions).

The default BSP values (i.e.10/40) work best in most scenes with less than one million triangles.
If not satisfied, then one can begin tweaking with its default values.

From my personal experience, the BSP parameters often have more significant results when instructed to use less memory (i.e. increased rendering times), than otherwise.

As mentioned earlier, to instruct mental ray to use less memory simply increase the default (i.e. 10) BSP Size values, and/or decrease the default (i.e. 40) BSP Depth values.


BSP2 method automatically maintains the balance between memory consumption and rendering times...in large complex scenes (i.e. more than one million triangles/polygons).

BSP2 also handles better the motion blur, instanced geometry and dynamic scenes. This is another major advantage over the default BSP method.

Note that when used in smaller scenes, BSP2 may take longer to render.
Users often press the hotkey 7 to determine the number of polygons/faces/triangles in the Max scene.

In scenes with over 1,000,000 triangles/polygons, it's often pointless to tinker with the default BSP values, as one would have random results most of the times.
It is safer,productive and more predictable to use the BSP2 method for scenes with over 1,000,000 triangles/polygons.




To have a visual representation of the BSP process, simply go to the mental ray processing parameters rollout. Under "diagnostics" parameters, enable the “visual” group:

This visual group consists of the following:

1-Sampling rate

2-coordinate space

3-Photon

4-BSP

5-Final Gather

The BSP visual diagnostics is divided by three different colours:

Blue, Green and Red.

Blue areas represent the lower areas of subdivision (i.e. less computation)

Green areas represent the middle areas of subdivision (i.e. intermediate computation)

Red areas represent greater areas of subdivision (i.e. high computation).

Production companies prefer to have a mix of all three colours in their diagnostics; which is an indication that mental ray is efficiently choosing the areas of the geometry to subdivide and otherwise.


To fine-tune the BSP values, simply use a nice/simple texture or colour in "material override" toggle at a small resolution (i.e. 500x500 pixels).
The "material override" function has been covered in detail, in our latest book.

A nice/simple texture/colour enables users to quickly determine if the BSP tree is rendering efficiently the geometry, or not.
It is a very common mistake for users to associate slow renders with BSP tree; even though is mostly attributed to other common factors (i.e. soft shadows; reflective surfaces; poor usage of mr proxies; etc).


Also, open the mental ray messages dialog, to monitor the Max and Average leaf size(i.e. triangles).

The Max and Average leaf Size(i.e. triangles) are the main values to concentrate on when tweaking with the BSP values.


The following images will help demonstrate how to control the Max and Average leaf sizes of the BSP tree:

The total amount of rendering time for the image below was was 36 seconds, with the depth value set at 40.

Note the balance of colours displayed in the visual diagnostics.
As previously mentioned, this would have been the ideal scenario for some production companies.
The red areas are in fact the areas of greater detail in the 3Ds Max scene.
The current BSP values are enabling mental ray to balance efficiently between memory consumption and speed.












The total amount of rendering time for the image below was 42 seconds, with the depth value set at 56.

The average leaf size(i.e. triangles) has decreased significantly, which is great however, the leafnodes and the bsp size (kb) have increased substantially .

High depth values often equals slower pre-processing times, as mental ray will take longer to travel(i.e. casting/hitting rays onto the triangles)through the tree depth.
This also equates to "fatter" leafnodes and bsp size(kb).


As mentioned earlier, some production companies prefer to have a good balance (i.e. low numbers) between the average leaf size, leafnodes and bsp size(kb).
The reason behind it, is that, over a number of frames there will minor losses of memory during the rendering times, which will contribute to better overall rendering times.

The default BSP values(10/40) were much closer to this principle.










The total amount of rendering time for the image below was 35 seconds, with the depth value set at 36.
If one was to follow the production companies principles/ methodologies, then the current depth value would have been the best so far (i.e. depth=36).
The average leaf size (i.e. triangles) value is reduced, along with the leafnodes and the bsp size (kb).
It is worth noting that, although the average leaf size is not as low as 8(i.e. with previous depth value set at 56); it is now lower than 27; coupled with the leafnodes and the bsp size (kb) numbers decreased substantially.

The overall average is very satisfactory now.






Next, we are going to tweak with the BSP Size values

The total amount of rendering time for the image below was 36 seconds, with the size value set at 10




The total amount of rendering time for the image below was 35 seconds, with the size value set at 20





The total amount of rendering time for the image below was 35 seconds, with the size value set at 5




As previously mentioned, size values often have little significance in the total amount of rendering times.




I hope this gives some insight into the BSP parameters.



The Practicalities



More often than not the default BSP value of 10/40 works best, for small/medium scenes (i.e. less than a million triangles).

The general rules are: When running out of memory, reduce depth values, or switch to BSP2 ray tracing method.

When equipped with enough memory to spare, increase depth default values (i.e. 50+).



In the nutshell, less memory usage equals to increased total rendering times, and less render crashing possibilities.


To further help you with practical rules, the following principles have worked for me most of the times:

BSP Depth values for computers with 4GB of memory or higher:

Small scene: 33 or lower

Medium scene: 33 to 50

Large scene: BSP2 or 50/higher



BSP Depth values for computers with less than 4GB of memory(i.e. memory issues):

Small scene: 53 or higher

Medium scene: 33 to 48

Large scene: BSP2 or 33/lower


If using motion blur, instanced geometry and dynamic scenes, then BSP2 is recommended.


The mental ray “geometry caching” function sometimes discards any pre-cached geometry when one closes the 3Ds Max program.
Therefore, it is imperative to cache the geometry again every time the Max file is opened; especially when using the default BSP Depth values.
Check if the “geometry caching” function is greyed out.
Caching the geometry in fact has more significant impact in reducing the rendering times than increasing the default BSP Depth values.

As explained in the book, to cache the geometry, one requires only to first enable the "geometry caching" function; followed by rendering a draft image with/without the pre-saved FG map saved(in fact one can stop the render once the translation process is finished and the render kicks in).

There is only a hand full of specific scenes where changing the default BSP values as mentioned earlier will massively reduce the rendering times however, BSP values will help handle the geometry more efficiently.

From the outset, there is no way of pinpointing the scenes that will benefit most from certain BSP values, without having to go through the lengthy process of fine-tuning the parameters.




To significantly/massively improve the rendering times, one should focus mainly in the techniques highlighted in the our book:

1-Cache final gather map files

2-Cache the geometry

3- Reduce the number of lights casting soft shadows, if many in the scene (i.e. have few key lights casting shadows, and the remaining lights should only illuminate the scene without casting shadows). This technique will reduce the rendering times by half.

4-Reduce the global reflection/refractions parameters if there are too many reflective objects in the scene (i.e. max trace depth= 3; max reflections=1 or 2; max refractions=3). These values should be reduced with caution, as very small values of "depth" and "refractions" may cause artifacts(i.e. black blotches).

5- If using displacement maps, reduce the global max subdivisions from the default 16k to 1k or less. Note that values lower than 256 may not look very accurate, depending on its proximity to the camera.

6-Have the correct sampling quality (i.e. maximum samples per pixel value should not be higher than 16)

7-Enable the strip network render;if not rendering animations(i.e. sequenced frames).This subject is also covered in detail in our latest book.


8-Finally, if using proxies use it with BSP2 and ensure to have instanced copies as oppose to just copies in the scene.
Even you have too many different sets of vegetation and/or geometry. Attach all these different sets into one or two separate meshes first, and later convert them into proxies.

If all instanced proxies in 3Ds Max come from one mesh, mental ray uses memory only once to load the proxy.
If one has two different sets of instanced proxies from two different meshes, mental ray uses the memory twice; and so on and forth.

One should avoid having mental ray use memory more than 2 or 3 times, especially when working on a 4GB of Ram machine or less.
The ideal situation would be to have mental ray use memory to load the proxy once/or twice only.

Note: Finally, in very extreme and difficult cases of memory loss, one can additionally enable the "use fast rasterizer (rapid motion blur) function, from the "rendering algorithms" parameters.
This rendering method will bypass most mental ray memory issues.

It is worth noting that although very powerful, this rendering algorithm disables some of render elements. To override this, simply render the file output to an EXR file extension type, provided one has the material IDs/Object IDs,etc, originally enabled in 3Ds Max.

If facing difficulties extracting these EXR passes in Photoshop,After Effects,etc;simply switch your 3Ds Max back to standard mental ray rendering algorithm when computing your AO pass, and enable your rendered elements again; with cached FG at a very low res to render the final output in higher res.


After all the above tweaking the final image below (i.e.4000x3062 pixels)went from 28.09 minutes to 7.08 minutes.






For more information about tackling memory issues,please check my other posts in this blog:


mental ray_the usual suspects:Displacement and proxy errors



Increasing the paging file to override Memory issues



.I hope you have found this article somehow useful!



My 3D Portfolio:



More tips and Tricks:

Post-production techniques

Tips & tricks for architectural Visualisation: Part 1

Essential tips & tricks for VRay & mental ray

Photorealistic Rendering

Creating Customised IES lights

Realistic materials

Creating a velvet/suede material 

FoxRenderfarm

www.arroway-textures.com 

Renderpeople

Gobotree

11 comments:

  1. Really Interesting, I never messed with BSP before. I have an 8GB machine at home , so if I have a scene with over 2 million polys and I geometry cache it, would you ignore toggling the BSP settings and jump straight to BSP2? Thank you for covering this, I haven't seen much about this topic.

    ReplyDelete
  2. Hi Felice,

    Yes, I would switch to BSP2 without hesitation.

    Moreover, when pressing the key “7” to assess the scene, the “triangle count” statistic may not appear on the viewport by default.

    To view it, simply go to the main toolbar and click on the “views” menu; on the dropdown list, choose “viewport configuration” to open its dialog box; under “statistics” parameters, check the “triangle count” function.

    The reason for this is to easily pinpoint "teapot in a stadium" situations (in CG theory); whereby the user may have a Max scene consisting of a 3D stadium containing 100.000 triangles and 2.000 polygons, and a small teapot in the same scene containing 5 million triangles with 10 polygons.

    If the "triangle count" function is not enabled in such situations, most users would have mistakenly opted for the default BSP method as oppose to BSP2, by assuming that there were less than 2 million triangles in the Max scene.

    Regards,

    Jamie

    ReplyDelete
  3. One last note: Global Reflections/Refractions parameters:

    As mentioned earlier, it is general practice to reduce these values in situations where there are noticeably numerous reflective surfaces however, there are exceptional scenes where only one sheet of glass (apparently) may cause a scene to run out of memory...and possibly crash.

    Such exceptional errors occur when the default global reflections/refractions are untouched (i.e. max. Trace depth 6; max reflections 4; Max refractions 6).

    Most experienced users wouldn’t normally tinker with the above values if there aren’t many reflective surfaces however; if there is one transparent reflective surface(i.e. glass) containing other glass type surfaces behind , it may cause the render to crash!!

    Even experienced users may mistakenly attribute this type of error to BSP values and other unrelated problems.

    The rule of thumb is to look at the exact area of the frame buffer where the last rendered bucket crashed at, in order to determine the nature of the error.

    Regards,

    Jamie

    ReplyDelete
  4. Finally, in very extreme and difficult cases of memory loss, simply enable the "use fast rasterizer (rapid motion blur) function, from the "rendering algorithms" parameters. This rendering method should bypass most mental ray memory issues.

    Ta

    Jamie

    ReplyDelete
  5. Pedazo de articulo, gracias por que me servira de gran ayuda. Suerte

    ReplyDelete
  6. Thanks for the great breakdown on BSP settings. This has me out a lot trying to figure out what is BSP and what is it's purpose.

    Thanks,
    Nick

    ReplyDelete
  7. very nice article Jamie Cardoso. Great help here. The last image rendertime was unbelievable. Can u please share which settings and values were tweaked to decrease that much render time.

    ReplyDelete
  8. Jamie please be kind to share which settings were used to decrease the render times in that last image...am a newbie to 3dsmax

    ReplyDelete
  9. Hi Team,

    It is already mentioned in the article:

    BSP size:10
    Depth:36

    In addition I have also used the strip render with cached fg; cached geometry, etc. Please read the entire article and its links.
    It is worth mentioning again that for scenes with more than one million triangles, one should choose the BSP2.

    Regards

    Jamie

    ReplyDelete
  10. Thank you so much Jamie Cardoso

    ReplyDelete