Optimizing StreamFX in your OBS Scenes

Real-Time Shader Optimization

Shaders are one of the features in StreamFX that allow you to do so much cool stuff – and at the same time mess everything up. The following is a list of things you should do:

  • Avoid the use of integers unless absolutely necessary. Integers have a significant overhead in pixel and vertex shaders, and you should always opt for floats instead. While the math might end up slightly more complicated, it will run faster than integer math.
  • Prefer unsigned integers over signed integers. Unsigned integers have a smaller overhead than signed integers, but they are still on the list of things to avoid using. If you don’t need an integer value to be less than zero, use uint!
  • Manually unroll loops. Automatic unrolling often produces functional but inefficient code, which can be avoided by manually unrolling. In the ideal case put the content of the loop into an inline bool myfunction(...params...) {...code...} function which returns true if the loop should be interrupted – allows for easy unrolling.
  • Render at a lower resolution. Not many shaders actually need to be rendering at 100% of the parents resolution – many actually look perfectly fine at 75% or even 50%. Some can even look nearly identical at 25% – experiment with this to see what works for you.
  • Group mathematical operations by what they do. Multiply next to multiply operations, additions next to additions, subtractions next to subtractions. This helps the shader transpiler generate more efficient code, and can bring performance boosts of up to 10%.
  • Avoid excessive use of if, for and while. Ideally you want your arguments to be known at the time of compiling, but that is not always possible. So in those cases you should keep your branching to the minimum possible – either by manually unrolling loops or by adding techniques to select features.
  • Don’t calculate everything in the pixel shader. Not all calculations need to be done in the pixel shader, for example calculating UVs in the vertex shader and directly using that value as an input to a texture sampling command allows the compiler to optimize the sample to a better location. This can get you around 20% extra performance.

About Xaymar

A programmer in his mid-twenties, skilled in various programming languages.
Bookmark the permalink.

Comments are closed.