Permalink
Browse files

GL3 memory leak fixed

  • Loading branch information...
VVS1864 committed Dec 16, 2017
1 parent 870b368 commit f7e806e916f7e25cc63c04df21a113c574cd8af3
@@ -48,10 +48,7 @@ public Core() {
//gui.jframe.setVisible(true);
program_state.set_default();
if (glRender.gl_init == false) {
System.out.println("OpenGL version is old, rendering fail, exit");
return;
}
@@ -1,12 +1,9 @@
package core;
import it.unimi.dsi.fastutil.floats.FloatArrayList;
import open_dxf_lib.Color_rgb;
import open_dxf_lib.dash_type;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import samoJ.Circle;
import samoJ.GroupShape;
@@ -18,23 +15,19 @@
import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.util.GLArrayDataServer;
import com.jogamp.opengl.util.GLBuffers;
import com.jogamp.opengl.util.PMVMatrix;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GL2ES2;
import com.jogamp.opengl.GL3;
import com.jogamp.opengl.GLException;
import com.jogamp.opengl.GLUniformData;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.glsl.ShaderCode;
import com.jogamp.opengl.util.glsl.ShaderProgram;
import com.jogamp.opengl.util.glsl.ShaderState;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GL_base {
@@ -86,7 +79,7 @@
public void init_GL() {
GL_version = core.gui.glcanvas.getContext().getGLSLVersionNumber().getMajor();
GL_version = 2;
GL_version = 1;
System.out.println("OpenGL version " + GL_version);
if (GL_version == 3) {
gl3 = core.gui.glcanvas.getGL().getGL3();
@@ -100,18 +93,22 @@ public void init_GL() {
create_GL2_data();
} else if (GL_version == 1) {
return;
System.out.println("OpenGL version is old, rendering fail");
// Rendering fail
return;
}
gl_init = true;
}
private void create_GL2_data() {
// create data structure for 4 line width
GL2_dataArray = new GL2_data[4];
//GL2_dataArray = new GL2_data[4];
for (int i = 0; i < 4; i++) {
if(null != GL2_dataArray[i]) {
gl2.glDeleteBuffers(2, GL2_dataArray[i].vbo_buffer, 0);
}
GL2_dataArray[i] = new GL2_data(i + 1);
}
}
@@ -170,7 +167,7 @@ public void init(GL3 gl2, int width, int height) {
y1 = randomGenerator.nextInt(1200);
x2 = randomGenerator.nextInt(1200);
y2 = randomGenerator.nextInt(1200);
int w = randomGenerator.nextInt(3)+1;
int w = 2;//randomGenerator.nextInt(3)+1;
/*
x1 = 0;
y1 = 0;
@@ -223,7 +220,9 @@ public void init(GL3 gl2, int width, int height) {
t_total += t2-t1;
t1 = System.currentTimeMillis();
for (int i = 0; i<20; i++) {
update_data();
}
t2 = System.currentTimeMillis();
System.out.println("upate_data(): " +(t2 - t1)+ " ms.");
t_total += t2-t1;
@@ -242,15 +241,15 @@ public void init(GL3 gl2, int width, int height) {
*/
public void update_data(){
if(gl_init == false) {
System.out.println("GL init...");
init_GL();
System.out.println("GL init OK");
if(gl_init == true) System.out.println("GL init OK");
}
if(GL_version == 3) {
FloatArrayList listFloat = new FloatArrayList(core.global.N_DrawableLines * 6);// (N*200);
FloatArrayList listFloatColor = new FloatArrayList(core.global.N_DrawableLines * 6);
FloatArrayList listFloatWidth = new FloatArrayList(core.global.N_DrawableLines * 2);
System.out.println(core.global.theShapes.size());
for (Shape sh : core.global.theShapes.values()) {
listFloat.addAll(sh.toListFloat());
listFloatColor.addAll(sh.toListFloatColor());
@@ -259,8 +258,14 @@ public void update_data(){
float[] vertices = listFloat.elements();
float[] colors = listFloatColor.elements();
float[] widths = listFloatWidth.elements();
//destroy old VBO (memory leak!)
if(null != interleavedVBO) {
st.ownAttribute(interleavedVBO, false);
interleavedVBO.destroy(gl3);
}
interleavedVBO = GLArrayDataServer.createGLSLInterleaved(3 + 3 + 1, GL3.GL_FLOAT, false,
core.global.N_DrawableLines * 2 * 3, GL3.GL_STATIC_DRAW);
update_vao(interleavedVBO, vertices, colors, widths, core.global.N_DrawableLines);
}
else if(GL_version == 2) {
@@ -292,7 +297,11 @@ public void update_dynamic_data(){
float[] colors = d.get_colors();
float[] widths = d.get_widths();
core.global.N_dynamic_lines = vertices.length / 6;
//destroy old VBO (memory leak!)
if(null != dynamic_interleavedVBO) {
st.ownAttribute(dynamic_interleavedVBO, false);
dynamic_interleavedVBO.destroy(gl3);
}
dynamic_interleavedVBO = GLArrayDataServer.createGLSLInterleaved(3 + 3 + 1, GL3.GL_FLOAT, false,
core.global.N_dynamic_lines * 2 * 3, GL3.GL_STATIC_DRAW);
update_vao(dynamic_interleavedVBO, vertices, colors, widths, core.global.N_dynamic_lines);
@@ -308,13 +317,13 @@ public void print_a(float[] a) {
public void update_vao(GLArrayDataServer iVBO, float[] vertices, float[] colors, float[] widths, int N_lines) {
st.useProgram(gl3, true);
iVBO.addGLSLSubArray("position", 3, GL3.GL_ARRAY_BUFFER);
iVBO.addGLSLSubArray("color", 3, GL3.GL_ARRAY_BUFFER);
iVBO.addGLSLSubArray("width", 1, GL3.GL_ARRAY_BUFFER);
final FloatBuffer ib = (FloatBuffer) iVBO.getBuffer();
for (int i = 0; i < N_lines * 2; i++) {
ib.put(vertices, i * 3, 3);
ib.put(colors, i * 3, 3);
@@ -498,8 +507,9 @@ public void update_data_GL2() {
for(GL2_data d: GL2_dataArray) {
d.vertices = d.listFloat.elements();
d.colors = d.listFloatColor.elements();
FloatBuffer fbVertices = Buffers.newDirectFloatBuffer(d.vertices);
FloatBuffer fbColores= Buffers.newDirectFloatBuffer(d.colors);
FloatBuffer fbColores = Buffers.newDirectFloatBuffer(d.colors);
gl2.glGenBuffers(2, d.vbo_buffer, 0);
@@ -15,7 +15,6 @@ public void keyTyped(KeyEvent e) {
if (c == '+' | c == '-'){
core.zoom.button_zoom(c);
}
}
@Override
@@ -101,8 +101,7 @@ public GUI(){
jframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowevent) {
jframe.dispose();
System.exit(0);
close();
}
});
@@ -399,6 +398,12 @@ public void actionPerformed(ActionEvent e)
}
public void close() {
jframe.dispose();
System.exit(0);
}
/*
public void set_GL_listeners(Mouse_events mouse, GL_canvas_event_listener glcanvas_events,
GL_key_listener key) {
@@ -121,7 +121,7 @@
public int N_DrawableLines = 0;
public int N_dynamic_lines = 0;
public int N = 5000;// number of test lines
public int N = 50020;// number of test lines
public Global_var() {
@@ -43,17 +43,17 @@ public void create_circle() {
for (int i = 0; i < points.length - 2; i += 2) {
add(new DrawableLine(points[i], points[i + 1], 0,
add(new DrawableLine(mode, points[i], points[i + 1], 0,
points[i + 2], points[i + 3], 0, color, width));
}
// last segment from last to first point
add(new DrawableLine(points[points.length - 2],
add(new DrawableLine(mode, points[points.length - 2],
points[points.length - 1], 0, points[0], points[1], 0, color, width));
// Crossers in center of circle
float s = Radius / 20;
add(new DrawableLine(theCenter.getX() - s, theCenter.getY(), 0, theCenter.getX() + s, theCenter.getY(), 0, color, width));
add(new DrawableLine(theCenter.getX(), theCenter.getY() - s, 0, theCenter.getX(), theCenter.getY() + s, 0, color, width));
add(new DrawableLine(mode, theCenter.getX() - s, theCenter.getY(), 0, theCenter.getX() + s, theCenter.getY(), 0, color, width));
add(new DrawableLine(mode, theCenter.getX(), theCenter.getY() - s, 0, theCenter.getX(), theCenter.getY() + s, 0, color, width));
// Snap center
add_snap_point(new SnapCoord(SnapType.MidPoint, theCenter));
@@ -3,6 +3,7 @@
import it.unimi.dsi.fastutil.floats.FloatArrayList;
import open_dxf_lib.Color_rgb;
import samoJ.Coord;
import samoJ.ObjectMode;
import core.Core;
import core.Global_var;
@@ -25,16 +26,16 @@
*/
private int width;
private DrawableLine(Coord c1, Coord c2, Color_rgb color, int width) {
private DrawableLine(ObjectMode mode, Coord c1, Coord c2, Color_rgb color, int width) {
super(c1, c2);
this.color_1 = this.color_2 = color;
this.width = width;
core.global.N_DrawableLines++;
if(mode == ObjectMode.New_object) core.global.N_DrawableLines++;
}
public DrawableLine(float x1, float y1, float z1, float x2, float y2,
public DrawableLine(ObjectMode mode, float x1, float y1, float z1, float x2, float y2,
float z2, Color_rgb color, int width) {
this(new Coord(x1, y1, z1), new Coord(x2, y2, z2), color, width);
this(mode, new Coord(x1, y1, z1), new Coord(x2, y2, z2), color, width);
}
public FloatArrayList toListFloatColor() {
@@ -41,11 +41,13 @@
protected Color_rgb color;
// Unique object ID
public int ID;
protected ObjectMode mode;
public Shape(ObjectMode mode) {
PrimLines = new LinkedList<DrawableLine>();
SnapLines = new LinkedList<Line>();
SnapPoints = new LinkedList<SnapCoord>();
this.mode = mode;
if(mode == ObjectMode.New_object){
core.global.theShapes.put(core.global.current_ID, this);
@@ -20,7 +20,7 @@
protected float y2;
protected float z2;
//protected int width; //!!!;
protected ObjectMode mode; //!!!;
/*
// Not dashed line
public Line(ObjectMode mode, double x1, double y1, double z1, double x2, double y2, double z2) {
@@ -58,7 +58,7 @@ public ShapeLine(ObjectMode mode, float x1, float y1, float z1, float x2, float
void formPrimitiveLines() {
if (dash.mask == null || dash.mask.length == 0) {
add(new DrawableLine(x1, y1, z1, x2, y2, z2, color, width));
add(new DrawableLine(mode, x1, y1, z1, x2, y2, z2, color, width));
/*
float h = 4 * Values.current_scale;
float x = x2 - x1;
@@ -120,7 +120,7 @@ void formPrimitiveLines() {
}
if ((i % 2) == 0) {
// System.out.println("draw");
add(new DrawableLine(x_begin, y_begin, z1, x_end,
add(new DrawableLine(mode, x_begin, y_begin, z1, x_end,
y_end, z2, color, width));
}
i++;

0 comments on commit f7e806e

Please sign in to comment.