Replies: 3 comments 3 replies
-
I'll look into it. Realistically we should touch on how to get your ray tracing to go quick. But it's never been a priority. |
Beta Was this translation helpful? Give feedback.
-
See https://stackoverflow.com/questions/8385457/should-i-pass-a-shared-ptr-by-reference. Passing by reference is faster, but because it bypasses the functionality that a shared pointer was supposed to serve in the first place. The original code allocated most objects and then never freed them. That works fine, as long as you're rendering a single image and then going home. It doesn't work as well if you decide to extend to multiple frames (rendering an animation, for example), or setting up some kind of service. Rather than impose some kind of lifetime management, we decided to use The larger issue here is the intent of the source code. This is absolutely positively not production code. It's certainly not the way I would implement my ray tracer, even as a hobby. The code as it stands is primarily expository, meant to be clear and unambiguous to all readers, including those who are barely familiar with C++ and like it that way. Another issue is "leaving enough meat on the bone" for the reader. Part of the joy of writing your own raytracer is adding your own features as you wish. It could really use some command-line switches, for example. It can definitely be sped up, and significantly. All of that said, simplicity will win every time for book source. I'm glad you're changing things up in the pursuit of faster renders -- have fun and let us know how it goes! |
Beta Was this translation helpful? Give feedback.
-
By the way, since the primary benefit of |
Beta Was this translation helpful? Give feedback.
-
I managed to reduce render time of "theRestOfYourLife" example from ~42 to ~35 secs with the following modifications:
(the code below is only for a quick proof of concept, it's not the way I would leave it)
1. avoided allocation in ray_color function:
Also had to change underlying classes to store ptr-s, or references (in hittable_pdf and mixture_pdf) instead of shared_ptr-s. (This might not be the best solution, because it needs the user of the mixture_pdf class to know that the lifetime of the passed in objects is not prolonged by mixture_pdf object.)
2. reduced parameter count of ray_color function by putting it in a class:
(world and lights could have stayed shared_ptr-s)
The performance gain with this comes from avoiding the costly (recursive) shared_ptr pass-by-value issue which could have been avoided also by passing it as shared_ptr&.
If I measured correctly, changing the original shared_ptr pass-by-value to shared_ptr pass-by-ref in ray_color function resulted in roughly 1% less render time.
Beta Was this translation helpful? Give feedback.
All reactions