Silverlight Hardware Acceleration

Caching visual elements as bitmaps allows you to take advantage of hardware accelaration. Once an object or tree of objects has been cached as a bitmap, it no longer goes through the render phase as the application refreshes, rather, the cached bitmap is rendered. The cached bitmap swapping is able to take advantage of hardware acceleration from the user’s GPU which can yield significant performance improvements for some scenarios.

 

 

 

 

 

Hardware acceleration can benefit performance for the following scenarios:

  • Blending two static layers using opacity. For example, you can mark both trees of objects as cached, and the opacity animation will be executed using Hardware Acceleration.
  • Transforming objects (e.g. stretching and rotating).

Note: Caching objects can hurt performance if you misuse it. See How Hardware Acceleration Works below.

How to Use Hardware Acceleration

In order to cache objects and allow them to take advantage of hardware acceleration, do the following:

1. Enable composition caching at the plug-in level by setting the EnableGPUAcceleration parameter to “true”.

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

2. You can now enable GPU acceleration on the element(s) you wish to cache by specifying a CacheMode value of BitmapCache on the object or container of objects.

<StackPanel CacheMode="BitmapCache" … />

You can cache a single UIElement or, as in the StackPanel example above, a container UIElement and all of its children.

How Hardware Acceleration Works

The illustration below summarizes how Hardware Acceleration works.

Notice that not everything can be hardware accelerated. Effects like DropshadowEffect and OpacityMask can only be rendered by the software. Also, WriteableBitmap goes through its own rendering pipeline and therefore cannot take advantage of hardware acceleration.

When you Cache an object, you are creating multiple rendering surfaces in the VRAM which are then accelerated using the GPU. Other visible objects in your application that are not cached are rendered as surfaces in software. Your performance will be best if you can minimize the total number of rendering surfaces and get the hardware to do work where it can. Note that BitmapCache is the only supported cache-mode.