{"id":50872,"date":"2020-09-29T06:00:19","date_gmt":"2020-09-29T10:00:19","guid":{"rendered":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=50872"},"modified":"2020-09-29T02:07:51","modified_gmt":"2020-09-29T06:07:51","slug":"project-bug-hunt-4-atlas","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=50872","title":{"rendered":"Project Bug Hunt #4: Atlas"},"content":{"rendered":"<p>I&#8217;m afraid this one got away from me. I wanted to talk about code reuse. So I decided to talk about the atlas texture I&#8217;m using. But then I needed to explain what that is and why we need it. And then I got dragged into asides about how I use my atlas and some theory about how texture addressing works.\u00a0\u00a0<\/p>\n<p>So my two-paragraph aside is now an entire entry of information that&#8217;s not directly connected to the problem of generating 3D levels. I could send this mess back for another round of editing and restructuring, but then I&#8217;d have nothing to post this week and you would be sad.<\/p>\n<blockquote><p>I have made this longer than usual because I have not had time to make it shorter. &#8211; <a href=\"https:\/\/quoteinvestigator.com\/2012\/04\/28\/shorter-letter\/\">Blaise Pascal<\/a><\/p><\/blockquote>\n<p>I don&#8217;t know if this entry will hold up, but I think we can all at least agree that Blaise Pascal is one of the coolest names ever. My anglo-centric take on his first name is that it ought to be pronounced &#8220;Blaze&#8221;. Blaze Pascal! That&#8217;s practically a superhero name.<\/p>\n<p>Anyway, I&#8217;m now making this overlong post even longer by complaining about the length. Let&#8217;s just Get On With It before I make everything worse.<\/p>\n<p><!--more--><\/p>\n<h3>Overdraw<\/h3>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bughunt_overdraw.jpg' width=100% alt='Happy little pixels.' title='Happy little pixels.'\/><\/div><div class='mouseover-alt'>Happy little pixels.<\/div><\/p>\n<p>Every graphics generation has its own bottleneck. At one point in history it was all about rendering as few polygons as possible, because those were calculated by your feeble mid-90s CPU, and that poor thing was already being overworked. Then a few years later all of that work was offloaded to your graphics card<span class='snote' title='1'>You might remember the times when you&#8217;d hear about needing a card that supported &#8220;Transform and Lighting&#8221; to play a game. That was the point where we dumped a lot of the polygon work onto the GPU.<\/span>, and suddenly &#8220;fill rate&#8221; was the big concern. Developers stopped being so obsessive about reducing the polygon count of everything and started worrying about how many total pixels the program had to draw.\u00a0<\/p>\n<p>At the time, you&#8217;d hear people talking about how to reduce &#8220;overdraw&#8221;. Overdraw is when you draw a bunch of pixels (perhaps to render the background) and then cover them up when you draw stuff in the foreground. If you&#8217;ve ever seen a Bob Ross painting, then you know he starts by filling the entire canvas with the sky color, then paints over most of it to create a gradient, then paints over 80% of the sky to draw the mountains, then paints over more sky with clouds, then covers up about half the mountains with happy little trees. In the end, he probably paints over enough surface area to cover 3 or 4 canvases. If Bob Ross was a game developer in the aughts, we&#8217;d say he had a massive overdraw problem.<\/p>\n<p>My knowledge of these problems kind of trails off around 2010 or so. As time has gone on, the whole system has grown more complex and these days there&#8217;s just too much to keep up with if you&#8217;re just a hobbyist like me.\u00a0<\/p>\n<p>However, I have gleaned a bit of wisdom from <a href=\"https:\/\/www.youtube.com\/watch?v=vmUbL16JQcI\">other developers<\/a>, <a href=\"https:\/\/www.youtube.com\/channel\/UC0JB7TSe49lg56u6qH8y_MQ\">GDC talks<\/a>, and <a href=\"?p=20541\">John Carmack sightings<\/a>. And I know that among the many concerns that devs have to worry about these days, limiting the number of draw calls is still very important.<\/p>\n<h3>Draw Calls<\/h3>\n<p>A draw call is when your graphics engine tells the GPU<span class='snote' title='2'>The graphics card.<\/span> &#8220;Hey, here&#8217;s a big lump of polygons. Draw these polygons in these different locations, using this texture. This would draw (say) all the instances of &#8220;wooden_door_3&#8221; in the entire level. A modern GPU is designed around massive parallelism. The polygons and texture are loaded into the card&#8217;s memory, and then the card is ready to process them in bulk. One processing unit on the card can be drawing the first door, another can be drawing the door elsewhere in the level, and so on. I don&#8217;t know how many lanes we have on the cards these days, but it&#8217;s <i>probably<\/i> a safe-ish guess to say that a modern GPU can draw a dozen doors just as quickly as they draw 1, provided that the developer has everything set up properly so that things can be done in batches.\u00a0<\/p>\n<p>It takes the same amount of time to bake one cookie as it does to bake a dozen of them. This is kinda the same deal<span class='snote' title='3'>It&#8217;s the year of Covid-19, so rather than leaving the house for a terrible car analogy, I thought it would be safer to stay inside and try a terrible baking analogy.<\/span>. The more stuff you can do in a single draw call, the closer you&#8217;ll get to utilizing 100% of that precious GPU power.<\/p>\n<p>But what if we&#8217;re not efficient about it? Let&#8217;s say we don&#8217;t put models into batches and we fling crap at the GPU one at a time, in random order. What if we bake our cookies one at a time? What you&#8217;ll get is something like this:<\/p>\n<p>We load the door model and door texture into memory. Then one small part of the GPU will draw the door while most of the rest of the card sits idle. Now it&#8217;s time to draw (say) a bathtub. So now the entire GPU needs to idle until we get the new models and textures into place. Then once again we use a fraction of the GPU&#8217;s power while the rest sits idle. Then we have another pause<span class='snote' title='4'>These pauses are measured in microseconds or smaller. These individual moments aren&#8217;t something you can &#8220;feel&#8221;, unless you get enough of them happening in a single frame.<\/span> The difference in performance can be extreme. Poor management of draw calls can make even the most powerful graphics cards run poorly, since you&#8217;ll always be wasting a majority of its capacity no matter how fast it is. In fact, the better the card, the more damage it does when you fail to properly manage draw calls.<\/p>\n<p><i>Disclaimer: The above description is a massive oversimplification. There are different stages of memory that run at different speeds, different amounts of memory that can help mitigate the cost of switching to a new texture, and a bunch of other hairy details I&#8217;m not qualified to explain. And like I said, my knowledge is spotty and out-of-date these days.<\/i>\u00a0<\/p>\n<p>Now, Unity is handling most of this for me. It keeps track of models and textures and does its best to do things in large batches. As a Unity developer, you just need to make sure all of those wooden doors draw from the same geometry<span class='snote' title='5'>When you&#8217;re a newbie, it&#8217;s easy to accidentally clone geometry instead of sharing it, meaning every door will get a copy of the geometry instead of everyone sharing a single copy.<\/span> and texture, and you&#8217;re all good.\u00a0<\/p>\n<h3>Atlas Texture<\/h3>\n<p>In terms of texture-switching vs. model-switching, texture-switching seems to be the bigger boogeyman. Getting back to our Bob Ross analogy, switching textures is like changing colors on your brush. If you&#8217;re cleaning off your brush after every stoke, then the job will take ages. On the other hand, if you do all the blue sky, then all the white mountains, then all the green trees, then you&#8217;ll work incredibly quickly. (For this example to fit, we have to assume that your paintbrush doesn&#8217;t run out of paint as long as you&#8217;re using the same color, and that changing color means throwing away the old brush and driving to the store for a new one.)<\/p>\n<p>So now you&#8217;re probably thinking, &#8220;Hey Shamus, the engine doesn&#8217;t care what the texture looks like. Why don&#8217;t you put a bunch of images on the same texture and just use that one texture for everything? Then you&#8217;d never have to change textures!&#8221;<\/p>\n<p>Yes! That&#8217;s a thing. It&#8217;s called an atlas texture. If you&#8217;re curious what one looks like, here&#8217;s a really old Minecraft atlas:<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bughunt_atlas1.jpg' width=100% alt='This was just the first result I found on Google. Looking closer, I think this is from an add-on texture pack. (Maybe Painterly Pack? I don&apos;t know. It&apos;s been ages.) Either way, you get the idea.' title='This was just the first result I found on Google. Looking closer, I think this is from an add-on texture pack. (Maybe Painterly Pack? I don&apos;t know. It&apos;s been ages.) Either way, you get the idea.'\/><\/div><div class='mouseover-alt'>This was just the first result I found on Google. Looking closer, I think this is from an add-on texture pack. (Maybe Painterly Pack? I don&apos;t know. It&apos;s been ages.) Either way, you get the idea.<\/div><\/p>\n<p>Atlas textures do help a great deal, although they make asset production more complicated.\u00a0<\/p>\n<p>As I&#8217;ve explained before. Models are made from triangles, and triangles are made by playing connect-the-dots between vertices. Every vertex in a model will have its location in 3D space, which is usually represented by the variables <b>xyz<\/b>. We also have a set of texture coordinates, which are expressed as <b>uv<\/b>. These coords say what part of the texture you&#8217;re interested in. A <b>uv<\/b> value of (0.25, 0) means 25% of the way across the image left-to-right, at the very top of the image.<\/p>\n<div class=\"dmnotes\">Annoying complication: Different systems reference the vertical in different ways. For some engines \/ APIs, 0 is the top and 1 is the bottom, where other things have things the other way around. If I remember correctly<span class='snote' title='6'>There is a very low probability of this being true.<\/span>, then OpenGL was bottom-up and DirectX is top-down. Or maybe I&#8217;m thhinking of an engine? Whatever. Over the years I&#8217;ve lost track of which way is &#8220;up&#8221;, so I normally just flip vertical coordinates until things look right.<\/div>\n<p>Below, I&#8217;m going to continue to think of everything as top-down, because that&#8217;s consistent with how Windows screen coordinates, image editors, and written language works.\u00a0I&#8217;ve always been annoyed by bottom-up systems by the way they break from our normal assumptions about coordinates in 2D space.<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bughunt_atlas_character.jpg' width=100% alt='I wonder if the texture for Atlas is stored in an atlas texture?' title='I wonder if the texture for Atlas is stored in an atlas texture?'\/><\/div><div class='mouseover-alt'>I wonder if the texture for Atlas is stored in an atlas texture?<\/div><\/p>\n<p>A <b>uv<\/b> value of (0.5, 0.5) means halfway across the image and halfway down, giving you the dead center. If you hand it a <b>u<\/b> value of 2.75, you&#8217;re talking about going all the way across the image and then wrap around to the left, then go all the way across a second time and wrap around, and then go 75% of the way across.\u00a0<\/p>\n<p>This system is what allows you to repeat the same texture several times over a very large polygon. If you couldn&#8217;t wrap, then you&#8217;d need to create a new polygon every time you wanted to repeat the texture. It would be very tedious and very wasteful.<\/p>\n<p>The problem is that if you&#8217;re using an atlas texture, that &#8220;wrapping around&#8221; stops working. Let&#8217;s have another look at that Minecraft texture:<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bughunt_atlas1.jpg' width=100% alt='This atlas is probably a decade old by now. I wonder what the Minecraft atlas looks like these days?' title='This atlas is probably a decade old by now. I wonder what the Minecraft atlas looks like these days?'\/><\/div><div class='mouseover-alt'>This atlas is probably a decade old by now. I wonder what the Minecraft atlas looks like these days?<\/div><\/p>\n<p>Look at the grass texture in the top-left position. If I try to repeat that grass 3 times horizontally, it&#8217;s not going to wrap. It&#8217;ll just keep going and grab the stone and dirt texture entries.<\/p>\n<p>So what you need here is a special shader. You can feed it normal repeating <b>uv<\/b> values, and then the shader will constrain their values to the specific sub-image you&#8217;re interested in. If it goes off the right side of the sub-image, it&#8217;ll wrap to the left edge, and if it goes off the bottom it&#8217;ll wrap to the top<span class='snote' title='7'>And vice-versa. The point is that the texture should behave like an infinite plain that can tile as much as you want in any direction.<\/span>.\u00a0<\/p>\n<p>Now you just need a way to explain to the graphics hardware WHERE that sub-image is. (I personally call them &#8220;cells&#8221; because it&#8217;s shorter, but I don&#8217;t know that the official industry terminology is.)<\/p>\n<p><b>The Brute-Force Way<\/b><\/p>\n<p>So we need to define a square region. We need some way to tell the GPU, &#8220;Okay, constrain the standard uv values to region such-and-such of the texture. We could do this by sending two additional coordinate pairs. One will be the upper-left corner of the cell, and the other will be the lower-right. This means we need an additional four variables, which we can call <b>qr<\/b> and <b>st<\/b>.<\/p>\n<p>This isn&#8217;t a terrible setup. Four variables isn&#8217;t much in the grand scheme of things. But this is a non-zero cost. Can we get this done in three?<\/p>\n<p><b>The Slightly Smarter Way<\/b><\/p>\n<p>If we agree ahead of time that &#8211; as in the case of the Minecraft atlas &#8211; all cells need to be the same size, then we can get rid of the variables <b>st<\/b>. We just hand it the upper-left corner and it will be able to figure out the rest.<\/p>\n<p>The disadvantage of this system is that every cell needs to be the same size. That&#8217;s fine if you&#8217;re making a Minecraft-style cube world where everything is built on the same 1-meter grid, but it&#8217;s a  mess if we try to do it in some other genre \/ art style. The tiny little control panel on the captain&#8217;s chair will take the same number of pixels as the giant viewscreen at the front of the bridge<span class='snote' title='8'>Sure, you could make the viewscreen out of hundreds of little sub-textures, but that would be an amazing pain in the ass to model. Your art team will probably try to assassinate you if you try.<\/span>.\u00a0<\/p>\n<p>We could use a third number for cell size. So <b>qr<\/b> gives us the cell origin, and <b>s<\/b> gives you the size.<\/p>\n<p>But can we do even better? Can we get the job done in just two numbers?<\/p>\n<p><b>My Way<\/b><\/p>\n<p>To be honest, I have no idea what the &#8220;official&#8221; way to handle this is. I doubt it looks anything like mine. I imagine the official system is a lot smarter in some way that never occurred to me. This is just what I worked out a few years ago on one of my own projects.<\/p>\n<p>We have two variables: <b>qr<\/b>. The <b>q<\/b> defines the overall size of the grid for this particular cell, and <b>r<\/b> provides both the horizontal and vertical positions on the grid. It sounds weird, but it&#8217;s actually simple and great for lazy people. <\/p>\n<p>A q value of 4 tells the shader to pretend that the entire texture is a 4&#215;4 grid of sub-texture cells. That means each cell is the size of 1\/q. A value of 2 would mean the shader should think of the whole texture as a 2&#215;2 grid of sub-textures. Thus each cell is 0.5 wide and tall. A 4 would give us a cell size of 0.25. And so on.<\/p>\n<p>So now we know how big our cell is. To find its origin: Divide <b>r<\/b> by <b>q<\/b>, and round it down to the nearest whole number. That&#8217;s your row. Now divide <b>r<\/b> by <b>q<\/b> again, only this time keep just the remainder.<\/p>\n<p>For a simpler way to visualize it, just imagine the cells are numbered left-to-right, top-to-bottom.<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bughunt_atlas2.png' width=100% alt='' title=''\/><\/div><div class='mouseover-alt'><\/div><\/p>\n<p>Is this still a bit hard to follow for you? Me too. So when I wrote this in 2019 I left myself some comments at the top of my shader file to remind me of how it works:<\/p>\n<pre lang=\"cpp\">\r\n\/*--------------------------------------------------------------------------\r\nThis shader is designed to allow many textures to exist in the same \r\ntexture image. This shader depends on two sets of UV coords. The \r\nfirst set comes from TEXCOORD0, and is the standard texture address.\r\n\r\nThe second UV comes from TEXCOORD1. The X value defines the size of \r\nthe texture grid. So a value of 4 would break the texture into a 4x4 \r\ngrid. The Y value is the number of the cell within that grid, numbering\r\nleft-to-right, bottom-to-top. A 4x4 grid would be numbered so:\r\n\r\nExamples:\r\n\r\nTEXCOORD1 = (4,12): SubTexture would be in square #12 of the diagram below.\r\n\r\n*---*---*---*---*\r\n| 12| 13| 14| 15|\r\n*---*---*---*---*\r\n| 8 | 9 | 10| 11|\r\n*---*---*---*---*\r\n| 4 | 5 | 6 | 7 |\r\n*---*---*---*---*\r\n| 0 | 1 | 2 | 3 |\r\n*---*---*---*---*\r\n\r\nTEXCOORD1 = (2,3): SubTexture is the upper-right quarter of the texture like so:\r\n\r\n*---*---*\r\n| 2 | 3 |\r\n*---*---*\r\n| 0 | 1 |\r\n*---*---*\r\n\r\n--------------------------------------------------------------------------*\/\r\n<\/pre>\n<p>Ah, I see here that I&#8217;m using bottom-to-top coords. Sigh. Whatever. That will never not feel upside-down to me, but whatever.<\/p>\n<p>The main reason I love this system is that it&#8217;s human-readable. If I&#8217;m trying to fix a bug and I find myself needing to examine individual texture coordinates, I want to be able to tell right numbers from wrong ones. If I&#8217;m looking at some texture coordinates see the numbers (0.3125, 0.1875), it&#8217;s not immediately clear which cell we&#8217;re dealing with. But if I see (16, 83), I can quickly work out that I should look for the cell in row 5, column 3 of a 16&#215;16 grid in my atlas texture. That will tell me what I SHOULD be seeing, which gets me halfway to figuring out where to find the bug that screwing up the process.<\/p>\n<p>This also means that I can have cells of mixed sizes on my atlas. As long as they&#8217;re powers of 2 in size, a 1024&#215;1024 atlas can support textures anywhere from 512&#215;512 in size, all the way down to dinky little 2&#215;2 textures.\u00a0<\/p>\n<h3>My Atlas<\/h3>\n<p>I don&#8217;t have any early versions of my atlas texture. But if you don&#8217;t mind a bit of a spoiler, then here&#8217;s the one I&#8217;m using now in late September:<\/p>\n<p><div class='imagefull'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/bughunt_atlas4.png' width=100% alt='More than half of this is old junk from the early stages of the project that I haven&apos;t gotten around to deleting yet.' title='More than half of this is old junk from the early stages of the project that I haven&apos;t gotten around to deleting yet.'\/><\/div><div class='mouseover-alt'>More than half of this is old junk from the early stages of the project that I haven&apos;t gotten around to deleting yet.<\/div><\/p>\n<p>My texture is actually 1024&#215;1024, but the top half is unused empty space right now so I cropped that out. You can see that we&#8217;ve got cells of many different sizes. The weird advertisements near the middle are left over from a bit when I was experimenting with glowing billboards and just needed something quick for testing.<\/p>\n<p>We&#8217;ll come back to this next week next week, and hopefully get back to the main point of the project.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m afraid this one got away from me. I wanted to talk about code reuse. So I decided to talk about the atlas texture I&#8217;m using. But then I needed to explain what that is and why we need it. And then I got dragged into asides about how I use my atlas and some [&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-50872","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\/50872","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=50872"}],"version-history":[{"count":15,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/50872\/revisions"}],"predecessor-version":[{"id":50887,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/50872\/revisions\/50887"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=50872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=50872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=50872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}