Accelerated “Ray Tracing in One Weekend” in CUDA
devblogs.nvidia.com
devblogs.nvidia.com
"Ray Tracing in One Weekend/The Next Week/The Rest of Your Life" have recently switched to DRM-free, "Pay What You Want" pricing http://in1weekend.blogspot.com/2016/01/ray-tracing-in-one-we...
I believe that Turner probably did some great things, but somehow I don't think I believe he invented ray tracing. Ray tracing has been around for so long in physics...
Instead of producing an image, it is an actual game :D And instead of running just on nVidia, it runs everywhere (in a browser, even on phones etc).
And then when you hit that object, you cast more rays from that intersection hit point: - to light sources and see whether or not there's some other object between that intersection and the light source. This determines shadows. - to the reflected position of the initial ray, to calculate reflections. - ...
It's a recursive process where you gather light values as you go, the combination of which gives you a final color for the pixel through which you shot that initial ray.
"Normal" computer graphics is comprised of doing every dirty shortcut trick you can imagine to avoid tracing hundreds of millions of rays per second while still having some semblance of lighting effects in the virtual scene.
You can do simple ray tracing by looping over each pixel, defining a ray that starts from the viewpoint and passes through the pixel on the screen, then looping over every object in the whole scene to see if and where that ray hits each object. That'll work, but it'll be very slow. Then the fun begins figuring out data structures and algorithms to make that process faster.
The advantage of rasterization is that it can be very fast because it works with small amounts of data at a time that has fairly predicable access patterns and good locality between pixels. But, that's also it's down side. It doesn't work well with information that is not local to a single point on an object (for example, how close are other nearby objects? That's hard for a rasterizer).
The advantage of ray tracing is that it is built from the ground up around querying the entire environment. So, features like shadows, reflections and ambient occlusion become much easier to get to work. That's also it's down side. Querying the environment is difficult to make fast. So, even though the features are easy to write, they are still a challenge to keep under the frame time budget.
Imagine light rays coming out of your eyes. For every ray that hits an object, draw rays to every light source from that hit point.
Light in reverse.
In-between those two steps you can figure out intensity and colour of light, the colour of objects, roughness, transparency, refraction, reflectivity, etc etc. You can even bounce multiple rays to other objects for global illumination and whatever.
Most games use scanline rendering. The program starts with a list of all the objects in the scene, and one by one calculates where they go on the screen.
Raytracing starts with all the pixels on the screen, and one by one calculates what goes in each pixel.
Adding shadows with scanline rendering is hard. Checking every object and calculating where its shadow goes is complicated. (Look up volumetric shadows and shadow maps if you want to know more.) Raytracing shadows is easy. For each pixel, you check if anything is blocking the light. If yes, it's in shadow. If no, it's lit.
Adding reflections with scanline rendering is almost impossible (ignoring hacks that only work in some situations). Since one object can have many reflections, you can't just go down the list of objects and calculate a reflection for each one. Raytracing reflections is easy. For each pixel on a reflective object, you check to see what would cast a reflection there.
Now, nobody is actually building fully raytrace games. "Raytraced" games first draw everything with scanline rendering, then go back and use raytracing to add shadows and reflections.
I still feel like that was more like an eli10 than an eli5, but good enough. :-)
Tracing means: "where does this vector intersect an object".
So there is nothing special about ray tracing. But GPUs now can calculate those vectors very fast. And by tracing rays we can simulate how real lights behaves. So today we can have photorealistic images in seconds or even realtime.
[weeb@neet ~/repo/raytracinginoneweekendincuda (ch01_output_cuda)]
$ ./cudart
Rendering a 1200x600 image in 8x8 blocks. CUDA error = 48 at main.cu:48 'cudaGetLastError()'
Make sure that you're building for the appropriate device architecture.
http://arnon.dk/matching-sm-architectures-arch-and-gencode-f...
Then I'll give you some unsolicited advice:
1. Don't assume it's someone else's fault - "What's wrong with the code" is the wrong first question.
2. Read the original article, in its entirety. Fortunately people are still nice enough to help you, but the combo of "how is this screwed up" and "I didn't thoroughly read the article" is off-putting to people who would otherwise love to assist you.
The relevant section:
> If you start with my Makefile, note that I build for a GTX 1070 card using specific -gencode flags for that card (-gencode arch=compute_60,code=sm_60). You will want to adjust the architecture and feature settings for the GPU or GPUs you will be running on.
NET::ERR_CERT_SYMANTEC_LEGACY
Subject: devblogs.nvidia.com
Issuer: RapidSSL SHA256 CA