Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
T3sT3ro opened this issue Jul 20, 2023 · 3 comments
Open

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

T3sT3ro opened this issue Jul 20, 2023 · 3 comments

Comments

@T3sT3ro
Copy link

T3sT3ro commented Jul 20, 2023

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
Copy link
Owner

tgjones commented Jul 30, 2023

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
Copy link
Author

T3sT3ro commented Jul 30, 2023

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
Copy link
Contributor

mrvux commented Oct 11, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants