SDL_GL_CreateContext how share a context between two windows

Hi All,
I’ve just started converting my code from 1.3 to 2. Thanks to a lot of tips on this forum I’ve got some test code up and running to render multiple screens.
However ( I’m using opengl ) I would like to share context between different screens, basically this is because whenever I load a model I need to do so twice.
eg
set context to window 1
load in model

set context to window 2
load in model again.

I can see when I produce a window I use SDL_GL_CreateContext to get a create a context, I then do this a second time for the second window. Is there a way I can rather than create do some sort of share context?

You can do multiple windows with SDL & OpenGL one of two ways. You can
make a new context for each window or you can use a single context for
multiple windows. Both ways use SDL_GL_MakeCurrent() to activate the
context you want GL calls to apply to. For a shared context, pass in the
same context with a different window pointer.

http://wiki.libsdl.org/SDL_GL_MakeCurrent

Jonny DOn Thu, Jan 9, 2014 at 12:58 PM, tony67 wrote:

Hi All,
I’ve just started converting my code from 1.3 to 2. Thanks to a lot of
tips on this forum I’ve got some test code up and running to render
multiple screens.
However ( I’m using opengl ) I would like to share context between
different screens, basically this is because whenever I load a model I need
to do so twice.
eg
set context to window 1
load in model

set context to window 2
load in model again.

I can see when I produce a window I use SDL_GL_CreateContext to get a
create a context, I then do this a second time for the second window. Is
there a way I can rather than create do some sort of share context?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi
Thanks for the answer, but I’m still insure on creating the context
Basically you creates window, the a context that points to that window.
When I use the create context function I pass it the window it’s linked to,
How can I share that context with a second window?
So basically I have
Window 1
Context 1 produced by create context function

Then

Window 2
Context 2 produced by creating context for window 2

Are you saying that if I use
make current window 2 , context 1
Draw model ( which was loaded with context set to screen 1)
Buffer screen 2

Then the model , textures and shaders will be applied to screen 2.
Rather than just the I textured inshdered veryexes?

Thanks for the help I just trying to get my head around the api

I’ve just started converting my code from 1.3 to 2. Thanks to a lot of
tips on this forum I’ve got some test code up and running to render
multiple screens.
However ( I’m using opengl ) I would like to share context between
different screens, basically this is because whenever I load a model I
need to do so twice.
eg
set context to window 1
load in model

set context to window 2
load in model again.

/* Don’t share the first context with any existing one. */
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 0);
win1 = SDL_CreateWindow(first screen window details go here);
ctx1 = SDL_GL_CreateContext(win1);

/* ctx1 is now current. Make the next one we create share with it. */
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
win2 = SDL_CreateWindow(second screen window details go here);
ctx2 = SDL_GL_CreateContext(win1);

/* ctx2 is now current, and shares various objects with ctx1. */

You’ll have to look into the details of context sharing to see exactly
what gets shared between the two. (textures, VBOs, etc).

Does that help?

–ryan.

Ryan, how does the explicit context sharing differ from switching windows
with one context? I presume you would still use SDL_GL_MakeCurrent to
choose which window you’re rendering to?

Jonny DOn Thu, Jan 9, 2014 at 3:31 PM, Ryan C. Gordon wrote:

I’ve just started converting my code from 1.3 to 2. Thanks to a lot of

tips on this forum I’ve got some test code up and running to render
multiple screens.
However ( I’m using opengl ) I would like to share context between
different screens, basically this is because whenever I load a model I
need to do so twice.
eg
set context to window 1
load in model

set context to window 2
load in model again.

/* Don’t share the first context with any existing one. */
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 0);
win1 = SDL_CreateWindow(first screen window details go here);
ctx1 = SDL_GL_CreateContext(win1);

/* ctx1 is now current. Make the next one we create share with it. */
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
win2 = SDL_CreateWindow(second screen window details go here);
ctx2 = SDL_GL_CreateContext(win1);

/* ctx2 is now current, and shares various objects with ctx1. */

You’ll have to look into the details of context sharing to see exactly
what gets shared between the two. (textures, VBOs, etc).

Does that help?

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Be aware that sharing of contexts can cause heavy CPU usage penalties due to the (cross-process) mutex stuff going on behind the scenes, the drivers (at least from NVIDIA and AMD - I have no data on
Intel at hand) are quite synchronous about this.

So using a separate load of the model may yield better performance, depending on your needs.On 01/09/2014 09:58 AM, tony67 wrote:

Hi All,
I’ve just started converting my code from 1.3 to 2. Thanks to a lot of tips on this forum I’ve got some test code up and running to render multiple screens.
However ( I’m using opengl ) I would like to share context between different screens, basically this is because whenever I load a model I need to do so twice.
eg
set context to window 1
load in model

set context to window 2
load in model again.

I can see when I produce a window I use SDL_GL_CreateContext to get a create a context, I then do this a second time for the second window. Is there a way I can rather than create do some sort of
share context?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


LordHavoc
Author of DarkPlaces Quake1 engine - http://icculus.org/twilight/darkplaces
Co-designer of Nexuiz - http://alientrap.org/nexuiz
"War does not prove who is right, it proves who is left." - Unknown
"Any sufficiently advanced technology is indistinguishable from a rigged demo." - James Klass
"A game is a series of interesting choices." - Sid Meier