PDA

View Full Version : Multithreaded OpenGL questions.



MrKaktus
10-25-2011, 06:54 AM
Hi, in my engine I have implemented Task-Pooling system. Then Thread0 creates main OpenGL context and rest of worker threads creates worker OpenGL contexts that share resources with main one. I want to distribute OpenGL calls in such a way that main context will always perform drawing stuff and worker contexts will always perform edit/update tasks.

Now I have some questions that bothers me:

1) Can I set:
On Thread 0 - Main OpenGL Context - TexUnit0 - Texture N
and
On Thread 1 - Worker OpenGL Context - TexUnit0 - Texture M
at the same time and operate on both of them on two threads?

I'm curious about that because I am considering situation in which
thread 0 wants to perform draw and starts binding textures from tex
unit 0 through X for each sampler parameter of shader and at the same
time Thread 1 wants to start update of some other texture but uses
TexUnit0 binding as default (and we have collision).

In fact this "texture binding" is in my opinnin legacy stuff as in
this example TexUnits on Thread 0 will correspond to TexUnits in
gpu used by program but TexUnit used for texture update is completly
virtual as we only bind texture to update it's data in memory. So
in fact there should be a way to update textures without binding them
at all.

And such stuff shouldn't be required when only updating data:
glActiveTexture(texunit);
glBindTexture(type,texId);

2) What is a proper model of using sampler objects? Should I create for each sampler parameter of each program corresponding sampler object?
Or should I maintain cache of sampler objects and create new ones only when I won't find sampler with requested state in my cache? And otherwise reuse already existing sampler objects?

3) Do I need to Re-bind textures and samplers to texture units after each change of program if there is no change in texture usage? And there is another performance question coming from this:
If I have two programs that use textures in order: A,B and B,A is it better to change sampler parameters bindings or change textures and samplers bindings to opposite? I think in first situation there can be need for recompiling the shader?

I know that there is a lot of "if's" in this post but this are the things that really bothers me and I would apreciate some professional advice in this subject.

eile
10-25-2011, 07:23 AM
1) Can I set:
On Thread 0 - Main OpenGL Context - TexUnit0 - Texture N
and
On Thread 1 - Worker OpenGL Context - TexUnit0 - Texture M
at the same time and operate on both of them on two threads?


Yes, although the GPU will serialize the execution of the task. Each GL context has its own state and the driver will constantly switch between the two context and reload the state accordingly.

tksuoran
10-25-2011, 09:10 AM
Texture units are independent from Programs. So you can setup texture units and use multiple programs without touching texture units, if you are happy to use the same texture unit settings. In fact this is good thing to do if you can.

You should avoid resetting texture units for every program switch if you can. For example, you could establish convention for a few texture units: Texture unit 0 is always shadow map, and so on. You could reserve one texture unit for modification only purposes, in order to avoid conflict as described in your post.