From b45fff547049103287ba4a6b0f7b635013b81298 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 12 Jun 2009 13:01:31 +0000 Subject: GLSL ShaderUtil: Use TLS to make it thread safe git-svn-id: file:///usr/local/projects/SUN/JOGL/git-svn/svn-server-sync/jogl/branches/JOGL_2_SANDBOX@1934 232f8b59-042b-4e1e-8c03-345bb8c30851 --- .../com/sun/opengl/util/glsl/ShaderState.java | 21 +++++++++++++-------- .../com/sun/opengl/util/glsl/ShaderUtil.java | 6 ++---- 2 files changed, 15 insertions(+), 12 deletions(-) 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 5acbff241..bab9ef4f3 100644 --- a/src/jogl/classes/com/sun/opengl/util/glsl/ShaderState.java +++ b/src/jogl/classes/com/sun/opengl/util/glsl/ShaderState.java @@ -20,16 +20,23 @@ public class ShaderState { public void setVerbose(boolean v) { verbose=v; } /** - * Fetches the current shader state + * Fetches the current shader state from the thread local storage (TLS) * * @see javax.media.opengl.glsl.ShaderState#glUseProgram(GL2ES2, boolean) * @see javax.media.opengl.glsl.ShaderState#getCurrent() */ - public static synchronized ShaderState getCurrent() { return currentShaderState; } + public static synchronized ShaderState getCurrent() { + GLContext current = GLContext.getCurrent(); + if(null==current) { + throw new GLException("No context is current on this thread"); + } + return (ShaderState) current.getAttachedObject(ShaderState.class.getName()); + } /** - * Turns the shader program on - * and set this ShaderState to current if on equals true + * Turns the shader program on or off.
+ * Puts this ShaderState to to the thread local storage (TLS), + * if on is true. * * @see javax.media.opengl.glsl.ShaderState#glUseProgram(GL2ES2, boolean) * @see javax.media.opengl.glsl.ShaderState#getCurrent() @@ -41,9 +48,8 @@ public class ShaderState { } else { throw new GLException("No program is attached"); } - if(currentShaderState!=this) { - currentShaderState = this; - } + // update the current ShaderState to the TLS .. + gl.getContext().putAttachedObject(ShaderState.class.getName(), this); } else if(null!=shaderProgram) { shaderProgram.glUseProgram(gl, false); } @@ -637,7 +643,6 @@ public class ShaderState { protected HashMap vertexAttribMap2Data = new HashMap(); protected HashMap uniformMap2Idx = new HashMap(); protected HashMap uniformMap2Data = new HashMap(); - protected static ShaderState currentShaderState = null; } 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 8bac24b86..d1bb8b32b 100644 --- a/src/jogl/classes/com/sun/opengl/util/glsl/ShaderUtil.java +++ b/src/jogl/classes/com/sun/opengl/util/glsl/ShaderUtil.java @@ -388,12 +388,10 @@ public class ShaderUtil { private static Impl getImpl(GL _gl) { GL2ES2 gl = _gl.getGL2ES2(); GLContext context = gl.getContext(); - // Argument could be anything suitably unique - String name = "com.sun.opengl.util.glsl.ShaderUtil.GL2ES2Impl"; - Impl impl = (Impl) context.getAttachedObject(name); + Impl impl = (Impl) context.getAttachedObject(ShaderUtil.class.getName()); if (impl == null) { impl = new GL2ES2Impl(); - context.putAttachedObject(name, impl); + context.putAttachedObject(ShaderUtil.class.getName(), impl); } return impl; } -- cgit v1.2.3