Implementing Ray Tracing in Python

Talk 30 min

Petras Zdanavičius
Ray tracing is a rendering method. It simulates the physical behavior of light and it allows to achieve generation of very realistic images. In this talk I will show an old-school CPU based Python implementation. Python is relatively slow programming language so a ray tracer written in Python is also going to be slow. I will show several (some of them hacky) tricks to speed up CPU intense calculations.

Transcript

Read transcript

[Music] ray tracing in one weekend it's a free book it's available online and i start doing that reading it and implementing examples uh but uh that book recommends use c plus plus but instead of that i was using sick and it was a lot of fun and by that sentence alone you can guess that i'm very fun and parties so i was doing that it was fun and i'm python programmer so i i was wondering is it insane to do that in python is it insane to suggest other python programmers to try to do toy ray tracer in python and this is how this talk was born so demo time uh let's start with a zika example so i'm generating and okay so this is very impressive not exactly but a little bit of impressive so what you see is three spares one is green one is mirror and one is so big that it looks like a plane and it's uh it's physical accurate lighting not really but sort of good enough so yeah this is the version and let's go to python version [Music] it's gonna take a while so let's continue slides so what [Music] pretend that sky is a light source if especially if it's cloudy like everything and light sources emits light it bounces all around [Music] [Music] everything is vector math in this problem so we have this what's the go to solution to speed up add slots close right so it's gonna be x eat quick set let's do that for a as well it's gonna be big and there let's try to run where is my terminal okay running and running a little bit a little bit faster not order of magnitude so still not good enough for me what we can do also i know numpy is fast right let's use numpy [Music] we're gonna not use this free and instead of that let's return way okay let's try now oh it's fail okay so i need to i need to actually place all x's too all these to that and all that's the second number of array let's run now oh no it took a lot of time so numpy isn't that fast is it uh there's a problem you're not supposed to use a numpy with very small arrays because the overhead of python will gonna hurt performance so let's go back to vector we need to go restore stuff so it's gonna be that right one is gonna be this and zero gonna be x is working okay be back into working let's try yesterday i learned about my pick let's try it all right commands and i think it's compiled yeah we have it so let's do that are you launching the compiled i'm not sure that because it should have compiled into uh transpiled into c compile and executable file so let's move look at this [Music] i was doing that means main food but the reason of course is i don't know don't use typing so that could be a reason but yeah so i was stuck at this moment i didn't know what to do and the last attempt to survive was to use pie pie and let's try pie pie boom nice we can work with that let's add a lot a little bit bigger resolution still reasonable a lot of artifacts though right so let's add more samples per pixel sorry samples per pixel is that basically a number of rays for each pixel it's so it's per edge so it means like samples raised per uh per pixel but what i'm two is two point two multiply by two so it's four actually okay okay so okay i'm more i think we have time we can one stop we can debug your code it's a little bit boring to look at this number but it's faster enough oh yeah beautiful it's possible like it's reasonable to wait 12 seconds you'll you wait for git up actions for half an hour twelve seven seconds is good enough to actually have this loop of change code look at the results change code or look at the results so it's possible it's possible bye bye thanks to pipe i and we can i actually do even better by by cheating even more so we can call zig from python you should have expected the propaganda i think so let's use c types okay so we don't need array anymore we don't need to call this we need to load load our library so it's going to be slab let's say it's she types is it or i think it's this one it's sick it's the ground slip load library so the clip what we're gonna let's look at the code so there we are exporting and as you can see i cheated a little bit and zip i'm using threads i'm making zip look better than it is actually anyway we're gonna call this draw frame [Music] it's gonna be bad i need me yeah okay this so draw fame i'm gonna pass this later now what i actually hold have this i think it's when that i don't need this now i need to pass a pointer there is a pointers and c type so what's okay it's gonna be white pointer i think nobody's going to complain so screen surface i'm using pig pie game and pie game is wrapper on sdl and this a secret variable how to access actual like memory position of the surface it should work i'm not sure but we have time for debugging now something is not defined okay i don't need to bullet anything yay it took 1.7 seconds nice as well let's go back to slide so wrong lessons to learn from this talk do not you see python always use pi pi no do you see python there's a problem with python language not problem but a in fact see python doesn't have standard reference it has reference implementation which means that anything else is catching up so you see python and try others if it doesn't work for you the second wrong lesson to learn is do not use numpy poor python is faster of course not numpy is faster but there is use case where it's useful when like having thousands of small objects and pretending to use numpy is a wrong way to use the tool and the third lesson with wrong lesson which is gonna for me be harder to dispute is do not use python use like zig c c plus plus or anything like that popular that became popular five minutes ago so there's a reason why python is the most popular language in the world programmed with i use python professionally like for 10 years and i always complain that it's slow because i'm quite negative person quite complainer i would call myself but in my career there there was like three to four times when it actually affected me that python was low otherwise it was just fine and easy to use nice language right so do use python there's nothing wrong with python until it is then you're something else right so good lessons uh i was really really impressed by pie pies just in time compiler it's almost the same as it managed to generate really really fast up codes and generate rate tracer image in really fast time so it's really good i'm really impressed kudos to the team and numpy is really good but you need to know how to use it and what what are downsides of mumbai and where you can burn yourself and also the most important lesson it is possible to implement a toy ray tracer using python and i highly recommend to try that so that's it that's my talk if you have any questions i think we have time so please do [Applause] like yeah you just simply mentioned that the tracing is on from the eye yeah to the sun but like usually the light comes to the eye yeah so that goes how do you do that mathematically like in platforming what what it didn't follow how you converted it so you need to read a book a book but to short versions you start you accumulate everything so you start with a black hole so at the beginning your ray has black color let's say yeah you shoot the ray you you hit you hit the the green thing it's fair at this point and you mark that down and you shoot another one it goes to infinity you you need you know that you hit hit the sky and now you trace back so you go back the sky let's say full white you go back you see that you you know your trouble oh sorry you know you know your path loret takes so you you can go back as well and you can calculate like sky is pure white it hits you actually it's fine there will be more interesting questions i'll just test okay [Music] there were these artifacts on the the random right yeah is there an easy to understand reason why they tend to coincide with where the shadows would be correct yes i think so so we can let's look at so there are artifacts and and and the shadows which means a lot of rays hit the ground from the underneath of spare spheres but some hit from various other places and it's marked as wide so there's not enough enough rays if we generate more rays the chances are that this will start to disappear but then we can not hit like 30 minutes per talk probably in the dark area there's just uh you mentioned threads in zero why not use green lights in python and see how how it feels i'm not that good at python i i have never used green lights at all so right now you just loaded the one frame right so yes have you tried like launching a game with a little bit rate racing it's all done so what i'm doing is old school and what what like offline rendering solutions would do like blender maya 3dx max current computer gpus have a special model for ray tracing so they can generate that in real time i don't know exactly how that like they do have a special unit to generate ray tracing so it would blow my examples away so thank god that my computer is too low too low then i cannot run that but yeah this real time uh they're gonna try to sell you new gpus with the real time ray tracing soon so be prepared to pay a lot of money or ignore that but the uh real time rate racing is a the thing about getting more and more popular yeah can you go back to the image with the sphere and the rain okay uh yeah can you hit it with the rain all right i'm so proud of this okay so yeah so you said that this pixel would be green right yeah shouldn't it be white because the ray hits the sky yeah but uh but it only hits sky after bouncing out of also so we do i do have he goes back back so okay i thought you traced back from the last object or not maybe yeah i just don't know but i thought that you would trace back from the very uh last object so there is no last object last object is the sky and uh when it you can do it in a other way but it it's easier to understand that so the white so you have a the vector your color is a vector so it's one one one you from the sky you travel to the sphere sphere like takes away the blue and red wireless so now you have zero one zero and you pass that to the pixel to camera and this is how it becomes green and mirror just reflects especially perfect mirror which there is no in the world but you can implement that in computer so it would have been white if the sphere was in green so do i have that would but if you notice from this picture sky is slightly blue and also this various effects of environment so the plane underneath is this gray and and i think there's some problems with displaying stuff on this screen but the mirror mirror just displays what it gets i'm very i don't feel very comfortable defending physics especially my buggy implementation of physics so sorry is that good okay thank you if you have more questions ask personally and think about [Applause]