Vulkan Cookbook
This is the code repository for Vulkan Cookbook, published by Packt. All the example workflows that are mentioned in the book are present in the package.
About the Book
Vulkan is the next generation graphics API released by the Khronos group. It is expected to be the successor to OpenGL and OpenGL ES, which it shares some similarities with such as its cross-platform capabilities, programmable pipeline stages, or nomenclature. Vulkan is a low-level API that gives developers much more control over the hardware, but also adds new responsibilities such as explicit memory and resources management. With it, though, Vulkan is expected to be much faster.
Related Books
Suggestions and Feedback
Click here if you have any feedback or suggestions.
Credits
Special thanks for authors and developers of the following projects and resources:
- tinyobjloader - A single-header library for loading Wavefront OBJ files.
- stb image - A single-header library for loading image files (other libraries are also available).
- Humus - A large collection of cubemaps (and other resources).
Please note!
Currently only Windows operating system is supported. Linux version is being prepared and should be ready soon.
Samples
Chapter 11 - Lighting
Sample showing how to implement a diffuse lighting algorithm calculated only at geometry's verices using vertex shaders.
Left mouse button: rotate the scene
This sample present the Phong specular lighting algorithm implemented on vertex and fragment shaders.
Left mouse button: rotate the scene
Here a normal mapping technique is presented and the model is lit using the specular lighting algorithm.
Left mouse button: rotate the scene
Sample presenting how to use cubemaps to render a transparent geometry that both reflects and refracts environment.
Left mouse button: rotate the scene
In this sample a basic shadow mapping algorithm is shown. In the first render pass a shadow map is generated. In the second render pass a scene is rendered and the data from the shadow map is used to check, whether the geometry is lit or covered in shadow.
Left mouse button: rotate the scene
Right mouse button: move the light
Chapter 12 - Advanced Rendering Techniques
Here it is shown how to draw a skybox, which simulates background - objects seen in a distance and/or a sky.
Left mouse button: look around
This sample presents a way of drawing sprites or bilboards - flat, textured quads that are always facing the camera.
Left mouse button: rotate the scene
Here an example of rendering particles is shown. Compute shaders are used to calculate positions of all particles in the system. Particles are rendered as flat bilboards (sprites).
Left mouse button: rotate the scene
This code sample shows one of the ways to draw a terrain. A complete graphics pipeline with all five programmable stages is used that tessellates the terrain near the camera to improve its complexity, with level of details faiding away with increasing distance from the camera, and with a flat shading lighting algorithm.
Left mouse button: rotate the scene
Mouse wheel: zoom in / zoom out
Sample presenting a fast and easy way to prepare an image postprocessing phase in a graphics pipeline - by using a fullscreen quad drawn already in a clip space. An edge detection algorithm is shown as on of the examples of postprocessing techniques.
In this code another postprocessing technique is shown that uses one of the Vulkan's specific features - input attachments, which allow reading data from render targets (attachments) in the same render pass.
Left mouse button: rotate the scene
Other
Code sample that shows basic Vulkan setup - instance creation, physical device enumeration and logical device creation.
Here a swapchain object is created, which allows us to render a scene directly to an application's window.
This example shows how to preapre a basic render pass - a description of attachments (render targets) needed to render a geometry.
Sample showing how to create a graphics pipeline, setup its multiple parameters and use it to draw a scene.
Here descriptor sets are introduced. They are required to setup an interface between application and a pipeline and to provide images (textures) to shaders.
Another example of using descriptor sets, but this time it presented how to prepare transformation matrices and provide them to shaders.
This code sample presents a very fast and easy way to provide data to shaders - push constants. Though the provided data may not be too big, they are ideal for performing frequent updates.
Here we can see how to create a graphics pipeline with tessellation control and evaluation shaders enabled responsible for increasing the complexity of a rendered geometry.
Sample presenting how to use geometry shaders and generate new primitives instead of those drawn in an application.
This code sample shows how to create a compute pipeline - the second type of pipelines supported in the Vulkan API. It allows us to perform mathematical computations.
Here a commonly used debugging technique is presented that uses geometry shaders to display normal vectors provided by the application.
In this example we can see how to setup a render pass, framebufer and a graphics pipeline to use depth attachment and enable depth test during drawing.
This code sample shows how to enable alpha blending (transparency) in a graphics pipeline.
Left mouse button: disable/enable blending
This sample shows an alternative for performing a postprocessing with a quad (two triangles). Here a single triangle covering the whole screen is used to apply a grayscale effect.
Recipes Library
Chapter 01 - Instance and Devices
-
14 - Selecting index of a queue family with desired capabilities
-
18 - Creating a logical device with geometry shaders and graphics queue
Chapter 02 - Image Presentation
-
03 - Selecting a queue family that supports presentation to a given surface
-
14 - Creating a swapchain with R8G8B8A8 format and a MAILBOX present mode
Chapter 03 - Command Buffers and Synchronization
-
13 - Checking if processing of a submitted command buffer has finished
-
14 - Waiting until all commands submitted to a queue are finished
Chapter 04 - Resources and Memory
-
15 - Using staging buffer to update a buffer with a device-local memory bound
-
16 - Using staging buffer to update an image with a device-local memory bound
Chapter 05 - Descriptor Sets
Chapter 06 - Render Passes and Framebuffers
-
06 - Preparing a render pass for geometry rendering and postprocess subpasses
-
07 - Preparing a render pass and a framebuffer with color and depth attachments
Chapter 08 - Graphics and Compute Pipelines
-
20 - Creating a pipeline layout with a combined image sampler, a buffer and push constant ranges
-
22 - Creating multiple graphics pipelines on multiple threads
Chapter 09 - Command Recording and Drawing
-
15 - Executing secondary command buffer inside a primary command buffer
-
16 - Recording a command buffer that draws a geometry with dynamic viewport and scissor states
-
19 - Increasing the performance through increasing the number of separately rendered frames
Chapter 10 - Helper Recipes
Download a free PDF
If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.