Possible to use vertex shader without vbo?

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

Possible to use vertex shader without vbo?

sirus20x6
Is it possible to use a vertex shader without your vertex data being in a VBO? (like with glbegin / glend) I have an erosion simulator that I'm working on and currently the map color is calculated on the cpu based on the z height. I would like to do this calculation on the gpu but all the tutorials I've seen use VBO and I dont think that's ideal for what I'm doing (updating the terrain geometry faster than 60fps).
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

Xerxes Rånby
sirus20x6 wrote
Is it possible to use a vertex shader without your vertex data being in a VBO?
Yes GL2, GLES2 and backward compatible GL3bc & GL4bc has a
gl.glVertexAttribPointer
that takes a Buffer instead of using a vbo offset.
https://jogamp.org/deployment/v2.3.1/javadoc/jogl/javadoc/com/jogamp/opengl/GL2.html#glVertexAttribPointer%28int,%20int,%20int,%20boolean,%20int,%20java.nio.Buffer%29

If you want to see the difference in use you may look at this commit that changes one of the jogl-demos from using glVertexAttribPointer that takes a Buffer into glVertexAttribPointer that uses the last bound VBO.
https://jogamp.org/git/?p=jogl-demos.git;a=commitdiff;h=84a886cf4957e8f55af39abf8e9bad04a1de2e4f

Newer GL3 & GL4 drivers have dropped backward compatible support thus if you want your code to run using the latest OpenGL versions and drivers you will have to use vbo.
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

Xerxes Rånby
In reply to this post by sirus20x6
sirus20x6 wrote
I've seen use VBO and I dont think that's ideal for what I'm doing (updating the terrain geometry faster than 60fps).
VBO's are ideal because it puts you in control how the GPU should handle memory.
The deprecated way of passing a Buffer puts the burden to handle memory on the GPU driver, the driver then has to guess and will cause unpredictable performance.

The GPU driver do not know anything about your application thus it can never figure out if it can reuse parts of the buffer. When you manage memory you can choose to update a part of the VBO.
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

Xerxes Rånby
In reply to this post by sirus20x6
sirus20x6 wrote
I have an erosion simulator that I'm working on and currently the map color is calculated on the cpu based on the z height. I would like to do this calculation on the gpu
I think you should do the color calculation in the fragment shader,
updating the color based on z depth is ideal if you want to do say a "fog" effect.
http://stackoverflow.com/a/17621928

updating the color based on y height is as easy.
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

gouessej
Administrator
In reply to this post by sirus20x6
Actually, you're wrong. You can use ImmModeSink to ease the port but VBOs are ideal for any case, they are intended to replace the whole immediate mode. You can use the dynamic or the stream draw mode.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

sirus20x6
I tried to make the conversion based on OneTriangle.java but my app just freezes on launch with no error thrown.


[code]

    private void drawTerrain()
    {
        float [] v1 = new float[3];
        float [] v2 = new float[3];
        float [] norm = new float[3];


        v1[0] = 30.0f / 5.0f;  // Scale factor is a hack
        v1[1] = 0.0f;

        v2[0] = 0.0f;
        v2[1] = 30.0f / 5.0f;  // Ditto on hack


        //gl.glBegin(GL.GL_TRIANGLE_STRIP);
        immModeSink.glBegin(GL.GL_TRIANGLE_STRIP);


        for(int x = 1; x < latticeSizeX; x++) {

            for(int y = 1; y < latticeSizeY; y++)
            {

                v1[2] = Wilsim.m.topo1d[x+1 + y * latticeSizeX] - Wilsim.m.topo1d[x-1 + y * latticeSizeX];
                v2[2] = Wilsim.m.topo1d[x + (y+1) * latticeSizeX] - Wilsim.m.topo1d[x + (y-1) * latticeSizeX];

                cross(norm, v2, v1);

                //gl.glNormal3fv(norm, 0);
                immModeSink.glNormal3f(norm[0], norm[1], norm[2]);
               
                //gl.glColor3f(Wilsim.m.vert_color2[x + y * Wilsim.m.lattice_size_x].x, Wilsim.m.vert_color2[(x + y * Wilsim.m.lattice_size_x)].y, Wilsim.m.vert_color2[(x + y * Wilsim.m.lattice_size_x)].z);
                immModeSink.glColor3f(Wilsim.m.vert_color2[x + y * Wilsim.m.lattice_size_x].x, Wilsim.m.vert_color2[(x + y * Wilsim.m.lattice_size_x)].y, Wilsim.m.vert_color2[(x + y * Wilsim.m.lattice_size_x)].z);

                //gl.glVertex3f(x, y, Wilsim.m.topo1d[x + y * latticeSizeX]);
                immModeSink.glVertex3f(x, y, Wilsim.m.topo1d[x + y * latticeSizeX]);


                //gl.glColor3f(Wilsim.m.vert_color2[x + y * Wilsim.m.lattice_size_x].x, Wilsim.m.vert_color2[(x + y * Wilsim.m.lattice_size_x)].y, Wilsim.m.vert_color2[(x + y * Wilsim.m.lattice_size_x)].z);
                immModeSink.glColor3f(Wilsim.m.vert_color2[x + y * Wilsim.m.lattice_size_x].x, Wilsim.m.vert_color2[(x + y * Wilsim.m.lattice_size_x)].y, Wilsim.m.vert_color2[(x + y * Wilsim.m.lattice_size_x)].z);

                //gl.glVertex3f(x + 1, y, Wilsim.m.topo1d[(x + 1) + y * latticeSizeX]);
                immModeSink.glVertex3f(x + 1, y, Wilsim.m.topo1d[(x + 1) + y * latticeSizeX]);

            }

            //degenerate triangles

            //eff
            immModeSink.glVertex3f(x, latticeSizeY, Wilsim.m.topo1d[x + latticeSizeY * latticeSizeX]);
            immModeSink.glVertex3f(x + 1, latticeSizeY, Wilsim.m.topo1d[(x + 1) + latticeSizeY * latticeSizeX]);
            immModeSink.glVertex3f(x + 1, latticeSizeY, Wilsim.m.topo1d[(x + 1) + latticeSizeY * latticeSizeX]);

            //FFG
            immModeSink.glVertex3f(x + 1, latticeSizeY, Wilsim.m.topo1d[(x + 1) + latticeSizeY * latticeSizeX]);
            immModeSink.glVertex3f(x + 1, latticeSizeY, Wilsim.m.topo1d[(x + 1) + latticeSizeY * latticeSizeX]);
            immModeSink.glVertex3f(x, 1, Wilsim.m.topo1d[x + 1 * latticeSizeX]);

            //FGG
            immModeSink.glVertex3f(x + 1, latticeSizeY, Wilsim.m.topo1d[(x + 1) + latticeSizeY * latticeSizeX]);
            immModeSink.glVertex3f(x, 1, Wilsim.m.topo1d[x + 1 * latticeSizeX]);
            immModeSink.glVertex3f(x, 1, Wilsim.m.topo1d[x + 1 * latticeSizeX]);

            //GGH
            immModeSink.glVertex3f(x, 1, Wilsim.m.topo1d[x + 1 * latticeSizeX]);
            immModeSink.glVertex3f(x, 1, Wilsim.m.topo1d[x + 1 * latticeSizeX]);
            immModeSink.glVertex3f(x + 1, 1, Wilsim.m.topo1d[(x + 1) + 1 * latticeSizeX]);

            //GHG
            immModeSink.glVertex3f(x, 1, Wilsim.m.topo1d[x + 1 * latticeSizeX]);
            immModeSink.glVertex3f(x + 1, 1, Wilsim.m.topo1d[(x + 1) + 1 * latticeSizeX]);
            immModeSink.glVertex3f(x, 1, Wilsim.m.topo1d[x + 1 * latticeSizeX]);

            //GGH
            immModeSink.glVertex3f(x, 1, Wilsim.m.topo1d[x + 1 * latticeSizeX]);
            immModeSink.glVertex3f(x, 1, Wilsim.m.topo1d[x + 1 * latticeSizeX]);
            immModeSink.glVertex3f(x + 1, 1, Wilsim.m.topo1d[(x + 1) + 1 * latticeSizeX]);


        }

        //gl.glEnd();
        immModeSink.glEnd(gl, true);

    }
[/code]
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

sirus20x6
any pointers here would be appreciated
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

gouessej
Administrator
Where do you call ImmModeSink.draw(GL, boolean)?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

sirus20x6
I don't. Where should I call it? I can't seem to find a complete example
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

gouessej
Administrator
You can call it in the display() method of your GLEventListener or directly in drawTerrain() if it is called directly or indirectly in display() which seems to be the case (difficult to think about that based on a tiny snippet). If you don't call it, it won't draw anything.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

sirus20x6
I tried calling it from a couple places but I still get nothing on the canvas and it still freezes
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

gouessej
Administrator
Where does it freeze? Please use a debugger. It's difficult to know what is wrong by seeing only a tiny part of your code.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

sirus20x6
It throws no exception so I placed a breakpoint at immodesink.draw(gl,true); It gets called twice before the freeze. so on the second time I stepped over the code until it wouldnt go any farther. the last line that gets called is in GLCanvas.class

    public void update(Graphics var1) {
        this.paint(var1);
    }


after that the app hangs for 2-3 minutes then it does eventually draw to the screen and then runs at the correct speed. I hadn't let it run for this long before so I thought it just crashed.

When I run it with the debugger the output isnt exactly correct though. right is immodesink




I wonder what's causing that 2-3 minutes of delay.
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

gouessej
Administrator
Please provide a full example to allow us to reproduce this bug if any. Maybe there is something obvious that we miss. I advise you to create a more simple example with just a single triangle. In my humble opinion, there is something wrong in the creation of the VBO in ImmModeSink, maybe the passed parameters are wrong.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

gouessej
Administrator
In reply to this post by sirus20x6
Where and how do you call ImmModeSink.createFixed()?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

gouessej
Administrator
In reply to this post by sirus20x6
Ensure that you give a color and a normal to each vertex (even though it leads to duplicate some code) and it will probably work.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

sirus20x6
If I limit how many vertices I draw it works (after a larger than expected delay of several seconds)

note x here is only 50 and I really have 678 elements

<code>
    private void drawTerrain()
    {
        float [] v1 = new float[3];
        float [] v2 = new float[3];
        float [] norm = new float[3];


        v1[0] = 30.0f / 5.0f;  // Scale factor is a hack
        v1[1] = 0.0f;

        v2[0] = 0.0f;
        v2[1] = 30.0f / 5.0f;  // Ditto on hack
       
        gl.glEnable(GLLightingFunc.GL_COLOR_MATERIAL);
        gl.glColorMaterial(GL.GL_FRONT_AND_BACK,
                GLLightingFunc.GL_AMBIENT_AND_DIFFUSE);

        immModeSink.glBegin(GL.GL_TRIANGLE_STRIP);



        for(int x = 1; x < 50; x++) {

            for(int y = 1; y < latticeSizeY; y++)
            {
                v1[2] = Model.topo1dim[x+1 + y * latticeSizeX] - Model.topo1dim[x-1 + y * latticeSizeX];
                v2[2] = Model.topo1dim[x + (y+1) * latticeSizeX] - Model.topo1dim[x + (y-1) * latticeSizeX];
                cross(norm, v2, v1);
                immModeSink.glNormal3f(norm[0],norm[1],norm[2]);
                immModeSink.glColor3f(Model.vert_color2[x + y * Model.lattice_size_x].x, Model.vert_color2[(x + y * Model.lattice_size_x)].y, Model.vert_color2[(x + y * Model.lattice_size_x)].z);
                immModeSink.glVertex3f(x, y, Model.topo1dim[x + y * latticeSizeX]);

                x++;

                v1[2] = Model.topo1dim[x+1 + y * latticeSizeX] - Model.topo1dim[x-1 + y * latticeSizeX];
                v2[2] = Model.topo1dim[x + (y+1) * latticeSizeX] - Model.topo1dim[x + (y-1) * latticeSizeX];
                cross(norm, v2, v1);
                immModeSink.glNormal3f(norm[0],norm[1],norm[2]);
                immModeSink.glColor3f(Model.vert_color2[x + y * Model.lattice_size_x].x, Model.vert_color2[(x + y * Model.lattice_size_x)].y, Model.vert_color2[(x + y * Model.lattice_size_x)].z);
                immModeSink.glVertex3f(x, y, Model.topo1dim[x + y * latticeSizeX]);
                x--;
            }

            //degenerate triangles

            //eff
            immModeSink.glNormal3f(0f,0f,0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x, latticeSizeY, Model.topo1dim[x + latticeSizeY * latticeSizeX]);
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x + 1, latticeSizeY, Model.topo1dim[(x + 1) + latticeSizeY * latticeSizeX]);
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x + 1, latticeSizeY, Model.topo1dim[(x + 1) + latticeSizeY * latticeSizeX]);

            //FFG
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x + 1, latticeSizeY, Model.topo1dim[(x + 1) + latticeSizeY * latticeSizeX]);
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x + 1, latticeSizeY, Model.topo1dim[(x + 1) + latticeSizeY * latticeSizeX]);
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x, 1, Model.topo1dim[x + latticeSizeX]);

            //FGG
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x + 1, latticeSizeY, Model.topo1dim[(x + 1) + latticeSizeY * latticeSizeX]);
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x, 1, Model.topo1dim[x + latticeSizeX]);
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x, 1, Model.topo1dim[x + latticeSizeX]);

            //GGH
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x, 1, Model.topo1dim[x + latticeSizeX]);
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x, 1, Model.topo1dim[x + latticeSizeX]);
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x + 1, 1, Model.topo1dim[(x + 1) + latticeSizeX]);

            //GHG
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x, 1, Model.topo1dim[x + latticeSizeX]);
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x + 1, 1, Model.topo1dim[(x + 1) + latticeSizeX]);
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x, 1, Model.topo1dim[x + latticeSizeX]);

            //GGH
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x, 1, Model.topo1dim[x + latticeSizeX]);
            immModeSink.glNormal3f(0f, 0f, 0f);
            immModeSink.glColor3f(0f, 0f, 0f);
            immModeSink.glVertex3f(x, 1, Model.topo1dim[x + latticeSizeX]);
            immModeSink.glNormal3f(0f,0f,0f);
            immModeSink.glColor3f(0f,0f,0f);
            immModeSink.glVertex3f(x + 1, 1, Model.topo1dim[(x + 1) + latticeSizeX]);
        }

        immModeSink.glEnd(gl);


    }
</code>





Thats a small strip of the terrain now working correctly.
If I increase x to say 150 or over then it never loads and I have to issue a kill signal to terminate it.


full code here https://github.com/sirus20x6/Wilsim-GC/blob/HR/src/View.java
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

sirus20x6
finally figured it out

Changed


<code>
    private final ImmModeSink immModeSink = ImmModeSink.createFixed(3*3,
            3, GL.GL_FLOAT, // vertex
            3, GL.GL_FLOAT, // color
            3, GL.GL_FLOAT, // normal
            0, GL.GL_FLOAT, // texCoords
            GL.GL_STATIC_DRAW);

</code>

to

<code>
    private final ImmModeSink immModeSink = ImmModeSink.createFixed(Wilsim.m.oneDimSize,
            3, GL.GL_FLOAT, // vertex
            3, GL.GL_FLOAT, // color
            3, GL.GL_FLOAT, // normal
            0, GL.GL_FLOAT, // texCoords
            GL.GL_STATIC_DRAW);

</code>
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

gouessej
Administrator
Great! Please can you post a screen capture? I'd like to see the final result :)
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Possible to use vertex shader without vbo?

sirus20x6
Sure thing!

I just got my hands on higher resolution topography so i really need to improve performance. The old version had 1/4th the vertices of this one.







the old version used to take 2 minutes and 38 seconds to run on my desktop. I've spent the last year optimizing it and now it runs at 17-19 seconds for a run, but now with this new high res topography data it takes 2 minutes and 5 seconds so I'm looking for other ways to optimize it. there are 678 x 524 cells
12