[英]OpenGL Multithreaded sample in Ubuntu virtualbox
我在啟用3D加速的vbox上遇到OpenGL示例的怪異問題
禁用3D加速后,此應用程序可在vbox上正常運行 ,我也在獨立的Linux PC上也進行了檢查。 如果在啟用3D加速的情況下運行相同的代碼,則它無法獲取出現錯誤的GL函數指針-function no-op
該應用程序很簡單,主線程創建2個線程。
這是示例應用程序的代碼。
#include<stdio.h>
#include<stdlib.h>
#include<X11/X.h>
#include<X11/Xlib.h>
#include<GL/gl.h>
#include<GL/glx.h>
//#include<GL/glu.h>
#include <dlfcn.h> /*dlopen*/
#include <pthread.h>
#include <unistd.h> /*sleep*/
Display *dpy;
Display *dpy2;
Window root;
GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
XVisualInfo *vi;
XVisualInfo *vi2;
Colormap cmap;
XSetWindowAttributes swa;
Window win;
GLXContext glc;
XWindowAttributes gwa;
XEvent xev;
bool render;
void DrawAQuad()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1., 1., -1., 1., 1., 20.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);
glTranslatef(0.0, 0.0, -10.0);
glBegin(GL_QUADS);
glColor3f(1., 0., 0.); glVertex3f(-.75, -.75, 0.);
glColor3f(0., 1., 0.); glVertex3f( .75, -.75, 0.);
glColor3f(0., 0., 1.); glVertex3f( .75, .75, 0.);
glColor3f(1., 1., 0.); glVertex3f(-.75, .75, 0.);
glEnd();
}
void *CreateMainWindow(void* threadID)
{
dpy = XOpenDisplay(NULL);
if(dpy == NULL)
{
printf("\n\tWindow Thread: cannot connect to X server\n\n");
exit(0);
}
root = DefaultRootWindow(dpy);
printf("\n *** CreateWindow: xopendisplay over *** \n");
vi = (XVisualInfo*)glXChooseVisual(dpy, 0, att);
if(vi == NULL)
{
printf("\n\tWindow Thread: no appropriate visual found\n\n");
exit(0);
}
else
{
printf("\n\tWindow Thread: visual %p selected\n", (void *)vi->visualid);
}
cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
swa.colormap = cmap;
swa.event_mask = ExposureMask | KeyPressMask;
win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);
XMapWindow(dpy, win);
XStoreName(dpy, win, "VERY SIMPLE APPLICATION");
while(1)
{
XNextEvent(dpy, &xev);
printf("\nXEVENT\n");
if(xev.type == Expose)
{
render = true;
}
else if(xev.type == KeyPress)
{
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
render = false;
break;
//exit(0);
}
}
}
void *RenderThread(void* threadID)
{
vi2 = (XVisualInfo*)glXChooseVisual(dpy2, 0, att);
printf("\n\tRenderThread : visual %p selected\n", (void *)vi2->visualid);
glc = (GLXContext)glXCreateContext(dpy2, vi2, NULL, GL_TRUE);
glXMakeCurrent(dpy2, win, glc);
glEnable(GL_DEPTH_TEST);
while(render)
{
//XGetWindowAttributes(dpy, win, &gwa);
glViewport(0, 0, 600, 600);
DrawAQuad();
glXSwapBuffers(dpy2, win);
} /* this closes while(render) */
glXMakeCurrent(dpy2, None, NULL);
glXDestroyContext(dpy2, glc);
XCloseDisplay(dpy2);
}
int main(int argc, char *argv[])
{
render = true;
pthread_t thread1;
pthread_t thread2;
char *temp1;
char *temp2;
//For Async issue
if(!XInitThreads())
{
fprintf(stderr, "XInitThread failed\n");
return 0;
}
//Create Main Window
int err = pthread_create(&thread1, NULL, CreateMainWindow, (void*)temp1);
if (err != 0)
printf("\n ERROR::can't create thread1 :[%d]", err);
else
printf("\n Thread1 created successfully\n");
sleep(1); // Wait for thread 1 to complete
dpy2 = XOpenDisplay(NULL);
if(dpy2 == NULL)
{
printf("\n\tMain : cannot connect to X server\n\n");
exit(0);
}
//Create Render Thread
err = pthread_create(&thread2, NULL, RenderThread, (void*)temp2);
if (err != 0)
printf("\n ERROR::can't create thread2 :[%d]", err);
else
printf("\n Thread2 created successfully\n");
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
} /* this is the } which closes int main(int argc, char *argv[]) { */
並編譯代碼-
g++ -o quad quad.cpp -lGL -lX11 -lXmu -lXi -lpthread -lm
幫我了解問題所在
無論您在做什么,停止!
多線程+ X11 + OpenGL是一件非常棘手的事情。 如果您使用的是Xlib,幾乎不可能進行更正。 Xlib從來沒有真正做到線程安全。
無論如何,最重要的是您的程序缺少對XInitThreads
的調用,以使其至少在多線程程序中安全使用。 但是,將Xlib調用分散到多個線程仍然不安全。 這真的很重要:無論您做什么,都將所有Xlib調用僅保留到一個線程中。
OpenGL本身並不那么棘手。 但是因為OpenGL需要使用glX創建的上下文,而后者又基於Xlib構建。 通常的方法是在Xlib線程中創建OpenGL上下文,但稍后使其在渲染器線程中成為當前上下文。 但是請注意,如果您獲得了間接渲染上下文,則所有OpenGL調用都將通過X11,這可能意味着通過Xlib,並且情況將再次變得不穩定。
由於種種混亂,最簡單的解決方案是:使所有圖形和窗口與一個線程相關。 如果將OpenGL操作放入與其余GUI操作分開的線程中,則沒有任何收獲(從技術上講,OpenGL也執行GUI操作)。 如果要使用多線程,則可以將if用於有意義的事情並發執行,例如音頻,物理模擬等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.