---
title: "Sampling"
canonical: "https://documentation.chaos.com/space/DOCSGUIDE/113290523/Sampling"
format: markdown
---
This page discusses the concept of sampling as it pertains to CG rendering.

 

 

## **Overview**

---

When a renderer renders an image, it determines the color of each pixel to produce the image at the specified resolution. The most basic pixel color calculation considers the material on an object and the light striking the object at that pixel. But within a single pixel there might be multiple colors from various objects or parts of a detailed texture, or even different brightnesses on the same object due to changes in object shape or falloff of light sources. The differences between these colors might be subtle or abrupt. If a pixel consists of multiple colors, some work has to be done to determine the best color for that particular pixel in the rendered image.

 

![image](media://064dac39-f885-4ea6-9f03-7b9069ba58bf)

*Multiple colors within a single pixel. What color should the pixel be? *

 

To determine the best color for an individual pixel, a renderer picks out some (but not all) colors from the pixel to help make its calculation. You can think of all the colors in the pixel as a big restaurant buffet; it would take too much time to try all the foods, so you just *sample* the foods by choosing a few. In the same way, the renderer *samples* colors from the pixel. Since the renderer looks within the pixel, we say that the renderer samples colors at the *sub-pixel level*. The algorithm used to choose the sub-pixel colors is called a *sampler* or *image sampler*. Different image samplers use different methods for choosing the sub-pixel colors to sample.

After picking the sub-pixel colors, the image sampler then performs calculations on these colors to determine the final color of each individual pixel. 

From this explanation, you can see that an image sampler performs two separate functions:

1. Sampling some of the sub-pixel colors
2. Averaging the final pixel color from the sampled colors

Naturally, you would think that the best approach for step 1 is a broad, random sampling of sub-pixel colors for every pixel. This is a valid approach for some scenes, and in the early days of CG it was the only one available. However, such an approach can be unnecessarily heavy on RAM and computing time; to assure an accurate pixel color, an enormous number of samples would have to be taken for each pixel. 

Instead, there are intelligent ways to cut down on the number of samples that need to be taken, which reduces computing (rendering) time. The entirety of the science of image sampling is devoted to getting the best possible pixel color with the least amount of time spent on sampling. Toward this goal, several image sampling approaches have been developed, each with its own advantages and disadvantages. There is no one image sampler that will work perfectly for every scene; the key to creating a beautiful, accurate rendering within a reasonable rendering time is choosing the image sampler that suits the scene you are working on. After learning a little bit about how these different types of image samplers work, you will be in a better position to choose the right one for your scene.

 

## **Sampling Basics**

---

Sampling follows a particular process. First, each pixel is divided into subdivisions. Then, within some or all of the subdivisions, a sample is taken at random.

The number of subdivisions is always a square such as 4, 16, 25, 36, etc. If you enter a value for a **Subdivisions** parameter, this value is squared to get the actual number of subdivisions for the pixel. For example, if you enter 16 as the subdivisions value, the actual number of subdivisions will be 16 squared, or 256.

 

 

In the last image, the harsh edge between the black and white areas is "softened" with gray pixels. In this case, the sampling process performed antialiasing where the black edge meets the white. Antialiasing is one of the main services performed by sampling, but not the only one.

In the example shown above, the sampler takes a sample from each subdivision. However, this is not always the way the sampler works. A more advanced algorithm will selectively choose which subdivisions to sample based on a variety of factors. By being selective about which subdivisions to sample, the sampler can devote more computing cycles to the subdivisions that need it. In other words, selective sampling will make a good rendering in less time than non-selective sampling, and if two renderings run for the same time period the one that uses selective sampling will usually look better.

Selective sampling is referred to as *adaptive sampling*, while non-selective sampling is called *fixed sampling*.

The images shown above show an example of fixed sampling with the Subdivisions value set to 2 (resulting in 4 subdivisions). In practice, this is a very low number of samples and it would not result in a very good rendered image. However, this number of samples is useful for illustrative purposes.

## **Fixed Sampling**  


---

The example above shows fixed sampling, where a set number of samples are taken per pixel no matter what colors are present in the pixel or its neighbors.

Fixed sampling has some disadvantages. While it is straightforward and predictable in the time it takes, a high number of samples need to be taken for each pixel in order to achieve an accurate result. And if there are large swaths of the same color, far more resources are used than necessary.

The image below shows the previous example, but with all-black or all-white pixels outlined in red. For these pixels, there would be no need to take a large number of samples to accurately get the pixel color.

 

![image](media://7729614e-e252-4bd9-b0da-daa854527f42)

 

## **Adaptive Sampling**

---

A more intelligent way of sampling selectively chooses the pixels from which multiple samples will be taken. Adaptive sampling is generally a more economic approach because it focuses the computing power where it's needed rather than on all parts of the image evenly.

The sampler first computes a small amount of samples from each pixel. Then, if the variance between adjacent samples it loo large, it makes more passes through the image and takes more samples from the areas that showed variance. If the variance between samples is small, the sampler stops taking samples from that area. The image below shows simplified version of this process, with only one sample taken on the first pass.

 

![image](media://181e3150-411f-450d-8f17-c9f6595b3884)