tgjones / HlslTools

A Visual Studio extension that provides enhanced support for editing High Level Shading Language (HLSL) files
http://timjones.io/blog/archive/2016/04/25/hlsl-tools-for-visual-studio-v1.0-released
Other
565 stars 97 forks source link

Can this tool be used to programatically generate shader code? #253

Open T3sT3ro opened 1 year ago

T3sT3ro commented 1 year ago

I see that it is similar to Roslyn, but when I inspect SyntaxFactory in search for creational methods, I see only Parse ones. Does it mean, that I can't use a subset of the libraries to transform and generate shaderlab/hlsl code?

Maybe I'm blind and look in bad places, but I would most certainly like to avoid writing it from scratch by myself if there is such a good framework like yours already present, with lots of things already implemented.

I am especially interested in using a subset of this library for generating and transforming code in Unity, i.e. programatically generate syntax trees without parsing anything.

I see it also depends internally on things like SourceText and libs like XUnit, so to make it work in unity I would have to manually remove and replace some code, no?

tgjones commented 1 year ago

It probably would require changes to the HlslTools source code to support the usage you have in mind, yes. I'd be open to PRs for such a thing though. There's a source generator that generates quite a lot of code from the Syntax.xml file, and it could be modified to also generate creational methods.

T3sT3ro commented 1 year ago

Ok, thanks. Could you maybe include some instructions on how to build and generate source from the Syntax.xml under linux? I am not that well versed in the windows built ecosystem and it's project structure, so instructions that could point in some good direction would be much appreciated.

mrvux commented 12 months ago

It is definitely be possible to generate/transform syntax trees (I already worked on a hlsl minifier that transformed existing syntax trees then re-generate hlsl after (which amazingly is basically just call ToString() on the root once you done your modifications.

If I remember I had to fork and promote a few methods from protected/internal to public in order for it to work, I used a rather old version so I should check with the latest one.

For generation or some cases of modifications, the only thing that will be rather tricky in the current state is includes (in my case I needed to resolve them so I got that feature for free, but if you need to preserve them it's definitely gonna be harder).

Since I'm gonna actually rework heavily on modified syntax tree in the near future (basically extending the language set) I'd be glad to help if needed.

EDIT : So I had a go at trying to generate a syntax tree and exporting as hlsl string, it's not completely trivial (since HLSL tools is designed for parse + editing, not for parsing an already compiled shader), but that is definitely doable.

Some constructors have to be promoted to public (SyntaxToken is the first one, then SeparatedSyntaxList there is certainly many more), also source ranges are not initialized but in this small use case it is unimportant).

It works but there is definitely a lot of boilerplate (I suppose a bunch of extension methods or utility library would do some wonders still).