How to transfer gl context to another thread in c# using OpenTK?

46 Views Asked by At

I am doing research on multithreading to develop my own game engine. I want to have two threads: one dedicated to rendering and main dedicated to game logic, i have found out that i can create gl context on the main thread and than move it to the rednering thread.

I tried to implement this transfering in the code below but when i run the program window shows up and just freezes without ability to even close it.

using OpenTK.Graphics.OpenGL4;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;

namespace TestApp
{
    internal class Program
    {   
        private static GameWindow _gameWindow;
        private static IGLFWGraphicsContext _glContext;

        private static void Main()
        {
            var gws = new GameWindowSettings();
            var nws = new NativeWindowSettings();

            _gameWindow = new GameWindow(gws, nws);
            _glContext = _gameWindow.Context;

            _glContext.MakeNoneCurrent();
            var thread = new Thread(RenderingThreadMethod);
            thread.Start();
        }

        private static void RenderingThreadMethod()
        {   
            _glContext.MakeCurrent();
            _gameWindow.Run();
        }
    }
}

What is wrong with this implementation of context tranfering?

0

There are 0 best solutions below