{"id":21274,"date":"2013-10-06T23:40:00","date_gmt":"2013-10-07T04:40:00","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=21274"},"modified":"2015-07-01T04:47:35","modified_gmt":"2015-07-01T09:47:35","slug":"project-good-robot-21-resource-usage","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=21274","title":{"rendered":"Project Good Robot 21: Resource Usage"},"content":{"rendered":"<p>You know what the hardest feature of this game was so far? Fullscreen mode. Well, not so much fullscreen as the ability to allow the user to change the window size, which crops up most often when toggling fullscreen mode. I don&#8217;t think it was the most time consuming feature, but it was certainly the most effort for the most mundane and uninteresting payoff. <\/p>\n<p>It&#8217;s not something you can skip, really. I&#8217;m long past the point where I&#8217;ll put up with a game that wants to restart because you changed the resolution. We&#8217;re all accustomed to being able to smack alt-enter to toggle fullscreen or to resize a window by dragging. It&#8217;s just part of making civilized software and not something a developer can leave out. On the other hand, it&#8217;s infuriatingly difficult and troublesome and adds all kinds of unwelcome complexity to systems that would otherwise be graceful and elementally simple. <\/p>\n<p>Ever wonder why some old games (and a few modern ones) won&#8217;t change the resolution until you restart the program? I&#8217;ll tell you.<\/p>\n<p><!--more-->The problem has to do with how video memory is allocated. Let&#8217;s imagine this blank canvas is your video memory: <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr21_mem1.jpg' class='insetimage'   alt='gr21_mem1.jpg' title='gr21_mem1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>When the game starts up the silly user has the resolution set to a hilarious 640&#215;480. So we need to allocate enough memory to hold that image. Actually, we need two of them. These blocks of memory are called the front and back buffers.  The idea is that the game shows you one image (the front) while it&#8217;s busy drawing the other (the back) and then swaps the two.  It&#8217;s always showing you one and drawing on the other. This swap is the end of a frame, and doing this switch is what gives us the &#8220;framerate&#8221; thing that the kids are always talking about. <\/p>\n<p>So when we start up OpenGL or DirectX we grab some memory for our front and back buffers:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr21_mem2.jpg' class='insetimage'   alt='gr21_mem2.jpg' title='gr21_mem2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Now, we might also need a few more buffers: Depth buffer, stencil buffer, accumulation buffer, or whatever those fancy folks working on the Cry Engine use to make their pixels so shiny. But you get the idea that we need to take a chunk of video memory to hold the stuff we&#8217;re drawing.<\/p>\n<p>We&#8217;ll also need to grab some more memory to hold our texture data. We might have a small number of huge textures, a modest number of medium textures, or a multitude of small textures. It kind of depends on the game. Either way, in most cases textures will take up more video memory than anything else.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr21_mem3.jpg' class='insetimage'   alt='gr21_mem3.jpg' title='gr21_mem3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>To the other coders: Yes, I know memory is linear and not arranged on a grid like this. But that&#8217;s not important for the conversation we&#8217;re having and this is much easier to visualize.<\/p>\n<p>We might also have some vertex buffers. That&#8217;s where we take a bunch of polygons and send them to live in video memory so we can refer to them quickly. Instead of sending all 10,000 polygons of Master Chief&#8217;s head every frame we can just say to the graphics card, &#8220;Hey man. Remember that head I gave you a while back? Draw that.&#8221; <\/p>\n<p>So let&#8217;s put some vertex buffers into video memory:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr21_mem4.jpg' class='insetimage'   alt='gr21_mem4.jpg' title='gr21_mem4.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>And finally if we&#8217;re doing bump mapping or somesuch then we need vertex and fragment shaders. These things are just bits of code, so they&#8217;re small. But they do exist in video memory. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr21_mem5.jpg' class='insetimage'   alt='gr21_mem5.jpg' title='gr21_mem5.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>So fine. We&#8217;re drawing this 3D scene. We&#8217;re rolling along, rendering stuff and giving the user aliens, Nazis, Zombies, or robots to shoot. Everyone&#8217;s happy. But then the user decides they don&#8217;t like 640&#215;480 mode and they switch to something like 2048&#215;1152. Suddenly&#8230;<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr21_mem6.jpg' class='insetimage'   alt='gr21_mem6.jpg' title='gr21_mem6.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Suddenly many times more memory is needed for those front and back buffers. (And stencil, depth, etc.) This space is gained by purging everything from video memory and starting over. This always happens, even if there&#8217;s tons of memory left.<\/p>\n<p>I&#8217;m sure there&#8217;s a good reason for this. That&#8217;s not sarcasm. I don&#8217;t know enough about the low-level videosystems to know why this purge happens, but a lot of people have been obsessing over these software layers for over a decade, and there&#8217;s no way this is the result of &#8220;laziness&#8221;. I&#8217;m sure the answer would be very technical and would come down to performance.<\/p>\n<p>It doesn&#8217;t matter. The point is, at any given frame we could suddenly discover that every single thing we&#8217;ve ever sent to video memory has been wiped out. Keep in mind that establishing these resources is a big percentage of the time you spend at the loading screen. If you&#8217;ve ever wondered why videogames tend to enter this massive funk just after you change the resolution, this is why.  The program has to stop and re-load every dang thing. (The textures take far longer than everything else combined. My images above are not at all to scale.  If they were, then the canvas would be about sixty times larger than the front\/back buffers, the textures would be at least twice the size of the LARGE buffers in the last image, and the shader programs would be too small to see.)<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr21_screen1.jpg' class='insetimage'   alt='gr21_screen1.jpg' title='gr21_screen1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>It&#8217;s really annoying to have to design a system to handle mid-game re-initialization like this, but it&#8217;s what you have to do.  Making the user restart is obnoxious and barbaric, but if we try to do ANYTHING without restoring the lost data we will end up drawing a heap of garbled mess. And then crash. (Trying to render from a missing vertex buffer is suicide.)<\/p>\n<p>There&#8217;s other stuff that&#8217;s not kept in video memory that also ends up getting killed by this reset, like OpenGL display lists. In my case, this means it kills all of the fonts and the HUD.<\/p>\n<p>The user re-sizes the screen and suddenly you have to re-init textures, vertex buffers, lists, shader programs, and fonts.  You have to do these in the right order, since some systems contain others. What a mess. <\/p>\n<p>On the upside, this acts as a great stress test. If your program can survive several successive re-sizes without crashing, leaking a bunch of memory, or displaying a bunch of ugly artifacts, then it (probably) means you&#8217;ve got a solid foundation on your program. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr21_screen2.jpg' class='insetimage'   alt='gr21_screen2.jpg' title='gr21_screen2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Performance-wise, I&#8217;m pretty happy with the program. <\/p>\n<h3>Some Trivia for the Curious<\/h3>\n<p>The project has ~40k lines in 84 source files. 2,500 of those lines are comments. The largest source file is 15k lines of code, but that&#8217;s a silly OpenGL thing called OpenGL extentions wrangler. It&#8217;s mostly a bunch of conditional statements to figure out what OpenGL features are available on the user&#8217;s system and how to get to them. The largest part that I&#8217;ve written personally is one tenth the size of that; the Robot AI is just 1,550 lines of code. <\/p>\n<p>I&#8217;ve never heard of Cyclomatic complexity before, but according to the source-examining tool I just downloaded my <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cyclomatic_complexity\">McCabe VG Complexity score<\/a> is 4,175.  I&#8217;m sure somewhere out there is a computer scientist who knows what that means and will find it interesting.<\/p>\n<p>We&#8217;re currently on week ten of the project. <\/p>\n<p>It&#8217;s a total of two seconds from the moment I start the program to the moment when I can start playing. I hate loading screens and I think I&#8217;m going to be able to meet my goal of making a game that doesn&#8217;t have one. So far I can fly from the starting area to the end of the game in a single unbroken journey. There&#8217;s a tiny little stutter when you move down to the next level, but I&#8217;m sure I can get rid of that. <\/p>\n<p>Memory-wise, I&#8217;m doing okay:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr21_performance.jpg' class='insetimage'   alt='gr21_performance.jpg' title='gr21_performance.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>My in-game memory usage is a good bit less than the memory used by <a href=\"http:\/\/store.steampowered.com\/app\/236450\/\">Pac-Man<\/a> at its main menu. I could get that even lower, but this is already ridiculously low. You can see it&#8217;s already using less memory than a couple of browser tabs in Chrome. (I think one of those is Facebook.) I&#8217;m trying to make efficient software, not mash my program down to some <a href=\"http:\/\/en.wikipedia.org\/wiki\/Demoscene\">teeny tiny size<\/a> for bragging rights.  <\/p>\n<p>How much more am I going to use in the way of resources? I think I&#8217;m basically done consuming texture memory.  I won&#8217;t need more unless I fill my atlas texture, and I&#8217;m using less than a third of it:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/gr21_atlas.jpg' class='insetimage'   alt='gr21_atlas.jpg' title='gr21_atlas.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I suspect I&#8217;ll eat some more memory with new sound effects. Other than that? I guess this is about what I can expect from the final product, minus optimizations.<\/p>\n<p>I&#8217;m still not to the point where I&#8217;m ready to start optimizing and testing to see what the final system requirements need to be. But at this point I&#8217;m feeling confident that I&#8217;m not making any egregious errors or doing anything obviously stupid.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You know what the hardest feature of this game was so far? Fullscreen mode. Well, not so much fullscreen as the ability to allow the user to change the window size, which crops up most often when toggling fullscreen mode. I don&#8217;t think it was the most time consuming feature, but it was certainly the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[498],"tags":[],"class_list":["post-21274","post","type-post","status-publish","format-standard","hentry","category-good-robot"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/21274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21274"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/21274\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}