Reader small image

You're reading from  Mastering Graphics Programming with Vulkan

Product typeBook
Published inFeb 2023
PublisherPackt
ISBN-139781803244792
Edition1st Edition
Right arrow
Authors (2):
Marco Castorina
Marco Castorina
author image
Marco Castorina

Marco Castorina first got familiar with Vulkan while working as a driver developer at Samsung. Later he developed a 2D and 3D renderer in Vulkan from scratch for a leading media-server company. He recently joined the games graphics performance team at AMD. In his spare time, he keeps up to date with the latest techniques in real-time graphics.
Read more about Marco Castorina

Gabriel Sassone
Gabriel Sassone
author image
Gabriel Sassone

Gabriel Sassone is a rendering enthusiast currently working as a Principal Rendering Engineer at Multiplayer Group. Previously working for Avalanche Studios, where his first contact with Vulkan happened, where they developed the Vulkan layer for the proprietary Apex Engine and its Google Stadia Port. He previously worked at ReadyAtDawn, Codemasters, FrameStudios, and some non-gaming tech companies. His spare time is filled with music and rendering, gaming, and outdoor activities.
Read more about Gabriel Sassone

View More author details
Right arrow

Getting Started with Ray Tracing

In this chapter, we are introducing ray tracing into our rendering pipeline. Thanks to the addition of hardware support for ray tracing in modern GPUs, it’s now possible to integrate ray tracing techniques into real-time rendering.

Ray tracing requires a different setup compared to the traditional rendering pipeline, which is why we are dedicating a whole chapter to setting up a ray tracing pipeline. We are going to cover in detail how to set up a shader binding table to tell the API which shaders to invoke when an intersection test for a given ray succeeds or fails.

Next, we are going to explain how to create the Bottom Level Acceleration Structure (BLAS) and Top Level Acceleration Structure (TLAS). These Acceleration Structures (AS) are needed to speed up scene ray traversal and ensure that ray tracing can be performed at an interactive rate.

In this chapter, we’ll cover the following main topics:

  • Introduction to ray...

Technical requirements

Introduction to ray tracing in Vulkan

Ray tracing support in hardware was first introduced in 2018 with the NVidia RTX series. Originally, ray tracing support in Vulkan was only available through an NVidia extension, but later, the functionality was ratified through a Khronos extension to allow multiple vendors to support the ray tracing API in Vulkan. We are dedicating a full chapter just to the setup of a ray tracing pipeline, as it requires new constructs that are specific to ray tracing.

The first departure from the traditional rendering pipeline is the need to organize our scene into Acceleration Structures. These structures are needed to speed up scene traversal, as they allow us to skip entire meshes that the ray has no chance to intersect with.

These Acceleration Structures are usually implemented as a Bounded Volume Hierarchy (BVH). A BVH subdivides the scene and individual meshes into bounding boxes and then organizes them into a tree. Leaf nodes of this tree are the...

Building the BLAS and TLAS

As we mentioned in the previous section, ray tracing pipelines require geometry to be organized into Acceleration Structures to speed up the ray traversal of the scene. In this section, we are going to explain how to accomplish this in Vulkan.

We start by creating a list of VkAccelerationStructureGeometryKHR when parsing our scene. For each mesh, this data structure is defined as follows:

VkAccelerationStructureGeometryKHR geometry{
    VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR };
geometry.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
geometry.flags =  mesh.is_transparent() ? 0 :
    VK_GEOMETRY_OPAQUE_BIT_KHR;

Each geometry structure can define three types of entries: triangles, AABBs, and instances. We are going to use triangles here, as that’s how our meshes are defined. We are going to use instances later when defining the TLAS.

The following code demonstrates how the triangles...

Defining and creating a ray tracing pipeline

Now that we have defined our Acceleration Structures, we can turn our attention to ray tracing pipelines. As we mentioned previously, ray tracing shaders work differently compared to traditional graphics and compute shaders. Ray tracing shaders are setup to call other shaders according to the shader binding table setup.

If you are familiar with C++, you can think of this setup as a simple form of polymorphism: the interface of a ray tracing pipeline is always the same, but we can dynamically override which shaders (methods) get called at runtime. We don’t have to define all the entry points though.

In this example, for instance, we are going to define only a ray generation, the closest hit, and the miss shader. We are ignoring any-hit and intersection shaders for now.

As the name implies, the shader binding table can be represented in table form. This is the binding table we are going to build in our example:

...

Summary

In this chapter, we have provided the details on how to use ray tracing in Vulkan. We started by explaining two fundamental concepts:

  • Acceleration Structures: These are needed to speed up scene traversal. This is essential to achieve real-time results.
  • Shader binding tables: Ray tracing pipelines can invoke multiple shaders, and these tables are used to tell the API which shaders to use for which stage.

In the next section, we provided the implementation details to create TLASes and BLASes. We first record the list of geometries that compose our mesh. Next, we use this list to create a BLAS. Each BLAS can then be instanced multiple times within a TLAS, as each BLAS instance defines its own transform. With this data, we can then create our TLAS.

In the third and final section, we explained how to create a ray tracing pipeline. We started with the creation of individual shader types. Next, we demonstrated how to combine these individual shaders into a ray...

Further reading

As always, we have only provided the most relevant details on how to use the Vulkan API. We recommend you read the Vulkan specification for more details. Here is the list of the most relevant sections:

This website provides more details on Acceleration Structures: https://www.scratchapixel.com/lessons/3d-basic-rendering/introduction-acceleration-structure/introduction.

There are plenty of resources online about real-time ray tracing. It’s still a novel field and subject to ongoing research. A good starting point is provided by these two freely available books:

...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Graphics Programming with Vulkan
Published in: Feb 2023Publisher: PacktISBN-13: 9781803244792
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Marco Castorina

Marco Castorina first got familiar with Vulkan while working as a driver developer at Samsung. Later he developed a 2D and 3D renderer in Vulkan from scratch for a leading media-server company. He recently joined the games graphics performance team at AMD. In his spare time, he keeps up to date with the latest techniques in real-time graphics.
Read more about Marco Castorina

author image
Gabriel Sassone

Gabriel Sassone is a rendering enthusiast currently working as a Principal Rendering Engineer at Multiplayer Group. Previously working for Avalanche Studios, where his first contact with Vulkan happened, where they developed the Vulkan layer for the proprietary Apex Engine and its Google Stadia Port. He previously worked at ReadyAtDawn, Codemasters, FrameStudios, and some non-gaming tech companies. His spare time is filled with music and rendering, gaming, and outdoor activities.
Read more about Gabriel Sassone