package com.sun.opengl.util.glsl; import javax.media.opengl.*; import com.sun.opengl.util.*; import java.util.*; import java.nio.*; import java.io.*; import java.net.*; public class ShaderCode { public static final String SUFFIX_VERTEX_SOURCE = "vp" ; public static final String SUFFIX_VERTEX_BINARY = "bvp" ; public static final String SUFFIX_FRAGMENT_SOURCE = "fp" ; public static final String SUFFIX_FRAGMENT_BINARY = "bfp" ; public static final String SUB_PATH_NVIDIA = "nvidia" ; public ShaderCode(int type, int number, String[][] source) { switch (type) { case GL2ES2.GL_VERTEX_SHADER: case GL2ES2.GL_FRAGMENT_SHADER: break; default: throw new GLException("Unknown shader type: "+type); } shaderSource = source; shaderBinaryFormat = -1; shaderBinary = null; shaderType = type; shader = BufferUtil.newIntBuffer(number); id = getNextID(); } public ShaderCode(int type, int number, int binFormat, Buffer binary) { switch (type) { case GL2ES2.GL_VERTEX_SHADER: case GL2ES2.GL_FRAGMENT_SHADER: break; default: throw new GLException("Unknown shader type: "+type); } shaderSource = null; shaderBinaryFormat = binFormat; shaderBinary = binary; shaderType = type; shader = BufferUtil.newIntBuffer(number); id = getNextID(); } public static ShaderCode create(GL2ES2 gl, int type, int number, Class context, String[] sourceFiles) { if(!ShaderUtil.isShaderCompilerAvailable(gl)) return null; String[][] shaderSources = null; if(null!=sourceFiles) { shaderSources = new String[sourceFiles.length][1]; for(int i=0; null!=shaderSources && i= 0) { String tmpPath = className.substring(0, lastSlash + 1) + path; url = Locator.getResource(tmpPath, contextCL); if (url != null) { path = tmpPath; } } } if (url == null) { return null; } StringBuffer result = new StringBuffer(); readShaderSource(contextCL, path, url, result); return result.toString(); } public static ByteBuffer readShaderBinary(Class context, String path) { try { URL url = Locator.getResource(context, path); if (url == null) { return null; } return StreamUtil.readAll2Buffer(new BufferedInputStream(url.openStream())); } catch (IOException e) { throw new RuntimeException(e); } } //---------------------------------------------------------------------- // Internals only below this point // protected String[][] shaderSource = null; protected Buffer shaderBinary = null; protected int shaderBinaryFormat = -1; protected IntBuffer shader = null; protected int shaderType = -1; protected Integer id = null; protected boolean valid=false; private static synchronized Integer getNextID() { return new Integer(nextID++); } protected static int nextID = 1; }