Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Improving raytracer performance

I'm writing a comparatively straightforward raytracer/path tracer in D (http://dsource.org/projects/stacy), but even with full optimization it still needs several thousand processor cycles per ray. Is there anything else I can do to speed it up? More generally, do you know of good optimizations / faster approaches for ray tracing?

Edit: this is what I'm already doing.

  • Code is already running highly parallel
  • temporary data is structured in a cache-efficient fashion as well as aligned to 16b
  • Screen divided into 32x32-tiles
  • Destination array is arranged in such a way that all subsequent pixels in a tile are sequential in memory
  • Basic scene graph optimizations are performed
    • Common combinations of objects (plane-plane CSG as in boxes) are replaced with preoptimized objects
  • Vector struct capable of taking advantage of GDC's automatic vectorization support
  • Subsequent hits on a ray are found via lazy evaluation; this prevents needless calculations for CSG
  • Triangles neither supported nor priority. Plain primitives only, as well as CSG operations and basic material properties
  • Bounding is supported
like image 549
FeepingCreature Avatar asked Apr 22 '09 16:04

FeepingCreature


People also ask

Does Ray Tracing use VRAM?

With RayTracing active and DirectX 12 instructions, it can consume up to 18.5GB of VRAM, which is nonsense. Currently on the market, the only one that has more VRAM than the game consumes is the NVIDIA TITAN RTX.


4 Answers

The typical first order improvement of raytracer speed is some sort of spatial partitioning scheme. Based only on your project outline page, it seems you haven't done this.

Probably the most usual approach is an octree, but the best approach may well be a combination of methods (e.g. spatial partitioning trees and things like mailboxing). Bounding box/sphere tests are a quick cheap and nasty approach, but you should note two things: 1) they don't help much in many situations and 2) if your objects are already simple primitives, you aren't going to gain much (and might even lose). You can more easily (than octree) implement a regular grid for spatial partitioning, but it will only work really well for scenes that are somewhat uniformly distributed (in terms of surface locations)

A lot depends on the complexity of the objects you represent, your internal design (i.e. do you allow local transforms, referenced copies of objects, implicit surfaces, etc), as well as how accurate you're trying to be. If you are writing a global illumination algorithm with implicit surfaces the tradeoffs may be a bit different than if you are writing a basic raytracer for mesh objects or whatever. I haven't looked at your design in detail so I'm not sure what, if any, of the above you've already thought about.

Like any performance optimization process, you're going to have to measure first to find where you're actually spending the time, then improving things (algorithmically by preference, then code bumming by necessity)

like image 159
simon Avatar answered Oct 05 '22 17:10

simon


One thing I learned with my ray tracer is that a lot of the old rules don't apply anymore. For example, many ray tracing algorithms do a lot of testing to get an "early out" of a computationally expensive calculation. In some cases, I found it was much better to eliminate the extra tests and always run the calculation to completion. Arithmetic is fast on a modern machine, but a missed branch prediction is expensive. I got something like a 30% speed-up on my ray-polygon intersection test by rewriting it with minimal conditional branches.

Sometimes the best approach is counter-intuitive. For example, I found that many scenes with a few large objects ran much faster when I broke them down into a large number of smaller objects. Depending on the scene geometry, this can allow your spatial subdivision algorithm to throw out a lot of intersection tests. And let's face it, intersection tests can be made only so fast. You have to eliminate them to get a significant speed-up.

Hierarchical bounding volumes help a lot, but I finally grokked the kd-tree, and got a HUGE increase in speed. Of course, building the tree has a cost that may make it prohibitive for real-time animation.

Watch for synchronization bottlenecks.

You've got to profile to be sure to focus your attention in the right place.

like image 43
Adrian McCarthy Avatar answered Oct 05 '22 19:10

Adrian McCarthy


Is there anything else I can do to speed it up?

D, depending on the implementation and compiler, puts forth reasonably good performance. As you haven't explained what ray tracing methods and optimizations you're using already, then I can't give you much help there.

The next step, then, is to run a timing analysis on the program, and recode the most frequently used code or slowest code than impacts performance the most in assembly.

More generally, check out the resources in these questions:

  • Literature and Tutorials for Writing a Ray Tracer
  • Anyone know of a really good book about Ray Tracing?
  • Computer Graphics: Raytracing and Programming 3D Renders
  • raytracing with CUDA

I really like the idea of using a graphics card (a massively parallel computer) to do some of the work.

There are many other raytracing related resources on this site, some of which are listed in the sidebar of this question, most of which can be found in the raytracing tag.

like image 20
Adam Davis Avatar answered Oct 05 '22 18:10

Adam Davis


I don't know D at all, so I'm not able to look at the code and find specific optimizations, but I can speak generally.

It really depends on your requirements. One of the simplest optimizations is just to reduce the number of reflections/refractions that any particular ray can follow, but then you start to lose out on the "perfect result".

Raytracing is also an "embarrassingly parallel" problem, so if you have the resources (such as a multi-core processor), you could look into calculating multiple pixels in parallel.

Beyond that, you'll probably just have to profile and figure out what exactly is taking so long, then try to optimize that. Is it the intersection detection? Then work on optimizing the code for that, and so on.

like image 3
Chad Birch Avatar answered Oct 05 '22 18:10

Chad Birch