Check out our Monthly Survey Page to see what our users are running.
We do often include affiliate links to earn us some pennies. See more here.

Natural Selection 2's Direct3D HLSL to OpenGL GLSL Parser Open Sourced

By - | Views: 22,280
In another win for open-source the developers behind Natural Selection 2 have opened up the code they use to parse HLSL calls into OpenGL's GLSL.

It has been released under the MIT license, so do with it as you wish as long as the MIT license sticks around.

This is after the developers also opened up the original Natural Selection for nostalgia.

Notes
QuoteHLSL Parser and GLSL code generator

This is the code we used in Natural Selection 2 to convert HLSL shader code to GLSL for use with OpenGL. The code is pulled from a larger codebase and has some dependencies which are not included, so the code will not compile as-is. These dependencies are all very basic though (array classes, memory allocators, etc.) so replacing them should be simple if you want to use this code.

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.


I hope some other developers find it useful, it is interesting to see more than just Valve doing things like this. Who knows maybe it could even help Valve themselves.

Who knows, maybe some helpful developers can peek at NS2's HLSL->GLSL code on github and improve the OpenGL performance as it is quite the hog.

For those that don't know, Valve also does something similair using their own open-source library called ToGL. Although Valve's Source Engine 2 should be using OpenGL directly rather than some sort of translation layer.

It seems even Unreal Engine also uses something similar (seeing a pattern here!), I am sure it makes things easier, but performance wise it just isn't as good as calling OpenGL directly.

Link: https://github.com/unknownworlds/hlslparser Article taken from GamingOnLinux.com.
0 Likes
About the author -
author picture
I am the owner of GamingOnLinux. After discovering Linux back in the days of Mandrake in 2003, I constantly came back to check on the progress of Linux until Ubuntu appeared on the scene and it helped me to really love it. You can reach me easily by emailing GamingOnLinux directly. Find me on Mastodon.
See more from me
The comments on this article are closed.
8 comments

Snev Mar 28, 2014
This is a very nice initiative! Great!
.... Mar 28, 2014
some one needs to make a OpenGL>DX for Xbone so developers can no longer fear not being able to develop for Xbone
loggfreak Mar 28, 2014
Quoting: ....some one needs to make a OpenGL>DX for Xbone so developers can no longer fear not being able to develop for Xbone
why would anyone want to develop for overpriced underpowered xbone, a 450$ pc beats it in performance and then you haven't considdered the fact that you need to pay 50-60$ each year just to stay online, games are cheaper on pc, the xbone has no decent exclusive yet that can't be replaced by pc alternatves, and you get mods and stuff on pc, there's only 1 reason people buy that and that is fanboyism
DrMcCoy Mar 28, 2014
Relatedly, does anybody know of a working ARB shaders (that asm-like shader language) to GLSL converter? :P
Mambo Mar 28, 2014
Unity has a similar, open-source compiler (derived from mesa): https://github.com/aras-p/hlsl2glslfork . Unreal uses the same codebase, but they aren't publishing their proprietary branch at all.

Here's a good overview of cross-API shaders: http://aras-p.info/blog/2014/03/28/cross-platform-shaders-in-2014/
philip550c Mar 29, 2014
Quoting: ....some one needs to make a OpenGL>DX for Xbone so developers can no longer fear not being able to develop for Xbone
Does the Xbone not support opengl?
pd12 Mar 30, 2014
Quoting: liamdawe"Although Valve's Source Engine 2 should be using OpenGL directly rather than some sort of translation layer."
Hi Liam, do you have a source for this (or anyone else)?

Quoting: philip550c
Quoting: ....some one needs to make a OpenGL>DX for Xbone so developers can no longer fear not being able to develop for Xbone
Does the Xbone not support opengl?
Ha, M$ has been restricting their XBox platforms to use DX/D3D for quite some time now (like all of them), which is why a lot of developers used DX to start off with - they wanted to go on XBoxes. M$ obviously did this to try and lock people into their own proprietary rendering engine with D3D as opposed to OpenGL (and similarly DX vs SDL).
pd12 Mar 31, 2014
Found it. Thanks! =) Yeah, I remember hearing that somewhere before, a lot of the Steam Dev Days videos are really interesting! I watched quite a few of them.
They say their engine still has D3D centric stuff from its heritage but they'd like to move away from that (see the slide as well).
http://youtu.be/45O7WTc6k2Y?t=6m50s
View video on youtube.com
goto 6m50s.

Video in full playlist:
https://www.youtube.com/watch?v=45O7WTc6k2Y&list=PLckFgM6dUP2hc4iy-IdKFtqR9TeZWMPjm&index=10
While you're here, please consider supporting GamingOnLinux on:

Reward Tiers: Patreon. Plain Donations: PayPal.

This ensures all of our main content remains totally free for everyone! Patreon supporters can also remove all adverts and sponsors! Supporting us helps bring good, fresh content. Without your continued support, we simply could not continue!

You can find even more ways to support us on this dedicated page any time. If you already are, thank you!
The comments on this article are closed.