Commit patch from abies that drastically reduce the garbage creation when switching techniques. It also reduce grabage collection for the AbdtractShadowRenderer.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10497 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
rem..om 12 years ago
parent 0186a20983
commit 723e3c0e30
  1. 13
      engine/src/core/com/jme3/material/Technique.java
  2. 60
      engine/src/core/com/jme3/shader/DefineList.java
  3. 25
      engine/src/core/com/jme3/shadow/AbstractShadowRenderer.java

@ -181,19 +181,18 @@ public class Technique /* implements Savable */ {
if (techniqueSwitched) {
// If the technique was switched, check if the define list changed
// based on material parameters.
DefineList newDefines = new DefineList();
Collection<MatParam> params = owner.getParams();
if (!defines.equalsParams(params,def)) {
// Defines were changed, update define list
defines.clear();
for (MatParam param : params) {
String defineName = def.getShaderParamDefine(param.getName());
if (defineName != null) {
newDefines.set(defineName, param.getVarType(), param.getValue());
defines.set(defineName, param.getVarType(), param.getValue());
}
}
if (!defines.getCompiled().equals(newDefines.getCompiled())) {
// Defines were changed, update define list
defines.clear();
defines.addFrom(newDefines);
needReload = true;
}
}

@ -32,7 +32,11 @@
package com.jme3.shader;
import com.jme3.export.*;
import com.jme3.material.MatParam;
import com.jme3.material.TechniqueDef;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
@ -178,6 +182,62 @@ public class DefineList implements Savable, Cloneable {
return defines.equals(other.defines);
}
public boolean equalsParams(Collection<MatParam> params, TechniqueDef def) {
int size = 0;
for (MatParam param : params) {
String key = def.getShaderParamDefine(param.getName());
if (key != null) {
Object val = param.getValue();
if (val != null) {
switch (param.getVarType()) {
case Boolean: {
String current = defines.get(key);
if (((Boolean) val).booleanValue()) {
if (current == null || current != ONE) {
return false;
}
size++;
} else {
if (current != null) {
return false;
}
}
}
break;
case Float:
case Int: {
String newValue = val.toString();
String current = defines.get(key);
if (!newValue.equals(current)) {
return false;
}
size++;
}
break;
default: {
if (!defines.containsKey(key)) {
return false;
}
size++;
}
break;
}
}
}
}
if (size != defines.size()) {
return false;
}
return true;
}
@Override
public int hashCode() {
if (cachedHashCode == 0) {

@ -31,6 +31,10 @@
*/
package com.jme3.shadow;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.jme3.asset.AssetManager;
import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
@ -61,9 +65,6 @@ import com.jme3.texture.Texture.MinFilter;
import com.jme3.texture.Texture.ShadowCompareMode;
import com.jme3.texture.Texture2D;
import com.jme3.ui.Picture;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* abstract shadow renderer that holds commons feature to have for a shadow
@ -101,6 +102,8 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable
protected GeometryList sceneReceivers;
protected GeometryList lightReceivers = new GeometryList(new OpaqueComparator());
protected GeometryList shadowMapOccluders = new GeometryList(new OpaqueComparator());
private String[] shadowMapStringCache;
private String[] lightViewStringCache;
/**
@ -134,6 +137,8 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable
shadowMaps = new Texture2D[nbShadowMaps];
dispPic = new Picture[nbShadowMaps];
lightViewProjectionsMatrices = new Matrix4f[nbShadowMaps];
shadowMapStringCache = new String[nbShadowMaps];
lightViewStringCache = new String[nbShadowMaps];
//DO NOT COMMENT THIS (it prevent the OSX incomplete read buffer crash)
dummyTex = new Texture2D(shadowMapSize, shadowMapSize, Format.RGBA8);
@ -150,8 +155,10 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable
//DO NOT COMMENT THIS (it prevent the OSX incomplete read buffer crash)
shadowFB[i].setColorTexture(dummyTex);
shadowMapStringCache[i] = "ShadowMap" + i;
lightViewStringCache[i] = "LightViewProjectionMatrix" + i;
postshadowMat.setTexture("ShadowMap" + i, shadowMaps[i]);
postshadowMat.setTexture(shadowMapStringCache[i], shadowMaps[i]);
//quads for debuging purpose
dispPic[i] = new Picture("Picture" + i);
@ -172,7 +179,7 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable
this.postshadowMat = postShadowMat;
postshadowMat.setFloat("ShadowMapSize", shadowMapSize);
for (int i = 0; i < nbShadowMaps; i++) {
postshadowMat.setTexture("ShadowMap" + i, shadowMaps[i]);
postshadowMat.setTexture(shadowMapStringCache[i], shadowMaps[i]);
}
setShadowCompareMode(shadowCompareMode);
setEdgeFilteringMode(edgeFilteringMode);
@ -492,10 +499,10 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable
mat.setFloat("ShadowMapSize", shadowMapSize);
for (int j = 0; j < nbShadowMaps; j++) {
mat.setMatrix4("LightViewProjectionMatrix" + j, lightViewProjectionsMatrices[j]);
mat.setMatrix4(lightViewStringCache[j], lightViewProjectionsMatrices[j]);
}
for (int j = 0; j < nbShadowMaps; j++) {
mat.setTexture("ShadowMap" + j, shadowMaps[j]);
mat.setTexture(shadowMapStringCache[j], shadowMaps[j]);
}
mat.setBoolean("HardwareShadows", shadowCompareMode == CompareMode.Hardware);
mat.setInt("FilterMode", edgeFilteringMode.getMaterialParamValue());
@ -519,8 +526,8 @@ public abstract class AbstractShadowRenderer implements SceneProcessor, Savable
protected void setPostShadowParams() {
setMaterialParameters(postshadowMat);
for (int j = 0; j < nbShadowMaps; j++) {
postshadowMat.setMatrix4("LightViewProjectionMatrix" + j, lightViewProjectionsMatrices[j]);
postshadowMat.setTexture("ShadowMap" + j, shadowMaps[j]);
postshadowMat.setMatrix4(lightViewStringCache[j], lightViewProjectionsMatrices[j]);
postshadowMat.setTexture(shadowMapStringCache[j], shadowMaps[j]);
}
}

Loading…
Cancel
Save