{"id":22004,"date":"2014-01-20T06:21:41","date_gmt":"2014-01-20T11:21:41","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=22004"},"modified":"2014-01-20T06:21:41","modified_gmt":"2014-01-20T11:21:41","slug":"screensaver-plasma","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=22004","title":{"rendered":"Screensaver: Plasma"},"content":{"rendered":"<p>So we&#8217;re doing another one of these things. I know this isn&#8217;t as fun as a Good Robot update, but hopefully this can keep us amused until the project gets moving again. While working on this, I found an interesting wrinkle that gives us a reason to talk about poly count vs. fill rate and why a full-blown 3D game runs many times faster than a trivial screensaver. <\/p>\n<p>But first let&#8217;s talk about what I&#8217;ve made&#8230;<\/p>\n<p><!--more--><table width='256'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/plasma1.jpg' class='insetimage' width='256' alt='plasma1.jpg' title='plasma1.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Let&#8217;s say we generate a simple texture that&#8217;s pure white in the center and fades to black at the edge to make a nice blurry circle.<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/plasma2.jpg' class='insetimage' width='600' alt='plasma2.jpg' title='plasma2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Now let&#8217;s slap that texture on a quad (a pair of triangles, really) defined by points a, b, c, and d. We deform this polygon by making opposing points a and d vary their distance from each other according to a sine wave. Likewise, b and c move towards and away from each other on another sine wave with a longer wavelength. <\/p>\n<p>We want to move this polygon around the screen. <\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/plasma3.jpg' class='insetimage' width='600' alt='Like this, only with an additional layer of complexity.' title='Like this, only with an additional layer of complexity.'\/><\/td><\/tr><tr><td class='insetcaption'>Like this, only with an additional layer of complexity.<\/td><\/tr><\/table><\/p>\n<p>We DO want the movement to be chaotic and unpredictable. We DON&#8217;T want the movement to be sharp or jagged. If it moves randomly, it will just make a zig-zagging scribble all over the place. So instead we create a three-body system. (Not like the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Three_body_problem\">three-body problem<\/a> they have in physics, although I guess there are superficial similarities between the two.) We put our polygon at point P1. P1 orbits point P2, which in turn orbits point P3, which orbits the center of the screen. The effect is a bit like a <a href=\"http:\/\/www.mathplayground.com\/Spiromath.html\">spirograph<\/a> with an extra layer. This produces large motions that will (over time) send our polygon all over the screen, and do so in sweeping motions instead of abrupt ones.<\/p>\n<p>The path it takes looks like this:<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/plasma8.jpg' class='insetimage' width='600' alt='plasma8.jpg' title='plasma8.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>This is a composite (created just for the purpose of this explanation) of what the polygon is doing over about a minute of running time. It&#8217;s bright where the poly is moving slow, and dim where it&#8217;s moving fast. <\/p>\n<p>Actually, we&#8217;re not just moving a single polygon around. We&#8217;re creating a trail of them.  As the polygon moves around, it leaves behind a copy. These copies gradually move towards the camera, which makes them seem to grow larger as they fade out. Every few seconds we pick a new random color for our polygon.<\/p>\n<p>During rendering, each copied polygon is drawn on top of the previous one, slightly brightening the pixels there. There are a total of 256 polygons in this trail. And so we end up with this blob of undulating, wobbling color that looks kind of like a slow-motion flame or vapor.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/plasma7.jpg' class='insetimage'   alt='plasma7.jpg' title='plasma7.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Well, I guess we should say there are 512 triangles, since that&#8217;s what really matters. In any case, the screensaver dawdles a bit. We&#8217;re only drawing 512 triangles. Why is it so slow?<\/p>\n<p>Back in 2004, Doom<sup>3<\/sup> gave us characters that weighed in at <a href=\"http:\/\/www.doom3world.org\/phpbb2\/viewtopic.php?f=3&#038;t=7040&#038;view=previous\">about 1,500<\/a> or so. For example, the imp&#8230;<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/doom3_imp.jpg' class='insetimage'   alt='doom3_imp.jpg' title='doom3_imp.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>&#8230;was the simplest foe in the game at just 1,100 triangles. So that imp has more than twice as many triangles as my screensaver. And the imp is being drawn using monumentally complex (by comparison, anyway) shaders that do lighting, shadowing, specular gloss, texturing, and bump-mapping. Meanwhile, my screensaver is drawing simple unlit no-shader polygons. And Doom3 shows us not only an imp, but several. Plus all the level geometry. Plus the moving machinery in the room. Plus all the other crap like the HUD and the player&#8217;s arms and weapon. And it did all of this on 2004 level hardware at a decent framerate. <\/p>\n<p>Meanwhile, my screensaver chugs on my <a href=\"?p=18607\" title=\"The New Computer\">not too shabby machine<\/a>. Why can a 2004 game draw many more (perhaps an order of magnitude more) triangles with far more demanding processing faster than my 2013 machine can draw this tiny handful of colored circles? <\/p>\n<p>It&#8217;s all about fill rate.<\/p>\n<p>How much of the screen are we filling with polygons? Or more accurately, how many total pixels are we drawing? Let&#8217;s assume we&#8217;re going to run Doom 3 and Plasma at the same resolution. And let&#8217;s just assume, for the sake of having a nice 16:9 ratio, that we&#8217;re running at a desktop resolution of 1600&#215;900. (That&#8217;s probably small by today&#8217;s standards, and large by 2004, standards, and I&#8217;m not sure if that was ever a native resolution of a monitor, but whatever. I&#8217;m making a point here.) That means you&#8217;ve got a total of 1,440,000 pixels. In Doom 3, you will draw every pixel at least once. Some portion of the screen will get drawn more than once. Back to our two imps:<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/doom3_imp2.jpg' class='insetimage'   alt='doom3_imp2.jpg' title='doom3_imp2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>In the circled area, we might end up drawing some of the pixels in the red circle as many as three times. Say we draw the floor first. Fine. We&#8217;ve drawn an empty room. Then we draw the imp in the back, which ends up overwriting a little bit of the drawing we did earlier. Then we draw the imp in the front, and all of the pixels he occupies are drawn. So everywhere we have imps, pixels were drawn twice, except for where the arm of the front imp covers that leg of the rear one. Those pixels were drawn three times. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/plasma6.jpg' class='insetimage'   alt='plasma6.jpg' title='plasma6.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>There are, in a broad sense, two stages to rendering. The first is vertex processing: <em>Considering where this imp is in the scene, will it appear on screen, and if so, where?<\/em> The second is pixel processing: <em>Now that we know what regions of the scene are occupied by the imp, let&#8217;s painstakingly go over it a pixel at a time and draw it.<\/em> For the curious: These two steps are what make up &#8220;vertex shaders&#8221; and &#8220;pixel shaders&#8221; that us programmers are always on about. The second step is way, way more expensive. A triangle only has three vertices, but can contain hundreds or even thousands of pixels, and in a game like Doom 3 a pixel is probably more computationally expensive than a vertex. (To draw a pixel you have to do multiple texture lookups to get the texture, specular, and bump map, and then do a page or so of math to turn that into your final color value.)<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/plasma4.jpg' class='insetimage'   alt='plasma4.jpg' title='plasma4.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>So the polygon count of our scene basically doesn&#8217;t matter. Both of the programs we&#8217;re discussing are drawing a trivial number of polygons as far as my graphics card is concerned. <\/p>\n<p>You&#8217;ll notice that in my program, polygons don&#8217;t occlude each other. It&#8217;s not like I&#8217;ve got solid imps blocking our view of the scenery. It draws one polygon, and then another on top of that, and another on that. Each time it draws a polygon it must draw all the pixels, adding to what&#8217;s already been drawn. It has to. That&#8217;s how this effect is achieved. <\/p>\n<p>Let me do a quick test&#8230;<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/plasma5.jpg' class='insetimage'   alt='plasma5.jpg' title='plasma5.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>That&#8217;s a single test polygon exactly halfway through its lifespan, rendered without the fuzzy texture so we can see its shape. I took a few screenshots like this, and this is a very typical sample. Running at our test resolution, it takes up about 176,319 pixels, or ~12% of the area of the screen. So each of our 256 polygons draws, on average, 12% of the screen pixels. <\/p>\n<p>In the Doom example, we rendered every pixel once and a few unlucky ones got drawn a couple of times. Even in our worst-case scenario, I doubt our overdraw was anywhere near double the total pixel count. On average, every pixel is drawn slightly more than once. <\/p>\n<p>In the case of my screensaver, every screen pixel pixel is drawn an average of&#8230;<\/p>\n<p><code>0.12 x 256 = 30.72<\/code><\/p>\n<p>&#8230;thirty times. <\/p>\n<p>And that&#8217;s what makes it slow.<\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/plasma9.jpg' class='insetimage'   alt='plasma9.jpg' title='plasma9.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>Having said all that, I accomplished what I set out to do, which was smear some pretty colors all over the place. There are a lot of things you could do to speed this up, but this effect is fundamentally mean to our existing graphics technology. It actually runs fine on one screen, but in my dual-monitor setup Windows asks the screensaver to handle a gargantuan 3600&#215;1334 window that covers both monitors, which (according to our back-of-the-napkin work above) gives my graphics card 147,529,728 pixels to draw, which is apparently unreasonable? The framerate dips to 20 or so, and the  effect loses some of the magic at that speed. <\/p>\n<p>So there it is. Another disposable afternoon project. As before, it&#8217;s released under a &#8220;do as you please&#8221; deal. <\/p>\n<p><a href=\"http:\/\/www.shamusyoung.com\/files\/plasma_binary.zip\">Plasma screensaver<\/a>.<br \/>\n<a href=\"http:\/\/www.shamusyoung.com\/files\/plasma_source.zip\">Plasma screensaver source<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So we&#8217;re doing another one of these things. I know this isn&#8217;t as fun as a Good Robot update, but hopefully this can keep us amused until the project gets moving again. While working on this, I found an interesting wrinkle that gives us a reason to talk about poly count vs. fill rate and [&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-22004","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\/22004","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=22004"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/22004\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}