I'm trying to make a raytracer. So far, it works for phong shading and no other features (it doesn't do shadows yet).
However, it takes 500ms to render one scene.
So, so far, I've applied optimizations to use SSE, and that brought it down to 300 (not more because I don't use a w component in my calculations).
Finally, I decided to make it multi-threaded. This is obvious and easy to implement in raytracing.
My paticular thing uses something of the following code:
raytrace () {
foreach (pixel in width) {
foreach (pixel in height) {
//do calculations
putpixel (format, finalpixelcolor, x, y)
}
}
}
Now, all I have to do is make it parallel. So I decided that one thread would render every other line, and create a static variable to determine how many times raytrace has been called.
So
raytrace () {
static stride=-1; stride++;
foreach (pixel in width) {
foreach (x=stride;x<height;x+=2) {
//do calculations
putpixel (format, finalpixelcolor, x, y)
}
}
}
When I use multiple threads, it just simply doesn't work. If I have a HWSURFACE, then it gives me the first call, but the second thread returns only half of the pixels it requries (about).
SWSURFACE only returns the results from the first thread.
So, how should I go about this? Should I create a buffer of final pixel color's for one thread, and pixel colors for the other, and then stitch them together later into a surface?
Or is there a more SDL way to do this?