[Committed] Fix for Exception when setting a uniform variable that doesn't exist

Hi guys,



as mentioned in that thread:http://www.jmonkeyengine.com/forum/index.php?topic=14412.0



The GLSL compiler optimizes stuff that isn’t used away, which results in an exception when you try to set a removed uniform in your application code. Now jme will just ignore setting this.



Also i formatted the three source files that have changes according to the standard Java CodeStyle Conventions (changed for 120 character lines instead of 80). Before the source was like … not … formatted  :|



I hope that is ok, here’s the patch:

http://pastebin.com/EmxgMW9j

Nice to see that changes are finally done, which should have been done years ago.  :roll:



Another thing regarding this, is that you should set the uniform id to -2 if it is not found, and should not again check for uniform location if the id is -2. This avoids the slow and unnecessary glGetUniformLocation call each time uniforms are applied.



Hints:

http://code.google.com/p/vlengine/source/browse/trunk/vle_cleanup/src/com/vlengine/scene/state/shader/ShaderVariableLocation.java#74

http://code.google.com/p/vlengine/source/browse/trunk/vle_cleanup/src/com/vlengine/scene/state/lwjgl/LWJGLShaderParameters.java#102

Perfect there is progress in the shader-section. I didn't use it (yet) so I can't say much about it but I'm pleased to know that another bug is killed before I could stumble into it :smiley:

vear said:

Another thing regarding this, is that you should set the uniform id to -2 if it is not found, and should not again check for uniform location if the id is -2. This avoids the slow and unnecessary glGetUniformLocation call each time uniforms are applied.


Thanks for the suggestion, it was already in there  ;)

Many thanks. This feature was really missing…



Now I patched the LWJGLSharderUtil again in order to fix this http://www.jmonkeyengine.com/forum/index.php?topic=13908.0



here's the patch



Index: LWJGLShaderUtil.java
===================================================================
--- LWJGLShaderUtil.java   (revision 5670)
+++ LWJGLShaderUtil.java   (working copy)
@@ -48,6 +48,8 @@
 public class LWJGLShaderUtil {
 
     private static final Logger logger = Logger.getLogger(LWJGLShaderUtil.class.getName());
+   
+    private static ByteBuffer   nameBuf   = BufferUtils.createByteBuffer(2048);
 
     /**
      * Updates a uniform shadervariable.
@@ -95,9 +97,9 @@
      */
     public static void updateUniformLocation(final ShaderVariable variable, final int programID) {
         if (variable.variableID == ShaderVariable.UNINITIALIZED) {
-            final ByteBuffer nameBuf = BufferUtils.createByteBuffer(variable.name.getBytes().length + 1);
             nameBuf.clear();
             nameBuf.put(variable.name.getBytes());
+            nameBuf.put((byte)0);
             nameBuf.rewind();
 
             variable.variableID = ARBShaderObjects.glGetUniformLocationARB(programID, nameBuf);
@@ -203,9 +205,9 @@
      */
     public static void updateAttributeLocation(final ShaderVariable variable, final int programID) {
         if (variable.variableID == ShaderVariable.UNINITIALIZED) {
-            final ByteBuffer nameBuf = BufferUtils.createByteBuffer(variable.name.getBytes().length + 1);
             nameBuf.clear();
             nameBuf.put(variable.name.getBytes());
+            nameBuf.put((byte)0);
             nameBuf.rewind();
 
             variable.variableID = ARBVertexShader.glGetAttribLocationARB(programID, nameBuf);



I hope that someone will commit this.

Cheers,

Mik
MikOfClassX said:

I hope that someone will commit this.


I did. There are more fixes in the Shader department to come. We are working on a huge project and use jme2. So for all of you that use jme2 out there, we won't let jme2 die and it will continue to improve ;)
We are working on a huge project and use jme2.


We too..  :wink: