"Ubershader" is just a name for a general pattern for authoring shader permutations. Typically you will write one large vertex and pixel shader with all possible features implemented, and then you will wrap the corresponding code for a certain features in "#ifdef" preprocessor statements. This allows you to compile different permutations of your shader with various features enabled or disabled by passing different macro definitions when compiling the shader. Here's a really simple example:
#ifdef ENABLE_COLOR_TEXTURE
Texture2D ColorTexture;
SamplerSTate ColorTextureSampler;
#endif
struct PSInput
{
float4 VertexColor : VTXCOLOR;
#ifdef ENABLE_COLOR_TEXTURE
float2 UV : UV0;
#endif
};
float4 PSMain(in PSInput input) : SV_Target0
{
float4 color = input.VertexColor;
#ifdef ENABLE_COLOR_TEXTURE
color *= ColorTexture.Sample(ColorTextureSampler, input.UV);
#endif
return color;
}
This is pixel shader for an ubershader that has 2 permutations: one with a color texture enabled, and one without. So you would compile one where you'd specify that you want ENABLE_COLOR_TEXTURE defined as an extra preprocessor macro, and one where you wouldn't. Then you can switch between the two at runtime depending on what you need for a particular mesh. Or alternatively you can have your features be options when authoring a material, and then you can specifically compile a shader permutation for that material. Typically you'll have lots of options that you want to disable, such as lighting, normal mapping, skinning, etc. Supporting all possible permutations of a bunch of on/off options then requires you to compile 2^N shaders, where N is the number of options you want to support. A common way index into all of these shaders is with a bitfield, where each the value of each bit corresponds to a feature being turned on or off. This gives you a simple hash that you can use to lookup into std::map or a similar data structure.
You can also support enabling or disabling features by wrapping them in regular if statements, and passing a bool through a constant buffer to specify whether you want them on or off. This saves you from having to compile different shaders and switch between them at runtime, which can save you build time and can also potentially save you some CPU time due to driver overhead from switching shaders. However in generally it will result in less optimal compiled shader code, since the compiler will be unable to fully optimize out the code in a branch that's not taken (or any code that produces results required for the branch not taken). This can result in extra instructions being executed, and potentially higher register usage which reduces thread occupancy. There's also some cost to actually executing a branch instruction, although this is generally small on modern DX11-capable hardware. The one major limitation of branches is that you can't use them to disable vertex shader inputs. Here's a simple example showing what I mean:
cbuffer VSConstants
{
float4x4 World;
float4x4 WorldViewProj;
}
#ifdef ENABLE_SKINNING
static const uint MaxBones = 256;
cbuffer SkinningConstants
{
float4x4 Bones[MaxBones];
}
struct VSInput
{
float3 Position : POSITION;
float3 Normal : NORMAL;
#ifdef ENABLE_SKINNING
uint4 SkinIndices : SKININDICES;
float4 SkinWeights : SKINWEIGHTS;
#endif
};
struct VSOutput
{
float4 Position : SV_Position;
float3 Normal : NORMAL;
};
VSOutput VSMain(in VSInput input)
{
VSOutput output;
#ifdef ENABLE_SKINNING
float3 position = 0.0f;
float3 normal = 0.0f;
[unroll]
for(uint i = 0; i < 4; ++i)
{
float4x4 bone = Bones[input.SkinIndices[i]];
float weight = input.SkinWeights[i];
position += mul(float4(input.Position, 1.0f), bone).xyz * weight;
normal = mul(float4(normal, 0.0f), bone).xyz * weight;
}
#else
float3 position = input.Position;
float3 normal = input.Normal;
#endif
output.Position = mul(float4(position, 1.0f), WorldViewProj).xyz;
output.Normal = mul(float4(normal, 0.0f), World).xyz;
return output;
}
If you were to try to implement this feature using runtime branching instead of preprocessor macros, you'd have the problem that you wouldn't be able to disable the SkinWeights and SkinIndices vertex inputs. This means that even when skinning is disabled the shader would still expect those elements to be provided in a vertex buffer, so you'd either need to pad out your vertex buffers or pass dummy vertex buffers as a separate stream. It also means that the shader will expect the "Bones" constant buffer to be bound, which means you'll get spammed with warnings from the debug device (although the shader will still work fine as long as you don't actually use anything from that constant buffer).