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 /src/jogl/classes/com/jogamp/opengl/util | |
parent | 4ae0b9a871467d3a322239b7ccb2ae682748289e (diff) |
Cleanup ShaderCode/Program/State
- Add multiple sources for create ShaderCode
- Add Shaderstate attachShaderProgram w/ enable flag
- Clarify doc
Diffstat (limited to 'src/jogl/classes/com/jogamp/opengl/util')
3 files changed, 62 insertions, 32 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); } } |