Friday, December 17, 2010

WPF - DrawingContext and ShaderEffect

DrawingContext does not support ShaderEffect.

Using DrawingContext in OnRender() method of adorner, we can Push effect using PushEffect() method. But the strange thing is that DrawingContext.PushEffect() does not support ShaderEffect. All it supports is BitmapEffect :(. It seems to be a missing piece of functionality of conversion of all shaders to ShaderEffect.

So if you don't want to use BitmapEffect as it is obsolete now then DrawingContext wouldn't be of much use to you. There seems to be no other option though, e.g. we want to push effect to adorner, the only option available is to push BitmapEffect. Even PushEffect is decorated with ObsoleteAttribute in DrawingContext class.

http://msdn.microsoft.com/en-us/library/system.windows.media.drawingcontext.pusheffect.aspx

I have discussed about adorners here:

http://shujaatsiddiqi.blogspot.com/2010/07/wpf-adorners.html

If you want to study about ShaderEffect, you can refer this post:

http://shujaatsiddiqi.blogspot.com/2010/08/wpf-uielements-effects.html

Now you must be wondering why we are so much interested in having ShaderEffect with DrawingContext. The reason is the performance benefits with ShaderEffect compared to BitmapEffect. ShaderEffect uses pixel shaders in graphics processor (GPU). So it pushes the work outside the regular CPU. BitmapEffect provides a software solution to provide similar effects but it does not use new features of graphics processor.

2 comments:

Unknown said...

Amen, brother. I ran into exactly the same thing. I spent all morning working on a nice Emboss effect for a watermark that I wanted to paste over my application when the license wasn't valid. To my horror when I got to the point of connecting it to the adorner for the client area, I find that only Bitmap Effects can be pushed. I'm crushed, a whole morning wasted.

Guilherme said...

use

dim vc as new visualcontext
dim dc as DrawingContext = vc.renderopen

...

dc.drawimage ()
dc.close

vc.effect = new effect

vc.render.render(vc.visual)

it's work !