{"id":3139,"date":"2009-04-27T08:00:00","date_gmt":"2009-04-27T12:00:00","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=3139"},"modified":"2010-10-24T08:05:57","modified_gmt":"2010-10-24T13:05:57","slug":"procedural-city-part-8-optimization-tests","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=3139","title":{"rendered":"Procedural City, Part 8: Optimization Tests"},"content":{"rendered":"<p>The project is now officially over budget.  I expended vast quantities of time obsessing over framerates and benchmarking this weekend.  My budget was about 30 hours, and while I haven&#8217;t been keeping a rigorous tally of hours, I&#8217;m well past the deadline and not yet done.  But the end is in sight.  I&#8217;ve determined to get this thing done this week. All told, it looks like I&#8217;ll have sunk 40 hours into it.  For perspective, if this was a game with an eighteen month budget, I would just have missed going gold and admitted that we were going to need another six months.  And that our entire staff spent six months of the budget playing Left 4 Dead. Good thing I don&#8217;t have investors.<\/p>\n<p>I&#8217;m afraid this stretch of the project is likely to be a bit dry. It can&#8217;t all be colored pixels and bloom lighting.  Sometimes I have to go and fuss over dull numbers and time things, which makes for unspectacular screenshots. I&#8217;ll do my best to make this interesting. <\/p>\n<p>By now the program is running like a slow pig, and has been for a while.  30FPS for a little city like this is appallingly slow, and before I move on to grander things I need to know how it&#8217;s going to run.<\/p>\n<p>The first step in speeding up a program like this is finding out where the bottlenecks are.  There are several aspects of rendering that I look into when facing slowdowns:  <\/p>\n<ol>\n<li>CPU bottleneck:  The program just isn&#8217;t running fast enough and so it isn&#8217;t sending polygons to the GPU fast enough.\n<\/li>\n<li>Throughput bottleneck: The software is capable of sending the polygons faster, and the GPU is capable of rendering them faster, but because of drivers or hardware limitations you just can&#8217;t send the data fast enough.  You&#8217;ll run into problems like this if you&#8217;re trying to render a whole bunch  of lightweight, easy-to-draw polygons. (With &#8220;bunch&#8221; in this case meaning &#8220;hundreds of thousands&#8221; or even &#8220;millions&#8221;.) If you&#8217;re drawing a bunch of tiny little triangles with no special lighting effects or texturing, (what the heck are you drawing, anyway?) you can run into this bottleneck.  I don&#8217;t encounter this very often due to the fact that I&#8217;m usually working on the lower end of the tech curve.  (Although I&#8217;m pretty sure I ran into it during the terrain project.  I can&#8217;t remember now.) In any case, I understand this bottleneck is one of the reasons for the move from PCI to AGP, and from AGP to PCIe slot interfaces.  The newer bus interface lets you pump more data to the GPU.    I have&#8230; wait, let me write some code to do a count&#8230;.  Okay:  40,804 polygons.  That&#8217;s not counting a few special case groups like the cars, ground or sky, but that number is probably within 1,000 polygons of the total.  So I don&#8217;t think I have to worry about throughput on anything built in this century.\n<\/li>\n<li>Fill-rate bottleneck: This has always been where I do most of my work.  Problems like this usually come from filling the screen with expensive polygons.  In contrast to the earlier case where I might have been drawing millions of cheap polygons, perhaps I&#8217;m slowing things down by drawing just a few expensive ones.  Fifty polygons sounds pretty pathetic compared to a million, but if all fifty polygons cover the <em>entire<\/em> screen and do complex blending, then you&#8217;ll end up with a fill rate problem.  Your program is sending those 50 polygons nice and fast, and there are so few they won&#8217;t clog up the hardware pipeline, but the graphics card is going to take a long time to draw them.  Full-screen effects (like bloom lighting and depth-of-field) can cause fill rate problems.\n<\/li>\n<\/ol>\n<p>An analogy I like: The CPU is a waitress taking orders.  The throughput is the rate at which she can put order strips up to be read by the cook, who is the GPU in this case.  Fill rate problems mean he&#8217;s not cooking stuff fast enough. (Yes, I know they&#8217;re called servers today, and not &#8220;waitresses&#8221;. But the restaurant used in this analogy is a roadside greasy spoon diner in 1978. She doesn&#8217;t care if you call her a waitress, as long as you tip well and the kids don&#8217;t make too much noise.)<\/p>\n<p>The third type of slowdown is easy to spot. If making the window smaller speeds things up, then it&#8217;s a fill rate problem.  If not, then it&#8217;s probably a CPU problem.  I&#8217;m sure I&#8217;m not having a fill-rate problem, but I check anyway because you don&#8217;t begin research by assuming you know everything.  <\/p>\n<p>I shrink the window.  No change in the framerate.  I shrink it to almost nothing.  Still no improvement.  Just for fun, I turn off writing to the z-buffer and change the program to draw the buildings as translucent.  This will make all of those polygons many, many more times expensive and will ensure that the GPU has to draw every. single. one.  Then I make the program run full-screen.<\/p>\n<p>Take that, fancy-pants hardware! Let&#8217;s see how you like choking on 40,000 two-sided alpha-blended, screen-filling polygons! <\/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_misc1.jpg' class='insetimage' width='512' alt='Hm.  That actually looks kind of cool.' title='Hm.  That actually looks kind of cool.'\/><\/td><\/tr><tr><td class='insetcaption'>Hm.  That actually looks kind of cool.<\/td><\/tr><\/table><\/p>\n<p>No change.<\/p>\n<p>Wow. <\/p>\n<p>Not only is my graphics hardware not the bottleneck (which I already suspected) but it&#8217;s not even being reasonably challenged.  Going back to the waitress analogy, here we have a cook that can <em>prepare meals faster than the waitress can write them down<\/em>.  She writes down a four-order meal with appetizers and desserts, and the food is done before she can get back out on the floor to take another order.  <\/p>\n<p>As someone pointed out earlier in the series, these new cards are designed for rendering with complex pixel shaders that do fancy bump mapping, specular mapping, texture layering, dynamic light passes, lighting objects at grazing angles, and a whole bunch of other intense math.  On every single pixel it draws.  Here I&#8217;m simply asking it to take one lone texture map and apply it to the polygon, and I doubt I could hope to keep the thing busy with such a lightweight job.  <\/p>\n<p>Actually, tests reveal there is one thing it&#8217;s <em>slightly<\/em> sensitive to, which is changing textures.  Back in <a href=\"?p=2940\">step one<\/a> I made texture maps for the buildings.  Think of rendering as painting.  If I ask you to paint a red stroke on the canvas, then a blue one, then a red one, it will take longer than it would to do both red strokes and then the blue.  It takes a moment to lower your brush, clean it off, load it up with paint again, and bring it back up to the canvas.  I can get a small performance boost by making sure I render all of the buildings that share a common texture at the same time.  With eight textures and (roughly) 3,000 buildings, rendering them in a random order will cause the graphics card to have to change paint over 2,500 times.  If I sort the buildings, it will only have to do so 8 times.  This gives me a modest performance boost of around 10fps.  That&#8217;s nice, but it&#8217;s trivial compared to the real optimizations I&#8217;ll need to do.  I should have gone after inefficiencies like this one later on, and go after the low-hanging fruit first. But I did this one more or less by accident as part of my tests.  <\/p>\n<p>(Note that I&#8217;m writing this after the fact, and I didn&#8217;t keep a perfect record of how much of a performance boost I got from each step.  The numbers I give for framerates are vague recollections and guesses.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The project is now officially over budget. I expended vast quantities of time obsessing over framerates and benchmarking this weekend. My budget was about 30 hours, and while I haven&#8217;t been keeping a rigorous tally of hours, I&#8217;m well past the deadline and not yet done. But the end is in sight. I&#8217;ve determined to [&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-3139","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\/3139","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=3139"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/3139\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}