RenderToBitmap poor performance
#1
Hi,

RenderToBitmap takes a long time (~60 seconds consistently) to render a 2048x2048 image. This seems excessive as in the onscreen view I can render in realtime. 

Is this expected behaviour? If so, are there any workarounds for it?

I'm calling this routine:
public RenderTargetBitmap RenderToBitmap(Brush backgroundBrush = null, int dpi = 96);

Code:
     var viewport3D = new Viewport3D();

     viewport3D.Width = 2048;
     viewport3D.Height = 2048;

     var camera = new FirstPersonCamera();
     camera.Position = new Point3D(0, 0, 0);
     camera.Bank = 0;
     camera.Heading = 0;
     camera.Attitude = -90;
     camera.FieldOfView = 92;
     camera.TargetViewport3D = viewport3D;

     var displayModels = objs.AsModel3DGroup();

     foreach (var m in objs.Values)
     {
         viewport3D.Children.Add(m.CreateModelVisual3D());
     }

     camera.Refresh();

     Stopwatch renderStopWatch = new Stopwatch();
     renderStopWatch.Start();

     var img = camera.RenderToBitmap(backgroundBrush);    

     renderStopWatch.Stop();
#2
camera.RenderToBitmap method is using WPF's RenderTargetBitmap to render the 3D scene to a bitmap.  RenderTargetBitmap may use software rendering for this. This is also probably why it takes much longer than when the 3D scene is shown on the screen - there hardware rendering is used. This is pure WPF code so I cannot do much to improve performance.

If want better performance, then you will need to use use Ab3d.DXEngine. It uses a super fast DirectX 11 rendering engine that can render 3D scene that is defined by WPF 3D and Ab3d.PowerToys objects - so you do not need to change the code that defines your scene. With Ab3d.DXEngine you will be able to call DXViewportView.RenderToBitmap that is fully hardware accelerated and will work as fast as when shown on the screen. Ab3d.DXEngine also provides significantly improved visuals. See samples here: https://github.com/ab4d/Ab3d.DXEngine.Wpf.Samples
Andrej Benedik
#3
Thanks for the prompt response. 

Apologies if not clear in the previous message, but it's an offscreen render.

I have my on screen render with directx - which would explain why that is fast.

Can the offscreen render be wrapped with directx too?
#4
Yes, just call DXViewportView.RenderToBitmap instead of camera.RenderToBitmap - the later will use WPF's rendering.
Andrej Benedik
#5
Looks like it works perfectly, thank you!  (60seconds down to 0.1seconds).

Thanks!
  


Forum Jump:


Users browsing this thread:
1 Guest(s)