ShaderEffect 类

定义

通过使用 PixelShader 提供自定义位图效果。Provides a custom bitmap effect by using a PixelShader.

public ref class ShaderEffect abstract : System::Windows::Media::Effects::Effect
public abstract class ShaderEffect : System.Windows.Media.Effects.Effect
type ShaderEffect = class
    inherit Effect
Public MustInherit Class ShaderEffect
Inherits Effect
继承

示例

下面的代码示例演示如何从ShaderEffect类派生。The following code example shows how to derive from the ShaderEffect class.

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
    }
}

下面的代码示例演示与前面ShaderEffect的类相对应的着色器。The following code example shows a shader that corresponds to the previous ShaderEffect class.

// 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;
}

下面的 XAML 演示如何使用自定义着色器效果。The following XAML shows how to use the custom shader effect.

<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>

注解

ShaderEffect类派生以实现基于单个像素着色器的自定义效果。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. PixelShader从预编译的高级着色语言 (HLSL) 字节码加载。Load a PixelShader from precompiled High Level Shading Language (HLSL) bytecode.

  2. 定义表示效果的参数和基于的Brush图面输入的依赖项属性。Define dependency properties that represent the parameters of the effect and the Brush-based surface inputs. 使用其中一个RegisterPixelShaderSamplerProperty重载将这些输入与 HLSL 字节码中引用的寄存器号关联。Use one of the RegisterPixelShaderSamplerProperty overloads to associate these inputs with register numbers that are referenced in the HLSL bytecode.

采样器数限制为4。The number of samplers is limited to 4.

使用 PS 3.0 着色器时, 以下限制适用。The following restrictions apply when using a PS 3.0 shader.

  • 分配 PS 3.0 着色器后, 取样器数将增加到8。When a PS 3.0 shader is assigned, the number of samplers increases to 8. 将 PS 3.0 着色器分配到其他着色器之前, 以允许注册8个取样器。Assign the PS 3.0 shader before other shaders to enable registering 8 samplers.

  • 使用完整的着色器常量寄存器限制 (224)。The full shader constant register limit of 224 for floats is used. 有关详细信息, 请参阅ps_3_0For more information, see ps_3_0.

  • 仅 PS 3.0 着色器支持以下数据类型。The following data types are supported in PS 3.0 shaders only. 如果在较低的着色器版本中使用了异常, 则会引发异常。An exception is thrown if these are used in lower shader versions.

    • int和类型可转换为int: uint byte sbyte 、、、longulong 、、、ushort short``charint and types convertible to int: uint, byte, sbyte, long, ulong, short, ushort, char

    • bool

  • 如果在不支持 PS 3.0 硬件的计算机上加载有效 PS 3.0 着色器, 则将忽略着色器。If a valid PS 3.0 shader is loaded on a computer that does not have hardware support for PS 3.0, the shader is ignored. 如果着色器无效, 则不会引发异常。If the shader is invalid, no exception is thrown.

  • 如果计算机有多个视频卡, 则该行为由功能最少的视频卡定义。If a computer has more than one video card, the behavior is defined by the least capable video card. 例如, 如果计算机有两个视频卡, 其中一个卡支持 PS 3.0, 其中一个不支持 PS, 则行为与计算机不支持 PS 3.0 时相同。For example, if the computer has two video cards, one of which supports PS 3.0 and one of which does not, the behavior is the same as if the computer does not support PS 3.0.

  • 如果计算机支持在硬件中呈现 PS 3.0, 但分配了无效的 ps 3.0 着色器, InvalidPixelShaderEncountered则会引发事件。If a computer supports rendering PS 3.0 in hardware, but an invalid PS 3.0 shader is assigned, the InvalidPixelShaderEncountered event is raised. 无效 PS 3.0 着色器的一个示例是用ps_3_sw标志编译的着色器。An example of an invalid PS 3.0 shader is one compiled with the ps_3_sw flag. 类只接受用传递到 fxc.exe 的ps_3_0标记编译的 PS 3.0 着色器。 ShaderEffectThe ShaderEffect class accepts only PS 3.0 shaders that are compiled with the ps_3_0 flag passed to fxc.exe. 有关详细信息, 请参阅效果-编译器工具For more information, see Effect-Compiler Tool.

备注

在软件中呈现时, PS 2.0 着色器将运行。PS 2.0 shaders run when rendering in software. 但是, 即使系统的硬件支持 PS 3.0, PS 3.0 着色器也不会在软件呈现期间运行。However, even if PS 3.0 is supported by the system's hardware, PS 3.0 shaders do not run during software rendering.

构造函数

ShaderEffect()

初始化 ShaderEffect 类的新实例。Initializes a new instance of the ShaderEffect class.

字段

PixelShaderProperty

标识 PixelShader 依赖项属性。Identifies the PixelShader dependency property.

属性

CanFreeze

获取一个值,该值指示是否可将对象变为不可修改。Gets a value that indicates whether the object can be made unmodifiable.

(继承自 Freezable)
DdxUvDdyUvRegisterIndex

获取或设置一个值,该值指示要用于纹理坐标对屏幕空间的偏导数的着色器寄存器。Gets or sets a value that indicates the shader register to use for the partial derivatives of the texture coordinates with respect to screen space.

DependencyObjectType

获取对此实例的 DependencyObjectType 类型进行包装的 CLRCLRGets the DependencyObjectType that wraps the CLRCLR type of this instance.

(继承自 DependencyObject)
Dispatcher

获取与此 Dispatcher 关联的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(继承自 DispatcherObject)
EffectMapping

当在派生类中重写时,通过效果转换鼠标输入和坐标系。When overridden in a derived class, transforms mouse input and coordinate systems through the effect.

(继承自 Effect)
HasAnimatedProperties

获取一个值,该值指示一个或多个 AnimationClock 对象是否与此对象的任何依赖项属性相关联。Gets a value that indicates whether one or more AnimationClock objects is associated with any of this object's dependency properties.

(继承自 Animatable)
IsFrozen

获取一个值,该值指示对象当前是否可修改。Gets a value that indicates whether the object is currently modifiable.

(继承自 Freezable)
IsSealed

获取一个值,该值指示此实例当前是否为密封的(只读)。Gets a value that indicates whether this instance is currently sealed (read-only).

(继承自 DependencyObject)
PaddingBottom

获取或设置一个值,该值指示沿下边缘方向效果的输出纹理大于其输入纹理。Gets or sets a value indicating that the effect's output texture is larger than its input texture along the bottom edge.

PaddingLeft

获取或设置一个值,该值指示沿左边缘方向效果的输出纹理大于其输入纹理。Gets or sets a value indicating that the effect's output texture is larger than its input texture along the left edge.

PaddingRight

获取或设置一个值,该值指示沿右边缘方向效果的输出纹理大于其输入纹理。Gets or sets a value indicating that the effect's output texture is larger than its input texture along the right edge.

PaddingTop

获取或设置一个值,该值指示沿上边缘方向效果的输出纹理大于其输入纹理。Gets or sets a value indicating that the effect's output texture is larger than its input texture along the top edge.

PixelShader

获取或设置要用于该效果的 PixelShaderGets or sets the PixelShader to use for the effect.

方法

ApplyAnimationClock(DependencyProperty, AnimationClock)

AnimationClock 应用到指定的 DependencyPropertyApplies an AnimationClock to the specified DependencyProperty. 如果该属性已进行动画处理,则使用 SnapshotAndReplace 切换行为。If the property is already animated, the SnapshotAndReplace handoff behavior is used.

(继承自 Animatable)
ApplyAnimationClock(DependencyProperty, AnimationClock, HandoffBehavior)

AnimationClock 应用到指定的 DependencyPropertyApplies an AnimationClock to the specified DependencyProperty. 如果该属性已进行动画处理,则使用指定的 HandoffBehaviorIf the property is already animated, the specified HandoffBehavior is used.

(继承自 Animatable)
BeginAnimation(DependencyProperty, AnimationTimeline)

将动画应用于指定 DependencyPropertyApplies an animation to the specified DependencyProperty. 动画会在呈现下一帧时启动。The animation is started when the next frame is rendered. 如果指定属性已进行动画处理,则使用 SnapshotAndReplace 切换行为。If the specified property is already animated, the SnapshotAndReplace handoff behavior is used.

(继承自 Animatable)
BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior)

将动画应用于指定 DependencyPropertyApplies an animation to the specified DependencyProperty. 动画会在呈现下一帧时启动。The animation is started when the next frame is rendered. 如果指定的属性已进行动画处理,则使用指定的 HandoffBehaviorIf the specified property is already animated, the specified HandoffBehavior is used.

(继承自 Animatable)
CheckAccess()

确定调用线程是否可以访问此 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)
ClearValue(DependencyProperty)

清除属性的本地值。Clears the local value of a property. 要清除的属性由 DependencyProperty 标识符指定。The property to be cleared is specified by a DependencyProperty identifier.

(继承自 DependencyObject)
ClearValue(DependencyPropertyKey)

清除只读属性的本地值。Clears the local value of a read-only property. 要清除的属性由 DependencyPropertyKey 指定。The property to be cleared is specified by a DependencyPropertyKey.

(继承自 DependencyObject)
Clone()

创建此 ShaderEffect 对象的可修改复本,从而深度复制此对象的值。Creates a modifiable clone of this ShaderEffect object, making deep copies of this object's values. 在复制此对象的依赖项属性时,此方法复制资源引用和数据绑定(可能不再解析),但不复制动画或其当前值。When copying this object's dependency properties, this method copies resource references and data bindings (which may no longer resolve), but not animations or their current values.

CloneCore(Freezable)

使用基(未经过动画处理的)属性值使该实例成为指定 Freezable 的克隆(深层复制)。Makes the instance a clone (deep copy) of the specified Freezable using base (non-animated) property values.

CloneCurrentValue()

创建此 ShaderEffect 对象的可修改复本,从而深度复制此对象的当前值。Creates a modifiable clone of this ShaderEffect object, making deep copies of this object's current values. 不复制资源引用、数据绑定和动画,而是复制其当前值。Resource references, data bindings, and animations are not copied, but their current values are copied.

CloneCurrentValueCore(Freezable)

使用当前属性值使该实例成为指定 Freezable 的可修改克隆(深层复制)。Makes the instance a modifiable clone (deep copy) of the specified Freezable using current property values.

CoerceValue(DependencyProperty)

对指定依赖属性的值进行强制。Coerces the value of the specified dependency property. 通过对调用方 CoerceValueCallback 上存在的依赖属性的属性元数据中所指定的任何 DependencyObject 函数进行调用来完成此操作。This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject.

(继承自 DependencyObject)
CreateInstance()

初始化 Freezable 类的新实例。Initializes a new instance of the Freezable class.

(继承自 Freezable)
CreateInstanceCore()

创建 Freezable 派生类的新实例。Creates a new instance of the Freezable derived class.

Equals(Object)

确定提供的 DependencyObject 是否等效于当前 DependencyObjectDetermines whether a provided DependencyObject is equivalent to the current DependencyObject.

(继承自 DependencyObject)
Freeze()

使当前对象不可修改,并且将其 IsFrozen 属性设置为 trueMakes the current object unmodifiable and sets its IsFrozen property to true.

(继承自 Freezable)
FreezeCore(Boolean)

使此 Animatable 对象成为不可修改的对象,或确定是否可使其成为不可修改的对象。Makes this Animatable object unmodifiable or determines whether it can be made unmodifiable.

(继承自 Animatable)
GetAnimationBaseValue(DependencyProperty)

返回指定的 DependencyProperty 的未经过动画处理的值。Returns the non-animated value of the specified DependencyProperty.

(继承自 Animatable)
GetAsFrozen()

使用基(未经过动画处理的)属性值创建 Freezable 的冻结副本。Creates a frozen copy of the Freezable, using base (non-animated) property values. 由于副本已冻结,因此将通过引用复制任何冻结的子对象。Because the copy is frozen, any frozen sub-objects are copied by reference.

(继承自 Freezable)
GetAsFrozenCore(Freezable)

使用基(未经过动画处理的)属性值使该实例成为指定的 Freezable 的冻结复本。Makes the instance a frozen clone of the specified Freezable using base (non-animated) property values.

GetCurrentValueAsFrozen()

使用当前属性值创建 Freezable 的冻结副本。Creates a frozen copy of the Freezable using current property values. 由于副本已冻结,因此将通过引用复制任何冻结的子对象。Because the copy is frozen, any frozen sub-objects are copied by reference.

(继承自 Freezable)
GetCurrentValueAsFrozenCore(Freezable)

使当前实例成为指定的 Freezable 的冻结复本。Makes the current instance a frozen clone of the specified Freezable. 如果对象具有经过动画处理的依赖项对象,则复制这些属性当前经过动画处理的值。If the object has animated dependency properties, their current animated values are copied.

GetHashCode()

获取此 DependencyObject 的哈希代码。Gets a hash code for this DependencyObject.

(继承自 DependencyObject)
GetLocalValueEnumerator()

创建一个专用的枚举数,用于确定哪些依赖项属性在此 DependencyObject 上具有以本地方式设置的值。Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject.

(继承自 DependencyObject)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
GetValue(DependencyProperty)

DependencyObject 的此实例返回依赖属性的当前有效值。Returns the current effective value of a dependency property on this instance of a DependencyObject.

(继承自 DependencyObject)
InvalidateProperty(DependencyProperty)

重新评估指定依赖属性的有效值。Re-evaluates the effective value for the specified dependency property.

(继承自 DependencyObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OnChanged()

修改当前 Freezable 对象时调用。Called when the current Freezable object is modified.

(继承自 Freezable)
OnFreezablePropertyChanged(DependencyObject, DependencyObject)

确保为刚刚设置的 DependencyObjectType 数据成员建立适当的上下文指针。Ensures that appropriate context pointers are established for a DependencyObjectType data member that has just been set.

(继承自 Freezable)
OnFreezablePropertyChanged(DependencyObject, DependencyObject, DependencyProperty)

此成员支持 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 基础结构,但不能在代码中直接使用。This member supports the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

(继承自 Freezable)
OnPropertyChanged(DependencyPropertyChangedEventArgs)

重写 OnPropertyChanged(DependencyPropertyChangedEventArgs)DependencyObject 实现,以同时调用任何响应类型 Freezable 不断变化的依赖属性的 Changed 处理程序。Overrides the DependencyObject implementation of OnPropertyChanged(DependencyPropertyChangedEventArgs) to also invoke any Changed handlers in response to a changing dependency property of type Freezable.

(继承自 Freezable)
PixelShaderConstantCallback(Int32)

将依赖项属性值与像素着色器的浮点常量寄存器相关联。Associates a dependency property value with a pixel shader's float constant register.

PixelShaderSamplerCallback(Int32)

将依赖项属性值与像素着色器的采样器寄存器相关联。Associates a dependency property value with a pixel shader's sampler register.

PixelShaderSamplerCallback(Int32, SamplingMode)

将依赖项属性值与像素着色器的采样器寄存器以及 SamplingMode 相关联。Associates a dependency property value with a pixel shader's sampler register and a SamplingMode.

ReadLocalValue(DependencyProperty)

如果存在,则返回依赖属性的本地值。Returns the local value of a dependency property, if it exists.

(继承自 DependencyObject)
ReadPreamble()

确保正在从有效的线程访问 FreezableEnsures that the Freezable is being accessed from a valid thread. Freezable 的继承者必须在任何 API 一开始读取不属于依赖项对象的数据成员时调用此方法。Inheritors of Freezable must call this method at the beginning of any API that reads data members that are not dependency properties.

(继承自 Freezable)
RegisterPixelShaderSamplerProperty(String, Type, Int32)

将依赖项属性与着色器的采样器寄存器相关联。Associates a dependency property with a shader sampler register.

RegisterPixelShaderSamplerProperty(String, Type, Int32, SamplingMode)

将依赖项属性与着色器的采样器寄存器以及 SamplingMode 相关联。Associates a dependency property with a shader sampler register and a SamplingMode.

SetCurrentValue(DependencyProperty, Object)

设置依赖属性的值而不更改其值源。Sets the value of a dependency property without changing its value source.

(继承自 DependencyObject)
SetValue(DependencyProperty, Object)

设置依赖属性的本地值,该值由其依赖属性标识符指定。Sets the local value of a dependency property, specified by its dependency property identifier.

(继承自 DependencyObject)
SetValue(DependencyPropertyKey, Object)

设置一个只读依赖属性的本地值,该值由依赖属性的 DependencyPropertyKey 标识符指定。Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.

(继承自 DependencyObject)
ShouldSerializeProperty(DependencyProperty)

返回一个值,该值指示序列化进程是否应序列化所提供的依赖属性的值。Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property.

(继承自 DependencyObject)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
UpdateShaderValue(DependencyProperty)

通知效果应当更新与指定依赖项属性对应的着色器常量或采样器。Notifies the effect that the shader constant or sampler corresponding to the specified dependency property should be updated.

VerifyAccess()

强制调用线程具有此 DispatcherObject 的访问权限。Enforces that the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)
WritePostscript()

引发 FreezableChanged 事件并调用其 OnChanged() 方法。Raises the Changed event for the Freezable and invokes its OnChanged() method. Freezable 派生的类应在修改的类成员不存储为依赖属性的任何 API 的末尾调用此方法。Classes that derive from Freezable should call this method at the end of any API that modifies class members that are not stored as dependency properties.

(继承自 Freezable)
WritePreamble()

验证 Freezable 是否未被冻结,并且是否正在从有效的线程上下文中访问它。Verifies that the Freezable is not frozen and that it is being accessed from a valid threading context. Freezable 的继承项应当在任何 API 一开始写入不属于依赖项属性的数据成员时调用此方法。Freezable inheritors should call this method at the beginning of any API that writes to data members that are not dependency properties.

(继承自 Freezable)

事件

Changed

在修改 Freezable 或其包含的对象时发生。Occurs when the Freezable or an object it contains is modified.

(继承自 Freezable)

安全性

UIPermission
对 UI 资源的完全访问权限。for full access to UI resources. 关联的枚举:AllWindowsAssociated enumeration: AllWindows

适用于

另请参阅