From 654c44abdda9b8aa6bf02fdef7f2186fdd4b4543 Mon Sep 17 00:00:00 2001 From: "rem..om" Date: Sun, 4 Mar 2012 14:08:15 +0000 Subject: [PATCH] Jme nifty render device optimization : - Banished color buffer updating at render time (except for gradient quads that uses vertex color) - fully transparent quads are not rendered anymore (that avoid rendering transparent layers allowing complex layout for free). git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9232 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../Common/MatDefs/Nifty/NiftyQuad.frag | 4 +- .../Common/MatDefs/Nifty/NiftyQuad.vert | 6 -- .../Common/MatDefs/Nifty/NiftyQuadGrad.frag | 6 ++ .../Common/MatDefs/Nifty/NiftyQuadGrad.j3md | 18 +++++ .../Common/MatDefs/Nifty/NiftyQuadGrad.vert | 14 ++++ .../Common/MatDefs/Nifty/NiftyTex.frag | 6 +- .../Common/MatDefs/Nifty/NiftyTex.vert | 6 +- .../com/jme3/niftygui/RenderDeviceJme.java | 73 ++++++++++--------- 8 files changed, 83 insertions(+), 50 deletions(-) create mode 100644 engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuadGrad.frag create mode 100644 engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuadGrad.j3md create mode 100644 engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuadGrad.vert diff --git a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.frag b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.frag index 1f0a645e5..31f49d6e5 100644 --- a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.frag +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.frag @@ -1,6 +1,6 @@ -varying vec4 color; +uniform vec4 m_Color; void main() { - gl_FragColor = color; + gl_FragColor = m_Color; } diff --git a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.vert b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.vert index b79cb1728..1eb1616c2 100644 --- a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.vert +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.vert @@ -1,15 +1,9 @@ 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/NiftyQuadGrad.frag b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuadGrad.frag new file mode 100644 index 000000000..1f0a645e5 --- /dev/null +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuadGrad.frag @@ -0,0 +1,6 @@ +varying vec4 color; + +void main() { + gl_FragColor = color; +} + diff --git a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuadGrad.j3md b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuadGrad.j3md new file mode 100644 index 000000000..4b3b22d48 --- /dev/null +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuadGrad.j3md @@ -0,0 +1,18 @@ +MaterialDef Default GUI { + + MaterialParameters { + } + + Technique { + VertexShader GLSL100: Common/MatDefs/Nifty/NiftyQuadGrad.vert + FragmentShader GLSL100: Common/MatDefs/Nifty/NiftyQuadGrad.frag + + WorldParameters { + WorldViewProjectionMatrix + } + } + + Technique FixedFunc { + } + +} \ No newline at end of file diff --git a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuadGrad.vert b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuadGrad.vert new file mode 100644 index 000000000..0ecaee4f2 --- /dev/null +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuadGrad.vert @@ -0,0 +1,14 @@ +uniform mat4 g_WorldViewProjectionMatrix; + +attribute vec4 inPosition; +attribute vec4 inColor; +attribute vec4 inIndex; + +varying vec4 color; + +void main() { + vec2 pos = (g_WorldViewProjectionMatrix * inPosition).xy; + gl_Position = vec4(pos, 0.0, 1.0); + + color = inIndex; +} \ No newline at end of file diff --git a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.frag b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.frag index cda981a23..b2b4b95d6 100644 --- a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.frag +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.frag @@ -1,12 +1,10 @@ uniform sampler2D m_Texture; -uniform vec4 m_Color; +uniform vec4 m_Color; varying vec2 texCoord; -varying vec4 color; void main() { vec4 texVal = texture2D(m_Texture, texCoord); - // texVal = m_UseTex ? texVal : vec4(1.0); - gl_FragColor = texVal * color ; + gl_FragColor = texVal * m_Color ; } diff --git a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.vert b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.vert index 6203a2896..c5c3dc1b3 100644 --- a/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.vert +++ b/engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.vert @@ -1,17 +1,13 @@ uniform mat4 g_WorldViewProjectionMatrix; -uniform vec4 m_Color; attribute vec4 inPosition; -attribute vec4 inColor; attribute vec2 inTexCoord; varying vec2 texCoord; -varying vec4 color; void main() { vec2 pos = (g_WorldViewProjectionMatrix * inPosition).xy; gl_Position = vec4(pos, 0.0, 1.0); - texCoord = inTexCoord; - color = inColor * m_Color; + texCoord = inTexCoord; } \ 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 ce18203b3..1f36a9775 100644 --- a/engine/src/niftygui/com/jme3/niftygui/RenderDeviceJme.java +++ b/engine/src/niftygui/com/jme3/niftygui/RenderDeviceJme.java @@ -69,6 +69,7 @@ public class RenderDeviceJme implements RenderDevice { private final Geometry quadGeom = new Geometry("nifty-quad", quad); private final Material niftyMat; private final Material niftyQuadMat; + private final Material niftyQuadGradMat; private boolean clipWasSet = false; private BlendMode blendMode = null; private VertexBuffer quadDefaultTC = quad.getBuffer(Type.TexCoord); @@ -88,10 +89,17 @@ public class RenderDeviceJme implements RenderDevice { quadModTC.setUsage(Usage.Stream); + //Color + texture color material for text and images niftyMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/NiftyTex.j3md"); niftyMat.getAdditionalRenderState().setDepthTest(false); + //Color material for uniform colored quads niftyQuadMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/NiftyQuad.j3md"); - niftyQuadMat.getAdditionalRenderState().setDepthTest(false); + niftyQuadMat.getAdditionalRenderState().setDepthTest(false); + + //vertex color only for gradient quads (although i didn't find a way in nifty to make a gradient using vertex color) + niftyQuadGradMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/NiftyQuadGrad.j3md"); + niftyQuadGradMat.getAdditionalRenderState().setDepthTest(false); + } public void setResourceLoader(NiftyResourceLoader niftyResourceLoader) { @@ -105,7 +113,7 @@ public class RenderDeviceJme implements RenderDevice { // TODO: Cursor support public MouseCursor createMouseCursor(String str, int x, int y) { return new MouseCursor() { - + public void dispose() { } }; @@ -179,19 +187,19 @@ public class RenderDeviceJme implements RenderDevice { return outColor.set(inColor.getRed(), inColor.getGreen(), inColor.getBlue(), inColor.getAlpha()); } - private void setColor(Color color) { - ByteBuffer buf = (ByteBuffer) quadColor.getData(); - buf.rewind(); - - int color2 = convertColor(color); - buf.putInt(color2); - buf.putInt(color2); - buf.putInt(color2); - buf.putInt(color2); - - buf.flip(); - quadColor.updateData(buf); - } +// private void setColor(Color color) { +// ByteBuffer buf = (ByteBuffer) quadColor.getData(); +// buf.rewind(); +// +// int color2 = convertColor(color); +// buf.putInt(color2); +// buf.putInt(color2); +// buf.putInt(color2); +// buf.putInt(color2); +// +// buf.flip(); +// quadColor.updateData(buf); +// } /** * @@ -252,9 +260,9 @@ public class RenderDeviceJme implements RenderDevice { Texture2D texture = jmeImage.getTexture(); niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); - niftyMat.setColor("Color", ColorRGBA.White); + niftyMat.setColor("Color", convertColor(color, tempColor)); niftyMat.setTexture("Texture", texture); - setColor(color); + //setColor(color); float imageWidth = jmeImage.getWidth(); float imageHeight = jmeImage.getHeight(); @@ -298,9 +306,9 @@ public class RenderDeviceJme implements RenderDevice { RenderImageJme jmeImage = (RenderImageJme) image; niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); - niftyMat.setColor("Color", ColorRGBA.White); + niftyMat.setColor("Color", convertColor(color, tempColor)); niftyMat.setTexture("Texture", jmeImage.getTexture()); - setColor(color); + //setColor(color); quad.clearBuffer(Type.TexCoord); quad.setBuffer(quadDefaultTC); @@ -319,17 +327,17 @@ public class RenderDeviceJme implements RenderDevice { } public void renderQuad(int x, int y, int width, int height, Color color) { - niftyQuadMat.getAdditionalRenderState().setBlendMode(convertBlend()); - niftyQuadMat.setColor("Color", ColorRGBA.White); - setColor(color); - - tempMat.loadIdentity(); - tempMat.setTranslation(x, getHeight() - y, 0); - tempMat.setScale(width, height, 0); - - rm.setWorldMatrix(tempMat); - niftyQuadMat.render(quadGeom, rm); - + if (color.getAlpha() > 0) { + niftyQuadMat.getAdditionalRenderState().setBlendMode(convertBlend()); + niftyQuadMat.setColor("Color", convertColor(color, tempColor)); + + tempMat.loadIdentity(); + tempMat.setTranslation(x, getHeight() - y, 0); + tempMat.setScale(width, height, 0); + + rm.setWorldMatrix(tempMat); + niftyQuadMat.render(quadGeom, rm); + } // System.out.println("renderQuad (Solid)"); } @@ -348,15 +356,14 @@ public class RenderDeviceJme implements RenderDevice { buf.flip(); quadColor.updateData(buf); - niftyQuadMat.getAdditionalRenderState().setBlendMode(convertBlend()); - niftyQuadMat.setColor("Color", ColorRGBA.White); + niftyQuadGradMat.getAdditionalRenderState().setBlendMode(convertBlend()); tempMat.loadIdentity(); tempMat.setTranslation(x, getHeight() - y, 0); tempMat.setScale(width, height, 0); rm.setWorldMatrix(tempMat); - niftyQuadMat.render(quadGeom, rm); + niftyQuadGradMat.render(quadGeom, rm); // // System.out.println("renderQuad (Grad)"); }