invalid shader version number

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

invalid shader version number

gmseed
Hi

My JOGL program [code pasted at bottom] produces the following console output and is failing due to an invalid shader version number of 330:

Compile failure in vertex shader: Vertex shader failed to compile with the following errors:
ERROR: 0:1: error(#106) Version number not supported by GL2
ERROR: 0:2: error(#279) Invalid layout qualifier 'location'
ERROR: error(#273) 2 compilation errors.  No code generated

The GLProfile is set to GL2 and I'm using jogl-2.0-b41-20110916-windows-amd64.

Has anyone else struggled with this issue?

Thanks

Graham

====


package modern_gl_prog;

// java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Canvas;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.charset.Charset;
// gl
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import com.jogamp.common.nio.Buffers;

/**
 * Java port of JL McKesson's tut1.cpp tutorial; Tutorial 0.3.7\Tut 01 Hello triangle
 * @author gmseed
 */
public class tut1 extends JFrame
{
    private JPanel contentPane;
    private Canvas canvas;
   
    static { GLProfile.initSingleton(true); } // recommended first call
   
    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    tut1 frame = new tut1();
                    frame.setVisible(true);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public tut1()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 545, 536);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
       
        // OpenGL 2.1 profile
        GLProfile prof = GLProfile.get(GLProfile.GL2);
        GLCapabilities caps = new GLCapabilities(prof);
        canvas = new tut1Canvas(caps);
        canvas.setBounds(0, 0, 517, 390);
        contentPane.add(canvas);        
    }
} // class tut1

class tut1Canvas extends GLCanvas
                 implements GLEventListener
{
    protected int mTheProgram;              // handle to program object
    protected int mPositionBufferObject;    // handle to buffer object
    protected int mvao;                     // vertex array object

    protected int           mVertexCount = 3;
    protected FloatBuffer   mVertexPositions = null;
   
    public tut1Canvas()
    {
        super();
        this.addGLEventListener(this);
    }
   
    public tut1Canvas(GLCapabilities caps)
    {
        super(caps);
        this.addGLEventListener(this);
    }
   
    private int createShader(GL2 gl, int eShaderType, String strShaderFile)
    {
        int shader = gl.glCreateShader(eShaderType);
        String[] strings = new String[1]; strings[0] = strShaderFile;
        gl.glShaderSource(shader, 1, strings,(int[])null,0); // glShaderSource(int shader, int count, java.lang.String[] string, int[] length, int length_offset);
   
        gl.glCompileShader(shader);
   
        int[] statusArray = new int[1];
        int status;
        gl.glGetShaderiv(shader, GL2.GL_COMPILE_STATUS, statusArray,0);
        status = statusArray[0];
        if (status == GL.GL_FALSE)
        {
            int[] infoLogLengthArray = new int[1];
            int infoLogLength;
            gl.glGetShaderiv(shader, GL2.GL_INFO_LOG_LENGTH,infoLogLengthArray,0);
            infoLogLength = infoLogLengthArray[0];
            IntBuffer intValue = IntBuffer.allocate(1);
            ByteBuffer strInfoLog = Buffers.newDirectByteBuffer(infoLogLength);
            gl.glGetShaderInfoLog(shader, infoLogLength, intValue, strInfoLog);
   
            String strShaderType = null;
            switch(eShaderType)
            {
                case GL2.GL_VERTEX_SHADER:   strShaderType = "vertex";   break;
                case GL3.GL_GEOMETRY_SHADER: strShaderType = "geometry"; break;
                case GL2.GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
            }
           
            // decode byte buffer
            String      encoding    = System.getProperty("file.encoding");
            CharBuffer  charBuff    = Charset.forName(encoding).decode(strInfoLog);
            String      buffString  = charBuff.toString();
            String      msg         = "Compile failure in " + strShaderType + " shader: " + buffString;  
            System.out.println(msg);
        }
   
        return shader;
    }

    private int createProgram(GL2 gl, int[] shaderList)
    {
        int program = gl.glCreateProgram();
   
        for(int iLoop = 0; iLoop < shaderList.length; iLoop++)
        {
            gl.glAttachShader(program, shaderList[iLoop]);
        }
   
        gl.glLinkProgram(program);
       
        int[] statusArray = new int[1];
        int status;
        gl.glGetProgramiv(program,GL2.GL_LINK_STATUS,statusArray,0);
        status = statusArray[0];
        if (status == GL.GL_FALSE)
        {
            int[] infoLogLengthArray = new int[1];
            int infoLogLength;
            gl.glGetShaderiv(program, GL2.GL_INFO_LOG_LENGTH,infoLogLengthArray,0);
            infoLogLength = infoLogLengthArray[0];

            IntBuffer intValue = IntBuffer.allocate(1);
            ByteBuffer strInfoLog = ByteBuffer.allocate(infoLogLength);
            gl.glGetProgramInfoLog(program, infoLogLength, intValue, strInfoLog);
           
            // decode byte buffer
            String      encoding    = System.getProperty("file.encoding");
            CharBuffer  charBuff    = Charset.forName(encoding).decode(strInfoLog);
            String      buffString  = charBuff.toString();
            String      msg         = "Linker failure: " + buffString;  
            System.out.println(msg);            
        }
   
        for(int iLoop = 0; iLoop < shaderList.length; iLoop++)
        {
            gl.glDetachShader(program, shaderList[iLoop]);
        }
   
        return program;
    }    
   
    public String strVertexShader()
    {
        String s = new String();
        s += "#version 340\n";
        s += "layout(location = 0) in vec4 position;\n";
        s += "void main()\n";
        s += "{\n";
        s += "   gl_Position = position;\n";
        s += "}\n";
        return s;
    }
   
    public String strFragmentShader()
    {
        String s = new String();
        s += "#version 340\n";
        s += "out vec4 outputColor;\n";
        s += "void main()\n";
        s += "{\n";
        s += "   outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n";
        s += "}\n";
        return s;
    };

    void initializeProgram(GL2 gl)
    {
        String  vertexShaderString      = strVertexShader();
        String  fragmentShaderString    = strFragmentShader();
        int     vertexShader            = createShader(gl,GL2.GL_VERTEX_SHADER,vertexShaderString);
        int     fragmentShader          = createShader(gl,GL2.GL_FRAGMENT_SHADER,fragmentShaderString);
        int[] shaderList = new int[2];
        shaderList[0] = vertexShader;
        shaderList[1] = fragmentShader;
       
        mTheProgram = createProgram(gl,shaderList);
    }

    void initializeVertexBuffer(GL2 gl)
    {
        int[] array = new int[1];
        gl.glGenBuffers(1, array, 0);
        mPositionBufferObject = array[0];

        // allocate vertex buffer [4 for (x,y,z,w) for each vertex]
        mVertexPositions = Buffers.newDirectFloatBuffer(4*mVertexCount);
        mVertexPositions.put(0.75f);  mVertexPositions.put(0.75f);  mVertexPositions.put(0.75f); mVertexPositions.put(1.0f);
        mVertexPositions.put(0.75f);  mVertexPositions.put(-0.75f); mVertexPositions.put(0.0f);  mVertexPositions.put(1.0f);
        mVertexPositions.put(-0.75f); mVertexPositions.put(-0.75f); mVertexPositions.put(0.0f);  mVertexPositions.put(1.0f);
        // invoke this method to prepare for a sequence of channel-write or relative get operations
        mVertexPositions.flip();
       
        gl.glBindBuffer(GL2.GL_ARRAY_BUFFER,mPositionBufferObject);
        gl.glBufferData(GL2.GL_ARRAY_BUFFER,4*mVertexCount*Buffers.SIZEOF_FLOAT,mVertexPositions,GL2.GL_STATIC_DRAW);
        gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, 0);
    }
   
    @Override
    public void init(GLAutoDrawable gLDrawable)
    {
        GL2 gl = gLDrawable.getGL().getGL2();
       
        initializeProgram(gl);
        initializeVertexBuffer(gl);

        // generate vertex array and bind
        int[] array = new int[1];
        gl.glGenVertexArrays(1,array,0);
        mvao = array[0];
        gl.glBindVertexArray(mvao);
    }
   
    @Override
    public void display(GLAutoDrawable gLDrawable)
    {
        final GL2 gl = gLDrawable.getGL().getGL2();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);

        gl.glUseProgram(mTheProgram);

        gl.glBindBuffer(GL2.GL_ARRAY_BUFFER,mPositionBufferObject);
        gl.glEnableVertexAttribArray(0); // GL2ES2
        gl.glVertexAttribPointer(0, 4, GL.GL_FLOAT,false, 0, 0);
       
        gl.glDrawArrays(GL.GL_TRIANGLES, 0, 3);
       
        gl.glDisableVertexAttribArray(0);
        gl.glUseProgram(0);

        gLDrawable.swapBuffers();
    }
   
    @Override
    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height)
    {      
        GL2 gl = gLDrawable.getGL().getGL2();
        if (height <= 0)
        {
            height = 1;
        }
        gl.glViewport(0,0,width,height);
    }
   
    @Override
    public void dispose(GLAutoDrawable arg0)
    {
        // do nothing
    }
} // class tut1Canvas

Reply | Threaded
Open this post in threaded view
|

Re: invalid shader version number

Demoscene Passivist
Administrator
As far as I know GLSL 3.30 is only supported in GL3+ profiles. To be on the safe side simply use the GL4bc profile (hope ur hardware supports it).

Also maybe removing the explicit version directive might help, as GLSL and the GL profiles are not that hard linked in most implementations.
Reply | Threaded
Open this post in threaded view
|

Re: invalid shader version number

gmseed
Hi

Thanks for your reply.

The weird thing is that the program runs fine on my desktop PC with WinXP-64 and doesn't throw any compile/link exceptions.

However, when I run the ame program on my Win7-64 it throws both compile and link exceptions.

I thought it might be that I was running jre6-u26 on my desktop and jre7 on my laptop but I've now rerun the program with jre6-u26 on my laptop and I get the same compile/link exceptions.

Graham
Reply | Threaded
Open this post in threaded view
|

Re: invalid shader version number

gmseed
This post was updated on .
Hi again

Just checked the OPenGL API version from the current JOGL download at jogamp, and it is 2.0.

I've always been slightly confused as to the Readme file indicates OpenGL 2.0, when it includes GL3, GL4, etc.

Presumably if the JOGL build is for OpenGL 2.0 then it is pointless going above GL2 - or I am missing something.

Checking the table on Wikipedia:

http://en.wikipedia.org/wiki/GLSL

we see that OpenGl2.1 supports GLSL 1.20.8, for which "lacation" is not supported.

I am totally baffled by this as GL2 with shader version 330 runs on my desktop WinXP-64 m/c but on my Win7-64 laptop doesn't work and seem to be getting bogged in shader versions/GL versions, when the issue is something else.

Graham
Reply | Threaded
Open this post in threaded view
|

Re: invalid shader version number

Demoscene Passivist
Administrator
>Just checked the OPenGL API version from the current JOGL download at jogamp, and it is 2.0.

OpenGL 2.0 ? U mean JOGL 2.0-rc3 ? JOGL supports all OpenGL versions, up to 4.+. The 2.0 is JOGL's own version number, or what are u referring to ?

>I thought it might be that I was running jre6-u26 on my desktop and jre7 on
>my laptop but I've now rerun the program with jre6-u26 on my laptop and
>I get the same compile/link exceptions.

Also shader compilation and/or OpenGL support has nothing to do with the JRE ur are running. Its completly unrelated. If u can get JOGL2 running on ur VM, the full OpenGL funtionality supported by ur driver/card is available to u.

As u are having problems with different hardware configurations and not in general with ur code make sure u have the newest drivers are installed on both systems. I know this sounds stupid, but it generally resolves most issues with least effort.

When u got ur drivers up2date check what profile u get back from JOGL with a simple "println" of ur "gl" instance. It will give u a hint what profile u actually are using regardless of the casting u have done.
Reply | Threaded
Open this post in threaded view
|

Re: invalid shader version number

gmseed
Hi

I downloaded jogl-2.0-b41-20110915-windows-amd64 and in the extracted \libs directory there is a Readme file. At the top of the file it states:

"

This is build version 2.0-b41-20110916, based on:
              branch  rc
              commit  64feda2fa7611627e31f55ecc7cf86e290fdf4e3

Instructions for unzipping Java Binding for the OpenGL API, version 2.0
"

and thus assumed that the current version of JOGL is for OpenGL v2.

Thanks

Graham
Reply | Threaded
Open this post in threaded view
|

Re: invalid shader version number

Sven Gothel
Administrator
On Monday, November 21, 2011 09:36:12 AM gmseed [via jogamp] wrote:

>
> Hi
>
> I downloaded jogl-2.0-b41-20110915-windows-amd64 and in the extracted \libs
> directory there is a Readme file. At the top of the file it states:
>
> "
>
> This is build version 2.0-b41-20110916, based on:
>               branch  rc
>               commit  64feda2fa7611627e31f55ecc7cf86e290fdf4e3
>
> Instructions for unzipping Java Binding for the OpenGL API, version 2.0
> "
>
> and thus assumed that the current version of JOGL is for OpenGL v2.
>

http://jogamp.org/jogl/doc/Overview-OpenGL-Evolution-And-JOGL.html

~Sven

> Thanks
>
> Graham
Reply | Threaded
Open this post in threaded view
|

Re: invalid shader version number

gmseed
In reply to this post by gmseed
Hi

Thanks for the link.

The previously posted program works fine on my desktop running WinXP-64 with an NVIDIA Quadro FX1400, which supports OpenGL2 but accepts the 330 shader version.

The program throws shader compile exceptions on my Win7-64 laptop with an ATI Mobility Radeon HD 5470 [version 8.672.1.3000], which from the following spec website supports OpenGL 3.2:

http://www.amd.com/uk/products/notebook/graphics/ati-mobility-hd-5400/Pages/hd-5450-specs.aspx

On both machines I'm using the same jars/dlls from the most recent JOGL download.

Graham
Reply | Threaded
Open this post in threaded view
|

Re: invalid shader version number

Sven Gothel
Administrator
On Monday, November 21, 2011 09:54:52 AM gmseed [via jogamp] wrote:

>
> Hi
>
> Thanks for the link.
>
> The previously posted program works fine on my desktop running WinXP-64 with
> an NVIDIA Quadro FX1400, which supports OpenGL2 but accepts the 330 shader
> version.
>
> The program throws shader compile exceptions on my Win7-64 laptop with an
> ATI Mobility Radeon HD 5470 [version 8.672.1.3000], which from the following
> spec website supports OpenGL 3.2:
>
> http://www.amd.com/uk/products/notebook/graphics/ati-mobility-hd-5400/Pages/hd-5450-specs.aspx
>
> On both machines I'm using the same jars/dlls from the most recent JOGL
> download.

JOGL doesn't parse or process the actual GLSL programs,
but passes them directly to the driver.

Indeed, GLSL code may behave differently (compile-time/run-time)
on diff platforms. It may be the supported GLSL version which differs
or other features like variable loops .. etc.

One driver may accept GLSL 330 w/ a GL 2 context, others don't.
Reason why they accept it could be that the underlying context you have
created actually is a GL 3 backward compatible one .. even if created w/
the traditional non ARB context creation code. This is usally true w/ NVidia.

However .. you cannot count on it.

~Sven

>
> Graham
>
Reply | Threaded
Open this post in threaded view
|

Re: invalid shader version number

gmseed
Hi

I'll admit that I don't understand fully your comments but based on what you've said how does anyone go out and buy a desktop/laptop to develop opengl using vertex shaders?

I have a desktop running a 7 year old WinXP/Nvidia Quadro/OpenGL 2.1/GLSL version 1.20 that runs the program fine and a laptop purchased a few months ago wunning Win7/ATI Radeon/OpenGL3/GLSL version 3.3 that doesn't run the program.

How could anyone predict or expect this? And how can anyone develop code for such scenarios?

Thanks

Graham
Reply | Threaded
Open this post in threaded view
|

Re: invalid shader version number

Sven Gothel
Administrator
On Monday, November 21, 2011 03:12:29 PM gmseed [via jogamp] wrote:

>
> Hi
>
> I'll admit that I don't understand fully your comments but based on what
> you've said how does anyone go out and buy a desktop/laptop to develop
> opengl using vertex shaders?
>
> I have a desktop running a 7 year old WinXP/Nvidia Quadro/OpenGL 2.1/GLSL
> version 1.20 that runs the program fine and a laptop purchased a few months
> ago wunning Win7/ATI Radeon/OpenGL3/GLSL version 3.3 that doesn't run the
> program.
>
> How could anyone predict or expect this? And how can anyone develop code for
> such scenarios?

As I said, you just cannot assume that something maybe supported,
if the spec doesn't guarantee it.

Check the GLSL spec .. you can also use preprocessor commands
for diff GLSL versions .. and you may use 'require' statements etc.

There is a guaranteed GLSL language version for each GL profile,
check the wikipedia pages. If you get more .. well, nice, but not guaranteed.

> it could be that the underlying context you have
> created actually is a GL 3 backward compatible one .. even if created w/
> the traditional non ARB context creation code. This is usally true w/
> NVidia.

This means, that you might have retrieved a GL3 backward compatible context,
even though you have asked for GL2 only. The GL spec allows it.
The driver may allow you to use the highest supported version.

You may query the avail GLSL version:
  - http://www.opengl.org/sdk/docs/man/xhtml/glGetString.xml
  - GL_SHADING_LANGUAGE_VERSION

It's also best to match the GLSL version w/ the intended GL context version,
maybe even reduce it if allowed.

After you made a few recursions w/ diff drivers and check the spec,
you will see that it is possible with some disciplin.
Check our GLSL examples .. they pass all unit tests currently.

~Sven
Reply | Threaded
Open this post in threaded view
|

Re: invalid shader version number

gmseed
Hi

Thanks again for further explanation.

Graham