Learning Vulkan Discover how to build impressive 3D graphics with the next- generation graphics API—Vulkan Parminder Singh BIRMINGHAM - MUMBAI Learning Vulkan Copyright © 2016 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: December 2016 Production reference: 1121216 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78646-980-9 www.packtpub.com Credits Author Copy Editor Parminder Singh Gladson Monteiro Reviewer Project Coordinator Chris Forbes Ritika Manoj Commissioning Editor Proofreader Ashwin Nair Safis Editing Acquisition Editors Indexer Smeet Thakkar Rekha Nair Aaron Lazar Content Development Editor Production Coordinator Sachin Karnani Aparna Bhagat Technical Editor Graphics Murtaza Tinwala Abhinash Sahu About the Author Parminder Singh is a computation graphics engineer with Blackmagic Design, Singapore. He has been working and developing graphic applications in the fields of network simulations, geo-modeling, navigation, automotive, infotainment systems, image processing, and post-production for the past decade. His research interests include GPU programming for scalable graphics and compute applications, porting, and performance optimization techniques. He is a Vulkan, Metal and OpenGL ES trainer and has also authored OpenGL ES 3.0 Cookbook, Packt. His hobbies include traveling, light cooking, and spending quality time with his baby girl. Feel free to connect Parminder at https://www.linkedin.com/in/parmindersingh18 or you can reach him at http://openglescookbook.com. Acknowledgments I dedicate this to my sweet baby girl, Raskeerat, who was born at the same time as we started this project. With a little baby onboard, it's challenging to write a book; I am grateful to my beloved wife Gurpreet Kaur and my family for helping me deliver this project to the community. I extend my gratitude to Mr. Ulrich Kabatek and the entire graphics team of Continental Automotive; every member of the team had something to offer me to scale my vision of graphics. I am grateful to Blackmagic Design, who helped me extend my horizon to take GPU programming to a whole new level. I express my regards to Mohit Sindhwani and the whole of Quantum Invention's team. It was a great pleasure to work for them and also was a wonderful learning experience. I am highly indebted to Chris Forbes from Google; his expertise in the graphics domain has raised the bar of this title. I am highly impressed with his reviews and the quality of work he delivered. Chris reviewed this title inch-by-inch and helped us not only to improve the contents but also our understanding of the concepts with his detailed explanation. Last but not the least, I am thankful to the entire division of Packt, especially Sachin Karnani, who constantly remained involved during the production of this title. Murtaza Tinwala, who brilliantly exhibited his content management and technical skills during the final stages. I'm really happy to have them work with me on this book. About the Reviewer Chris Forbes works as a software developer for Google, working on Vulkan validation support and other ecosystem components. Previously he has been involved in implementing OpenGL 3 and 4 support in open source graphics drivers for Linux (www.mesa3d.org), as well as rebuilding classic strategy games to run on modern systems (www.openra.net). www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https://www.packtpub.com/mapt Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser Table of Contents Preface 1 Chapter 1: Getting Started with the NextGen 3D Graphics API 8 Vulkan and its evolution 8 Vulkan versus OpenGL 10 Important jargons before we get started 12 Learning the fundamentals of Vulkan 13 Vulkan's execution model 13 Vulkan's queues 15 The object model 16 Object lifetime and command syntax 16 Error checking and validation 17 Understanding the Vulkan application 17 Driver 18 Application 18 WSI 18 SPIR-V 18 LunarG SDK 19 Getting started with the Vulkan programming model 19 Hardware initialization 20 Window presentation surfaces 21 Resource setup 22 Pipeline setup 25 Descriptor sets and descriptor pools 25 Shaders with SPIR-V 26 Pipeline management 26 Recording commands 27 Queue submission 29 Summary 30 Chapter 2: Your First Vulkan Pseudo Program 31 Installing Vulkan 31 The Hello World!!! pseudocode 32 Initialization – a handshake with the device 33 Swapchain initialization – querying the WSI extension 37 Command buffer initialization – allocating command buffers 40 Resource objects – managing images and buffers 40 Creating a presentation surface – creating a swapchain 42 Creating a depth image 44 Resource allocation – allocating and binding device memory 46 Supplying shaders – shader compilation into SPIR-V 47 Building layouts – descriptor and pipeline layouts 48 Creating a Render Pass – defining a pass attribute 50 Framebuffer – connect drawing images to the Render Pass 50 Populating geometry – storing a vertex into GPU memory 51 Pipeline state management – creating pipelines 55 Defining states 55 Creating a graphics pipeline 57 Executing the Render Pass – drawing Hello World!!! 58 Acquiring the drawing surface 58 Preparing the Render Pass control structure 59 Render Pass execution 59 Queue submission and synchronization – sending jobs 61 Displaying with presentation layer – rendering a triangle 62 Fitting it all together 63 Summary 64 Chapter 3: Shaking Hands with the Device 66 Getting started with the LunarG SDK 67 Setting up our first project with CMake 68 How to build the CMake file 73 Introduction to extensions 74 Querying layers and extensions 75 Creating a Vulkan instance 80 Enabling layers and extensions 84 Testing the enabled layers and extensions 86 Understanding physical and logical devices 88 Physical devices 88 Enumerating physical devices 90 Querying physical device extensions 91 Getting the properties of a physical device 92 Interrogating memory properties from the physical device 93 Logical device 93 Creating a logical device 94 Waiting on the host 95 Losing the device 96 Understanding queues and queue families 96 Querying queue families 97 Storing the graphics queue handle 99 Creating a queue 100 [ ii ]