简体   繁体   English

使用专有的Nvidia驱动程序在Linux上出现多个OpenGLX渲染上下文失败

[英]Multiple OpenGLX rendering contexts fail on Linux with proprietary Nvidia Drivers

When I try to run over 128 current OpenGLX rendering contexts on individual threads, the call to glXMakeCurrent starts failing. 当我尝试在各个线程上运行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

This issue only occurs with proprietary Nvidia drivers and Nvidia GPUs. 此问题仅发生在专有的Nvidia驱动程序和Nvidia GPU上。 I was not able to reproduce with Intel GPUs. 我无法使用英特尔GPU重现。

Reproduction code glx.cpp : 复制代码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;
}

Build & Run: 构建和运行:

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

As discussed in the comments, it seems you're hitting a driver limitation because you're doing something highly unusual and unexpected. 正如评论中所讨论的那样,你似乎正在达到驾驶员的限制,因为你正在做一些极不寻常和意想不到的事情。 I'm answering this to remove it from the list of unanswered questions. 我正在回答这个问题,将其从未回答的问题列表中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM