Device Setup & Pipeline cont. - Vulkan Game Engine Tutorial 03

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to another vulcan tutorial last time we went over the graphics pipeline and created our shader programs for our vertex and fragment stages what is left to be done is configuring the fixed function pipeline stages and creating our vulcan graphics pipeline object as the next step we're going to download some code that i've written that encapsulates our vulcan device stop please don't run away just yet now personally i really dislike tutorials that make me download code it completely goes against why i follow tutorials in the first place but i think in this rare case it might be a better way to learn vulkan i'm trying something different here and can't promise it will be any better than the other vulcan tutorials out there but my intent is to make things more approachable by flattening out the steepness of vulcan's learning curve all of the code provided will eventually be examined rewritten or extended so the end result is that you've gotten to the same destination learning wise but did so by taking a much more enjoyable route rather than making you slog through all of this code now we can do so bit by bit with the context of when it makes sense to do so finally my goal with this series is that it can be used as a standalone resource appropriate for newcomers but also will work well as a next step for someone who completed the vulcan tutorial and wants to continue learning so if you wish to gain a line-by-line understanding of this code it is mostly just a combination of four sections from vulkantutorial.com i've provided links in the description below as optional reading alright so if you're still with me let's download the header and cpp file and put them with your other code follow the link in the description below to the google drive and download the header and implementation files now drag both files you downloaded into your project and open the my engine device header file at the top of the file change this include to be for your window class update the namespace to match the rest of your project then do a find and replace to change the my engine prefix to whatever you'd like so in my case i'm replacing all occurrences of my engine to lve in both my header and implementation files let's update the file names as well so first we update our device cpp file name and then our device header file name then in your device implementation file update the include at the top of the file to match what you just renamed your header to all right now if you scroll down to line 196 you should be getting a compiler warning that our window has no create windows surface member function yet and that's because it doesn't so navigate to your window header and just below your should close method let's add a new public method void create window surface which takes a vk instance and a vk surface khr pointer grab this function and go to your windows implementation and paste it in i'll add the class name and then in an if statement call glfw create windows surface and pass in the instance the window a null pointer for the allocator callback and the pointer to our surface variable check that its return value is not equal to vk success otherwise throw a runtime error fail to create windows surface and then i'll include standard accept at the top of the file now if we go back to line 196 of our device file we should have no more errors now when you copy and paste files into your project sometimes your ide doesn't add them to your build system i've had this problem when using xcode before so whatever build system you are using it's good to double check to make sure that your device file is actually being compiled now if you build and run your code everything should still work as it did before here's a brief overview of what this device class does for us in its constructor the first thing we do is create a vulcan instance this initializes the vulcan library and is the connection between our application and vulkan next we set up validation layers by default the vulkan api does extremely little error checking during operation meaning that even small errors won't be caught and can result in a crash or undefined behavior so when debugging we should enable validation layers to check for errors and then disable them for release builds when performance is critical next we create our surface which relies on glfw and is the connection between the window we've set up in the previous lessons and vulcan's ability to display results following that we pick the physical device our application will be using so the physical device is the graphics device in your system capable of working with the vulkan api this can be a discrete gpu like a 2080 ti or an integrated gpu in either case we need to pick the physical device that we will be using to run our program it's even possible to have your program use multiple devices at the same time but this makes handling resources way more complicated and not something we'll be doing next we have logical device creation which describes what features of our physical device we want to use and finally it's convenient here to set up a command pool for later use this will help us with command buffer allocation in an upcoming video so just to reiterate you're not supposed to understand all of this yet for now the only takeaways are is one we're setting up our connection with vulcan and picking a physical device in our system that is capable of working with the vulkan api and two we create validation layers that will help us catch errors now navigate to your pipeline header file and then include your device class next we're going to create a struct to help us organize some data we will use to configure our pipeline so in your namespace but outside of the class add a struct called pipeline config info this is going to contain the data specifying how we want to configure our pipeline the reason we're pulling this information out of the pipeline class is that we want our application layer code to be easily able to configure our pipeline completely as well as to share that configuration between multiple pipelines so just leave this struct empty for now and we'll come back to it later next change your pipeline constructor to take a device passed by reference and a const pipeline config info the ordering of your parameters doesn't matter i'm just putting the device at the start and the config info at the end because i like how it looks better also update your create graphics pipeline function signature to take the pipeline config info next i'll add a private member variable storing our device reference this could potentially be memory unsafe if our device is released from memory before our pipeline we could attempt to de-reference this dangling pointer which would crash our program so the only time you'll see me using a reference type member variable in this manner is when we have an implicit relationship that our member variable in this case the device will outlive any instances of the containing class that depend on it which makes sense because a pipeline fundamentally needs a device to exist in uml this type of relationship would be known as aggregation now underneath add a vk pipeline graphics pipeline variable this is the handle to our vulcan pipeline object then a vk shader module variable vert shader module for our vertex shader and another vk shader module for our fragment shader note that these vulcan object types are tight deft pointers in this case it is a good practice to check what these vulcan object types are sometimes it will be a pod struct at other times it will be a tight def pointer to a struct then underneath our create graphics pipeline function add void create shader module that takes in the shader code in the form of a vector of characters and a pointer to a shader module like with our vk pipeline vk shader module is also a pointer so this is in fact a pointer to a pointer which is what we want in this case because this function will be used to create the module and initialize the variable now since our pipeline class is responsible for managing the lifetime of these resources let's add a destructor with an empty implementation here just for now and this is also a good time to delete or copy constructors because we want to avoid accidentally duplicating the pointers to our vulcan objects and finally let's add a public static function for creating a default pipeline configuration so static pipeline config info as the return type and we'll use default pipeline config info for the function name as parameters we'll take a unin 32 type for both the width and the same for height all right now let's get to implementing copy our new constructor parameters but first i just realized the mistake i made this const shouldn't be here so remove the const before your device parameter and now copy our constructor and go to your implementation file and paste over your parameters initialize our device in a member initializer list so lve device device and then pass through your config info to the create graphics pipeline function and then also update the parameters for our create graphics pipeline as well all right now grab your function signature from our pipeline header paste it in and add our engine's pipeline class name we're going to create a local vk shader module create info variable and we'll call it create info this is going to be a common pattern you'll see throughout using the vulkan api where rather than calling a function with a bunch of parameters we instead configure a struct and call a function with a pointer to it so in this case there are three values we need to set the s type which is equal to vk structure type shader module create info the code size which is just the size of our vector array and lastly a pointer to our code we need to use a reinterpret cast on our data since vulcan expects a unin 32 type rather than a car array this is something to be careful with since you may have realized that an n32 and a character are not the same size however since our data is stored in a vector the default allocator already ensures that the data satisfies the worst case alignment requirement but if we were using a c style character array instead this cast wouldn't be valid in an if statement we call vk create shader module we use lvdevice.device which is a getter to the vulcan device handle that our device class encapsulates then a pointer to our create info a null pointer since we're not using any allocation callbacks and finally our shader module check that this is not equal to vk success otherwise throw some runtime error like fail to create shader module next let's grab our default pipeline config info function remove static and add your class name and start by making a config info local variable and then simply return it for now we will come back to this in just a moment but it will be nice to get our code back into state that it compiles so in your first app header include your device class and create a new member variable lve device and it takes our window as a constructor argument then for our pipeline constructor add the device as your first argument and as the last a call to our lve pipeline default pipeline config info function with our width and our heights as arguments and now if you build and run your program when we instantiate our lve device we output to the console what device extensions are available what extensions are required as well as which physical device was picked the required extensions are determined for us by glfw and are the extensions that it needs in order to display content to the window it creates if we take a look into our device class you can find where we output this information and optionally remove it if you'd prefer not seeing it every time your program runs okay so i've decided to split this video in two as it's already getting a bit long and it's been delayed enough as it is we'll finish configuring our pipeline in the next video which will be out soon and if you would like a step-by-step video going through this device code please leave a comment below and maybe i could make something like appendix a device setup for this tutorial series sometime in the future [Music] you
Info
Channel: Brendan Galea
Views: 18,811
Rating: undefined out of 5
Keywords: Vulkan, vulkan api, vulkan tutorial, 3d game engine, coding tutorial, vulkan coding, vulkan graphics, 3d graphics, programming, gpu, vulkan programming, vulkan game engine, vulkan game engine tutorial, vulkan engine tutorial, learn vulkan, how to code vulkan, Vulkan beginner, graphics beginner, vulkan noob, vulkan from scratch
Id: LYKlEIzGmW4
Channel Id: undefined
Length: 14min 6sec (846 seconds)
Published: Sun Dec 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.