首页 > 解决方案 > 用 pbo 生成 mipmap 太慢?

问题描述

我正在开发一个类似视频的应用程序。
为了加快贴图上传的速度,我使用pbo来增加它。
为了处理锯齿伪影,我使用 mipmap 来帮助解决这个问题。
好吧,让我给你看主要代码

// 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);

}

结果是帧率显着下降。
我认为原因是生成 mipmap 会破坏使用 pbo 异步上传纹理的优势。
据我所知,没有任何方法可以在纹理上传完成时自动生成 mipmap。
有没有提前?

标签: openglmipmapspbo

解决方案


推荐阅读