Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Setting up Rendering In a Separate Thread.  (Read 2682 times)

0 Members and 2 Guests are viewing this topic.

thecurseofnor

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Setting up Rendering In a Separate Thread.
« on: May 19, 2012, 07:54:53 pm »
Hello. I'm experiencing some lag during my program's event loop when I move the mouse. So I was hoping to someone could direct me to a method for separating the event loop from the rendering code with a separate thread.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Setting up Rendering In a Separate Thread.
« Reply #1 on: May 19, 2012, 07:58:32 pm »
This shouldn't happen. How many mouse events do you receive per frame?
Laurent Gomila - SFML developer

thecurseofnor

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Setting up Rendering In a Separate Thread.
« Reply #2 on: May 19, 2012, 08:20:59 pm »
sf::Event::MouseMoved occurs  about 4-6 times a frame, If thats what you meant. Its a small lag. Hardly
noticeable, yet still noticeable. From what I've seen, the more events I check for in the event loop, whatever they may be, the more sf::Event::MouseMoved events I get. Is it something I'm doing wrong?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Setting up Rendering In a Separate Thread.
« Reply #3 on: May 19, 2012, 08:22:45 pm »
And what do you do when you receive these events?
Laurent Gomila - SFML developer

thecurseofnor

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Setting up Rendering In a Separate Thread.
« Reply #4 on: May 19, 2012, 08:38:50 pm »
example:
Code: [Select]
if((event.type == sf::Event::MouseButtonPressed) && (event.mouseButton.button == sf::Mouse::Left))
myclass.doThis = true;

I should also mention, I'm using SFML 2.0 RC.
And I didn't experience this in SFML 1.6.

But besides this, I'd still like to know how to set up a separate Render Thread.
« Last Edit: May 19, 2012, 08:40:30 pm by thecurseofnor »

thecurseofnor

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Setting up Rendering In a Separate Thread.
« Reply #5 on: May 19, 2012, 09:40:01 pm »
Well I've been reworking my event loop. And I've managed to reduce the amount of necessary event checks to a much smaller amount. And now the mouse move lag is undetectable. Though I'm still open to methods of setting up the rendering code in a separate thread. Currently the program crashes whenever I attempt it.


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Setting up Rendering In a Separate Thread.
« Reply #6 on: May 20, 2012, 09:25:59 am »
Quote
Currently the program crashes whenever I attempt it
Can you show what you tried?
Laurent Gomila - SFML developer

thecurseofnor

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Setting up Rendering In a Separate Thread.
« Reply #7 on: May 20, 2012, 07:17:47 pm »
This is what I tried putting into a thread;

void Render(void * data)
{
        sf::RenderWindow &window = *(static_cast<sf::RenderWindow*>(data));
       
        while(window.isDone())
        {
                // Render 3D
                glLoadIdentity();
                glTranslatef( 0, 0, -5 );

                glBegin(GL_QUADS);
                          glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f, -1.0f, 1.0f );
                          glTexCoord2f( 1.0f, 1.0f ); glVertex3f(  1.0f, -1.0f, 1.0f );
                          glTexCoord2f( 1.0f, 0.0f ); glVertex3f(  1.0f,  1.0f, 1.0f );
                          glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.0f,  1.0f, 1.0f );

                          glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, -1.0f );
                          glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f,  1.0f, -1.0f );
                          glTexCoord2f( 1.0f, 1.0f ); glVertex3f(  1.0f,  1.0f, -1.0f );
                          glTexCoord2f( 1.0f, 0.0f ); glVertex3f(  1.0f, -1.0f, -1.0f );

                          glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -1.0f,  1.0f, -1.0f );
                          glTexCoord2f( 1.0f, 0.0f ); glVertex3f( -1.0f,  1.0f,  1.0f );
                          glTexCoord2f( 0.0f, 0.0f ); glVertex3f(  1.0f,  1.0f,  1.0f );
                          glTexCoord2f( 0.0f, 1.0f ); glVertex3f(  1.0f,  1.0f, -1.0f );

                          glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f, -1.0f, -1.0f );
                          glTexCoord2f( 1.0f, 1.0f ); glVertex3f(  1.0f, -1.0f, -1.0f );
                          glTexCoord2f( 1.0f, 0.0f ); glVertex3f(  1.0f, -1.0f,  1.0f );
                          glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.0f, -1.0f,  1.0f );

                          glTexCoord2f( 0.0f, 0.0f ); glVertex3f( 1.0f, -1.0f, -1.0f );
                          glTexCoord2f( 0.0f, 1.0f ); glVertex3f( 1.0f,  1.0f, -1.0f );
                          glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 1.0f,  1.0f,  1.0f );
                          glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 1.0f, -1.0f,  1.0f );

                          glTexCoord2f( 1.0f, 0.0f ); glVertex3f( -1.0f, -1.0f, -1.0f );
                          glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.0f, -1.0f,  1.0f );
                          glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.0f,  1.0f,  1.0f );
                          glTexCoord2f( 1.0f, 1.0f ); glVertex3f( -1.0f,  1.0f, -1.0f );
                        glEnd( );

                glLoadIdentity();


               
                window.display();
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glMatrixMode(GL_MODELVIEW);
                glLoadIdentity();
        }
}

And the program crashed with the output:

[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
rthread: ../../src/xcb_io.c:273: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
Aborted
« Last Edit: May 20, 2012, 08:53:59 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Setting up Rendering In a Separate Thread.
« Reply #8 on: May 20, 2012, 08:55:52 pm »
Yep, XInitThreads (Linux only) must be called for multithreading to work. This will be fixed soon.

And you must deactivate the window in its initial thread (window.setActive(false)) before activating it in its new thread (window.setActive(true)).
Laurent Gomila - SFML developer

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: Setting up Rendering In a Separate Thread.
« Reply #9 on: May 21, 2012, 09:36:41 pm »
What is sf::RenderWindow::isDone() ? First time I see this and I can't find it in the documentation of sf::RenderWindow.
Want to play movies in your SFML application? Check out sfeMovie!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Setting up Rendering In a Separate Thread.
« Reply #10 on: May 21, 2012, 10:22:54 pm »
Probably isOpen().
Laurent Gomila - SFML developer

 

anything