Want more features and no adverts?
Subscribe to GOL Premium
Natural Selection 2's Direct3D HLSL To OpenGL GLSL Parser Open Sourced
Posted , 28 March 2014 at 9:49 am UTC / 9617 views
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

I am the owner of GamingOnLinux. A fan of anything techy, and not just Linux stuff.

You can follow my personal blog here.

Snev commented on 28 March 2014 at 10:48 am UTC

This is a very nice initiative! Great!

This is a very nice initiative! Great!

.... commented on 28 March 2014 at 10:56 am UTC

some one needs to make a OpenGL>DX for Xbone so developers can no longer fear not being able to develop for Xbone

some one needs to make a OpenGL>DX for Xbone so developers can no longer fear not being able to develop for Xbone

mirv commented on 28 March 2014 at 11:05 am UTC
  • GOL Supporter

It's good seeing all these hlsl->glsl converters popping up. Writing glsl from the start would of course be preferred, but this lowers the barriers of porting existing games and helps those not familiar with OpenGL.
So, basically, awesome.

It's good seeing all these hlsl->glsl converters popping up. Writing glsl from the start would of course be preferred, but this lowers the barriers of porting existing games and helps those not familiar with OpenGL. So, basically, awesome.

loggfreak commented on 28 March 2014 at 11:14 am UTC

....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

[quote=....]some one needs to make a OpenGL>DX for Xbone so developers can no longer fear not being able to develop for Xbone[/quote] 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 commented on 28 March 2014 at 11:38 am UTC

Relatedly, does anybody know of a working ARB shaders (that asm-like shader language) to GLSL converter?

Relatedly, does anybody know of a working ARB shaders (that asm-like shader language) to GLSL converter? :P

Mambo commented on 28 March 2014 at 5:21 pm UTC

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/

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 commented on 29 March 2014 at 1:14 am UTC

....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?

[quote=....]some one needs to make a OpenGL>DX for Xbone so developers can no longer fear not being able to develop for Xbone[/quote] Does the Xbone not support opengl?

pd12 commented on 30 March 2014 at 8:11 am UTC

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)?

philip550c
....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).

[quote=liamdawe]"Although Valve's Source Engine 2 should be using OpenGL directly rather than some sort of translation layer."[/quote] Hi Liam, do you have a source for this (or anyone else)? [quote=philip550c][quote=....]some one needs to make a OpenGL>DX for Xbone so developers can no longer fear not being able to develop for Xbone [/quote] Does the Xbone not support opengl?[/quote] 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).

mirv commented on 30 March 2014 at 10:00 am UTC
  • GOL Supporter

It was mentioned on one of the Steam Dev Days talks that Source 2 would be using native OpenGL - or at least could use it as a rendering backend. Forget the exact talk.

It was mentioned on one of the Steam Dev Days talks that Source 2 would be using native OpenGL - or at least could use it as a rendering backend. Forget the exact talk.

  Go to:

Due to spam you need to Register and Login to comment.


Or login with...

Sales Tracker
Popular this week
View by Category
Contact
Twitter
Latest Forum Posts
Facebook
Who's Online
There are 195 in total online.
Users online: manu2oh9
Misc