JTalton's picture

GTK GLWidget - OpenTK GraphicsContext Support

I was hoping to update my GTK# GLWidget to support the OpenTK GraphicsContext by adding a function much like the WinForms function to OpenTK.Platform.Utilities.

public static IWindowInfo CreateWindowInfo(Graphics.GraphicsMode mode, IntPtr widgetHandle)

I think I have it mostly worked out, but the problem I am having is that it crashes. I have found that if I just call GraphicsMode.Default it will cause a GTK# application to crash. It is something about the enumeration of the GraphicsModes.

I have not had time to debug that part of the code. Any thoughts or suggestions would be welcome.

protected override void OnRealized()
{
	base.OnRealized();
	GraphicsMode gm = GraphicsMode.Default; // Causes GTK to crash at a later point in time
}

Comments

Comment viewing options

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

Which OS? Is this an AccessViolationException or something different?

On X11, every window carries a specific visual set at creation time. IIRC, the GraphicsContext must be created with the same visual to work. The process for OpenTK.GameWindow is similar to this:

  1. You select a GraphicsMode (which defines the visual id).
  2. You create the visual using this visual id.
  3. You create the window using the visual. The visual is stored in a X11WindowInfo instance.
  4. You create the context using the X11WindowInfo instance.

Win32 works in a similar fashion: the GraphicsMode defines the pixel format, which is shared by both the window and the GraphicsContext. The difference is that you don't need the pixel format when you create the window - you can specify it later on.

The exact same process is used in OpenTK.GLControl. The only complication is that we need to set the (X11) visual before the control is created, which is achieved using reflection (kudos to the Mono WinForms team for making this possible).

If my reading of your code is correct, it seems likely that the visual of the widget is different than the GraphicsMode.Default visual. I'm not sure what the consequences are supposed to be, but I've seen strange behaviour arise from such mismatches: falling back to indirect rendering, MakeCurrent failing or even outright crashes. I admit that my knowledge of X11 is not very deep, but these problems seemed to go away once I used the same visual for both the window and the context.

Is it possible to specify the visual of the widget before it is created? You can then store this information in the IWindowInfo instance and the rest should work as expected.

Edit: I just came across this feature request on the GTK# bug tracker. I don't know how relevant it is to this discussion, but the original discussion indicated that this would be useful for adding OpenGL support.