Wooni
Wooni

Reputation: 501

Is there any alternative to read pixel values in OpenGL instead of using glReadPixels?

I'm using glReadPixel for OpenGL window (width*height). To apply my algorithm, I have to read depth buffer 2 times and color buffer(frame) one time. However

glReadPixels(j ,i ,1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &value);

is too slow to use. Is there any way to fast up?

Upvotes: 2

Views: 394

Answers (1)

Columbo
Columbo

Reputation: 6766

glReadPixels(j ,i ,1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &value);

'i' and 'j' are typical names for loop variables. Along with the fact that you mention 1-2 seconds as acceptable latency for your use case, I'm going to jump to a conclusion that you're reading many pixels one at a time in loop. e.g. you're calling glReadPixels 1280*720=921600 times. Apologies if that was incorrect.

Normally, glReadPixels is considered slow because the CPU is forced to stall until the GPU has finished rendering. In this context glReadPixels might stall for many milliseconds and badly affect realtime framerates, but I wouldn't expect anything more than 50-100ms of delay at the most (usually much less).

In your case, I think it's slow because glReadPixels has a large per-call overhead. If you need to read a whole bunch of pixels, then allocate a larger chunk of memory and read them with a single call to glReadPixels using the width and height parameters. It will be orders of magnitude faster.

Upvotes: 4

Related Questions