{"id":26340,"date":"2015-04-09T21:16:46","date_gmt":"2015-04-10T02:16:46","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=26340"},"modified":"2015-04-10T13:34:48","modified_gmt":"2015-04-10T18:34:48","slug":"the-strange-evolution-of-opengl-part-3","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=26340","title":{"rendered":"The Strange Evolution of OpenGL Part 3"},"content":{"rendered":"<p>So <a href=\"?p=26282\" title=\"The Strange Evolution of OpenGL Part 2\">last time<\/a> I described how OpenGL used to work in the pre-graphics card stone age. Those were simpler, clearer days. Yes, they were also slow as hell and unable to do much in the way of fancy graphics. But, you know, <em>simple<\/em>.<\/p>\n<p>But the simplicity couldn&#8217;t last. The evolution of OpenGL is basically a long series of refinements where more and more work was gradually moved to the graphics card. Let&#8217;s go over them.<\/p>\n<p><!--more--><\/p>\n<h3>1. Rasterization<\/h3>\n<p>As far as I can tell, this was the &#8220;killer app&#8221; of graphics cards. Your game shoves the texture maps over to the graphics card. Then it tells OpenGL how big the canvas (the screen) is. Then it sends a bunch of 2D triangles along the lines of, &#8220;This triangle occupies this part of the screen and uses such-and-such part of this texture.&#8221; The graphics card would do all the work of coloring those triangles in with pixels. <\/p>\n<p>Let&#8217;s talk about a CPU. The CPU inside your computer is a complex beast. Even ignoring the fact that it&#8217;s actually many cores strapped together, it needs to be able to perform all sorts of operations. Code needs to be able to branch. (If X then do thing A or else do thing B.) It needs to be able to switch between running one program and another, completely different program constantly. It&#8217;s got this complex system where it brings in data from main memory and moves it into progressively faster but smaller banks of memory. Basically, it needs to be able to run anything. I&#8217;ve heard people refer to this as being a machine that is <a href=\"http:\/\/en.wikipedia.org\/wiki\/Turing_completeness\">Turing Complete<\/a>.<\/p>\n<p>Not so with graphics cards. They just needed to fill in triangles with pixels. They didn&#8217;t need to do three completely different things at once, or handle complex branching code. Since their output was in colored pixels and not hard data, a certain degree of slop was allowed. The math could make certain approximations and shortcuts because if the output was 0.01% off, nobody would be able to tell. Even if you had superhuman eyes that could spot the subtle color differences, you&#8217;re using a monitor that can&#8217;t <em>display<\/em> differences that slight. <\/p>\n<p>All of this meant that graphics processors could be much simpler. It&#8217;s a bit like comparing a classic fast-food place to the work of a single highly trained chef. The chef can make you almost anything you can ask for, while the fast food place can only make hamburgers. But the fast food place is optimized for it and can crank out 24 hamburgers a minute<span class='snote' title='1'>Something like that. You young people don&#8217;t realize this, but fast food used to be FAST. Back before McNuggets, McChicken, McRib, McFish, McSoup, McPizza, Fajitas, Grilled Chicken, Chicken Tenders, six different salads and ten different types of hamburger.<\/span>. Simplicity is speed.<\/p>\n<p>Simpler cores didn&#8217;t just make them faster, it also made them smaller. So instead of one giant core they could have a whole bunch of them. And those cores would do nothing but take in 2D triangles and spit out pixels. <\/p>\n<h3>2. Transform and Lighting<\/h3>\n<p>So it&#8217;s sometime in the late 90&#8217;s and we&#8217;ve got these graphics cards that take 2D triangles<span class='snote' title='2'>That is, a triangle expressed in terms of where it will appear on the screen. The graphics card has NO IDEA where the vertex might be in terms of your 3D game.<\/span> and fill them in with pixels. That&#8217;s cool, I guess. It was certainly a massive boost in terms of our rendering capabilities. But it was clear they could be doing a lot more. <\/p>\n<p>There&#8217;s a step I&#8217;ve been sort of glossing over here. That&#8217;s the bit of math you have to do to figure out if a particular triangle will end up on-screen, and if so, where. &#8220;Hm. Given that the camera is in position C and looking in direction D, and this particular triangle is so many units away, then the vertices of this triangle will end up on <em>this<\/em> part of the screen.&#8221; Like coloring in triangles with pixels, this is yet another brute-force, bulk, doing-math-on-three-numbers kind of job<span class='snote' title='3'>Yes nitpickers, it&#8217;s actually FOUR numbers. But I don&#8217;t want to burn through a couple of paragraphs explaining why. We&#8217;re already in a two-levels-deep digression. Let It Go.<\/span>. Which means it&#8217;s a good thing to offload onto the graphics card.<\/p>\n<p>This process of translating vertices from game-space to screen-space is called &#8220;Transform and lighting&#8221;<span class='snote' title='4'>I&#8217;m glossing over the whole &#8220;lighting&#8221; thing here because I don&#8217;t think we need to go over it in detail. And we&#8217;ve got enough ground to cover as it is.<\/span>. <\/p>\n<p>Because I spent so much of my career riding the tail end on the technology curve, the timeline is always a bit muddled for me. Apparently <a href=\"http:\/\/www.nvidia.com\/page\/geforce256.html\">all of this happened in 1999<\/a>, but I didn&#8217;t really think about it for at least another four years or so<span class='snote' title='5'>Which is a long time in computer graphics, and a <strong>really<\/strong> long time at <em>this particular point<\/em> in graphics evolution.<\/span>. <\/p>\n<p>According to the NVIDIA page, the new T&#038;L-capable cards offered &#8220;an order of magnitude increase in visual complexity&#8221;. An &#8220;order of magnitude&#8221; is just engineer talk for &#8220;ten times more \/ less&#8221;, but it sounds so much more impressive and technical than just saying &#8220;ten times&#8221;. On one hand, I&#8217;d caution against believing breathless claims made by marketing. On the other hand, that sounds pretty accurate. We really did get a huge jump up in model complexity.<\/p>\n<p>Here is a screenshot from the pre-T&#038;L game Thief:<\/p>\n<p><table width='800' class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/thief1_4.jpg' class='insetimage' width='800' alt='Ha ha. The graphics in the past were so terrible. The gameplay, on the other hand. . .' title='Ha ha. The graphics in the past were so terrible. The gameplay, on the other hand. . .'\/><\/td><\/tr><\/table><\/p>\n<p>The canister on that table is a cylinder with 5 sides. (Not counting the top and bottom.) It looks ridiculous. At the time, every polygon was a precious thing, and we couldn&#8217;t afford to waste them. So our games were filled with coarse geometric shapes. When we were able to offload a bunch of polygon processing to the GPU, we suddenly had enough polygons to make round things look round. <\/p>\n<p>The problem was, this jump was almost too big. It was so big that we kind of didn&#8217;t need to make another. If we double the number of sides in the canister above, you&#8217;ll end up with something that looks completely round to the user. Maybe if they mash their face into the object they will be able to see the angled edges, but it&#8217;s no longer the glaring problem. The jump from 5 to 10 polygons is a lot more visually important than the jump from (say) 10 to 100, or even 1,000. We suddenly had as many polygons as we could hope to use on those old machines. <\/p>\n<h3>3. Vertex Buffers<\/h3>\n<p>Remember that a graphics card is, in a lot of ways, a separate computer. It&#8217;s got its own memory and its own processors. So when you want to tell the graphics card to render a triangle, you need to send it all of the information about that triangle. There is a bit of a choke point between the devices, meaning it takes much longer to send a triangle to the graphics card that it does to (say) move the triangle from one part of memory to another.<\/p>\n<p>Once you have T&#038;L, you&#8217;ll quickly notice that you spend a lot of time sending the exact same data to the GPU over and over again. Every single frame, you tell the graphics cards about the same exact polygons that are still in the same positions. (The walls of your level, and other non-dynamic stuff.) The camera is moving, not the walls. So why do I need to keep sending the same huge load of data every frame?<\/p>\n<p>So vertex buffers give us a way to shove all the data from the PC and store it on the GPU. So instead of sending 1,000 polygons, I just need to tell the card, &#8220;Remember where I gave you 1,000 triangles? Draw those again, but with the camera in this new location.&#8221;<\/p>\n<h3>4. Shaders<\/h3>\n<p>Like I said a few paragraphs ago: If we wanted games to continue improving visually, it was pretty clear that mindlessly cranking up the polygon counts wasn&#8217;t the way to go. To take the next step, we needed to change <strong>how<\/strong> we drew those polygons. And for that we needed shaders.<\/p>\n<p>From this point, it was no longer possible to offer a generalized solution for all games. You could make a graphics card that was good at cel shading, but that would only help with games that were cel shaded. You could make a card good at bump mapping, but that wouldn&#8217;t do anything for games without bump maps. Instead of adding more &#8220;features&#8221; to the graphics card, we just needed to give developers a way to control all that raw rendering power directly. We needed to give them a way to write programs that ran on the graphics hardware. <\/p>\n<p>So now developers need to make two shaders: A vertex shader to do the Transform &#038; Lighting, and a fragment shader to do the rasterization. <\/p>\n<p><table width='800' class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/octant11_1.png' class='insetimage' width='800' alt='Not pictured: A complete lack of documentation, ambiguous standards, sloppy implementation, and standards-breaking features from the big GPU companies.' title='Not pictured: A complete lack of documentation, ambiguous standards, sloppy implementation, and standards-breaking features from the big GPU companies.'\/><\/td><\/tr><\/table><\/p>\n<p>Shaders made a lot of things possible or practical: Light bloom, anti-aliasing, various lighting tricks, normal maps. This was a massive turning point in game development. In one leap:<\/p>\n<ol>\n<li>Games took a <em>massive<\/em> step forward in visual quality. I think this was actually really important from a cultural perspective. Serious games now looked good enough that they could show up in a TV commercial without looking ridiculous. Before this point, only cartoon-y games (Mario, et al) could get away with this.\n<li>Games took a huge jump in expense to produce.\n<li>Games took a big jump in complexity. In the late 90&#8217;s, it was still possible for a small team to get together and make a AAA game without publisher backing<span class='snote' title='6'><a href=\"http:\/\/en.wikipedia.org\/wiki\/BioWare\">BioWare<\/a> is the oft-cited example of this.<\/span>. This was the beginning of the end of that era. (You could argue that with the release of so many free AAA game engines, those days are returning. But that&#8217;s another article.)\n<\/ol>\n<p>Like a lot of technical advancements, you can argue about where to &#8220;officially&#8221; draw this particular line. I draw it at 2004. Half-Life 2. Doom 3. Thief Deadly Shadows. Compare each of those games with their predecessors<span class='snote' title='7'>To be clear, I mean you should compare Doom 3 with Quake III Arena, not with Doom 2.<\/span> to see just how extreme this move was. Visually, I think Doom 3 has more in common with the games of 2015 than it does the games of 1999. <\/p>\n<p><table width='800' class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><a href='images\/bump_map_full.jpg'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bump_map_small.jpg' class='insetimage' width='800' alt='Left is without normal maps. Right is standard rendering. Note the keyboard and face. Click for LOLHUGE! view.' title='Left is without normal maps. Right is standard rendering. Note the keyboard and face. Click for LOLHUGE! view.'\/><\/a><\/td><\/tr><tr><td class='insetcaption'>Left is without normal maps. Right is standard rendering. Note the keyboard and face. Click for LOLHUGE! view.<\/td><\/tr><\/table><\/p>\n<h3>5. More Shaders<\/h3>\n<p>Since 2004, the graphics race has mostly been about what we can do with shaders. I can&#8217;t think of any big changes since then. Every once in a while shader programs get some new ability. Sometimes the ability is explicit. <em>We&#8217;ve added some ability to manipulate pixel data in ways that weren&#8217;t possible before.<\/em> Sometimes the feature is implicit. <em>Graphics hardware is now fast enough to do some heavy-duty lighting effect that would have been too slow on the old cards.<\/em> But either way, the steps have been more evolutionary than revolutionary.  <\/p>\n<p>And to be honest, for me all the changes kind of blur together here. The documentation on the OpenGL shading languages isn&#8217;t that great to begin with, so it&#8217;s pretty hard to piece together the various iterations of the language if you weren&#8217;t already following them when they happened. <\/p>\n<p>The point of all this is: Right now, in 2015, you can use any or none of these features of OpenGL. You can render with all of the advancements of the last 20 years, or you can render raw immediate-mode triangles like it&#8217;s 1993. This has made OpenGL sort of cluttered, confusing, obtuse, and ugly. It&#8217;s poisoned the well of documentation by making sure there are five conflicting answers to every question, and it&#8217;s difficult for the student to know if the answer they&#8217;re reading is actually the most recent. <\/p>\n<p>So now the Kronos group &#8211; the folks behind OpenGL &#8211; are wiping the slate clean and trying to come up with something specifically designed for the world of rendering as it exists today. Vulkan is the new way of doing things, and it is not an extension of OpenGL. It&#8217;s a new beast. <\/p>\n<p>I know nothing about it, aside from the overview-style documents I&#8217;ve read and the tech demos I&#8217;ve seen. Given my habit of lagging behind technology until the documentation has a chance to catch up, I probably won&#8217;t mess with Vulkan for a few years. <\/p>\n<p>In the meantime: OpenGL is strange and difficult, and there&#8217;s nothing we can do about it. This is a rotten time to be learning low-level rendering stuff. The old way is a mess and the new way isn&#8217;t ready yet.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So last time I described how OpenGL used to work in the pre-graphics card stone age. Those were simpler, clearer days. Yes, they were also slow as hell and unable to do much in the way of fancy graphics. But, you know, simple. But the simplicity couldn&#8217;t last. The evolution of OpenGL is basically a [&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-26340","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\/26340","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=26340"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/26340\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}