0

I have an IDS ueye cam and proceed the capture via PBO to OpenGL (OpenTK). On my developer-pc it works great, but on slower machines the video freezes after some time.

Code for allocating memory via opengl and map to ueye, so camera saves processed images in here:

// Generate PBO and save id
GL.GenBuffers(1, out this.frameBuffer[i].BufferID);

// Define the type of the buffer.
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, this.frameBuffer[i].BufferID);

// Define buffer size.
GL.BufferData(BufferTarget.PixelUnpackBuffer, new IntPtr(width * height * depth), IntPtr.Zero, BufferUsageHint.StreamDraw);

// Get pointer to by openGL allocated buffer and
// lock global with uEye.
this.frameBuffer[i].PointerToNormalMemory = GL.MapBuffer(BufferTarget.PixelUnpackBuffer, BufferAccess.WriteOnly);
this.frameBuffer[i].PointerToLockedMemory = uEye.GlobalLock(this.frameBuffer[i].PointerToNormalMemory);

// Unmap PBO after use.
GL.UnmapBuffer(BufferTarget.PixelUnpackBuffer);

// Set selected PBO to none.
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);

// Register buffer to uEye
this.Succeeded("SetAllocatedImageMem", this.cam.SetAllocatedImageMem(width, height, depth, this.frameBuffer[i].PointerToLockedMemory, ref this.frameBuffer[i].MemId));

// Add buffer to uEye-Ringbuffer
this.Succeeded("AddToSequence", this.cam.AddToSequence(this.frameBuffer[i].PointerToLockedMemory, this.frameBuffer[i].MemId));

To copy the image from pbo to an texture (Texture is created and ok):

// Select PBO with new video image
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, nextBufferId);

// Select videotexture as current
GL.BindTexture(TextureTarget.Texture2D, this.videoTextureId);

// Copy PBO to texture            
GL.TexSubImage2D(
    TextureTarget.Texture2D,
    0,
    0,
    0,
    nextBufferSize.Width,
    nextBufferSize.Height,
    OpenTK.Graphics.OpenGL.PixelFormat.Bgr,
    PixelType.UnsignedByte,
    IntPtr.Zero);

// Release Texture
GL.BindTexture(TextureTarget.Texture2D, 0);

// Release PBO
GL.BindBuffer(BufferTarget.PixelUnpackBuffer, 0);

Maybe someone can see the mistake... After about 6 seconds the ueye events don't deliver any images any more. When I remove TexSubImage2D it works well, but of course no image appears. Is there maybe a lock or something from opengl? Thanks in advance - Thomas

freakinpenguin
  • 831
  • 14
  • 24

2 Answers2

1

it seems like a shared buffer problem. you may try to implement a simple queue mechanism to get rid of that problem.

sample code (not meant to be working):

queue< vector<BYTE> > frames;

...

frames.push(vector<BYTE>(frameBuffer, frameBuffer + frameSize));

...

// use frame here at GL.TexSubImage2D using frames.front()
frames.pop();
Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57
  • That's actually what I do. The cam has a ringbuffer and every time I receive a new frame I pass the buffer-ID to opengl. Currently I have 2 Elements in Buffer, but the error also occurs with more; but than it tooks longer... – freakinpenguin Oct 27 '11 at 11:29
  • since i'm not able to see all the code (like where nextBufferId is assigned etc.) i can't tell you if your queue mechanism is perfectly safe. my guess is there is something wrong with your multiple buffers implementation. if it's not too hard for you, please try using std::queue and std::vector like above. – Emir Akaydın Oct 27 '11 at 11:36
  • I'm pretty shure that this works fine. NextBufferId is the one which I get from GenBuffer... – freakinpenguin Oct 27 '11 at 11:37
  • ah, also if your buffer keeps growing because of not enough cpu/gpu power, you may consider dropping frames when buffer reaches a maximum. that way some glitches will occur but it never freezes. – Emir Akaydın Oct 27 '11 at 11:38
0

Found the failure by myself. Just replace in the code above StreamDraw with StreamRead.

GL.BufferData(BufferTarget.PixelUnpackBuffer, new IntPtr(width * height * depth), IntPtr.Zero, BufferUsageHint.StreamRead);
freakinpenguin
  • 831
  • 14
  • 24