Introduction to DirectX Raytracing

To get jump started with real-time ray tracing, I’ve made a barebones tutorial application and posted the code on GitHub. Unlike other DirectX Raytracing (DXR) tutorials that are available, this sample code does not use any abstractions on top of the DXR host API. By using the API directly, the focus is on highlighting some of the most important new features, functions, and data structures introduced by real-time GPU ray tracing.

Check out the code on GitHub. It has a permissive license, so go nuts extending it and have fun with ray tracing! The code sample traces primary rays and includes a simple hit shader that samples a texture.

Release Mode Output


  • Windows 10 v1809, “October 2018 Update” (RS5)
  • Windows 10 SDK v1809 (10.0.17763.0). Download it here.
  • Visual Studio 2017 or VS Code

Code Organization

Data is passed through the application using structs. These structs are defined in Structures.h and are organized into three categories:

  • Global
  • Standard D3D12
  • DXR

Rendering code lives in Graphics.h/cpp and is broken into four namespaces to clearly separate new DXR functionality from existing D3D12 functionality.

The D3DResources namespace contains common functions to create D3D12 resources like (vertex, index, constant) buffers, textures, samplers, and heaps.

namespace D3DResources 
	void Create_Buffer(D3D12Global &d3d, D3D12BufferCreateInfo& info, ID3D12Resource** ppResource);
	void Create_Texture(D3D12Global &d3d, D3D12Resources &resources, Material &material);
	void Create_Vertex_Buffer(D3D12Global &d3d, D3D12Resources &resources, Model &model);
	void Create_Index_Buffer(D3D12Global &d3d, D3D12Resources &resources, Model &model);

The D3DShaders namespace contains functions to initialize the DXCompiler as well as load and compile shaders (including the new DXR ray tracing shaders).

namespace D3DShaders 
	void Init_Shader_Compiler(D3D12ShaderCompilerInfo &shaderCompiler);
	void Compile_Shader(D3D12ShaderCompilerInfo &compilerInfo, RtProgram &program);

The D3D12 namespace contains functions for common D3D12 setup operations like Device, Command List, Command Queue, Command Allocator, Fence, Swap Chain, and Root Signature creation. Note that small differences exist in Device and Command List creation when using DXR.

namespace D3D12 
	void Create_Device(D3D12Global &d3d);
	void Create_CommandList(D3D12Global &d3d);

Last but not least, the DXR namespace contains the new functionality specific to DirectX Raytracing. This includes acceleration structure creation, shader table allocation and population, ray tracing pipeline state object creation, and ray tracing shader loading and compilation.

namespace DXR
	void Create_Bottom_Level_AS(D3D12Global &d3d, DXRGlobal &dxr, D3D12Resources &resources, Model &model);
	void Create_Top_Level_AS(D3D12Global &d3d, DXRGlobal &dxr, D3D12Resources &resources);
	void Create_RayGen_Program(D3D12Global &d3d, DXRGlobal &dxr, D3D12ShaderCompilerInfo &shaderCompiler);
	void Create_Miss_Program(D3D12Global &d3d, DXRGlobal &dxr, D3D12ShaderCompilerInfo &shaderCompiler);
	void Create_Closest_Hit_Program(D3D12Global &d3d, DXRGlobal &dxr, D3D12ShaderCompilerInfo &shaderCompiler);
	void Create_Pipeline_State_Object(D3D12Global &d3d, DXRGlobal &dxr);
	void Create_Shader_Table(D3D12Global &d3d, DXRGlobal &dxr, D3D12Resources &resources);	

The application has a few command line arguments built-in, including:

  • -width [integer] specifies the width (in pixels) of the rendering window
  • -height [integer] specifies the height(in pixels of the rendering window
  • -model [path] specifies the file path to a OBJ model

Suggested Exercises

Once you have a good understanding of how the application works, I encourage you to dig deeper by removing limitations of the current code and adding new rendering features. A few suggested features:

  • Add antialiasing by casting multiple rays per pixel.
  • Add loading and rendering of models with multiple materials (only a single material is supported now)
  • Add realistic lighting and shading (lighting is currently baked!)
  • Add ray traced shadows. Extra credit: use Any Hit Shaders for shadow rendering
  • Add ray traced ambient occlusion.
  • Add ray traced reflections for mirror-like materials.
  • Add camera translation and rotation mapped to keyboard and mouse inputs.

If you’ve implemented the above features, you’ll be just a few steps away from your very own DXR path tracer! For extra bonus points, implement Ray Tracing In One Weekend.