Login  Register

How to share context between GLAutoDrawable and GLOffscreenAutoDrawable

Posted by voyta on Dec 04, 2017; 10:32am
URL: https://forum.jogamp.org/How-to-share-context-between-GLAutoDrawable-and-GLOffscreenAutoDrawable-tp4038376.html

Hi,
is there any way to share context between GLAutoDrawable and GLOffscreenAutoDrawable?

I tried following code:

   public static void main(String[] args) {
       
      // profile and capabilities
      GLProfile glProfile = GLProfile.getDefault();
      GLCapabilities caps = new GLCapabilities(glProfile);
      caps.setHardwareAccelerated(false);
      caps.setOnscreen(false);
 
      // shared drawable
      GLAutoDrawable sharedDrawable = GLDrawableFactory.getFactory(glProfile).createDummyAutoDrawable(null, true, caps, null);
      sharedDrawable.display();

      // offscreen drawable
      GLOffscreenAutoDrawable offscreenDrawable = GLDrawableFactory.getFactory(glProfile).createOffscreenAutoDrawable(null, caps, null, 300, 300);
     
      // share context
      offscreenDrawable.setSharedAutoDrawable(sharedDrawable);
      offscreenDrawable.display();
     
      GLContext ctx = offscreenDrawable.getContext();
      ctx.makeCurrent();
     
      // render some picture
      GL2 gl = offscreenDrawable.getGL().getGL2();    
      gl.glBegin (GL2.GL_LINES);//static field
      gl.glVertex3f(0.50f,-0.50f,0);
      gl.glVertex3f(-0.50f,0.50f,0);
      gl.glEnd();
     
      // save to file
      BufferedImage im = new AWTGLReadBufferUtil(offscreenDrawable.getGLProfile(), false).readPixelsToBufferedImage(offscreenDrawable.getGL(), 0, 0, 400, 400, true);
      try {
        boolean bb = ImageIO.write(im,"png",new File("im.png"));
      } catch (IOException ex) {
        Logger.getLogger(OffscreenFrame.class.getName()).log(Level.SEVERE, null, ex);
      }
   }
   
This works as intended when I use hardware accreleration. But when I turn off my graphic card (and use GDI Generic instead), it gives me following error:

Exception in thread "main" com.jogamp.opengl.GLException: wglShareLists(0x10001, 0x10002) failed: werr 0
        at jogamp.opengl.windows.wgl.WindowsWGLContext.createImpl(WindowsWGLContext.java:424)
        at jogamp.opengl.GLContextImpl.makeCurrentWithinLock(GLContextImpl.java:763)
        at jogamp.opengl.GLContextImpl.makeCurrent(GLContextImpl.java:648)
        at jogamp.opengl.GLContextImpl.makeCurrent(GLContextImpl.java:586)
        at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1279)
        at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1147)
        at jogamp.opengl.GLAutoDrawableBase.defaultDisplay(GLAutoDrawableBase.java:467)
        at com.jogamp.opengl.GLAutoDrawableDelegate.display(GLAutoDrawableDelegate.java:190)
        at offscreenRender.OffscreenFrame.main(OffscreenFrame.java:51)
       
Thanks,
Vojta