From e6d829fac4d0744e59deb22b634b1910edad6f5b Mon Sep 17 00:00:00 2001 From: Kirill Vainer Date: Fri, 21 Aug 2015 20:34:34 -0400 Subject: [PATCH] GLRenderer: added fast uniforms - still need to fix Uniform.clear() --- .../com/jme3/renderer/opengl/GLRenderer.java | 14 ++-- .../main/java/com/jme3/shader/Uniform.java | 64 +++++++++++++------ 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java index 636733583..20c75d47f 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java @@ -964,12 +964,12 @@ public final class GLRenderer implements Renderer { gl.glUniform1i(loc, b.booleanValue() ? GL.GL_TRUE : GL.GL_FALSE); break; case Matrix3: - fb = (FloatBuffer) uniform.getValue(); + fb = uniform.getMultiData(); assert fb.remaining() == 9; gl.glUniformMatrix3(loc, false, fb); break; case Matrix4: - fb = (FloatBuffer) uniform.getValue(); + fb = uniform.getMultiData(); assert fb.remaining() == 16; gl.glUniformMatrix4(loc, false, fb); break; @@ -978,23 +978,23 @@ public final class GLRenderer implements Renderer { gl.glUniform1(loc, ib); break; case FloatArray: - fb = (FloatBuffer) uniform.getValue(); + fb = uniform.getMultiData(); gl.glUniform1(loc, fb); break; case Vector2Array: - fb = (FloatBuffer) uniform.getValue(); + fb = uniform.getMultiData(); gl.glUniform2(loc, fb); break; case Vector3Array: - fb = (FloatBuffer) uniform.getValue(); + fb = uniform.getMultiData(); gl.glUniform3(loc, fb); break; case Vector4Array: - fb = (FloatBuffer) uniform.getValue(); + fb = uniform.getMultiData(); gl.glUniform4(loc, fb); break; case Matrix4Array: - fb = (FloatBuffer) uniform.getValue(); + fb = uniform.getMultiData(); gl.glUniformMatrix4(loc, false, fb); break; case Int: diff --git a/jme3-core/src/main/java/com/jme3/shader/Uniform.java b/jme3-core/src/main/java/com/jme3/shader/Uniform.java index 5d5fd2fe3..521b10d38 100644 --- a/jme3-core/src/main/java/com/jme3/shader/Uniform.java +++ b/jme3-core/src/main/java/com/jme3/shader/Uniform.java @@ -102,6 +102,10 @@ public class Uniform extends ShaderVariable { public Object getValue(){ return value; } + + public FloatBuffer getMultiData() { + return multiData; + } public boolean isSetByCurrentMaterial() { return setByCurrentMaterial; @@ -111,21 +115,6 @@ public class Uniform extends ShaderVariable { setByCurrentMaterial = false; } - private static void setVector4(Vector4f vec, Object value) { - if (value instanceof ColorRGBA) { - ColorRGBA color = (ColorRGBA) value; - vec.set(color.r, color.g, color.b, color.a); - } else if (value instanceof Quaternion) { - Quaternion quat = (Quaternion) value; - vec.set(quat.getX(), quat.getY(), quat.getZ(), quat.getW()); - } else if (value instanceof Vector4f) { - Vector4f vec4 = (Vector4f) value; - vec.set(vec4); - } else{ - throw new IllegalArgumentException(); - } - } - public void clearValue(){ updateNeeded = true; @@ -189,20 +178,36 @@ public class Uniform extends ShaderVariable { switch (type){ case Matrix3: + if (value.equals(this.value)) { + return; + } Matrix3f m3 = (Matrix3f) value; if (multiData == null) { multiData = BufferUtils.createFloatBuffer(9); } m3.fillFloatBuffer(multiData, true); multiData.clear(); + if (this.value == null) { + this.value = new Matrix3f(m3); + } else { + ((Matrix3f)this.value).set(m3); + } break; case Matrix4: + if (value.equals(this.value)) { + return; + } Matrix4f m4 = (Matrix4f) value; if (multiData == null) { multiData = BufferUtils.createFloatBuffer(16); } m4.fillFloatBuffer(multiData, true); multiData.clear(); + if (this.value == null) { + this.value = new Matrix4f(m4); + } else { + ((Matrix4f)this.value).copy(m4); + } break; case IntArray: int[] ia = (int[]) value; @@ -283,11 +288,32 @@ public class Uniform extends ShaderVariable { } multiData.clear(); break; + case Vector4: + if (value.equals(this.value)) { + return; + } + if (value instanceof ColorRGBA) { + if (this.value == null) { + this.value = new ColorRGBA(); + } + ((ColorRGBA) this.value).set((ColorRGBA) value); + } else if (value instanceof Vector4f) { + if (this.value == null) { + this.value = new Vector4f(); + } + ((Vector4f) this.value).set((Vector4f) value); + } else { + if (this.value == null) { + this.value = new Quaternion(); + } + ((Quaternion) this.value).set((Quaternion) value); + } + break; // Only use check if equals optimization for primitive values case Int: case Float: case Boolean: - if (this.value != null && this.value.equals(value)) { + if (value.equals(this.value)) { return; } this.value = value; @@ -297,9 +323,9 @@ public class Uniform extends ShaderVariable { break; } - if (multiData != null) { - this.value = multiData; - } +// if (multiData != null) { +// this.value = multiData; +// } varType = type; updateNeeded = true;