kaludis
kaludis

Reputation: 21

GLSL Shader compilation error

I'm trying to compile simple shader on my linux machine with Radeon HD 5470 video card and fglrx AMD driver.

My vertex shader code

#version 330 core

layout(location = 0) in vec3 vertexPosition_modelspace;

void main()
{

    gl_Position.xyz = vertexPosition_modelspace;
    gl_Position.w = 1.0;

}

Read code from file

void Shader::load_from_file(const std::string& file)
{
    std::ifstream is(file, std::ios_base::in);
    if (is.is_open()) {
        std::string line{""};
        while(std::getline(is, line)) {
            // program_code_ is a std::string member
            program_code_ += "\n" + line; 
        }

        is.close();
    } else {
         throw Exception("Could not open shader source code file");
    }       
}

Try to compile

void Shader::build_shader()
{   
    const GLchar* tmp = program_code_.c_str();    
    const GLint tmplen = program_code_.length();

    std::cout << "Shader code: " << tmp << std::endl;

    glShaderSource(shader_handler_, 1, &tmp, &tmplen);
    CHECK_ERR();

    glCompileShader(shader_handler_);
    CHECK_ERR();
    //...
}

And have error from glGetShaderInfoLog

Exception caught: Vertex shader failed to compile with the following errors:
ERROR: 0:1: error(#132) Syntax error: "<" parse error
ERROR: error(#273) 1 compilation errors.  No code generated

But before I calling glShaderSource, I print to stdout value of tmp pointer and it seems to valid shader code:

Shader code: 
#version 330 core

layout(location = 0) in vec3 vertexPosition_modelspace;

void main()
{

    gl_Position.xyz = vertexPosition_modelspace;
    gl_Position.w = 1.0;

}

My code doesn't read garbage from memory, but I can't understand what's wrong.

Also

 % glxinfo | grep vertex_program
 % GL_ARB_vertex_program

Upvotes: 2

Views: 992

Answers (1)

leemes
leemes

Reputation: 45665

Reading the file line by line, and concatenating these lines, seems to be the problem.

I don't know how this introduces an error which matches the error message you got from the shader compiler, but as suggested in the comments, reading the whole file at once solves the problem.

The following lines reads from a file stream is by utilizing the function rdbuf and a stringstream (you need to #include <sstream>):

std::ostringstream contents;
contents << is.rdbuf();
program_code_ = contents.str();

For more information about this method, and a comparison to other methods, see http://insanecoding.blogspot.de/2011/11/how-to-read-in-file-in-c.html.

Upvotes: 1

Related Questions