Modular Shaders

Started by
1 comment, last by xerzi 12 years, 2 months ago
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.
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.
Ah, thank you for clearing that up and for the suggestion, I'll look into both methods.

This topic is closed to new replies.

Advertisement