title: Texture streaming in 4K (multi-res)
body: <p>My goal is to stream single channel images in 4K at the highest possible frame rate using OpenGL. This means I have 1 texture which I'm going to update with new content very often.</p>
  
  <p>As for now I'm using PBO's to take advantage of the asynchronous texture download on the GPU. The code is basically:</p>
  
  <pre><code>// EACH TIME TEXTURE NEEDS UPDATE
  const int pboSize = 4096 * 4096 * 4;
  
  // Bind PBO to texture data source
  glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboHandle);
  // Discard data in PBO - already sent to GPU
  glBufferData(GL_PIXEL_UNPACK_BUFFER, pboSize, NULL, GL_STREAM_DRAW);
  // Map buffer to client memory
  float* data = static_cast&lt;float*&gt;(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY));
  if (data) {
      std::valarray&lt;IMG_PRECISION&gt;&amp; newImageContent = myImageArray[currentImage];
      // This seems too slow
      std::memcpy(data, &amp;newImageContent[0], newImageContent.size() * sizeof(IMG_PRECISION));
  } else {
      std::err &lt;&lt; "Failed to map PBO to client memory";
  }
  
  // Release the mapped buffer
  glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
  
  const glm::uvec3&amp; dimensions = _texture-&gt;dimensions();
  _texture-&gt;bind();
  // Send async to GPU
  glTexSubImage2D(
      _texture-&gt;type(),
      0,
      0,
      0,
      GLsizei(dimensions.x),
      GLsizei(dimensions.y),
      GLint(_texture-&gt;format()),
      _texture-&gt;dataType(),
      nullptr
  );
  
  // Set back to normal texture data source
  glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
  </code></pre>
  
  <p>This works faster, but not fast enough, than just using <code>glTexSubImage()</code> without PBO's. The bottleneck seems to be the <code>std::memcpy</code> that takes around <code>0.0211s</code>  each frame. I create the texture with <code>glTexImage2D()</code> using <code>GL_R16F</code> as internal format, <code>GL_RED</code> as format and <code>GL_FLOAT</code> as data type.</p>
  
  <p>However, I really only need 4K resolution when the texture is very close to the camera, but generating mipmaps at run-time seems too slow. Precomputing them manually would also be pretty annoying since I have lots of these images already in System RAM, loading the mipmaps together with the normal images would be too slow on the CPU side.</p>
  
  <p>Is there any other multi-resolution approach I can't think of right now, or is the only way to get a decent frame rate to use some video codec such as <code>ffmpeg</code>? I have a lot of metadata in these images when I load them from disc and would ideally want to avoid that.</p>
  
  <p><strong>EDIT</strong>:
  Target is cross-platform desktop environment using OpenGL 4.5</p>