Vulkan API Tutorial - 1 - New Project, Instance and Device

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the second Vulkan API tutorial in this tutorial i'm going to go through the instance creation and the logical device creation if you haven't seen my getting started tutorial yet you should go there and watch it out first because this tutorial is based on that ok so the first thing we want to do is to actually create our project and let us go here and create our empty projects and don't worry about the name too much not yet and let's add our main dot CPP file I think the easiest way is to just make a class and we go make a renderer class which is going to handle all the Valken commands so new class renderer let's include that and let's also create our main okay renderer I'm just going to call it our for now and this is basically all I'm going to do in this tutorial in this main file the rest of the time I'm just going to work on these two files here let's make a private section here I want to create functions to initialize and create the instance and the device so in its in its instance also make it void return nothing as well as T in it of the instance then in it device and the init device okay now the next thing we need is we need to actually use some Vulcan functions for those though we need to include the Vulcan libraries and header headers so let's include that in the e C++ directories and yeah this is all configurations all platforms for the include directory which is the Vulcan SDK in include there we go now for the library for the 32 and 64-bit applications we have two different libraries you know I save that and this is for the 64-bit executable yeah anyway let's go to the welcome libratory vulcan sdk this bin folder here has all the 64 bits executables and libraries which we need for the 64-bit executables so let's include that for the 32 bits we would include the let's go to the SDK again the bin 32 and then the next step is the linker input additional dependencies walking slash one dart Lib that's pretty much all we need now the renderer dot H file I'm going to include Vulcan Vulcan dot H ok for the next step let's create our function body folder in its instance what we want to do here is a VK create instance the first parameter is a VK instance create info structure the second parameter is a VK allocation callbacks stroke and the last one is a pointer to the instance and the last value in this case is the return value okay so let's create VK instance create info VK instance create info I'm going to name it a instance create info also I'm going to default initialize that because there is a lot of data we do not actually need to touch in here so I'm just going to do this and then only modify the fields I need so instance create info dot the first thing we need to modify is the S type structures sometimes has to have the variable or a member variable called s type if it does have it then you need to put certain numerator value into this this one so this one will always be BK structure type instance create info this instance create info only takes this value and nothing else if you put anything else in here it should fail although I did try that once and it didn't fail for some reason but if you have any validation layers or debugging enabled that debugging would report is if it wasn't correct value for this structure okay moving on since I already have this structure here it doesn't have anything in it but yeah I'm just going to give this pointer to this function already we'll do a little bit more of it this later but for now this is okay now the Falken allocation callbacks we're not going to do this and we won't walk into do its own memory management so I'm just Canole pointer here and that should do it so the last parameter the third one in this case is going to be our output we are going to give a pointer to this and it's going to return us the instance so I actually want to do this as a private member variable VK instance and I'm just going to call it instance the underlying data type of this VK instance is a pointer on this instance variable represents the Vulcan instance that we're creating right now so just to be safe I just put it to null pointer for now let's give it here yeah let's actually give it as a pointer and there we go one last step is to check the return value this return values that Walken returns is any one of these except these last ones here yeah this last four ones here are not actually any kind of error reports or anything like that it just tells you the range of this enumerator and those are of errors but basically what you need to know is zero is success a positive value is a success or partial success and the negative value is always a failure of some sort so weak a success is not error variable and so if it's not success which is gonna exit and I'm actually going to include CST T live here and there we go and if we run on the day debug mode I actually want to see the error so let's also include a search let's put the error message as a Vulcan error create instance failed okay now I want to make that day in it in yeah the in it instance function this is actually really easy just VK destroy instance will give it the instance here and again in here it once now if you if we want to use our own memory management and we don't so again null pointer here I actually want to be sure that the instance points to nothing after this so I'm just gonna set it as a null pointer and this is all you need to do to destroy the instance and let's see if this works obviously in the constructor though constructor we need to initialize their instance and D in it of course in their destructor so in its instance and D init instance okay let's see what happens and this is by the way 32 bits program right now so I'm actually not too sure if this works it seems to be working just fine but okay oh well nevermind I just put it a 64 now there's unresolved external symbol VK create instance okay I forgot to put the library here for the 64-bit executables - it's the same name but different folder walkins slash 1 dot lip try it let's run again ok it says it's AIT's a success also it gave us a pointer while the instance though they should be fine and it also calls a VK destroy instance so this works is fine I want to do this or take this a little bit further and actually make a VK application info structure because there's actually a option here in the instance create info to put a pointer to application info structure so I'm going to do this right here now there's a number of things we can find in here this type again is the first one if the structure has the S type you need to have it set to a certain value which in this case is VK structure type structure type and this is a V key application info structure so this is a structure type application info okay we can also give a lot more in there in here we can set like API version this API version tells the application or the Treiber which Vulcan version this application uses so let's put a VK API version this VK a PR version here is API version that you are currently using this is defined in the Vulcan H file and this actually makes Kotwal uses the VK make version 1.0 0.3 we can also put the engine version here and application version just for the kicks I'm gonna put the application version to ek make version major 0 minor 1 and patch 0 I'm also going to give this application name the P in front of these variables means that it's a pointer or pointer type I'm just going to call this own welcome tutorial one that just gives us or the Vulcan a bit more information about the program itself okay so the next step is to create the device the logical device that we can use to actually do all this rendering and other stuff in Vulcan to put it into perspective the logical device or VK device is equivalent of OpenGL context the difference the opengl in this case is that you cannot have to do the context by yourself and I'll show you what I mean so let's go and make the body is for the init device and D init device okay init device what we want to do is a VK create device so it wants a physical device it wants VK device creates infrastructure it wants the allocator yeah this is an old point of in here and it returns a device okay well let's do the easy steps first and let's make our device handle so how do we get a physical device handle is by VK enumerates physical devices and this wants an instance as well as amount of physical devices you don't like call any other function to get the count or amount of physical devices though user of this program has instead you call this same function twice if the last parameter is null pointer it returns the amount of physical devices to the second parameter and on the second time when you call this function it takes the list on the last parameter and populates it with the handles of all your physical devices and the second parameter in this case is an input which tells how many how large that list is so it sounds a bit weird when I say like that but let's just continue you ain't 32 underscore team I'm going to call this GPU column she's going to put a zero into this one I'm going to give that VK enumerate physical devices the pointer to this UN 32 T so GPU count on the last parameter I'm going to put a null pointer here this gets us how many GPUs you the user has and from here we can actually create our list of our GPUs I'm actually going to put this into brackets as or appraises and I'm gonna use let's use a vector for this STD vector of type r VK physical device now let's call this GPU list and I'm going to initialize the size of this with the GPU count okay that's our list now the weird part is that we're going to call this function again VK enumerate physical devices the instance GPU count here now on the last one we're going to give the pointer or the lists so GPU lists point Tatom and that populates our list with all the handles of all the GPUs we have for the simplicity and because it can actually be rather hard to pick just one GPU I'm just going to assume that the best possible option here is going to be the first one and I'm going to actually going to create a handle for this beak a physical device I'm just going to hold it GPU because it's a shorter GPU equals GPU list with a slot 1 + 0 okay so that gets you the first GPU this does not mean that it's the best one it just means that it's the first one on the list and yeah selecting the best one the most powerful one is up to you but this will work in any case for this tutorial anyways this will work just fine ok we have our GPU now we can just put it here done the next step is a bit more involved and that is VK device creates info ok let's start with a familiar one s type became structure type Dubois create info can't type there we go P next is reserved and flags I think it was reserved this is actually a good place to check so copy this name go to the vulcan 1.0 + WSI extension specification and check this one yeah is the definition of VK device creates info as type is the type of the structure P next is null or a pointer to extension specific structure so extension not layer two different things flags is reserved for future use so I'm not going to touch that one okay the two things we need to modify is a cue create infos and cue create info count these count variables are they'd actually mean account how many and I'm going to set the cue create info count as one because the pointer to cue create infos is going to be just one it's a list of cue create info sorry VK device cue create info and this is a pointer to a list or well a list not a pointer to a list but yeah you put a list in here since I'm just going to put one entry in this list I'm just going to use it as a pointer so let's create this VK device cue create info VK twice Q create in full okay and I'm actually going to give this pointer to this one already so I don't forget and as one I'm going to put a pointer of this two here this is a bit more involved because you need to actually find out what kind of properties the Q family house and I'm actually just going to show you so yeah this is easier if I just show you show it to you device Q create in film s type viic a structure type device Q create info P next is again reserved let's see if the flags flags is as soon as I find it there we go yeah flags is reserved for future use so Winnicott we are not going to touch that one either now is the three things we need to do here Q family index Q count and Q priorities and the best way I can put this so in vulcan in the GPU there are of q families and those families support different kind of things like traffics calculations or compute like general computing on the GPU you need to select one of these q families and then you put then you can lead to tell how many of those queues in that family you are going to use to keep it really simple I'm just going to select one Q family with only one Q that we're going to use so in this device creating full structure we are going to introduce one a Q create info structure and here's a pointer for it and then this one Q create info structure we are going to use one Q of one family so we need to know the Q family index and we don't know that yet we need to figure out which a family supposed graphics bits device Q creates info and the second parts how many cues of that family we're going to use I'm going to say one in here and then those cues in this family a harm I need to set the priorities of these cues you can submit work to these cues and they will run simultaneously more or less anyways these priorities tell the Vulcan driver which one of these cues take priority if both have work and basically it just tell us how much compute time is allocated to these cues these cue priorities is a list of floats so I'm just going to do this now float cue priorities list and since when we have one cue in this family that we want to use I only put one value into this list and then I'm just going to give it here so this only leaves one thing for us we need to figure out for this structure which family we are going to use and to figure this out we're going to have to ask Logan from this GPU what kind of families does it have so to get this we came get physical device cue family properties now this is a long name already so physical device here GPU and this kind of works the same way as VK enumerate physical devices that's kind of that's why I put it in the braces in this the second parameter to this function it's a number unsigned integer so you win32 t family count give it a pointer to this one and pattern puts a null pointer to the last one let's this gets us how many families there are in this GPU we will just create the vector for this SCD vector and this type is VK q family properties and i'm going to call this family property list it calls it a list I'm going to give it a size of families a family count and I'm going to call this function again so we can get physical device Q family properties GPU here pointer to this family count and then the pointer to the first lot of this list so family property list dot data okay so that was a lot to type in already what do we do with this list well we need to loop through this list and go through every single one of these Q families and check which one of these families support the traffic's bit so for loop if family property list of slot I point Q Flags this is a pit filled flag I'm in a bit field or bit flag anyway what we want to check here is this one VK q graphics bit we want this / - we want this family to have support for traffic's if we found this I'm just going to call it make a boolean here if we found this and also I'm going to make a new member variable and I'll call this so you in 32 team yeah let's call this a graphics family index so graphics family index equals I one last step is if we don't find a family that supports graphics bits or graphical operations then we need to close because there's nothing we can do it after that so if not found just can all cert here well can error q family q family supporting traffic's not found and exits obviously if you do your own proper application you want to do something else than just exit but just trying to keep it as simple as possible okay now the last step is to put the traffic's family index to this structure here and this should be all for the creation of the device the destruction is a fortunately a lot simpler so just walkin destroy device and we'll all come to do its own memory management's and just be safe I'm just going to put the device to null pointer okay that was a lot let's see if it works obviously it doesn't work because I forgot to initialize the device I also knit forgot to do you neutralize it there we go let's try again okay and member rates physical devices it says that I have one GPU and it gave me its handle so I got the handle to my GPU let's see where I can get I have one family Q family and it has 16 Q's in it also the flag value is 15 so this family supports all operations including graphical yep and it did give me device one more thing always check for check for errors now let's just muster this and exits after that and there we go these destroy functions they don't return any errors or anything at all just something to keep in mind okay that seems to work and there's a lot more you can do with this I think because Vulcan is based on mantle and mantle was created by AMD there are these families NVIDIA GPUs generally have only one family but you can't count on that so you have to go through all these families and find the one that supports the graphics graphics bits but generally NVIDIA has only one cue family with 16 cues in it and AMD has a few families and some of those families and we support compute and some of those families and we support graphics also this intel graphics card cards those Intel cards only have generally again only have one cue family with only one cue in it so again you can't expect that and you can't just expect any of this you have to check it every single time you actually create a device also there's a lot more we can do in here now that we have our GPU we can get like the C actually let's add VK let's out of physical device properties here can fries again okay GPU properties and let's initialize it with empty okay so in here we can also get the physical device properties so VK get physical device there's a lot of options in here features format properties image format properties memory properties let's get their properties for now physical device GPU here and then the pointer to the properties or the structure of properties now if I run this again it's going to fill this with information that we can check against so it gives me a device name here which is a gtx 780 which is correct device type is discrete GPU there are more options here of course that we can check these ones here actually physical device type hmm yeah here's all the options other integrated GPU discrete GPU which is a GPU that goes into a PCIe Express slot and that should be all about it you can look into this list see all the limits tells you everything in here all your GPU limits I hoped you liked this tutorial and I'll see about the next one soon
Info
Channel: Niko Kauppi
Views: 66,507
Rating: undefined out of 5
Keywords: vulkan, api, tutorial, project, instance, device, specification, graphics, programmin, gpu, graphics card, c++, cpp, code, coding, advanced, close to metal, glnext
Id: YhQZYnpFX_0
Channel Id: undefined
Length: 47min 26sec (2846 seconds)
Published: Sun Mar 06 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.