From Filter Forge Wiki
Revision as of 15:52, 11 October 2010; view current revision←Older revision
| Newer revision→
This chapter of The DOs and DON'Ts of Filter Construction shows how to optimize filters using clipping. The method can help you construct much more efficient filters and build large repetitive filter constructions without much more processing overhead than what is actually needed to get to your visual goal.
So what does clipping mean? In rendering, clipping refers to an optimization where the computer only draws things that might be visible to the viewer. (Source). When you construct filters, things do not always work as intuitive under the hood as one might think. Samples may be fetched from subtrees even though they actually don't affect the final result in any way. We can prevent that by implementing proper clipping.
Clipping can be done in many different ways, but they all share the same principle: avoid rendering what is not visible in the end result by replacing the heavy content with a cheaper alternative. In Filter Forge 2 they introduced many new shape components which render very fast and can be positioned easily. They are all excellent for clipping purposes. The Threshold is also very useful since it will only fetch Low and Hi inputs when they are needed (the new If component will NOT work as it always seem to fetch all inputs).
Below we shall look at different clipping strategies, but first lets discuss some initial guidelines on when and how to implement clipping more generally.
Since clipping is an optimization trick and optimization in general will make things less obvious and complicate design, the first advice is to WAIT till your basic design is complete.
Second advice: keep track of rendering times! The whole idea is to make things render faster, but even the most simple clipping mechanism adds render time, so you need to keep things in balance. Always take note of the no-clipping render time vs. the clipped render time.
Thirdly, just because you don't see it, it doesn't mean that it isn't rendered. While it seem very logical to use alpha channels / opacity to control what is visible and what is not, it will not (and should not) stop transparent areas from being rendered fully. Think about that when you use Set Alpha etc. to block out things - there is a good chance that you can achieve the same using a clipping trick of some sort where you use a transparent background setting in your clipping component.
The Threshold component possess much more potential than just being used for simple threshold operations. Basically you can think of the threshold operation as a switch that based on the Source and Threshold settings selects either the High or Low input. To put it in logic, if Source > Threshold then output = High else output = Low.
The Smooth input allows you to specify a transition area where both High and Low are combined, but only for the transition area! Looking at the above picture (left side), we can see that respectively the red and blue sources are used based on the gradient. The low Smooth setting creates a short transition area between the two colors in the middle. Using the Smooth parameter we can thus do operations similar to the Blend, however with certain limitations (only Normal mode and no alpha channel based blending).
If you look at the above picture (right side), you can see how the Threshold is used as a switch or clipping mechanism between two Perlin Noise sources. A third Perlin Noise source is used for Source input, or said differently: it controls which of the two other inputs we want to use. The two noise sources used for Low and High are both using computationally costly settings, but the Source input noise is not. If we were to use a Blend for this combine operation, the blend would first fetch samples from both "layers" and then blend them. However using the threshold clipping trick like above, we effectively only fetch samples when they are visible in the end result.
The new Shape Components of Filter Forge 2 are ideal for clipping control. They render fast and only fetch the inner and outer region sources when needed. This behavior is also true for older FF 1.x components, but these can be harder to control shapewise and are in general also more complex, which is not ideal since we want the clipping to be lightweight.
In the images above we have some sort of sun composition representing the heavy subtree object we want to clip. Here a polygon can be used to create a roughly fitting clipping mask. In the image on the left we see a temporary blend of the clipping mask and the heavy source. I used this temporary blend to adjust the polygon parameters. In the right image we see the clipped result, which visually is no different from the source, but renders faster as the outside region of the polygon uses a transparent color from the polygon background instead of the transparent region from the heavy source. Simple and effective.
Note that this example illustrates the simplicity of shape clipping - in the actual filter there is a great chance that we could have done Cheap Source Clipping and get a tighter mask at no extra expense.
Building a Threshold Clipping Tree
In the section on Threshold Clipping we saw a simple example on how the Threshold acts as a dynamic switch that lets us choose what branch to use later on. In the above image we see how a stack of thresholds are used to construct a clipping tree, each adding in content based on the Threshold parameter settings. The different sources we see combined in the Result component are actually only in use in these areas (no obsolete sample fetching is going on).
This principle can be extended to as many steps as wanted, and the only added rendering overhead is that of the thresholds them selves. In the example above we see one common threshold Source, the Frame, and the different clipping areas are set via the Threshold parameter. This is not a vital part of the construction, however it is important to keep the computational cost as low as possible for the Source and/or Threshold inputs, as these are needed in order to evaluate wether to use the Low or High inputs. You may in some cases even be able to use the previous Threshold components output as Source input in the next.
Cheap Source Clipping
Cheap Source Clipping is about identifying an early step which controls the final shape of the rendering (or intermediate subtree rendering) and use that source in a clipping bypass after the heavier parts. This is probably one of the easiest ways to optimize an already finished filter, and if applied correctly it drastically reduce rendering time (in the example below we reduce a half minute rendering to just six seconds).
Consider Betis Night filter, which renders a moon with stars behind. Looking at the filter construction we can see 3 basic sub clusters: The moon part, the stars/space part and some positioning of the moon. The stars part renders quite fast, and the moon is a bit more heavy, however most of the moon rendering part of the filter is active during the whole rendering, even when there is only stars and empty space visible.
Now the moon shape is defined in a very early step by an ellipse, which then is processed with craters and what not to make the cool moon surface texture, and all that stuff going on after the ellipse is the heavy stuff. All this is being processed for the whole image and not just the actual visible moon.
Here we introduce clipping by inserting a Threshold with the base ellipse as source and the final moon result as High parameter (Threshold and Smooth set to zero). The Low parameter is simply a constant black color. This means that when we're not rendering the actual moon body, we will be using the fast black color replacement. There is NO visual difference at all, but we reduced the rendering time from over a half minute to just six seconds for that subtree! Thats something.