简体   繁体   中英

QGLShaderProgram recompiling shader

I have a working GLSL fragment shader that uses QT's GLShaderProgram class. It works just fine the first time the shader is compiled.

When I try to recompile the shader (at runtime) by using the code below, there are no compilation errors ("Shader recompiled"), but the shader display does not change. What's going on?

delete SHADER; // free up the existing shader
SHADER = new QGLShaderProgram(context);
if (SHADER->addShaderFromSourceFile(QGLShader::Fragment, ":/shaders/test_shader1.frag")) {
    if (!SHADER->link()) {
        qDebug() << SHADER->log().trimmed();
        delete SHADER;
    }
    qDebug() << "Shader recompiled";
} else {
    qDebug() << SHADER->log().trimmed();
    delete SHADER;
}

Found out what the problem was. I was using QT resources ( http://doc.qt.io/qt-5/resources.html ) to load the shader file via relative paths. Unbeknownst to my knowledge, QT caches the contents of resources the first time they are accessed, so changing the contents of the resource does not update the program.

By referencing an absolute path to the file, reloading the shader now works fine.

addShaderFromSourceFile(QGLShader::Fragment, "/home/eric/test_shader1.frag")

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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