Read pixel data from default framebuffer in OpenGL: Performance of FBO vs. PBO
<p>My goal is to read the contents of the default OpenGL framebuffer and store the pixel data in a <code>cv::Mat</code>. Apparently there are <em>two</em> different ways of achieving this:</p>

<p>1) Synchronous: use FBO and <code>glRealPixels</code></p>

<pre><code>cv::Mat a = cv::Mat::zeros(cv::Size(1920, 1080), CV_8UC3);
glReadPixels(0, 0, 1920, 1080, GL_BGR, GL_UNSIGNED_BYTE, a.data);
</code></pre>

<p>2) Asynchronous: use PBO and <code>glReadPixels</code></p>

<pre><code>cv::Mat b = cv::Mat::zeros(cv::Size(1920, 1080), CV_8UC3);
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo_userImage);
    glReadPixels(0, 0, 1920, 1080, GL_BGR, GL_UNSIGNED_BYTE, 0);
    unsigned char* ptr = static_cast&lt;unsigned char*&gt;(glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY));
    std::copy(ptr, ptr + 1920 * 1080 * 3 * sizeof(unsigned char), b.data);
    glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
</code></pre>

<p>From all the information I collected on this topic, the asynchronous version 2) should be much faster. However, comparing the elapsed time for both versions yields that the differences are often times minimal, and sometimes version 1) events outperforms the PBO variant.</p>

<p>For performance checks, I've inserted the following code (based on <a href="https://stackoverflow.com/questions/2808398/easily-measure-elapsed-time">this</a> answer):</p>

<pre><code>std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
....
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout &lt;&lt; "Time difference = " &lt;&lt; std::chrono::duration_cast&lt;std::chrono::microseconds&gt;(end - begin).count() &lt;&lt; std::endl;
</code></pre>

<p>I've also experimented with the <em>usage hint</em> when creating the PBO: I didn't find much of difference between <code>GL_DYNAMIC_COPY</code> and <code>GL_STREAM_READ</code> here.</p>

<p>I'd be happy for suggestions how to increase the speed of this pixel read operation from the framebuffer even further.</p>