Linux上使用专有的Nvidia驱动程序时,多个OpenGLX渲染上下文会失败。

26
当我尝试在单独的线程上运行超过128个当前的OpenGLX渲染上下文时,调用glXMakeCurrent开始失败。
Display *display = XOpenDisplay(":0")
Window root_win = RootWindow(display, screen);
Window win = XCreateWindow (display, root_win, ...)
GLXContext context = glXCreateContext(display, visinfo, 0, True);

glXMakeCurrent(display, win, context); <---- Fails here on 128th

这个问题只会在专有的 Nvidia 驱动和 Nvidia 显卡上出现。我使用 Intel 显卡无法复现该问题。
复现代码 glx.cpp:
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <GL/glxext.h>
#include <string.h>
#include <unistd.h>
#include <thread>
#include <vector>
#include <mutex>
#include <condition_variable>
#include <chrono>

#define MAX_CONTEXTS 200;

std::mutex mutex;
std::condition_variable cond;
bool will_stop = false;

int numSuccessfulContexts = 0;
#define EXIT_IF(condition, ...) if (condition) { printf(__VA_ARGS__); exit(EXIT_FAILURE);}
#define RETURN_IF(condition, ...) if (condition) { printf(__VA_ARGS__); stop(); return; }

void stop() {
    std::lock_guard<std::mutex> lk(mutex);
    will_stop = true;
    cond.notify_all();
}

void createWindow() {
    /* Init X and GLX */
    Display *display = XOpenDisplay(":0.0");
    RETURN_IF(!display, "Cannot open X display\n");
    int screen = DefaultScreen(display);
    Window root_win = RootWindow(display, screen);
    RETURN_IF(!glXQueryExtension(display, 0, 0),"X Server doesn't support GLX extension\n");
    /* Pick an FBconfig and visual */
    static const int attributeList[] = { None };
    int fbcount;
    GLXFBConfig *fbconfig = glXChooseFBConfig(display, screen, attributeList, &fbcount);

    EXIT_IF(!fbconfig, "Failed to get GLXFBConfig\n");
    XVisualInfo *visinfo = glXGetVisualFromFBConfig(display, *fbconfig);
    EXIT_IF(!visinfo, "Failed to get XVisualInfo\n");
    /* Create the X window */
    XSetWindowAttributes winAttr ;
    winAttr.colormap = XCreateColormap(display, root_win, visinfo->visual, AllocNone);
    unsigned int mask = CWColormap;
    Window win = XCreateWindow (display, root_win, 256, 64, 320, 320, 0,
        visinfo->depth, InputOutput, visinfo->visual, mask, &winAttr) ;
    /* Create an OpenGL context and attach it to our X window */
    GLXContext context = glXCreateContext(display, visinfo, 0, True);
    EXIT_IF(!context, "Could not create GL context\n");
    RETURN_IF(! glXMakeCurrent(display, win, context), "glXMakeCurrent failed 1. \n");
    RETURN_IF(!glXIsDirect (display, glXGetCurrentContext()), "Indirect GLX rendering context obtained\n");
    RETURN_IF(!glXMakeCurrent(display, win, context), "glXMakeCurrent failed 2.\n");

    numSuccessfulContexts++;

    std::unique_lock<std::mutex> lk(mutex);
    cond.wait(lk, [] {return will_stop;});
}

int main(int argc, char *argv[]) {
    std::vector<std::thread> ts;
    printf("Starting, your computer might become unresponsive...\n");

    int maxContexts = MAX_CONTEXTS;
    while (maxContexts--) {
    ts.push_back(std::thread(&createWindow));
    }

    {
    std::unique_lock<std::mutex> lk(mutex);
    cond.wait_for(lk, std::chrono::seconds(10), []{return will_stop;});
    }

    if (!will_stop) {
    stop();
    }

    for (auto& v: ts) {
    v.join();
    }
    printf("Done. Max concurrent contexts: %d\n", numSuccessfulContexts);
    return EXIT_SUCCESS;
}

构建和运行:

g++ -std=c++11 glx.cpp -L/usr/lib/nvidia-375 -lGL -lX11 -lGLU -lGLX -lpthread -o glx && ./glx

8
可能是驱动程序的错误,但更有可能的是驱动程序拒绝提供那么多上下文,并在那里设置了一个硬限制。首先,为什么要创建那么多上下文?我没看到需要那么多上下文的好理由。OpenGL上下文相当昂贵且在它们之间切换也不便宜。 - datenwolf
5
哦,你为什么要创建这么多线程呢?除非你有一台拥有100个CPU线程以上的机器,否则这是没有意义的。 - datenwolf
感觉限制来自于驱动程序。我们尝试了不同的Nvidia Tesla/GTX GPU,但结果相同。 - Utku Zihnioglu
@datenwolf:当然,我的想法远非最佳,但是OP正在寻求一个临时解决方案,直到他摆脱那些疯狂的上下文。 - Ripi2
3
发现这个链接:https://dev59.com/bm445IYBdhLWcg3wRoLH问题:创建OpenGL渲染上下文的数量是否存在限制?回答:是的,存在限制,但具体数量因系统而异。OpenGL渲染上下文需要分配显存等资源,同时还需要操作系统支持。在处理大量渲染上下文时,可能会遇到性能瓶颈或者资源不足等问题。 - Ripi2
显示剩余10条评论
1个回答

1

正如评论中所讨论的,似乎您因为正在进行一些非常不寻常和意外的操作而遇到了驱动程序限制。我回答这个问题是为了将其从未回答的问题列表中删除。


我同意你的观点。不过,这个问题似乎已经收集到了足够的反馈和需求。我宁愿保持这个问题的开放状态,以防有人提出解决方案或可靠的解决方法。 - Utku Zihnioglu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接