Pixel shaders enable you to add effects to Silverlight controls by modifying the pixels of their rendered output before displaying them on the screen. Typical effects include shadows, blurs, reflections, and color alteration, but the options are almost endless.
There are two pixel shaders included in the Silverlight 5 runtime:
DropShadowEffect
BlurEffect
Applying one of these effects is an easy as assigning the pixel shader to the Effect
property of a control. The following example demonstrates assigning a drop shadow to a control, with the result shown in Figure 9-2:
<Button Content="Shadow" Width="75" Height="23">
<Button.Effect>
<DropShadowEffect Opacity="0.5" />
</Button.Effect>
</Button>
You can create your own pixel shaders using the High-Level Shader Language (HLSL), which you then need to compile into a .ps
file, using the fxc
command-line tool included with the DirectX SDK. An easy way to do this is using a free tool named Shazzam, by Walt Ritscher, which can be downloaded at http://shazzam-tool.com
. This tool saves the need for downloading the large DirectX SDK and provides a user interface for editing, testing, and compiling pixel shaders. Writing pixel shaders is a complex topic and beyond the scope of this book. A good article by for learning how to write a pixel shader is the one by Rene Schulte available at http://channel9.msdn.com/coding4fun/articles/SilverShader--Introduction-to-Silverlight-and-WPF-Pixel-Shaders. However, it's rare to need to write your own pixel shaders, as a quick web search will probably turn up a pixel shader that provides the effect that you are after; many are available for download.
Note Pixel shader effects are rendered in software. Generally, they can be rather CPU-intensive to render, so you should use them sparingly to avoid negatively affecting the performance of your application.