Site Logo

Pixel-in-Gene

Exploring Frontend Development with Design / Graphics / Technology

Don’t forget to pass the Input brush to the Shader

When writing ShaderEffects for WPF, it is always tempting to first test the shader with images. Testing with images instinctively seems easy but there are some caveats to remember. When the ShaderEffect is applied to an Image, the texture information is directly pushed to the sampler register s0. So there is no need to setup a DependencyProperty of type Brush using ShaderEffect.RegisterPixelShaderSamplerProperty(). You would do this with the following code:

public static readonly DependencyProperty InputProperty =
            ShaderEffect.RegisterPixelShaderSamplerProperty("Input",
                    typeof(WarpEffect), 0);

public Brush Input
{
    get { return (Brush)GetValue(InputProperty); }
    set { SetValue(InputProperty, value); }
}

and also call UpdateShaderValue(InputProperty) in the ShaderEffect’s constructor.

If you forget to do the above, you will have blank output when applying the ShaderEffect to regular UIElements. I found this out the hard way: losing a patch of hair!

Don’t forget to pass the Input brush to the Shader
Pavan Podila
Pavan Podila
July 28th, 2008