Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Vulkan 3D Graphics Rendering Cookbook

You're reading from   Vulkan 3D Graphics Rendering Cookbook Implement expert-level techniques for high-performance graphics with Vulkan

Arrow left icon
Product type Paperback
Published in Feb 2025
Publisher Packt
ISBN-13 9781803248110
Length 714 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Alexey Medvedev Alexey Medvedev
Author Profile Icon Alexey Medvedev
Alexey Medvedev
Sergey Kosarevsky Sergey Kosarevsky
Author Profile Icon Sergey Kosarevsky
Sergey Kosarevsky
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Establishing a Build Environment 2. Getting Started with Vulkan FREE CHAPTER 3. Working with Vulkan Objects 4. Adding User Interaction and Productivity Tools 5. Working with Geometry Data 6. Physically Based Rendering Using the glTF 2.0 Shading Model 7. Advanced PBR Extensions 8. Graphics Rendering Pipeline 9. glTF Animations 10. Image-Based Techniques 11. Advanced Rendering Techniques and Optimizations 12. Other Books You May Enjoy
13. Index

Setting up Vulkan debugging capabilities

After creating a Vulkan instance, we can start monitoring all potential errors and warnings generated by the validation layers. This is done by using the VK_EXT_debug_utils extension to create a callback function and register it with the Vulkan instance. In this recipe, we’ll learn how to set up and use this feature.

Getting ready

Please revising the first recipe Initializing Vulkan instance and graphical device for details how to initialize Vulkan in your applications and enable the instance extension VK_EXT_debug_utils.

How to do it...

We have to provide a callback function to Vulkan to catch the debug output. In LightweightVK it is called vulkanDebugCallback(). Here’s how it can be passed into Vulkan to intercept logs.

  1. Let’s create a debug messenger to forward debug messages to an application-provided callback function, vulkanDebugCallback(). This can be done right after the VkInstance object has been created.
    ...
    vkCreateInstance(&ci, nullptr, &vkInstance_);
    volkLoadInstance(vkInstance_);
    const VkDebugUtilsMessengerCreateInfoEXT ci = {
      .sType =
        VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
      .messageSeverity =
        VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
        VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
        VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
        VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT,
      .messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
                     VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
                     VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT,
      .pfnUserCallback = &vulkanDebugCallback,
      .pUserData = this,
    };
    vkCreateDebugUtilsMessengerEXT(
      vkInstance_, &ci, nullptr, &vkDebugUtilsMessenger_);
    
  2. The callback code is more elaborate and can provide information about the Vulkan object causing an error or warning. However, we won’t cover tagged object allocation or associating custom data. Some performance warnings are suppressed to keep the debug output easier to read.
    VKAPI_ATTR VkBool32 VKAPI_CALL vulkanDebugCallback(
      VkDebugUtilsMessageSeverityFlagBitsEXT msgSeverity,
      VkDebugUtilsMessageTypeFlagsEXT msgType,
      const VkDebugUtilsMessengerCallbackDataEXT* cbData,
      void* userData)
    {
      if (msgSeverity < VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT)
        return VK_FALSE;
      const bool isError = (msgSeverity &
        VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0;
      const bool isWarning = (msgSeverity &
        VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) != 0;
      lvk::VulkanContext* ctx = static_cast<lvk::VulkanContext*>(userData);
      minilog::eLogLevel level = minilog::Log;
      if (isError) {
        level = ctx->config_.terminateOnValidationError ?
          minilog::FatalError : minilog::Warning;
      }
      MINILOG_LOG_PROC(
        level, "%sValidation layer:\n%s\n", isError ?
        "\nERROR:\n" : "", cbData->pMessage);
      if (isError) {
        lvk::VulkanContext* ctx =
          static_cast<lvk::VulkanContext*>(userData);
        if (ctx->config_.terminateOnValidationError) {
          std::terminate();
        }
      }
      return VK_FALSE;
    }
    

This code is enough to get you started with reading validation layer messages and debugging your Vulkan applications. Remember to destroy the validation layer callbacks just before destroying the Vulkan instance. Refer to the full source code for all the details https://github.com/corporateshark/lightweightvk/blob/master/lvk/vulkan/VulkanClasses.cpp.

There’s more…

The extension VK_EXT_debug_utils provides the ability to identify specific Vulkan objects using a textual name or tag to improve Vulkan objects tracking and debugging experience.

For example, in LightweightVK, we can assign a name to our VkDevice object.

lvk::setDebugObjectName(vkDevice_, VK_OBJECT_TYPE_DEVICE,
  (uint64_t)vkDevice_, "Device: VulkanContext::vkDevice_");

This helper function is implemented in lvk/vulkan/VulkanUtils.cpp and looks as follows:

VkResult lvk::setDebugObjectName(VkDevice device, VkObjectType type,
  uint64_t handle, const char* name)
{
  if (!name || !*name) return VK_SUCCESS;
  const VkDebugUtilsObjectNameInfoEXT ni = {
    .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
    .objectType = type,
    .objectHandle = handle,
    .pObjectName = name,
  };
  return vkSetDebugUtilsObjectNameEXT(device, &ni);
}
You have been reading a chapter from
Vulkan 3D Graphics Rendering Cookbook - Second Edition
Published in: Feb 2025
Publisher: Packt
ISBN-13: 9781803248110
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.
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 $19.99/month. Cancel anytime
Visually different images