Question

Is there a way to combine shaders?

Let's say i want to have 4 models :

  • One with just the texture rendered.
  • One with the texture and a basic light effect.
  • One rendered with skeletal animation.
  • One rendered with skeletal animation and basic light effect.

The only way i know to do that, is to create 4 pixel and vertex shaders files. But is i have 5 more effects that i want to add, i must created more files, and if i want to add more and more effects? I doubt that for each effect i have to create one more file for each possibility. I know that for the light i can set it directly in the other files, and when i dont want to use light, i set it to 1.0f so the color dont change. But if i want to have more effects, i will have like 20 variables in my shader, and isn't that useless if i dont use them? Is there another way to do that?

Thanks in advance, i tried to be as understandable as i could, sorry for my english.

Was it helpful?

Solution

Here are some thoughts:

  • Move common functionality to separate file and use #include to spread it between shaders.
  • Use #ifdef for conditional compilation. Pass defines via /D flag of fxc.exe of D3D_SHADER_MACRO of D3DCompile. You can combine all in one shader and then compile it into many different binaries.
  • Use some effect framework (default DirectX one, googled or make your own): you will not need separate files for shaders, all goes in one file, so as states.
  • Leave all as is: HLSL is complicated and any more complications just can lead to unmaintainable mess.

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top