opengl视频冻结

时间:2011-10-27 11:18:39

标签: opengl pbo

我有一个IDS ueye cam并通过PBO进行捕获到OpenGL(OpenTK)。在我的开发者电脑上它运行良好,但在较慢的机器上,视频会在一段时间后冻结。

通过opengl分配内存并映射到ueye的代码,因此相机会在此处保存已处理的图像:

// 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));

将图像从pbo复制到纹理(纹理已创建且正常):

// 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);

也许有人可以看到错误...大约6秒后,眼睛事件不再传递任何图像。当我删除TexSubImage2D时,它运行良好,但当然没有图像出现。 可能是一个锁或什么来自opengl? 在此先感谢 - 托马斯

2 个答案:

答案 0 :(得分:1)

它似乎是一个共享缓冲区问题。您可以尝试实现一个简单的队列机制来解决这个问题。

示例代码(不打算工作):

queue< vector<BYTE> > frames;

...

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

...

// use frame here at GL.TexSubImage2D using frames.front()
frames.pop();

答案 1 :(得分:0)

我自己发现了失败。只需使用StreamRead替换StreamDraw上面的代码。

GL.BufferData(BufferTarget.PixelUnpackBuffer, new IntPtr(width * height * depth), IntPtr.Zero, BufferUsageHint.StreamRead);