This is a mirror of official site: http://jasper-net.blogspot.com/

Getting started with shader effects in WPF

| Tuesday, August 3, 2010
Introduction

Hardware accelerated effects for WPF were first introduced in .NET 3.5 SP1. Very complex effects and graphically rich applications can be created with little impact on performance thanks to the huge computing power of modern graphic cards. However, if you want to take advantage of this feature, you first need to learn a thing or two. The purpose of this article is to provide all the information you need to get started with Effects.

What is an effect?

Effects are an easy-to-use API to create (surprisingly) graphical effects. For example, if you want a button to cast a shadow, there are several ways to accomplish the task, but the simplest and most efficient method is to assign the "Effect" property of the button, either from code or in XAML:

Collapse
MyButton.Effect = new DropShadowEffect() { ... };
<Button Name="MyButton" ... >
 <Button.Effect>
   <DropShadowEffect ... />
 </Button.Effect>
</Button>  
As you can see, effects are so easy to use that you don't need any further explanation. The fun starts when you decide to write your own effects...

BitmapEffect, Effect, ShaderEffect... What?

First of all, there are several .NET classes that share the "Effect" suffix, and to make it even more confusing they are all in the System.Windows.Media.Effects namespace. However, not all of those classes are useful when it comes to hardware acceleration, in fact some of them are completely useless.

BitmapEffect

The BitmapEffect class and its subclasses were originally supposed to provide the functionality of effects. However, this API doesn't use any hardware acceleration and it has been marked obsolete in .NET 4.0. It's strongly recommended to avoid using the BitmapEffect class or any of its subclasses!

Effect and its derived classes

As stated above, you apply an effect to a control by assigning the control's Effect property (the property is actually inherited from UIElement, just in case you needed to know). Now the question is... What needs to be assigned to the Effect property? The answer is as simple as it can be - it's an object of type Effect.

The Effect class is the base class of all hardware accelerated effects. It has three subclasses: BlurEffect, DropShadowEffect and ShaderEffect. The first two are ready-to-use effects included directly in the .NET library. The ShaderEffect class is the base class of all custom effects.

Why BlurEffect and DropShadowEffect?

Why are there only 2 fully implemented effects in the library and why don't these 2 effects derive from ShaderEffect? I can't answer the first question, but I can tell you what makes BlurEffect and DropShadowEffect so special.

Both DropShadowEffect and BlurEffect are using complex algorithms that require multiple passes, but multi-pass effects are not normally possible. However, the guys at Microsoft probably did a few dirty hacks deep inside the unmanaged core of the WPF rendering engine and created these two effects.

Note: It is possible to create a single-pass blurring algorithm, but such algorithm is terribly slow compared to multi-pass blurring. Anyway, there are probably more reasons why these 2 effects are implemented in a special way.

How does it work?

If you want to take advantage of hardware acceleration, you first need to know how the whole thing works.

A few words about the GPU architecture

The architecture of Graphic Processing Units (GPUs) is different than the architecture of CPUs. GPUs are not general-purpose, they are designed to perform simple operations on large data sets. The operations are executed with high amount of parallelism, which results in great performance.

Modern GPUs are becoming more and more programmable and the range of tasks that can be executed on GPUs is growing (although there are several restrictions described below). Small programs executed on GPU are called shaders. There are several kinds of shaders - vertex shaders and geometry shaders are used when rendering 3D objects (not used by WPF Effects) and pixel shaders are used to perform simple operations on pixels.

There are even attempts to use the sheer computing power of GPUs for general purpose programming... Unfortunately there are several restrictions, such as limited number of instructions in one program, no ability to work with advanced data structures, limited memory management abilities etc. Amazing speed comes with several trade-offs...

Pixel shaders

A pixel shader is a short program that defines a simple operation executed on each pixel of the output image. That's pretty much all you need to create all kinds of interesting pixel-based effects.

Read more: Codeproject

Posted via email from .NET Info

13 comments:

Anonymous said...

Nice blog here! Also your site loads up very fast! What web host are you
using? Can I get your affiliate link to your host? I wish
my site loaded up as fast as yours lol

My web blog - she'd lose. This is the scientific explanation of weightloss plateaus and which the set point. You end up gazing for chances to achieve the sleep and rest you've forfeited. It has nutrition-empty calories and creates you will enjoy fat. Category:

Anonymous said...

It's nearly impossible to find well-informed people about this subject, however, you seem like you know what you're
talking about! Thanks

Feel free to surf to my weblog :: Adorna Reviews

Anonymous said...

Hello excellent blog! Does running a blog like this take a large amount of
work? I've absolutely no expertise in computer programming however I had been hoping to start my own blog in the near future. Anyhow, if you have any ideas or techniques for new blog owners please share. I know this is off subject nevertheless I just needed to ask. Thank you!

Here is my homepage working online from home

Anonymous said...

Hello! I could have sworn I've visited this web site before but after going through a few of the posts I realized it's new to
me. Regardless, I'm certainly pleased I discovered it and I'll
be bookmarking it and checking back frequently!

Here is my web page :: Green Coffee Cleanse Reviews

Anonymous said...

whoah this blog is fantastic i love reading your posts.
Keep up the great work! You realize, many individuals are looking around for this information,
you can aid them greatly.

my homepage :: Viarex

Anonymous said...

These are in fact great ideas in concerning blogging. You have
touched some good things here. Any way keep up wrinting.


Look at my web page :: Muscle BUilding Supplements

Anonymous said...

Howdy this is kind of of off topic but I was wanting to
know if blogs use WYSIWYG editors or if you have to
manually code with HTML. I'm starting a blog soon but have no coding experience so I wanted to get guidance from someone with experience. Any help would be enormously appreciated!

Feel free to visit my web blog :: enduros

Anonymous said...

I was curious if you ever considered changing the layout of your website?

Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so
people could connect with it better. Youve got an awful lot of text for
only having 1 or two pictures. Maybe you could space it out better?


my site - Best Weight Loss PRoducts

Anonymous said...

Hello! I know this is somewhat off topic but I was wondering which blog
platform are you using for this site? I'm getting tired of Wordpress because I've had
issues with hackers and I'm looking at alternatives for another platform. I would be fantastic if you could point me in the direction of a good platform.

Feel free to surf to my web site - the green coffee bean

Anonymous said...

I enjoy reading through a post that will make people think.

Also, many thanks for allowing me to comment!


Feel free to visit my homepage; garcinia cambogia where to but

Anonymous said...

Hello, Neat post. There's a problem together with your site in internet explorer, may check this? IE nonetheless is the marketplace chief and a big section of other folks will pass over your fantastic writing due to this problem.

My homepage; No2 maximus

Anonymous said...

Hello, i read your blog occasionally and i own a similar one and i
was just curious if you get a lot of spam remarks? If so how do you prevent it,
any plugin or anything you can suggest? I get so much lately it's driving me crazy so any help is very much appreciated.

Here is my blog post Pur Essence

Anonymous said...

I'm gone to say to my little brother, that he should also visit this blog on regular basis to get updated from newest gossip.

my web site - Garcinia Cambogia Trim Reviews