.NET Framework Class Library
ShaderEffect Class

Updated: July 2008

Provides a custom bitmap effect by using a PixelShader.

Namespace:  System.Windows.Media.Effects
Assembly:  PresentationCore (in PresentationCore.dll)
Syntax

Visual Basic (Declaration)
Public MustInherit Class ShaderEffect _
    Inherits Effect
Visual Basic (Usage)
Dim instance As ShaderEffect
C#
public abstract class ShaderEffect : Effect
Visual C++
public ref class ShaderEffect abstract : public Effect
JScript
public abstract class ShaderEffect extends Effect
XAML
This class is abstract; see Inheritance Hierarchy for derived non-abstract classes usable in XAML.
Remarks

Derive from the ShaderEffect class to implement a custom effect based on a single pixel shader.

The following steps show how to create a custom effect.

  1. Load a PixelShader from precompiled High Level Shading Language (HLSL) bytecode.

  2. Define dependency properties that represent the parameters of the effect and the Brush-based surface inputs. Use one of the RegisterPixelShaderSamplerProperty overloads to associate these inputs with register numbers that are referenced in the HLSL bytecode.

Examples

The following code example shows how to derive from the ShaderEffect class.

C#
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Reflection;

namespace ShaderEffectDemo
{

    public class ThresholdEffect : ShaderEffect
    {
        private static PixelShader _pixelShader =
            new PixelShader() { UriSource = MakePackUri("ThresholdEffect.fx.ps") };

        public ThresholdEffect()
        {
            PixelShader = _pixelShader;

            UpdateShaderValue(InputProperty);
            UpdateShaderValue(ThresholdProperty);
            UpdateShaderValue(BlankColorProperty);
        }

        // MakePackUri is a utility method for computing a pack uri
        // for the given resource. 
        public static Uri MakePackUri(string relativeFile)
        {
            Assembly a = typeof(ThresholdEffect).Assembly;

            // Extract the short name.
            string assemblyShortName = a.ToString().Split(',')[0];

            string uriString = "pack://application:,,,/" +
                assemblyShortName +
                ";component/" +
                relativeFile;

            return new Uri(uriString);
        }

        ///////////////////////////////////////////////////////////////////////
        #region Input dependency property

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

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

        #endregion

        ///////////////////////////////////////////////////////////////////////
        #region Threshold dependency property

        public double Threshold
        {
            get { return (double)GetValue(ThresholdProperty); }
            set { SetValue(ThresholdProperty, value); }
        }

        public static readonly DependencyProperty ThresholdProperty =
            DependencyProperty.Register("Threshold", typeof(double), typeof(ThresholdEffect),
                    new UIPropertyMetadata(0.5, PixelShaderConstantCallback(0)));

        #endregion

        ///////////////////////////////////////////////////////////////////////
        #region BlankColor dependency property

        public Color BlankColor
        {
            get { return (Color)GetValue(BlankColorProperty); }
            set { SetValue(BlankColorProperty, value); }
        }

        public static readonly DependencyProperty BlankColorProperty =
            DependencyProperty.Register("BlankColor", typeof(Color), typeof(ThresholdEffect),
                    new UIPropertyMetadata(Colors.Transparent, PixelShaderConstantCallback(1)));

        #endregion
    }
}

The following code example shows a shader that corresponds to the previous ShaderEffect class.

C#
// Threshold shader 

// Object Declarations

sampler2D implicitInput : register(s0);
float threshold : register(c0);
float4 blankColor : register(c1);

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 color = tex2D(implicitInput, uv);
    float intensity = (color.r + color.g + color.b) / 3;

    float4 result;
    if (intensity > threshold)
    {
        result = color;
    }
    else
    {
        result = blankColor;
    }

    return result;
}

The following XAML shows how to use the custom shader effect.

XAML
<Window x:Class="ShaderEffectDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ShaderEffectDemo"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <local:ThresholdEffect x:Key="thresholdEffect" Threshold="0.25" BlankColor="Orange" />
    </Window.Resources>

    <Grid Effect="{StaticResource thresholdEffect}">

    </Grid>
</Window>
.NET Framework Security

Inheritance Hierarchy

System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows..::.Freezable
        System.Windows.Media.Animation..::.Animatable
          System.Windows.Media.Effects..::.Effect
            System.Windows.Media.Effects..::.ShaderEffect
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5 SP1, 3.0 SP2
See Also

Reference

Change History

Date

History

Reason

July 2008

Added topic for new class.

SP1 feature change.

Tags :


Community Content

Robin E Davies
How dependency properties of various types are loaded into floating point shader constant registers
The DependencyProperties that are bound to floating point shader constant registers can be any of the following types:

  • Double
  • Single ('float' in C#)
  • Color
  • Size
  • Point
  • Vector
  • Point3D
  • Vector3D
  • Point4D
They each will go into their shader register filling up whatever number of components of that register are appropriate. For instance, Double and Single go into one component, Color into 4, Size, Point and Vector into 2, etc. Unfilled components are set to '1'.
Tags :

Page view tracker