SpriteHand
Module Border
  Silverlight 3: GPU Acceleration and Bitmap Caching
Module Border
Location: BlogsAndy's Blog    
Posted by: host 3/18/2009 3:14 PM

GPU Acceleration is my favorite new feature in Silverlight 3 because I think it will truly enable new, unique web experiences.  GPU Acceleration allows Silverlight to use the video card to render portions of the User Interface, which can greatly improve performance. Under the covers, Silverlight uses DirectX in Windows, and OpenGL on the Mac to take advantage of the video hardware.

Enabling GPU Acceleration

By default, GPU Acceleration is not enabled in Silverlight. You need to opt in to this feature using the following steps:

First, add in a parameter to your Silverlight object tag as follows:

<object id="plugin1" data="data:application/x-silverlight-2," type="application/x-silverlight-2"

    width="100%" height="100%">

    <param name="source" value="ClientBin/ScrollMonster.xap" />

    <param name="onerror" value="onSilverlightError" />

    <param name="background" value="white" />

    <param name="minRuntimeVersion" value="3.0.40307.0" />

    <param name="autoUpgrade" value="true" />

    <param name="EnableGPUAcceleration" value="true" />

    <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">

        <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"

            style="border-style: none" />

    </a>

</object>



Now that you have enabled GPU Acceleration at the plug-in level, you can then enable Bitmap Caching on individual UI Elements. You should choose elements in the UI that are relatively static, such as scrolling backgrounds. Items that Rotate, Scale or Clip are also accelerated. But if an element changes (such as through StoryBoard animation), then it is not a good candidate for Bitmap Caching because the element will need to be re-rendered for each change and the cache will be invalidated.

Consider the following example which depicts a UserControl that has Bitmap Caching enabled:

<UserControl x:Class="ScrollMonster.ucMonkey"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    >

    <Canvas x:Name="LayoutRoot" Width="739" Height="700" >

        <Canvas.CacheMode>

            <BitmapCache/>

        </Canvas.CacheMode>

 

       <!—OTHER XAML CONTENT GOES HERE -->

 

    </Canvas>

</UserControl>

 

That’s it! They really made GPU Acceleration and Bitmap Caching easy to implement!

A Sample Application

Here is a little sample application that really shows just how dramatic the performance improvement is with GPU Acceleration and Bitmap Caching:

IMPORTANT: THE FOLLOWING IS A SILVERLIGHT 3 DEMO! SILVERLIGHT 3 IS CURRENTLY IN BETA AND YOU SHOULD ONLY INSTALL SL3 IF YOU ARE A DEVELOPER INTERESTED IN EXPERIMENTING!

[VIEW DEMO WITHOUT CACHING]  and note the Frames per second

[VIEW DEMO WITH CACHING] and note the Frames per second

[DOWNLOAD SOURCE]

Scrolling Game Sample

Parallax Scrolling is an old school method of introducing depth to a 2D scrolling game by providing several layers that move at different speeds (closer layers move by faster than layers farther away). In Silverlight 2, it was pretty difficult to pull this off because of performance constraints. But in Silverlight 3 we can take advantage of GPU Acceleration and do some very nice Parallax Scrolling effects J

Here is a little demo that shows off scrolling with GPU Acceleration in Silverlight 3. It uses the Physics Helper Library – actually a newer version of Physics Helper that I’ll be releasing soon.

IMPORTANT: THE FOLLOWING IS A SILVERLIGHT 3 DEMO! SILVERLIGHT 3 IS CURRENTLY IN BETA AND YOU SHOULD ONLY INSTALL SL3 IF YOU ARE A DEVELOPER INTERESTED IN EXPERIMENTING!

[VIEW DEMO WITH CACHING]

Debugging and Troubleshooting

What happens if you see no performance increase when turning on GPU Acceleration in Silverlight? It would be helpful if there was some way to determine if elements were successfully bitmap cached or not.

It turns out there is just such an option: EnableCacheVisualization. We just add in a param to our Silverlight <object> tag like so:

<param name="EnableCacheVisualization" value="true" />

 



… and what you will see is that any uncached object is tinted a color. That’s right: uncached objects are tinted, and cached objects appear normally.

One other note: you need to be careful of is the size (height and width) of your UI Elements that you are trying to cache. On some video cards, there is a limit to the size of the surface that can be handled by the hardware.  A good rule of thumb is to keep the height and width of your cached UI Elements under 2048 pixels.

 

 

Permalink |  Trackback

Comments (14)   Add Comment
Links to Silverlight 3.0 resources    By TrackBack on 3/19/2009 3:26 AM
Links to Silverlight 3.0 resources
# Silverlight Playground

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 3/19/2009 7:16 AM
So, if we are moving things around with RenderTransforms (rather than story boards), should they use BitMap Caching?

How does this relate to the "CacheMode" property on UI Elements?

-Jeff Weber

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 3/19/2009 11:37 AM
Hi Jeff,

If you are doing a RenderTransform on a "static" element (such as a scrolling background, or something without animation), then you could take advantage of Bitmap Caching.

If you get a chance to install the SL 3 Beta, take a look at the first scrolling sample (with the monkeys on buildings). You'll see just how crazy fast it makes it. I'm seeing 60 FPS on a crappy consumer model notebook with onboard video, full screen! And on my kids' PC (an old AMD 3200 with ATI 9600 card) it's pretty close to that.

-Andy

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 3/19/2009 5:06 PM
Amazing article Andy, can't wait to try things out my self. Just need to have an MCTS exam done first :)

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 3/20/2009 5:41 AM
"But if an element changes (such as through StoryBoard animation), then it is not a good candidate for Bitmap Caching because the element will need to be re-rendered for each change and the cache will be invalidated."

Im not completely sure what you mean here. You say that Scale, Rotate etc. is good for GPU rendering, but at the same time you say that Storyboards are bad? Typically what a Storyboard do, is to Scale and Rotate? Can you explain that a little further?

:)

//Mads
http://laumania.net

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 3/20/2009 6:03 AM
Sure. Here is an example in the context of the "Robot Physics Scroller" (parallax scoller) example above:

In this example there is a "ground" layer in the foreground and a "mountain" layer in the background. These are both great candidates for Bitmap Caching because they never change (they are "static").

But the little Robot dude that you steer around has an animated face. He blinks his eyes and his mouth is animated, using a StoryBoard. So he is NOT a good candidate for Bitmap Caching because he changes his appearance quite frequently - and each change would invalidate the Cache.

As for rotating and scaling, I don't have an example in the context of the Robot Physics Scoller... But if you look at the first example with the Monkeys scrolling around, you will see there are Scale and Rotate checkboxes. Note that when you select those, the FrameRate drops a bit, but is still a lot faster than the example without Bitmap Caching turned on.

-Andy

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 3/20/2009 8:21 AM
It sounds like you are saying that it always faster to use Bitmap Caching, but at the same time your not. Sorry to be a little anoing here :)

But your answer is ok, guess I will see your point as I get time to look more into SL3.

//Mads
http://laumania.net

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 3/27/2009 2:59 AM
i had craeated a silverlight application in silverlight2.0 rtm and now i canverted it to SL 3.0 beta, in my application i am downloading about 200 images from remote server and dispaling them on relation basic so my memory consuption is too high, and each image is created in rumtime and have some animation through storybord so i want to know is there anybenifit if i use the GPU acceleration. on first trail i am not getting any diffrence by EnableGPUAcceleration. please suggest.

neeraj Trivedi
neeraj.pu@gmail.com

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 3/27/2009 5:36 AM
Hi neeraj, it depends on (1) what your storyboards are animating and (2) what elements you add Bitmap Caching too.

There is a discussion I would recommend reading here - http://silverlight.net/forums/t/83076.aspx

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 8/23/2009 7:39 AM
Hi, I am playing a video with own shader effect and of course the bitmap cache is invalidated in SL3.
Is there any work around to make MediaElement have bitmap cache in my case of custom shader effect? For example, using Bitmap api, mediaelement binding ?
Thanks in advance.... Robert

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 8/23/2009 6:28 PM
Hi Robert, Unfortunately Effects cannot be GPU Accelerated in Silverlight 3 :(

-Andy

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 8/24/2009 10:02 AM
Hi Andy,

Thanks for the post! I tried this out with my own app and didn't notice any difference in speed. But then just to check I tried out your demos with/without caching (the monkey image) and each averaged about 4FPS. I have a decent enough GC and I have Silverlight 3 installed, is there anything else you know of that might be stopping Silverlight using my GC?

Thanks,
John

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 8/24/2009 11:54 AM
Hi John,

If you're getting 4 FPS on the NON-GPU accelerated than something is probably awry right there. You don't happen to have any screen sharing or screen capturing software running do you?

Do you mind sharing your configuration information? (OS, video card, memory, etc)? GPU Acceleration in SL uses DirectX, so I'm curious also if your DirectX 2D games are performing OK? The only instances I've heard of GPU Acceleration being that slow was on Netbooks which had really funky GPUs (if you could even call them that :))

-Andy

Re: Silverlight 3: GPU Acceleration and Bitmap Caching    By Anonymous on 10/1/2009 5:07 AM
Hey Andy,

sorry it took so long to get back to you - I got put on another project - but I finally got around to trying this on another laptop & the GPU accelerated version was at least twice as fast. And yes, good guess, I was on a Netbook with a funky GPU - Unreal Tournament works well on it though :) I'll have to hope that it works with the clients' GPUs (hoping they have any at all!)

Thanks,
John


Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Subscribe
Module Border

RSS

Module Border Module Border
Module Border
  Diversions
Module Border

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) 2010 andy.beaulieu.com - Login