ofBufferObject and async camera/video texture upload

Hello,

I’m trying use the ofBufferObject to implement async texture upload from a camera and video. I’m a little confused on the ofBufferObject syntax. I did see the ofBufferObject save image examples, but could use some help going the other way. Has anyone done this?

thanks,
Charles

the idea is pretty much the same, when you get a new frame from the camera just bind the buffer as GL_PiXEL_PACK_BUFFER, map the buffer onto memory as GL_WRITE_ONLY and memcpy the frane pixels to the buffer. Then to finally send the pixels to a texture, load the buffer contents to it using ofTexture::loadData as you would normally do with pixels but using the ofBufferObject instead. haven’t tested it but it would be something like:

//.h
ofTexture tex;
ofBufferObject pbo1;
ofThreadChannel<unsigned char*> channel;
ofThreadChannel<bool> ready;


//setup
tex.allocate(w,h,GL_RGB);
pbo.allocate(bytes,  GL_STREAM_DRAW);
auto dstBuffer = pbo.map<unsigned char>(GL_WRITE_ONLY);
channel.send(dstBuffer);


//video callback
unsigned char * dstPixels;
dstPixels.receive(dstPixels);
memcpy(dstPixels, rawCamPixels, w*h*3);
channelReady.send(true);

//draw
bool ready;
ifchannel.tryReceive(ready)){
    pbo.unmap();
    tex.loadData(pbo,GL_RGB,GL_UNSIGNED_BYTE);
    auto dstBuffer = pbo.map<unsigned char>(GL_WRITE_ONLY);
    channel.send(dstBuffer);
}

i’m using a channel to send the pixels cause usually when using a camera or video library the frame callback will happen on a different thread. you can also use more than 1 pbo in case it’s nt fast enough to help hide the latency

also i just checked and there was a small fix required to make this work, you will need the latest ofBufferObject from git master or just apply the changes from https://github.com/openframeworks/openFrameworks/commit/93334123637dbc50aa8ca85cfabde2af8a21b13e

2 Likes

hmm so to understand it right…the example ThreadedPixelBufferexample demonstrates how a texture (of the fbo) can be copied to another thread by the use of map and unmap of pibo.

However i dont understand why this stops working as soon as i draw the camera into the FBO like this

if (camera.isFrameNew()) {
	fbo.begin();
	ofClear(0, 255);
	ofSetColor(100);
	ofSetColor(255);
	camera.draw(0, 0);
	fbo.end();
} 

when i do this it all stops working - is it because of the thing you mentioned with the video callback?

The thing is that i need to run the video input on a shader and then translate its fbo texture to this other thread doing quite expensive opencv stuff.

Is PBO the right choice here?

anyway any help would be nice!!

mio

Ok sorry to bump this thread but im still hanging on the same issue.

I dont know how to use pbo for ulpoading camera texture and reading pixels in a different thread.

if i draw my texture directly into the fbo as shown above it doesnt work. If i upload my camera texture directly it also doesnt work.

If someone could jump in would be crazy help

Thx for reading

Mio

What you are trying to do seems to be different to the original post, can you better open a new thread explaining what you want to do instead? it would be much easier to answer you that way