<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The ryg blog]]></provider_name><provider_url><![CDATA[https://fgiesen.wordpress.com]]></provider_url><author_name><![CDATA[fgiesen]]></author_name><author_url><![CDATA[https://fgiesen.wordpress.com/author/fgiesen/]]></author_url><title><![CDATA[Optimizing Software Occlusion Culling: The&nbsp;Reckoning]]></title><type><![CDATA[link]]></type><html><![CDATA[<p><em>This post is part of a series &#8211; go <a href="https://fgiesen.wordpress.com/2013/02/17/optimizing-sw-occlusion-culling-index/">here</a> for the index.</em></p>
<p>Welcome back! Last time, I promised to end the series with a bit of reflection on the results. So, time to find out how far we&#8217;ve come!</p>
<h3>The results</h3>
<p>Without further ado, here&#8217;s the breakdown of per-frame work at the end of the respective posts (names abbreviated), in order:</p>
<table>
<tr>
<th>Post</th>
<th>Cull / setup</th>
<th>Render depth</th>
<th>Depth test</th>
<th>Render scene</th>
<th>Total</th>
</tr>
<tr>
<td>Initial</td>
<td>1.988</td>
<td>3.410</td>
<td>2.091</td>
<td>5.567</td>
<td>13.056</td>
</tr>
<tr>
<td>Write&nbsp;Combining</td>
<td>1.946</td>
<td>3.407</td>
<td>2.058</td>
<td>3.497</td>
<td>10.908</td>
</tr>
<tr>
<td>Sharing</td>
<td>1.420</td>
<td>3.432</td>
<td>1.829</td>
<td>3.490</td>
<td>10.171</td>
</tr>
<tr>
<td>Cache&nbsp;issues</td>
<td>1.045</td>
<td>3.485</td>
<td>1.980</td>
<td>3.420</td>
<td>9.930</td>
</tr>
<tr>
<td>Frustum&nbsp;culling</td>
<td>0.735</td>
<td>3.424</td>
<td>1.812</td>
<td>3.495</td>
<td>9.466</td>
</tr>
<tr>
<td>Depth buffers&nbsp;1</td>
<td>0.740</td>
<td>3.061</td>
<td>1.791</td>
<td>3.434</td>
<td>9.026</td>
</tr>
<tr>
<td>Depth buffers&nbsp;2</td>
<td>0.739</td>
<td>2.755</td>
<td>1.484</td>
<td>3.578</td>
<td>8.556</td>
</tr>
<tr>
<td>Workers&nbsp;1</td>
<td>0.418</td>
<td>2.134</td>
<td>1.354</td>
<td>3.553</td>
<td>7.459</td>
</tr>
<tr>
<td>Workers&nbsp;2</td>
<td>0.197</td>
<td>2.217</td>
<td>1.191</td>
<td>3.463</td>
<td>7.068</td>
</tr>
<tr>
<td>Dataflows</td>
<td>0.180</td>
<td>2.224</td>
<td>0.831</td>
<td>3.589</td>
<td>6.824</td>
</tr>
<tr>
<td>Speculation</td>
<td>0.169</td>
<td>1.972</td>
<td>0.766</td>
<td>3.655</td>
<td>6.562</td>
</tr>
<tr>
<td>Mopping up</td>
<td>0.183</td>
<td>1.940</td>
<td>0.797</td>
<td>1.389</td>
<td>4.309</td>
</tr>
<tr>
<td><b>Total&nbsp;diff.</b></td>
<td>-90.0%</td>
<td>-43.1%</td>
<td>-61.9%</td>
<td>-75.0%</td>
<td>-67.0%</td>
</tr>
<tr>
<td><b>Speedup</b></td>
<td>10.86x</td>
<td>1.76x</td>
<td>2.62x</td>
<td>4.01x</td>
<td>3.03x</td>
</tr>
</table>
<p>What, you think that doesn&#8217;t tell you much? Okay, so did I. Have a graph instead:</p>
<p><a href="https://fgiesen.files.wordpress.com/2013/03/post_breakdown1.png"><img src="https://fgiesen.files.wordpress.com/2013/03/post_breakdown1.png?w=1024&#038;h=356" alt="Time breakdown over posts"   class="aligncenter size-large wp-image-1963" /></a></p>
<p>The image is a link to the full-size version that you probably want to look at. Note that in both the table and the image, updating the depth test pass to use the rasterizer improvements is chalked up to &#8220;Depth buffers done quick, part 2&#8221;, not &#8220;The care and feeding of worker threads, part 1&#8221; where I mentioned it in the text.</p>
<p>From the graph, you should clearly see one very interesting fact: the two biggest individual improvements &#8211; the write combining fix at 2.1ms and &#8220;Mopping up&#8221; at 2.2ms &#8211; both affect the <em>D3D rendering code</em>, and don&#8217;t have anything to do with the software occlusion culling code. In fact, it wasn&#8217;t until &#8220;Depth buffers done quick&#8221; that we actually started working on that part of the code. Which makes you wonder&#8230;</p>
<h3>What-if machine</h3>
<p>Is the software occlusion culling actually worth it? That is, how much do we actually get for the CPU time we invest in occlusion culling? To help answer this, I ran a few more tests:</p>
<table>
<tr>
<th>Test</th>
<th>Cull / setup</th>
<th>Render depth</th>
<th>Depth test</th>
<th>Render scene</th>
<th>Total</th>
</tr>
<tr>
<td>Initial</td>
<td>1.988</td>
<td>3.410</td>
<td>2.091</td>
<td>5.567</td>
<td>13.056</td>
</tr>
<tr>
<td>Initial,&nbsp;no&nbsp;occ.</td>
<td>1.433</td>
<td>0.000</td>
<td>0.000</td>
<td>25.184</td>
<td>26.617</td>
</tr>
<tr>
<td>Cherry-pick</td>
<td>1.548</td>
<td>3.462</td>
<td>1.977</td>
<td>2.084</td>
<td>9.071</td>
</tr>
<tr>
<td>Cherry-pick, no&nbsp;occ.</td>
<td>1.360</td>
<td>0.000</td>
<td>0.000</td>
<td>10.124</td>
<td>11.243</td>
<tr>
<td>Final</td>
<td>0.183</td>
<td>1.940</td>
<td>0.797</td>
<td>1.389</td>
<td>4.309</td>
</tr>
<tr>
<td>Final,&nbsp;no&nbsp;occ.</td>
<td>0.138</td>
<td>0.000</td>
<td>0.000</td>
<td>6.866</td>
<td>7.004</td>
</tr>
</table>
<p>Yes, the occlusion culling was a solid win both before and after. But the interesting value is the &#8220;cherry-pick&#8221; one. This is the original code, with only the following changes applied: (okay, and also with the timekeeping code added, in case you feel like nitpicking)</p>
<ul>
<li><a href="https://github.com/rygorous/intel_occlusion_cull/commit/e1839f69cf0680ad3339a5aa0f0b633bf71bcb68">Don&#8217;t read back from the constant buffers we&#8217;re writing</a>. Total diff: 3 lines.</li>
<li><a href="https://github.com/rygorous/intel_occlusion_cull/commit/1e1b5cca743c5ce26d2d5e8570f1ac689b5ce7fb">Don&#8217;t update debug counters in CPUTFrustum</a>. Total diff: 2 lines.</li>
<li><a href="https://github.com/rygorous/intel_occlusion_cull/commit/2504647a050e8c56ef2c4b4e03cce2ca7608343e">Use only one dynamic constant buffer</a>. Total diff: 10 lines changed, 8 added.</li>
<li><a href="https://github.com/rygorous/intel_occlusion_cull/commit/b4e29b2dfb43a040a9eb5ed5c074092766fe4ba7">Load materials only once</a>. Total diff: 7 lines changed, 1 added.</li>
<li><a href="https://github.com/rygorous/intel_occlusion_cull/commit/464503ca5bd657d7d6c6dc9e8a9144e1f223a278">Share materials instead of cloning them</a>. Total diff: 3 lines changed.</li>
<li><a href="https://github.com/rygorous/intel_occlusion_cull/commit/aa09c99a361988c1e7dd8765c0cbb9bd3bb5d527">AABBoxRasterizer traversal fix</a> &#8211; keep list of models instead of going over whole database every time. Total diff: 15 lines added, 18 deleted.</li>
</ul>
<p>In other words, &#8220;Cherry-pick&#8221; is within a few dozen lines of the original code, all of the changes are to &#8220;framework&#8221; code not the actual sample, and none of them do anything fancy. Yet it makes the difference between occlusion culling enabled and disabled shrink to about a 1.24x speedup, down from the 2x it was before!</p>
<h3>A brief digression</h3>
<p>This kind of thing is, in a nutshell, the reason why graphics papers really need to come with source code. Anything GPU-related in particular is <em>full</em> of performance cliffs like this. In this case, I had the source code, so I could investigate what was going on, fix a few problems, and get a much more realistic assessment of the gain to expect from this kind of technique. Had it just been a paper claiming a &#8220;2x improvement&#8221;, I would certainly not have been able to reproduce that result &#8211; note that in the &#8220;final&#8221; version, the speedup goes back to about 1.63x, but that&#8217;s with a considerable amount of extra work.</p>
<p>I mention this because it&#8217;s a very common problem: whatever technique the author of a paper is proposing is well-optimized and tweaked to look good, whereas the things that it&#8217;s being compared with are often a very sloppy implementation. The end result is lots of papers that claim &#8220;substantial gains&#8221; over the prior state of the art that somehow never materialize for anyone else. At one extreme, I&#8217;ve had one of my professors state outright at one point that he just stopped giving out source code to their algorithms because the effort invested in getting other people to successfully replicate his old results &#8220;distracted&#8221; him from producing new ones. (I&#8217;m not going to name names here, but he later stated a several other things along the same lines, and he&#8217;s probably the number one reason for me deciding against pursuing a career in academia.)</p>
<p>To that kind of attitude, I have only one thing to say: If you care only about producing results and not independent verification, then you may be brilliant, but you are not a scientist, and there&#8217;s a very good chance that your life&#8217;s work is useless to anyone but yourself.</p>
<p>Conversely, exposing your code to outside eyes might not be the optimal way to stroke your ego in case somebody finds an obvious mistake :), but it sure makes your approach a lot more likely to actually become relevant in practice. Anyway, let&#8217;s get back to the subject at hand.</p>
<h3>Observations</h3>
<p>The number one lesson from all of this probably is that there&#8217;s lots of ways to shoot yourself in the foot in graphics, and that it&#8217;s really easy to do so without even noticing it. So don&#8217;t assume, <em>profile</em>. I&#8217;ve used a fancy profiler with event-based sampling (VTune), but even a simple tool like Sleepy will tell you when a small piece of code takes a disproportionate amount of time. You just have to be on the lookout for these things.</p>
<p>Which brings me to the next point: you should always have an expectation of how long things should take. A common misconception is that profilers are primarily useful to identify the hot spots in an application, so you can focus your efforts there. Let&#8217;s have another look at the very first profiler screenshot I posted in this series:</p>
<p><a href="https://fgiesen.files.wordpress.com/2013/01/wc_slow1.png"><img loading="lazy" data-attachment-id="1304" data-permalink="https://fgiesen.wordpress.com/2013/01/29/write-combining-is-not-your-friend/wc_slow/" data-orig-file="https://fgiesen.files.wordpress.com/2013/01/wc_slow1.png" data-orig-size="497,300" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}" data-image-title="Reading from write-combined memory" data-image-description="" data-image-caption="" data-medium-file="https://fgiesen.files.wordpress.com/2013/01/wc_slow1.png?w=300" data-large-file="https://fgiesen.files.wordpress.com/2013/01/wc_slow1.png?w=497" src="https://fgiesen.files.wordpress.com/2013/01/wc_slow1.png?w=497&#038;h=300" alt="Reading from write-combined memory" width="497" height="300" class="aligncenter size-full wp-image-1304" srcset="https://fgiesen.files.wordpress.com/2013/01/wc_slow1.png 497w, https://fgiesen.files.wordpress.com/2013/01/wc_slow1.png?w=150&amp;h=91 150w, https://fgiesen.files.wordpress.com/2013/01/wc_slow1.png?w=300&amp;h=181 300w" sizes="(max-width: 497px) 100vw, 497px" /></a></p>
<p>If I had gone purely by what takes the largest amount of time, I&#8217;d have started with the depth buffer rasterization pass; as you should well recall, it took me several posts to explain what&#8217;s even going on in that code, and as you can see from the chart above, while we got a good win out of improving it (about 1.1ms total), doing so took lots of individual changes. Compare with what I <em>actually</em> worked on first &#8211; namely, the Write Combining issue, which gave us a 2.1ms win for a three-line change.</p>
<p>So what&#8217;s the secret? Don&#8217;t use a profile exclusively to look for hot spots. In particular, if your profile has the hot spots you expected (like the depth buffer rasterizer in this example), they&#8217;re not worth more than a quick check to see if there&#8217;s any obvious waste going on. What you really want to look for are <em>anomalies</em>: code that seems to be running into execution issues (like <code>SetRenderStates</code> with the read-back from write-combined memory running at over 9 cycles per instruction), or things that just shouldn&#8217;t take as much time as they seem to (like the frustum culling code we looked at for the next few posts). If used correctly, a profiler is a powerful tool not just for performance tuning, but also to find deeper underlying architectural issues.</p>
<h3>While you&#8217;re at it&#8230;</h3>
<p>Anyway, once you&#8217;ve picked a suitable target, I recommend that you do not just the necessary work to knock it out of the top 10 (or some other arbitrary cut-off). After &#8220;<a href="https://fgiesen.wordpress.com/2013/02/02/frustum-culling-turning-the-crank/">Frustum culling: turning the crank</a>&#8220;, a commenter asked why I would spend the extra time optimizing a function that was, at the time, only at the #10 spot in the profile. A perfectly valid question, but one I have three separate answers to:</p>
<p>First, the answer I gave in the comments at the time: code is not just isolated from everything else; it exists in a context. A lot of the time in optimizing code (or even just reading it, for that matter) is spent building up a mental model of what&#8217;s going on and how it relates to the rest of the system. The best time to make changes to code is while that mental model is still current; if you drop the topic and work somewhere else for a bit, you&#8217;ll have to redo at least part of that work again. So if you have ideas for further improvements while you&#8217;re working on code, that&#8217;s a good time to try them out (once you&#8217;ve finished your current task, anyway). If you run out of ideas, or if you notice you&#8217;re starting to micro-optimize where you really shouldn&#8217;t, then stop. But by all means continue while the going is good; even if you don&#8217;t need that code to be faster now, you might want it later.</p>
<p>Second, never mind the relative position. As you can see in the table above, the &#8220;advanced&#8221; frustum culling changes reduced the total frame time by about 0.4ms. That&#8217;s about as much as we got out of our first set of depth buffer rendering changes, even though it was much simpler work. Particularly for games, where you usually have a set frame rate target, you don&#8217;t particularly care where exactly you get the gains from; 0.3ms less is 0.3ms less, no matter whether it&#8217;s done by speeding up one of the Top 10 functions slightly or something else substantially!</p>
<p>Third, relating to my comment about looking for anomalies above: unless there&#8217;s a really stupid mistake somewhere, it&#8217;s fairly likely that the top 10, or top 20, or top whatever hot spots are actually code that does substantial work &#8211; certainly so for code that other people have already optimized. However, most people do tend to work on the hot spots first when looking to improve performance. My favorite sport when optimizing code is starting in the middle ranks: while everyone else is off banging their head against the hard problems, I will casually snipe at functions in the 0.05%-1.0% total run time range. This has two advantages: first, you can often get rid of a lot of these functions entirely. Even if it&#8217;s only 0.2% of your total time, if you manage to get rid of it, that&#8217;s 0.2% that are gone. It&#8217;s usually a lot easier to get rid of a 0.2% function than it is to squeeze an extra 2% out of a 10%-run time function that 10 people have already looked at. And second, the top hot spots are usually in leafy code. But down in the middle ranks is &#8220;middle management&#8221; &#8211; code that&#8217;s just passing data around, maybe with some minor reformatting. That&#8217;s your entry point to re-designing data flows: this is the code where subsystems meet &#8211; the place where restructuring will make a difference. When optimizing interfaces, it&#8217;s crucial to be working on the interfaces that actually have problems, and this is how you find them.</p>
<h3>Ground we&#8217;ve covered</h3>
<p>Throughout this series, my emphasis has been on changes that are fairly high-yield but have low impact in terms of how much disruption they cause. I also made no substantial algorithmic changes. That was fully intentional, but it might be surprising; after all, as any (good) text covering optimization will tell you, it&#8217;s much more important to get your algorithms right than it is to fine-tune your code. So why this bias?</p>
<p>Again, I did this for a reason: while algorithmic changes are indeed the ticket when you need large speed-ups, they&#8217;re also very context-sensitive. For example, instead of optimizing the frustum culling code the way I did &#8211; by making the code more SIMD- and cache-friendly &#8211; I could have just switched to a bounding volume hierarchy instead. And normally, I probably would have. But there&#8217;s plenty of material on bounding volume hierarchies out there, and I trust you to be able to find it yourself; by now, there&#8217;s also a good amount of Google-able material on &#8220;Data-oriented Design&#8221; (I dislike the term; much like &#8220;Object-oriented Design&#8221;, it means everything and nothing) and designing algorithms and data structures from scratch for good SIMD and cache efficiency.</p>
<p>But I found that there&#8217;s a distinct lack of material for the actual problem most of us actually face when optimizing: how do I make existing code faster without breaking it or rewriting it from scratch? So my point with this series is that there&#8217;s a lot you can accomplish purely using fairly local and incremental changes. And while the actual changes are specific to the code, the underlying ideas are very much universal, or at least I hope so. And I couldn&#8217;t resist throwing in some low-level architectural material too, which I hope will come in handy. 🙂</p>
<h3>Changes I intentionally did not make</h3>
<p>So finally, here&#8217;s a list of things I did <em>not</em> discuss in this series, because they were either too invasive, too tricky or changed the algorithms substantially:</p>
<ul>
<li><em>Changing the way the binner works</em>. We don&#8217;t need that much information per triangle, and currently we gather vertices both in the binner and the rasterizer, which is a fairly expensive step. I did implement a variant that writes out signed 16-bit coordinates and the set-up Z plane equation; it saves roughly another 0.1ms in the final rasterizer, but it&#8217;s a fairly invasive change. Code is <a href="https://github.com/rygorous/intel_occlusion_cull/tree/blog_past_the_end">here</a> for those who are interested. (I may end up posting other stuff to that branch later, hence the name).</li>
<li><em>A hierarchical rasterizer for the larger triangles</em>. Another thing I <a href="https://github.com/rygorous/intel_occlusion_cull/tree/hier_rast">implemented</a> (note this branch is based off a pre-blog version of the codebase) but did not feel like writing about because it took a lot of effort to deliver, ultimately, fairly little gain.</li>
<li><em>Other rasterizer techniques or tweaks</em>. I could have implemented a scanline rasterizer, or a different traversal strategy, or a dozen other things. I chose not to; I wanted to write an introduction to edge-function rasterizers, since they&#8217;re cool, simple to understand and less well-known than they should be, and this series gave me a good excuse. I did not, however, want to spend more time on actual rasterizer optimization than the two posts I wrote; it&#8217;s easy to spend years of your life on that kind of stuff (I&#8217;ve seen it happen!), but there&#8217;s a point to be made that this series was already too long, and I did not want to stretch it even further.</li>
<li><em>Directly rasterizing quads in the depth test rasterizer</em>. The depth test rasterizer only handles boxes, which are built from 6 quads. It&#8217;s possible to build an edge function rasterizer that directly traverses quads instead of triangles. Again, I wrote the code (not on Github this time) but decided against writing about it; while the basic idea is fairly simple, it turned out to be really ugly to make it work in a &#8220;drop-in&#8221; fashion with the rest of the code. See <a href="https://fgiesen.wordpress.com/2013/02/28/reshaping-dataflows/#comment-2466">this comment</a> and my reply for a few extra details.</li>
<li><em>Ray-trace the boxes in the test pass instead of rasterizing them</em>. Another suggestion by <a href="https://fgiesen.wordpress.com/2013/02/28/reshaping-dataflows/#comment-2466">Doug</a>. It&#8217;s a cool idea and I think it has potential, but I didn&#8217;t try it.</li>
<li><em>Render a lower-res depth buffer using very low-poly, conservative models</em>. This is how I&#8217;d actually use this technique for a game; I think bothering with a full-size depth buffer is just a waste of memory bandwidth and processing time, and we do spend a fair amount of our total time just transforming vertices too. Nor is there a big advantage to using the more detailed models for culling. That said, changing this would have required dedicated art for the low-poly occluders (which I didn&#8217;t want to do); it also would&#8217;ve violated my &#8220;no-big-changes&#8221; rule for this series. Both these changes are definitely worth looking into if you want to ship this in a game.</li>
<li><em>Try other occlusion culling techniques</em>. Out of the (already considerably bloated) scope of this series.</li>
</ul>
<p>And that&#8217;s it! I hope you had as much fun reading these posts as I did writing them. But for now, it&#8217;s back to your regularly scheduled, piece-meal blog fare, at least for the time being! Should I feel the urge to write another novella-sized series of posts again in the near future, I&#8217;ll be sure to let you all know by the point I&#8217;m, oh, nine posts in or so.</p>
]]></html><thumbnail_url><![CDATA[https://fgiesen.files.wordpress.com/2013/03/post_breakdown1.png?w=497&fit=440%2C330]]></thumbnail_url><thumbnail_width><![CDATA[]]></thumbnail_width><thumbnail_height><![CDATA[]]></thumbnail_height></oembed>