Re: Speeding the display
Posted by Dex55 on
URL: https://forum.jogamp.org/Speeding-the-display-tp4032405p4032474.html
Hi ,
I tried to modify the code and use glBufferSubData but when i run nothing is drawed . In my code the cells are stored into a list of buffers ( created in Class cache) every buffer contains 4 principla attributs (vertices , colors , outline colors , indices ) each buffer contains stores many cells . the original code of the method UpdateBuffers is sending every time the contents of each buffer from the list to the gpu with GlBufferData ( used 4 times ; one time for each attribut of the buffer ) . I thought i can create from the begining one big space for every attribute ( vertices , colors , outline colors , indices ) with GLBuffer data and then use GLBufferSubData to send just the new cells to the GPU . Here is my code for the method : updateBuffers
----------
private void updateBuffers(GL2 gl) {
synchronized (cache) {
minPoint = (cache.getMinPoint() == null)? new Vector3d() : new Vector3d(cache.getMinPoint());
maxPoint = (cache.getMaxPoint() == null)? new Vector3d() : new Vector3d(cache.getMaxPoint());
int[] gpuBuffers;
List<CellQuadBuffer> oldList = listBuffers;
listBuffers = new ArrayList<>(cache.getCellBuffers());
Buffer vertices, colors, outlinesColors, indices;
if (vboEnabled) {
for (CellQuadBuffer cb : oldList) {
// delete existing buffers
gpuBuffers = new int[] { cb.gpuBufferVertices, cb.gpuBufferColors, cb.gpuBufferIndices,
cb.gpuBufferOutlinesColors };
gl.glDeleteBuffers(CellQuadBuffer.NUMBER_OF_BUFFERS, IntBuffer.wrap(gpuBuffers));
}
oldList.clear();
for (CellQuadBuffer cb : listBuffers) {
if (i==0)
{
// generate new buffers
gpuBuffers = new int[] { 0,1, 2, 3 };
gl.glDeleteBuffers(CellQuadBuffer.NUMBER_OF_BUFFERS, IntBuffer.wrap(gpuBuffers));
gl.glGenBuffers(CellQuadBuffer.NUMBER_OF_BUFFERS, IntBuffer.wrap(gpuBuffers));
cb.gpuBufferVertices = gpuBuffers[0];
cb.gpuBufferColors = gpuBuffers[1];
cb.gpuBufferIndices = gpuBuffers[2];
cb.gpuBufferOutlinesColors = gpuBuffers[3];
}
vertices = cb.getVertices();
colors = cb.getColors();
outlinesColors = cb.getOutlinesColors();
indices = cb.getIndices();
// vertex buffer
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
if (i==0)
{
gl.glBufferData(GL.GL_ARRAY_BUFFER, vertices.capacity() * Buffers.sizeOfBufferElem(vertices)*10, vertices,
GL.GL_STATIC_DRAW);
offsetPosition1 = vertices.capacity() * Buffers.sizeOfBufferElem(vertices);
}
else
{
gl.glBufferSubData(GL.GL_ARRAY_BUFFER,offsetPosition1, vertices.capacity() * Buffers.sizeOfBufferElem(vertices), vertices);
offsetPosition1 = offsetPosition1 + vertices.capacity() * Buffers.sizeOfBufferElem(vertices);
}
// color buffer
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 1);
if (i == 0)
{
gl.glBufferData(GL.GL_ARRAY_BUFFER, colors.capacity() * Buffers.sizeOfBufferElem(colors)*10, colors,
GL.GL_STATIC_DRAW);
offsetPosition2 = colors.capacity() * Buffers.sizeOfBufferElem(colors);
}
else
{
gl.glBufferSubData(GL.GL_ARRAY_BUFFER,offsetPosition2, colors.capacity() * Buffers.sizeOfBufferElem(colors), colors);
offsetPosition2 = offsetPosition2 + colors.capacity() * Buffers.sizeOfBufferElem(colors);
}
// outlines color buffer
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 2);
if (i == 0)
{
gl.glBufferData(GL.GL_ARRAY_BUFFER, outlinesColors.capacity() * Buffers.sizeOfBufferElem(outlinesColors)*10,
outlinesColors, GL.GL_STATIC_DRAW);
offsetPosition3 = outlinesColors.capacity() * Buffers.sizeOfBufferElem(outlinesColors);
}
else
{
gl.glBufferSubData(GL.GL_ARRAY_BUFFER,offsetPosition3, outlinesColors.capacity() * Buffers.sizeOfBufferElem(outlinesColors),
outlinesColors);
offsetPosition3 = offsetPosition3 + outlinesColors.capacity() * Buffers.sizeOfBufferElem(outlinesColors);
}
// indices buffer
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 3);
if (i== 0)
{
gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indices.capacity() * Buffers.sizeOfBufferElem(indices)*10, indices,
GL.GL_STATIC_DRAW);
offsetPosition4 = indices.capacity() * Buffers.sizeOfBufferElem(indices);
i=1;
}
else
{
gl.glBufferSubData(GL.GL_ELEMENT_ARRAY_BUFFER,offsetPosition4, indices.capacity() * Buffers.sizeOfBufferElem(indices), indices);
System.out.println("ici");
offsetPosition4=offsetPosition4 + indices.capacity() * Buffers.sizeOfBufferElem(indices);
}
cb.resetBuffer();
}
}
cache.resetData();
cache.dataUsed();
}
}
==============
And this is the code responsible for the drawing :
public void display(GLAutoDrawable gLDrawable) {
long start = System.nanoTime();
int nbIndicesTotal=0;
GL2 gl = gLDrawable.getGL().getGL2();
if (cache.isDataChanged()) {
updateBuffers(gl);
}
// erase display and clear buffers
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glMultMatrixd(camera.getViewCopy(), 0);
// Transparence
if (transparencyEnabled) {
gl.glDisable(GL.GL_DEPTH_TEST);
gl.glEnable(GL.GL_BLEND);
} else {
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDisable(GL.GL_BLEND);
}
if (drawDebug) {
drawReferenceAxes(gl);
drawCubeEdges(gl, minPoint, maxPoint);
}
// enable and specify pointer to vertex and color array
gl.glEnableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
gl.glEnableClientState(GLPointerFunc.GL_COLOR_ARRAY);
//Buffer vertices, colors, outlinesColors, indices;
for (CellQuadBuffer cb : listBuffers)
{
nbIndicesTotal = nbIndicesTotal + cb.getNbIndices() ;
}
if (vboEnabled) {
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
gl.glVertexPointer(CellQuadBuffer.NB_COORDINATES_PER_VERTEX, GL.GL_FLOAT, 0, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 1);
gl.glColorPointer(CellQuadBuffer.NB_COLORS_PER_VERTEX, GL.GL_UNSIGNED_BYTE, 0, 0);
// Link the buffer
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 3);
gl.glDrawElements(GL2.GL_QUADS, nbIndicesTotal, GL.GL_UNSIGNED_INT, 0);
if (outlineVisible) {
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 2);
gl.glColorPointer(CellQuadBuffer.NB_COLORS_PER_VERTEX, GL.GL_UNSIGNED_BYTE, 0, 0);
gl.glDrawElements(GL2.GL_LINES, nbIndicesTotal, GL.GL_UNSIGNED_INT, 0);
}
} /*else {
if (cb.getVertices().capacity() > 0) {
// Vertex arrays
gl.glVertexPointer(CellQuadBuffer.NB_COORDINATES_PER_VERTEX, GL.GL_FLOAT, 0, cb.getVertices());
gl.glColorPointer(CellQuadBuffer.NB_COLORS_PER_VERTEX, GL.GL_UNSIGNED_BYTE, 0, cb.getColors());
gl.glDrawElements(GL2.GL_QUADS, cb.getIndices().capacity(), GL.GL_UNSIGNED_INT, cb.getIndices());
if (outlineVisible) {
gl.glColorPointer(CellQuadBuffer.NB_COLORS_PER_VERTEX, GL.GL_UNSIGNED_BYTE, 0, cb.getOutlinesColors());
gl.glDrawElements(GL2.GL_LINES, cb.getIndices().capacity(), GL.GL_UNSIGNED_INT, cb.getIndices());
}
}
}*/
// disable vertex and color arrays after drawing
gl.glDisableClientState(GLPointerFunc.GL_VERTEX_ARRAY);
gl.glDisableClientState(GLPointerFunc.GL_COLOR_ARRAY);
gl.glFlush();
fireDisplayUpdated();
if (debug) {
Logger.getGlobal().finest("total time for display(): " + (System.nanoTime() - start) / 1000 + "µs");
}
}
======================
i hope you can help me , i'm really stuck here . Thank you for your replies and time .