I attempted to implement a readback of a drawn framebuffer for auto-exposure, but no matter what I try, either glGetTexImage or glMapBuffer stalls.
Am I doing this wrong, or this this an issue with my particular machine, because I can’t observe the stalling on my notebook.
struct ExposureReadback {
int buffers = 3;
int counter = 0;
std::vector<DownloadPbo> pbos;
ExposureReadback () {
for (int i=0; i<buffers; ++i) {
pbos.push_back( DownloadPbo{prints("ExposureReadback[%d]", i)} );
}
counter = 0;
}
bool readback (Render_Texture& tex, int2 full_res, lrgb* weighted_average) {
int cur_buf = counter;
counter = wrap(counter+1, buffers);
int oldest_buf = counter;
//...
{ // Trigger read into current pbo
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbos[cur_buf]);
glBufferData(GL_PIXEL_PACK_BUFFER, size, nullptr, GL_STREAM_DRAW);
glBindTexture(GL_TEXTURE_2D, tex);
glGetTexImage(GL_TEXTURE_2D, mip, GL_RGB, GL_FLOAT, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
}
bool readback_avail = false;
{ // Map now available oldest pbo
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbos[oldest_buf]);
auto* mapped = (lrgb*)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
if (mapped) { // returns null for first few frames
//...
readback_avail = true;
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glInvalidateBufferData(pbos[oldest_buf]);
}
}
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
return readback_avail;
}
};
Profiler Results: