An open (BSD) shader compiler front end / translator

FoxMcCloud

Newcomer
I've been running into issues with using Cg on non-NVIDIA platforms and often frustrated by some bugs and slow turnaround on cgc's GLSL output target. I've been working on writing a front-end for a Cg-like shading language (named LtiteGraph Shader Language since it came out of a toy project called LiteGraph) as a side project for a while, and it's beginning to get to the point where it can perform basic translation to GLSL using a bunch of simple pre-order / post-order passes on the generated parse tree.

Here's what a basic vertex program looks like:

http://www.flickr.com/photos/59098813@N06/6648763241/

And here's its parse graph inside a simple OS X app I wrote to visualize it:

http://www.flickr.com/photos/59098813@N06/6648763251/

The parser is in standards-compliant C++ code, currently runs on Windows and OS X, doesn't share global state (so you can parse more than one shader in parallel inside the same process) and I plan to release it under the BSD license if anyone's interested. The code isn't optimized and currently it takes an average of ~250ms to parse every shader in the corpus I have of shaders from The Cg Book (~40).

The parser can also serialize the parse graph to readable YAML, and I've been using the serialized parse graphs to experiment with writing passes quickly in Python, so I have serialization / deserialization code for C++ (using yaml-cpp) and Python (using PyYAML). I plan to move the code I write in Python to C++ eventually - I merely use Python for the fast iteration speed.

Currently a half-dozen passes (200 LOC total) in Python can do basic conversion to GLSL, and I think that writing a complete translator should be doable in six to twelve man-months (but don't hold me to that). Here's what the input / output look like for a simple shader:

http://pastebin.com/VxQn5tcT

A corpus of shaders in Cg and their LiteGraph equivalents with serialized parse graphs can be found here: https://bitbucket.org/sherief/cg-lgsl-shaders/overview

Is anyone else interested in this? I'm thinking of releasing the code under a BSD license since I think other people might find an embeddable, open shader language front-end might make their targeting of multiple platforms easier, but I'd love to hear what you guys have to say first.
 
I'm pretty sure that Unity still uses Cg, which means a lot of people are using Cg. :smile:
 
Forgive the noobishness of the question but your program takes your shading languade and converts it to cg ?
isnt that a bit like speaking english, wanting to speak german, so learning french and then using babelfish to convert french to german ?
If i'm going to have to learn LtiteGraph Shader Language why not learn cg instead ?
 
If i'm going to have to learn LtiteGraph Shader Language why not learn cg instead ?

The differences between Cg and LGSL can be printed on an A4 paper. In its current state though there's no reason to learn it or use it. It's be useful only if it has a usable, open (commercial friendly) translator / backend, since problems like this [ http://forum-archive.developer.nvidia.com/index.php?showtopic=6025 ] won't have you blocked until the compiler vendor decides to fix them, if ever. If it has an open, easy-to-tinker-with translator to GLSL it'd be useful for custom-tailoring it to platforms like OS X.

Again, in it's current state it's pretty much useless. If (Big Fat If) it has working translators it'd be worth using so as to support running the same code on GL ES, GL 3.x, DX, etc.

If you're into that kind of thing you can also use the annotated parse tree to do some code analysis and print out diagnostics, since each node keeps track of its start / end address in line numbers and byte offsets.
 
I didn't know anyone was still using Cg. Why do you use it over GLSL or DX HLSL?
I'd assume legacy reasons. For a long time Cg was the only de-facto cross-platform HL shading language, so a shader codebase has been accumulated. Also, it's still the only choice on platforms which for one reason or another are limited to ARB_program. And then there's PS3 ; )
 
Back
Top