Shader #Defines/ Key Words

Hi all, is it possible to set “Shader defines” or Keywords as they are called in Unity?
Would be REALLY helpful if I could do this, as I can then have code for both forward and deferred rendering in the one fx file.
If you don’t know quite what I mean, this post on Unity show what I am trying to do.

Thanks in advance :slight_smile:

Yes, 2MGFX - the tool MG maintains for compiling shaders - runs a preprocessor and supports custom symbols for conditional compilation. Check the docs for the syntax (I’m on mobile and don’t know by heart).
The built-in shaders also use these symbols to check if it they are compiled for DX or for GL.

Hi @Jjagg,

Thanks for the reply :slight_smile:

Yea, thinking about it, I am not sure I am going to be able to do what I want due to the framework. I was hoping to be able to set them dynamically at run-time so I could switch from forward rendering to deferred by using #if Deferred {} #endif and then switching vertex and pizel shaders…

To do that would need a re compile of the shader at run-time I guess… Oh well, was just a thought :slight_smile:

Just include both versions precompiled. If the shaders are very much alike but differ in details you can use preprocessor directives. If they’re very different you can just write separate techniques in the same effect and switch between them at runtime. Techniques is what you want if you switch pixel or vertex shaders like you mention. I don’t have time to explain in detail atm, but ask if you need help and I’ll help out later :slight_smile:

No, I know exactly what you mean :slight_smile:

And I can do that, but just would have been nice to have been able to set the defines at run time, I was going to break it up by technique anyway,

Thanks again,

Charles :slight_smile:

setting defines at runtime?
That’s simple shader inputs then.

Defines tell the compiler what to compile, so changing that during runtime only works if you hot-reload your shaders.

I think this shader editor does just that, if you want to have something to start from for hot-reloading ->

If you want to compile shaders for an editor (:wink:) like it is done in UE4, there is a lot of work waiting for you

This thread about runtime modification touches on how to do it and why things are the way they are.

It was pretty trivial to get working, though I wasn’t concerned about hot-swapping when I followed this recently, so I got to bypass most of the issues.

Yea, think I am not going to go with hot swapping, just would have been nice to be able to have a camera running deferred render, and another (or others) running forward render and not having to have each object worry about picking the right/alternative shader/material for the render type.