yadda
yadda

Reputation: 53

How to create multiple shader effect instances?

I've created custom shader effect that looks like this:


class MyShaderEffect : ShaderEffect
{
    private PixelShader _pixelShader = new PixelShader();
    public readonly DependencyProperty InputProperty =
        ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(MyShaderEffect), 0);

    public MyShaderEffect()
    {
        _pixelShader.UriSource = new Uri("MyShader.ps", UriKind.Relative);
        this.PixelShader = _pixelShader;
        this.UpdateShaderValue(InputProperty);
    }

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

I need to apply a slightly different variations of this shader effect (it have some parameters) to a different images, but when I try to create a second MyShaderEffect object, I get "'Input' property was already registered" exception.

Is there a way around this, so I can create multiple ShaderEffect instances from a single shader?

Upvotes: 1

Views: 263

Answers (1)

Eli Arbel
Eli Arbel

Reputation: 22739

Dependency Properties should be registered with static fields, so registration only happens once per type:

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

This field is just the identifier of the property. It's a key that is used to access the property values using GetValue and SetValue. The Input` property itself is not static.

Upvotes: 2

Related Questions