GPU programming is becoming more and more accessible for programmers, and being a huge fan of Python, I wanted to test the limits of this. Could one write a performant renderer in Python? Also I’m lazy, and wanted to test some ideas for rendering algorithms in python.
So recently I came across this python package “Taichi” which basically let’s you translate Python code to run on the GPU. Pretty cool. My first try was to implement Peter Shirley’s “Ray Tracing in One Weekend” in this https://github.com/bsavery/ray-tracing-one-weekend-taichi. Which actually was quite fast for me to run. The language is a bit tricky to debug, but generally pretty simple once you wrap your head around the data fields (very similar to numpy).
Well, the next thing I thought was after translating the Ray Tracing in One Weekend, was “why not put this directly in blender”? Since it’s just Python code, you could have a whole render add-on in one codebase.
Here’s the result.
I’ll update this post as it moves forward, and this is a fairly early version that I wanted to share:
It will render Cycles scenes and use emissive materials for lights and all the other materials are treated as diffuse currently. There are some obvious artifacts with the geometry that I have to debug.
Currently the renderer is definitely slow. If I turn off updating each Frame it’s actually not bad, but there is zero optimization going on here. Some todos:
Possibly numerical precision, though I’m not sure. Also the tire transform seems off clearly. You can see this in the rear tail light too. I haven’t had time to really debug that yet.
This is one downside to this Taichi framework, because it translates Python code via LLVM into something that can run on the GPU. So debugging it is a bit more difficult as you really have to look at the translated code.
With regards to mesh processing. Absolutely. Take a look at the render/mesh.py file in the repo to look at how I read the blender mesh into a numpy array and then copy to a taichi field on the gpu. You’d just replace the “kernel” for taichi with some different code to manipulate the mesh.