Login  Register

Re: GLJPanel Native crash when sharing context of offscreen drawable

Posted by npryor on Jul 12, 2018; 8:38pm
URL: https://forum.jogamp.org/GLJPanel-Native-crash-when-sharing-context-of-offscreen-drawable-tp4039013p4039017.html

Fair enough on both counts.

import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLJPanel;

import javax.swing.*;
import java.nio.FloatBuffer;

public class GLCrashTest {
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

            JFrame frame = new JglpanelDemo();
            frame.pack();
            frame.setSize(100, 100);
            frame.isLocationByPlatform();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static class JglpanelDemo extends  JFrame {
        JglpanelDemo() {
            GLProfile profile = GLProfile.getDefault();
            GLDrawableFactory factory = GLDrawableFactory.getFactory(profile);
            GLCapabilities caps = new GLCapabilities(profile);
            caps.setHardwareAccelerated(true);
            caps.setDoubleBuffered(false);
            caps.setAlphaBits(8);
            caps.setRedBits(8);
            caps.setBlueBits(8);
            caps.setGreenBits(8);
            caps.setOnscreen(false);

            GLOffscreenAutoDrawable offscreenDrawable = factory.createOffscreenAutoDrawable(factory.getDefaultDevice(), caps, new DefaultGLCapabilitiesChooser(), 1, 1);
            offscreenDrawable.display();

            GLJPanel glPanel = new GLJPanel();
            glPanel.addGLEventListener(new GLEventListener() {
                @Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {}
                @Override public void dispose(GLAutoDrawable drawable) {}

                @Override
                public void display(GLAutoDrawable drawable) {
                    drawable.getContext().makeCurrent();
                    float array[] = {1f, 0f, 0f, 1f};
                    FloatBuffer buf = Buffers.newDirectFloatBuffer(array);
                    buf.rewind();
                    drawable.getGL().getGL2().glClearBufferfv(GL2.GL_COLOR, 0, buf);
                    drawable.getContext().release();
                }

                @Override
                public void init(GLAutoDrawable drawable) {
                    GLContext primaryContext = offscreenDrawable.getContext();

                    GLContext unusedDefaultContext = drawable.getContext();
                    unusedDefaultContext.makeCurrent();
                    drawable.setContext(null, true);


                    GLContext subContext = drawable.createContext(primaryContext);
                    subContext.makeCurrent();
                    drawable.setContext(subContext, true);
                }
            });

            add(glPanel);
        }
    }
}

I also explicitly set the Capabilities this time.

As for glClearBufferfv, this was only added to demonstrate (mostly for me) that the window was drawing; the crash happens with or without this line and, in fact, without anything in display()

This is the only code in the project I'm running.

And I don't think the problem is my understanding FBO's from within my own scope, the problem is I don't have a solid understanding on how JOGL's Contexts wraps and utilizes GL functionality as I have frequently encountered crashes like these and errors along the lines of "Failed to release context after 512 attempts" when I try to make a JOGL context current, release it, or draw on it at the wrong time.  And I have even less grasp of what constitutes the "wrong time" and it seems to be different on every machine I run.