有没有人有 PBO(像素缓冲区对象)+ SDL2.0(简单 DirectMedia 层)+ OpenGL 的工作示例?
原因是使用 GLGetPixels 获得异步 GPU -> CPU 下载,从而获得性能提升。
这是我的尝试。use_pbo = false 或 true 根本没有测量差异。而且我之前在同一台机器上使用过带有 GLUT 的 PBO,所以我知道我的硬件支持它。
我看了很多http://www.songho.ca/opengl/gl_pbo.html教程,但没有看到我做错了什么。
bool use_pbo = true; //remember to wait a bit for fps to pick up speed
if(SDL_Init(SDL_INIT_EVENTS) != 0) throw "SDL_Init";
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
sdl_window = SDL_CreateWindow("", 10, 20, window_width, window_height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
sdl_gl_context = SDL_GL_CreateContext(sdl_window);
SDL_GL_MakeCurrent(sdl_window, sdl_gl_context);
vertical_sync(false);
{
glGenBuffers(2, pbos);
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbos[0]);
glBufferData(GL_PIXEL_PACK_BUFFER, pbo_width*pbo_height*4, 0, GL_STREAM_READ);
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbos[1]);
glBufferData(GL_PIXEL_PACK_BUFFER, pbo_width*pbo_height*4, 0, GL_STREAM_READ);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
}
//render to default frame buffer
{
glDrawBuffer(GL_BACK);
glClearColor(0.1, 0.2, 0.3, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(simple_shader);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, transparent_texture);
glBegin(GL_TRIANGLE_STRIP);
//textcoord, color, position
glVertexAttrib2f(2, 0, 1); glVertexAttrib4f(1, 1, 0, 0, 1); glVertexAttrib2f(0, -1, -1); //bottom left
glVertexAttrib2f(2, 1, 1); glVertexAttrib4f(1, 0, 1, 0, 1); glVertexAttrib2f(0, +1, -1); //bottom right
glVertexAttrib2f(2, 0, 0); glVertexAttrib4f(1, 0, 0, 1, 1); glVertexAttrib2f(0, -1, +1); //top left
glVertexAttrib2f(2, 1, 0); glVertexAttrib4f(1, 1, 1, 0, 1); glVertexAttrib2f(0, +1, +1); //top right
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glUseProgram(0);
glDisable(GL_BLEND);
while(true)
{
if(use_pbo)
{
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbos[1]);
GLvoid* map_buffer_data = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
if(map_buffer_data)
{
//glRasterPos2i(-1, -1); //default, left bottom
glRasterPos2i(0, -1);
glDrawPixels(pbo_width, pbo_height, GL_BGRA, GL_UNSIGNED_BYTE, map_buffer_data);
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
}
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
}
else
{
glRasterPos2i(0, -1); //right bottom
glDrawPixels(pbo_width, pbo_height, GL_BGRA, GL_UNSIGNED_BYTE, t.c);
}
}
}
SDL_GL_SwapWindow(sdl_window);
//cycle buffers
{
GLuint t = pbos[1];
pbos[1] = pbos[0];
pbos[0] = t;
}