Login  Register

Re: need help understanding glVertexAttribPointer and GL_INVALID_OPERATION

Posted by Xerxes Rånby on Jan 08, 2013; 10:41pm
URL: https://forum.jogamp.org/need-help-understanding-glVertexAttribPointer-and-GL-INVALID-OPERATION-tp4027730p4027748.html

Hi i can see two issues with your code:

1.  do not recompile a new shader program each frame. you currently do this by having these two lines:
        int positionAttribute = loadShaderProgram(gl);
        gl.glUseProgram(program.program());
placed inside your display function. The loadShaderProgram function contains:
        program = new ShaderProgram();
so... for each second you upload, compile and link 60 new vertex and fragment shader program that gets stored inside your GPU memory. You will quickly run out of GPU memory by doing this.
The display function gets run about 60 times/second!
     
I suggest that you make the possitionAttribute global
and then move the above two lines from the display function into the
        public void init(GLAutoDrawable drawable) {
function so that they only gets run once at program startup.

The only way to reclaim GPU memory is by explicit detach the program that you have previously have linked inside the GPU memory.
You reclaim ShaderProgram GPU memory by calling:
        program.release(gl);
or
        program.destroy(gl);

http://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/opengl/util/glsl/ShaderProgram.html


2.) swap the order of these three lines that initialize the VertexAttribArray and place gl.glEnableVertexAttribArray at the end:
Change:
                        gl.glEnableVertexAttribArray(positionAttribute);
                        FloatBuffer fbVertices = Buffers.newDirectFloatBuffer(vertices);
                        gl.glVertexAttribPointer(positionAttribute, 3, GL3.GL_FLOAT, false, 0, fbVertices);

to:
                        FloatBuffer fbVertices = Buffers.newDirectFloatBuffer(vertices);
                        gl.glVertexAttribPointer(positionAttribute, 3, GL3.GL_FLOAT, false, 0, fbVertices);
                        gl.glEnableVertexAttribArray(positionAttribute);

Unfortunately I do not own a GL3 enabled device so my suggestions are untested.