LWJGLX / debug

Debugging LWJGL3 OpenGL applications
MIT License
37 stars 7 forks source link

Correctly Handle GLX Calls #29

Open gudenau opened 2 years ago

gudenau commented 2 years ago

The JAWT demos use GLX in order to render GL content on Linux. Currently the debug jar has a fatal exception on o.l.o.GL.createCapabilities because it doesn't see that there is a current context.

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: No OpenGL context has been made current through recognized API methods (glfwMakeContextCurrent).
Brainfree commented 2 years ago

I made a hack that seems to fix the problem, or at least allows it to continue and be more useful. It's invoked before any GL is called, within the correct thread. Hope it helps.


private static void fixDebugAgent() {
    try {
        Class<?> c = Class.forName("org.lwjglx.debug.org.lwjgl.opengl.Context");
        {
            Method m = c.getMethod("create", long.class, long.class);
            m.setAccessible(true);
            m.invoke(null, 0L, 0L);
        }
        Object ctx;
        {
            Field f = c.getField("CONTEXTS");
            f.setAccessible(true);
            Map<Long,Object> contexts = (Map<Long,Object>) f.get(null);
            ctx = contexts.get(0L);
            if(ctx == null) throw new IllegalStateException();
        }
        {
            Field f = c.getField("CURRENT_CONTEXT");
            f.setAccessible(true);
            ThreadLocal<Object> v = (ThreadLocal<Object>) f.get(null);
            v.set(ctx);
        }
        System.out.println("SET!");
    } catch (Throwable e) {
        e.printStackTrace();
    }
}