informer2000
informer2000

Reputation: 409

GLSL Shader Program Randomly Fails to Compile

I'm experiencing a strange behaviour in my OpenGL application. I generate a number of GLSL programs during the initialization of the program. The shader programs are read from text files and the programs are compiled and linked. However, I randomly encounter compilation errors for one of the shader programs (a pass-through vertex shader). I cannot understand why the program loads perfectly fine and the shader program successfully compiles several times but fails to do in other times!

Here is the shader code:

#version 330

// vertex position in the model space
layout(location = 0) in vec3 inPosition;  
layout(location = 1) in vec2 inTexCoord;

// will be interporlated for each fragment
smooth out vec2 vTexCoord;

// uniforms
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;


void main(void) {
    gl_Position = projectionMatrix * modelViewMatrix * vec4(inPosition, 1.0);
    vTexCoord = inTexCoord;
}

And here is the code for compiling the shader (passThroughVertShader is a QString):

this->passThroughVertShader = glCreateShader(GL_VERTEX_SHADER);

const char *passThroughVertShaderCodeC = passThroughVertShaderCode.toStdString().c_str();
sourceCode = passThroughVertShaderCodeC;
glShaderSource(this->passThroughVertShader, 1, &sourceCode, NULL);

glCompileShader(this->passThroughVertShader);
glGetShaderiv(this->passThroughVertShader, GL_COMPILE_STATUS, &isCompiled);
if(isCompiled == GL_FALSE)
{
    qDebug("ERROR compiling pass-through vertex shader..");
    exit(-1);
}
glAttachShader(this->shaderProgram, this->passThroughVertShader);

And the function that loads it:

QString MyClass::readShaderFile(const QString &filename) {

    QString content;
    QFile file(filename);
    if (file.open(QIODevice::ReadOnly)) {
        QTextStream tStream(&file);
        content = tStream.readAll();
    }
    return content;
}

Update:

Following Andon's suggestion, I double checked and I wasn't checking the log after failure. Here is what the log says:

Error: 0(17) : error C0000: syntax error, unexpected '!', expecting "::" at token "!"

Upvotes: 2

Views: 1255

Answers (1)

informer2000
informer2000

Reputation: 409

Ok, answering myself here. Thanks to AndonM.Coleman for pointing out that I should check the error log. The output was not clear, but it lead me to another SO question that shed some light on a bug I had in the code. Apparently I was doing the right thing for compiling all other shaders in the program and somehow forgot one line when I was writing the code for this one. What I should have been doing was the following:

string passThroughVertShaderCodeS = passThroughVertShaderCode.toStdString();
const char *passThroughVertShaderCodeC = passThroughVertShaderCodeS.c_str();

In short, the toStdString() method was returning a temporary copy of the buffer. Since I'm calling c_str() on that temporary copy which gets destroyed after that line, that pointer becomes invalid. I'm not sure why it occasionally succeeded though.

Upvotes: 2

Related Questions