Luca's picture

[SVN] Context sharing problem

Hello everybody!
I'm pretty new to OpenGL programming, and I really want to thank every single contributor to this great project. I spent an entire week battling with Tao before discovering OpenTK.. this project really rocks! It simply works!

Well, let's get to the point. I'm developing an MDI-style app, where each MDI window contains a GlControl. Since these are all views of the same scene, and this scene contains a huge texture (8192x4096, Earth imagery) I was thinking about using the context sharing feature to cut load times. So I downloaded and compiled the SVN sources with context sharing support, but it does not seem to work: the scene renders with a white texture. I added a debug statement after the wglShareContext call, and this is the result:

Creating default GraphicsMode (32, 16, 0, 0, 0, 2, False).
Device context: 771825523
Selecting pixel format... Creating GraphicsContext.
    GraphicsMode: Index: 8, Color: 32 (8888), Depth: 24, Stencil: False, Samples: 0, Accum: 64 (16161616), Buffers: 2, Stereo: False
    IWindowInfo: Windows.WindowInfo: Handle 462368, Parent (null)
    Loaded opengl32.dll: 1595146240
    OpenGL will be bound to handle: 462368
    Setting pixel format... 
    8
    Creating render context... done! (id: 65536)
    Load extensions for OpenTK.Platform.Windows.Wgl... 49 extensions loaded in 25 ms.
    Load extensions for OpenTK.Graphics.OpenGL.GL... 1007 extensions loaded in 414 ms.
    Load extensions for OpenTK.Graphics.OpenGL.Glu... 58 extensions loaded in 58 ms.
    Extension "
 
    Creating GraphicsContext.
        GraphicsMode: Index: 8, Color: 32 (8888), Depth: 24, Stencil: False, Samples: 0, Accum: 64 (16161616), Buffers: 2, Stereo: False
        IWindowInfo: Windows.WindowInfo: Handle 1117384, Parent (null)
        OpenGL will be bound to handle: 1117384
        Setting pixel format... 
        8
        Creating render context... done! (id: 65537)
        Sharing state with context OpenTK.Graphics.GraphicsContext
        Sharing state failed with error -1073282985
        Load extensions for OpenTK.Platform.Windows.Wgl... 49 extensions loaded in 10 ms.
        Load extensions for OpenTK.Graphics.OpenGL.GL... 1007 extensions loaded in 51 ms.
        Load extensions for OpenTK.Graphics.OpenGL.Glu... 58 extensions loaded in 4 ms.
        Extension "
 
The "Sharing state failed with error -1073282985" is produced by this line I added in the WinGlContext constructor:
 
if (sharedContext != null)
{
                Debug.Print("Sharing state with context {0}", sharedContext.ToString());
                if (!Wgl.Imports.ShareLists(renderContext, (sharedContext as IGraphicsContextInternal).Context))
                    Debug.Print("Sharing state failed with error {0}", Marshal.GetLastWin32Error());
}

Google does not help me in finding the error message for this error code (hex 0xC0070057), and I have to admit that since it's about a week that I'm working with OpenGL I have no idea where to start. Can someone help me?

Thanks in advance

[mod edit: added code tags]


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Ok, I'll take a look into this, as soon as Visual Studio is installed here (just formatted my disk).

Till then, can you please check whether Wgl.Imports.ShareLists has the SetLastError attribute defined?

straka.milan's picture

I believe there is an logical error in the opentk source files. The WinGLContext.cs:

renderContext = new ContextHandle(Wgl.Imports.CreateContext(currentWindow.DeviceContext));
...
if (sharedContext != null)
{
     Debug.Print("Sharing state with context {0}", sharedContext.ToString());
     Wgl.Imports.ShareLists(renderContext, (sharedContext as IGraphicsContextInternal).Context);
}

The MSDN says:

BOOL wglShareLists(
  HGLRC  hglrc1,  // OpenGL rendering context with which to share display lists
  HGLRC  hglrc2   // OpenGL rendering context to share display lists
);
Parameters
hglrc1 - Specifies the OpenGL rendering context with which to share display lists.
hglrc2 - Specifies the OpenGL rendering context to share display lists with hglrc1. The hglrc2 parameter should not contain any existing display lists when wglShareLists is called.

But the "sharedContext" can already contain some display lists. On the other hand, the "renderContext" is fresh new context -- it contains no displaylists. So I believe the parameters of ShareLists should be reversed, to the WinGLContext should read:

...
     Wgl.Imports.ShareLists((sharedContext as IGraphicsContextInternal).Context,renderContext);
...

 

To address the original post -- you said you want to share textures between contexts. But sharedContext using "wglShareLists" will not work, it shares only displaylists. There are definitely some ways to make it work, but "wglShareLists" will not help you.

EDIT: I was not right. OpenGl specs (in the glBindTexture) say:
Texture names are unsigned integers. The value zero is reserved to represent the default texture for each texture target. Texture names and the corresponding texture contents are local to the shared display list space (see glXCreateContext) of the current GL rendering context; two rendering contexts share texture names only if they also share display lists.
So it will help you :) Sorry for confusion...

Till then, can you please check whether Wgl.Imports.ShareLists has the SetLastError attribute defined?
Yes it has...

the Fiddler's picture

So I believe the parameters of ShareLists should be reversed, to the WinGLContext should read:

     Wgl.Imports.ShareLists((sharedContext as IGraphicsContextInternal).Context,renderContext);


Ah, of course. Thanks for pointing that out, I'd have spent hours spending for the problem :)

djk's picture

Thanks guys, I have been chasing this all weekend. In our app it was manifesting as all displaylists being rendered in all the MDI windows that contained a GLControl, reguardless of which context had makecurrent set.

I had started with the assumption that I had missed a call to makecurrent at some point.

djk

Luca's picture

A big thank you to you all! It now works flawlessly :)
Your quick response makes me like this project even more!

Greetings from Italy
--
Luca

The Price of Freedom is Eternal Vigilance.