diff options
author | Sven Gothel <[email protected]> | 2011-04-23 06:12:10 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-04-23 06:12:10 +0200 |
commit | 48201a6ea6471eb5951edb735b36156ab3410a15 (patch) | |
tree | b22314430e78ee9269f4fcb358b9b5a7dc8d1de7 /src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java | |
parent | 54f58c0cb990eb2b4fc8c3be785cc47bde575f37 (diff) |
Refactored graph: Reduce/remove data copy/recreation; Shader cleanup
- Pass the current GL context object where it's required
- Introduce RenderState (which has ShaderState) to acquire/change shader related data (Region)
- Shader Cleanup: User import for common stuff; use req. version
- Reduce/remove data copy/recreation in *Region implementation
- UI/RIButton: Use defaults I like :)
Diffstat (limited to 'src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java')
-rw-r--r-- | src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java | 220 |
1 files changed, 117 insertions, 103 deletions
diff --git a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java index 05814965e..181ce77b0 100644 --- a/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java +++ b/src/jogl/classes/jogamp/graph/curve/opengl/VBORegion2PES2.java @@ -32,15 +32,18 @@ import java.util.ArrayList; import javax.media.opengl.GL2ES2; // FIXME: Subsume GL2GL3.GL_DRAW_FRAMEBUFFER -> GL2ES2.GL_DRAW_FRAMEBUFFER ! import javax.media.opengl.GL; -import javax.media.opengl.GLContext; import javax.media.opengl.GLUniformData; import javax.media.opengl.fixedfunc.GLMatrixFunc; +import jogamp.graph.curve.opengl.shader.AttributeNames; +import jogamp.graph.curve.opengl.shader.UniformNames; + import com.jogamp.graph.geom.AABBox; import com.jogamp.graph.geom.Triangle; import com.jogamp.graph.geom.Vertex; import com.jogamp.graph.curve.Region; +import com.jogamp.graph.curve.opengl.RenderState; import com.jogamp.opengl.util.FBObject; import com.jogamp.opengl.util.GLArrayDataServer; import com.jogamp.opengl.util.PMVMatrix; @@ -51,74 +54,82 @@ public class VBORegion2PES2 implements Region { private ArrayList<Triangle> triangles = new ArrayList<Triangle>(); private ArrayList<Vertex> vertices = new ArrayList<Vertex>(); - private GLArrayDataServer verticeTxtAttr = null; - private GLArrayDataServer texCoordTxtAttr = null; - private GLArrayDataServer indicesTxt = null; - private GLArrayDataServer verticeFboAttr = null; - private GLArrayDataServer texCoordFboAttr = null; - private GLArrayDataServer indicesFbo = null; - - private GLContext context; + private GLArrayDataServer verticeTxtAttr; + private GLArrayDataServer texCoordTxtAttr; + private GLArrayDataServer indicesTxt; + private GLArrayDataServer verticeFboAttr; + private GLArrayDataServer texCoordFboAttr; + private GLArrayDataServer indicesFbo; private boolean flipped = false; - private boolean dirty = false; + private boolean dirty = true; - private AABBox box = null; - private FBObject fbo = null; + private AABBox box; + private FBObject fbo; + private PMVMatrix fboPMVMatrix; + GLUniformData mgl_fboPMVMatrix; + private int tex_width_c = 0; private int tex_height_c = 0; + GLUniformData mgl_ActiveTexture; + int activeTexture; // texture engine 0 == GL.GL_TEXTURE0 - private ShaderState st; - - public VBORegion2PES2(GLContext context, ShaderState st){ - this.context =context; - this.st = st; + public VBORegion2PES2(RenderState rs, int textureEngine) { + fboPMVMatrix = new PMVMatrix(); + mgl_fboPMVMatrix = new GLUniformData(UniformNames.gcu_PMVMatrix, 4, 4, fboPMVMatrix.glGetPMvMatrixf()); - GL2ES2 gl = context.getGL().getGL2ES2(); + activeTexture = GL.GL_TEXTURE0 + textureEngine; + mgl_ActiveTexture = new GLUniformData(UniformNames.gcu_TextureUnit, textureEngine); - indicesFbo = GLArrayDataServer.createGLSL(gl, null, 3, GL2ES2.GL_SHORT, false, - 2, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); + final int initialSize = 256; + final ShaderState st = rs.getShaderState(); + + indicesFbo = GLArrayDataServer.createData(3, GL2ES2.GL_SHORT, initialSize, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); indicesFbo.puts((short) 0); indicesFbo.puts((short) 1); indicesFbo.puts((short) 3); indicesFbo.puts((short) 1); indicesFbo.puts((short) 2); indicesFbo.puts((short) 3); - indicesFbo.seal(gl, true); + indicesFbo.seal(true); - texCoordFboAttr = GLArrayDataServer.createGLSL(gl, Region.TEXCOORD_ATTR_NAME, 2, GL2ES2.GL_FLOAT, false, - 4, GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); - texCoordFboAttr.setLocation(Region.TEXCOORD_ATTR_IDX); + texCoordFboAttr = GLArrayDataServer.createGLSL(st, AttributeNames.TEXCOORD_ATTR_NAME, 2, + GL2ES2.GL_FLOAT, false, initialSize, GL.GL_STATIC_DRAW); + st.bindAttribute(texCoordFboAttr); texCoordFboAttr.putf(5); texCoordFboAttr.putf(5); texCoordFboAttr.putf(5); texCoordFboAttr.putf(6); texCoordFboAttr.putf(6); texCoordFboAttr.putf(6); texCoordFboAttr.putf(6); texCoordFboAttr.putf(5); - texCoordFboAttr.seal(gl, true); + texCoordFboAttr.seal(true); - verticeFboAttr = GLArrayDataServer.createGLSL(gl, Region.VERTEX_ATTR_NAME, 3, GL2ES2.GL_FLOAT, false, - 4, GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); - verticeFboAttr.setLocation(Region.VERTEX_ATTR_IDX); - verticeFboAttr.putf(0f); indicesFbo.putf(0f); indicesFbo.putf(0f); - verticeFboAttr.putf(0f); indicesFbo.putf(0f); indicesFbo.putf(0f); - verticeFboAttr.putf(0f); indicesFbo.putf(0f); indicesFbo.putf(0f); - verticeFboAttr.putf(0f); indicesFbo.putf(0f); indicesFbo.putf(0f); - verticeFboAttr.seal(gl, true); + verticeFboAttr = GLArrayDataServer.createGLSL(st, AttributeNames.VERTEX_ATTR_NAME, 3, + GL2ES2.GL_FLOAT, false, initialSize, GL.GL_STATIC_DRAW); + st.bindAttribute(verticeFboAttr); - verticeFboAttr.enableBuffer(gl, false); - texCoordFboAttr.enableBuffer(gl, false); - indicesFbo.enableBuffer(gl, false); - if(DEBUG) { + + box = new AABBox(); + + indicesTxt = GLArrayDataServer.createData(3, GL2ES2.GL_SHORT, initialSize, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); + + verticeTxtAttr = GLArrayDataServer.createGLSL(st, AttributeNames.VERTEX_ATTR_NAME, 3, + GL2ES2.GL_FLOAT, false, initialSize, GL.GL_STATIC_DRAW); + st.bindAttribute(verticeTxtAttr); + + texCoordTxtAttr = GLArrayDataServer.createGLSL(st, AttributeNames.TEXCOORD_ATTR_NAME, 2, + GL2ES2.GL_FLOAT, false, initialSize, GL.GL_STATIC_DRAW); + st.bindAttribute(texCoordTxtAttr); + + if(DEBUG_INSTANCE) { System.err.println("VBORegion2PES2 Create: " + this); } } - public void update(){ - GL2ES2 gl = context.getGL().getGL2ES2(); - - destroyTxtAttr(gl); + public void update(GL2ES2 gl){ + if(!dirty) { + return; + } - box = new AABBox(); - - indicesTxt = GLArrayDataServer.createGLSL(gl, null, 3, GL2ES2.GL_SHORT, false, - triangles.size(), GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER); + // process triangles + indicesTxt.seal(gl, false); + indicesTxt.rewind(); for(Triangle t:triangles){ if(t.getVertices()[0].getId() == Integer.MAX_VALUE){ t.getVertices()[0].setId(numVertices++); @@ -144,10 +155,14 @@ public class VBORegion2PES2 implements Region { } } indicesTxt.seal(gl, true); + indicesTxt.enableBuffer(gl, false); - verticeTxtAttr = GLArrayDataServer.createGLSL(gl, Region.VERTEX_ATTR_NAME, 3, GL2ES2.GL_FLOAT, false, - vertices.size(), GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); - verticeTxtAttr.setLocation(Region.VERTEX_ATTR_IDX); + // process vertices and update bbox + box.reset(); + verticeTxtAttr.seal(gl, false); + verticeTxtAttr.rewind(); + texCoordTxtAttr.seal(gl, false); + texCoordTxtAttr.rewind(); for(Vertex v:vertices){ verticeTxtAttr.putf(v.getX()); if(flipped){ @@ -161,52 +176,65 @@ public class VBORegion2PES2 implements Region { } else { box.resize(v.getX(), v.getY(), v.getZ()); } - } - verticeTxtAttr.seal(gl, true); - - texCoordTxtAttr = GLArrayDataServer.createGLSL(gl, Region.TEXCOORD_ATTR_NAME, 2, GL2ES2.GL_FLOAT, false, - vertices.size(), GL.GL_STATIC_DRAW, GL.GL_ARRAY_BUFFER); - texCoordTxtAttr.setLocation(Region.TEXCOORD_ATTR_IDX); - for(Vertex v:vertices){ - float[] tex = v.getTexCoord(); + + final float[] tex = v.getTexCoord(); texCoordTxtAttr.putf(tex[0]); - texCoordTxtAttr.putf(tex[1]); + texCoordTxtAttr.putf(tex[1]); } texCoordTxtAttr.seal(gl, true); - - // leave the buffers enabled for subsequent render call + texCoordTxtAttr.enableBuffer(gl, false); + verticeTxtAttr.seal(gl, true); + verticeTxtAttr.enableBuffer(gl, false); + + // update all bbox related data + verticeFboAttr.seal(gl, false); + verticeFboAttr.rewind(); + verticeFboAttr.putf(box.getLow()[0]); verticeFboAttr.putf(box.getLow()[1]); verticeFboAttr.putf(box.getLow()[2]); + verticeFboAttr.putf(box.getLow()[0]); verticeFboAttr.putf(box.getHigh()[1]); verticeFboAttr.putf(box.getLow()[2]); + verticeFboAttr.putf(box.getHigh()[0]); verticeFboAttr.putf(box.getHigh()[1]); verticeFboAttr.putf(box.getLow()[2]); + verticeFboAttr.putf(box.getHigh()[0]); verticeFboAttr.putf(box.getLow()[1]); verticeFboAttr.putf(box.getLow()[2]); + verticeFboAttr.seal(gl, true); + verticeFboAttr.enableBuffer(gl, false); + + fboPMVMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); + fboPMVMatrix.glLoadIdentity(); + fboPMVMatrix.glOrthof(box.getLow()[0], box.getHigh()[0], box.getLow()[1], box.getHigh()[1], -1, 1); + + // push data 2 GPU .. + indicesFbo.seal(gl, true); + indicesFbo.enableBuffer(gl, false); dirty = false; + + // the buffers were disabled, since due to real/fbo switching and other vbo usage } - public void render(PMVMatrix matrix, int vp_width, int vp_height, int width){ - GL2ES2 gl = context.getGL().getGL2ES2(); - if(null == matrix || vp_width <=0 || vp_height <= 0 || width <= 0){ + public void render(GL2ES2 gl, RenderState rs, int vp_width, int vp_height, int width) { + if(vp_width <=0 || vp_height <= 0 || width <= 0){ renderRegion(gl); } else { if(width != tex_width_c){ - renderRegion2FBO(gl, matrix, width); - setupBBox2FboAttr(gl); + renderRegion2FBO(gl, rs, width); } -// System.out.println("Scale: " + matrix.glGetMatrixf().get(1+4*3) +" " + matrix.glGetMatrixf().get(2+4*3)); - renderFBO(gl, matrix, vp_width, vp_height); + // System.out.println("Scale: " + matrix.glGetMatrixf().get(1+4*3) +" " + matrix.glGetMatrixf().get(2+4*3)); + renderFBO(gl, rs, vp_width, vp_height); } } - private void renderFBO(GL2ES2 gl, PMVMatrix matrix, int width, int hight) { - gl.glViewport(0, 0, width, hight); - if(!st.glUniform(gl, new GLUniformData("mgl_PMVMatrix", 4, 4, matrix.glGetPMvMatrixf()))){ - System.out.println("Cnt set tex based mat"); - } - gl.glEnable(GL2ES2.GL_TEXTURE_2D); - gl.glActiveTexture(GL2ES2.GL_TEXTURE0); - fbo.use(gl); + private void renderFBO(GL2ES2 gl, RenderState rs, int width, int hight) { + final ShaderState st = rs.getShaderState(); - st.glUniform(gl, new GLUniformData("texture", fbo.getTextureName())); - int loc = gl.glGetUniformLocation(st.shaderProgram().program(), "texture"); - gl.glUniform1i(loc, 0); + gl.glViewport(0, 0, width, hight); + gl.glEnable(GL2ES2.GL_TEXTURE_2D); + /* setback: + int[] currentActiveTextureEngine = new int[1]; + gl.glGetIntegerv(GL.GL_ACTIVE_TEXTURE, currentActiveTextureEngine, 0); + */ + gl.glActiveTexture(activeTexture); + st.glUniform(gl, mgl_ActiveTexture); + fbo.use(gl); verticeFboAttr.enableBuffer(gl, true); texCoordFboAttr.enableBuffer(gl, true); indicesFbo.enableBuffer(gl, true); @@ -215,22 +243,15 @@ public class VBORegion2PES2 implements Region { verticeFboAttr.enableBuffer(gl, false); texCoordFboAttr.enableBuffer(gl, false); - indicesFbo.enableBuffer(gl, false); - } - - private void setupBBox2FboAttr(GL2ES2 gl){ - verticeFboAttr.seal(gl, false); - verticeFboAttr.rewind(); - - verticeFboAttr.putf(box.getLow()[0]); verticeFboAttr.putf(box.getLow()[1]); verticeFboAttr.putf(box.getLow()[2]); - verticeFboAttr.putf(box.getLow()[0]); verticeFboAttr.putf(box.getHigh()[1]); verticeFboAttr.putf(box.getLow()[2]); - verticeFboAttr.putf(box.getHigh()[0]); verticeFboAttr.putf(box.getHigh()[1]); verticeFboAttr.putf(box.getLow()[2]); - verticeFboAttr.putf(box.getHigh()[0]); verticeFboAttr.putf(box.getLow()[1]); verticeFboAttr.putf(box.getLow()[2]); + indicesFbo.enableBuffer(gl, false); + fbo.unuse(gl); - verticeFboAttr.seal(gl, true); + // setback: gl.glActiveTexture(currentActiveTextureEngine[0]); } - private void renderRegion2FBO(GL2ES2 gl, PMVMatrix m, int tex_width){ + private void renderRegion2FBO(GL2ES2 gl, RenderState rs, int tex_width) { + final ShaderState st = rs.getShaderState(); + tex_width_c = tex_width; tex_height_c = (int)(tex_width_c*box.getHeight()/box.getWidth()); @@ -253,21 +274,15 @@ public class VBORegion2PES2 implements Region { } //render texture - PMVMatrix tex_matrix = new PMVMatrix(); gl.glViewport(0, 0, tex_width_c, tex_height_c); - tex_matrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION); - tex_matrix.glLoadIdentity(); - tex_matrix.glOrthof(box.getLow()[0], box.getHigh()[0], box.getLow()[1], box.getHigh()[1], -1, 1); - - if(!st.glUniform(gl, new GLUniformData("mgl_PMVMatrix", 4, 4, tex_matrix.glGetPMvMatrixf()))){ - System.out.println("Cnt set tex based mat"); - } + st.glUniform(gl, mgl_fboPMVMatrix); // use orthogonal matrix gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glClear(GL2ES2.GL_COLOR_BUFFER_BIT | GL2ES2.GL_DEPTH_BUFFER_BIT); renderRegion(gl); - fbo.unbind(gl); + + st.glUniform(gl, rs.getPMVMatrixUniform()); // switch back to real PMV matrix } private void renderRegion(GL2ES2 gl) { @@ -301,11 +316,10 @@ public class VBORegion2PES2 implements Region { return dirty; } - public void destroy() { - if(DEBUG) { + public void destroy(GL2ES2 gl) { + if(DEBUG_INSTANCE) { System.err.println("VBORegion2PES2 Destroy: " + this); - } - GL2ES2 gl = context.getGL().getGL2ES2(); + } destroyFbo(gl); destroyTxtAttr(gl); destroyFboAttr(gl); |