简体   繁体   English

QGLShaderProgram重新编译着色器

[英]QGLShaderProgram recompiling shader

I have a working GLSL fragment shader that uses QT's GLShaderProgram class. 我有一个使用QT的GLShaderProgram类的工作GLSL片段着色器。 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. 当我尝试使用下面的代码重新编译着色器(在运行时)时,没有编译错误(“Shader重新编译”),但着色器显示不会改变。 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. 我使用QT资源( http://doc.qt.io/qt-5/resources.html )通过相对路径加载着色器文件。 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. 据我所知,QT在第一次访问资源时会缓存资源的内容,因此更改资源的内容不会更新程序。

By referencing an absolute path to the file, reloading the shader now works fine. 通过引用文件的绝对路径,重新加载着色器现在可以正常工作。

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

Cheers! 干杯!

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

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