Relying on thread_local variables bad?

Started by
8 comments, last by Chris_F 10 years ago
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.
Advertisement

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

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!

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.


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.

Sean Middleditch – Game Systems Engineer – Join my team!

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.

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


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

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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.

This topic is closed to new replies.

Advertisement