{"id":23304,"date":"2014-06-12T11:17:30","date_gmt":"2014-06-12T16:17:30","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=23304"},"modified":"2014-06-12T13:14:10","modified_gmt":"2014-06-12T18:14:10","slug":"frontier-rebooted-part-6-worst-case-scenario","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=23304","title":{"rendered":"Frontier Rebooted Part 6: Worst-case Scenario"},"content":{"rendered":"<p>When it comes to rendering, speed is everything. Well, speed and looks. I mean, you need looks. No sense in drawing things if they look terrible. So the two most important things are speed and looks. And latency. Obviously latency is important. You can&#8217;t bloody well play a game if it takes several seconds for your input to make something happen on screen because the engine is building up these massive framebuffer effects. So the <em>three<\/em> most important things are speed, looks, and latency. And compatibility. What&#8217;s the sense in writing an engine that&#8217;s only fast and pretty and on one set of hardware? That&#8217;s buying into the pointless wanking and pissing matches between hardware manufacturers, right there. So our top priorities are speed, looks, latency, and compatibility. And consistency. <\/p>\n<p>Let me start over.<\/p>\n<p><!--more-->In your typical game, you&#8217;re generally pushing to render stuff as fast as you can. That&#8217;s still the case, obviously. But if you&#8217;re developing for VR<span class='snote' title='1'>Which we aren&#8217;t, but for educational purposes we&#8217;re pretending we are.<\/span> then you have something new to worry about: Stuttering<span class='snote' title='2'>Also known as hitching, stalling, frame-skipping, frame-dropping, and &#8220;crap&#8221;.<\/span>. In the old days, having the game hiccup and pause for a tenth of a second was nothing more than a mild annoyance.  Maybe the player rounds a corner and suddenly gets line-of-sight to (say) the Doomsday Cannon. But maybe the model or texture isn&#8217;t in memory yet, or they haven&#8217;t been properly packed up and made available to the GPU. So the rendering stalls for a split second while it gets the assets ready to be drawn. <\/p>\n<p>Now that the cannon is in memory, it&#8217;s not a problem any more. The player can play peek-a-boo with it all day and they won&#8217;t bump into any more stuttering<span class='snote' title='3'>Unless we had to dump something ELSE out of memory to make room for the cannon. In that case, the player might have another stutter when they go look at that OTHER thing.<\/span>. A bit of stuttering is a harmless nuisance. We work to prevent it, but no engine is perfect. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/freboot24.jpg' class='insetimage'   alt='freboot24.jpg' title='freboot24.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>But in the world of VR, stuttering is no longer a nuisance.  It&#8217;s a dangerous failure state. When the screens are strapped to your eyeballs and you&#8217;re moving you&#8217;re head around, having rendering skip some frames &#8211; even just one or two &#8211; can be jarring, disorienting, and even nauseating. Anything that causes physical discomfort to the user needs to be at the top of the priority list. Screw special effects. Screw graphics. We can&#8217;t afford to miss a frame. If that means the Doomsday Cannon spends half a second untextured, or unlit, or even completely missing, then so be it. As long as we keep drawing the scene.<\/p>\n<p>This presents a scary problem. What if we just can&#8217;t keep up? What if we&#8217;re stuttering not because we&#8217;re waiting to cache some assets, but because the hardware literally can&#8217;t draw eveything in the given time? What if there is suddenly just too much stuff to draw? If someone is running the game on hardware that just barely meets the requirements, you might run into a scenario where some arrangement of objects pushes us over the fatal limit that will overburden the GPU, stutter, and send the user to puke city. <\/p>\n<p>This becomes important when you&#8217;re dealing with huge numbers of cheap objects, like grass. In that case our worst-case scenario is one where the player is standing in the middle of a field of grass. For our hypothetical game, let&#8217;s say that this scenario is unlikely but not impossible. Which means that the engine needs to be able to handle this state gracefully. <\/p>\n<p>You could make the case that it makes more sense to simply render this worst-case scenario every frame rather than allow it to surprise us. It needs to be able to handle maximum grass, so why waste precious CPU time trying to minimize it? If we need to be able to handle the worst-case anyway, then perhaps it&#8217;s safer to just make sure we&#8217;re <em>always<\/em> drawing the worst case? Once we get the game running at the desired speed, we can be confident that no matter what happens or what we need to draw, it shouldn&#8217;t harm our framerate or our poor user.<\/p>\n<p>Of course, the counter-argument here is that we might want to cull grass because we&#8217;ve got mutually exclusive stuff in our world. Maybe we have trees. We need to be able to handle maximum trees. We need to be able to handle maximum grass. But there will never be a situation where you have the maximum of both at the same time. This spot of ground is either trees or grass, but not both. In which case it makes sense to do some kind of culling. Fine. But say you&#8217;ve got a few different systems playing off each other: How can you be sure there&#8217;s not some magic ratio of the three that will lead to frame skipping? You can&#8217;t test everywhere in all configurations. <\/p>\n<p>I honestly don&#8217;t know. It might be better to simply apply the maximum load at all times to avoid any surprises, even if that means raising your minimum system requirements. In that case once the user has it running smoothly, they can feel safe that it will keep running smoothly and not oblige them to go mucking about with graphics settings once they have the headset on. <\/p>\n<p>People are still working on this stuff and figuring out what the best practices are. It&#8217;s one of the reasons I&#8217;m so into VR right now. There&#8217;s room to learn new stuff rather than just read a white paper from someone who solved the problem fifteen years ago.<\/p>\n<p>Are we a thousand words into this and still not making polygons? Apologies. I don&#8217;t think we&#8217;re going to make any polygons today. I think I need to laboriously talk around the problem for fifteen hundred words before I do something radical like <em>write actual code<\/em>.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/freboot_som.jpg' class='insetimage'   alt='freboot_som.jpg' title='freboot_som.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Grass is interesting because it&#8217;s small. It usually doesn&#8217;t occupy much screen space, but if you can crouch (or it can be very tall) then there are situations where it might fill ALL Screen space. It&#8217;s usually made up of tens of thousands a really cheap triangles. However, if your game engine is aiming for photorealism then you might want to blend the edges of the grass blades.  This looks nice, but can get expensive fast. It&#8217;s also strange because it&#8217;s almost never the center of attention, yet it has a massive impact on the scene<span class='snote' title='4'>Try turning off grass in Oblivion. The game will suddenly look like Morrowind.<\/span>.<\/p>\n<p>The problem with grass is that we need it everywhere, but we can&#8217;t actually draw it everywhere. We need to draw grass right here at the player&#8217;s feet, and we need to <em>not<\/em> draw grass on those far hills. At this distance we can just paint the hills green and it&#8217;s all good.  The trick is how we transition between the two. Having grass suddenly pop in is jarring and distracting. Same goes for having it abruptly vanish again. <\/p>\n<p>The standard trick is to have the grass fade in. At (say) twenty meters it&#8217;s just barely visible, but by the time you&#8217;re five meters away it&#8217;s fully opaque. The problem with that idea is that it drags us into the thorny world of alpha blending. (That&#8217;s fancy programmer talk for drawing semi-transparent surfaces.) For starters, drawing alpha-blended stuff is slower than drawing non-blended stuff. Second, you have to sort all the semi-transparent stuff you&#8217;re going to draw, so that you start with the most distant and draw the nearby stuff last. Otherwise:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/freboot23.jpg' class='insetimage'   alt='freboot23.jpg' title='freboot23.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Those blades of grass on the right show us what happens if you don&#8217;t draw back-to-front. The edges of the blade nicely blended with the background just fine. But then when we&#8217;re drawing the more distant grass it gets blocked from view by the wispy edges of the close grass. There&#8217;s no way to properly &#8220;insert&#8221; the far grass in between the near and far stuff. That&#8217;s like painting and needing to put some paint down between the canvas and some paint you&#8217;ve already applied. <\/p>\n<p>Finally, drawing back-to-front is more expensive because we get more overdraw<span class='snote' title='5'>Drawing over the same pixel again and again. In the ideal scenario, you would draw every pixel on screen exactly once.<\/span>. If we weren&#8217;t trying to blend the edges of the grass above, then it would properly obscure our view of the grass behind it, and the GPU  wouldn&#8217;t need to waste time putting those pixels down.<\/p>\n<p>So fading grass in looks great, but it sucks. It requires some CPU-sucking sorting, it causes overdraw, and alpha-blended stuff is slower to draw. Also: Aside from speed concerns, sorting is annoying. You&#8217;ve got this awful sorting system right in the middle of your beautiful rendering pipeline. It introduces a lot of code complexity. Oh sure, sorting itself is basically a <a href=\"?p=21115\" title=\"15 Sorting Algorithms in 6 Minutes\">solved problem<\/a>. But you&#8217;ve still got to do it at the right time. You have to do it just often enough to keep the scene from looking glitchy. You need to clutter up your rendering logic with stuff to make objects fade in and out. A couple of lines of code can easily become a couple of pages if you really want to do it right. So what is a cheap, corner-cutting, lo-fi developer to do?<\/p>\n<p>There&#8217;s a trick I want to play around with. We&#8217;ll try it next time. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>When it comes to rendering, speed is everything. Well, speed and looks. I mean, you need looks. No sense in drawing things if they look terrible. So the two most important things are speed and looks. And latency. Obviously latency is important. You can&#8217;t bloody well play a game if it takes several seconds for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-23304","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/23304","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=23304"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/23304\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}