Jump to content

  • Log In with Google      Sign In   
  • Create Account

Relying on thread_local variables bad?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Ubik   Members   -  Reputation: 698

Like
0Likes
Like

Posted 29 March 2014 - 04:46 PM

I have my personal hobby probl^H^H^H^H^Hproject of making a convenience wrapper for OpenGL in C++. Posting in this subforum, however, because this isn't all that much about OpenGL itself.
 
First, usually OpenGL code looks a bit like this:
glBindBuffer(bufferId);
glBufferData(pointerToData);

glBindBuffer(anotherBufferId);
glBufferData(pointerToMoreData);

glBindBuffer(bufferId);
glDrawElements(/* element type, count and such here */); // Drawing using the last bound object

My thought was to make that look more like this:
buffer.data(pointerToData);
buffer.use();
anotherBuffer.data(pointerToMoreData);
context.drawElements(/* glDraw* parameters here */); // Draws using buffer, not anotherBuffer
The actual OpenGL bind commands have been moved behind the scenes and instead there is a use() method that marks an object to be used when drawing. glBind* should only be called when necessary. This would require some state tracking made by myself, by tracking what's "bound for drawing" and what's actually bound at the moment. The OpenGL context that the gl* functions operate on is very much a thread local concept itself, so it would make sense to have a hidden (from library user's perspective) state tracker object. Using thread_local to store a pointer to my own state tracker seems very obvious choice.

The thread local pointer to the tracker would have to be used pretty much once for every OpenGL call that manipulates GL state. Does this seem like a bad idea to you?

---------------------

After some rubber ducking (partly whily trying to figure out how to put out things in this post), I've started to think maybe it's after all better to make the user call bind explicitly, and then just validate the calls with the thread-local state. The validation could be disabled in release mode as the program doesn't actually rely on it - well, except maybe for skipping unnecessary bind calls. (Sort of annoyingly shaders in OpenGL work like in the use() example above, though.)
buffer.bind();
anotherBuffer.data(pointerToData); // Throws or does some other terrible thing when validation is on!
I wish there was a compile time way to enforce ordering of calls that's not terribly cludgy, but I guess runtime validation has to do.

The Object initialization design issue thread has been going over at least tangentially related issues, so hopefully some of that interest and insight could leak into this thread too.

Sponsor:

#2 Javier Meseguer de Paz   Members   -  Reputation: 371

Like
0Likes
Like

Posted 29 March 2014 - 06:44 PM

I am sorry this is not really an answer but just a further question: do you intend your wrapper to be called from different threads? 

 

Because you can certainly call OpenGL from multiple threads but since an OpenGL context is thread-locked and thus you need several contexts (one per thread) sharing display lists data, etc, yet they do not share state... It's a bit tricky...


“We should forget about small efficiencies, say about 97% of the time; premature optimization is the root of all evil” -  Donald E. Knuth, Structured Programming with go to Statements

 

"First you learn the value of abstraction, then you learn the cost of abstraction, then you're ready to engineer" - Ken Beck, Twitter


#3 Hodgman   Moderators   -  Reputation: 31963

Like
3Likes
Like

Posted 29 March 2014 - 07:43 PM

I would put the state caching into the context object, and have the usage as:
context.use(bufferB, slot);
context.data(bufferA, pointerToData);
context.draw...//will bind bufferB to VB# 'slot'
OpenGL is the only API that uses the silly bind-to-edit pattern, so if you ever want to port your wrapper, this kind of client code would just work without any state caching.
If you have no plans to ever port it and you keep it as a very transparent GL-helper library, then having the user re-call 'use' themselves after they've edited a buffer might be ok... Otherwise, I would try to completely shield the user from the fact that bind-to-edit is occurring behind the scenes.


As for the original question, thread local storage is fairly cheap on x86 an x64 - similar to having a pointer to a pointer to your variable. On other CPU architectures, there may be significant cost involved. I would still choose to just put this data into the context object though!

#4 Chris_F   Members   -  Reputation: 2467

Like
2Likes
Like

Posted 29 March 2014 - 09:48 PM


First, usually OpenGL code looks a bit like this:

glBindBuffer(bufferId);
glBufferData(pointerToData);

glBindBuffer(anotherBufferId);
glBufferData(pointerToMoreData);

glBindBuffer(bufferId);
glDrawElements(/* element type, count and such here */); // Drawing using the last bound object

 

You can make use of common extensions refereed to as Direct State Access to avoid most of this bind-to-edit behavior. For example, instead of glBufferData you can use glNamedBufferDataEXT.


Edited by Chris_F, 29 March 2014 - 09:49 PM.


#5 SeanMiddleditch   Members   -  Reputation: 7261

Like
1Likes
Like

Posted 30 March 2014 - 04:20 AM


You can make use of common extensions refereed to as Direct State Access to avoid most of this bind-to-edit behavior. For example, instead of glBufferData you can use glNamedBufferDataEXT.

 

Not in practice. Not all drivers support DSA.  Relying on GL extensions or even new versions is risky business.



#6 Ubik   Members   -  Reputation: 698

Like
0Likes
Like

Posted 30 March 2014 - 05:39 AM

I am sorry this is not really an answer but just a further question: do you intend your wrapper to be called from different threads?

Yeah, the idea has been to support multiple contexts and sharing resources between them.  That's actually what led me to think about using thread local storage to keep track of the current context. Even if I didn't actually end up using it that way right away, I think being able to have multiple context support easily workable as a sign of "healthy" design.
 

I would put the state caching into the context object, and have the usage as:

context.use(bufferB, slot);
context.data(bufferA, pointerToData);
context.draw...//will bind bufferB to VB# 'slot'
OpenGL is the only API that uses the silly bind-to-edit pattern, so if you ever want to port your wrapper, this kind of client code would just work without any state caching.
If you have no plans to ever port it and you keep it as a very transparent GL-helper library, then having the user re-call 'use' themselves after they've edited a buffer might be ok... Otherwise, I would try to completely shield the user from the fact that bind-to-edit is occurring behind the scenes.


As for the original question, thread local storage is fairly cheap on x86 an x64 - similar to having a pointer to a pointer to your variable. On other CPU architectures, there may be significant cost involved. I would still choose to just put this data into the context object though!

Thanks for affirmation of the use() concept. smile.png I have somehow been bent into thinking about explicit context having to be like buffer.data(context, pointerToData) to avoid context exploding in size. Then again, a buffer doesn't do things with context, it's the other way around, and the number of methods might not be that big after all. (For a while, I thought about keeping a pointer into the context in the resource handle class alongside the shared_ptr to the actual resource, but having the handles be context-specific felt really smelly. More so than TLS.)

Thanks also for the TLS info! I'd like to keep things portable-ish, again as a healthy design thing. Aside performance concerns, I've also been thinking about the implicitness of not actively having the context in the code where it is in fact used, so I do agree it's better to have the context around.
 

You can make use of common extensions refereed to as Direct State Access to avoid most of this bind-to-edit behavior. For example, instead of glBufferData you can use glNamedBufferDataEXT.

 
Not in practice. Not all drivers support DSA.  Relying on GL extensions or even new versions is risky business.

I'm on Linux, and while people probably have the closed source drivers installed (as do I), I'm currently targeting OpenGL 3.3 as it is what the open drivers support at the moment as far as I know. Therefore avoiding stuff outside 3.3 core. Optionally supporting the bindless and other more advanced behavior seems interesting, but probably looking into them at a lot later phase than where I'm now.

#7 L. Spiro   Crossbones+   -  Reputation: 14423

Like
1Likes
Like

Posted 30 March 2014 - 04:52 PM

an OpenGL context is thread-locked and thus you need several contexts (one per thread)

OpenGL contexts are not locked to any specific thread. A single OpenGL context can be used across multiple threads, but only on one thread at a time.


As for the original question, thread local storage is fairly cheap on x86 an x64 - similar to having a pointer to a pointer to your variable. On other CPU architectures, there may be significant cost involved.

Just to add one more confirmed platform to the list, it is also extremely cheap on iOS, and we do use it with our OpenGL ES 2.0 code in-house.


L. Spiro

Edited by L. Spiro, 30 March 2014 - 04:54 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#8 Chris_F   Members   -  Reputation: 2467

Like
1Likes
Like

Posted 30 March 2014 - 05:17 PM

 


You can make use of common extensions refereed to as Direct State Access to avoid most of this bind-to-edit behavior. For example, instead of glBufferData you can use glNamedBufferDataEXT.

 

Not in practice. Not all drivers support DSA.  Relying on GL extensions or even new versions is risky business.

 

 

Nvidia and AMD have both supported these for a very long time. If need be you could even implement your own versions as fallbacks as is described here:

 

http://www.gdcvault.com/play/1017850/

http://adrienb.fr/blog/wp-content/uploads/2013/04/PortingSourceToLinux.pdf


Edited by Chris_F, 30 March 2014 - 05:30 PM.


#9 mhagain   Crossbones+   -  Reputation: 8285

Like
0Likes
Like

Posted 31 March 2014 - 03:06 AM

 

 

Nvidia and AMD have both supported these for a very long time. If need be you could even implement your own versions as fallbacks as is described here:

 

http://www.gdcvault.com/play/1017850/

http://adrienb.fr/blog/wp-content/uploads/2013/04/PortingSourceToLinux.pdf

 

 

Intel are the only vendor who didn't support it last time I checked.  Both id and Valve use DSA so it seems safe to assume that support is otherwise (1) widespread, and (2) robust.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#10 Chris_F   Members   -  Reputation: 2467

Like
0Likes
Like

Posted 31 March 2014 - 04:37 AM

 

 

 

Nvidia and AMD have both supported these for a very long time. If need be you could even implement your own versions as fallbacks as is described here:

 

http://www.gdcvault.com/play/1017850/

http://adrienb.fr/blog/wp-content/uploads/2013/04/PortingSourceToLinux.pdf

 

 

Intel are the only vendor who didn't support it last time I checked.  Both id and Valve use DSA so it seems safe to assume that support is otherwise (1) widespread, and (2) robust.

 

 

I get the feeling that Intel has no plans on supporting DSA, which is incredibly annoying to me. That would be OK if Intel GPUs were still garbage, but they are actually getting decently fast and are close to supporting the latest version of OpenGL. Now would be a good time for them to implement it.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS