OpenGL Context Sharing in the Cocoa version?


#1

Is this implemented yet? My own OpenGL code is working, and it uses CGL mainly - AGL only where I needed to match with juce & carbon, but now I see that you chose NSOpenGLContext as the native format.

That seems to be a wrapper around the more (non cocoa) friendly CGLContextObject which is what I picked to work with a CGL created full screen context.

Are there options here? Could the share use CGL, so it’s not quite such a cocoa lock-in?

Here’s the situation. My shared context inherits from OpenGLContext so it can share, but it does it’s own context creation, ending up with an AGL and CGLContext. Another class is a subclass of OpenGLComponent. It, and the fullscreen CGL class, need to share the context.

So, it seems like I’ll have to change my shared context to make an offscreen NSOpenGLContext then? From which I could get a CGL, I’d think. Does that sound right?

In that case, it looks like there’s just some holes on the juce mac side. I think you need to do something like this, instead of just making the NSOpenGLView:

[code]sharedContext = [[NSOpenGLContext alloc] initWithFormat:[NSOpenGLView defaultPixelFormat] shareContext:nil];

[self setOpenGLContext:[[[NSOpenGLContext alloc] initWithFormat:[NSOpenGLView defaultPixelFormat] shareContext:sharedContext] autorelease]];[/code]

Bruce


#2

So in juce_Mac_OpenGLComponent.mm, something like:

[code] NSOpenGLView* view
= [[NSOpenGLView alloc] initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
pixelFormat: format];

	renderContext = [[NSOpenGLContext alloc] initWithFormat:format shareContext:sharedContext];
	
	[view setOpenGLContext:[renderContext];
	
    [format release];[/code]

Does that look right? I can give it a try, but it will take me a while to change over my shared context class.

Bruce


#3

Ok, Jules I tried this:

[code] NSOpenGLView* view
= [[NSOpenGLView alloc] initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
pixelFormat: format];

	renderContext = [[NSOpenGLContext alloc] initWithFormat:format shareContext:sharedContext];
	
	[view setOpenGLContext:renderContext];
	
    [format release];

[/code]

I can see the correct context (apparently) making ti all the way to the share as we make a new context, but I can’t confirm that the view is losing the old context. The share context is good - the CGL screen shares it and is fine - that uses a CGLContextObj obtained from the NSOoenGLContext.

I sort of expected to see the share fail, that seems to be normal for OS X, but it didn’t complain AFAICT. Ah, I do see one invalid drawable in my log. Maybe it’s a standard OpenGL snag, if that code looks OK to you?

Bruce


#4

That looks pretty sensible to me - I’ll roll it in there and see if any snags show up…


#5

Right, except it doesn’t work, AFAICT. Any Cocoa whiz kids on the list?

Bruce


#6

Hmm. According to the docs, you also need to set the view, so how about this:

[code] NSOpenGLView* view
= [[NSOpenGLView alloc] initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
pixelFormat: format];

    if (sharedContext != 0)
    {
        renderContext = [[NSOpenGLContext alloc] initWithFormat: format
                                                   shareContext: sharedContext];
        [view setOpenGLContext: renderContext];
        [renderContext setView: view];
    }
    else
    {
        renderContext = [view openGLContext];
    }

    [format release];

[/code]


#7

Ok. I tried with one or the other, not both.

But anyway, it doesn’t seem to be working…

Do you have a test project with context sharing?

Maybe it’s time to pull my stuff out, although it’s the juce component that’s having a problem.

Let me try souping up the Juce OpenGL demo.

Bruce


#8

Well, I’m still having some trouble, but when I jammed some test code into the demo, along with some binds and texture object generation, it does share (so that fix is good Jules).

So my problem is elsewhere, and I fear it may be related to C++ versus Obj C. My context is a void* and I cast it to the type in my obj-C code, I wonder if that could be the problem. That’s mainly since including cocoa stuff in my main, cross-platform C++ base is so hard. The actual gubbins are in an mm file, of course, but not the class definition.

Anything occuring to anybody? Anything I can get to in the debugger indicates that it’s OK, it’s just that the OpenGLComponent, or MAcWindowedComponent somehow treats my context differently than another context.

Bruce


#9

No ideas, I’m afraid. I’ve not had any trouble casting pointers to c++ objects with obj-c, as far as I can tell, it just leaves them alone.


#10

Stepping through the the 100th time, I noticed that the cursor was 2 lines off. Sure enough, a re-re-re-build did it.

Sweet, thanks.

Hoping to make a sale soon, BTW, so I can actually join the juce commercial user ranks.

Bruce