Multiple context with different version
Asked Answered
C

1

2

I'm experimenting on list sharing among multiple OpenGL contextes. It is a great feature, since it allow me to execute parallel rendering threads.

But since I'm using CreateContextAttribs, I offer the possibility to request a specific OpenGL implementation. So, it can happen the some context is implementing version 3.2+ while the other is implementing version 2.1.

Actually works quite fine, but I suspect that this modus operandi hides some side effect. What would be a list of problems which can occour while using contextes having different versions?

Beyond this, I query the implemented extentions for each context version, since I suppose that different versions can support different extension, is this right? And what about function pointers? I have to requery them for each context with different version (indeed, pointers changes depending on versions)?

Chaunceychaunt answered 21/9, 2011 at 5:47 Comment(0)
E
8

It is a great feature, since it allow me to execute parallel rendering threads.

Accessing the GPU from multiple threads in parallel is a serious performance killer. Don't do it. The GPU will parallelize any rendering internally. Anything else you do, is throwing logs into its wheels.

If you want to speed up asset uploads, look into buffer objects and asynchronous access. But stay away from doing multiple OpenGL contexts in separate threads at the same time.


But since I'm using CreateContextAttribs, I offer the possibility to request a specific OpenGL implementation. So, it can happen the some context is implementing version 3.2+ while the other is implementing version 2.1.

Actually works quite fine, but I suspect that this modus operandi hides some side effect. What would be a list of problems which can occour while using contextes having different versions?

This is actually a very good question. And the specification answers it clearly:

1) Can different GL context versions share data?

    PROPOSED: Yes, with restrictions as defined by the supported feature
    sets. For example, program and shader objects cannot be shared with
    OpenGL 1.x contexts, which do not support them.

    NOTE: When the new object model is introduced, sharing must be
    established at creation time, since the object handle namespace is
    also shared. wglShareLists would therefore fail if either context
    parameter to it were to be a context supporting the new object
    model.

Beyond this, I query the implemented extentions for each context version, since I suppose that different versions can support different extension, is this right?

Indeed querying the set of supported extensions for each context is the right thing to do.

And what about function pointers? I have to requery them for each context with different version (indeed, pointers changes depending on versions)?

On Windows extension function pointers are tied to the context. The sane way to do this is having some

typedef struct OpenGLExtFunctions_S {
    GLvoid (*glFoobarEXT)(GLenum, ...);
    /* OpenGL function pointers */
} OpenGLExtFunctions;

/* currentContextFunction must be thread loacal since
   contexts are active in one thread only */
__declspec(thread) OpenGLExtFunctions *currentContextFunctions;

#define glFoobarEXT (currentContextFunctions->glFoobarEXT);
#define ...

And wrap wglMakeCurrent and wglMakeContextCurrent with some helper function that sets the currentContextFunctions pointer to the one of the context being made current. Extension wrapper libraries like GLEW do all this grunt work for you, so you don't have to bother doing it yourself.

On X11/GLX things are much simpler: The function pointers returned by glXGetProcAddress must be the same for all contexts, so no need to switch them.

Easton answered 21/9, 2011 at 7:28 Comment(6)
Thank you for your critics, but I found that shaders compilation and texture uploading in separate threads actually performs "faster" (i.e. Don't affect FPS of then actual rendering). And... What about the real question?Chaunceychaunt
@Chaunceychaunt Piccioni: Shader Compilation and texture uploads, those are the only two things that actually make sense doing in a separate thread, since neither of them actually throttles the GPU. Shaders are compiled by the CPU, texture uploads are a two fold process, first upload to a buffer in CPU memory, then delayed upload to GPU memory.Easton
I've experimented on this. My question come from the following situation: a shader generated by a 2.1 context, can be executed by a context having a different version? Can I mix different shader objects coming from different contextes having other version? Apart this cases, the question was posted in a more general way.Chaunceychaunt
@Chaunceychaunt Piccioni: I answered your other questions as well. See my edit.Easton
Thanks, but I do not use GLEW since I'm developing my own project on .NET platform.Chaunceychaunt
@LucaPiccioni: Function pointers not just depend on the pixel format, but also the GPU type the context is created on. Technically on Windows you can make no assumptions about extension function pointer identity, other than knowing that they may differ between contexts.Easton

© 2022 - 2024 — McMap. All rights reserved.