qt5.9.1 QOpenGLWidget绘图崩溃到nvoglv64.dll:drvpresentBuffer();



  • 程序中OpenGLWidget继承QOpenGLWidget和QOpenGLFunctions,同时打开多个OpenGLWidget播放视频(视频解码通过ffmpeg完成,解码出来的YUV数据存储在AVFrame结构体中。)
    绘图类代码如下:
    #define ATTRIB_VERTEX 3
    #define ATTRIB_TEXTURE 4

    #include "openglwidget.h"
    #include <QOpenGLShaderProgram>
    #include <QOpenGLShader>
    #include <QOpenGLTexture>

    const char *vsrc =
    "#version 410\n"
    "in vec4 vertexIn;\n"
    "in vec2 textureIn;\n"
    "out vec2 textureOut;\n"
    "void main(void)\n"
    "{\n"
    " gl_Position = vertexIn;\n"
    " textureOut = textureIn;\n"
    "}";

    const char *fsrc =
    "#version 410\n"
    "in vec2 textureOut;\n"
    "out vec4 fragColor;\n"
    "uniform sampler2D tex_y;\n"
    "uniform sampler2D tex_u;\n"
    "uniform sampler2D tex_v;\n"
    "void main(void)\n"
    "{\n"
    " vec3 yuv;\n"
    " vec3 rgb;\n"
    " yuv.x = texture(tex_y, textureOut).r;\n"
    " yuv.y = texture(tex_u, textureOut).r - 0.5;\n"
    " yuv.z = texture(tex_v, textureOut).r - 0.5;\n"
    " rgb = mat3( 1, 1, 1,\n"
    " 0, -0.21482, 2.12798,\n"
    " 1.28033, -0.38059, 0) * yuv;\n"
    " fragColor = vec4(rgb, 1);\n"
    "}";

    OpenGLWidget::OpenGLWidget(QWidget *parent)
    {
    setParent(parent);
    y_texture = false;
    u_texture = false;
    v_texture = false;
    paint = false;
    width = 0;
    height = 0;
    this->setMouseTracking(true);
    setUpdateBehavior(QOpenGLWidget::PartialUpdate);
    }

    void OpenGLWidget::clearWidget()
    {
    makeCurrent();
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    }

    void OpenGLWidget::initializeGL()
    {
    initializeOpenGLFunctions();
    m_shader = prepareShaderProgram();

    textureUniformY = glGetUniformLocation(m_shader, "tex_y");
    textureUniformU = glGetUniformLocation(m_shader, "tex_u");
    textureUniformV = glGetUniformLocation(m_shader, "tex_v");
    
    
    // Create a interleaved triangle (vec3 position, vec3 color)
    float vertexPoints[] ={
        -1.0f, -1.0f,0.0f, 1.0f,
        1.0f, -1.0f,1.0f, 1.0f,
        -1.0f, 1.0f,0.0f, 0.0f,
        1.0f, 1.0f,1.0f, 0.0f,
    };
    
    glGenVertexArrays(1, &m_vertexBuffer);
    glBindVertexArray(m_vertexBuffer);
    
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, 4 * 4 * sizeof(float), vertexPoints, GL_STATIC_DRAW);
    vertextAttribute = glGetAttribLocation(m_shader, "vertexIn");
    textureAttribute = glGetAttribLocation(m_shader, "textureIn");
    glEnableVertexAttribArray(vertextAttribute);
    glVertexAttribPointer(vertextAttribute, 2, GL_FLOAT, GL_FALSE, sizeof(float)*4, (const GLvoid *)0);
    glEnableVertexAttribArray(textureAttribute);
    glVertexAttribPointer(textureAttribute, 2, GL_FLOAT, GL_FALSE, sizeof(float)*4, (const GLvoid *)(sizeof(float)*2));
    
    
    //Init Texture
    glGenTextures(1, &id_y);
    glBindTexture(GL_TEXTURE_2D, id_y);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    
    glGenTextures(1, &id_u);
    glBindTexture(GL_TEXTURE_2D, id_u);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    
    glGenTextures(1, &id_v);
    glBindTexture(GL_TEXTURE_2D, id_v);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    

    }

    GLuint OpenGLWidget::prepareShaderProgram()
    {
    GLuint program = glCreateProgram();

    //vertex shader
    GLuint vertexShader = glCreateShader( GL_VERTEX_SHADER);
    glShaderSource( vertexShader, 1, (const GLchar**)&vsrc, NULL );
    glCompileShader( vertexShader );
    GLint  compiled;
    glGetShaderiv( vertexShader, GL_COMPILE_STATUS, &compiled );
    glAttachShader( program, vertexShader );
    
    //fragment shader
    GLuint fragmentShader = glCreateShader( GL_FRAGMENT_SHADER);
    glShaderSource( fragmentShader, 1, (const GLchar**)&fsrc, NULL );
    glCompileShader( fragmentShader );
    glGetShaderiv( fragmentShader, GL_COMPILE_STATUS, &compiled );
    glAttachShader( program, fragmentShader );
    
    /* Link output */
    glBindFragDataLocation(program, 0, "fragColor");
    /* link  and error check */
    glLinkProgram(program);
    
    GLint  linked;
    glGetProgramiv( program, GL_LINK_STATUS, &linked );
    if ( !linked ) {
        qWarning() << "Shader program failed to link";
    }
    glUseProgram(program);
    return program;
    

    }

    bool OpenGLWidget::getPaint() const
    {
    return paint;
    }

    void OpenGLWidget::setPaint(bool value)
    {
    paint = value;
    }

    void OpenGLWidget::paintGL()
    {
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    if(paint)
    {
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, id_y);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, this->width, this->height, 0, GL_RED, GL_UNSIGNED_BYTE, frame.data[0]);
            glUniform1i(textureUniformY, 0);
    
            glActiveTexture(GL_TEXTURE1);
            glBindTexture(GL_TEXTURE_2D, id_u);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, this->width/2, this->height/2, 0, GL_RED, GL_UNSIGNED_BYTE, frame.data[1]);
            glUniform1i(textureUniformU, 1);
    
    
    
            glActiveTexture(GL_TEXTURE2);
            glBindTexture(GL_TEXTURE_2D, id_v);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, this->width/2, this->height/2, 0, GL_RED, GL_UNSIGNED_BYTE, frame.data[2]);
            glUniform1i(textureUniformV, 2);
    
        // Draw stuff
        glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
    }
    

    }

    void OpenGLWidget::resizeGL(int w, int h)
    {
    show_width = w;
    show_height = qMax(h,1);
    glViewport(0,0,show_width,show_height);
    }



  • 0_1508382122722_QQ图片20171019110154.png



  • 翻x之后查资料,有2种说法,一种是让关闭nv的线程优化选项(一看就不靠谱),另一种说是显存泄露(??)。线程优化就不用说了,关了之后仍然会崩溃。特来此求教。。。



  • 想问一下,会不会是因为多个窗口同时绘图、访问显存出现了问题?



  • 而且不崩溃的时候创建多个openglwidget窗口时,新窗口的创建会导致老窗口闪烁一下(有时图像绘制出现颜色变化),不解,求大家指导。



  • 蹦到绘图里的问题找到了。是因为frame结构体的多线程读写导致的。但是现在又开始崩溃到mf.dll is required for hardware video decoding。。。无语,翻资料去了。


  • 网站研运

    @flowerlove 多线程来进行渲染?我看到成功的似乎只有Qt的Scene Graph渲染框架,而且这个也分情况,在某些平台下多线程渲染仍然是不足的。


登录后回复
 

与 萌梦社区 的连接断开,我们正在尝试重连,请耐心等待