/** * Bind the {@link GLUniform} lifecycle to this ShaderState. * * <p>If a uniform location is cached it is promoted to the {@link GLUniformData} instance. * * <p>The attribute will be destroyed with {@link #destroy(GL2ES2)} and it's location will be * reset when switching shader with {@link #attachShaderProgram(GL2ES2, ShaderProgram)}. * * <p>The data will not be transfered to the GPU, use {@link #uniform(GL2ES2, GLUniformData)} * additionally. * * @param uniform the {@link GLUniformData} which lifecycle shall be managed * @see #getUniform(String) */ public void ownUniform(GLUniformData uniform) { final int location = getCachedUniformLocation(uniform.getName()); if (0 <= location) { uniform.setLocation(location); } activeUniformDataMap.put(uniform.getName(), uniform); managedUniforms.add(uniform); }
/** * Validates and returns the location of a shader uniform.<br> * Uses either the cached value {@link #getCachedUniformLocation(String)} if valid, or the GLSL * queried via {@link GL2ES2#glGetUniformLocation(int, String)}.<br> * The location will be cached and set in the {@link GLUniformData} object. * * <p>The current shader program ({@link #attachShaderProgram(GL2ES2, ShaderProgram)}) must be in * use ({@link #useProgram(GL2ES2, boolean) }) ! * * @return -1 if there is no such attribute available, otherwise >= 0 * @throws GLException is the program is not linked * @see #glGetUniformLocation * @see javax.media.opengl.GL2ES2#glGetUniformLocation * @see #getUniformLocation * @see ShaderProgram#glReplaceShader */ public int getUniformLocation(GL2ES2 gl, GLUniformData data) { if (!shaderProgram.inUse()) throw new GLException("Program is not in use"); final String name = data.getName(); int location = getCachedUniformLocation(name); if (0 <= location) { data.setLocation(location); } else { if (!shaderProgram.linked()) throw new GLException("Program is not linked"); location = data.setLocation(gl, shaderProgram.program()); if (0 <= location) { Integer idx = new Integer(location); activeUniformLocationMap.put(name, idx); } else if (verbose) { System.err.println( "ShaderState: glUniform failed, no location for: " + name + ", index: " + location); if (DEBUG) { Thread.dumpStack(); } } } activeUniformDataMap.put(name, data); return location; }