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

Author Topic: Using threads to load resources for OpenGL  (Read 4431 times)

0 Members and 1 Guest are viewing this topic.

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Using threads to load resources for OpenGL
« on: October 15, 2011, 06:36:09 pm »
I am trying to get this code to work, but having no luck...

http://www.sfml-dev.org/wiki/en/sources/loadimagesinthread.cpp

but says it can't activate the window? IIRC....

I am not sure, but almost thinking it may be easier to have the thread load the data and when back in the main thread just upload the data to OpenGL then?

Reason for that thinking is I need one thread to load resources into OpenGL, and the main thread will still need to display things like GUI, splash screen ect... So is this even possible? As I recall you can only have one OpenGL RC active or available?

Thanks!

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: Using threads to load resources for OpenGL
« Reply #1 on: October 16, 2011, 11:30:46 am »
Quote from: "Mars_999"
I am trying to get this code to work, but having no luck...

http://www.sfml-dev.org/wiki/en/sources/loadimagesinthread.cpp

Not sure if this sample should be taken as a reference, it contains more synchronization than necessary, thus it's not very efficient. It may be a good start though.

Quote from: "Mars_999"
but says it can't activate the window? IIRC....

Uh..?

Quote from: "Mars_999"
I am not sure, but almost thinking it may be easier to have the thread load the data and when back in the main thread just upload the data to OpenGL then?

What's the purpose of the second thread if you want to do things in a sequential way ?

Quote from: "Mars_999"
Reason for that thinking is I need one thread to load resources into OpenGL, and the main thread will still need to display things like GUI, splash screen ect... So is this even possible? As I recall you can only have one OpenGL RC active or available?

RC = ?

You can have one OpenGL context active per thread. Thus several active context at the same time in different threads. Plus, SFML enables sharing between these contexts, which means that what you load in one context is available in the other contexts too (with some constraints). If you use that sharing feature (rather than activating the main window's context as in your sample and needing useless synchronization), make sure to flush (glFlush) your thread's context. Otherwise the images you load in that thread may not immediately be available in the main thread's context.

And to answer your question, what you want to do IS possible.
Want to play movies in your SFML application? Check out sfeMovie!

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Using threads to load resources for OpenGL
« Reply #2 on: October 16, 2011, 02:59:02 pm »
Thanks for the reply.

Yeah after looking at the example again I was thinking, not sure this is a great example to learn from....

RC = rendering context

All I am trying to do is, start a second thread and have that thread load up textures and put them into OpenGL, where the main thread is moving on and e.g. allows user to interface with the main menu ect... until the resources are loaded.

So am I reading you right? You can have a second window or context in the second thread do all the loading in that one and then when done just call glFlush()? to upload the texture data? Just make sure I call that window active for the second thread and leave the main window active also for the main thread? Because the main thread will be rendering OpenGL commands while this second thread is loading resources.

Thanks!

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Using threads to load resources for OpenGL
« Reply #3 on: October 16, 2011, 07:51:53 pm »
Quote from: "Mars_999"
So am I reading you right? You can have a second window or context in the second thread do all the loading in that one and then when done just call glFlush()? to upload the texture data? Just make sure I call that window active for the second thread and leave the main window active also for the main thread? Because the main thread will be rendering OpenGL commands while this second thread is loading resources.


Yup, at least with SFML (because the contexts are shared). Actually glFlush() only ensures that the textures you uploaded in one context are also available to the other shared contexts.

And you don't need a window to have an OpenGL context. You can create one manually by creating a sf::Context object, or let sf::Texture create one for you the first time you do a sf::Texture::LoadFrom....() call.
Want to play movies in your SFML application? Check out sfeMovie!

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Using threads to load resources for OpenGL
« Reply #4 on: October 16, 2011, 09:05:41 pm »
You should have a look at the usage example of sf::Thread : http://www.sfml-dev.org/documentation/2.0/classsf_1_1Thread.php

Edit: uh.. deleted post?
Want to play movies in your SFML application? Check out sfeMovie!

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Using threads to load resources for OpenGL
« Reply #5 on: October 16, 2011, 09:43:59 pm »
Quote from: "Ceylo"
You should have a look at the usage example of sf::Thread : http://www.sfml-dev.org/documentation/2.0/classsf_1_1Thread.php

Edit: uh.. deleted post?


Yeah I think I got it working! SO far so good I think. The only thing is file accessing for my error manager. Each thread that has an error, gets dumped and I would like to save these.

As of now I think which ever thread gets the file stream first dumps and the other threads miss out on writing data. I tried loading the same texture to test and make sure it wouldn't get loaded, and it doesn't but I wanted to dump a message about that texture has already been loaded. I only got one message which I should have had 4.

Thanks! for all the help

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Using threads to load resources for OpenGL
« Reply #6 on: October 16, 2011, 09:47:34 pm »
Probably because of concurrent writing. Thus you'd need something like:
Code: [Select]

void write(msg)
{
    sf::Lock l(writingMutex); // exclusive lock to prevent concurrent writing
    open log file
    append msg to log file
    close log file
}
Want to play movies in your SFML application? Check out sfeMovie!

 

anything