Tuesday, March 11, 2014

togl D3D9->OpenGL layer source release on github

This is a raw dump of the togl layer right from DoTA2:

This is old news by now; I think the press picked up on this even before I heard it was finally released. I really wish we had the time to package it better (so you could actually compile it!) with some examples, etc. There's a ton of practical Linux GL driver know-how packed all over this code -- if you look carefully. Every Valve Source1 title ultimately goes through this layer on Linux/Mac. (The Mac ports do use a different, much earlier branch, however. At some point the Linux and Mac branches merged back together, but I don't know if that occurred in time for DoTA's version.)

We talked a lot about what we learned while working on this layer at GDC last year:

Porting Source to Linux: Valve's Lessons Learned

Or here:

There's a lot of history to this particular code. This layer was first started by the Mac team, then later ported from Mac to Linux by the Steam team, and then finally ported by the Linux team to Windows (!) so we could actually debug it. (Because the best available GL debuggers at the time were Windows-only. We are working to correct that with vogl.) John McDonald, Rick Johnson, Mike Sartain, Pierre-Loup Griffais and I then got our hands on it (at various times) to push it down the correctness (for Source1) and performance axes. I spent many months agonizing over this layer's per-batch flush path: tweaking, profiling (with Rad's awesome Telemetry tool), optimizing, and testing it to run the Source1 engine correctly, quickly, and reliably on the drivers available for Linux.

The code is far from perfect: many parts are more like a battleground in there. It's optimized for results, and the primary metrics for success were perf vs. Windows and Source1 correctness, sometimes to the detriment of other factors. A lot of experiments were conducted, some blind alleys were backed out of, and we learned *a lot* about the true state of OpenGL drivers during the effort. If you want to see how to stay in the "fast lanes" of multiple Linux GL drivers simultaneously it might be worth checking out. (Most of the Linux drivers share common codebases with the Windows GL drivers, so a lot of what's in there is relevant to Windows GL too.)

(The first version of this post stated there was another version of togl that supported both Mac and Linux, and had all the SM3 fixes I made for various projects. Turns out the version on github is the very latest version, because all the togl branches were merged back into Dota2 at some point.)


  1. We would love the branch that also works on OSX.

    This leaves me with a few questions though.

    A: Why wasn't that (objectively superior?) branch released instead in the first place?
    B: Will that other branch eventually make it's way back into Dota2? If not, why not?

  2. A: The Dota2 version was a good choice because it's the top played title on Steam, and it's been hammered on by customers and driver devs quite a bit. The latest in-progress togl contains a bunch of new code (in the shader translator, and the Mac changes) that has yet to be released, so it hasn't been as well tested.

    B: I'm not involved in Dota2, so I can't specifically answer your question. In many cases the fixes or tweaks to the togl layer are game-specific enough that these changes are not always backported to other titles (especially already released ones) unless they address a critical problem. There's always the risk of breaking something even while merging over fixes, and testing games is surprisingly hard.

    When starting a new port we try to merge all the fixes into a single new version which is then hammered on over time to work out any issues. We've got a new drop of togl in progress that contains almost all the fixes from our various efforts (and updated Mac support) that I would like to see released, but I'll need to check in on the exact status of it.

  3. This comment has been removed by a blog administrator.