r/GraphicsProgramming • • Mar 31 '20

Just got dispersion working in our path-tracer

Post image
206 Upvotes

25 comments sorted by

29

u/BryalT Mar 31 '20 edited Mar 31 '20

Me and a friend are writing this renderer as part of our master's thesis. Feature-wise, it's a path-tracer that uses multiple importance sampling for estimating the direct radiance term. It's implemented from scratch, and is heavily based on the PBR book.

The language used is Futhark, an ML-inspired array functional programming language for GPGPU programming. The Futhark code compiles to OpenCL, and the posted image is rendered on an AMD RX 580. Dispersion is supported via an implementation of spectral ray-tracing, where we have a special spectrum type instead of a simple vec3 to represent material color/albedo, BSDF-values, emission, etc. A single ray has a single wavelength that is sampled from a user-provided list of normal distributions, supposed to roughly match the sensitivity distributions of some camera sensor.

For this particular render, I have exaggerated the slope of the refractive index of the glass material by a factor of ~5, just to get a more clearly defined rainbow. Normally the effect is more subtle.

The light source is an area-spotlight. The light is emitted in a frustum where θ=3°.

11

u/VincentDankGogh Mar 31 '20

Have you seen this paper on hero wavelength sampling? It allows you to sample the spectrum at multiple wavelengths per path and combine the results via MIS.

I’ve been working on implementing a spectral renderer myself, and must admit that some the formulas in there don’t make a whole lot of sense to me, but hopefully I’ll figure it out soon enough.

7

u/BryalT Apr 01 '20

No. We've been reading way less literature on spectral tracing than we should really. I will definitely check out that paper — thanks!

2

u/RomanRiesen Apr 02 '20

Woah, that language looks amazing!

4

u/Stylpe Apr 01 '20

That's really cool, but I'm pretty sure the color order is reversed? Did you forget a negative sign somewhere?

April fools :D made you doubt for a second

2

u/BryalT Apr 01 '20

Haha you really did! I was fairly confident, but had to double-check with the figures on wikipedia.

3

u/qblock Mar 31 '20

Nice. Is that a prism dispersing the spectral band? Are you importance sampling from both the Fresnel transmittance term and the spectrum? How are you implementing the BRDF for the ground? How many samples did it take for that image?

2

u/BryalT Apr 01 '20 edited Apr 01 '20

Yes it's a prism.

I'm not sure I understand correctly, but when reflecting vs. refracting we importance sample on the Fresnel reflectance, which in turn is computed from the wavelength-dependent refractive index.

The ground is a simple lambertian. It's color parameter is a spectrum, so to ecaluate the BSDF for some ray wavelength, we return(spectrum_lookup ray_wavelength color_spectrum) * 1/pi.

It took many samples — approx 75'000 spp! I could've set up a better scene, and our current light sampling for frustum-ligthts is kind of naïve, but indeed it's quite slow. I suppose we'd need to implement something like metropolis light transport for a more significant reduction in convergence time.

2

u/leseiden Apr 01 '20

That is really nice.

1

u/BryalT Apr 01 '20

I'm glad you like it :)

2

u/oparisy Apr 01 '20

This is superb to behold.

3

u/BryalT Apr 01 '20

Now if only I could make it do a double rainbow...

3

u/[deleted] Mar 31 '20

language used ?

7

u/BryalT Mar 31 '20

It's called Futhark, and it's quite nice to use if you're coming from an FP background and don't want to have to deal with low-level imperative languages like OpenCL and Cuda C. It's actually an ongoing research project lead by Troels Henriksen over at DIKU in Denmark.

Futhark is not primarily meant for graphics, and unlike Cuda (OptiX) does not have any built-in features for BVH traversal, intersection testing, or anything like that. It's still a joy to use though, and the generated code utilizes the GPU parallelism really well, as far as I can tell.

1

u/Ditschel Aug 26 '24

Do you have a repository I can look at? currently interested in doing something similar

2

u/Ansoulom Apr 01 '20

Oh, cool! I was planning on doing something similar for my thesis, but then I switched up the subject last minute to something that's not even graphics related.

2

u/[deleted] Apr 01 '20

[removed] — view removed comment

2

u/BryalT Apr 01 '20

It's not bidirectional, and shadow rays toward transmitting materials are not handled. We rely on MIS for the direct light term after reaching the other side via indirect rays to get caustics etc.

I'm afraid I can't help you with bidirectional methods -- I'm relatively unfamiliar with them myself.

2

u/[deleted] Apr 01 '20

[removed] — view removed comment

1

u/BryalT Apr 03 '20

Sure. Here's one after running for quite long. Note that it even after running for many hours is still a bit noisy -- as we don't use any bidirectional method (yet?), many refraction related phenomena will take awfully long to converge.

0

u/[deleted] Apr 03 '20

[deleted]

1

u/BryalT Apr 03 '20

No, on a GPU. More specifically, an AMD RX 580.

We've not been extremely careful too adapt our code to GPU hardware, like keeping stack usage small etc., but thanks to Futhark, the generated OpenCL code is still pretty good as far as I can tell. We definitely maximize usage of the compute cores, although I don't know if the utilization is the most effective.

0

u/[deleted] Apr 03 '20

[deleted]

1

u/BryalT Apr 03 '20

Is it because they use a simplified algorithm?

Yes and no. In one way the ray tracing is simpler -- it may not be applied for all illumination phenomenon of the render, and it may not handle certain kinds of materials at all -- but in many ways it's way more complex. The problem with a naïve implementation is not that it looks bad -- it doesn't, at all, it looks great! The problem is that it's slow. Games have to do amazingly smart and complex things to our simple path-tracing algorithm to make it run fast. Different games do different things, and sort of pick-and-choose what features of path tracing they want and which they can live without, and then they do smart things to combine it with traditional methods to achieve a performant engine.

how do games get 60fps path tracing on mid range cards

One reason that ray traced games can be fast is that, as far as I know, very few -- if any -- current games use path tracing for all of it's graphics, only parts of it. E.g. "Shadow of the Tomb Raider" can use ray tracing to get soft shadows, but rasterization is still used to render the rest of the view. Ray traced shadows are relatively cheap to produce -- you only send one or a handful of rays towards the light sources per pixel/point, and see if there's anything in the way or not.

Another common thing to use ray tracing for in newer games are reflections. However, it's only used for the reflections, and even then, typically only for direct reflections (i.e. not reflections-in-reflections and so on).

Fully path traced games may be possible on high-end hardware, e.g. the Quake II "demo" does this, but you have to make a lot of tradeoffs. For one, you don't trace until convergence, which would require hundreds of thousands of samples per pixel. Instead, you trace one or two or maybe four samples per pixel, take the noisy result, and apply some kind of post-processing denoising. There are many smart tricks to denoise real-time path-tracing, e.g. temporal denoising. With temporal denoising, you remember the result from the previous frame, compute how the camera has moved and how the scene has changed, and reproject all samples from the old frame to the new, filtering out those that are now modified or occluded. Mara et al. has a good paper that covers a combination of some methods, "An efficient denoising algorithm for global illumination". We don't do any kind of denoising in our renderer -- that's one of the reasons it takes long to produce a noise-free image.

Finally, one thing that the games visual designers take into consideration when path tracing is involved is to not feature many refractive objects (like glass and water) and light sources with delta/sharp distributions (like point lights and directional light sources, like frustum area lights), especially at the same time -- they don't play well together at all! My scene is an example of a kind of pathological case like this: the light source emits only in a small frustum (or per point, in a cone), and the glass of the prism refracts light along a delta-distribution, i.e. only in exactly one direction. The result is that when we're refracting out of the prism and want to calculate the direct incident radiance for the junktion, sampling the light source is no good due to the delta distribution of the glass's BSDF, and sampling the BSDF has a very low probability of hitting the light source due to it's directional nature. The result is that while the diffuse and glossy/specular surfaces of the scene converge quite quickly, it takes a long time for phenomena like dispersion and caustics to converge. Using some bidirectional method would help, but it's a little more tricky to implement, and we've chosen not to do it in order to limit the scope of the project.