diff options
author | Sven Gothel <[email protected]> | 2012-02-19 03:10:09 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2012-02-19 03:10:09 +0100 |
commit | fb7165e690546359dee92dd60b04be69f141c87e (patch) | |
tree | c6fed4d26fe157d1a23760b40b88f4739ecf8ff6 | |
parent | 4ae0b9a871467d3a322239b7ccb2ae682748289e (diff) |
Cleanup ShaderCode/Program/State
- Add multiple sources for create ShaderCode
- Add Shaderstate attachShaderProgram w/ enable flag
- Clarify doc
7 files changed, 66 insertions, 40 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java index cc75b89c0..85e288638 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java +++ b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderCode.java @@ -94,7 +94,7 @@ public class ShaderCode { String[][] shaderSources = null; if(null!=sourceFiles) { shaderSources = new String[sourceFiles.length][1]; - for(int i=0; null!=shaderSources && i<sourceFiles.length; i++) { + for(int i=0; i<sourceFiles.length; i++) { shaderSources[i][0] = readShaderSource(context, sourceFiles[i]); if(null == shaderSources[i][0]) { shaderSources = null; @@ -143,34 +143,46 @@ public class ShaderCode { public static ShaderCode create(GL2ES2 gl, int type, int number, Class<?> context, String srcRoot, String binRoot, String basename) { + return create(gl, type, number, context, srcRoot, new String[] { basename }, binRoot, basename ); + } + + public static ShaderCode create(GL2ES2 gl, int type, int number, Class<?> context, + String srcRoot, String[] srcBasenames, String binRoot, String binBasename) { ShaderCode res = null; - String srcFileName = null; + final String srcPath[]; + String srcPathString = null; String binFileName = null; - if(ShaderUtil.isShaderCompilerAvailable(gl)) { - String srcPath[] = new String[1]; - srcFileName = srcRoot + '/' + basename + "." + getFileSuffix(false, type); - srcPath[0] = srcFileName; + if(null!=srcRoot && null!=srcBasenames && ShaderUtil.isShaderCompilerAvailable(gl)) { + srcPath = new String[srcBasenames.length]; + final String srcSuffix = getFileSuffix(false, type); + for(int i=0; i<srcPath.length; i++) { + srcPath[i] = srcRoot + '/' + srcBasenames[i] + "." + srcSuffix; + } res = create(gl, type, number, context, srcPath); if(null!=res) { return res; } + srcPathString = Arrays.toString(srcPath); + } else { + srcPath = null; } - Set<Integer> binFmts = ShaderUtil.getShaderBinaryFormats(gl); - for(Iterator<Integer> iter=binFmts.iterator(); null==res && iter.hasNext(); ) { - int bFmt = iter.next().intValue(); - String bFmtPath = getBinarySubPath(bFmt); - if(null==bFmtPath) continue; - binFileName = binRoot + '/' + bFmtPath + '/' + basename + "." + getFileSuffix(true, type); - res = create(type, number, context, bFmt, binFileName); - } - - if(null==res) { - throw new GLException("No shader code found (source nor binary) for src: "+srcFileName+ - ", bin: "+binFileName); + if(null!=binRoot && null!=binBasename) { + Set<Integer> binFmts = ShaderUtil.getShaderBinaryFormats(gl); + final String binSuffix = getFileSuffix(true, type); + for(Iterator<Integer> iter=binFmts.iterator(); iter.hasNext(); ) { + int bFmt = iter.next().intValue(); + String bFmtPath = getBinarySubPath(bFmt); + if(null==bFmtPath) continue; + binFileName = binRoot + '/' + bFmtPath + '/' + binBasename + "." + binSuffix; + res = create(type, number, context, bFmt, binFileName); + if(null!=res) { + return res; + } + } } - - return res; + throw new GLException("No shader code found (source nor binary) for src: "+srcPathString+ + ", bin: "+binFileName); } /** diff --git a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderProgram.java b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderProgram.java index 8dd09ffab..22c582865 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderProgram.java +++ b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderProgram.java @@ -69,7 +69,8 @@ public class ShaderProgram { } /** - * Detaches all shader codes and deletes the program. + * Detaches all shader codes and deletes the program, + * but leaves the shader code intact. * Calls release(gl, false) * * @see #release(GL2ES2, boolean) @@ -80,16 +81,16 @@ public class ShaderProgram { /** * Detaches all shader codes and deletes the program. - * If releaseShaderToo is true, destroys the shader codes as well. + * If <code>destroyShaderCode</code> is true it destroys the shader codes as well. */ - public synchronized void release(GL2ES2 gl, boolean releaseShaderToo) { + public synchronized void release(GL2ES2 gl, boolean destroyShaderCode) { useProgram(gl, false); for(Iterator<ShaderCode> iter=allShaderCode.iterator(); iter.hasNext(); ) { ShaderCode shaderCode = iter.next(); if(attachedShaderCode.remove(shaderCode)) { ShaderUtil.detachShader(gl, shaderProgram, shaderCode.shader()); } - if(releaseShaderToo) { + if(destroyShaderCode) { shaderCode.destroy(gl); } } diff --git a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderState.java b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderState.java index 2ef977ecf..a276018d9 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderState.java +++ b/src/jogl/classes/com/jogamp/opengl/util/glsl/ShaderState.java @@ -197,12 +197,31 @@ public class ShaderState { * as well as switching to another program on the fly, * while managing all attribute and uniform data.</p> * - * <p>[Re]sets all data and use program in case of a program switch.<br> - * Use program if linked in case of a 1st time attachment.</p> + * <p>[Re]sets all data and use program in case of a program switch.<br> + * + * Use program if linked and if previous program was in use.</p> * * @throws GLException if program was not linked and linking fails */ public synchronized void attachShaderProgram(GL2ES2 gl, ShaderProgram prog) throws GLException { + attachShaderProgram(gl, prog, false); + } + + /** + * Attach or switch a shader program + * + * <p>Attaching a shader program the first time, + * as well as switching to another program on the fly, + * while managing all attribute and uniform data.</p> + * + * <p>[Re]sets all data and use program in case of a program switch.<br> + * + * Use program if linked and if previous program was in use, + * or if <code>enable</code> is true.</p> + * + * @throws GLException if program was not linked and linking fails + */ + public synchronized void attachShaderProgram(GL2ES2 gl, ShaderProgram prog, boolean enable) throws GLException { boolean prgInUse = false; // earmarked state if(DEBUG) { @@ -245,7 +264,7 @@ public class ShaderState { // or use program if program is linked if(shaderProgram.linked() || resetAllShaderData) { useProgram(gl, true); // may reset all data - if(!prgInUse) { + if(!prgInUse && !enable) { useProgram(gl, false); } } @@ -286,7 +305,7 @@ public class ShaderState { * @see #glReleaseAllUniforms * @see ShaderProgram#release(GL2ES2, boolean) */ - public synchronized void release(GL2ES2 gl, boolean destroyBoundAttributes, boolean releaseProgramToo, boolean releaseShaderToo) { + public synchronized void release(GL2ES2 gl, boolean destroyBoundAttributes, boolean destroyShaderProgram, boolean destroyShaderCode) { if(null!=shaderProgram) { shaderProgram.useProgram(gl, false); } @@ -297,10 +316,8 @@ public class ShaderState { } releaseAllAttributes(gl); releaseAllUniforms(gl); - if(null!=shaderProgram) { - if(releaseProgramToo) { - shaderProgram.release(gl, releaseShaderToo); - } + if(null!=shaderProgram && destroyShaderProgram) { + shaderProgram.release(gl, destroyShaderCode); } } diff --git a/src/jogl/classes/jogamp/opengl/util/glsl/fixedfunc/FixedFuncPipeline.java b/src/jogl/classes/jogamp/opengl/util/glsl/fixedfunc/FixedFuncPipeline.java index 7bc0c5468..973bcc147 100644 --- a/src/jogl/classes/jogamp/opengl/util/glsl/fixedfunc/FixedFuncPipeline.java +++ b/src/jogl/classes/jogamp/opengl/util/glsl/fixedfunc/FixedFuncPipeline.java @@ -449,8 +449,7 @@ public class FixedFuncPipeline { throw new GLException("Couldn't link VertexColorLight program: "+shaderProgramColorTextureLight); } - shaderState.attachShaderProgram(gl, shaderProgramColor); - shaderState.useProgram(gl, true); + shaderState.attachShaderProgram(gl, shaderProgramColor, true); // mandatory .. if(!shaderState.uniform(gl, new GLUniformData(mgl_PMVMatrix, 4, 4, pmvMatrix.glGetPMvMvitMatrixf()))) { diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/ElektronenMultiplizierer.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/ElektronenMultiplizierer.java index 52a98fde8..582335ae7 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/ElektronenMultiplizierer.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/ElektronenMultiplizierer.java @@ -239,8 +239,7 @@ public class ElektronenMultiplizierer implements GLEventListener { final ShaderProgram sp0 = new ShaderProgram(); sp0.add(gl, vp0, System.err); sp0.add(gl, fp0, System.err); - st.attachShaderProgram(gl, sp0); - st.useProgram(gl, true); + st.attachShaderProgram(gl, sp0, true); final float XRESf = (float) drawable.getWidth(); final float YRESf = (float) drawable.getHeight(); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/GearsES2.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/GearsES2.java index 51bc7d137..262a79671 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/GearsES2.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/GearsES2.java @@ -121,8 +121,7 @@ public class GearsES2 implements GLEventListener { final ShaderProgram sp0 = new ShaderProgram(); sp0.add(gl, vp0, System.err); sp0.add(gl, fp0, System.err); - st.attachShaderProgram(gl, sp0); - st.useProgram(gl, true); + st.attachShaderProgram(gl, sp0, true); // Use debug pipeline // drawable.setGL(new DebugGL(drawable.getGL())); diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/RedSquareES2.java b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/RedSquareES2.java index d645fb9f8..2a86a5bd8 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/RedSquareES2.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/demos/es2/RedSquareES2.java @@ -85,8 +85,7 @@ public class RedSquareES2 implements GLEventListener { final ShaderProgram sp0 = new ShaderProgram(); sp0.add(gl, vp0, System.err); sp0.add(gl, fp0, System.err); - st.attachShaderProgram(gl, sp0); - st.useProgram(gl, true); + st.attachShaderProgram(gl, sp0, true); // setup mgl_PMVMatrix pmvMatrix = new PMVMatrix(); |