diff options
author | Sven Gothel <[email protected]> | 2009-08-01 07:20:33 -0700 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2009-08-01 07:20:33 -0700 |
commit | 3d7e3437e52d1bbc9031e4be0325ea6dea3b33b8 (patch) | |
tree | 1187ff2e40c4567512ac3cef0784e76ecf4baad9 /src/jogl/classes/com/sun/opengl/util | |
parent | 4e0a5af0b359b98b26ea3e961d023c658650be6c (diff) |
Cleanup GLSL ShaderUtil/Code; Add dumpCode
Diffstat (limited to 'src/jogl/classes/com/sun/opengl/util')
3 files changed, 124 insertions, 62 deletions
diff --git a/src/jogl/classes/com/sun/opengl/util/glsl/ShaderCode.java b/src/jogl/classes/com/sun/opengl/util/glsl/ShaderCode.java index 6e7512e6a..606d2e1b5 100644 --- a/src/jogl/classes/com/sun/opengl/util/glsl/ShaderCode.java +++ b/src/jogl/classes/com/sun/opengl/util/glsl/ShaderCode.java @@ -3,13 +3,18 @@ package com.sun.opengl.util.glsl; import javax.media.opengl.*; import com.sun.opengl.util.*; +import com.sun.opengl.impl.Debug; import java.util.*; import java.nio.*; import java.io.*; import java.net.*; +import java.security.*; public class ShaderCode { + public static final boolean DEBUG = Debug.debug("GLSLCode"); + public static final boolean DEBUG_CODE = Debug.isPropertyDefined("jogl.debug.GLSLCode", true, AccessController.getContext()); + public static final String SUFFIX_VERTEX_SOURCE = "vp" ; public static final String SUFFIX_VERTEX_BINARY = "bvp" ; public static final String SUFFIX_FRAGMENT_SOURCE = "fp" ; @@ -31,6 +36,11 @@ public class ShaderCode { shaderType = type; shader = BufferUtil.newIntBuffer(number); id = getNextID(); + + if(DEBUG_CODE) { + System.out.println("Created: "+toString()); + dumpShaderSource(System.out); + } } public ShaderCode(int type, int number, int binFormat, Buffer binary) { @@ -134,61 +144,6 @@ public class ShaderCode { return res; } - public static boolean createAndLoadShader(GL2ES2 gl, IntBuffer shader, int shaderType, - int binFormat, java.nio.Buffer bin, - PrintStream verboseOut) - { - int err = gl.glGetError(); // flush previous errors .. - if(err!=GL.GL_NO_ERROR && null!=verboseOut) { - verboseOut.println("createAndLoadShader: Pre GL Error: 0x"+Integer.toHexString(err)); - } - - ShaderUtil.createShader(gl, shaderType, shader); - err = gl.glGetError(); - if(err!=GL.GL_NO_ERROR) { - throw new GLException("createAndLoadShader: CreateShader failed, GL Error: 0x"+Integer.toHexString(err)); - } - - - ShaderUtil.shaderBinary(gl, shader, binFormat, bin); - - err = gl.glGetError(); - if(err!=GL.GL_NO_ERROR && null!=verboseOut) { - verboseOut.println("createAndLoadShader: ShaderBinary failed, GL Error: 0x"+Integer.toHexString(err)); - } - return err == GL.GL_NO_ERROR; - } - - public static boolean createAndCompileShader(GL2ES2 gl, IntBuffer shader, int shaderType, - java.lang.String[][] sources, - PrintStream verboseOut) - { - int err = gl.glGetError(); // flush previous errors .. - if(err!=GL.GL_NO_ERROR && null!=verboseOut) { - verboseOut.println("createAndCompileShader: Pre GL Error: 0x"+Integer.toHexString(err)); - } - - ShaderUtil.createShader(gl, shaderType, shader); - err = gl.glGetError(); - if(err!=GL.GL_NO_ERROR) { - throw new GLException("createAndCompileShader: CreateShader failed, GL Error: 0x"+Integer.toHexString(err)); - } - - ShaderUtil.shaderSource(gl, shader, sources); - err = gl.glGetError(); - if(err!=GL.GL_NO_ERROR) { - throw new GLException("createAndCompileShader: ShaderSource failed, GL Error: 0x"+Integer.toHexString(err)); - } - - ShaderUtil.compileShader(gl, shader); - err = gl.glGetError(); - if(err!=GL.GL_NO_ERROR && null!=verboseOut) { - verboseOut.println("createAndCompileShader: CompileShader failed, GL Error: 0x"+Integer.toHexString(err)); - } - - return ShaderUtil.isShaderStatusValid(gl, shader, gl.GL_COMPILE_STATUS, verboseOut) && err == GL.GL_NO_ERROR; - } - /** * returns the uniq shader id as an integer * @see #key() @@ -231,11 +186,11 @@ public class ShaderCode { // Create & Compile the vertex/fragment shader objects if(null!=shaderSource) { - valid=createAndCompileShader(gl, shader, shaderType, - shaderSource, verboseOut); + valid=ShaderUtil.createAndCompileShader(gl, shader, shaderType, + shaderSource, verboseOut); } else if(null!=shaderBinary) { - valid=createAndLoadShader(gl, shader, shaderType, - shaderBinaryFormat, shaderBinary, verboseOut); + valid=ShaderUtil.createAndLoadShader(gl, shader, shaderType, + shaderBinaryFormat, shaderBinary, verboseOut); } else { throw new GLException("no code (source or binary)"); } @@ -282,6 +237,31 @@ public class ShaderCode { return buf.toString(); } + public void dumpShaderSource(PrintStream out) { + if(null==shaderSource) { + out.println("<no shader source>"); + return; + } + int sourceNum = (null!=shaderSource)?shaderSource.length:0; + int shaderNum = (null!=shader)?shader.capacity():0; + for(int i=0; i<shaderNum; i++) { + out.println(""); + out.println("Shader #"+i+"/"+shaderNum+" name "+shader.get(i)); + out.println("--------------------------------------------------------------"); + if(i>=sourceNum) { + out.println("<no shader source>"); + } else { + String[] src = shaderSource[i]; + for(int j=0; j<src.length; j++) { + out.println("Segment "+j+"/"+src.length+" :"); + out.println(src[j]); + out.println(""); + } + } + out.println("--------------------------------------------------------------"); + } + } + public static void readShaderSource(ClassLoader context, String path, URL url, StringBuffer result) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); diff --git a/src/jogl/classes/com/sun/opengl/util/glsl/ShaderState.java b/src/jogl/classes/com/sun/opengl/util/glsl/ShaderState.java index bab9ef4f3..d8f6eeea2 100644 --- a/src/jogl/classes/com/sun/opengl/util/glsl/ShaderState.java +++ b/src/jogl/classes/com/sun/opengl/util/glsl/ShaderState.java @@ -3,14 +3,17 @@ package com.sun.opengl.util.glsl; import javax.media.opengl.*; import com.sun.opengl.util.*; +import com.sun.opengl.impl.Debug; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.nio.*; import java.io.PrintStream; +import java.security.*; public class ShaderState { + public static final boolean DEBUG = Debug.isPropertyDefined("jogl.debug.GLSLState", true, AccessController.getContext()); public ShaderState() { } @@ -77,8 +80,10 @@ public class ShaderState { int curId = (null!=shaderProgram)?shaderProgram.id():-1; int newId = (null!=prog)?prog.id():-1; System.err.println("Info: attachShaderProgram: "+curId+" -> "+newId+"\n\t"+shaderProgram+"\n\t"+prog); - Throwable tX = new Throwable("Info: attachShaderProgram: Trace"); - tX.printStackTrace(); + if(verbose) { + Throwable tX = new Throwable("Info: attachShaderProgram: Trace"); + tX.printStackTrace(); + } } if(null!=shaderProgram) { if(shaderProgram.equals(prog)) { @@ -635,7 +640,6 @@ public class ShaderState { return buf.toString(); } - protected static final boolean DEBUG = false; protected boolean verbose = false; protected ShaderProgram shaderProgram=null; protected HashMap attribMap2Idx = new HashMap(); diff --git a/src/jogl/classes/com/sun/opengl/util/glsl/ShaderUtil.java b/src/jogl/classes/com/sun/opengl/util/glsl/ShaderUtil.java index d1bb8b32b..390fb27c7 100644 --- a/src/jogl/classes/com/sun/opengl/util/glsl/ShaderUtil.java +++ b/src/jogl/classes/com/sun/opengl/util/glsl/ShaderUtil.java @@ -60,6 +60,14 @@ public class ShaderUtil { public abstract void attachShader(GL gl, int program, IntBuffer shaders); public abstract void detachShader(GL gl, int program, IntBuffer shaders); public abstract void deleteShader(GL gl, IntBuffer shaders); + + public abstract boolean createAndLoadShader(GL gl, IntBuffer shader, int shaderType, + int binFormat, java.nio.Buffer bin, + PrintStream verboseOut); + + public abstract boolean createAndCompileShader(GL gl, IntBuffer shader, int shaderType, + java.lang.String[][] sources, + PrintStream verboseOut); } static class GL2ES2Impl extends Impl { @@ -307,6 +315,64 @@ public class ShaderUtil { } } + + public boolean createAndLoadShader(GL _gl, IntBuffer shader, int shaderType, + int binFormat, java.nio.Buffer bin, + PrintStream verboseOut) + { + GL2ES2 gl = _gl.getGL2ES2(); + int err = gl.glGetError(); // flush previous errors .. + if(err!=GL.GL_NO_ERROR && null!=verboseOut) { + verboseOut.println("createAndLoadShader: Pre GL Error: 0x"+Integer.toHexString(err)); + } + + createShader(gl, shaderType, shader); + err = gl.glGetError(); + if(err!=GL.GL_NO_ERROR) { + throw new GLException("createAndLoadShader: CreateShader failed, GL Error: 0x"+Integer.toHexString(err)); + } + + + shaderBinary(gl, shader, binFormat, bin); + + err = gl.glGetError(); + if(err!=GL.GL_NO_ERROR && null!=verboseOut) { + verboseOut.println("createAndLoadShader: ShaderBinary failed, GL Error: 0x"+Integer.toHexString(err)); + } + return err == GL.GL_NO_ERROR; + } + + public boolean createAndCompileShader(GL _gl, IntBuffer shader, int shaderType, + java.lang.String[][] sources, + PrintStream verboseOut) + { + GL2ES2 gl = _gl.getGL2ES2(); + int err = gl.glGetError(); // flush previous errors .. + if(err!=GL.GL_NO_ERROR && null!=verboseOut) { + verboseOut.println("createAndCompileShader: Pre GL Error: 0x"+Integer.toHexString(err)); + } + + createShader(gl, shaderType, shader); + err = gl.glGetError(); + if(err!=GL.GL_NO_ERROR) { + throw new GLException("createAndCompileShader: CreateShader failed, GL Error: 0x"+Integer.toHexString(err)); + } + + shaderSource(gl, shader, sources); + err = gl.glGetError(); + if(err!=GL.GL_NO_ERROR) { + throw new GLException("createAndCompileShader: ShaderSource failed, GL Error: 0x"+Integer.toHexString(err)); + } + + compileShader(gl, shader); + err = gl.glGetError(); + if(err!=GL.GL_NO_ERROR && null!=verboseOut) { + verboseOut.println("createAndCompileShader: CompileShader failed, GL Error: 0x"+Integer.toHexString(err)); + } + + return isShaderStatusValid(gl, shader, gl.GL_COMPILE_STATUS, verboseOut) && err == GL.GL_NO_ERROR; + } + } public static String getShaderInfoLog(GL gl, int shaderObj) { @@ -385,6 +451,18 @@ public class ShaderUtil { getImpl(gl).deleteShader(gl, shaders); } + public static boolean createAndLoadShader(GL gl, IntBuffer shader, int shaderType, + int binFormat, java.nio.Buffer bin, + PrintStream verboseOut) { + return getImpl(gl).createAndLoadShader(gl, shader, shaderType, binFormat, bin, verboseOut); + } + + public static boolean createAndCompileShader(GL gl, IntBuffer shader, int shaderType, + java.lang.String[][] sources, + PrintStream verboseOut) { + return getImpl(gl).createAndCompileShader(gl, shader, shaderType, sources, verboseOut); + } + private static Impl getImpl(GL _gl) { GL2ES2 gl = _gl.getGL2ES2(); GLContext context = gl.getContext(); |