Sign in to follow this  
rjbishop

[SOLVED] (SlimDX) Mouse disrupts DirectX Panel, High CPU

Recommended Posts

All, I have an audio based program written in C# and use SlimDX for DirectX interfacing. I have a standard Panel that is being used to draw spectrum analysis type of data at high speed, using DirectX. The DirectX drawing is working no problem- I'm doing around 48 fps, and keeping up with the FFT being done on the audio data no problem. But as soon as my mouse pointer enters any area of the Panel, CPU spikes up to 75%, and my audio gets very choppy and sounds cyborg-like. What could be causing this? I don't process any mouse messages, and have disabled OnPaint and OnBackgroundPaint messages. I'm a bit of a newbie with DirectX, so perhaps there is something that I am missing. Thanks for any input! [Edited by - rjbishop on October 11, 2008 12:24:02 AM]

Share this post


Link to post
Share on other sites
The first thing to do is run a profiler in the two different cases. It should give you a better idea of where the CPU usage is coming from.

Your audio stuttering is probably happening because of the heavy CPU usage stopping you from updating your streaming buffer quick enough. Making the buffer somewhat bigger should help there. I'd also suggest putting the audio update inside a higher priority thread than the rendering.

My first guess would be that hiding the mouse will fix the high CPU usage. If that fixes it then simply draw your own mouse pointer if you need one.

I'd also make sure all your drivers are up to date, just to rule out any problems there.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike.Popoloski
That sounds odd indeed. You aren't using DirectInput or RawInput, and aren't processing mouse messages?


Mike- no, not using DirectInput, RawInput, or even DirectSound. Only using the Graphic component of DirectX.

Below is some of the pertinant code:

Here is the Class for my form, where objects are defined:


public class MainForm
{
...
...
private System.Windows.Forms.Panel panel2;
// SlimDX objects
static Device Device;
static Direct3D direct3D;
static Line spectrumLine;
static Line midPointLine;
static Vector2[] spectrumVector2;
static Vector2[] midPointVector2;
static Color4 spectrumColor;
static Color4 midPointColor;
static Sprite mySprite;
static Texture myNTexture;
static ImageInformation imageInformation;
...
...
}

InitializeComponent()
{
this.panel2 = new System.Windows.Forms.Panel();
this.panel2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.panel2.CausesValidation = false;
this.panel2.Controls.Add(this.panel3);
this.panel2.Controls.Add(this.testTrans);
this.panel2.Enabled = false;
this.panel2.Location = new System.Drawing.Point(10, 286);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(1110, 280);
this.panel2.TabIndex = 27;

//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.ActiveBorder;
this.ClientSize = new System.Drawing.Size(1124, 566);
this.Controls.Add(this.panel2);
...
...
}


MainFormLoad()
{

....
....
direct3D = new Direct3D();
PresentParameters presentParams = new PresentParameters();
presentParams.BackBufferHeight = panelHeight;
presentParams.BackBufferWidth = panelWidth;
presentParams.DeviceWindowHandle = panel2.Handle;
Device = new Device(direct3D, 0, DeviceType.Hardware, this.Handle, CreateFlags.HardwareVertexProcessing, presentParams);
spectrumColor = new Color4(Color.Green);
midPointColor = new Color4(Color.Red);
spectrumLine = new Line(Device);
midPointLine = new Line(Device);
spectrumVector2 = new Vector2[panelWidth * 3];
lastVector = new long[panelWidth];
midPointVector2 = new Vector2[2];
// Now for the background image for the Spectrum
mySprite = new Sprite(Device);
imageInformation = new ImageInformation();
myNTexture = Texture.FromFile(Device, "Spectrum_BackgroundImage.PNG", D3DX.DefaultNonPowerOf2,
D3DX.DefaultNonPowerOf2, 1, Usage.None, Format.Unknown, Pool.Managed,
SlimDX.Direct3D9.Filter.None, SlimDX.Direct3D9.Filter.None, 0,
out imageInformation);

...
...

...Next, the code in my draw routine, which happens 48 times/second (roughly)

DrawSpectrum()
{
...
...
// Now process the bins and ready for presentation
for (int i = 0; i < formWidth; i++)
{
...
...
// 3rd seg is to move to the right one pixel
spectrumVector2[i * 3] = new Vector2(i, formHeight - j);
spectrumVector2[i * 3 + 1] = new Vector2(i, formHeight);
spectrumVector2[i * 3 + 2] = new Vector2(i + 1, formHeight);
}
// Call DirectX and render
// Clear the screen
Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.LightYellow, 1.0f, 0);
// Begin DirectX drawing
Device.BeginScene();
// Draw the background image
mySprite.Begin(SpriteFlags.None);
mySprite.Draw(myNTexture, Color.White);
mySprite.End();
// Draw the spectrum
spectrumLine.Begin();
spectrumLine.Draw(spectrumVector2, spectrumColor);
spectrumLine.End();
// End DirectX drawing
Device.EndScene();
Device.Present();
}



There you go- thats it in a nutshell.

Thanks for taking a quick look at this. Hope it's not too much.

BTW- I am using C# Express 2005, DirectX 9.0c, SlimDX Sept 08 release, MAudio Delta44 Soundcard with XP SP2 ASIO Drivers, Windows XP SP3, and fairly recent ATI Radeon Drivers.

-Reid-

[Edited by - rjbishop on October 11, 2008 12:30:55 AM]

Share this post


Link to post
Share on other sites
There doesn't seem to be anything blatantly wrong from what you've posted. If you'd like, you could send me your project and I'll do a little debugging from my end, or at least confirm that I experience the same behavior. (Email should be in my profile).

Also, for the future, when you post code on these forums use the [ source ] [ /source ] tags to make things easy to read.

Share this post


Link to post
Share on other sites
Mike,

I found the problem! I was preparing a test project that didn't involve any of the FFT sound code- just the DirectX rendering. Sure enough, the problem was still evident, but I did notice that if I moved my mouse just a teeny weeny bit, sometimes the CPU would drop back down to normal levels. Sometimes high CPU, sometimes not- it seemed to be random and depended on a small mouse nudge. I also noticed that sometimes, I would see a "double" mouse pointer.

Aha, I said- I wonder if the fact that I have my mouse configured to display "pointer trails" is somehow causing this? So I went in to Control Panel, turned off Pointer Trails, and now all is well! No additional load on the CPU whatsoever, no matter what I do with the mouse.

So lessons learned... if you have your mouse configured for Pointer Trails, expect that you will put significant load on the CPU if your pointer happens to be over a screen that is having high speed graphics drawing being performed.

Thanks for your input!


Quote:
Original post by Mike.Popoloski
Also, for the future, when you post code on these forums use the [ source ] [ /source ] tags to make things easy to read.


Thanks for the tip- I actually tried to use what I am used to - the [ code ] [ /code ] tags. Evidently it didn't quite work out.

Happy once again. Oh, and I just LOVE SlimDX- it makes working with DirectX a lot easier to work with. I sure wish that the Line class could be ported to DirectX10 however... I'm going to miss that once I'm forced to move off DirectX9.



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this