Rendering Context | Game Engine series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is Jenna welcome back to my game engine series feel sore good to say that it's been so long since the last episode I am now married so finally we can get back to this game engine am i right that's what all have you've been thinking I mean I'm very happy with the wedding everything went really really well and all of that can't say that the game engine was on my mind but it is now so we're gonna get right back into it and we're finally gonna start on the rendering because I feel like rendering whether the huge kind of thing that I was waiting to do and didn't have time but now I've kind of got a lot more time so we're going to just jump right on into this and begin our journey to creating a render up for hazel first of all just want to give a huge thank you to all the patreon supporters that actually made this series possible you can head on over to patreon and conversation the churner and help support the series there you'll get stuff like access to the source code that I've already written for hazel which is kind of my own private development branch version of hazel looks a lot more advanced than what you see here and like very very many videos essentially ahead and you'll also get access to a private channel so you can talk to me and do a whole bunch of other stuff so huge thank you as always for those people who are generous enough to support this series okay so rendering the first thing that we need for our rendering we kind of went over the rendering architecture of hazel if you guys haven't seen that video definitely check that out last episode as well was the call kind of making hazel into a static library and cleaning everything up I'm really really happy that we did all that because now I mean this compiles with zero warnings everything is just ready to go it's a static library it's all kind of just ready for us to continue development and that's a really good state to kind of be in for something as big as the renderer and I mean as much as I'd like to just write a renderer from start to finish there's obviously going to be exhilarating stuff that we kind of do like I will probably at some point I'd want to write some I'm GUI kind of widgets or just like maybe even use this kind of docking library that we have and I am going to create somewhat of like an editor or an interface it's not gonna be like bang let's just go from nothing to having like a full kind of PR deferred renderer that does everything in post-processing and everything just kind of without taking a break that there are going to be bits in which I'm like a cable now let's focus on something else so I just don't want you guys to be too disappointed if that happens I'm going to write this renderer as I would write a renderer there's not going to be any kind of like modification that I'm doing just because this is a video series I'm just gonna kind of do it my way and hopefully that's kind of something that you guys will be into but we'll see obviously I'm always open to feedback and the comments section is really important to me I mean I used to just like look at dis and copy the code that I wrote earlier but clearly you guys didn't like that so I now write all the code live to the point where I actually don't even know what code I want to write today but hopefully it's gonna be good anyway enough chitchat let's talk about what we're actually doing today so the first thing that we need for a renderer is a context we need a rendering context now we have rendered have we rendered a triangle and OpenGL in hazel before I don't actually know cuz I don't remember but we need a rendering context again that rendering architecture video is really useful and kind of cover the reason I keep talking about it is because it kind of covers the fact that we need a rendering API like a renderer and then a rendering API light right this is part of the rendering API this is something that's going to be different for each renderer that we actually use or for each API that we use so by API I mean things like OpenGL DirectX development that kind of stuff right and we need to wait a credit context for that API so in OpenGL it's really really simple and in fact gfw handles pretty much all of it for us it's super simple for Vulcan it's a lot more complicated like everything else is right we need to create apart from like doing things like picking the logical and physical device which means just picking which GPU to use and then creating like a and I'm a Vulcan side device for it and all that stuff we also need to create things like the swap chain this this is a lot more simple because we're dealing with OpenGL to begin with don't worry we will get developing hazel will support Vulcan at some point in the future and we'll kind of cross that bridge when we get that but for now in OpenGL it's dead simple so I'm not going to create a billion classes right now our billion files to kind of do different abstracted things until we actually need to but I will create an OpenGL context kind of file which will contain everything that OpenGL needs to actually render things and we're gonna kind of decide how we abstract that away from basically the window class in which it can't it kind of lives there right now um because obviously we have written the kind of context creation and also this watch and code what else is there to say really I just want to jump into hazel right now and take a look at if we have a triangle if we can get a triangle on the screen and then we'll start actually kind of stripping that code out and creating our context class and we'll kind of talk about why we're doing that and how it's gonna work as well so let's just jump on over into the code and see what we have alright so this is the window class this is the windows window class to be more specific so inside our initialization code we kind of have these three lines right we have this make context current if we look at what that actually does you'll see that based basically this winter has a context class right this is very similar to what we're actually doing now we've got a winter class that's kind of our you know Windows window in this case but then what we don't have and what we need and what the purpose of this episode is is to create this context class and this context class is gonna basically contain context specific so graphics kind of rendering context specific so in other words OpenGL specific in a sense code right so this window context make current again is something that yep W will do based on the context that you're using it could be OpenGL shelf so view also supports the Vulcan that's essentially what that will do it will just kind of take the window handle and then set that window handle as the rendering target or context for the rendering API that we're actually using and jela dolly will kind of do that for us so that we don't actually have to touch like windows specific code to set up an OpenGL kind of context - same with Vulcan as well instead of kind of retrieving certain pointers from the Vulcan dll's to set this up for Windows and that's you know done via essentially catch our extensions Chronos extensions instead of doing all that stuff kind of does it for us which is great for Vulcan we probably will use jewel of W as well although in my kind of own personal Vulcan projects I've always done everything manually so will will say again as I said we'll cross that bridge when we get there we're doing open gel for now because it's it's the most simple API to use and then there's swap buffers right which is a swap chain I'm talking about again super simple it does it for us right so we essentially need to call this line of code and this line of code but I don't want to do it from the window I want to do it from the actual context class and you might be like why why are we doing that if gfw already abstract stuff for us why are we adding another layer of abstraction the reason is this is definitely not all we want to support jela probably supports OpenGL and Vulcan which is fantastic but what we actually need is also support for DirectX and metal right possibly even other API is if we like I don't know go onto the ps4 or anything I want to kind of future proof this basically is what I'm saying DirectX 100% be supported metal as well almost definitely but way further down in the future so because of that we can't just have gel W make contacts current here right because this implies that every window is window that we ever make will initialize they shelved on the context shelf so V context means that it's going to be either open jail or Vulcan so how on earth from this code do we say we want a DirectX context we can't write and that's a problem which is why this needs to be abstracted out into a class that we're gonna call OpenGL context essentially and then this of course also loads OpenGL kind of extensions right again not something that needs to be done for DirectX off of even for Vulcan so this stuff is really kind of API specific and that's kind of a goal it's gonna be to kind of move that stuff out and create it and kind of put it into its own class which again will the implementation of that class will be dependent on which API will actually pick and also I mean api's are gonna be picked at runtime as well right so at runtime we'll be able to decide on Windows for example if we want OpenGL Vulcan or DirectX since that platform sports all three and then if we scroll down a bit and on in on up there we've got Paul events which is great but then we've got swap buffers again this is kind of the window on update function this is when we want to refresh our window so that's not something that really kind of you know this shelf Obispo Perez is not something that we really do for you know indirect tax or metal or whatever we're using I mean it's not gonna be metal right because this is windows window but anyway for DirectX not something we want it'll be completely different right so because of that we need to we can't just do gel W swap buffers we need to essentially do something like M context you know not swap offers now this might even be something like you know if we wanted to really be cool it might have even been contacts now it gets WAP shame and then you know flush or I don't know it might be the API will probably change over time especially if we decide to implement Vulcan as it's like kind of not a through gel view kind of route because that does simplify things but we'll see you know in that case we might have to do stuff like there's essentially and we might have to set up abstractions for all the api's to be able to do that but for now I think that's pretty good context you know swap buffers kind of makes sense and it's clearly gonna touch the swap chain so I'm pretty happy with this kind of API for now so we'll kind of just leave that there just so that we can fill in the blanks in a minute I'm here as well pretty simple it could definitely be something like context equals new OpenGL context and now that will kind of just create the context there I don't actually want to do that there I'm actually going to the context is probably gonna be a stock variable that just gets initialized automatically and may perform some checks to make sure it's actually valid I'm just thinking about how I want to do this mmm now having a stack variable probably isn't the best thing to do since it will actually need to be kind of polymorphic in the sense that like we might have a derived class that is depending on the API that we actually choose there's also gonna have to be some kind of context selection so I think that ultimately speaking context will most likely be just a pointer in which case we'll just we'll leave an arrow here for now context might even be something that's static yeah the ownership of contacts is something that we haven't really thought about that's kind of important because you know it should the window essentially own the context that's that's one way of doing it if we look at window I usually like doing that having the window earn the context I think I've done that and pretty much everything I've ever written but I still do like the context to be something that is static because it's not something that you know a rendering context you almost always have one of them at least per window right you're not gonna have multiple rendering contexts just trying to think I mean there are advanced things you could do that there's always things that you can do right I just want to make that clear if I just pop back from the screen to the camera to even say this there are always things that you can do that you might want to kind of modify this design for I want to kind of make that explicit as well designs that we decide on right now are not going to be final they're gonna be liable to change just like anything ever one thing I can think of is some scenarios is you you might you might want to do something like have a window and then inside that window have like kind of a split view maybe and then half of its being rendered using DirectX half of its being rendered using Balkin that's something that the people have done before in fact EA is kind of Halcyon like is the seed kind of research engine that EA has is a public kind of PowerPoint presentation that I can link in the comment section below if people are interested more in the description that that's something that for example does DirectX and valve and rendering at the same time in the same kind of frame like in the same window as side by side so that they can as they developed you know Vulcan for example they can kind of compare it easily to the direct export render and see if there are any kind of discrepancies between the tree rendering kind of visuals essentially so there are always things that you can do like that might break this design but for what we're doing right now at least I feel like a contact having a static context is pretty reasonable what you're going to do in hazel is you're going to kind of open up a window you're gonna create a in context for say DirectX and that's it you really don't have any reason to change if you do have a reason to change you're going to basically recreate the whole thing right so you're gonna be like I'm going to tear down my window then I'm gonna create a new window which might use Vulcan or something like that right and maybe in that in that sense you could even have two windows open that would use that but again just to keep things simple for now because we could spend hours discussing this in fact we've already been discussing this a lot but I think that what we really should do and could do for now is just basically keep it simple and say that we have a window we have a rendering context that's it we have one rendering context that's what you use to render in hazel it's one context dumb ok let's get back into it ok so that's what I'll kind of do for now I think and then we can also create kind of a static interface to that context so that we can access it from anywhere in the future but what I will do for now is inside windows window I will probably create essentially some kind of rendering context so we'll say rendering context mmm I'm not even sure what to call this because probably a graphics context will make it a pointer and context right and then this is something that return windows a window in it you know meet we might want to say at this point in time based on a property even you could define it but what I'm gonna do is you say that the rendering context right now it's going to be a new OpenGL context or a graphics context OpenGL we can just call it OpenGL context it was obviously going to be graphics um I'll put that onto the poor infer this doesn't it doesn't really matter what's line of code it's gonna be because I'm not gonna do anything in this initialization like in this constructor we're not gonna do anything so it doesn't matter if it's really before or after jail of W we might want to put it after but not really that important because over here is where it's actually going to be initialized and that's where we're going to do the following code right so this now kind of belongs inside that line of code right and then we're gonna do all that and then obviously we're gonna do this what buffers thing which is going to be inside contact right over here okay so now we're pretty much done let's go ahead and create that so what I want to do is in inside you know well let's just make sure we're in show of files yep we don't actually have anything to do with the rendering so I'll add a new folder called renderer and then inside there I'm gonna make a header file called graphics contacts dot H we're gonna obviously put this into the hazal namespace graphics contacts and then inside opengl I'm also going to create a new item and I had a file it's gonna be called OpenGL contacts and then we're going to also create a C++ file which is called OpenGL contacts that's going to include our PCH and the OpenGL context file name space hazel just copy that put it into the header file class OpenGL context public graphics context maybe we should have called the rendering context because the renderer anyway I really could not care less about names right now we might change change them depending on my kind of mood but we'll just say this is gonna be in hazel Brannon Road rap this contest cool so that's our context essentially that we could probably could just keep it cold we could is called context um puts office inside the renderer but because we're not using namespace is like a renderer I thought it'd make more sense to actually call it a graphics context this is gonna be again just a normal kind of interface which will have to send things like in this and also probably swap buffers very very bland for now I'll make these things your virtual right very bland for now very simple we're gonna keep it as simple as we need to because well first of all rendering can get complicated in math and I want to try my best rather serious kept as simple as possible right but also because OpenGL really is simple now especially with GL of W so this obviously is going to overwrite this stuff now here's the thing clearly when we create something like a note from your contact we're going to need some kind of handle to the window so that we can actually control things right basically we're gonna just take in a gel W window handle and I'm actually going to do this from the constructor okay so the constructor is just kind of going to underscore this job double window the constructor or maybe we can just pull this window handle for now that way if we change from gel W will still be a window handle it's kind of kind of makes sense so basically what I'm saying and what I might do is just make a forward declaration watch it off W window here so we don't have to include it so basically what we're gonna do here is just set that property and window handle all right that's what we're gonna do in the constructor which also means that we need to go into windows wind over here I'll include our context renderer graphics context hazal do that there this thing I'll include in OpenGL context LCVP I don't know why it's not toggling between header file and it's a file but anyway so we've got the constructor we've got a neat this is OpenGL context we can remove the pure virtual stuff and add that it's an override and then from our from our actual window let's not just open that we don't need from our actual window that's where we're actually going to set our window so M window and obviously we're going to do that after we actually create the window so it'll create the context and then we'll initialize that context okay so OpenGL context it's not happy with that so got graphics context and then over here what we will use hazel or platform where is it it's not it's just platform is it yeah platform OpenGL OpenGL context alright cool that should make most things go away here all the arrows yeah cool alright and that's pretty much it so we have the window and now we can kind of do everything we need and that looks pretty good to me so we'll cop we'll grab this line of code we'll go into in it which I guess we haven't created yet and we will just put that in there so let's actually make all of this stuff yeah it really doesn't think that how I oh of course I miss belt it well I would that's why gone text I wonder how long you guys were watching that full before you realize how stupid that was you're probably yelling at your screens thing like what are you doing Jonah okay great mantid implementations and then obviously here the anything were really going to do is window handle winter handle open jail context in its gonna do that this is the window handle and then glad we also need glad so one of the things we've done here is we've removed a vigil dependencies right so glad it's not open glad is open jail specific gel Toby isn't but Gladius so that's another thing that we've kind of taken or now removed from window so there's nothing really the ties it specifically token jail anymore because this stuff kind of happens here which is really cool and then we'll also go into windows window and then take that swap buffers code that we had and actually move it into here into swap buffers alright really easy and then window handles obviously what we have over here um and we could do things like assert to make sure that that window handle actually exists so you know window handle handle is not visible what to do some basic checking if we wanted to but I honestly think that this is really dead simple and we don't really need to add anything to make it super complicated okay cool now that was something else I feel like I wanted to address but I don't really remember okay well this is an error what actually is the structure of this since that source we do have platform OpenGL and then yeah not gone text context okay cool let's build everything see if we get any errors or warnings or anything and then we can run it and hopefully it'll work okay so we do get warnings type notepad Santa Claus says he's struck yep that's because when we even get um well yeah I probably misspelled something here context maybe not that's all anyway this should be struct that'll solve that warning um and then what's this it's not found I can't find something to do with that I was build again okay we're fine maybe it was to do with the struct thing um let's just run see what we get there you go all right cool so we have this going on here you know we could do something like inside swap offers if we really wanted to test it out we could may you try to notice to work in our guarantees that we could maybe try some immediate murder to jail which you know I don't even know if it'll let us do that um let's just do that early no compile wow it really doesn't compile right now I don't know what we can do here the stuff doesn't that's really quite interesting isn't it this subject exists what's in here GL dot H which I thought I included but apparently I didn't I was gonna try and do some stuff the other thing we could potentially do I don't know if this will work though is we could try and create a vertex buffer with some data and then see if you can render it automatically but yeah kind of wished immediate mode we'll just we'll just work for us but apparently it just doesn't are so interesting that it just does not whatever knew it included that's probably a good thing I mean it probably wouldn't work anyway because we're using the core profile but anyway my point is that this should actually now be payable rendering just like it was before we don't really have any test with I mean you can see that the clay color is pink that's compromising and if you're wondering the clear color will be controlled by the actual renderer class not by the context or anything like that and in fact to be a little bit more specific this whole GL clear thing which right now actually is happening inside application this whole thing will technically be happening on a frame buffer so we'll have like a rendering buffer essentially a frame buffer that will just be our screen right and then that will contain things like clear commands and that's gonna be the architecture of that anyway that is pretty much the context class really simple again for OpenGL but now the big thing that we've achieved here is we've we're creating a rendering context and it's completely you know independent here I mean we have our pijl context we should be able to just say hey I wanted a direct tax context just like that and guess what all it does is it takes in the window handle all it does is in it and swap offers right that Direct X also needs both of those things we can just have a different implementation and suddenly we have Direct X and then obviously maybe in window props in the future when we have multiple api's we would have something along the lines of like m data you know api or something Propst OPI and then based on that we could maybe do like a switch or something and decide or maybe even ahead of time that instead of using an OpenGL context we would use a DirectX one or anything else hope you guys enjoyed that video if you didn't hit the like button you can also help support the stairs by going over to patreon a conversation the chair a huge thank you as always to all the wonderful people that make this series possible next time we're gonna move on and start creating the renderer class right that's going to be something that's gonna be probably designed and redesigned a billion times but essentially we need that for things like the clear color we need that for issuing rendering commands right we need that for state we need that to so many things and we're probably gonna we might even have a bit of a design episode next time I'm not really sure at this point I still have a few things in my life that I need to sort out at this point in time because making some changes but yeah essentially next time I think we're gonna start off with around or unless I kind of realize that something else is important that at this point in time I'm kind of I forgotten about but anyway I hope you guys enjoyed this video I hope you guys are as happy as I am to be back into hazel way into this series I will see you next time goodbye [Music]
Info
Channel: The Cherno
Views: 29,986
Rating: undefined out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, game engine, how to make a game engine, game engine series
Id: YZKEjaCnsjU
Channel Id: undefined
Length: 27min 3sec (1623 seconds)
Published: Sun May 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.