Click or drag to resize
PixelShaderEffect Class
[NoComposition] Custom image effect which uses a pixel shader to apply arbitrary user-defined processing.
Inheritance Hierarchy
SystemObject
  Microsoft.Graphics.Canvas.EffectsPixelShaderEffect

Namespace:  Microsoft.Graphics.Canvas.Effects
Assembly:  Microsoft.Graphics.Canvas (in Microsoft.Graphics.Canvas.dll) Version: 0.0.0.0
Syntax
C#
public sealed class PixelShaderEffect : ICanvasEffect, 
	IGraphicsEffect, IGraphicsEffectSource, ICanvasImage, IDisposable

The PixelShaderEffect type exposes the following members.

Constructors
  NameDescription
Public methodPixelShaderEffect
Initializes a new instance of the PixelShaderEffect class.
Top
Properties
  NameDescription
Public propertyBufferPrecision
Specifies what precision to use for intermediate buffers when drawing this effect.
Public propertyCacheOutput
Enables caching the output from drawing this effect.
Public propertyMaxSamplerOffset
Describes the maximum offset the shader will add to or subtract from a texture coordinate when sampling from an input that uses Offset coordinate mapping mode.
Public propertyName
Attaches a user-defined name string to the effect.
Public propertyProperties
Collection of properties configures the constant buffer that will be passed to the custom pixel shader.
Public propertySource1
Gets or sets the first input source for the custom pixel shader.
Public propertySource1BorderMode
Sets the border mode used when sampling the first input texture.
Public propertySource1Interpolation
Sets the interpolation mode used when sampling the first input texture.
Public propertySource1Mapping
Describes what texture coordinates the shader will use when sampling its first input texture.
Public propertySource2
Gets or sets the second input source for the custom pixel shader.
Public propertySource2BorderMode
Sets the border mode used when sampling the second input texture.
Public propertySource2Interpolation
Sets the interpolation mode used when sampling the second input texture.
Public propertySource2Mapping
Describes what texture coordinates the shader will use when sampling its second input texture.
Public propertySource3
Gets or sets the third input source for the custom pixel shader.
Public propertySource3BorderMode
Sets the border mode used when sampling the third input texture.
Public propertySource3Interpolation
Sets the interpolation mode used when sampling the third input texture.
Public propertySource3Mapping
Describes what texture coordinates the shader will use when sampling its third input texture.
Public propertySource4
Gets or sets the fourth input source for the custom pixel shader.
Public propertySource4BorderMode
Sets the border mode used when sampling the fourth input texture.
Public propertySource4Interpolation
Sets the interpolation mode used when sampling the fourth input texture.
Public propertySource4Mapping
Describes what texture coordinates the shader will use when sampling its fourth input texture.
Public propertySource5
Gets or sets the fifth input source for the custom pixel shader.
Public propertySource5BorderMode
Sets the border mode used when sampling the fifth input texture.
Public propertySource5Interpolation
Sets the interpolation mode used when sampling the fifth input texture.
Public propertySource5Mapping
Describes what texture coordinates the shader will use when sampling its fifth input texture.
Public propertySource6
Gets or sets the sixth input source for the custom pixel shader.
Public propertySource6BorderMode
Sets the border mode used when sampling the sixth input texture.
Public propertySource6Interpolation
Sets the interpolation mode used when sampling the sixth input texture.
Public propertySource6Mapping
Describes what texture coordinates the shader will use when sampling its sixth input texture.
Public propertySource7
Gets or sets the seventh input source for the custom pixel shader.
Public propertySource7BorderMode
Sets the border mode used when sampling the seventh input texture.
Public propertySource7Interpolation
Sets the interpolation mode used when sampling the seventh input texture.
Public propertySource7Mapping
Describes what texture coordinates the shader will use when sampling its seventh input texture.
Public propertySource8
Gets or sets the eighth input source for the custom pixel shader.
Public propertySource8BorderMode
Sets the border mode used when sampling the eighth input texture.
Public propertySource8Interpolation
Sets the interpolation mode used when sampling the eighth input texture.
Public propertySource8Mapping
Describes what texture coordinates the shader will use when sampling its eighth input texture.
Top
Methods
  NameDescription
Public methodDispose
Releases all resources used by the effect.
Public methodGetBounds(ICanvasResourceCreator)
Retrieves the bounds of this PixelShaderEffect.
Public methodGetBounds(ICanvasResourceCreator, Matrix3x2)
Retrieves the bounds of this PixelShaderEffect.
Public methodGetInvalidRectangles
Queries what regions of the effect output have changed since it was last drawn.
Public methodCode exampleGetRequiredSourceRectangle
Queries what part of an effect source image is needed to draw an output region.
Public methodGetRequiredSourceRectangles
Queries what parts of the effect source images are needed to draw an output region.
Public methodInvalidateSourceRectangle
Notifies the effect that one of its source images has changed.
Public methodIsSupported
Checks whether the pixel shader used by this effect is compatible with the Direct3D feature level of the specified device.
Top
Remarks

Supported by Win2D but not Windows.UI.Composition.

This effect renders graphics using a custom pixel shader. Depending on the chosen shader, it supports from 0 to 8 sources. These can be any ICanvasImage, and are passed into the shader as input textures.

Steps to use a custom pixel shader:

  1. Write a pixel shader in HLSL
  2. Compile it using fxc.exe
  3. Include the resulting .bin file with your app (you do not need to ship the source .hlsl file)
  4. Load the .bin file and pass it to the PixelShaderEffect(Byte) constructor
  5. If your shader uses non 1:1 texture coordinate mapping (sampling at different locations from the pixel being shaded), make sure the appropriate SamplerCoordinateMapping is set (eg. Source1Mapping)
  6. Set from 0 to 8 source properties (eg. Source1) as required by the shader
  7. Configure any constant buffer Properties required by the shader
  8. Draw the effect!

See the PixelShaderEffect(Byte) constructor documentation for more details about steps #1 and #2.

Note that custom pixel shaders work in units of pixels rather than dips. For instance the value returned by D2DGetScenePosition (from d2d1effecthelpers.hlsli) is in pixels. If your shader manipulates position or size values, you have two options:

  • Have the shader work in pixels. In your CPU program code, convert all position and size values from dips to pixels (eg. using ConvertDipsToPixels(Single, CanvasDpiRounding)) before setting them as effect properties.
  • Or have the shader work in dips. Pass the current Dpi value as an effect property, and in your HLSL shader code, scale the result of D2DGetScenePosition by 96/dpi to convert it from pixels to dips.
Examples
var effect = new PixelShaderEffect(File.ReadAllBytes("MyShader.bin"))
{
    Source1 = tigerBitmap
};

effect.Properties["tint"] = new Vector4(0.5f, 1, 0, 1);

drawingSession.DrawImage(effect);

Note: the File.ReadAllBytes API is only available in UWP. If you are building for Windows or Windows Phone 8.1, replace this call with StorageFile.GetFileFromApplicationUriAsync and FileIO.ReadBufferAsync. See the ReadAllBytes helper in Example Gallery for details.

See Also