0
\$\begingroup\$

Here is the SlimDX version:

 var form = new SlimDX.Windows.RenderForm("Test"); form.Width = 1280; form.Height = 1024; SlimDX.Direct3D9.PresentParameters presentParams = new SlimDX.Direct3D9.PresentParameters { BackBufferWidth = form.Width, BackBufferHeight = form.Height, DeviceWindowHandle = form.Handle, PresentFlags = SlimDX.Direct3D9.PresentFlags.None, Multisample = SlimDX.Direct3D9.MultisampleType.None, BackBufferCount = 0, PresentationInterval = SlimDX.Direct3D9.PresentInterval.Immediate, SwapEffect = SlimDX.Direct3D9.SwapEffect.Flip, BackBufferFormat = SlimDX.Direct3D9.Format.X8R8G8B8, Windowed = true, }; var device = new SlimDX.Direct3D9.Device(new SlimDX.Direct3D9.Direct3D(), 0, SlimDX.Direct3D9.DeviceType.Hardware, form.Handle, SlimDX.Direct3D9.CreateFlags.HardwareVertexProcessing, presentParams); device.Viewport = new SlimDX.Direct3D9.Viewport(0, 0, form.Width, form.Height); SlimDX.Direct3D9.Sprite sprite; sprite = new SlimDX.Direct3D9.Sprite(device); SlimDX.Windows.MessagePump.Run(form, () => { sprite.Begin(SlimDX.Direct3D9.SpriteFlags.None); device.BeginScene(); Stoptimer = Stopwatch.StartNew(); Stoptimer.Start(); var tx = SlimDX.Direct3D9.Texture.FromStream(device, ms, 0, 0, 0, SlimDX.Direct3D9.Usage.None, SlimDX.Direct3D9.Format.X8R8G8B8, SlimDX.Direct3D9.Pool.Managed, SlimDX.Direct3D9.Filter.None, SlimDX.Direct3D9.Filter.None, 0); Stoptimer.Stop(); System.Console.WriteLine(Stoptimer.ElapsedMilliseconds.ToString()); sprite.Draw(tx, Color.Transparent); sprite.End(); tx.Dispose(); device.EndScene(); device.Present(); }); 

This is very slow at higher resolutions and also not that fast at lower ones. I don't quite understand why, as I thought, it would be faster since it uses the GPU.

And here is the drawing version:

newImage = Image.FromStream(ms); gmp.DrawImage(newImage, 0, 0); 

It's just that inside a loop (SlimDX has its own loop inside the message pump).

Can you tell me why drawing is faster? From my understanding, drawing with Panels and such is slow, as it's made for complete compatibility and isn't optimized for hardware acceleration.

And this is supposed to redraw constantly as a new image will arrive all the time (ms is from a TCP Stream then put in a memorystream when received).

I basically ask for an analysis of the code.

\$\endgroup\$
3
  • \$\begingroup\$ Have you tried to profile the code? Is the slow part actually where you expect it? \$\endgroup\$ Commented Aug 18, 2013 at 12:16
  • \$\begingroup\$ I have tried using stopwatch at many places, and the slowest thing is the Texture From Stream. I even checked if it was the stream itself (tried imagefrom stream just to check it alone), and that was super fast compared to the Texture conversion:S \$\endgroup\$ Commented Aug 18, 2013 at 22:11
  • \$\begingroup\$ Not a performance tip (you really should be profiling first to help us here), but I'd recommend wrapping your tx variable in a using statement to ensure it gets properly disposed even when an exception occurs. \$\endgroup\$ Commented Sep 9, 2014 at 9:15

1 Answer 1

3
\$\begingroup\$

You seem to be timing this code:

 var tx = SlimDX.Direct3D9.Texture.FromStream(device, ms, 0, 0, 0, SlimDX.Direct3D9.Usage.None, SlimDX.Direct3D9.Format.X8R8G8B8, SlimDX.Direct3D9.Pool.Managed, SlimDX.Direct3D9.Filter.None, SlimDX.Direct3D9.Filter.None, 0); 

This code is loading a texture not rendering the scene. So to fix:

  1. Move the texture loading outside of the message pump (textures should be loaded once).
  2. Time your draw call sprite.Draw(tx, Color.Transparent); instead.
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.