**A Gentle Introduction To DirectX Raytracing** **Quick, easy-to-understand tutorials to learn basics of Microsoft's DirectX Raytracing** What is DirectX Raytracing? =========================== At the 2018 [Game Developer's Conference](http://www.gdconf.com/), [Microsoft introduced](https://blogs.msdn.microsoft.com/directx/2018/03/19/announcing-microsoft-directx-raytracing/) an addition to [DirectX 12](https://en.wikipedia.org/wiki/DirectX) called _DirectX Raytracing_ (DXR), an API allowing easy use of GPU-accelerated [ray tracing]("https://en.wikipedia.org/wiki/Ray_tracing_(graphics)") in DirectX and allowing simple interoperability with traditional DirectX rasterization. Unlike most other GPU-accelerated ray tracing APIs, there are not specialized interoperability considerations to allow a rasterizer and ray tracer to efficiently share GPU resources. Instead, the same resources can easily be reused between rasterization and ray tracing. Additionally, as a Microsoft standard, this should enable writing cross-vendor ray tracers. If your GPU drivers do not support DXR today, Microsoft has a [software fallback layer](http://aka.ms/dxrsdk) that should work on Windows 10 RS4 using any GPU with Shader Model 6.0 support. We also presented a [SIGGRAPH 2018](https://s2018.siggraph.org/) [course](https://s2018.siggraph.org/presentation/?id=gensubcur_104&sess=sess268) covering the basics in an ["Introduction to DirectX Raytracing"](http://intro-to-dxr.cwyman.org/). You can find detailed slides and other resources on the [course webpage](http://intro-to-dxr.cwyman.org/). _**Impatient to get started?**_ Grab [the code](http://cwyman.org/code/dxrTutors.Code.zip) and [jump to](#toc5) the tutorials walkthrough. (##) Resources for Learning Ray Tracing My goal is *_not_* to teach you ray tracing. Instead, I largely assume you understand the basics of ray tracing, e.g., from an introductory graphics text. If you are looking for resources to get you to that stage, some good options include: * [Ray Tracing In One Weekend](https://www.amazon.com/Ray-Tracing-Weekend-Minibooks-Book-ebook/dp/B01B5AODD8), which has a devoted following on my Twitter feed. * [Fundamentals of Computer Graphics](https://www.amazon.com/Fundamentals-Computer-Graphics-Steve-Marschner/dp/1482229390/), a widely-used introductory graphics text. * [Realistic Ray Tracing](https://www.amazon.com/Realistic-Tracing-Second-Peter-Shirley/dp/1568814615), a text focusing on implementing a realistic ray tracer. * [Ray Tracing From The Ground Up](http://www.raytracegroundup.com/), an introductory graphics text focusing on ray tracing. * [Graphics Codex](https://graphicscodex.com/), an online resource with bits of information about all things graphics. Tutorial Goals ============================ Frequently, when new APIs are released, documentation is accessible and understandable only by experts in the area. This is especially true with modern graphics APIs, like DirectX 12, which operate close to the hardware. These tutorials are _**explicitly designed**_ to help more naive GPU programmers get a simple ray tracer running quickly and allow for easy experimentation. That means we have highly abstracted the CPU-side, C++ DirectX Raytracing API. If you want to learn the specifics of the host-side API, other tutorials can help you (e.g., the [Falcor DXR API tutorials](https://github.com/NVIDIAGameWorks/DxrTutorials) or [Microsoft's DXR SDK](http://aka.ms/dxrsdk)). To abstract the API, we use NVIDIA's [Falcor](https://developer.nvidia.com/falcor) [framework](https://github.com/NVIDIAGameWorks/Falcor), an open-source prototyping infrastructure we built at [NVIDIA Research](https://research.nvidia.com/) to help improve our research productivity. I added an additional syntactic sugar layer to further simplify the interface. The tutorials build on each other, so each adds only an incremental, easy-to-grasp chunk of code. Our abstraction relies on a simple, modular render pass architecture that localizes changes and simplifies code reuse between tutorials. A resource manager abstracts creation and management of texture resources shared between these modular render passes. After finishing the tutorials, you will be able to quickly and easily: * Open a window using a DirectX context, * Run raster, ray tracing, and compute passes that interact with each other, * Load Falcor scene files (ASCII text-based and can include .obj and .fbx files), * Generate an easily-customizable [G-Buffer](https://en.wikipedia.org/wiki/Deferred_shading) using either rasterization or ray tracing, * Spawn deferred shading rays from this G-Buffer for hybrid ray-raster rendering, * Use traditional ray tracing effects including: ambient occlusion, mirror reflections, shadow rays, and path tracing, * Use a simple GGX material model for direct and indirect lighting, * Easily add complex camera models introducing depth-of-field, * Accumulate samples temporally for generating ground truth ray traced images. * Build an interactive version of the final example from [Pete Shirley](https://www.cs.utah.edu/~shirley/)'s [Ray Tracing In One Weekend](https://www.amazon.com/Ray-Tracing-Weekend-Minibooks-Book-ebook/dp/B01B5AODD8) e-book, which includes intersecting spheres and reflective and refractive materials. Tutorial Requirements ========================== Requirements depend on if you are using a release version of DirectX Raytracing (in Windows 10 RS5 or later) or are using the experimental version of DirectX Raytracing (in Windows 10 RS4). We encourage everyone to develop on RS5 or later, as it removes multiple sources of crypic errors. For all DirectX Raytracing, you need: * A 64-bit version of Windows 10. Check the version by running [_winver.exe_](rs4winver.png). - Version 1803 is Windows 10 RS4. - Version 1809 is Windows 10 RS5. - Later versions should also work. * _Visual Studion 2017_. Note: the free [Visual Studio Community Edition](https://www.visualstudio.com/downloads) works just fine. * A graphics card and driver that support DirectX Raytracing - We suggest NVIDIA drivers should be 416.xx and above. For Windows 10 RS5, you also need: * _Windows 10 SDK 10.0.17763.0_. [Get it here](https://developer.microsoft.com/en-us/windows/downloads/sdk-archive). - Later SDK versions may work, but you need to know how to update the SDK used in Visual Studio. For Windows 10 RS4, you also need: * _Windows 10 SDK 10.0.16299.91_. [Get it here](https://developer.microsoft.com/en-us/windows/downloads/sdk-archive). - Later SDK versions may work, but you need to know how to update the SDK used in Visual Studio. * Ensure Windows is in _Developer Mode_. (See *PC Settings* -> *Update & Security* -> *For Developers*, and select "Developer mode") * Our RS4 tutorials will *only* work on NVIDIA Titan V, Quadro GV100, and GeForce RTX cards. We have not had a chance (or the hardware) to test the software fallback layer on Windows 10 RS5. In theory, these RS5 tuorials run on GPUs without hardware-accelerated ray tracing that support DirectX 12. We will update the information here as time permits. (Please let me know your results if you try running on other hardware.) Getting the Code ============================= You can grab the tutorials: * The [code](http://cwyman.org/code/dxrTutors.Code.zip), [readme](tutorCodeReadme.txt), and [precompiled binaries](http://cwyman.org/code/dxrTutors.Binary.Win10-RS4.zip) for Windows 10 RS4. * The [code](http://cwyman.org/code/dxrTutors.Code.Win10-RS5.zip), [readme](http://cwyman.org/code/tutorCodeReadmeRS5.txt), and [precompiled binaries](http://cwyman.org/code/dxrTutors.Binary.Win10-RS5.zip) for Windows 10 RS5 and later. Prebuilt binaries come with all dependencies included (except the GPU, driver, and OS). Please read the README in the binaries package for more details. Code comes with most dependencies. However, we rely on the open source [Falcor](https://developer.nvidia.com/falcor) framework, which has a number of external dependencies. These are downloaded via a simple script which runs as a build step in Visual Studio ([see the README](tutorCodeReadme.txt)), so your first build may take longer than usual. The code for RS4 comes with a custom, precompiled version of Falcor. The code for RS5 comes with a source copy of [Falcor 3.1.0](https://github.com/NVIDIAGameWorks/Falcor), with one tiny update to a build script that copies tutorial shader files correctly during the build. Tutorial Walkthroughs and Descriptions ======================================= Code for all listed tutorials is included in our [code bundle](http://cwyman.org/code/dxrTutors.Code.zip). Each page below contains a detailed walkthrough of important aspects for each tutorial: * [Tutorial 1](tutors/Tutor1/tutorial01.md.html): Basics. Open a window, and clear it to a user-controllable color. * [Tutorial 2](tutors/Tutor2/tutorial02.md.html): Basics. Create a simple raster full screen pass with HLSL pixel shader. * [Tutorial 3](tutors/Tutor3/tutorial03.md.html): Basics. Create a rasterization-based G-buffer pass. Render a loaded scene. * [Tutorial 4](tutors/Tutor4/tutorial04.md.html): Start using DXR. Create the same G-buffer, this time using DirectX ray tracing. * [Tutorial 5](tutors/Tutor5/tutorial05.md.html): Ambient occlusion. Start from G-buffer, trace one AO ray per pixel. * [Tutorial 6](tutors/Tutor6/tutorial06.md.html): Accumulate frames over time, giving AO with arbitrarily many rays per pixel. * [Tutorial 7](tutors/Tutor7/tutorial07.md.html): Antialiasing. Adds camera jitter to antialias when accumulating temporially. * [Tutorial 8](tutors/Tutor8/tutorial08.md.html): Depth-of-field. Add a controllable camera aperture, introducing depth of field. * [Tutorial 9](tutors/Tutor9/tutorial09.md.html): Lambertian shading, shooting one shadow ray to each light in the scene. * [Tutorial 10](tutors/Tutor10/tutorial10.md.html): Add a light probe environment map for rays that miss the scene geometry. * [Tutorial 11](tutors/Tutor11/tutorial11.md.html): Reduce ray count. Shoot shadow ray to only one random light per pixel. * [Tutorial 12](tutors/Tutor12/tutorial12.md.html): Add one-bounce diffuse global indirect illumination. * [Tutorial 13](tutors/Tutor13/tutorial13.md.html): Apply Falcor's tonemapper to allow display of high dynamic range outputs. * [Tutorial 14](tutors/Tutor14/tutorial14.md.html): Swap out Lambertian for a glossy GGX material, add multiple light bounces.