SpriteHand
Module Border
  Silverlight 4: GPU Accelerated PlaneProjection
Module Border
Location: BlogsAndy's Blog    
Posted by: host 3/16/2010 11:22 AM

Silverlight 4 introduces GPU Acceleration to Perspective 3D Transforms (PlaneProjection), which allows much better performance with simple 3D effects. Perspective 3D Transforms were originally introduced in Silverlight 3, and allow 2D visual elements to be rotated in 3D space. Consider the example below…



An image element, plain and simple.

The same image element, with a PlaneProjection applied to simulate 3D space.

The problem in Silverlight 3 was that PlaneProjection’s were rendered using software instead of the video card (GPU), so you could quickly peg the CPU if you overused this transform effect.

So how much faster is a PlaneProjection when using hardware acceleration? Well it all depends on your video card, but chances are it’s crazy fast J Here is a little sample that will let you compare software vs. GPU rendering in Silverlight 4:

VIEW THE SAMPLE WITHOUT GPU ACCELERATION. Add a bunch of phones and notice how the framerate starts to drop.

VIEW THE SAMPLE WITH GPU ACCELERATION. Add a bunch of phones and notice how the framerate remains consistently high.

DOWNLOAD THE SAMPLE. Requires Visual Studio 2010, Silverlight 4.

HOW TO GPU ACCELERATE A PLANEPROJECTION

We can apply hardware acceleration to a Perspective Transform in the same way as any UI element:

1.       First, enable GPU Acceleration at the plug-in level. Open you Silverlight host html page, and add the following parameters to the plug-in.

enableGpuAcceleration – required for any acceleration to take place
enableCacheVIsualization – if true, then any elements that are not cached will be tinted a color.
enableFrameRateCounter – will show a framerate counter at the top-left of the plug-in window.

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
  <param name="source" value="ClientBin/AccceleratedPersp3DSimple.xap"/>
  <param name="onError" value="onSilverlightError" />
  <param name="background" value="white" />
  <param name="minRuntimeVersion" value="4.0.50303.0" />
  <param name="autoUpgrade" value="true" />
  <param name="enableGpuAcceleration" value="true" />
  <param name="enableCacheVisualization" value="false" />
  <param name="enableFrameRateCounter" value="true" />

  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50303.0" style="text-decoration:none">
                <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
  </a>
</object>


2.       Next, apply a PlaneProjection to a visual element (in this case we’ll apply it to an Image element), and add in a CacheMode=”BitmapCache” attribute.

<Image x:Name="phoneFront" CacheMode="BitmapCache" Source="phoneSkin.png" Stretch="Fill" Width="303" Height="587" >
       <Image.Projection>
        <PlaneProjection x:Name="rotateFrontY" RotationY="0" GlobalOffsetZ="0"/>
       </Image.Projection>

</Image>

3.       That’s it! The PlaneProjection will now be rendered by the GPU.


SOME NOTES

This simple example makes it look easy, but as you get into more complicated examples you may run into issues. Keep in mind the following:

1.       Not all video cards are created equal. You will see entirely different performance and caching thresholds on different GPU’s. And on some low-end Netbooks, you may actually see worse performance when caching is turned on.

2.       The content of the PlaneProjection needs to stay static in order to keep the Cache valid. Think of the cache as a snapshot of the contents of the PlaneProjection which can be manipulated by scaling, clipping and perspective transforms. But if any of the contents are animated, it can invalidate the cache and you will lose most benefits of GPU acceleration.

3.       Be careful when setting properties on Visual elements such as Visibility. The Silverlight property system does not check if a property is “really” changed from its previous value, so if you change values often, you can incur extra rasterizations. If you look at the sample code, you’ll see that a _state flag that tells when to change Visibility to cover this issue. Thanks to David Ferguson at Microsoft for this tip!

 

Permalink |  Trackback

Comments (4)   Add Comment
Re: Silverlight 4: GPU Accelerated PlaneProjection    By Anonymous on 4/26/2010 10:55 AM
Does S4 accelerate Matrix3DProjection? thanks

Re: Silverlight 4: GPU Accelerated PlaneProjection    By Anonymous on 4/26/2010 8:25 PM
There is an open discussion on accelerating Matrix3DProjection with the Silverlight team. There seems to be issues with this, or it is unsupported. I'll post any details I learn of here.
-Andy

Re: Silverlight 4: GPU Accelerated PlaneProjection    By Anonymous on 11/15/2010 3:52 PM
Hey Andy,
Thanks for the samples. I was toying around with blender and I started to wonder we we could pull from this instead for our project. Unfortunately, it looks like your sample (and my testing) on my laptop starts dropping below 30 fps once you get over 300 phones. Where as with Balder, I can still hit 20-30 fps with over 10,000 faces. At 1k rectangles, even with hardware acceleration, the perspective engine just can't cope. But it was fun to toy around with.

Thanks!
-Rick

Re: Silverlight 4: GPU Accelerated PlaneProjection    By Anonymous on 11/15/2010 5:07 PM
edit: that should be "Balder" not "Blender".


Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Subscribe
Module Border
RSS   Twitter
Module Border Module Border
Module Border
  Diversions
Module Border

TALKING RAGDOLL
This Windows Phone 7 App was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine. It gets interesting when you import your friends photos and have your way with them!

MORE INFO



DROPPYPOP
This Windows Phone 7 game was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine.
DEMO

MORE INFO



BOSS LAUNCH
This physics game won first place in the Server Quest Contest. Created using Silverlight 2, the Physics Helper Library,  and the Farseer Physics Engine.
PLAY IT

MORE INFO



DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



PHYSICS HELPER DEMOS
These demos were created for the Physics Helper Library, which makes it easy to create physics games and simulations using Expression Blend, Silverlight, and the Farseer Physics Engine.
PLAY IT

INFO AND CODE



HOOK SHOT
This little basketball game took first place in the TeamZoneSports Silverlight Contest. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2013 andy.beaulieu.com - Login