Implemented Peter Shirley's Ray Tracing In One Weekend in Rust for learning purpose
Here Github repo, commits are done chapter by chapter so that anyone can follow it
Since I did it for learning Rust, the code might be off so, please feel free to give constructive criticism
Here the final result, Width: 1200, Height: 800, Sampling: 10, took ~14 mins to render Edit: The above render took ~14 mins because of running on debug mode, take only 3.5 mins on running with --release
Neat! Thanks for the book recommendation, that was a lot of fun.
I worked through the exercises in Rust, and then did some optimization. The same scene at 1200x800 with x10 sampling takes 8.5 seconds on my 4-core laptop. (I used Rayon to get nearly-free support for multiple cores; with parallelism disabled it takes 31s.)
Edit:@BlackGoku36, I read through your implementation, and I don't see a smoking gun to explain the performance difference, except for one thing: I notice that in sphere.rs you clone a Box<Material> inside of hit. That routine is probably the hottest piece of code in the whole program, so it might help to avoid allocating memory there. If you use Rc in place of Box you can return a new pointer to the same Material, avoiding a new allocation.
Also, make sure you're running it with --release .
@cbiffle Thank you for your advice.
Yes, I first ran it without --release (didn't know that before ) and then tried with --release(I saw it from other forum post before) the rendering time of that scene(1200x800 with x10 sampling) went down to just 3.5 mins and now I implemented multiple-cores rendering(Rayon) and with same scene and settings, rendering time went down to just 1.2 mins(My CPU is Intel Core i5-7400 3.00GHz (4 cores and 4 threads)).
Now this is speed i wanted from long time
My implementation is about 30x slower than @cbiffle's (90 secs vs 3 secs for the final image of RTTNW) . I have to figure out where I'm doing it wrong. I guess it is somewhere in the bvh implementation.
Edit: indeed there was an error in the axis-aligned bounding box hit function, now fixed. Now it is about 8 secs. There's still room for improvements for the bvh implementation.
Here are some exercises that might improve the performance of your renderer. As always, it is useful to get acquainted with your operating system's profiler.
Avoid allocating or cloning an Arc in any routine called from color.
Replace recursion with iteration in color.
Evaluate improving locality by storing copies of things directly instead of indirectly through Box or sharing them through Arc. The Material in a Hitable is the case that brought me the biggest speedups.
Use a faster, lower quality random number generator. rand::thread_rng is seeded from system entropy and intended to be cryptographically secure. I switched to rand::rng::SmallRng. The down side of this is that you have to pass it around.
Globally, try to reduce dynamic dispatch.
Try making "modifier" types into generic types. For example, convert FlipNormals into FlipNormals<O> where O is some kind of object. Implement Hitable for Box<dyn Hitable> and then you can write FlipNormals<Box<dyn Hitable>> if you really need the old behavior, or (say) FlipNormals<AARect> if you want compile-time specialization.
HitableList is an excellent candidate for that technique, btw. If you profile your program, you're likely to see it near the top.
If you do both of those, you might observe that a cube can be described as two pieces: a HitableList<AARect> and a HitableList<FlipNormals<AARect>>. In the book's final scene there are a lot of cubes, so removing the dynamic dispatch inside each cube's intersection routine will save cycles.
@BlackGoku36This project Is based off of ray tracing in one weekend and has a really clean way of implementing .obj files. You may also want to look into the tobj crate which points to another example ray tracer that does it. This is definitely my next step too.
Thanks for those link, I tried implementing it from Scratchapixel and Bheisler's Pathtracer but the result was really slow, it took whole minute to render low-poly standford bunny, but with that crate and implementation based on that project, the rendering now only take 2-3 secs
I decided to keep the tutorial(Peter Shirley's minibooks) seperate, so it is here now: https://github.com/BlackGoku36/RRayTracer