From 2d57665dfd91417b5bbdb59c2b5e3a6077fcf814 Mon Sep 17 00:00:00 2001 From: "rem..om" Date: Sun, 4 Mar 2012 12:54:02 +0000 Subject: [PATCH] JME nifty's render device now uses 2 separate shaders : one for images and texts and one for quads, this way there is no more branching in the frag shader greatly improving speed (specially on android) git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9229 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../Common/MatDefs/Nifty/NiftyQuad.frag | 6 +++ .../Common/MatDefs/Nifty/NiftyQuad.j3md | 19 ++++++++ .../Common/MatDefs/Nifty/NiftyQuad.vert | 15 +++++++ .../Nifty/{Nifty.frag => NiftyTex.frag} | 5 +-- .../Nifty/{Nifty.j3md => NiftyTex.j3md} | 5 +-- .../Nifty/{Nifty.vert => NiftyTex.vert} | 3 +- .../com/jme3/niftygui/RenderDeviceJme.java | 43 +++++++++---------- 7 files changed, 66 insertions(+), 30 deletions(-) create mode 100644 engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.frag create mode 100644 engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.j3md create mode 100644 engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.vert rename engine/src/niftygui/Common/MatDefs/Nifty/{Nifty.frag => NiftyTex.frag} (56%) rename engine/src/niftygui/Common/MatDefs/Nifty/{Nifty.j3md => NiftyTex.j3md} (58%) rename engine/src/niftygui/Common/MatDefs/Nifty/{Nifty.vert => NiftyTex.vert} (81%) diff --git a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.frag b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.frag new file mode 100644 index 000000000..1f0a645e5 --- /dev/null +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.frag @@ -0,0 +1,6 @@ +varying vec4 color; + +void main() { + gl_FragColor = color; +} + diff --git a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.j3md b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.j3md new file mode 100644 index 000000000..d0213e913 --- /dev/null +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.j3md @@ -0,0 +1,19 @@ +MaterialDef Default GUI { + + MaterialParameters { + Vector4 Color (Color) + } + + Technique { + VertexShader GLSL100: Common/MatDefs/Nifty/NiftyQuad.vert + FragmentShader GLSL100: Common/MatDefs/Nifty/NiftyQuad.frag + + WorldParameters { + WorldViewProjectionMatrix + } + } + + Technique FixedFunc { + } + +} \ No newline at end of file diff --git a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.vert b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.vert new file mode 100644 index 000000000..b79cb1728 --- /dev/null +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.vert @@ -0,0 +1,15 @@ +uniform mat4 g_WorldViewProjectionMatrix; +uniform vec4 m_Color; + +attribute vec4 inPosition; +attribute vec4 inColor; + +varying vec2 texCoord; +varying vec4 color; + +void main() { + vec2 pos = (g_WorldViewProjectionMatrix * inPosition).xy; + gl_Position = vec4(pos, 0.0, 1.0); + + color = inColor * m_Color; +} \ No newline at end of file diff --git a/engine/src/niftygui/Common/MatDefs/Nifty/Nifty.frag b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.frag similarity index 56% rename from engine/src/niftygui/Common/MatDefs/Nifty/Nifty.frag rename to engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.frag index 5e77548d1..cda981a23 100644 --- a/engine/src/niftygui/Common/MatDefs/Nifty/Nifty.frag +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.frag @@ -1,4 +1,3 @@ -uniform bool m_UseTex; uniform sampler2D m_Texture; uniform vec4 m_Color; @@ -7,7 +6,7 @@ varying vec4 color; void main() { vec4 texVal = texture2D(m_Texture, texCoord); - texVal = m_UseTex ? texVal : vec4(1.0); - gl_FragColor = texVal * color * m_Color; + // texVal = m_UseTex ? texVal : vec4(1.0); + gl_FragColor = texVal * color ; } diff --git a/engine/src/niftygui/Common/MatDefs/Nifty/Nifty.j3md b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.j3md similarity index 58% rename from engine/src/niftygui/Common/MatDefs/Nifty/Nifty.j3md rename to engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.j3md index 9ba39b1ed..07f5e928c 100644 --- a/engine/src/niftygui/Common/MatDefs/Nifty/Nifty.j3md +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.j3md @@ -2,13 +2,12 @@ MaterialDef Default GUI { MaterialParameters { Texture2D Texture - Boolean UseTex Vector4 Color (Color) } Technique { - VertexShader GLSL100: Common/MatDefs/Nifty/Nifty.vert - FragmentShader GLSL100: Common/MatDefs/Nifty/Nifty.frag + VertexShader GLSL100: Common/MatDefs/Nifty/NiftyTex.vert + FragmentShader GLSL100: Common/MatDefs/Nifty/NiftyTex.frag WorldParameters { WorldViewProjectionMatrix diff --git a/engine/src/niftygui/Common/MatDefs/Nifty/Nifty.vert b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.vert similarity index 81% rename from engine/src/niftygui/Common/MatDefs/Nifty/Nifty.vert rename to engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.vert index 67c864d96..6203a2896 100644 --- a/engine/src/niftygui/Common/MatDefs/Nifty/Nifty.vert +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.vert @@ -1,4 +1,5 @@ uniform mat4 g_WorldViewProjectionMatrix; +uniform vec4 m_Color; attribute vec4 inPosition; attribute vec4 inColor; @@ -12,5 +13,5 @@ void main() { gl_Position = vec4(pos, 0.0, 1.0); texCoord = inTexCoord; - color = inColor; + color = inColor * m_Color; } \ No newline at end of file diff --git a/engine/src/niftygui/com/jme3/niftygui/RenderDeviceJme.java b/engine/src/niftygui/com/jme3/niftygui/RenderDeviceJme.java index 8977ed64c..858b95878 100644 --- a/engine/src/niftygui/com/jme3/niftygui/RenderDeviceJme.java +++ b/engine/src/niftygui/com/jme3/niftygui/RenderDeviceJme.java @@ -71,6 +71,7 @@ public class RenderDeviceJme implements RenderDevice { private final Quad quad = new Quad(1, -1, true); private final Geometry quadGeom = new Geometry("nifty-quad", quad); private final Material niftyMat; + private final Material niftyQuadMat; private boolean clipWasSet = false; private BlendMode blendMode = null; @@ -93,8 +94,10 @@ public class RenderDeviceJme implements RenderDevice { quadModTC.setUsage(Usage.Stream); - niftyMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/Nifty.j3md"); + niftyMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/NiftyTex.j3md"); niftyMat.getAdditionalRenderState().setDepthTest(false); + niftyQuadMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/NiftyQuad.j3md"); + niftyQuadMat.getAdditionalRenderState().setDepthTest(false); } public void setResourceLoader(NiftyResourceLoader niftyResourceLoader) { @@ -210,8 +213,7 @@ public class RenderDeviceJme implements RenderDevice { } @Override - public void renderFont(RenderFont font, String str, int x, int y, Color color, float sizeX, float sizeY){ - //TODO find out what the f1 param is for + public void renderFont(RenderFont font, String str, int x, int y, Color color, float sizeX, float sizeY){ if (str.length() == 0) return; @@ -229,17 +231,18 @@ public class RenderDeviceJme implements RenderDevice { } textCacheCurrentFrame.put(key, text); - niftyMat.setColor("Color", convertColor(color, tempColor)); - niftyMat.setBoolean("UseTex", true); - niftyMat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha); + niftyMat.setColor("Color", convertColor(color, tempColor)); + niftyMat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha); // niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); text.setMaterial(niftyMat); float width = text.getLineWidth(); - float height = text.getLineHeight(); + float height = text.getLineHeight(); - float x0 = x + 0.5f * width * (1f - sizeX); - float y0 = y + 0.5f * height * (1f - sizeY); +// System.out.println("----------------------------------------------------"); +// System.out.println("POS "+x+" "+width+" "+sizeX); + float x0 = x + 0.5f * width * (1.0f - sizeX); + float y0 = y + 0.5f * height * (1.0f - sizeY); tempMat.loadIdentity(); tempMat.setTranslation(x0, getHeight() - y0, 0); @@ -260,8 +263,7 @@ public class RenderDeviceJme implements RenderDevice { niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); niftyMat.setColor("Color", ColorRGBA.White); - niftyMat.setTexture("Texture", texture); - niftyMat.setBoolean("UseTex", true); + niftyMat.setTexture("Texture", texture); setColor(color); float imageWidth = jmeImage.getWidth(); @@ -307,8 +309,7 @@ public class RenderDeviceJme implements RenderDevice { niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); niftyMat.setColor("Color", ColorRGBA.White); - niftyMat.setTexture("Texture", jmeImage.getTexture()); - niftyMat.setBoolean("UseTex", true); + niftyMat.setTexture("Texture", jmeImage.getTexture()); setColor(color); quad.clearBuffer(Type.TexCoord); @@ -328,10 +329,8 @@ public class RenderDeviceJme implements RenderDevice { } public void renderQuad(int x, int y, int width, int height, Color color){ - niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); - niftyMat.setColor("Color", ColorRGBA.White); - niftyMat.clearParam("Texture"); - niftyMat.setBoolean("UseTex", false); + niftyQuadMat.getAdditionalRenderState().setBlendMode(convertBlend()); + niftyQuadMat.setColor("Color", ColorRGBA.White); setColor(color); tempMat.loadIdentity(); @@ -339,7 +338,7 @@ public class RenderDeviceJme implements RenderDevice { tempMat.setScale(width, height, 0); rm.setWorldMatrix(tempMat); - niftyMat.render(quadGeom, rm); + niftyQuadMat.render(quadGeom, rm); // System.out.println("renderQuad (Solid)"); } @@ -359,17 +358,15 @@ public class RenderDeviceJme implements RenderDevice { buf.flip(); quadColor.updateData(buf); - niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); - niftyMat.setColor("Color", ColorRGBA.White); - niftyMat.clearParam("Texture"); - niftyMat.setBoolean("UseTex", false); + niftyQuadMat.getAdditionalRenderState().setBlendMode(convertBlend()); + niftyQuadMat.setColor("Color", ColorRGBA.White); tempMat.loadIdentity(); tempMat.setTranslation(x, getHeight() - y, 0); tempMat.setScale(width, height, 0); rm.setWorldMatrix(tempMat); - niftyMat.render(quadGeom, rm); + niftyQuadMat.render(quadGeom, rm); // // System.out.println("renderQuad (Grad)"); }