Sign in to follow this  

Modular Shaders

This topic is 2127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

From what I've seen of the Unreal Engine 3, it's shader system is very dynamic. I was wondering if anyone has any theories to how they created something like this. It seems like it would take a lot of work to create an efficient system like this. From what I've searched it seems that the program used to create the shaders generates code for each shader you create and than compiles it for use.

Share this post


Link to post
Share on other sites
Unreal uses [url="http://graphics.cs.brown.edu/http://graphics.cs.brown.edu/games/AbstractShadeTrees/"]shader trees[/url], which allows visual creation of shaders but also makes generation pretty complex. To do that you essentially need to split up your shader code into feature blocks with a set of inputs and outputs, and then you have your visual editor where you place blocks and connect the inputs and outputs between those blocks.Then you need a backend that generates the final shader by emiting the code for the feature blocks, as well as glue code for linking together inputs and outputs.

A lot of people prefer to go for something simpler, such as writing an "ubershader" where features can be enabled or disabled with preprocessor macros. So you'd have something like this:
[code]
float3 normal = input.Normal;
#if EnableNormalMapping_
float3 tangent = input.Tangent;
float3 bitangent = input.Bitangent;
float3 normalMapSample = NormalMap.Sample(TexSampler, input.UV).xyz * 2.0f - 1.0f;
normal = mul(normalMapSample, float3(tangent, bitangent, normal);
#endif
[/code]

You can then control which shader gets used at runtime by specifying integer where each bit in the integer specifies whether a feature is enabled or disabled.

Share this post


Link to post
Share on other sites

This topic is 2127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this