Path Tracing vs. Ray Tracing
techspot.com
techspot.com
Yeah, that's pretty much exactly wrong.
Path tracing exists not because it's faster (it isn't, except perhaps in some pathological conditions), it exists because there are certain lighting effects that standard recursive ray tracing doesn't handle at all, but path tracing does. Caustics, global illumination, subsurface scattering, etc... We could be using ray tracing for just about everything now, but we don't because it doesn't look very realistic.
(Photon mapping can be used to create those lighting effects as well, but path tracing seems to have won out, being a bit less fiddly. With photon mapping you have to know sort of what you're going to be looking at in advance, whereas with path tracing you don't.)
Path tracing can be very slow, but improvements in computation speed plus algorithmic tweaks to prune unnecessary work mean it can be sort of done in real time these days.
One of the big differences between ray tracing and path tracing is that the former is deterministic, whereas the latter is probabilistic. Ray tracing just does a bunch of math until it arrives at the answer, but in path tracing the image quality depends on how much effort you put into it. If you use a few rays per pixel, you get a grainy image. If you use a lot of rays per pixel, you get something that's a lot smoother and clearer, but takes a long time to render.
One of the big semi-recent advances is to use denoising algorithms. Instead of throwing a lot of compute cycles at the problem to trace lots of rays, you just render a noisy image and use some algorithm (perhaps based on machine learning, perhaps not) to smooth it out. So now instead of tracing 100 or 1000 rays per pixel to get something that looks good, you might only trace 10, or 2, or 1.
If you're talking about movies, ray tracing usually means path tracing these days. If you're talking about games, it probably means either path tracing or taking a conventional rasterization engine and bolting Whitted ray tracing on the side because they want realistic reflection and/or shadows.
Companies that make GPUs advertise "ray tracing" as a hardware feature, because that's what the hardware does: it tests rays for intersection against some kind of bounding volume hierarchy. Whether software uses that for Whitted ray tracing or path tracing or something else is up to the application.
Something I've wondered about is whether these denoising algorithms are always purely used as a last stage of post-processing at the end of the graphics pipeline, or whether they're able to, in effect, go back and say "hey, this area right here I'm pretty uncertain about, so I think we should trace some more rays" and so you might go back and forth between the raytracing engine and the denoising engine a couple times before you get a final image. It seems like the latter could be pretty effective.
My very short summary: Let's model the scene as a function (x,y,z,u,v)->(R,G,B,d). That is for a given ray origin and direction, give us the corresponding radiance and density. Take a deep network, and train it to approximate this function on a scene produced from renderings or video/photos. Now you can use techniques from volumetric rendering (ray marching as used for clouds and such) to render novel views of the scene.
I'd really suggest watching the video part way down the page though. These representations do shockingly good at occlusion, which is the difficult problem in rendering.
I think this paradigm opens up a lot of interesting possibilities. The elephant in the room is training time, but with good enough compositing of static assets this could still make sense. The trained models are extremely compact.
Edit:
Oh forgot your second idea. With offline path tracers there are techniques like that. The most elegant is perhaps Metropolis Light Transport, which uses a random walk through the space of possible paths, adjusting step size based on how "interesting" the region its in is. There's also Importance Sampling... lots and lots of ideas in the research literature.
But also for cinema rendering a lot of the time it's just "oh we shoot 64 paths per pixel no matter what" because that produces very even noise grain and very good antialiasing. If you use some technique that has temporal biases about noise across sequences of frames it tends to get picked up by the viewer easily.
I am not entirely sure if I should flag it, but at least the comments on HN are 100x better than the article.
I would also not be surprised, if this isn't being dealt with early on, the word "Path Tracing" will become another marketing hype and being over used in every single internet comment, just like Apple's "* unified memory* " was some special memory built into the chip or worst SRAM cache inside the chip etc....
I'm not following completely. Are they saying that raytracing only generates new rays in the reflection or refraction direction? The diagram seems to contradict that, there's three new rays generated at each surface for raytracing on that.
Does pathtracing not take reflection and refraction into account at all? Does it only generate new rays in random directions to sample the incoming light? The diagram shows two new rays being generated at each surface, and one of them fins a light each time.
> But here's the really clever part: ordinarily fewer rays would result in a less realistic lighting, but since the bulk of the color of the frame's pixels are only affected by the primary rays, dumping most or all of the secondary ones doesn't affect things as much as one might think.
I don't understand this either. This just sounds like raytracing without reflections.
Can someone walk me through how the rendering of one pixel works in each case? I think the difference is how the secondary rays are generated, right?
So, basic Whitted style ray tracer is, for each pixel, project a ray into the scene and see what the closest intersection is. Now shoot secondary rays at each light source to see if the intersection is in shadow for that light, sum the lighting accordingly. Also if the material hit is shiny, shoot a ray out using snells law and recurse to see what it sees.
Now, with path tracing instead we approach the rendering problem more generally, and think of it as a 6D integral that describes all possible ways light can bounce in the scene. Then we sample this integral using monte carlo techniques. The most naive version of this would be to emit virtual photons from lights, trace where they go probabilistically using scattering equations at each surface interaction (so not just snells law) and then seeing if it ever blunders into the virtual camera.
Obviously that's very inefficient, so practical path tracers work backwards tracing paths from the camera, or bidirectionally doing both and seeing if they can connect. Then there's a huge pile of techniques like importance sampling that help make all this more efficient.
That said, at the end of the day, path tracing is much more computationally intensive than the classic ray tracer. However it's worth this because it can truly render all lighting in a physically correct way. Classic ray tracing bakes in assumptions about shadow rays and simple reflections, which is why images made with it have a very stark unrealistic appearance (think sphere and pyramid floating over a checkerboard style images).
Let's start with a ray. It begins at the camera, travels through some point in the viewport, and goes on its merry way through the scene. It does so in a straight line, and Interesting Stuff happens when the ray hits some object. In practice by far the hardest part is figuring out what you are going to hit - and doing so quickly. But we're going to completely ignore that here for simplicity.
The most primitive way of ray tracing is to draw a pixel colored by the object you hit. The ray hit a red ball? Pixel is red. The ray hit a green cube? Pixel is green. The ray hit nothing? Pixel is black. But this creates solidly colored objects, so it is pretty useless. One important thing to note is that you are done after a single ray.
Let's add some basic physics to it. Perfect mirrors are easy: the angle of incidence is equal to the angle of reflection, so we just alter the direction of the ray and continue tracing it. Transparent objects are easy too: Snell's Law will trivially tell you how the ray should be adjusted to account for different refractice indices - sometimes it gets bent, and sometimes it gets reflected. And we just got things like lenses implemented! Adjusting for things like colored mirrors or tinted water is left to the reader. Opaque objects are slightly more complicated. They get their light from, well, light sources. To do this, we cast secondary "shadow rays": one ray from each point light source to the point on the opaque object we hit. If nothing is in the way we use a shading algorithm like a Phong shader to determine the influence of that light source at that angle on that object. The important takeaway here is that the ray tracing is now recursive. We have now arrived at what we call "Whitted-style ray tracing".
There are two important things to keep in mind for Whitted-style ray tracing. First, you have to shoot a shadow ray to every light source. This gets expensive really fast. Second, while you can create objects which do multiple things at once (somewhat opaque, somewhat glassy), this results in even more rays. In practice, hitting a surface point spawns 1) a shadow ray for each light source 2) a reflection ray 3) a ray transmitted through the material. And the reflected and transmitted rays will go on to hit even more surfaces - leading to an exponential growth in the number of rays. And the final result isn't even physics-accurate!
Finally, path tracing. We start off the same, but when we hit an object we choose a single random path to continue on, until we finally hit a light source to terminate on. The key point here is that the continuation vector is a weighted random value of a probability function: if enough rays hit this object, the average result will eventually approximate the integral of this function. Similarly, if an object does multiple things, like both reflection and transmission, we randomly choose one of them to continue with. This means we can simulate complicated physics without generating an exponential amount of secondary rays. The only downside is that you need to trace a lot of paths to get a good result: a single path can end up with a heavily biased result due to the randomness rolling dice in a silly way, which ends up generating noise in the output.
----------------------
In summary, ray tracing recursively generates secondary rays to exactly calculate some laws of physics, leading to an explosion of the amount of secondary rays you need to handle, and poor results because you simply cannot implement all physics correctly. Path tracing generates a predetermined and fixed amount of rays randomly going through the scene in an effort to eventually approximate pretty much all laws of physics, but you need a lot more primary rays to get a good approximation and thus a noise-less result.
I followed the Advanced Graphics course at Utrecht University a few years ago, taught by the amazing Jacco Bikker. It seems last year's recordings are available (in English!), so if you want to know more check them out at http://www.cs.uu.nl/docs/vakken/magr/2021-2022/index.html
At each intersection, a ray tracer might spawn a refraction ray, a reflection ray, and a shadow ray towards each of the lights. The reflection and refraction rays might spawn rays of their own, recursively. There are pathological cases where this gets out of hand, so most ray tracer have a maximum recursion depth or some threshold that if the ray doesn't contribute more than some threshold to the color of the pixel it just defaults to black (or whatever) and isn't computed.
Most scenes aren't made entirely of shiny glass spheres, so this usually isn't a major problem.
Path tracing is a little different, instead of evaluating a recursive tree of rays, you just do a lot of random sampling. Instead of evaluating both the reflection and refration ray, you flip a coin and evaluate one or the other. Or if the surface isn't perfectly smooth and glossy, you pick a random direction. Generally, path tracers have to trace a lot of rays per pixel to get anything other than a very noisy image. That can take a long time, so historically path tracing has been a lot slower than classical Whitted-style recursive ray tracing.
In either case, path tracing or ray tracing, the traversal doesn't (usually) terminate in the ray colliding with a light source. Instead, at some hit location you do a direct illumination test which usually amounts to casting a shadow ray at all the light sources to see if that spot is directly illuminated, and then evaluating the texture and lighting at that point to produce a color, which propagates back up to the final result.
> I don't understand this either. This just sounds like raytracing without reflections.
Yep, that's raytracing without reflections, refractions, or shadows. Or basically anything that distinguishes it from plain polygon rasterization. Which is why ray tracers and path tracers do actually compute those secondary rays, they just have different strategies about which ones they evaluate.
Plain Whitted ray tracing handles some additional optical effects that polygon rasterizers aren't good at (reflection, refraction, shadows), but it's not a complete solution. (Or we would say that it's not a full solution to the "rendering equation", because it omits some important terms.)
Path tracing on the other hand is a complete, unbiased solution to the rendering equation. (Which is to say it renders a "correct image" according to a fairly realistic model of how light behaves, given infinite compute power, and if one's resources aren't infinite, at least it will give a statistically representative if perhaps noisy result.) The main thing path tracing handles that ray tracing doesn't is global illumination: basically, light that bounces off more than one surface between the light source and the camera. It can even correctly model semi-glossy surfaces. Global illumination can often make the difference between "this looks CG" and "this look like a photograph" (assuming there aren't any other major tells).
"Raytracing" is a bit of an overloaded term, but conventionally "Raytracing" for graphics meant a serious of "raycasting" events, and "Whitted" style was a technique which could approximate reflective and refractive materials fairly accurately (as long as you modelled the fresnel effect and absorption) in a recursive style. Modifications on top of this are also to do limited indirect rays for global illumination and this was used a bit (in film/CG VFX in hybrid renderers like PRMan pre 19), but doing global illumination this way was pretty slow and didn't scale well (an exponential branching factor at each bounce). So technically, the article is sort of correct on this point, as Pathtracing is more smart about how it handles the full rendering equation of "all" light transport simulation, and is faster than Raytracing for high fidelity (i.e. global illumination, area lights), due to its stochastic nature of sampling multiple dimensions of lights, surfaces, volumes, etc across many samples per pixel.
Pathtracing does take reflection and refraction into account, but generally (there's a technique called 'splitting', which sort of branches the paths to make it more like 'Whitted' raytracing, but ignore that for now) it relies on "importance sampling" to make smarter decisions, based off the material response (diffuse, glossy, reflective, refractive), which guides the direction of the next ray off the previous surface. This generally means that due to the "random" nature of pathtracing sampling (it uses monte carlo random sampling), predominant lighting is generally more efficient, although things like caustics, SSS and indirect illumination can still be very noisy as they're hard to sample and can take thousands of samples to resolve to "clean".
There are other techniques like Russian Roulette to kill particular paths which don't have much throughput and/or light contributions, which can speed up renders as well, and I think is what the "dumping most or all of the secondary ones doesn't affect things as much as one might think." part is referring to.
This session has one of the best overviews of Lumen. The previous year has a really good overview of Nanite.
The unreal docs are probably a little easier to grok https://docs.unrealengine.com/5.0/en-US/lumen-technical-deta... but they do leave a lot out.
At their core, they’re also not that different than how REYES used to work with its micropolygon architecture and the way GI was bolted on. Given how old REYES is, you might find more reading material there.
In traditional rasterized games, the only thing that matters is whether the end result looks good. This means adding things like global illumination (a magical diffuse light source which lights up stuff regardless of position), or simply making some parts of a texture brighter to make it look as if a light source is shining on it. Things like shadows are mostly pre-baked into the textures, and primitive reflections can be faked by just mirroring whatever is already on the screen. Basically, fake it until you make it.
With path tracing, the renderer follows the laws of physics. If something is lit up, the light actually has to come from somewhere. And as it turns out, most light will have hit multiple objects before finally ending up at the viewer. The artist now has to make a reasonably physics model for every single object in the game - even those the player might not directly look at. It's a completely different approach - made even worse because we aren't yet at a point where every gamer has a computer which can do 100% path tracing, so you still need to support traditional rasterization for people without bleeding-edge GPUs.
To get an example of the visuals possible, look at modern cinema. Disney and Pixar movies are path traced, as is probably all CGI these days. I expect games to get a more diverse look over time, as artists get more experience with it.
They are mostly terrible examples of path tracing, as they just took the original map files with the lights authored for the original renderer, erased the baked lighting, and run the new renderer with those lights. So it's not that game designers are not used to lighting for path tracing, it's just that nobody designed it. Heck, even porting maps between two versions of the same lineage of offline raytracer can result in ridiculously different outputs: https://www.youtube.com/watch?v=2JkRwPWnL_c
Among other things, these games did not have high dynamic range lighting at all, so light data can only ever make the picture darker than the base textures. (Think of it as a layer set to "multiply" blend mode in a photo editor) So making a light brighter at some point wouldn't make things look any closer to white, just making the falloff sharper. So most lights were authored to look good in the renderer, not make any actual physical sense, as you said.
That said, this approach of "just swap out the renderer" could work much better for games that work on a physically-based rasteriser workflow (mostly newer stuff, like from around 2015 and onward), because they mostly already use compliant and realistic light sources, and the baked lights and rasterisation are "merely" for performance. (a.k.a. being a game and not a 2-day-per-frame art installation)
If you want to see a ray-tracing-native game, take a look at Teardown (https://www.teardowngame.com/), it does not exhibit any of the "chiaroscuro" described, and has perfectly cromulent lighting.