The developers behind Natural Selection 2, Unknown Worlds Entertainment, have released their translation layers that they use to parse HLSL into OpenGL’s GLSL. They are using the MIT license so anyone is free to do what ever they want with the code, as long as it adheres to the license.
The code, which is available on github, isn’t compile-able out of the box is missing some basic dependencies like memory allocators, array classes etc, which shouldn’t be much of a trouble for people experienced in programming. Maybe some enterprising programmer or a group of them can pick it up and include these and maybe even wrap it in an easy to use package that can be easily used in other games, thus streamlining the porting process and overall decrease the time required.
The parser is designed to work with HLSL code written in the legacy Direct3D 9 style (e.g. D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY should be used with D3D11). The parser works with cbuffers for uniforms, so in addition to generating GLSL, there is a class provided for generating D3D9-compatible HLSL which doesn’t support cbuffers. The GLSL code requires version 3.1 for support of uniform blocks. The parser is designed to catch all errors and generate “clean” GLSL which can then be compiled without any errors.
The HLSL parsing is done though a basic recursive descent parser coded by hand rather than using a parser generator. We believe makes the code easier to understand and work with.
To get consistent results from Direct3D and OpenGL, our engine renders in OpenGL “upside down”. This is automatically added into the generated GLSL vertex shaders.
Although this code was written specifically for our use, we hope that it may be useful as an educational tool or a base for someone who wants to do something similar.