Fast Voronoi F2F1 in Blender Cycles on GPU and CPU

The voronoi noise texture in Blender Cyles is rather limited: it is only based on the distance to the nearest point while many interesting patterns need the distance to the next nearest points as well (or the difference F2 - F1). Also there is no way to vary the distance metric, everything is based on the distances squared while for example so called manhattan metrics yield squarish patterns that can be quite useful.

I did submit the path for review just now, if you're interested you can follow its fate here.

All of this can be overcome by using Open Shading Language, but the Cycles OSL implementation is limited to the CPU and cannot benefit from a much faster GPU. I therefore decided to try and implement it in the Blender source code and that went rather well (but see my rants below):

As you can see I added a second value output to the Voronoi node that yields the distance to the next nearest neighbour and added a dropdown to select the distance metric. Currently I have implemented the distance, distance squared (the default), manhattan and chebychev metrics, I might add the generalized minkowsky metric as well. The images show the F2 - F1 noise for the distance squared and manhattan metrics respectively:

The code works on GPU and on CPU but I only tested it on 64bits Linux (Ubuntu 15.04 to be precise) although there is no reason to believe it will work differently on antoher OS.

Can I haz codez?

The diff to the Blender source code as of 8/8/2015 is for the time being available via DropBox. If there is enough positive feedback I will try to get the patch accepted for inclusion in the source code. Note that the diff shows also some changes to files that have nothing to do with the node itself, notably device_cuda.cpp, which i need to tweak because although I have a GTX970 (= SM_52 capable) card, I didn't upgrade my cuda drivers so i needed to hack the source to restrict the cuda compiler to sm_50. This is irrelevant to the patch itself.

Some remarks about the code: coding this was not all pleasure and that is an understatement. No less than 8 files needed to be changed to alter a single node type. That isn't a bad thing in itself but there is duplicated code, a lot of redundant constants defined in different places and I could find no decent documentation on the stack based virtual machine (svm) that is used by Cycles. It took me way longer to fathom the ins and outs of the code than it took to write the code itself and I am still not 100% certain that I did not miss something. Blender is a wonderful piece of software but it certainly would benefit from a set of decent architecture docs :-)

1 comment:

  1. I'd love to see more versatile voronoi stuff happening in Blender, lot's of game textures require some good cracks! :D Aidy.