Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

In order to render images with as low noise and as fast convergence as possible, it is important to use samples that do not clump and do not leave large parts of the sampling domain empty.  We call such samples well stratified.

...

The "RNG" part of RixRNG stands for "Random Number Generator" even though the samples are not random at all.  The RixRNG class is basically just a wrapper around an array of per-shading-point sample contexts (SampleCtxArray) and an integer (numPts) specifying how many sample contexts there are in the array.  There is typically one SampleCtx for each point in a ray shading batch.  The RixRNG wrapper class makes it convenient to generate sample points (or new sample domains) for an entire ray shading batch with just a single function call.

Generating samples

There are six different functions to generate samples: 

...

By convention, Bxdfs call the DrawSamples?DDrawSamples1D(), DrawSamples2D(), and/or DrawSamples3D() functions.  These functions do not increment the sampleids.  This means that an Integrator calling a Bxdf has to do this incrementing after the Bxdf call – otherwise multiple bxdf samples will be the same sample value, ie. the same direction.  Similar for light sampling and indirect illumination sampling: the Integrator has to increment the sampleids after the samples have been used.  This is easily done by looping over all the sample contexts in a RixRNG like this:

Code Block
for (int i = 0; i < numPts; i++)
    sampleCtxArray[i].sampleid++;


Generating new sample domains (new patternids)

We need different sample sequences for each combination of pixel, ray depth, and domain (bxdf lobes, area lights, indirect illumination, etc.).  (If the same sample sequence was used for everything, there would be correlation between samples and the image would not converge.)  RenderMan internally selects sample sequences for pixel position (for anti-aliasing), lens position (for depth-of-field), and time (for motion blur), and sets up an initial patternid (bit pattern) and sampleid for camera ray hit points.  All sample sequences used by Bxdfs and light source sampling need to be set up in the Integrators calling them: separate domains for bxdf, light sources, and indirect illumination derived from the parent domains – and then new domains again at the next bounce (derived from those at the first bounce), etc.  This is accomplished by calling one of the NewDomain*() functions.  The NewDomain*() functions create a new patternid based on a hash of the parent RixRNG's patternid and a scramble bit-pattern.

...

Code Block
  SampleCtx* samplectxarray = new SampleCtx[numPts];
  RixRNG rng(parentRng, samplectxarray, numPts);
  RixRNG::Scramble scramble = static_cast<RixRNG::Scramble>(0x8732f9a1)
  for (int pt = 0; pt < numPts; pt++)
  {
    int index = shadingContext->integratorCtxIndex[pt];
    samplectxarray[pt] = parentRng→NewDomainsplitparentRng→NewDomainSplit(index, scramble, 4);
  }

Here the scramble bit-pattern is 0x8732f9a1 and the splitting factor is 4.

...

This section contains a few practical tips for how to use the samples returned by the RixRNG DrawSample* and GenerateSample* API functions.

Mapping samples

The samples returned by the DrawSample2D() and GenerateSamples2D() functions are in the unit square.  It is often necessary to map from the unit square to other domains.  Example: uniform sampling a disk with a given radius can be done by picking a radius proportional to the square root of a sample between 0 and 1, and an angle between 0 and 2pi, and then computing the xy position corresponding to that radius and angle:

...

Other common mappings map samples to the surface of a sphere, hemisphere, or cylinder, map samples to directions proportional to a glossy bxdf lobe, and so on.

Shirley remapping

We often need to combine a selection between sub-domains and generation of positions (or directions) on one of those sub-domains.  For example: select a light source and generate a sample position on that light source, or select a bxdf lobe and generate a sample direction proportional to that lobe.  

...

This method of using 2D samples to both choose between sample sub-domains and at the same time stretching the samples from the chosen sub-domain to provide stratified 2D sample points is implemented in the RixChooseAndRemap() function in the RixShadingUtils.h include file.

Path splitting vs distribution ray tracing

Path splitting and distribution ray tracing were mentioned above (in the description of the various NewDomain*() functions).  But when is it appropriate to use which?  A couple examples should provide some guidance:

...

Resort to distribution ray tracing if there is no fixed branching factor.  For example, if some specular ray hit points spawn 16 new sample directions but other specular ray hit points (perhaps with a lower throughput) only spawn 4 or even 1 new sample directions.  In this case there is no simple way to compute the offset into a combined sample sequence.  Instead the 16 (or 4) sample directions are stratified with respect to each other, but not with respect to any other samples for that same pixel.  (If only 1 new sample direction is spawned then there is no stratification at all.)

Uniform random samples

Non-stratified samples similar to e.g. drand48() can be obtained by calling the HashToRandom() function.  It computes a repeatable random float between 0 and 1 given two unsigned int inputs, for example patternid and sampleid.  Using the same patternid and continuously incrementing sampleid gives a sequence of samples with good statistical variation – similar to drand48().  The HashToRandom() function is repeatable (i.e. the same two inputs always give the same output), and has no multi-threaded contention (whereas drand48() has notoriously bad cache line contention, hampering multi-threaded performance).  HashToRandom() is located in the RixRNGInline.h include file.

...