Using an Effect
This example demonstrates how to use an effect technique that was loaded from a file.
Add effects code to your rendering method, such as OnRender(), in-between calls to Device.BeginScene and Device.EndScene.
- Create or obtain access to a high-level shader language (HLSL) file (.fx).
- Load the HLSL file using Effect.FromFile.
- Set Effect.Technique with the effect's technique.
- Use Effect.Begin to obtain the number of passes for the effect.
- Create a loop to iterate through all of the effect's passes. Rendering an effect occurs within a loop between calls to Effect.BeginPass and Effect.EndPass. The loop itself is nested between calls to Effect.Begin and Effect.End.
- Render each pass within the loop with calls to Effect.BeginPass, Device.DrawPrimitives, and Effect.EndPass.
In the following C# code example, device is assumed to be the rendering Device.
[C#]
public void OnRender ()
{
.
.
.
// Load the effect from file.
Effect effect = Effect.FromFile(device, "shadercode.fx", null,
ShaderFlags.None, null);
// Set the technique.
effect.Technique = "ShaderTechnique";
// Note: Effect.Begin returns the number of
// passes required to render the effect.
int passes = effect.Begin(0);
// Loop through all of the effect's passes.
for (int i = 0; i < passes; i++)
{
// Set a shader constant
effect.SetValue("WorldMatrix", worldMatrix);
// Set state for the current effect pass.
effect.BeginPass(i);
// Render some primitives.
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
// End the effect pass
effect.EndPass();
}
// Must call Effect.End to signal the end of the technique.
effect.End();
.
.
.
}