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
3.0
rem..om 13 years ago
parent b99e5a9bfb
commit 2d57665dfd
  1. 6
      engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.frag
  2. 19
      engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.j3md
  3. 15
      engine/src/niftygui/Common/MatDefs/Nifty/NiftyQuad.vert
  4. 5
      engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.frag
  5. 5
      engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.j3md
  6. 3
      engine/src/niftygui/Common/MatDefs/Nifty/NiftyTex.vert
  7. 31
      engine/src/niftygui/com/jme3/niftygui/RenderDeviceJme.java

@ -0,0 +1,6 @@
varying vec4 color;
void main() {
gl_FragColor = color;
}

@ -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 {
}
}

@ -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;
}

@ -1,4 +1,3 @@
uniform bool m_UseTex;
uniform sampler2D m_Texture; uniform sampler2D m_Texture;
uniform vec4 m_Color; uniform vec4 m_Color;
@ -7,7 +6,7 @@ varying vec4 color;
void main() { void main() {
vec4 texVal = texture2D(m_Texture, texCoord); vec4 texVal = texture2D(m_Texture, texCoord);
texVal = m_UseTex ? texVal : vec4(1.0); // texVal = m_UseTex ? texVal : vec4(1.0);
gl_FragColor = texVal * color * m_Color; gl_FragColor = texVal * color ;
} }

@ -2,13 +2,12 @@ MaterialDef Default GUI {
MaterialParameters { MaterialParameters {
Texture2D Texture Texture2D Texture
Boolean UseTex
Vector4 Color (Color) Vector4 Color (Color)
} }
Technique { Technique {
VertexShader GLSL100: Common/MatDefs/Nifty/Nifty.vert VertexShader GLSL100: Common/MatDefs/Nifty/NiftyTex.vert
FragmentShader GLSL100: Common/MatDefs/Nifty/Nifty.frag FragmentShader GLSL100: Common/MatDefs/Nifty/NiftyTex.frag
WorldParameters { WorldParameters {
WorldViewProjectionMatrix WorldViewProjectionMatrix

@ -1,4 +1,5 @@
uniform mat4 g_WorldViewProjectionMatrix; uniform mat4 g_WorldViewProjectionMatrix;
uniform vec4 m_Color;
attribute vec4 inPosition; attribute vec4 inPosition;
attribute vec4 inColor; attribute vec4 inColor;
@ -12,5 +13,5 @@ void main() {
gl_Position = vec4(pos, 0.0, 1.0); gl_Position = vec4(pos, 0.0, 1.0);
texCoord = inTexCoord; texCoord = inTexCoord;
color = inColor; color = inColor * m_Color;
} }

@ -71,6 +71,7 @@ public class RenderDeviceJme implements RenderDevice {
private final Quad quad = new Quad(1, -1, true); private final Quad quad = new Quad(1, -1, true);
private final Geometry quadGeom = new Geometry("nifty-quad", quad); private final Geometry quadGeom = new Geometry("nifty-quad", quad);
private final Material niftyMat; private final Material niftyMat;
private final Material niftyQuadMat;
private boolean clipWasSet = false; private boolean clipWasSet = false;
private BlendMode blendMode = null; private BlendMode blendMode = null;
@ -93,8 +94,10 @@ public class RenderDeviceJme implements RenderDevice {
quadModTC.setUsage(Usage.Stream); 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); niftyMat.getAdditionalRenderState().setDepthTest(false);
niftyQuadMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/NiftyQuad.j3md");
niftyQuadMat.getAdditionalRenderState().setDepthTest(false);
} }
public void setResourceLoader(NiftyResourceLoader niftyResourceLoader) { public void setResourceLoader(NiftyResourceLoader niftyResourceLoader) {
@ -211,7 +214,6 @@ public class RenderDeviceJme implements RenderDevice {
@Override @Override
public void renderFont(RenderFont font, String str, int x, int y, Color color, float sizeX, float sizeY){ 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
if (str.length() == 0) if (str.length() == 0)
return; return;
@ -230,7 +232,6 @@ public class RenderDeviceJme implements RenderDevice {
textCacheCurrentFrame.put(key, text); textCacheCurrentFrame.put(key, text);
niftyMat.setColor("Color", convertColor(color, tempColor)); niftyMat.setColor("Color", convertColor(color, tempColor));
niftyMat.setBoolean("UseTex", true);
niftyMat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha); niftyMat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
// niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); // niftyMat.getAdditionalRenderState().setBlendMode(convertBlend());
text.setMaterial(niftyMat); text.setMaterial(niftyMat);
@ -238,8 +239,10 @@ public class RenderDeviceJme implements RenderDevice {
float width = text.getLineWidth(); float width = text.getLineWidth();
float height = text.getLineHeight(); float height = text.getLineHeight();
float x0 = x + 0.5f * width * (1f - sizeX); // System.out.println("----------------------------------------------------");
float y0 = y + 0.5f * height * (1f - sizeY); // 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.loadIdentity();
tempMat.setTranslation(x0, getHeight() - y0, 0); tempMat.setTranslation(x0, getHeight() - y0, 0);
@ -261,7 +264,6 @@ public class RenderDeviceJme implements RenderDevice {
niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); niftyMat.getAdditionalRenderState().setBlendMode(convertBlend());
niftyMat.setColor("Color", ColorRGBA.White); niftyMat.setColor("Color", ColorRGBA.White);
niftyMat.setTexture("Texture", texture); niftyMat.setTexture("Texture", texture);
niftyMat.setBoolean("UseTex", true);
setColor(color); setColor(color);
float imageWidth = jmeImage.getWidth(); float imageWidth = jmeImage.getWidth();
@ -308,7 +310,6 @@ public class RenderDeviceJme implements RenderDevice {
niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); niftyMat.getAdditionalRenderState().setBlendMode(convertBlend());
niftyMat.setColor("Color", ColorRGBA.White); niftyMat.setColor("Color", ColorRGBA.White);
niftyMat.setTexture("Texture", jmeImage.getTexture()); niftyMat.setTexture("Texture", jmeImage.getTexture());
niftyMat.setBoolean("UseTex", true);
setColor(color); setColor(color);
quad.clearBuffer(Type.TexCoord); 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){ public void renderQuad(int x, int y, int width, int height, Color color){
niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); niftyQuadMat.getAdditionalRenderState().setBlendMode(convertBlend());
niftyMat.setColor("Color", ColorRGBA.White); niftyQuadMat.setColor("Color", ColorRGBA.White);
niftyMat.clearParam("Texture");
niftyMat.setBoolean("UseTex", false);
setColor(color); setColor(color);
tempMat.loadIdentity(); tempMat.loadIdentity();
@ -339,7 +338,7 @@ public class RenderDeviceJme implements RenderDevice {
tempMat.setScale(width, height, 0); tempMat.setScale(width, height, 0);
rm.setWorldMatrix(tempMat); rm.setWorldMatrix(tempMat);
niftyMat.render(quadGeom, rm); niftyQuadMat.render(quadGeom, rm);
// System.out.println("renderQuad (Solid)"); // System.out.println("renderQuad (Solid)");
} }
@ -359,17 +358,15 @@ public class RenderDeviceJme implements RenderDevice {
buf.flip(); buf.flip();
quadColor.updateData(buf); quadColor.updateData(buf);
niftyMat.getAdditionalRenderState().setBlendMode(convertBlend()); niftyQuadMat.getAdditionalRenderState().setBlendMode(convertBlend());
niftyMat.setColor("Color", ColorRGBA.White); niftyQuadMat.setColor("Color", ColorRGBA.White);
niftyMat.clearParam("Texture");
niftyMat.setBoolean("UseTex", false);
tempMat.loadIdentity(); tempMat.loadIdentity();
tempMat.setTranslation(x, getHeight() - y, 0); tempMat.setTranslation(x, getHeight() - y, 0);
tempMat.setScale(width, height, 0); tempMat.setScale(width, height, 0);
rm.setWorldMatrix(tempMat); rm.setWorldMatrix(tempMat);
niftyMat.render(quadGeom, rm); niftyQuadMat.render(quadGeom, rm);
// //
// System.out.println("renderQuad (Grad)"); // System.out.println("renderQuad (Grad)");
} }

Loading…
Cancel
Save