opengl - 用 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。
有没有提前?
解决方案
推荐阅读
- android - Android AlarmManager 只重复一次
- python - AttributeError:“工人”对象没有属性“idf”
- google-app-engine - 通过 GAE 标准上的 Websockets 订阅 GraphQL 没有响应
- python - 如何在 wxPython 中强制关闭小部件
- php - Laravel 检查作业是否扩展了某个类
- webpack - 静态网页 webpack hotloader 不工作
- swift - 访问受 macOS 用户数据保护保护的数据
- javascript - 无法更新 MySQL 数据库中的单元格?
- arrays - Ruby 类型错误:没有将 Symbol 隐式转换为 Integer
- c# - 如何将物体吸引到圆柱体的表面,而不是质心?