Effect language

Create shaders in C

You can create a shader at runtime with ShaderSource objects. Shaders come in three types:

  • ShaderClassSource correspond to a unique shader class
  • ShaderMixinSource mix several ShaderSource, set preprocessor values, define compositions
  • ShaderArraySource are used for arrays of compositionsThis method produces shaders at runtime. However, many platforms don't support HLSL and have no ability to compile shaders at runtime. Additionally, the approach doesn't benefit from the reusability of mixins.

Xenko Effects (XKFX)

Many shaders are variations or combinations of pre-existing shaders. For example, some meshes cast shadows, others receive them, and others need skinning. To reuse code, it's a good idea to select which parts to use through conditions (eg "Skinning required"). This is often solved by "uber shaders": monolithic shaders configured by a set of preprocessor parameters.

Xenko offers the same kind of control, keeping extensibility and reusability in mind. The simple code blocks defined by shader classes can be mixed together by a shader mixer. This process can use more complex logic, described in Xenko Effect (*.xkfx) files.

General syntax

An .xkfx file is a small program used to generate shader permutations. It takes a set of parameters (key and value in a collection) and produces a ShaderMixinSource ready to be compiled.

An example .xkfx file:

  1. using Xenko.Effects.Data;
  2. namespace XenkoEffects
  3. {
  4. params MyParameters
  5. {
  6. bool EnableSpecular = true;
  7. };
  8. effect BasicEffect
  9. {
  10. using params MaterialParameters;
  11. using params MyParameters;
  12. mixin ShaderBase;
  13. mixin TransformationWAndVP;
  14. mixin NormalVSStream;
  15. mixin PositionVSStream;
  16. mixin BRDFDiffuseBase;
  17. mixin BRDFSpecularBase;
  18. mixin LightMultiDirectionalShadingPerPixel<2>;
  19. mixin TransparentShading;
  20. mixin DiscardTransparent;
  21. if (MaterialParameters.AlbedoDiffuse != null)
  22. {
  23. mixin compose DiffuseColor = ComputeBRDFDiffuseLambert;
  24. mixin compose albedoDiffuse = MaterialParameters.AlbedoDiffuse;
  25. }
  26. if (MaterialParameters.AlbedoSpecular != null)
  27. {
  28. mixin compose SpecularColor = ComputeBRDFColorSpecularBlinnPhong;
  29. mixin compose albedoSpecular = MaterialParameters.AlbedoSpecular;
  30. }
  31. };
  32. }

Add a mixin

To add a mixin, use mixin <mixin_name>.

Use parameters

The syntax is similar to C#. The following rules are added:

  • When you use parameter keys, add them using params <shader_name>. If you don't, keys are treated as variables.

  • You don't need to tell the program where to check the values behind the keys. Just use the key.

  1. using params MaterialParameters;
  2. if (MaterialParameters.AlbedoDiffuse != null)
  3. {
  4. mixin MaterialParameters.AlbedoDiffuse;
  5. }

The parameters behave like any variable. You can read and write their value, compare their values, and set template parameters. Since some parameters store mixins, they can be used for composition and inheritance, too.

Custom parameters

You can create your own set of parameters using a structure definition syntax.

Note

Even if they're defined in the XKFX file, don't forget the using statement when you want to use them.

  1. params MyParameters
  2. {
  3. bool EnableSpecular = true; // true is the default value
  4. }

Compositions

To add a composition, assign the composition variable to your mixin with the syntax below.

  1. // albedoSpecular is the name of the composition variable in the mixin
  2. mixin compose albedoSpecular = ComputeColorTexture;
  3. or
  4. mixin compose albedoSpecular = MaterialParameters.AlbedoSpecular;

Partial effects

You can also break the code into sub-mixins to reuse elsewhere with the syntax below.

  1. partial effect MyPartialEffect
  2. {
  3. mixin ComputeColorMultiply;
  4. mixin compose color1 = ComputeColorStream;
  5. mixin compose color2 = ComputeColorFixed;
  6. }
  7. // to use it
  8. mixin MyPartialEffect;
  9. mixin compose myComposition = MyPartialEffect;

You can use the MyPartialEffect mixin like any other mixin in the code.

See also