Improving Performance of images used with 3D

I've been working on some 3D using WPF and was making heavy use of some largish (around 1 megapixel) images with ImageBrushes applied to DiffuseMaterials. Naturally, being WPF there was some animation thrown in for good measure to make sure I'm working that processor like a bad boy.

All was going well, except for one annoying niggle that I couldn't fix. Whenever a new image was displayed on screen there was a notable stutter in the animation whilst *something* happened. For starters, I assumed this was the I/O time to read the image from disk but I tried pre-loading the images in all kinds of ways without any succcess.

Failing to get a result, I turned to some graphics heroes in the WPF team who immediately suspected it was the Fant scaler resizing the image on first load (I too thought he'd misspelt Font for a while). The quick fix, it transpires is to swap your ImageBrush for a VisualBrush.

Whoa! But VisualBrushes are slower than ImageBrushes. I know, but it works. The key to the trick is removing the Fant scaler and going for the faster, Linear scaler. So we went from:

<DiffuseMaterial.Brush>
<ImageBrush ImageSource="{Binding Source}" />
</DiffuseMaterial.Brush>

... to ...

<DiffuseMaterial.Brush>
<VisualBrush RenderOptions.CachingHint="Cache">
<VisualBrush.Visual>
<Image Source="{Binding Source}" RenderOptions.BitmapScalingMode="Linear" />
</VisualBrush.Visual>
</VisualBrush>
</DiffuseMaterial.Brush>

And now it's lightning fast and silky smooth. There's a little loss in quality of the scaled bitmap but you'd have to be looking closely to spot it. And you'll be too busy admiring the smooth animation to do so.

Originally posted by Josh Twist on 21 April 2009 here.