Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: glShaderSource() doesn't copy the code correctly

  1. #1
    Junior Member Newbie Apoptose's Avatar
    Join Date
    Jun 2012
    Location
    Germany
    Posts
    6

    glShaderSource() doesn't copy the code correctly

    Hi,

    my Problem is, that glShaderSource() cuts the Vertex Shader code, so glCompileShader() will fail and report the following error:

    Code :
    0(11) : error C0000: syntax error, unexpected $undefined, expecting "::" at token "<undefined>"

    the Vertex Shader code is:
    Code :
    #version 400
     
    layout (location = 0) in vec3 VertexPosition;
    layout (location = 1) in vec3 VertexTexCoord;
     
    out vec3 TexCoord;
     
    void main()
    { 
        TexCoord = VertexTexCoord; 
        gl_Position = vec4(VertexPosition, 1.0);
    }

    glGetShaderSource() returns:
    Code :
    #version 400
     
    layout (location = 0) in vec3 VertexPosition;
    layout (location = 1) in vec3 VertexTexCoord;
     
    out vec3 TexCoord;
     
    void main()
    { 
        TexCoord = VertexTexCoord; 
        gl_Position = vec4(VertexPosit�

    so glShaderSource() seems to screw up with copying the String. I use the following function to load the shader:
    Code :
     const char* loadShaderAsString(const char* file){
        std::ifstream shader_file(file, std::ifstream::in);
        std::string str((std::istreambuf_iterator<char>(shader_file)), std::istreambuf_iterator<char>());
     
        return str.c_str();
    }

    printf() prints the String correctly.

    I've tested this with an NVIDIA GTS 450. Operating system is Ubuntu 12.04.

    I attached the Eclipse(Indigo) CDT Project.

    Hopefully this is the correct forum since this *might* be a bug.

    Thanks for any help,
    Apoptose
    Attached Files Attached Files
    Last edited by Apoptose; 06-14-2012 at 09:33 AM.

  2. #2
    Member Regular Contributor
    Join Date
    Apr 2009
    Posts
    268
    How do you upload the shader?

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,213
    glShaderSource itself won't cut a source string, unless you have a very very odd driver bug (and if so it would break with every other program that uses shaders too) - the most likely cause of this problem is in your own code.

    Have a look at the documentation for glShaderSource - you'll see that the last parameter is an array of string lengths. One probable cause of this is that you're just passing in the wrong lengths. Other causes are that your file loader is failing to get the full text or is injecting (or somehow otherwise picking up) garbage into the source string that somehow includes a '\0' (unlikely as printf works). Also, won't str be out of scope after loadShaderAsString returns? You're probably corrupting the stack here too, so there's something else to check.

    Set a breakpoint and run in your debugger - you'll be able to check the string that is being passed to glShaderSource, the lengths you're passing, and backtrack up the call stack until you discover the point at which it goes bad.

    Which I'll bet is not in glShaderSource...

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Dec 2007
    Location
    Hungary
    Posts
    985
    Quote Originally Posted by mhagain View Post
    Also, won't str be out of scope after loadShaderAsString returns? You're probably corrupting the stack here too, so there's something else to check.
    Actually, this will be the problem. Once loadShaderAsString returns the str string object is released and you are returning an invalid pointer. Yes, if you print out immediately the pointer as a string, it will work as nobody else had the chance to overwrite the data at that memory location, but that memory no longer belongs to the string object, it is unallocated memory.
    Disclaimer: This is my personal profile. Whatever I write here is my personal opinion and none of my statements or speculations are anyhow related to my employer and as such should not be treated as accurate or valid and in no case should those be considered to represent the opinions of my employer.
    Technical Blog: http://www.rastergrid.com/blog/

  5. #5
    Junior Member Newbie Apoptose's Avatar
    Join Date
    Jun 2012
    Location
    Germany
    Posts
    6
    Thanks everyone, that solved the Problem.

    Didn't thought he would free the memory.

    After copying the c-String to an new Memory-Location it worked.

    Thanks again,
    Apoptose

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •