Enabling Raytracing - Part 1
Code so Far
Here is the code after this section
Enabling Raytracing - Part 1
Now that we have created a basic render using rasterization, the next goal is to create the same render using ray tracing, and a mechanism to switch between the two rendering modes. For this I followed the NVidia raytracing tutorial which is split into two parts.
In order to switch between the the two modes I needed to bind keyboard events from the Window to the Game class. Just like how the WM_Paint method is subscribed to currently, I added subscription callback mechanism for WM_KEYDOWN, and WM_KEYUP Window's events that the game class now subscribes to.
On space bar, the window is configured to switch between raytracing and rasterization. For now, since raytracing is not implemented, if the user presses space bar, it goes between using rasterization, and rendering nothing. This was updated within the render loop of the game.
After making these updates, this is the current state of the program:
After adding in the toggle, I began setting up the components needed to get raytracing up and running.
Before setting up the pipeline for raytracing, some initial things needed to be done. The first thing was to change the device and command lists class types we are retrieving during initialization. Raytracing APIs were included in ID3DDevice5, and ID3D12GraphicsCommandList4, so initialization of these objects was updated. I then created a method for checking whether the the device supports raytracing. If the application does not support it, the application asserts.
Setting up the Raytracing pipeline
Unlike how rasterization writes to a render target, the raytracing pipeline writes to an image using a unordered access view (UAV). With rasterization we execute a draw through Draw* methods on the command list. With Raytracing, we will execute the DispatchRays method. The number of rays we generate can be customized, but for our rendering we will draw one ray per pixel. The GPU pipeline for raytracing is depicted through this simplified control flow diagram:
In the ray generation shader, the the ray is created. The rays collision with primitives is calculated using an accelerated structure for speeding up collision traversal. The intersection and any hit are set to passthrough for this project. After computing the closest triangle hit, the closests hit shader is ran. If the ray never hit anything, the miss shader is ran. The output of the closest hit and miss shader is a color an distance. These outputs are returned to the RayGen which writes that pixel's color to the UAV.
Comments
Post a Comment