Okay, first of all, the context:
I am actually working on a 2weeks project about raytracing. Initially, I wanted to explore the topic and and try to get the basics used to render those beautiful images. I made a first attempt with local illumination (simple raytracing), then I came accross this small global illumination path tracer from Kevin Beason, which was, by far, more realistic, and I wanted to give it try. Anyway I didn't want my port to be so compact, so I splitted the code in several files, made some design and added a bit of object orientation, to have something more understandable.
Here's the link to the Github repository: smallpt-lua
For one thing, it works fine. I didn't pushed yet any example of use, as I am still polishing some stuff on the background, but it works flawlessly.
Problem is, it runs reaally slow.
So far, I have to admit that global illumination rendering is a really expensive task. Actually, the program take some inputs (scene, primitives, intial viewpoint), performs some quick camera setup, then processes each pixel to compose the the output:
Here is the main loop skeleton:
Code: Select all
for y = 1, height do -- height for x = 1, width do -- width for sx = 0,aa-1 do -- for anti-aliasing for sy = 0,aa-1 do -- for anti-aliasing for samp = 1, spp do -- for sampling : we need at least 16~25 samples/per pixels to have a clean output -- Processes pixel x,y, with possible recursive calls (around 5, in general) -- evaluate a partial radiance for each sampling, save it end end end -- cumulate all radiances to evaluate the final color for pixel x,y end end
But, I can't thing of using a table-less structure here (in other words, multiple return values instead), as the code would no longer be readable nor clean enough to me: functions passing vectors to each others will take lots of args, syntax would be a lot more verbose...
So Threads ?
Then I started thinking of using love.thread as a possible way to gain speed.
Actually, I have a very basic understanding of love.thread module and threads in general (and don't laugh at me, i can see you ).
First of all, why can't we pass tables from one thread to another ? (Well that's more of a general question, was just wondering why).
Second, I know that multiple threads can be created with love.thread module. And they can run in parallel. This is really nice because, in my case, I can make it so that each <x,y> pixel sampling would be processed in its own thread. Then i'll just have to pass the scene description, plus a <x,y> couple matching a pixel to be evaluated to a thread, and it will return to the main thread the color for that pixel.
Let's say that I have to render a 200*100 sized-image, which makes 20000 pixels to render.
Well, it doesn't mean I have to create 20000 threads, right ?
And even if I could, they woudn't run all at the same time ?
So how much maximum threads can I run at the same time ? This depends on the processor, right ? (and stop laughing at me... )
What would be the pros/cons of doing this, though ? Actually, what I've done so far whith love thread was loading resources in a separate thread, to preserve the framerate in the main thread. In this case actually, I will obviously have two ore more threads running, the main one for basic display, and the others for raytracing operations...But does this means necessarily a gain in terms of speed ?
I have some others questions left, but they can wait, actually.
Sorry for the long post.