0

I'm developing a video-like application.
In order to accelerate the speed of the texture uploading, I use pbo to increase it.
In order to deal with the aliasing artifact, I use mipmap to help this.
Well, let me show you the major code

// Step 1: Build the texture.
   // Generate texture.
   GLuint tex;
   glGenTextures(1, &tex)
  // Generate pbo.
   GLuint pbo;
   glGenBuffers(1, &pbo);
   glBufferData(GL_PIXEL_UNPACK_BUFFER, width_* height_ * channel_, 0, GL_DYNAMIC_DRAW);
   glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
  // Set tex parameters.
   glBindTexture(GL_TEXTURE_2D, tex_id);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   // Allocate memory for texture
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width_, height_, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
  // Generate the mipmap
  glGenerateMipMap(GL_TEXTURE_2D);

// Step2: For each frame, upload texture and generate mipmap.
while(true){
   // Map pbo to client memory.
  glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
  glBufferData(GL_PIXEL_UNPACK_BUFFER, width_* height_ * channel_, 0, GL_DYNAMIC_DRAW);
  mapped_buffer_ = (GLubyte*) glMapBufferRange(GL_PIXEL_UNPACK_BUFFER,  0, width_ * height_ * channel_, GL_MAP_WRITE_BIT);
  glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
   // Copy image data.
   memcpy(mapped_buffer, pointer, height_ * width * channel_);
   // Setting last parameter to 0 indicate that we use async mode to upload texture from pbo to texture. 
   glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, GL_RGB, GL_UNSIGNED_BYTE, 0);
  // Generate mip map is sync function, so it will block until texture uploading is done.
   glGenerateMipMap(GL_TEXTURE_2D);

}

The result is that frame rate dropped significantly.
I think the reason is that generating mipmap will break the advantage of async uploading texture using pbo.
As far as I know, there are no method to make mipmap to be generated automatically when texture-uploading is done.
Is there some advance?

hao li
  • 367
  • 2
  • 13
  • 1
    Generating mipmaps is never going to be fast, it involves quite heavy filtering algorithms. It's ok to make them once (or 1 time per second +-). Generating them every frame will be slow, nothing to suggest – Alexey S. Larionov Jun 30 '21 at 12:59
  • Your code is broken. Since you have not bound the PBO at the `glTexSubImage2D` call, you don';t ever use that PBO. On the other hand, you _try_ to use the PBO while it is still mapped, which is also not allowed unless you use persistent mappings (and manual synchronizatoon). As stated in the question, this code is never going to work. – derhass Jun 30 '21 at 18:49

0 Answers0