avogatro's picture

<solved>GLControl + thread

Data:
hardware: Geforce8600GT + Athlon X2 5000
OS: Archlinux i686 32 bit
driver: NVIDIA beta 190.18
opentk: 0.9.8.2
mono:
Mono JIT compiler version 2.4.2.3
TLS: __thread
GC: Included Boehm (with typed GC)
SIGSEGV: altstack
Notifications: epoll
Architecture: x86
Disabled: none

HI:
I use GLcontrol to print moving text on a Windows Form(ontop, with fix location).
I use the Application_Idle methode. but the speed is not very constant .
A normal loop without checking "glcontrol.isIdle" is much more smooth.
Without threading, it works.
But I need to run the loop in a thread becauseI I have to be able to kill the the run loop at any time.

        public void Run()
        {
            long timestamp;
            long timedelta;
            bool should_render = false;
            long delta = 320000;
            int tempconter = 0 ;
            while (!this.exiting){
                timestamp = Stopwatch.GetTimestamp();
                if (last_timestamp == 0){
                    last_timestamp = Stopwatch.GetTimestamp();
                    continue;
                }
                timedelta = timestamp-last_timestamp;
 
                should_render = false;
                if (timedelta > delta)
                {
                    posX-=speed;
                    if (posX<=-fontWidth){
                    posX= this.Width;
                    }
                    timedelta -= delta;
                    should_render = true;
                }
                //posX-=tempconter;
 
                if (should_render == true)
                {
                    this.render(); 
                    last_timestamp = Stopwatch.GetTimestamp(); 
                }
                else
                Thread.Sleep(2);
            }
        private void render()
        {
 
            GL.ShadeModel(ShadingModel.Smooth);
            GL.Enable( EnableCap.DepthTest );
 
            GL.Enable(EnableCap.LineSmooth);
            GL.ClearDepth(1.0);
            GL.ClearColor(0.0F,0.0F,0.0F,0.0F);
            GL.DepthFunc(DepthFunction.Lequal);
            GL.Hint(HintTarget.PerspectiveCorrectionHint,HintMode.Nicest);
            GL.Enable(EnableCap.CullFace);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
 
            this.glControl.MakeCurrent();
            //GraphicsContext.CurrentContext().
            GL.ClearColor(Color.Black);
            GL.Clear(ClearBufferMask.ColorBufferBit|ClearBufferMask.DepthBufferBit);
 
            this.printer.Begin();
 
            this.printer.Print(text, the_font, fontColor,
                          new RectangleF(posX,0,fontWidth,this.Height*0.9F),
                          TextPrinterOptions.Default,TextAlignment.Center);
 
            this.printer.End();
            this.glControl.SwapBuffers();
 
 
        }
        }

the Problem:
function glcontrol.MakeCurrent(), don't work in a thread
Error:

Unhandled Exception: OpenTK.Graphics.GraphicsContextException: Failed to make context current.
  at OpenTK.Platform.X11.X11GLContext.MakeCurrent (IWindowInfo window) [0x000e3] in /home/moi/Projects/opentk-0.9.8-2/Source/OpenTK/Platform/X11/X11GLContext.cs:219 
  at OpenTK.Graphics.GraphicsContext.MakeCurrent (IWindowInfo info) [0x00000] in /home/moi/Projects/opentk-0.9.8-2/Source/OpenTK/Graphics/GraphicsContext.cs:387 
  at OpenTK.GLControl.MakeCurrent () [0x00000] in /home/moi/Projects/opentk-0.9.8-2/Source/OpenTK/GLControl.cs:191 
  at (wrapper remoting-invoke-with-check) OpenTK.GLControl:MakeCurrent ()
  at RollingText.RollingText.render () [0x00000] in /home/moi/Projects/opentk-0.9.8-2/Fonttest/RollingText.cs:144 
  at RollingText.RollingText.Run () [0x00087] in /home/moi/Projects/opentk-0.9.8-2/Fonttest/RollingText.cs:238

I checkt the documentation http://www.opentk.com/doc/graphics/graphicscontext :
However function GraphicsContext.MakeCurrent(null, null) don't exist.
so i can't make the context non-current on the main thread.

Can someone help me?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture
this.glControl.Context.MakeCurrent(null, null);

If this method doesn't exist, this is a bug (and a serious one at that).

Edit: clarified the documentation, MakeCurrent() is an instance method.

avogatro's picture

well

this.glControl.Context.MakeCurrent(null,null);

[Task:File=/home/moi/Projects/opentk-0.9.8-2/Fonttest/RollingText.cs, Line=73, Column=36, Type=Error, Priority=Normal, Description=No overload for method `MakeCurrent' takes `2' arguments(CS1501)]

the Fiddler's picture

Ok, this is a documentation bug (edit: fixed), try with a single argument:

this.glControl.Context.MakeCurrent(null);
avogatro's picture

OpenTK.Platform.IWindowInfo as null ...
error

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
  at OpenTK.Platform.X11.X11GLContext.MakeCurrent (IWindowInfo window) [0x00007] in /home/moi/Projects/opentk-0.9.8-2/Source/OpenTK/Platform/X11/X11GLContext.cs:207 
  at OpenTK.Graphics.GraphicsContext.MakeCurrent (IWindowInfo info) [0x00000] in /home/moi/Projects/opentk-0.9.8-2/Source/OpenTK/Graphics/GraphicsContext.cs:387 
  at RollingText.RollingText.Application_Idle (System.Object sender, System.EventArgs e) [0x00000] in /home/moi/Projects/opentk-0.9.8-2/Fonttest/RollingText.cs:252 
  at System.Windows.Forms.XplatUIX11.UpdateMessageQueue (System.Windows.Forms.XEventQueue queue) [0x0004b] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/XplatUIX11.cs:1599 
  at System.Windows.Forms.XplatUIX11.GetMessage (System.Object queue_id, System.Windows.Forms.MSG& msg, IntPtr handle, Int32 wFilterMin, Int32 wFilterMax) [0x00022] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/XplatUIX11.cs:3779 
  at System.Windows.Forms.XplatUI.GetMessage (System.Object queue_id, System.Windows.Forms.MSG& msg, IntPtr hWnd, Int32 wFilterMin, Int32 wFilterMax) [0x00000] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/XplatUI.cs:725 
  at System.Windows.Forms.Application.RunLoop (Boolean Modal, System.Windows.Forms.ApplicationContext context) [0x003be] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Application.cs:891 
  at System.Windows.Forms.Application.Run (System.Windows.Forms.ApplicationContext context) [0x00014] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Application.cs:761 
  at System.Windows.Forms.Application.Run () [0x00000] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Application.cs:735 
  at RollingText.MainClass.Main (System.String[] args) [0x00034] in /home/moi/Projects/opentk-0.9.8-2/Fonttest/RollingText.cs:294
the Fiddler's picture

This code works as expected in 0.9.9-2b. Please upgrade to this version of OpenTK.

avogatro's picture

How can I do something like this:
I need extra Thread, but i can't use the function glControl.context.makeCurrent();

        void Application_Idle(object sender, EventArgs e)
        {
 
            this.glControl.Context.MakeCurrent(null);
            Thread mainloop = new Thread(new ThreadStart(this.Run));
            mainloop.Start();
 
        }

error:

Unhandled Exception: OpenTK.Graphics.GraphicsContextException: Failed to make context current.
  at OpenTK.Platform.X11.X11GLContext.MakeCurrent (IWindowInfo window) [0x00109] in /home/moi/Projects/opentk-0.9.9-2b/Source/OpenTK/Platform/X11/X11GLContext.cs:221 
  at OpenTK.Graphics.GraphicsContext.MakeCurrent (IWindowInfo window) [0x00000] in /home/moi/Projects/opentk-0.9.9-2b/Source/OpenTK/Graphics/GraphicsContext.cs:309 
  at OpenTK.GLControl.MakeCurrent () [0x00006] in /home/moi/Projects/opentk-0.9.9-2b/Source/GLControl/GLControl.cs:230 
  at (wrapper remoting-invoke-with-check) OpenTK.GLControl:MakeCurrent ()
  at RollingText.RollingText.render () [0x0000b] in /home/moi/Projects/opentk-0.9.9-2b/Source/Fonttest/RollingText.cs:124 
  at RollingText.RollingText.Run () [0x00087] in /home/moi/Projects/opentk-0.9.9-2b/Source/Fonttest/RollingText.cs:231
the Fiddler's picture

This code should work correctly in OpenTK 0.9.9-2 and higher.

However, you should disconnect the Application_Idle handler prior to launching the new thread:

        void Application_Idle(object sender, EventArgs e)
        {
 
            Application.Idle -= Application_Idle;
 
            this.glControl.Context.MakeCurrent(null);
            Thread mainloop = new Thread(new ThreadStart(this.Run));
            mainloop.Start();
 
        }

Otherwise you will launch a new thread every time Application.Idle is raised (not good!)

avogatro's picture

I did what you say:
0.9.9.2b
I deactivatet the textprinter.
so it's now just a winform under X11,
with a main loop in a thread.
this is the error, maybe the stack helps:

Unhandled Exception: OpenTK.Graphics.GraphicsContextException: Failed to make context current.
  at OpenTK.Platform.X11.X11GLContext.MakeCurrent (IWindowInfo window) [0x00109] in /home/moi/Projects/opentk-0.9.9-2b/Source/OpenTK/Platform/X11/X11GLContext.cs:221 
  at OpenTK.Graphics.GraphicsContext.MakeCurrent (IWindowInfo window) [0x00000] in /home/moi/Projects/opentk-0.9.9-2b/Source/OpenTK/Graphics/GraphicsContext.cs:309 
  at OpenTK.GLControl.MakeCurrent () [0x00006] in /home/moi/Projects/opentk-0.9.9-2b/Source/GLControl/GLControl.cs:230 
  at (wrapper remoting-invoke-with-check) OpenTK.GLControl:MakeCurrent ()
  at RollingText.RollingText.render () [0x0000b] in /home/moi/Projects/opentk-0.9.9-2b/Source/Fonttest/RollingText.cs:124 
  at RollingText.RollingText.Run () [0x00087] in /home/moi/Projects/opentk-0.9.9-2b/Source/Fonttest/RollingText.cs:231 
Stacktrace:
 
  at (wrapper managed-to-native) object.wrapper_native_0xb584592c (OpenTK.Graphics.OpenGL.ClearBufferMask) <0x00004>
  at (wrapper managed-to-native) object.wrapper_native_0xb584592c (OpenTK.Graphics.OpenGL.ClearBufferMask) <0xffffffff>
  at OpenTK.Graphics.OpenGL.GL.Clear (OpenTK.Graphics.OpenGL.ClearBufferMask) [0x0000c] in /home/moi/Projects/opentk-0.9.9-2b/Source/OpenTK/Graphics/OpenGL/GL.cs:28495
  at RollingText.RollingText.render () [0x00097] in /home/moi/Projects/opentk-0.9.9-2b/Source/Fonttest/RollingText.cs:140
  at RollingText.RollingText.Run () [0x00087] in /home/moi/Projects/opentk-0.9.9-2b/Source/Fonttest/RollingText.cs:231
  at RollingText.RollingText.glControl_Paint (object,System.Windows.Forms.PaintEventArgs) [0x00011] in /home/moi/Projects/opentk-0.9.9-2b/Source/Fonttest/RollingText.cs:119
  at System.Windows.Forms.Control.OnPaint (System.Windows.Forms.PaintEventArgs) [0x0001c] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Control.cs:6423
  at OpenTK.GLControl.OnPaint (System.Windows.Forms.PaintEventArgs) [0x00022] in /home/moi/Projects/opentk-0.9.9-2b/Source/GLControl/GLControl.cs:177
  at System.Windows.Forms.Control.WmPaint (System.Windows.Forms.Message&) [0x0008a] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Control.cs:5561
  at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message&) [0x0016c] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Control.cs:5319
  at System.Windows.Forms.ScrollableControl.WndProc (System.Windows.Forms.Message&) [0x00000] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ScrollableControl.cs:807
  at System.Windows.Forms.ContainerControl.WndProc (System.Windows.Forms.Message&) [0x0003d] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ContainerControl.cs:642
  at System.Windows.Forms.UserControl.WndProc (System.Windows.Forms.Message&) [0x00037] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/UserControl.cs:150
  at System.Windows.Forms.Control/ControlWindowTarget.OnMessage (System.Windows.Forms.Message&) [0x00000] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Control.cs:234
  at System.Windows.Forms.Control/ControlNativeWindow.WndProc (System.Windows.Forms.Message&) [0x00000] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Control.cs:215
  at System.Windows.Forms.NativeWindow.WndProc (intptr,System.Windows.Forms.Msg,intptr,intptr) [0x00085] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/NativeWindow.cs:242
  at System.Windows.Forms.XplatUIX11.DispatchMessage (System.Windows.Forms.MSG&) [0x00000] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/XplatUIX11.cs:3424
  at System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG&) [0x00000] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/XplatUI.cs:605
  at System.Windows.Forms.Application.RunLoop (bool,System.Windows.Forms.ApplicationContext) [0x0036a] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Application.cs:982
  at System.Windows.Forms.Application.Run (System.Windows.Forms.ApplicationContext) [0x00014] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Application.cs:761
  at System.Windows.Forms.Application.Run () [0x00000] in /home/ise/svn/mono/trunk/src/mono-2.4.2.3/mcs/class/Managed.Windows.Forms/System.Windows.Forms/Application.cs:735
  at RollingText.MainClass.Main (string[]) [0x00034] in /home/moi/Projects/opentk-0.9.9-2b/Source/Fonttest/RollingText.cs:289
  at (wrapper runtime-invoke) RollingText.MainClass.runtime_invoke_void_object (object,intptr,intptr,intptr) <0xffffffff>
 
Native stacktrace:
 
	/usr/bin/mono [0x80c86af]
	/usr/bin/mono [0x80f3313]
	[0xb7f4340c]
	/usr/lib/libGLcore.so.1 [0xb4eded08]
 
Debug info from gdb:
 
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
[Thread debugging using libthread_db enabled]
[New Thread 0xb7ca3900 (LWP 12269)]
[New Thread 0xb41ffb70 (LWP 12274)]
[New Thread 0xb7319b70 (LWP 12271)]
[New Thread 0xb733db70 (LWP 12270)]
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
0xb7f43424 in __kernel_vsyscall ()
  4 Thread 0xb733db70 (LWP 12270)  0xb7f43424 in __kernel_vsyscall ()
  3 Thread 0xb7319b70 (LWP 12271)  0xb7f43424 in __kernel_vsyscall ()
  2 Thread 0xb41ffb70 (LWP 12274)  0xb7f43424 in __kernel_vsyscall ()
  1 Thread 0xb7ca3900 (LWP 12269)  0xb7f43424 in __kernel_vsyscall ()
 
Thread 4 (Thread 0xb733db70 (LWP 12270)):
#0  0xb7f43424 in __kernel_vsyscall ()
#1  0xb7e4f256 in nanosleep () from /lib/libpthread.so.0
#2  0x081b1708 in ?? ()
#3  0xb7e4765c in start_thread () from /lib/libpthread.so.0
#4  0xb7da21fe in clone () from /lib/libc.so.6
 
Thread 3 (Thread 0xb7319b70 (LWP 12271)):
#0  0xb7f43424 in __kernel_vsyscall ()
#1  0xb7e4dd65 in sem_wait@@GLIBC_2.1 () from /lib/libpthread.so.0
#2  0x0819f649 in ?? ()
#3  0x0811414c in ?? ()
#4  0x081a2e82 in ?? ()
#5  0x081d5375 in ?? ()
#6  0xb7e4765c in start_thread () from /lib/libpthread.so.0
#7  0xb7da21fe in clone () from /lib/libc.so.6
 
Thread 2 (Thread 0xb41ffb70 (LWP 12274)):
#0  0xb7f43424 in __kernel_vsyscall ()
#1  0xb7e4e5d9 in __lll_lock_wait () from /lib/libpthread.so.0
#2  0xb7e49cca in _L_lock_554 () from /lib/libpthread.so.0
#3  0xb7e49b48 in pthread_mutex_lock () from /lib/libpthread.so.0
#4  0xb57d4101 in ?? () from /usr/lib/libGL.so.1
#5  0xb584fea0 in ?? () from /usr/lib/libGL.so.1
#6  0xb7e57ff4 in ?? () from /lib/libpthread.so.0
#7  0xb7e4afab in _L_unlock_430 () from /lib/libpthread.so.0
#8  0xb7e4aeca in __pthread_mutex_unlock_usercnt () from /lib/libpthread.so.0
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
 
Thread 1 (Thread 0xb7ca3900 (LWP 12269)):
#0  0xb7f43424 in __kernel_vsyscall ()
#1  0xb7e4ea3b in read () from /lib/libpthread.so.0
#2  0x080c882b in ?? ()
#3  0x080f3313 in ?? ()
#4  <signal handler called>
#5  0xb4eded08 in ?? () from /usr/lib/libGLcore.so.1
#6  0xb584fea0 in ?? () from /usr/lib/libGL.so.1
#7  0x00000000 in ?? ()
#0  0xb7f43424 in __kernel_vsyscall ()
 
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================
the Fiddler's picture

I'm looking into the issue.