{"id":3185,"date":"2009-04-28T13:02:27","date_gmt":"2009-04-28T17:02:27","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=3185"},"modified":"2010-10-24T08:05:57","modified_gmt":"2010-10-24T13:05:57","slug":"procedural-city-part-9-speeding-things-up-2","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=3185","title":{"rendered":"Procedural City, Part 9: Speeding Things Up"},"content":{"rendered":"<p>Time to optimize the program.  If you&#8217;ll recall, it&#8217;s running at (maybe) 30fps.  I want it running at 100fps or better.  The big thing that needs to be done is to take a lot of the weight off of the CPU. It&#8217;s thrashing through 4,000+ buildings every time it draws the scene, gathering up polygons and sending them to the renderer. Let&#8217;s see if we can&#8217;t cull those numbers.  <\/p>\n<p>The most obvious way to take some of the load off of the CPU is to not attempt to draw stuff that&#8217;s out-of-view.  Right now when I&#8217;m rendering, I go through the list of 3,000 buildings and cram all of the vertex and polygon data into OpenGL.  It then takes each and every vertex and runs it through a bunch of mathematical mumbo-jumbo to figure out the answer to the question: <em>Where &#8211; given the current location and orientation of the camera &#8211; will this particular building end up on-screen?  Nowhere? Oh? This building is behind the camera? Oh well. Chuck it.  Let&#8217;s see the next one.<\/em>  <\/p>\n<p>This is a very expensive form of math, and it&#8217;s all being done by the CPU.  I <em>could<\/em> use vertex shaders to offload most of this work onto the GPU.  (A few years ago you used to hear about stuff supporting &#8220;Hardware Transform &#038; Lighting&#8221;. This is what that term is all about.) I might resort to that if I become desperate, but adding shaders is a <em>lot<\/em> of new code and complexity.  The shaders themselves are pretty simple, and T&#038;L shaders are usually given away as ten-line example programs.  But to get <em>access<\/em> to shaders requires a lot of boilerplate code and doing a little meet &#038; greet with the system hardware to make sure it&#8217;s down with Transform &#038; Lighting.  I&#8217;d really like to avoid doing that if I can get the job done with the tools I&#8217;m already using. <\/p>\n<p>With a 90 degree viewing angle, the camera isn&#8217;t going to see more than 1\/4 of the stuff around it.  But the program is doing matrix math on <em>everything<\/em> before it gets to the point where OpenGL realizes I&#8217;ve been wasting its time.<\/p>\n<p>There are a lot of techniques you can use to figure out what&#8217;s in view, but I want something simple to code and easy to manage.  I have a lot of flexibility in my design and not much in my timetable, so it&#8217;s better to go for a rough system that solves 90% of the problem than for a robust and complex one that solves 98% of it. <\/p>\n<p>Observation: The city data is more or less 2-dimensional.  I can simply look at the angle to the object from the camera position on the horizontal plane.  If it&#8217;s more than 45 degrees to the left or right, then it&#8217;s out of the 90 degree cone of stuff we can see, and it can be discarded before we go bothering OpenGL with it.  But, in order to get an angle&#8230;<\/p>\n<p>Let&#8217;s look at the code I use to get an angle from A to B on a 2d plane. Let&#8217;s see&#8230; a bunch of floating-point math. Some <em>more<\/em> floating point math.  And an arctangent operation. Crimey. Finally there are some annoying comparisons and faffing about to make sure the angle stays between 0 and 360.  This is not something you want to do to 4,000 objects in the scene.  (Actually, that&#8217;s how many <em>buildings<\/em> there are.  The total count of objects eligible for a ride through the OpenGL funhouse is <a href=\"http:\/\/www.youtube.com\/watch?v=TBtpyeLxVkI\">over 9,000<\/a> once you take into account  street lights and cars and some other bits.) <\/p>\n<p>Doing an angle check to an object would possibly be faster than cramming it through OpenGL, although that might not be true in all cases.  It might be faster to simply draw a one-polygon car than to do a check on all and <em>then<\/em> draw the genuinely visible ones. But even in a best-case scenario, it would still be giving the CPU a brutal workout while the GPU naps on the couch.  To alleviate this, I divide the world into regions:<\/p>\n<p><table width='256'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/pixelcity_vis_grid1.jpg' class='insetimage' width='256' alt='Overhead view. The world is divided up on this grid.  The red dot is the camera, and the blue is the cone that will be visible to the viewer.' title='Overhead view. The world is divided up on this grid.  The red dot is the camera, and the blue is the cone that will be visible to the viewer.'\/><\/td><\/tr><tr><td class='insetcaption'>Overhead view. The world is divided up on this grid.  The red dot is the camera, and the blue is the cone that will be visible to the viewer.<\/td><\/tr><\/table><\/p>\n<p>Now instead of doing an angle check on all of those thousands of objects, I&#8217;ll just do the check on my 256 regions to see which ones are in view.  At render time, I go over the visible regions and render their contents, ignoring everything else.<\/p>\n<p><table width='256'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/pixelcity_vis_grid2.jpg' class='insetimage' width='256' alt='Objects inside of the green regions will be drawn.  Everything else will be ignored.' title='Objects inside of the green regions will be drawn.  Everything else will be ignored.'\/><\/td><\/tr><tr><td class='insetcaption'>Objects inside of the green regions will be drawn.  Everything else will be ignored.<\/td><\/tr><\/table><\/p>\n<p>This is not without its drawbacks. <\/p>\n<blockquote><p>1) Buildings sometimes straddle the line between regions, meaning the center of the building might be sitting in a red (hidden) region but a bit of it is poking into a green (visible) region. It would mean that you&#8217;d see an empty lot on the edge of the screen, but as you turned to look a building would pop in out of nowhere.  This is very noticeable and ugly. Identifying and dealing with these cases can get very tricky. <\/p>\n<p><strong>Solution:<\/strong>  I widened the assumed viewing angle to 100 degrees. This means I&#8217;ll be rendering (or attempting to render) a lot of extraneous crap just to catch a few difficult objects.  If I&#8217;m still having speed problems later, this is one thing I can revisit and try to improve.<\/p>\n<p>2) Doing these calculations on a 2D plane is fast, but it assumes you&#8217;re looking straight ahead, level.  Tilting the view downward will bring nearby red regions into view.  As I look down and pan left and right I can see buildings popping in and out of view.  That wouldn&#8217;t be bad, except that looking down on the city is pretty much the entire point of the project. <\/p>\n<p><strong>Solution:<\/strong> I mark all adjacent regions visible.  No big deal.  It&#8217;s only a few dozen objects.  Doing the math to truly sort this stuff out wouldn&#8217;t be worth it.  I doubt I could even measure the performance improvement from a dozen stragglers.  <\/p>\n<p>3) I&#8217;m drawing a lot of extra buildings.  As soon as the corner of a region enters view, <em>everything<\/em> in that region gets drawn.  I can make the regions smaller, and thus more accurate.  This would cull out a lot of stuff that&#8217;s still way out of view, at the expense of giving me more angle checks to perform.  Right now the world is divided into a grid of 16&#215;16 regions, for a total of 256.  If I made the world (say) 32&#215;32 regions, I&#8217;d have 1,024 checks to do.  I&#8217;d have twice the accuracy at the expense of doing four times as many calculations.  I could also move to a 8&#215;8 grid and do one-fourth of the calculations, at the expense of having even more overdraw.  Hm.<\/p>\n<p>This is a fiendish one, because the sweet spot &#8211; the optimal spot &#8211; is somewhere in the middle.  A 2&#215;2 grid would be worthless and cull no objects. A 128&#215;128 grid would require 16,384 angle checks, which is actually greater than the number of objects in the scene. That&#8217;s worse than doing the angle check on each and every building individually.  I want the region size to be big enough that every region will contain more than one building, but not so big that I&#8217;ll be attempting to render a lot of off-to-the-side stuff.  (Note also that this interacts with the texture-sorting I talked about last time.  If I&#8217;m rendering by region, I can no longer render all of the same-textured objects together.  Smaller regions mean more texture switching.  <em>Sorting<\/em> them before each rendering pass would fix this by having the CPU spend a lot more effort in order to save the GPU a little effort, which is the very opposite of what I want to do.) <\/p>\n<p><strong>Solution:<\/strong> Lots of tests later, and it is apparent that my initial choice of a 16&#215;16 grid was <em>already<\/em> the optimal one.  There are reasons why the optimal setting might be different on a different machine.   I must not think about this or it will drive me crazy.\n<\/p><\/blockquote>\n<p><table width='512'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/pixelcity_misc3.jpg' class='insetimage' width='512' alt='pixelcity_misc3.jpg' title='pixelcity_misc3.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>So far, I&#8217;ve worked the framerate up to 130-ish. I&#8217;ve technically met my goal, although framerate is still under 100 when the  bloom effect is turned on.<\/p>\n<p><table width='512'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/pixelcity_misc2.jpg' class='insetimage' width='512' alt='pixelcity_misc2.jpg' title='pixelcity_misc2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>I&#8217;m sure I can do better than that.  I still have another round of optimizations to make.  Ideally, I&#8217;d like to get it working so that it&#8217;s running at over 100fps even when bloom is on.  We&#8217;ll see how the next round of changes go. <\/p>\n<p>I&#8217;m sure some people will notice that  I re-worked the sky to be a single gradient, added some simple radio towers on top of buildings, and added some other bits of sparkle power. None of it is set in stone. These were just experimental visual changes I made over the weekend and not related to any of this optimization work. I&#8217;ll have time to fuss with that stuff once I have the rendering working smoothly. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time to optimize the program. If you&#8217;ll recall, it&#8217;s running at (maybe) 30fps. I want it running at 100fps or better. The big thing that needs to be done is to take a lot of the weight off of the CPU. It&#8217;s thrashing through 4,000+ buildings every time it draws the scene, gathering up polygons [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66,10],"tags":[],"class_list":["post-3185","post","type-post","status-publish","format-standard","hentry","category-programming","category-projects"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/3185","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=3185"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/3185\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}