glpbuffer: invalid context state

hi!

i the display() method i am trying to to render to a pbuffer and then bind this pbuffer an draw it using glquads. here is what i do in my display:


// the glpbuffer object shoud only be created once
if(this.buffer==null)   // private GLPbuffer buffer
{
   GLCapabilities cap = new GLCapabilities();
   cap.setDoubleBuffered(false);
   cap.setPbufferRenderToTexture(true);
   cap.setPbufferRenderToTextureRectangle(true);

   GLDrawableFactory fact = GLDrawableFactory.getFactory();
   if(!fact.canCreateGLPbuffer()) throw new RuntimeException("no glpbuffer");
   this.buffer = fact.createGLPbuffer(cap, null, 320, 240, GLContext.getCurrent());
   this.buffer.addGLEventListener(...);
}

// in the following 5 lines of code seems to be the problem
GLContext oldctx = GLContext.getCurrent();
GLContext bufferctx = this.buffer.getContext();
bufferctx.makeCurrent();
this.buffer.display();
oldctx.makeCurrent();

gl.glEnable(GL.GL_TEXTURE_2D);   // what to enable here? there is no this.buffer.getTarget()
this.buffer.bindTexture();

gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(...);
gl.glVertex3d(...),
gl.glTexCoord2f(...);
gl.glVertex3d(...),
gl.glTexCoord2f(...);
gl.glVertex3d(...),
gl.glTexCoord2f(...);
gl.glVertex3d(...),
gl.glEnd();

this.buffer.releaseTexture();

the code is always executed by the awt thread. no matter what i do with the contexts, either the this.buffer.display() writes “invalid context state” or the later binding of the pbuffer does.

what do i make wrong?

thanks in advance

I’m leery of the fact that you’re switching contexts in the middle of your display() method. However, it should be supported; the main problem is that the display() method (with no arguments) handles making the context current and restoring the old one. So you should just call this.buffer.display() and remove all hint of oldctx and bufferctx.

This might work, hopefully.

no this doesn’t work. i get the “invalid context state” error as soon as i bind the pbuffer.

you are right, display handels all that fou you. i checked it by comparing the currentContext before and after the display call. but sadly this doesn’t help. :frowning:

do i have to call this.buffer.createContext sometime?

You shouldn’t need to call createContext(). Unfortunately for you, I’ve never tried using the bindTexture() method, but the javadoc for it says it may be unsupported on certain machines. If this is the case, you might just be out of luck.

Because there is also a chance that that functionality will become deprecated, here’s what I recommend you do:

  1. In the setup of your program (not in any GLEventListeners), create a GLPBuffer without specifying render-to-texture
  2. Then create your GLCanvas, but pass in the pbuffer’s GLContext as the share parameter. This way textures will be shared between the two
  3. In the init of the pbuffer, create a texture that’s sized the same as the pbuffer
  4. When rendering to the pbuffer, render normally and then bind the texture from #3 and call glCopyTexSubImage() to copy the pbuffer data
  5. When rendering into the canvas, you can use the #3 texture, too and it will have the pbuffer’s color data.