Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

HLSL Primer with WPF 3.5 SP1

High Level Shading Language (HLSL) is a language developed by Microsoft that allows you to create C-like programmable shaders for the Direct3D pipeline. DirectX provides a compiler to compile the HLSL down into GPU assembly code. HLSL programs come in three forms: vertex shaders, geometry shaders, and pixel shaders. WPF 3.5 SP1 will offer developers the capability to implement custom HLSL pixel shaders. A pixel shader performs per-pixel processing such as texture blending, lighting model computation, and per-pixel normal and/or environmental mapping. Limitations are placed upon HLSL programs based upon the pixel and/or vertex shader model version (i.e: Pixel shader V2.0 is instruction limited to 96 total instructions, 32 texture and 64 arithmetic instructions). For more information on HLSL, the MSDN Library entry serves as a good reference tool.

Learn How to Build Your Own Pixel Shader Using WPF

HLSL pixel shaders allow you to implement high-quality, hardware rendered visual effects, which enables one to realize the valuable performance advantages of utilizing the computer’s GPU as opposed to the CPU. With the release of WPF 3.5 SP1 developers are now able to develop their own HLSL PS v2.0 compliant shader effects and implement them in WPF applications.

Sample Transition Effect Developed Using HLSL

HLSL Primer Image HLSL Primer Image HLSL Primer Image HLSL Primer Image

Image 1 1st Progression 2nd Progress of Transition Image 2

The process of building your own pixel shader is very systematic: simply code your HLSL effect; compile it into bytecode using fxc.exe, which comes with the Microsoft DirectX SDK; include the shader effect in the WPF project and you can add your custom effect! You can even animate your effects by passing values to the parameters of the of shader effects through a technique similar to dependency property definition. Before you get started your system should meet the following system requirements:

  • Windows Vista; Windows XP; Windows Server 2003; Windows Server 2008;
  • Microsoft Visual Studio 2008 (C# Express, Visual Basic Express, or Professional Edition)
  • Microsoft .NET Framework 3.5 service pack 1
  • DirectX Software Development Kit

To develop an effect, code the HLSL using an editor of your preference and save the code with a .fx extension. Next compile the .fx file into a .ps file using fxc.exe. This can be done via the windows command line. Navigate to the directory containing the .fx file and issue the following command:

> fxc /T ps_2_0 /E main /Fo<name of HLSL file>.ps <name of HLSL file>.fx //This line says to compile to the Pixel Shader 2.0 profile, and to look for the entry point named "main".

Create a project containing all of the custom developed effects and include that project as part of any solution that will utilize them (make sure to remember to add the project as a reference of WPF project that will utilize them also). Set the build task of the .ps file to ‘Resource.’ Lastly, create a C# wrapper for your effect that sets up dependency properties for any parameters in the HLSL and creates a URI for the .ps file.

Now that you can add your own custom shader effects, you have a new dimension available in WPF to create unique applications and user experiences!

Comments: 0

You must Login to comment.

Featured Item

Page view counter