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

Author Topic: OpenGL and Threading  (Read 2061 times)

0 Members and 1 Guest are viewing this topic.

nxzdr

  • Newbie
  • *
  • Posts: 5
    • MSN Messenger - nixzdr@gmail.com
    • View Profile
    • Email
OpenGL and Threading
« on: May 18, 2011, 09:22:48 am »
Alright so I've been playing around with SFML for a few days now, just for a bit of fun. I've decided to try to get it working with two threads, one to handle input, audio and the like, and the other to loop through a 3d scene and perform all the rendering tasks. The trouble is, I'm not sure if I'm getting this down correctly, presently I have a black filled window, that handles input and everything fine, but nothing from the rendering thread draws to the window. I've debugged and checked to make sure everything is running as I expect it to, so I'm guessing I've misinterpreted the docs on how to accomplish this. Here's what I've got thus far (just the important parts obviously):

Main Thread
App has a sf::Window as a member
Code: [Select]

App::Init()
{
    // Creates and sf::Window
    sf::VideoMode videoMode(iWidth, iHeight, 32);
    mWindow.Create(videoMode, "AppNameHere");
}

App::Loop()
{
    while(!quit)
    {
        mWindow.SetActive();

        // Process Events for close, resize ect here

        mWindow.Display();
    }

    mWindow.Close();
}


Render Thread
Renderer Class has a sf::Context as a member
Code: [Select]

Renderer::Init()
{
    // Set GL States and Projection Matrix
}

Renderer::Loop()
{
    while(!quit)
    {
        mContext.SetActive();

        // OpenGL Commands here

    }
}



This is what I gathered from searching about thus far. Any help would be greatly appreciated =]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
OpenGL and Threading
« Reply #1 on: May 18, 2011, 09:45:06 am »
You're drawing in your sf::Context, so you're not drawing in your window ;)

You must do this instead:

Code: [Select]
App::Loop()
{
    while(!quit)
    {
        // Process Events for close, resize ect here
    }

    mWindow.Close();
}

Renderer::Loop()
{
    // mWindow is a reference to the window which is created in the main thread
    mWindow.SetActive();
   
    while(!quit)
    {
        // OpenGL Commands here

        mwindow.Display();
    }
}
Laurent Gomila - SFML developer

nxzdr

  • Newbie
  • *
  • Posts: 5
    • MSN Messenger - nixzdr@gmail.com
    • View Profile
    • Email
OpenGL and Threading
« Reply #2 on: May 18, 2011, 11:43:10 am »
So I don't need the sf::Context at all then?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
OpenGL and Threading
« Reply #3 on: May 18, 2011, 12:08:09 pm »
Yup. You need a sf::Context only when you can't use another existing context (most likely a window).

And contexts only share their resources, so if you use a dummy context it can only be for stuff like loading images; if you draw in a dummy context you won't see anything.
Laurent Gomila - SFML developer

nxzdr

  • Newbie
  • *
  • Posts: 5
    • MSN Messenger - nixzdr@gmail.com
    • View Profile
    • Email
OpenGL and Threading
« Reply #4 on: May 18, 2011, 12:27:14 pm »
So if I made a loading thread to load and bind textures, that would need a context. But my renderer is fine with using the window pointer.

Cool, thanks for your help! =D

nxzdr

  • Newbie
  • *
  • Posts: 5
    • MSN Messenger - nixzdr@gmail.com
    • View Profile
    • Email
OpenGL and Threading
« Reply #5 on: May 18, 2011, 01:01:29 pm »
Hmm no luck, I made the change, and now it keeps signalling GL_INVALID_OPERATION for everything, which is due to no GLContext.

Any reason this might happen? I have the App class pass a pointer to the sf::Window it creates to the renderer just before the thread starts. then the rendering thread sets the window to active before calling any gl functions.

Is there anything I'm leaving out?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
OpenGL and Threading
« Reply #6 on: May 18, 2011, 01:05:44 pm »
You can activate a context/window in a thread only if it's not already active in other threads. So you must call mwindow.SetActive(false) in the main thread before launching the thread.
Laurent Gomila - SFML developer

nxzdr

  • Newbie
  • *
  • Posts: 5
    • MSN Messenger - nixzdr@gmail.com
    • View Profile
    • Email
OpenGL and Threading
« Reply #7 on: May 18, 2011, 01:15:36 pm »
Success! =D Thanks!

So I presume that the window sets itself as Active upon creation (which I probably should have thought about before =p)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
OpenGL and Threading
« Reply #8 on: May 18, 2011, 01:18:26 pm »
Quote
So I presume that the window sets itself as Active upon creation

Absolutely ;)
Laurent Gomila - SFML developer