public static int linkProgram( GL2ES2 gl, int... shaders ) { int program = gl.glCreateProgram( ); for ( int s : shaders ) gl.glAttachShader( program, s ); try { gl.glLinkProgram( program ); int[] linkStatus = new int[ 1 ]; gl.glGetProgramiv( program, GL_LINK_STATUS, linkStatus, 0 ); if ( linkStatus[ 0 ] != GL_TRUE ) throw new RuntimeException( getProgramInfoLog( gl, program ) ); return program; } finally { for ( int s : shaders ) gl.glDetachShader( program, s ); } }
public DncIconProgramHandles( GL2ES2 gl ) { this.program = createProgram( gl, dncIconVertShader_GLSL, dncIconGeomShader_GLSL, dncIconFragShader_GLSL ); this.AXIS_RECT = gl.glGetUniformLocation( program, "AXIS_RECT" ); this.VIEWPORT_SIZE_PX = gl.glGetUniformLocation( program, "VIEWPORT_SIZE_PX" ); this.ATLAS = gl.glGetUniformLocation( program, "ATLAS" ); this.IMAGE_BOUNDS = gl.glGetUniformLocation( program, "IMAGE_BOUNDS" ); this.IMAGE_SIZE_PX = gl.glGetUniformLocation( program, "IMAGE_SIZE_PX" ); this.IMAGE_ALIGN = gl.glGetUniformLocation( program, "IMAGE_ALIGN" ); this.HIGHLIGHT_SET = gl.glGetUniformLocation( program, "HIGHLIGHT_SET" ); this.HIGHLIGHT_SCALE = gl.glGetUniformLocation( program, "HIGHLIGHT_SCALE" ); this.inIconVertex = gl.glGetAttribLocation( program, "inIconVertex" ); } }
public static String getProgramInfoLog( GL2ES2 gl, int program ) { int[] maxLength = new int[ 1 ]; gl.glGetProgramiv( program, GL_INFO_LOG_LENGTH, maxLength, 0 ); if ( maxLength[ 0 ] == 0 ) return ""; int[] length = new int[ 1 ]; byte[] bytes = new byte[ maxLength[ 0 ] ]; gl.glGetProgramInfoLog( program, maxLength[ 0 ], length, 0, bytes, 0 ); return new String( bytes, 0, length[ 0 ], UTF_8 ); }
public static int compileShader( GL2ES2 gl, int shaderType, String[] sources ) { int shader = gl.glCreateShader( shaderType ); gl.glShaderSource( shader, 1, sources, null ); gl.glCompileShader( shader ); int[] compileStatus = new int[ 1 ]; gl.glGetShaderiv( shader, GL_COMPILE_STATUS, compileStatus, 0 ); if ( compileStatus[ 0 ] != GL_TRUE ) throw new RuntimeException( getShaderInfoLog( gl, shader ) ); return shader; }
gl.glEnable( GL_BLEND ); gl.glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA ); gl.getGL3( ).glBindVertexArray( GLUtils.defaultVertexAttributeArray( gl ) ); gl.glGetIntegerv( GL_MAX_TEXTURE_SIZE, maxTextureDim, 0 ); this.rasterizeArgs = new RasterizeArgs( maxTextureDim[ 0 ], context.getDPI( ) ); logger.fine( "Rasterization args: max-texture-dim = " + rasterizeArgs.maxTextureDim + ", screen-dpi = " + rasterizeArgs.screenDpi ); gl.glActiveTexture( GL_TEXTURE0 + highlightSetTextureUnit ); highlightSet.bind( gl, dChunk.featureCount, rasterizeArgs.maxTextureDim ); gl.glUseProgram( handles.program ); gl.glUniform4fv( handles.RGBA, 1, style.fillRgba, 0 ); gl.glUniform1i( handles.HIGHLIGHT_SET, highlightSetTextureUnit ); gl.glEnableVertexAttribArray( handles.inAreaVertex ); gl.glBindBuffer( GL_ARRAY_BUFFER, dChunk.verticesHandle ); gl.glVertexAttribPointer( handles.inAreaVertex, coordsPerRenderTriangleVertex, GL_FLOAT, false, 0, group.trianglesCoordFirst * SIZEOF_FLOAT ); gl.glDrawArrays( GL_TRIANGLES, 0, group.trianglesCoordCount / coordsPerRenderTriangleVertex ); gl.glDisableVertexAttribArray( handles.inAreaVertex ); gl.glUseProgram( handles.program ); gl.glUniform4fv( handles.RGBA, 1, style.lineRgba, 0 );
} else { if (gl.isGL2ES2()) { state._programID = gl.getGL2ES2().glCreateProgram(); } else { if (gl.isGL2ES2()) { state._vertexShaderID = gl.getGL2ES2().glCreateShader(GL2ES2.GL_VERTEX_SHADER); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glShaderSource(state._vertexShaderID, 1, new String[] { new String(array) }, new int[] { array.length }, 0); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glCompileShader(state._vertexShaderID); gl.getGL2ES2().glGetShaderiv(state._vertexShaderID, GL2ES2.GL_COMPILE_STATUS, compiled); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glAttachShader(state._programID, state._vertexShaderID); } else { if (gl.isGL2ES2()) { state._fragmentShaderID = gl.getGL2ES2().glCreateShader(GL2ES2.GL_FRAGMENT_SHADER); } else { if (gl.isGL2ES2()) { gl.getGL2ES2().glShaderSource(state._fragmentShaderID, 1, new String[] { new String(array) },
glsl.glBindBuffer(ad.getVBOTarget(), ad.getVBOName()); if(null!=buffer) { glsl.glBufferData(ad.getVBOTarget(), ad.getSizeInBytes(), buffer, ad.getVBOUsage()); glsl.glVertexAttribPointer(ad); glsl.glBindBuffer(ad.getVBOTarget(), 0); } else { glsl.glGetVertexAttribiv(location, GL2ES2.GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, tempI, 0); if(ad.getVBOName() != tempI[0]) { glsl.glBindBuffer(ad.getVBOTarget(), ad.getVBOName()); glsl.glVertexAttribPointer(ad); glsl.glBindBuffer(ad.getVBOTarget(), 0); glsl.glVertexAttribPointer(ad); glsl.glEnableVertexAttribArray(location); } else { glsl.glDisableVertexAttribArray(location);
throw new InternalError("Using VBO but no vboName"); glsl.glBindBuffer(GL.GL_ARRAY_BUFFER, vboName); if(!bufferWritten) { writeBuffer(gl); glsl.glEnableVertexAttribArray(vArrayData.getLocation()); glsl.glVertexAttribPointer(vArrayData); } else { glsl.glDisableVertexAttribArray(vArrayData.getLocation()); glsl.glEnableVertexAttribArray(cArrayData.getLocation()); glsl.glVertexAttribPointer(cArrayData); } else { glsl.glDisableVertexAttribArray(cArrayData.getLocation()); glsl.glEnableVertexAttribArray(nArrayData.getLocation()); glsl.glVertexAttribPointer(nArrayData); } else { glsl.glDisableVertexAttribArray(nArrayData.getLocation()); glsl.glEnableVertexAttribArray(tArrayData.getLocation()); glsl.glVertexAttribPointer(tArrayData); } else { glsl.glDisableVertexAttribArray(tArrayData.getLocation()); glsl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
private final void setAttribute(final GL2ES2 gl, final GLArrayData attribute) { // get new location .. final String name = attribute.getName(); final int loc = attribute.getLocation(); if(0<=loc) { bindAttribLocation(gl, loc, name); if(isVertexAttribArrayEnabled(name)) { // enable attrib, VBO and pass location/data gl.glEnableVertexAttribArray(loc); } if( attribute.isVBO() ) { gl.glBindBuffer(GL.GL_ARRAY_BUFFER, attribute.getVBOName()); gl.glVertexAttribPointer(attribute); gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0); } else { gl.glVertexAttribPointer(attribute); } } }
public Optional<String> verify() { GL2ES2 gl = gc.getGL().getGL2ES2(); gl.glValidateProgram(programObject); int[] error = new int[]{-1}; gl.glGetProgramiv(programObject, GL2GL3.GL_LINK_STATUS, error, 0); if (error[0] == GL.GL_FALSE) { int[] len = new int[]{512}; byte[] errormessage = new byte[512]; gl.glGetProgramInfoLog(programObject, 512, len, 0, errormessage, 0); return Optional.of(new String(errormessage, 0, len[0])); } return Optional.absent(); }
System.err.println("ShaderState: glVertexAttribPointer: "+data); gl.glVertexAttribPointer(data); return true;
public void prepare() { buffer.bind(); GL2ES2 gl = gc.getGL().getGL2ES2(); for (AttributConfig ac : configs) { gl.glEnableVertexAttribArray(ac.index); gl.glVertexAttribPointer(ac.index, ac.size, ac.glconst, false, ac.stride, ac.offset); } buffer.disable(); }
@Override public final void enableState(final GL gl, final boolean enable, final Object ext) { final GL2ES2 glsl = gl.getGL2ES2(); if( null != ext ) { final ShaderState st = (ShaderState)ext; if(enable) { st.enableVertexAttribArray(glsl, ad); } else { st.disableVertexAttribArray(glsl, ad); } } else { final int location = ad.getLocation(); if( 0 <= location ) { if(enable) { glsl.glEnableVertexAttribArray(location); } else { glsl.glDisableVertexAttribArray(location); } } } } }
public Optional<ShaderProgramm> toShader(GraphicContext gc) { GL2ES2 gl = gc.getGL().getGL2(); int programObject = gl.glCreateProgram(); ShaderProgramm prog = new ShaderProgramm(gc, programObject); gl.glProgramBinary(prog.getPObject(), format, data, data.limit()); if (GL2ES2.GL_INVALID_ENUM == gl.glGetError()) { return Optional.absent(); } Optional<String> error = prog.verify(); if (error.isPresent()) { return Optional.of(prog); } else { return Optional.absent(); } }
public static CompiledShader fromShader(GraphicContext gc, ShaderProgramm prog) { GL2ES2 gl = gc.getGL().getGL2ES2(); int pid = prog.getPObject(); IntBuffer length = Buffers.newDirectIntBuffer(1); gl.glGetProgramiv(pid, GL2.GL_PROGRAM_BINARY_RETRIEVABLE_HINT, length); if (length.get(0) == GL.GL_FALSE) { throw new RuntimeException("The binary of this shader program is not retrievable(set PROGRAM_BINARY_RETRIEVABLE_HINT to true)!"); } gl.glGetProgramiv(pid, GL2.GL_PROGRAM_BINARY_LENGTH, length); IntBuffer format = Buffers.newDirectIntBuffer(1); ByteBuffer data = Buffers.newDirectByteBuffer(length.get(0)); gl.glGetProgramBinary(pid, length.get(0), length, format, data); if (gl.glGetError() == GL.GL_INVALID_OPERATION) { //invalid state can occure when program isn't linked or the buffer is to smale throw new RuntimeException("The given shader program is not in a valid state(not linked correctly)"); } return new CompiledShader(format.get(0), data); }
/** * Disables all vertex attribute arrays. * * Their enabled stated will be removed from this state only * if 'removeFromState' is true. * * This method purpose is more for debugging. * * @see #glEnableVertexAttribArray * @see #glDisableVertexAttribArray * @see #glVertexAttribPointer * @see #getVertexAttribPointer * @see #glReleaseAllVertexAttributes * @see #glResetAllVertexAttributes * @see #glResetAllVertexAttributes * @see ShaderProgram#glReplaceShader */ public void disableAllVertexAttributeArrays(final GL2ES2 gl, final boolean removeFromState) { for(final Iterator<String> iter = activedAttribEnabledMap.keySet().iterator(); iter.hasNext(); ) { final String name = iter.next(); if(removeFromState) { activedAttribEnabledMap.remove(name); } final int index = getAttribLocation(gl, name); if(0<=index) { gl.glDisableVertexAttribArray(index); } } }
@Override public final int setLocation(final GL2ES2 gl, final int program) { location = gl.glGetAttribLocation(program, name); return location; }
/** * Retrieves the location of the shader uniform from the linked shader program. * <p> * No validation is performed within the implementation. * </p> * @param gl * @param program * @return ≥0 denotes a valid uniform location as found and used in the given shader program. * <0 denotes an invalid location, i.e. not found or used in the given shader program. */ public int setLocation(final GL2ES2 gl, final int program) { location = gl.glGetUniformLocation(program, name); return location; }
private boolean enableVertexAttribArray(final GL2ES2 gl, final String name, int location) { activedAttribEnabledMap.put(name, Boolean.TRUE); if(0>location) { location = getAttribLocation(gl, name); if(0>location) { if(verbose) { System.err.println("ShaderState: glEnableVertexAttribArray failed, no index for: "+name); if(DEBUG) { Thread.dumpStack(); } } return false; } } if(DEBUG) { System.err.println("ShaderState: glEnableVertexAttribArray: "+name+", loc: "+location); } gl.glEnableVertexAttribArray(location); return true; }
/** * Creates the empty GL program object using {@link GL2ES2#glCreateProgram()}, * if not already created. * * @param gl * @return true if shader program is valid, i.e. not zero */ public synchronized final boolean init(final GL2ES2 gl) { if( 0 == shaderProgram ) { shaderProgram = gl.glCreateProgram(); } return 0 != shaderProgram; }