How to get shader version from QOpenGLShader?

232 views Asked by At

I am using QT to load, compile and link OpenGL shaders.

I need to perform specific operations depending on the GLSLversion used in the shader code

Is it possible to recover the version from the QOpenGLShader interface without actually getting the shader source code and parsing it to detect the version line?

2

There are 2 answers

0
gan_ On BEST ANSWER

Neither Qt nor OpenGL gives access to the shader version (in the case of Qt, probably because it does not know it and does not need to). You are left with parsing the source code, however since #version needs to be on its own line, you should be able to extract what you want with just a split and basic string operations.

0
Pierluigi On

Here is the solution that reads the version parsing the source code. It might be useful to someone.

int extractVersion(QOpenGLShader* s){
    assert(s);

    //get the source code
    QString code = QString::fromUtf8(s->sourceCode());

    QRegExp versionExp("#version\\s+(\\d+)");
    bool b = code.contains(versionExp);

    if (!b) return 100;
    else return versionExp.cap(1).toInt();
}