O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Vulkan

Book Description

Discover how to build impressive 3D graphics with the next-generation graphics API—Vulkan

About This Book

  • Get started with the Vulkan API and its programming techniques using the easy-to-follow examples to create stunning 3D graphics
  • Understand memory management in Vulkan and implement image and buffer resources
  • Get hands-on with the drawing process and synchronization, and render a 3D graphics scene with the Vulkan graphics pipeline

Who This Book Is For

This book is ideal for graphic programmers who want to get up and running with Vulkan. It’s also great for programmers who have experience with OpenGL and other graphic APIs who want to take advantage of next generation APIs. A good knowledge of C/C++ is expected.

What You Will Learn

  • Learn fundamentals of Vulkan programing model to harness the power of modern GPU devices.
  • Implement device, command buffer and queues to get connected with the physical hardware.
  • Explore various validation layers and learn how to use it for debugging Vulkan application.
  • Get a grip on memory management to control host and device memory operations.
  • Understand and implement buffer and image resource types in Vulkan.
  • Define drawing operations in the Render pass and implement graphics pipeline.
  • Manage GLSL shader using SPIR-V and update the shader resources with descriptor sets and push constants.
  • Learn the drawing process, manage resources with synchronization objects and render 3D scene output on screen with Swapchain.
  • Bring realism to your rendered 3D scene with textures, and implement linear and optimal textures

In Detail

Vulkan, the next generation graphics and compute API, is the latest offering by Khronos. This API is the successor of OpenGL and unlike OpenGL, it offers great flexibility and high performance capabilities to control modern GPU devices. With this book, you'll get great insights into the workings of Vulkan and how you can make stunning graphics run with minimum hardware requirements.

We begin with a brief introduction to the Vulkan system and show you its distinct features with the successor to the OpenGL API. First, you will see how to establish a connection with hardware devices to query the available queues, memory types, and capabilities offered. Vulkan is verbose, so before diving deep into programing, you’ll get to grips with debugging techniques so even first-timers can overcome error traps using Vulkan’s layer and extension features.

You’ll get a grip on command buffers and acquire the knowledge to record various operation commands into command buffer and submit it to a proper queue for GPU processing. We’ll take a detailed look at memory management and demonstrate the use of buffer and image resources to create drawing textures and image views for the presentation engine and vertex buffers to store geometry information.

You'll get a brief overview of SPIR-V, the new way to manage shaders, and you'll define the drawing operations as a single unit of work in the Render pass with the help of attachments and subpasses. You'll also create frame buffers and build a solid graphics pipeline, as well as making use of the synchronizing mechanism to manage GPU and CPU hand-shaking.

By the end, you’ll know everything you need to know to get your hands dirty with the coolest Graphics API on the block.

Style and approach

This book takes a practical approach to guide you through the Vulkan API, and you will get to build an application throughout the course of the book. Since you are expected to be familiar with C/C++, there is not much hand-holding throughout the course of the book.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Learning Vulkan
    1. Learning Vulkan
    2. Credits
    3. About the Author
    4. Acknowledgments
    5. About the Reviewer
    6. www.PacktPub.com
      1. Why subscribe?
    7. Preface
      1. What this book covers  
      2. What you need for this book 
      3. Who this book is for 
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code 
        2. Downloading the color images of this book 
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Getting Started with the NextGen 3D Graphics API
      1. Vulkan and its evolution
      2. Vulkan versus OpenGL
      3. Important jargons before we get started
      4. Learning the fundamentals of Vulkan
        1. Vulkan's execution model
        2. Vulkan's queues
        3. The object model
        4. Object lifetime and command syntax
        5. Error checking and validation
      5. Understanding the Vulkan application
        1. Driver
        2. Application
        3. WSI
        4. SPIR-V
        5. LunarG SDK
      6. Getting started with the Vulkan programming model
        1. Hardware initialization
        2. Window presentation surfaces
        3. Resource setup
        4. Pipeline setup
          1. Descriptor sets and descriptor pools
          2. Shaders with SPIR-V
          3. Pipeline management
          4. Recording commands
          5. Queue submission
      7. Summary
    9. 2. Your First Vulkan Pseudo Program
      1. Installing Vulkan
      2. The Hello World!!! pseudocode
        1. Initialization - a handshake with the device
        2. Swapchain initialization - querying the WSI extension
          1. Command buffer initialization - allocating command buffers
          2. Resource objects - managing images and buffers
          3. Creating a presentation surface - creating a swapchain
          4. Creating a depth image
          5. Resource allocation - allocating and binding device memory
        3. Supplying shaders - shader compilation into SPIR-V
        4. Building layouts - descriptor and pipeline layouts
        5. Creating a Render Pass - defining a pass attribute
        6. Framebuffer - connect drawing images to the Render Pass
        7. Populating geometry - storing a vertex into GPU memory
        8. Pipeline state management - creating pipelines
          1. Defining states
          2. Creating a graphics pipeline
        9. Executing the Render Pass - drawing Hello World!!!
          1. Acquiring the drawing surface
          2. Preparing the Render Pass control structure
          3. Render Pass execution
        10. Queue submission and synchronization - sending jobs
        11. Displaying with presentation layer - rendering a triangle
      3. Fitting it all together
      4. Summary
    10. 3. Shaking Hands with the Device
      1. Getting started with the LunarG SDK
      2. Setting up our first project with CMake
        1. How to build the CMake file
      3. Introduction to extensions
        1. Querying layers and extensions
      4. Creating a Vulkan instance
        1. Enabling layers and extensions
          1. Testing the enabled layers and extensions
      5. Understanding physical and logical devices
        1. Physical devices
          1. Enumerating physical devices
          2. Querying physical device extensions
          3. Getting the properties of a physical device
          4. Interrogating memory properties from the physical device
        2. Logical device
          1. Creating a logical device
          2. Waiting on the host
          3. Losing the device
      6. Understanding queues and queue families
        1. Querying queue families
          1. Storing the graphics queue handle
        2. Creating a queue
      7. Implementing devices and queues all together
      8. Summary
    11. 4. Debugging in Vulkan
      1. Peeking into Vulkan debugging
      2. Understanding LunarG validation layers and their features
      3. Implementing debugging in Vulkan
      4. Summary
    12. 5. Command Buffer and Memory Management in Vulkan
      1. Getting started with command buffers
        1. Explicit synchronization
        2. Types of command in command buffers
        3. Command buffers and queues
          1. The order of execution
      2. Understanding command pool and buffer APIs
        1. Creating a command pool
          1. Resetting a command pool
          2. Destroying a command pool
        2. Command buffer allocation
          1. Resetting command buffers
          2. Freeing command buffers
      3. Recording command buffers
        1. Queue submission
        2. Queue waiting
      4. Implementing the wrapper class for a command buffer
        1. Implementing the command buffer allocation process
        2. Recording the command buffer allocation process
          1. How to use command buffer recording functions
        3. Submitting the command to the queue
      5. Managing memory in Vulkan
        1. Host memory
        2. Device memory
        3. Allocating device memory
        4. Freeing up device memory
        5. Accessing device memory from the host
        6. Lazily allocated memory
      6. Summary
    13. 6. Allocating Image Resources and Building a Swapchain with WSI
      1. Getting started with image resources
        1. Image creation overview
      2. Understanding image resources
        1. Creating images
          1. Destroying the created images
        2. Understanding image layouts
        3. Creating an image view
          1. Destroying the image view
      3. Memory allocation and binding image resources
        1. Gathering memory allocation requirements
        2. Allocating physical memory on the device
        3. Binding the allocated memory to an image object
      4. Introducing swapchains
        1. Understanding the swapchain implementation flow
        2. The swapchain implementation's class block diagram
        3. Renderer - a window management custom class
          1. Creating the presentation window
          2. Initializing the renderer
          3. Creating the command pool
          4. Building swapchain and depth images
          5. Rendering the presentation window
        4. VulkanSwapChain - the swapchain manager
          1. Querying swapchain extensions
        5. Creating the surface with WSI and associating it with the created window
        6. The graphics queue with present support
        7. Querying swapchain image formats
        8. Creating the swapchain
          1. Swapchain surface capabilities and the presentation mode
          2. Managing presentation mode information
          3. Retrieving the swapchain's color images
          4. Creating color image views
      5. Creating a depth image
        1. Introduction to tiling
        2. Creating a depth buffer image object
        3. Getting the depth image's memory requirements
        4. Determining the type of memory
        5. Allocating and binding physical memory to a depth image
        6. Image layout transition
        7. Image layout transition with memory barriers
        8. Creating the image view
      6. Summarizing the application flow
        1. Initialization
        2. Rendering - displaying the output window
      7. Summary
    14. 7. Buffer Resource, Render Pass, Framebuffer, and Shaders with SPIR-V
      1. Understanding the Vulkan buffer resource type
        1. Creating the buffer resource object
          1. Destroying the buffer
        2. Creating a buffer view
          1. Destroying the buffer view
      2. Creating geometry with a buffer resource
        1. Preparing geometry data
        2. Creating a vertex buffer
        3. Buffer creation overview
        4. Implementing a buffer resource - creating the vertex buffer for the geometry
        5. Understanding the code flow
      3. Understanding a Render Pass
        1. Attachments
        2. Subpasses
        3. Vulkan APIs for the Render Pass
        4. Implementing the Render Pass
      4. Using the Render Pass and creating the framebuffer
        1. Implementing the framebuffer
      5. Clearing the background color
        1. Setting the background color in the Render Pass instance
        2. Rendering the colored background
      6. Working with a shader in Vulkan
        1. Introduction to SPIR-V
        2. Compiling a GLSL shader into SPIR-V
          1. Offline compilation with the glslangValidator executable
          2. Online compilation with SPIR-V tool libraries
        3. Implementing a shader
      7. Summary
    15. 8. Pipelines and Pipeline State Management
      1. Getting started with pipelines
        1. VulkanPipeline - the pipeline implementation class
      2. Caching pipeline objects with a PCO
        1. Creating a pipeline cache object
        2. Merging pipeline caches
        3. Retrieving data from pipeline caches
        4. Implementing the PCO
      3. Creating a graphics pipeline
        1. Implementing a graphics pipeline
        2. Destroying pipelines
      4. Understanding compute pipelines
      5. Pipeline State Objects (PSO) in Vulkan
        1. Dynamic states
          1. Implementing dynamic states
        2. Vertex input states
          1. Implementing vertex input states
        3. Input assembly states
          1. Implementing input assembly states
            1. Primitive restart
          2. Primitive topologies
            1. Primitives topologies with no adjacency
            2. Primitives topologies with adjacency
        4. Rasterization
          1. Rasterization states
          2. Implementing rasterization states
        5. Blending
          1. Color blend states
          2. Implementing color blend states
        6. Viewport management
          1. The viewport state
          2. Implementing the viewport state
        7. Depth and stencil tests
          1. Depth and stencil states
          2. Implementing depth stencil states
        8. Multisample states
          1. Implementing multisample states
      6. Implementing the pipeline
      7. Summary
    16. 9. Drawing Objects
      1. Overview of the drawing process in Vulkan
        1. Walking through the header declaration
      2. Preparing the drawing object
        1. Recording Render Pass commands
          1. Beginning Render Pass instance recording
          2. Transitioning to the next subpass
          3. Finishing Render Pass instance recording
          4. Implementation
        2. Binding pipeline object
          1. Implementation
        3. Specifying drawing object geometry information
          1. Implementation
        4. Defining a dynamic viewport
          1. Implementation
        5. Scissoring
          1. Implementation
        6. Draw command
          1. vkCmdDraw command
        7. Implementing drawing object preparation
      3. Rendering the drawing object
        1. Acquiring the swapchain image
        2. Executing the drawing command buffer object
        3. Displaying the output with the presentation engine
        4. Implementing drawing object rendering
      4. Rendering an indexed geometry
      5. Understanding synchronization primitives in Vulkan
        1. Fences
        2. Semaphores
        3. Events
      6. Resizing the display window
      7. Summary
    17. 10. Descriptors and Push Constant
      1. Understanding the concept of descriptors
        1. VulkanDescriptor - a user-defined descriptor class
        2. Descriptor set layout
          1. Implementing the descriptor set layout
          2. Destroying the descriptor set layout
        3. Understanding pipeline layouts
          1. Creating a pipeline layout
          2. Implementing the pipeline layout creation
          3. Destroying the pipeline layout
          4. Implementing the pipeline layout destruction process
        4. Descriptor pool
          1. Creating a descriptor pool
          2. Implementing the creation of the descriptor pool
          3. Destroying the descriptor pool
          4. Implementing the destruction of the descriptor pool
        5. Creating the descriptor set resources
        6. Creating the descriptor sets
          1. Allocating the descriptor set object from the descriptor pool
            1. Destroying the allocated descriptor set objects
          2. Associating the resources with the descriptor sets
          3. Implementing descriptor set creation
      2. How to implement Uniforms in Vulkan?
        1. Prerequisites
        2. Execution model overview
        3. Initialization
          1. Shader implementation
          2. Creating descriptors
        4. Rendering
          1. Binding the descriptor set
        5. Update
          1. Updating the transformation
      3. Push constant updates
        1. Defining the push constant resource in the shader
        2. Updating the pipeline layout with the push constant
        3. Updating the resource data
      4. Summary
    18. 11. Drawing Textures
      1. Image resource - a quick recap
      2. Prerequisites for texture drawing
        1. Specifying the texture coordinates
        2. Updating the shader program
        3. Loading the image files
          1. Using the GLI library
        4. Local image data structure
      3. Implementing the image resource with linear tiling
        1. Loading the image file
        2. Creating the image object
        3. Memory allocation and binding
        4. Populating the allocated device memory
        5. Creating the command buffer object
        6. Setting the image layout
        7. Submitting the command buffer
        8. Creating an image sampler
        9. Filtering
        10. Wrapping modes
        11. Creating the image view
      4. Implementing the image resource with optimal tiling
        1. Loading the image file
        2. Buffer object memory allocation and binding
        3. Populating the allocated device memory
        4. Creating the image object
        5. Image object memory allocation and binding
        6. Creating a command buffer object
        7. Setting the image layout
        8. Buffer to image copy
        9. Setting the optimal image layout
        10. Submitting the command buffer
        11. Creating an image sampler
        12. Creating the image view
      5. Copying data content between images and buffers
      6. Updating the descriptor set
      7. Summary