# Which is faster: A math node tree or an OSL shader?

In terms of render times for Cycles materials, which is faster? A material node tree containing many (~20-50) math nodes or a single OSL shader script node performing the same calculation.

• OSL isn't supported on the GPU, so I'd say nodes are faster if you have a GPU to render on.
– gandalf3
Jun 12 '15 at 18:55
• Thanks, @gandalf3. I usually use large image textures so GPU isn't an option for me. But that OSL limitation is an important one. Jun 16 '15 at 13:50
• I would still be interested in the answere. Jul 28 '16 at 11:48
• This question has no real possible good answer. because it depends on what you want to do in OSL or cycles OSL scripts can be large and node systems too. so in comparing apples and pears, we cannot tell anything about weight if we don't know their size. Sep 27 '16 at 22:16
• why not simply test it ?. Dec 12 '17 at 7:51

Answer is hard to give, so I will rephrase the question to will an OSL script node eventually be faster than an SVM node graph with math nodes. The answer to this question is yes. Then the next question, when will this benefit happen. this one depends on many parameters. Would I recomment to migrate to OSL script fore solely this reason? No.

I did a test on this subject. As GPU depends on many other stuff I will stick to a SVM CPU vs OSL CPU comparison.

Specs of my test machine:

• Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz (8 cores)
• Running on Linux
• Using official Blender 2.79 release

Test scenes

I constructed 5 scenes for this test.

SVM-0: SVM without the math nodes

SVM-math: SVM with the math nodes

OSL-0: OSL without the math nodes

OSL-math: OSL with the math nodes

OSL-scipt: OSL with the script node

The scene uses around 12 math nodes. I used the emission shader as it will not scatter new rays.

SVM-0  SVM-math | OSL-0  OSL-math OSL-script
------ -------- | ------ -------- ----------
22.90  25.86    | 25.47  27.70    26.05
22.87  25.80    | 25.40  27.64    26.01
22.86  25.84    | 25.38  27.68    25.99
22.92  25.84    | 25.42  27.65    26.04
22.91  25.79    | 25.44  27.63    26.04
-----  -------- | ------ -------- ----------
22.892 25.826   | 25.422 27.660   26.026


What is important in these numbers: - SVM average time in the math nodes is 2.934 - OSL average time in the math nodes is 2.238 - OSL average time in script node is 0.604

SVM math nodes scales linear. Meaning adding more math nodes will add on average a constant performance penalty.

There is a positive effect on using script nodes. Where is the theoretically break even?

((26.026-22.892)/0.604)*12=62.2649


But different scenes will have different results. For example: the test scenes are constructed that the shader is full screen. Normally you have many different objects and materials in the scene. So let's calculate the number of samples that are needed.

1920*1080*128*62.2649 = 16526399569.92 # resolution * samples * breakeven


On my machine the benefit would happen around 16,526,399,569.92 samples that pass though a node graph where 62 mathnodes were replaced with a single script node.

Note: on different machines different things will happen and that can influence the results.

There are also other stuff to be aware off: SVM codebase is fully shared between CPU/Cuda/OpenCL. As GPU's don't have the ability to have a stack the cycles developers came up with an idea to implement a fixed stack that could compile on all devices. This stack is limited to 256. When node graphs get too large SVM will not become an option anymore so the artist might want to switch to OSL.

Would I still recommend it?

No. reason: other materials setup have other cost penalties in OSL. So this theoretically benefit in practice might be much higher. So for this reason I would stick to SVM.

For most use cases, OSL will be faster because it is compiled.

• can you provide any examples? the math nodes are compiled as well, so im wondering how you draw that conclusion. Apr 1 '17 at 6:10
• Math nodes are not compiled, they are interpreted and stacked. Depending on the function you're executing, you'll get different results using SVM or OSL (both in cpu). OSL has the particularity that it optimizes calculations whenever they can be optimized, but in SVM every node is calculated even if the end result doesn't depend on the node's result. Feb 11 '18 at 16:33

It depends on the specific case. Of course OSL is powerful tool for pattern generation in comparison with cycles' build in math node but you should realise that it doesn't support GPU rendering and it also increases your render time if you use non .tx format textures.

ps. Remember that sometimes it's much faster to write a couple of lines of code instead of making a large node tree.