• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ubik

Relying on thread_local variables bad?

9 posts in this topic

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.
0

Share this post


Link to post
Share on other sites

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...

0

Share this post


Link to post
Share on other sites

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
2

Share this post


Link to post
Share on other sites


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.

1

Share this post


Link to post
Share on other sites

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.
0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

 


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
1

Share this post


Link to post
Share on other sites

 

 

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.

0

Share this post


Link to post
Share on other sites

 

 

 

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0