【发布时间】:2017-02-08 21:46:59
【问题描述】:
世界您好,感谢您花时间阅读本文!
我正在用 GTK2/3 + OpenGL 编写一个程序,我运行了两个版本的程序:
- (a) GTK+2 + GtkGlext 扩展 -> 效果很好!
- (b) GTK+3 + LibX11 -> 工作正常!
一切看起来都很好,除了 (a) 中的渲染明显快于 (b) 中的渲染......我不知道为什么。 以下是一些用于创建 OpenGL 上下文的代码部分示例:
-
(a)
// To create the context, and the associated GtkWidget GdkGLConfig * glconfig = gdk_gl_config_new_by_mode (GDK_GL_MODE_RGBA | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE); GtkWidget * drawing_area = gtk_drawing_area_new (); gtk_widget_set_gl_capability (drawing_area, glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE); g_signal_connect (G_OBJECT (drawing_area), "expose-event", G_CALLBACK (on_expose), data); // And later on to draw using the OpenGL context: gboolean on_expose (GtkWidget * widg, GdkEvent * event, gpointer data) { GdkGLContext * glcontext = gtk_widget_get_gl_context (widg); GdkGLDrawable * gldrawable = gtk_widget_get_gl_drawable (widg); if (gdk_gl_drawable_gl_begin (gldrawable, glcontext)) { // OpenGL instructions to draw here ! gdk_gl_drawable_swap_buffers (view -> gldrawable); gdk_gl_drawable_gl_end (view -> gldrawable); } return TRUE; } -
(b)
// To create the GtkWidget GtkWidget * drawing_area = gtk_drawing_area_new (); // Next line is required to avoid background flickering gtk_widget_set_double_buffered (drawing_area, FALSE); g_signal_connect (G_OBJECT (drawing_area), "realize", G_CALLBACK(on_realize), data); g_signal_connect (G_OBJECT (drawing_area), "draw", G_CALLBACK(on_expose), data); // To create the OpenGL context GLXContext glcontext; G_MODULE_EXPORT void on_realize (GtkWidget * widg, gpointer data) { GdkWindow * xwin = gtk_widget_get_window (widg); GLint attr_list[] = {GLX_DOUBLEBUFFER, GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, None}; XVisualInfo * visualinfo = glXChooseVisual (GDK_WINDOW_XDISPLAY (xwin), gdk_screen_get_number (gdk_window_get_screen (xwin)), attr_list); glcontext = glXCreateContext (GDK_WINDOW_XDISPLAY (xwin), visualinfo, NULL, TRUE); xfree (visualinfo); } // To Draw using the OpenGL context G_MODULE_EXPORT gboolean on_expose (GtkWidget * widg, cairo_t * cr, gpointer data) { GdkWindow * win = gtk_widget_get_window (widg); if (glXMakeCurrent (GDK_WINDOW_XDISPLAY (xwin), GDK_WINDOW_XID (xwin), glcontext)) { // OpenGL instructions to draw here ! glXSwapBuffers (GDK_WINDOW_XDISPLAY (win), GDK_WINDOW_XID (win)); } return TRUE; }
试图理解为什么 (a) 比 (b) 快,我下载了 GtkGLext 库的源代码,阅读它们,发现命令与调用 X11 完全相同。 现在我的想法是(b)中的以下行
gtk_widget_set_double_buffered (drawing_area, FALSE);
正在搞乱渲染,然后我无能为力... 或者可能解释我注意到的行为的 OpenGL 上下文之间存在差异,如果我朝这个方向跟进,我需要用尽可能多的细节比较两个上下文......到目前为止,我选择了似乎是获取一些信息的最常用方式:
OpenGL Version : 3.0 Mesa 12.0.3
OpenGL Vendor : nouveau
OpenGL Renderer : Gallium 0.4 on NVCF
OpenGL Shading Version : 1.30
Color Bits (R,G,B,A) : 8, 8, 8, 0
Depth Bits : 24
Stencil Bits : 0
Max. Lights Allowed : 8
Max. Texture Size : 16384
Max. Clipping Planes : 8
Max. Modelview Matrix Stacks : 32
Max. Projection Matrix Stacks : 32
Max. Attribute Stacks : 16
Max. Texture Stacks : 10
Total number of OpenGL Extensions : 227
Extensions list:
N°1 : GL_AMD_conservative_depth
N°2 : GL_AMD_draw_buffers_blend
...
但是两种上下文都返回完全相同的信息......
感谢您已经到达那里......现在我的问题是:
有没有办法尽可能多地输出有关 OpenGL 上下文的信息,以及如何输出?
我欢迎任何其他关于我在做什么的建议!
S.
PS:我正在为 GTK3 使用 GtkGLArea 小部件,但正如 here 所述,我还没有。
[编辑] 一些 OpenGL 指令:
// OpenGL instructions to draw here !
glLoadIdentity ();
glPushMatrix ();
// d is the depth ... calculated somewhere else
glTranslated (0.0, 0.0, -d);
// Skipping the rotation part for clarity, I am using a quaternion
rotate_camera ();
// r, g, b and a are GLFloat values
glClearColor (r,g,b,a);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glDisable (GL_LIGHTING);
int i;
// nbds is the number of chemical bonds
GLfloat * lineVertices;
// This is "roughly" what I do to draw chemical bonds, to give you an idea
for (i=0; i<nbds;i++)
{
// get_bonds (i) gives backs a 6 float array
lineVertices = get_bonds(i);
glPushMatrix();
glLineWidth (1.0);
glEnableClientState (GL_VERTEX_ARRAY);
glVertexPointer (3, GL_FLOAT, 0, lineVertices);
glDrawArrays (GL_LINES, 0, 2);
glDisableClientState (GL_VERTEX_ARRAY);
glPopMatrix();
}
glEnable (GL_LIGHTING);
[/编辑]
【问题讨论】:
-
据我了解,GTK 端的双缓冲只是将影响 GTK 小部件缓冲区的所有内容渲染到屏幕外,然后再呈现。我不明白为什么这会以任何方式干扰 GLX 或为什么它应该施加严厉的惩罚。您确定在第二种情况下获得 direct 渲染上下文吗?能否请您使用
LIBGL_ALWAYS_INDIRECT=1运行第一个应用程序,看看性能是否同样糟糕? -
你能发布一些实际的 OpenGL 代码(你用
// OpenGL instructions to draw here !替换的代码)吗? -
将您的应用程序运行到apitrace 并找出不同之处:-)
-
大家好,首先我要道歉没有早点回答,我写完我的问题后周末离开了:-P
-
然后按照它们出现的顺序回答你的问题,@thokra,没有办法运行我的程序,两个版本中的任何一个,使用 LIBGL_ALWAYS_INDIRECT=1 选项......我收到这种消息'收到 X Window 系统错误': a)
The error was 'BadValue (integer parameter out of range for operation)'. (Details: serial 12479 error_code 2 request_code 154 (GLX) minor_code 3)和 b):The error was 'GLXBadContext'. (Details: serial 8563 error_code 170 request_code 154 minor_code 6)