Jump to content
  • Advertisement
Sign in to follow this  
xerzi

Modular Shaders

This topic is 2496 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
Advertisement
Unreal uses shader trees, 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:

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


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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!