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.

  • 使用浮點數的完整著色器常數 register 限制為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: uintbytesbytelongulong、 、shortushort``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 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 的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 和目前的 DependencyObject 是否相等。Determines 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 的淺層複本 (Shallow Copy)。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 實作也可以叫用任何 Changed 處理常式,以回應類型 Freezable 的變更相依性屬性。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()

確定 Freezable 是從有效的執行緒進行存取。Ensures that the Freezable is being accessed from a valid thread. 如果 API 會讀取非相依性屬性的資料成員,則 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()

傳回代表目前物件的字串。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. 在任何 API 修改未以相依性屬性儲存的類別成員之後,衍生自 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. 在任何 API 將資料寫入至非相依性屬性的資料成員之前,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

適用於

另請參閱