SFML community forums

Help => Window => Topic started by: Fray on August 16, 2011, 10:05:09 pm

Title: Speed issues with Joystick event processing
Post by: Fray on August 16, 2011, 10:05:09 pm
Hello,

I'm still having this weird problem where joystick polling drastically slows down the game (huge fps drop, stuttering, etc). I'm using the basic while loop to poll events, nothing too fancy.
The problem disappears if I comment out the ProcessJoystickEvents() call in WindowImpl.cpp

Chances are it's a problem on my side, but I'm wondering if anyone had experienced the same issue and maybe knows how to resolve it.

Here's what I get after quickly profiling a minimal sfml app:
http://img703.imageshack.us/img703/9109/sleepyk.png

I'm using win7, lastest SFML2, VS2010

Thanks
Title: Speed issues with Joystick event processing
Post by: Laurent on August 17, 2011, 08:10:50 am
Is it slow even if you have no joystick connected?

Does it disappear if you put a "return false" at the beginning of JoystickImpl::IsConnected, in src\SFML\Window\Win32\JoystickImpl.cpp?
Title: Speed issues with Joystick event processing
Post by: Fray on August 17, 2011, 01:07:30 pm
Quote from: "Laurent"
Is it slow even if you have no joystick connected?

Does it disappear if you put a "return false" at the beginning of JoystickImpl::IsConnected, in src\SFML\Window\Win32\JoystickImpl.cpp?


It's slow even with no joystick connected, but the problem indeed desappears if JoystickImpl::IsConnected doesn't do the joyGetPosEx() stuff.

Cheers
Title: Speed issues with Joystick event processing
Post by: Laurent on August 17, 2011, 01:13:52 pm
Ok, thanks.

I really need to find another way of testing joysticks.
Title: Speed issues with Joystick event processing
Post by: mbro2k on October 15, 2011, 11:36:45 pm
I think I may have a related issue.

When I turn on my xbox gamepad in windows before starting
my game, the game runs choppy for about 15 seconds or so
and then goes back to normal for good.  

This only seems to happen during
the first time connecting the gamepad, and it only happens when
using a gamepad.

I'm using sfml2 from late september(0fd992d) and windows 7.

I think this wasn't happening in older versions of sfml2 I was using(49c0208).
Title: Speed issues with Joystick event processing
Post by: mbro2k on October 17, 2011, 12:06:27 am
I found a solution for my particular problem over in general

http://www.sfml-dev.org/forum/viewtopic.php?p=40190#40190
Title: Speed issues with Joystick event processing
Post by: novemberdobby on February 29, 2012, 02:28:39 pm
Sorry to bump an old topic, but I also had this problem and discovered a different solution.

I modified JoystickImpl::IsConnected to use XInput instead of JoyGetPosEx, and there's no longer any lag. I was getting huge spikes when triggering mouse move events on my SFML window, particularly when a gamepad was recently unplugged (even after restarting the program).


This probably only works for Xbox controllers as I haven't got anything else to test it with, but here's what I changed:

src\SFML\Window\Win32\JoystickImpl.cpp
Code: [Select]

#include <Xinput.h>
...
bool JoystickImpl::IsConnected(unsigned int index)
{
    //removed joyGetPosEx stuff

    XINPUT_STATE dummy;
    ZeroMemory(&dummy, sizeof(XINPUT_STATE));
    DWORD result = XInputGetState(index, &dummy);
    return result == ERROR_SUCCESS;
}


But it allows you to poll the connection state as often as you like with no slowdown.
Title: Speed issues with Joystick event processing
Post by: Laurent on February 29, 2012, 03:56:19 pm
Since XInput is only for XBox controllers, a solution using DirectInput would be better ;)
Title: Re: Speed issues with Joystick event processing
Post by: speps on April 17, 2012, 10:12:52 pm
After some weird slowdowns happening, I did run xperf on our game and I found out that 15% of CPU is spent polling for joysticks (which we do not support directly, we use XInput outside SFML).

Here is a proof screenshot :

(http://speps.fr/pub/dinput-sfml.png)

I was wondering if you could move the code for polling joystick to the raw input interface (sf::Input) and don't clobber the other event based path? This way, people wanting joystick input would suffer but other developers would not have to worry about this.

If I find something that works well, I could do a pull request.

Cheers,
Remi Gillig.
Title: Re: Speed issues with Joystick event processing
Post by: speps on April 18, 2012, 12:24:38 am
I did the source modification you recommended here [1]. Here is the pull request [2].

I started a discussion about the use of SetThreadAffinityMask there, I would like to know if this change could be possible in the future. It seems a big overhead for something which should be specific to only some games.

[1] http://en.sfml-dev.org/forums/index.php?topic=6079.0
[2] https://github.com/SFML/SFML/pull/210
Title: Re: Speed issues with Joystick event processing
Post by: Laurent on April 18, 2012, 08:17:17 am
Well, SetThreadAffinityMask is used everywhere (Ogre3D, Qt) and it doesn't seem to be a problem.
Title: Re: Speed issues with Joystick event processing
Post by: origon on April 20, 2012, 12:01:36 am
Hello,
I am also battling with very slow event processing, could be related.
SFML 2.0
The packaged examples (.exe's) (and my own programs) come to a halt when i move the mouse cursor continiously over the active window. It seems that the event queue is filling faster than it is being read. Same effect if i spam keystrokes.
(Windows - MinGW (Code::Blocks) (16.1 MB))
Windows 7

I tried running the examples at work, with no event problems. (XP)
Title: Re: Speed issues with Joystick event processing
Post by: speps on April 20, 2012, 11:37:18 am
Well, SetThreadAffinityMask is used everywhere (Ogre3D, Qt) and it doesn't seem to be a problem.

I'm only basing this on the profiling I did, in my patch for checking connection only every second, I use sf::Clock and this made "SetThreadAffinityMask" appear in the top functions at around 9% of total CPU time over the sampling period. So instead of having joyGetPosEx at 16%, it makes SetThreadAffinityMask jump to 9%. Maybe because it checks for every possible joystick and should only check it once, I will try to do a different match if I find time this weekend.
Title: Re: Speed issues with Joystick event processing
Post by: pdinklag on May 11, 2012, 09:26:24 am
I just hit into this problem, also using XBox controllers.
My event handler is virtually flooded with joystick move events, but apparently the polling itself has performance issues?

Is it possible to tell SFML to not listen for joystick move events? Polling the axis positions seems to work fine, and that's really all I need.

Furthermore, is it safe to poll / wait for events (in general) in another thread and add them to a queue to be processed in the main loop? That way I could filter joystick move events as a workaround. It might cause slight input lags, but at least the framerate doesn't drop.
Title: Re: Speed issues with Joystick event processing
Post by: Laurent on May 11, 2012, 09:58:40 am
Quote
My event handler is virtually flooded with joystick move events, but apparently the polling itself has performance issues?
This is not the same problem. The initial problem was just a performance issue, no extra events were triggered.

What do these events contain? Is it always the same position, almost the same, or a random one? Does it happen when you don't move the axis at all? Have you tried to play with setJoystickThreshold?
Title: Re: Speed issues with Joystick event processing
Post by: pdinklag on May 11, 2012, 10:30:41 am
Have you tried to play with setJoystickThreshold?
Whoops, that will probably be the issue. :-[ I get an event for every minor movement of a joystick axis, and it's probably because I did not define a treshold. Kinda forgot about this, thanks!
Title: Re: Speed issues with Joystick event processing
Post by: Laurent on May 11, 2012, 10:36:04 am
Quote
I get an event for every minor movement of a joystick axis, and it's probably because I did not define a treshold.
This is potentially an issue, because the default threshold (0.1) is supposed to be ok.
Title: Re: Speed issues with Joystick event processing
Post by: pdinklag on May 11, 2012, 01:46:37 pm
If I quickly move around the analog stick using the default treshold of 0.1, I usually get up to 3 events per frame, which is perfectly bearable. However, at times, I get way more than 100 per frame, and that's when it starts to hang badly (not a constantly low FPS, but sudden short freezes like every second or so).

I'll be observing this and see if I can find a possible cause. It mostly happened after other XBox controllers (I have 4) disconnected, maybe that's related somehow.
Title: Re: Speed issues with Joystick event processing
Post by: pdinklag on May 11, 2012, 02:26:32 pm
OK, I can definitely confirm it now.
It's all OK until controllers disconnect.

I waited for a while until they were all disconnected. Now I connected the first one again, and now the event handler seems to stop whenever I move the analog stick. The event handler (pollEvent) stops until I stop moving the sticks, as if it is locked. If I'm not moving the sticks, it all runs smoothly.

Altering the joystick treshold does not help here.

I'll try creating a minimal example in C++ soon.

Note that since it cannot be fixed until I restart my computer (simply re-plugging the adapter does not fix it), it might as well be an issue with the XBox adapter driver.
Title: Re: Speed issues with Joystick event processing
Post by: pdinklag on May 17, 2012, 11:10:10 am
The code below lets me reproduce the problem I had using XBox 360 controllers. It would be good if somebody could try it with others. I am not sure whether it can be reproduced with only one controller, but it's probably worth a try as well.

Steps to reproduce:

This keeps happening everytime you restart the application now until the system is restarted, so I guess it might be a driver issue related to the XBox 360 controller adapter.

This is the code in question:
#include <stdio.h>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

int main() {
        //Create window
        sf::RenderWindow window(sf::VideoMode(200, 150), "Joystick Test");
        window.setFramerateLimit(60);
       
        //Detect joysticks
        printf("Detecting joysticks... ");
       
        int numJoysticks = 0;
        while(sf::Joystick::isConnected(numJoysticks))
                numJoysticks++;

        printf("%d joystick(s) detected!\n", numJoysticks);
       
        //Blah
        sf::Text text("See console output...");
        text.setCharacterSize(16);
        text.setPosition(sf::Vector2f(5, 5));
       
        //Main loop
        printf("Listening for joystick move events...");
       
        sf::Clock clock;
       
        sf::Event event;
        while(window.isOpen()) {
                int numJoystickMoved = 0;
       
                while(window.pollEvent(event)) {
                        switch(event.type) {
                                case sf::Event::Closed:
                                        window.close();
                                        break;
                               
                                case sf::Event::JoystickMoved:
                                        numJoystickMoved++;
                                        break;
                       
                                default:
                                        break;
                        }
                }
               
                if(numJoystickMoved)
                        printf("Received %d joystick movement events this frame!\n", numJoystickMoved);
               
                float frameTime = clock.restart().asSeconds();
                if(frameTime > 0.25f)
                        printf("This frame took %0.2f seconds!\n", frameTime);
       
                window.clear();
                window.draw(text);
                window.display();
        }
}
 
Title: Re: Speed issues with Joystick event processing
Post by: Laurent on May 17, 2012, 03:34:04 pm
Have you tried the same steps with another program (game) that uses a joystick, to see whether it's specific to SFML or not?
Title: Re: Speed issues with Joystick event processing
Post by: pdinklag on May 17, 2012, 04:37:39 pm
I don't have too many Joystick related applications, but I just tried it in Project64 (Nintendo 64 emulator) and it does not have this problem, neither does the Windows gamepad calibration window.

After I ran those, the problem didn't occur in that SFML application again. Weird.  :-\
Title: Re: Speed issues with Joystick event processing
Post by: Laurent on May 17, 2012, 04:50:31 pm
Have you tried the latest sources?
Title: Re: Speed issues with Joystick event processing
Post by: pdinklag on May 17, 2012, 04:59:44 pm
The version I'm using now is from Saturday (May 12), I see you applied some changes on May 13. Talk about bad timing. :) Trying the latest version now...
Title: Re: Speed issues with Joystick event processing
Post by: pdinklag on May 17, 2012, 05:26:05 pm
Same setup, latest SFML snapshot.
sf::Joystick::isConnected(0) (and probably all other indices) always returns "false" for me now, even though the controller is connected.

However, the input events for it are received properly and I could not reproduce the problem I had before so far.
Title: Re: Speed issues with Joystick event processing
Post by: Laurent on May 17, 2012, 05:41:05 pm
Quote
sf::Joystick::isConnected(0) (and probably all other indices) always returns "false" for me now, even though the controller is connected.
There's an update delay now, so if you check directly at startup, the result will most likely be wrong.
Title: Re: Speed issues with Joystick event processing
Post by: pdinklag on May 17, 2012, 08:14:40 pm
There's an update delay now, so if you check directly at startup, the result will most likely be wrong.
Indeed, after a while (first few frames) I get a joystick conncetion event. ;)