Page 1 of 7 123 ... LastLast
Results 1 to 10 of 67

Thread: Nanhua's Shader Library with source code

  1. #1
    Join Date
    Dec 2008
    Location
    Chengdu China
    Posts
    44

    Smile Nanhua's Shader Library with source code

    Hi, all

    (sorry for my poor English)


    Major features:


    1.Multidimensional Adaptive Sampling
    1.1 Regular grid – it use the regular grid rather than the KD-tree for neighbor queries, it is simple, fast and able to measure all of the neighboring nodes.
    1.2 Region detection – it can detect not only edges but also regions of high variance.
    1.3 Multidimensional adaptive constructing – the method for adaptive sampling could be extended for adaptive constructing, for example, to adaptively construct the lookup acceleration data structure for importance-sampling of the environment (Image Based Lighting).
    1.4 Multidimensional adaptive Quasi-Monte Carlo method – it has a suitable Quasi-Monte Carlo method for adaptive sampling, which can produce the (0, m, 2)-net in base 2.
    2.Image Based Lighting (IBL)
    2.1 Multidimensional adaptive constructing – as mentioned above.
    2.2 Importance sampling for the product of environment and BRDF (so far, only the Lambert BRDF is supported).
    2.3 Photon emission – it combines the algorithms for importance-sampling and emitting photon from the sky to provide an efficient photon emission approach.
    3.Volumetric particle rendering
    3.1 3dsmax particle system supported – and it could be extended to support other particle systems.
    3.2 Bounding Interval Hierarchy – it uses the BIH as the acceleration data structure to support varying particle size and 3D motion blur.
    3.3 Volume photon mapping – the algorithm for non-uniform volume photon mapping is adaptive and efficient.
    3.4 Photon illumination – it can store the primary photon from lights instead of directly sampling them to improve performance. It will be very useful for scenes with the flame light (see below).
    3.5 Flame volume/light – besides volume rendering it is also a real light which can trace shadow ray and emit photon from the space based on the position and radiance of the particles (for efficiency, it uses the multidimensional adaptive constructing method to construct the data structure for importance-sampling).
    3.6 Ray-marching volume mixing – a simple way to mix overlapped volumes is to average their respective results, but it can not provide physically plausible result for complex shaders which are based on ray-marching. This issue can be solved by ray-marching volume mixing, both of volume rendering and volume photon mapping are supported.
    4.A microfacet model for glossy sampling – in the real world, the effective roughness of a surface will vary with the incidence angle. This phenomenon could be interpreted and implemented by a principle which is based on the microfacet model, both of isotropic and anisotropic distribution are supported.

    Update (2010.10.10)


    1. 64bit support.
    2. The performance of IBL is much improved.
    3. For non-uniform media and motion-blurred particle volumes, the quality of photon illumination is much improved.
    4. For heavy particle volumes, the performance is improved in many cases.
    5. Fixed a bug of MDAS.
    6. Fixed an issue relating to hull objects.
    7. Fixed a potential infinite loop of BIH.
    8. The statistics of MDAC was incorrect, fixed.

    (If you have the previous version, the mi file should also be replaced.)
    Attached Files Attached Files
    Last edited by Nanhua; October 14th, 2010 at 05:28.

  2. #2
    Join Date
    Dec 2008
    Location
    Chengdu China
    Posts
    44

    Default

    nslib_dof


    nslib_dof” (Fig 1-1) is a lens shader which simulates depth of field (DOF). Its displayed name in the material browser is “DOF (NSLIB)”. The key difference between it and other similar lens shader is that it use the multidimensional adaptive sampling method to improve performance.


    The first three parameters are “plane”, “size” and “mode”. The usages of “plane” and “size” are same as “physical_lens_dof” (for convenience, “plane” uses positive values instead of negative ones). The “mode” controls the distribution of the samples, there are three possible values for it: 0 – Box, 1 – Gauss, 2 – Blade (it's a hexagon). The difference is showed in Fig 1-2.


    The rest parameters are all in the “Sampling” rollout, they control the adaptive sampling. Before you can understand them, we should first explore the theory of the multidimensional adaptive sampling.

    Fig 1-1:
    Fig 1-1.jpg

    Fig 1-2:
    Fig 1-2.jpg
    Last edited by Nanhua; July 16th, 2010 at 09:14.

  3. #3
    Join Date
    Dec 2008
    Location
    Chengdu China
    Posts
    44

    Default

    Multidimensional Adaptive Sampling


    Many applications in computer graphics rely on taking a large number of samples to get satisfied results, such as depth of field, area lighting and motion blur etc. Because taking many samples is expensive, adaptive sampling scheme was suggested.


    Paper [1] proposed a multidimensional adaptive sampling strategy which use KD-tree for storing and neighbor queries. Due to the irregularity of KD-tree, this method can only measure the variance of each individual node. In order to avoid under-sampling features near the common edge of two nodes, they generate samples in a region slightly larger than the node’s extent, but no guarantee can be given by this approach.


    Instead, we could use the regular grid to store samples, which is the quadtree in 2D and the octree in 3D etc. To maintain such regular data structure is easy and we can measure the variance of each square region which consisted of 2^D nodes (Fig 2-1). Because these 2x2 regions evenly cover the entire sampling domain and any feature which is larger than 2x2 area will cover at least one node regardless of its location, a guarantee can be given that all of these features can be detected by measuring the variance of each 2x2 region (Fig 2-2).


    My approach first constructs a regular grid of 2^min_level nodes, each node has one sample (Fig 2-3a) – min_level is a user-specified number, if (min_level % D != 0) the node count will be less than 2^min_level and the sample count of each node will be larger than 1 (Fig 2-3b). The distribution of the samples is a low discrepancy point set which will be described later, you can assume it is a uniform random sequence for the moment.


    Then, we travel the nodes, from left to right, top to down, measure the variance of each 2x2 region (2x2x2 region in 3D etc.), if the variance exceeds the user-specified threshold, the first node of the region will be labeled a flag f_probe and other nodes will be labeled a flag f_update (Fig 2-4), the flag f_probe means when we meet a node with this flag we should probe its neighbors to measure the variance of the region consisted of these nodes, the flag f_update means when we meet such a node we only need to update its state.


    The second time we travel the nodes, we insert new samples into the nodes with f_probe flag or f_update flag (in practice, it's done when we probe the f_probe node's neighbors), and update their state (the flag and function value etc.). If the node contains only one sample, we divide the node along the X-axis, if the previous sample falls into left side, we insert the new one into right side and vice versa. If the node contains 2 or more samples, it should have been divided along the X-axis, we compare the sample counts of left and right side, if the counts are not equal, we insert the new sample into the side which has less samples, if they are equal, the order of inserting could be determined by a random number. After divided the node alone the X-axis, the next time to divide the node we will alone the Y-axis (Fig 2-5).


    When the nodes labeled the f_probe or f_update flag have contained 2^D samples, we should begin to travel their sub-nodes. The flags of the sub-nodes will be written in this way: the sub-nodes of the node which has f_probe flag will all inherit the f_probe flag; the sub-nodes of the node which has f_update flag will inherit the f_update flag except the neighbors of the f_probe nodes, which will be labeled the f_probe flag instead (Fig 2-6).


    We repeat these steps until reach the user-specified max_level or there is no node has the f_probe flag.


    (For more technical details, please read the source code: src/Common/Lv0_Algo/MDAM/MDAS/)

    Fig 2-1:
    Fig 2-1.png

    Fig 2-2:
    Fig 2-2.png

    Fig 2-3:
    Fig 2-3.png
    Last edited by Nanhua; July 16th, 2010 at 09:16.

  4. #4
    Join Date
    Dec 2008
    Location
    Chengdu China
    Posts
    44

    Default

    Fig 2-4:
    Fig 2-4.png

    Fig 2-5:
    Fig 2-5.png

    Fig 2-6:
    Fig 2-6.png
    Last edited by Nanhua; July 16th, 2010 at 09:18.

  5. #5
    Join Date
    Dec 2008
    Location
    Chengdu China
    Posts
    44

    Default

    Let's go back to the nslib_dof.


    The parameters “min” and “max” in the “Sampling” rollout control the sampling rate per point. At each point there will be traced at least 2^min eye-rays and at most 2^max eye-rays. The parameter “step” specifies that each time we travel the grid 2^step samples will be added to each labeled node, increase it will force the shader to trace more rays. The parameter “threshold” determines whether a node in the grid need more samples, if the variance of a region exceeds the threshold, all of the nodes in the region will be added more samples. In practice, this threshold is modified based on the recursion level (multiplied by 2^level), if you increase the “max” parameter in order to trace more rays, you should also decrease the “threshold” accordingly.


    Fig 3-1 is a reference image, it is rendered by assigning 6 to both of the “min” and “max”, which will force the shader to trace 2^6 = 64 rays everywhere, the total of traced ray is 23,090,112. The Fig 3-2 is rendering by MDAS, the minimum sample count is 2^4 = 16 and the maximum sample count is 2^6 = 64 at each point, the total of traced ray is 16,767,616. Fig 3-3 is the sampling density image of Fig 3-2, there are many noise in it because shader can only determine the sampling rate within its own domain and doesn't have the power to affect the sampling rate through the image plane (it's controlled by mental ray exclusively). This issue could be alleviated by the super-sampling of mental ray, but if the quality of the result still doesn't meet your requirement, you could use a map to control the sampling rate, for example, we could blur the Fig 3-3 and use the blurred image as the control map (Fig 3-4). The Fig 3-5 is rendered in this way.

    Fig 3-1:
    Fig 3-1.jpg

    Fig 3-2:
    Fig 3-2.jpg

    Fig 3-3:
    Fig 3-3.jpg

    Fig 3-4:
    Fig 3-4.jpg

    Fig 3-5:
    Fig 3-5.jpg
    Last edited by Nanhua; July 16th, 2010 at 09:20.

  6. #6
    Join Date
    Dec 2008
    Location
    Chengdu China
    Posts
    44

    Default

    nslib_collector


    nslib_collector” (Fig 4-1) is an utility shader which collects the Arbitrary Output Variables (AOV) from other shaders and writes the collected data into frame-buffer. Its displayed name in the material browser is “Collector (NSLIB)”. A main usage of it is to display the sampling density of MDAS.


    In order to display the sampling density of nslib_dof, we should assign the “Shader List” shader to the Lens slot in the Renderer Panel > Camera Effects rollout (Fig 4-2) and add the nslib_collector and the nslib_dof to the List of shaders (Fig 4-3), nslib_collector must be the first one. Turn on the “diagnosis” parameter in the “Sampling” rollout of nslib_dof (Fig 4-4), turn on the parameters of nslib_collector (Fig 4-5). In the “Sampling Density” rollout of nslib_collector, there is a string parameter “frame buffer” which defines the name of the user frame-buffer to be written to (named frame-buffer is available since mental ray 3.6 and hasn't been supported by 3dsmax yet). If there is not any available user frame-buffer, the shader will write the value to the alpha channel (Fig 4-6). If you intend to modify the alpha channel, you should set the alpha contrast to 1.0 (Fig 4-7), otherwise mental ray will do many unnecessary super-sampling.

    Fig 4-1:
    Fig 4-1.jpg

    Fig 4-2:
    Fig 4-2.jpg

    Fig 4-3:
    Fig 4-3.jpg

    Fig 4-4:
    Fig 4-4.jpg
    Last edited by Nanhua; July 16th, 2010 at 09:27.

  7. #7
    Join Date
    Dec 2008
    Location
    Chengdu China
    Posts
    44

    Default

    Fig 4-5:
    Fig 4-5.jpg

    Fig 4-6:
    Fig 4-6.jpg

    Fig 4-7:
    Fig 4-7.jpg
    Last edited by Nanhua; July 16th, 2010 at 09:28.

  8. #8
    Join Date
    Dec 2008
    Location
    Chengdu China
    Posts
    44

    Default

    Region Detection


    Paper [2] pointed out that the MDAS will suffer from the curse of dimensionality and they proposed an adaptive wavelet sampling method which is free from this issue.


    The key for avoiding the curse of dimensionality is to sample the scene in low dimensional space and use a heuristic method to detect both of edges and the regions of high variance which are the projections of the higher dimension, add more samples near the edges or into the regions.


    Update (2010.10.10)


    In the previous version, I try to use a basic approach to implement the region detection but there is a bug of it, which causes a false appearance that it will significantly improve the quality. In fact, we don't need to make extra effort for region detection, the MDAS method described above is enough.


    Fig 5-1 is a scene contains only a sphere assigned the DGS material. Fig 5-2 is the same scene rendered with nslib_dof (“size” = 0.001, “min” = 4, “max” = 8, “step” = 0 and “threshold” = 0.01). Fig 5-3 is the sampling density image of Fig 5-2. As can be seen from Fig 5-2 and Fig 5-3, the glossy reflection of the sphere has been successfully detected.

    Fig 5-1:
    Fig 5-1.jpg

    Fig 5-2:
    Fig 5-2.jpg

    Fig 5-3:
    Fig 5-3.jpg
    Last edited by Nanhua; October 11th, 2010 at 14:08.

  9. #9
    Join Date
    Dec 2008
    Location
    Chengdu China
    Posts
    44

    Default

    Multidimensional Adaptive Quasi-Monte Carlo Method

    screenshot 6-1:
    screenshot 6-1.png

    screenshot 6-2:
    screenshot 6-2.png

    screenshot 6-3:
    screenshot 6-3.png
    Last edited by Nanhua; October 11th, 2010 at 14:11.

  10. #10
    Join Date
    Dec 2008
    Location
    Chengdu China
    Posts
    44

    Default

    Multidimensional Adaptive Quasi-Monte Carlo Method (2)

    screenshot 6-4:
    screenshot 6-4.png

    screenshot 6-5:
    screenshot 6-5.png

    screenshot 6-6:
    screenshot 6-6.png

    screenshot 6-7:
    screenshot 6-7.png
    Last edited by Nanhua; October 11th, 2010 at 14:12.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •