What is Raytracing?
In all modern forms of 3D rendering for display on a computer, the goal is to determine the color of every pixel on the screen as fast as possible. Raytracing is simply a method that can be used to do so. Currently, the most common method for rendering realtime 3D graphics is rasterization. There are fundamental differences between the way rasterization and raytracing go about determining pixel color.
With rasterization and raytracing, we start with geometry. Triangles to be specific. We have a scene made up of triangles and shader programs are used to determine the color at any given point on every triangle. With a rasterizer, we loop through every triangle and use math to project the triangle onto the screen. This is like taking a 3D scene, and flattening it out. We find out what pixels every triangle overlaps and save the depth values for later when we shade those pixels. We use lighting algorithms, texture maps, and the location of the pixel on the triangle itself to do the shading.
Unlike rasterization, raytracing starts with the pixels. If we draw a line from a central eye (or camera) position through each pixel, we can use math to determine what triangles this line (called a primary ray) intersects. For every triangle that our primary intersects, we save the position of the intersection. After all our geometry has been checked for intersection, we keep the intersection closest to the viewer (ignoring transparency for a minute). This process means lots of conditionals and branching in addition to the compute power required by whatever shader programs are used.
From here, like with rasterization, we can use shaders to determine the color of our pixel, but the input the shaders use can be other rays (these are secondary rays) that have been cast (or shot) from our saved closest point of intersection. These secondary rays can be used to do lots of things like look for shadows (shoot a ray at every light source and see if that light is blocked by something), and reflections (shoot a ray at the angle the primary ray would reflect from and start the process over again). Rays used to do reflection, refraction, radiosity, and other effects can end up generating a good number of secondary rays. The key advantages to the rendering quality of raytracing lie in secondary rays, but these are also what add the incredible complexity to raytracing renderers.
Calculating secondary rays is particularly time consuming, as not only do we have the same branching issues, but we are less likely to see speed up from grouping rays together into packets. Its easy to see that when we shot a lot of primary rays (say four for each pixel for some antialiasing), include a lot of bounces for reflective surfaces (lots of secondary rays becoming increasingly incoherent), have a lot of geometry (and thus lots of things to check for intersection), lots of light sources (which means lots of shadows), have translucent material with refraction indexes, or treat other lit objects as light sources (radiosity), computing our scene has a ton of branches and a ton of computation.
Typically CPUs are really good for branching and sequential operations. On the flipside, GPUs are great for situations with tons of parallel independent operations with little branching. Putting both lots of branching and lots of parallel independent operations together can lead to an algorithm that can't benefit from the full potential of either CPUs or GPUs. Caustic Graphics has put together hardware that attempts to create a perfect environment for raytracing, approaching the problem differently than either a CPU or a GPU. Unfortunately, they didn't go into much detail about the architecture of their hardware. But they did tell us some things and we can speculate on others.
48 Comments
View All Comments
ssj4Gogeta - Monday, April 20, 2009 - link
I'm no pro, but from what I know the main difference is that things like shadows, refractions and reflections render MUCH better. This is because in raytracing you also use secondary rays. So the rays reflected off/refracted from a surface can affect the color of other nearby surfaces, producing shadows, reflections, etc. In ray tracing you do it just like nature does it in real world (in reverse of that, but that doesn't affect the outcome). In rasterization, you need to manually program for producing shadows, reflections, etc. and so they are mostly just approximations.Another advantage of ray tracing is that programmers don't need to work that hard - things which may take you hundreds of lines of code in rasterization, only take 10 lines in ray tracing.
Compare this to 3D vs 2D rendering of a 3D cube. Suppose you need to render the cube as the camera circles around it. If you're doing it in 3D, you just render it in 3D, flatten the image and display it. Now if you're using 2D, but you want to create the effect of 3D, you don't have to render a cube - you need to directly render how the flattened image would look. That is, you need to take into account the 3D distortion due to perspective/depth and you need to make the edges of the cube oblique accordingly, you need to make the part of the cube that is farther look farther by rendering it smaller than the face of the cube that is closer to the camera. A lot of work for the programmer, and needless to say, it won't be very accurate. Well in this case it may be accurate as it's just a cube with straight edges, and so you can easily calculate things. But not in the case of a complex object.
Same way, rasterization at best can offer approximations of phenomena like refraction etc., how close they are depend on the programmer.
Check this rendered pic to see what ray tracing can deliver:
http://upload.wikimedia.org/wikipedia/commons/e/ec...">http://upload.wikimedia.org/wikipedia/commons/e/ec...
Note: I'm not a graphics programmer but this is how I understand it. Please correct me where I'm wrong.
AmbroseAthan - Wednesday, April 22, 2009 - link
That picture is a ray-traced rendering?! It looks like a photograph! Someone put in a lot of time and crunching power on that one.JimmiG - Monday, April 20, 2009 - link
I'm also wondering about #2 - is Raytracing really "better", or just "different"?Back in the 90's I used to be really impressed with the quality of raytraced animations and pictures, with their shiny, reflective objects, realistic water, lighting, soft shadows etc. However back then the capabilities of "3d accelerators" were very limited - 3d games used simple models, "flat" lighting models with no shadows and only one light source, and blurry textures without any shader effects like bumps, parallax maps, reflections etc. Today it seems the latest 3d engines already do everything in realtime that you needed raytracing and many minutes/hours per scene to do in the past.
jimhsu - Monday, April 20, 2009 - link
Someone correct me, but i think it is not that raytracing "looks" better, but because it is closer to a physical description of light (only in inverse), effects such as ambient occlusion, caustics, and other shiny things can be implemented in a relatively straightforward manner in a physically correct manner, while rasterization requires the use of shaders to "emulate" reality. These approximations are often complicated to program and implement, even though they achieve nearly the same effect.DerekWilson - Tuesday, April 21, 2009 - link
this is sort of true ... it's possible to write shaders for a rasterizer that do everything a raytracer does. But in addition to the code complexity in a z-buffer based rasterizer you end up with performance disadvantages.At the point where you properly and accurately emulate raytracing with a rasterizer you need to start generating all kinds of dynamic environment maps every frame for every object. treating objects as light sources and doing real time radiosity for rasterization (which can be done as well) is also difficult. To get an image that is as physically accurate as raytracing, rasterization (at this point with todays technology) would be slower even using a GPU designed for rasterization.
Honestly, there are some things that rasterization can approximate well enough that we don't notice the difference, and I think for a long time to come we'll still see rasterization as the main vehicle for realtime graphics. I think we'll start to see raytracing come in as a secondary tool to augment rasterization and add effects that are tough to achieve otherwise.
lopri - Tuesday, April 21, 2009 - link
I think I am learning more abou raytracing from this article and comments than anywhere else to date. Thank you for excellent explanations and analogies!jimhsu - Monday, April 20, 2009 - link
An analogy for math majors would probably be trying to solve a function analytically (i.e. rules of calculus) vs. numerically (i.e. Newton's Method, Euler, etc). The numerical result is often close to the analytical result, but intuitively the analytical result is the "right" way to do the problem, except when it is infeasible (we have something that we can't integrate).Einy0 - Monday, April 20, 2009 - link
This may actually help Larrabee gain ground. An alternative to do realtime raytracing. Then again Larrabee may help this gain ground as Larrabee will be do both rasterizing and raytracing.I'm surpirsed they can make anything worth buying with FPGAs. I guess FPGAs have come a long way. I'd love to get some specifics on the underlying architecture.
Bad timing considering the global economy etc...
ifkopifko - Tuesday, April 21, 2009 - link
lol... real-time raytracing? Keep dreaming. :-D Something like that is far far in the future.Sivar - Tuesday, April 21, 2009 - link
There have been assembly language demos from the 4k scene which have done realtime ratracing since the late 90's. In software. On a Pentium MMX.They aren't quite what I'd call Pixar-quality, but it's far from impossible.