Reader small image

You're reading from  C++ Game Animation Programming - Second Edition

Product typeBook
Published inDec 2023
Reading LevelN/a
PublisherPackt
ISBN-139781803246529
Edition2nd Edition
Languages
Tools
Concepts
Right arrow
Authors (2):
Michael Dunsky
Michael Dunsky
author image
Michael Dunsky

Michael Dunsky is an educated electronics technician, game developer, and console porting programmer with more than 20 years of programming experience. He started at the age of 14 with BASIC, adding on his way Assembly language, C, C++, Java, Python, VHDL, OpenGL, GLSL, and Vulkan to his portfolio. During his career, he also gained extensive knowledge in virtual machines, server operation, infrastructure automation, and other DevOps topics. Michael holds a Master of Science degree in Computer Science from the FernUniversität in Hagen, focused on computer graphics, parallel programming and software systems.
Read more about Michael Dunsky

Gabor Szauer
Gabor Szauer
author image
Gabor Szauer

Gabor Szauer has been making games since 2010. He graduated from Full Sail University in 2010 with a bachelor's degree in game development. Gabor maintains an active Twitter presence, and maintains a programming-oriented game development blog. Gabor's previously published books are Game Physics Programming Cookbook and Lua Quick Start Guide, both published by Packt.
Read more about Gabor Szauer

View More author details
Right arrow

3

Building a Vulkan Renderer

Welcome to Chapter 3! In the previous chapter, we took a deeper look into OpenGL as a method to get some polygons onto your screen. In this chapter, we will move on to its successor, Vulkan, which aims to give you much more control of your graphics hardware, thus resulting in improved performance.

Vulkan is a quite complex and also verbose API. You will have to create a lot of objects to get even a single colored triangle onto your screen, resulting in the creation of hundreds of C++ lines before you see anything. But you also get advanced error handling and debugging with an extra validation layer, allowing you to easily see where you have missed something or where an operation failed.

Due to the extensive amount of code needed for the basics, this chapter gives only a broad overview of the internals of Vulkan, plus some code snippets to explain how to initialize some of the objects. The complete rendering code for this chapter can be found in...

Technical requirements

For this chapter, you will need the Vulkan SDK, installed according to the Getting the source code and the basic tools section of Chapter 1.

Basic anatomy of a Vulkan application

Vulkan was released in 2016 as a successor to OpenGL. The goal was to develop a modern, scalable, low-overhead, cross-platform, 3D graphics API capable of matching the growing number of processors in computers and polygons in games and graphics applications. At the same time, the development of new features for OpenGL had slowed down. The latest version, 4.6, was released in 2017, and it will be still maintained for many more years, but we should look at the changes Vulkan brings to the 3D rendering process.

This is a picture of the – more or less – most important required objects to draw colorful triangles on the screen. Additionally, approximately 30 Vulkan C-style struct definitions must be constructed to create these objects:

Figure 3.1: Main Vulkan objects and their dependencies

Figure 3.1: Main Vulkan objects and their dependencies

We will take a closer look at these objects and their functions in the rendering process:

  • OS Window: This is...

Differences and similarities between OpenGL 4 and Vulkan

It shouldn’t be a surprise that Vulkan is unable to create any kind of rendering miracles when used instead of OpenGL, as the underlying hardware remains the same. However, there are a number of improvements in the management of the GPU.

Let’s take a look at some of the most visible points.

Technical similarities

These are a few technical similarities – things you may find familiar when switching from OpenGL to Vulkan:

  • The framebuffer works quite similarly in Vulkan and OpenGL. You create a special object and attach one or more textures (images in Vulkan) to it, and the GPU renders the picture to it.
  • If you use deferred rendering, a technique where different intermediate steps write their passes into buffers, this is similar to a Vulkan render pass and its subpasses.
  • The shader stages of the GPU are the same. We are using only vertex and fragment shaders, but the remaining stages...

Using helper libraries for Vulkan

Having full control of your graphics hardware sounds cool, but the extensive amount of code for the basic initialization might scare people who are new to Vulkan. Writing about 1,000 lines of code just to get a colored triangle onto the screen may sound frightening.

To reduce the code a bit, two helper libraries are integrated:

  • vk-bootstrap, the Vulkan Bootstrap, which is for the first steps of creating the instance, device, and swapchain
  • The Vulkan Memory Allocator (VMA), taking some of the complexity out of the memory management out of the code

We start with the simplification of the creation of the most important objects.

Initializing Vulkan via vk-bootstrap

If you visit the GitHub page for vk-bootstrap at https://github.com/charles-lunarg/vk-bootstrap, the benefits are listed right at the top of the README file. It will help you with all the steps needed for the following:

  • Instance creation, enabling the validation...

Fitting the Vulkan nuts and bolts together

Going from zero to hero in Vulkan is not hard. It takes a couple of hours if you copy and paste the example code from one of the tutorials, and even longer if you type it. Once you understand the roles and dependencies of the objects, your adventure begins.

The example code for the Vulkan renderer can be found in the chapter03 | vulkan_renderer folder of this book’s GitHub repo.

We start with the basic classes of the Vulkan renderer.

General considerations about classes

Similar to the OpenGL renderer, the creation and management of some Vulkan objects has been moved to separate classes. This helps to avoid creating a so-called “god class,” which is huge and a maintenance nightmare for developers, and it enables us to quickly add more objects of a specific type. In Chapter 4, we will create new shaders and switch between them, and having a separate Pipeline class to hand makes this possible with a few changes...

Summary

In this chapter, you learned how a Vulkan renderer can be created. After an overview of some of the objects and the relations between them, two helper libraries were introduced. Using vk-bootstrap and the VMA helps to reduce the number of lines of code needed for a renderer, and there are more abstraction layers that can be used to make it even more compact.

In the last section of the chapter, we inspected the implementation details of some of the Vulkan objects. You will find the pattern with one or more VK_STRUCTURE objects carrying the desired properties of all Vulkan objects, making it easy to read the code once you get into it. Even if you will not extend the code or experiment with it, knowing details about the structures and objects of Vulkan should help you to build a mental model of the components needed for the rendering process in our game character animations.

In the next chapter, we will dive deeper into vertex and fragment shaders. You will learn how they...

Practical sessions

As in the previous chapters, here are some ideas to try out with the example Vulkan rendering code. If you break anything, just roll back to the downloaded version:

  • Add some more triangles to the model class. You can achieve this by adding more vertex and texture lines to std::vector. See what happens if you change the last value of the glm::vec3 position of the vertices. This is the z-value (or depth) of the vertex. The renderer has a depth buffer as an attachment on the framebuffer. The current ordering of the triangles from front to back should be independent of their position in the vertex’s vectors of VkMesh.
  • Use another image type as a texture, such as a JPG file from your system. You will break the display for sure because the number of channels is hardcoded for a PNG file (four channels: red, green, blue, and transparency), and a JPG file has no transparency. The stbi_load() function returns the number of channels of the loaded image, adding...

Additional resources

For more information, take a look at the following:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
C++ Game Animation Programming - Second Edition
Published in: Dec 2023Publisher: PacktISBN-13: 9781803246529
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
Michael Dunsky

Michael Dunsky is an educated electronics technician, game developer, and console porting programmer with more than 20 years of programming experience. He started at the age of 14 with BASIC, adding on his way Assembly language, C, C++, Java, Python, VHDL, OpenGL, GLSL, and Vulkan to his portfolio. During his career, he also gained extensive knowledge in virtual machines, server operation, infrastructure automation, and other DevOps topics. Michael holds a Master of Science degree in Computer Science from the FernUniversität in Hagen, focused on computer graphics, parallel programming and software systems.
Read more about Michael Dunsky

author image
Gabor Szauer

Gabor Szauer has been making games since 2010. He graduated from Full Sail University in 2010 with a bachelor's degree in game development. Gabor maintains an active Twitter presence, and maintains a programming-oriented game development blog. Gabor's previously published books are Game Physics Programming Cookbook and Lua Quick Start Guide, both published by Packt.
Read more about Gabor Szauer