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.
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
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?
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.
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.