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 10 of 14

Thread: Simple OpenGL 4.X does not run on GTX 480 but runs on ATI

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Intern Newbie
    Join Date
    Nov 2013
    Posts
    48

    Simple OpenGL 4.X does not run on GTX 480 but runs on ATI

    A simple program that draws a triangle with 3 vertices in red over a white background using a simple one liner vshader and fshader that simply reads the 3 vertices and sets the color to red respectively runs as expected on a computer with an ATI card but crashes on a computer with GeForce 480X.
    Upon examining the capabilities of the cards using GPU_CAPS I found that the shading language version for the NVIDIA card is suffixed by : via cg compiler while the ATI card does not have this suffix in the shading language version.
    Older opengl programs 2.X not using shaders run fine on both.

    I do not know what need to be done to get the opengl programs using glsl running on GeForce GTX 480? Any clues?

  2. #2
    Junior Member Regular Contributor
    Join Date
    Dec 2010
    Location
    Oakville, ON, CA
    Posts
    165
    Sorry, there are no telepathy-people here. Give us the code of the program in question - that would help.

  3. #3
    Intern Newbie
    Join Date
    Nov 2013
    Posts
    48
    Well I was expecting an opengl program that draws a red triangle on a white background using programmable shaders and does work on both ATI and GeForce cards.
    Anyway here is the code that works on ATI and crashes on GeForce:

    test.cpp

    Code cpp:
    // Two-Dimensional Sierpinski Gasket 
    // Generated using randomly selected vertices and bisection
     
    #include "Angel.h"
     
     
    //----------------------------------------------------------------------------
     
    void
    init( void )
    {
     
    // Specifiy the vertices for a triangle
    vec4 vertices[3] = {
    vec4( -1.0, -1.0, 0.0, 1.0 ), vec4( 0.0, 1.0, 0.0, 1.0 ), vec4( 1.0, -1.0, 0.0, 1.0 )
    };
     
    // Create a vertex array object
    GLuint vao;
    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );
     
    // Create and initialize a buffer object
    GLuint buffer;
    glGenBuffers( 1, &buffer );
    glBindBuffer( GL_ARRAY_BUFFER, buffer );
    glBufferData( GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW );
     
    // Load shaders and use the resulting shader program
    GLuint program = InitShader( "vshader21.glsl", "fshader21.glsl" );
    glUseProgram( program );
     
    // Initialize the vertex position attribute from the vertex shader
    GLuint loc = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( loc );
    glVertexAttribPointer( loc, 4, GL_FLOAT, GL_FALSE, 0,
    BUFFER_OFFSET(0) );
     
    glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
    }
     
    //----------------------------------------------------------------------------
     
    void
    display( void )
    {
    glClear( GL_COLOR_BUFFER_BIT ); // clear the window
    glDrawArrays( GL_TRIANGLES, 0, 3 ); // draw the points
    glFlush();
    }
     
    //----------------------------------------------------------------------------
     
    void
    keyboard( unsigned char key, int x, int y )
    {
    switch ( key ) {
    case 033:
    exit( EXIT_SUCCESS );
    break;
    }
    }
     
    //----------------------------------------------------------------------------
     
    int
    main( int argc, char **argv )
    {
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA );
    glutInitWindowSize( 512, 512 );
     
    // If you are using freeglut, the next two lines will check if 
    // the code is truly 3.2. Otherwise, comment them out
     
    glutInitContextVersion( 3, 2 );
    glutInitContextProfile( GLUT_CORE_PROFILE );
     
    glutCreateWindow( "Sierpinski Gasket" );
     
    glewInit();
     
    init();
     
    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );
     
    glutMainLoop();
    return 0;
    }

    vshader21.glsl:

    Code glsl:
    #version 150
     
    in vec4 vPosition;
     
    void
    main()
    {
    gl_Position = vPosition;
    }

    fshader21.glsl:

    Code glsl:
    #version 150
     
    out vec4 fColor;
     
    void
    main()
    {
    fColor = vec4( 1.0, 0.0, 0.0, 1.0 );
    }

    Finally InitShader.cpp:

    Code cpp:
    #include "Angel.h"
     
    namespace Angel {
     
    // Create a NULL-terminated string by reading the provided file
    static char*
    readShaderSource(const char* shaderFile)
    {
    FILE* fp = fopen(shaderFile, "r");
     
    if ( fp == NULL ) { return NULL; }
     
    fseek(fp, 0L, SEEK_END);
    long size = ftell(fp);
     
    fseek(fp, 0L, SEEK_SET);
    char* buf = new char[size + 1];
    fread(buf, 1, size, fp);
     
    buf[size] = '\0';
    fclose(fp);
     
    return buf;
    }
     
     
    // Create a GLSL program object from vertex and fragment shader files
    GLuint
    InitShader(const char* vShaderFile, const char* fShaderFile)
    {
    struct Shader {
    const char* filename;
    GLenum type;
    GLchar* source;
    } shaders[2] = {
    { vShaderFile, GL_VERTEX_SHADER, NULL },
    { fShaderFile, GL_FRAGMENT_SHADER, NULL }
    };
     
    GLuint program = glCreateProgram();
     
    for ( int i = 0; i < 2; ++i ) {
    Shader& s = shaders[i];
    s.source = readShaderSource( s.filename );
    if ( shaders[i].source == NULL ) {
    std::cerr << "Failed to read " << s.filename << std::endl;
    exit( EXIT_FAILURE );
    }
     
    GLuint shader = glCreateShader( s.type );
    glShaderSource( shader, 1, (const GLchar**) &s.source, NULL );
    glCompileShader( shader );
     
    GLint compiled;
    glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
    if ( !compiled ) {
    std::cerr << s.filename << " failed to compile:" << std::endl;
    GLint logSize;
    glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize );
    char* logMsg = new char[logSize];
    glGetShaderInfoLog( shader, logSize, NULL, logMsg );
    std::cerr << logMsg << std::endl;
    delete [] logMsg;
     
    exit( EXIT_FAILURE );
    }
     
    delete [] s.source;
     
    glAttachShader( program, shader );
    }
     
    /* link and error check */
    glLinkProgram(program);
     
    GLint linked;
    glGetProgramiv( program, GL_LINK_STATUS, &linked );
    if ( !linked ) {
    std::cerr << "Shader program failed to link" << std::endl;
    GLint logSize;
    glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
    char* logMsg = new char[logSize];
    glGetProgramInfoLog( program, logSize, NULL, logMsg );
    std::cerr << logMsg << std::endl;
    delete [] logMsg;
     
    exit( EXIT_FAILURE );
    }
     
    /* use program object */
    glUseProgram(program);
     
    return program;
    }
     
    } // Close namespace Angel block
    Last edited by Dark Photon; 08-26-2014 at 05:11 PM.

  4. #4
    Junior Member Regular Contributor
    Join Date
    Dec 2010
    Location
    Oakville, ON, CA
    Posts
    165
    I do not see the call to the glBindFragDataLocation(program, 0, "fColor")

    When exactly does the program crashes? Did you debugged it step-by-step?

  5. #5
    Member Regular Contributor malexander's Avatar
    Join Date
    Aug 2009
    Location
    Ontario
    Posts
    369
    As there is only one fragment shader output, fColor will be at data location zero.

  6. #6
    Intern Newbie
    Join Date
    Nov 2013
    Posts
    48
    But what about vPosition?

  7. #7
    Intern Newbie
    Join Date
    Nov 2013
    Posts
    48
    No I did not debug it, but I will add it and see if it works, but where do I add it?

  8. #8
    Intern Newbie
    Join Date
    Nov 2013
    Posts
    48
    After doing some search on net I found the following shaders:

    vshader.glsl

    #version 330

    layout (location = 0) in vec4 Position;

    void main()
    {
    gl_Position = Position;
    }

    fshader.glsl:

    #version 330

    out vec4 FragColor;

    void main()
    {
    FragColor = vec4(1.0, 0.0, 0.0, 1.0);
    }

    Would that be enough and sets Position at location 0? or I still need to bind FragColor to location 0?

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
  •