final RenderContext context = ContextManager.getCurrentContext();
final ContextCapabilities caps = context.getCapabilities();
if (caps.isGLSLSupported()) {
// Ask for the current state record
final ShaderObjectsStateRecord record = (ShaderObjectsStateRecord) context
.getStateRecord(StateType.GLSLShader);
context.setCurrentState(StateType.GLSLShader, state);
if (state.isEnabled()) {
if (state._needSendShader) {
sendToGL(state, caps);
}
if (state._shaderDataLogic != null) {
state._shaderDataLogic.applyData(state, state._mesh, renderer);
}
}
if (!record.isValid() || record.getReference() != state || state.needsRefresh()) {
record.setReference(state);
if (state.isEnabled() && state._programID != -1) {
// clear any previously existing attributes
clearEnabledAttributes(record, gl);
// set our current shader
JoglShaderUtil.useShaderProgram(state._programID, record);
final List<ShaderVariable> attribs = state.getShaderAttributes();
for (int i = attribs.size(); --i >= 0;) {
final ShaderVariable shaderVariable = attribs.get(i);
if (shaderVariable.needsRefresh) {
JoglShaderUtil.updateAttributeLocation(shaderVariable, state._programID);
shaderVariable.needsRefresh = false;
}
JoglShaderUtil.updateShaderAttribute(renderer, shaderVariable, state.isUseAttributeVBO());
}
final List<ShaderVariable> uniforms = state.getShaderUniforms();
for (int i = uniforms.size(); --i >= 0;) {
final ShaderVariable shaderVariable = uniforms.get(i);
if (shaderVariable.needsRefresh) {
JoglShaderUtil.updateUniformLocation(shaderVariable, state._programID);
JoglShaderUtil.updateShaderUniform(shaderVariable);
shaderVariable.needsRefresh = false;
}
}
} else {
JoglShaderUtil.useShaderProgram(0, record);
clearEnabledAttributes(record, gl);
}
}
if (!record.isValid()) {
record.validate();
}
}
}