Got rid of the sorting for Technique selection

native-compilation-test
Nehon 8 years ago
parent cb04548fd5
commit f2709858bb
  1. 14
      jme3-core/src/main/java/com/jme3/material/Material.java
  2. 25
      jme3-core/src/main/java/com/jme3/material/MaterialDef.java
  3. 167
      jme3-core/src/test/java/com/jme3/material/TestTechniqueDefOrdering.java

@ -711,19 +711,22 @@ public class Material implements CloneableSmartAsset, Cloneable, Savable {
// supports all the caps. // supports all the caps.
if (tech == null) { if (tech == null) {
EnumSet<Caps> rendererCaps = renderManager.getRenderer().getCaps(); EnumSet<Caps> rendererCaps = renderManager.getRenderer().getCaps();
List<TechniqueDef> techDefs = def.getSortedTechniqueDefs(name, renderManager); List<TechniqueDef> techDefs = def.getTechniqueDefs(name);
if (techDefs == null || techDefs.isEmpty()) { if (techDefs == null || techDefs.isEmpty()) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format("The requested technique %s is not available on material %s", name, def.getName())); String.format("The requested technique %s is not available on material %s", name, def.getName()));
} }
TechniqueDef lastTech = null; TechniqueDef lastTech = null;
float weight = 0;
for (TechniqueDef techDef : techDefs) { for (TechniqueDef techDef : techDefs) {
if (rendererCaps.containsAll(techDef.getRequiredCaps())) { if (rendererCaps.containsAll(techDef.getRequiredCaps())) {
// use the first one that supports all the caps float techWeight = techDef.getWeight() + (techDef.getLightMode() == renderManager.getPreferredLightMode() ? 10f : 0);
tech = new Technique(this, techDef); if (techWeight > weight) {
techniques.put(name, tech); tech = new Technique(this, techDef);
break; techniques.put(name, tech);
weight = techWeight;
}
} }
lastTech = techDef; lastTech = techDef;
} }
@ -734,6 +737,7 @@ public class Material implements CloneableSmartAsset, Cloneable, Savable {
+ "The capabilities %s are required.", + "The capabilities %s are required.",
name, def.getName(), lastTech.getRequiredCaps())); name, def.getName(), lastTech.getRequiredCaps()));
} }
logger.log(Level.FINE, this.getMaterialDef().getName() + " selected technique def " + tech.getDef());
} else if (technique == tech) { } else if (technique == tech) {
// attempting to switch to an already // attempting to switch to an already
// active technique. // active technique.

@ -55,7 +55,6 @@ public class MaterialDef{
private Map<String, List<TechniqueDef>> techniques; private Map<String, List<TechniqueDef>> techniques;
private Map<String, MatParam> matParams; private Map<String, MatParam> matParams;
private TechDefComparator comparator = new TechDefComparator();
/** /**
* Serialization only. Do not use. * Serialization only. Do not use.
@ -188,17 +187,6 @@ public class MaterialDef{
return techniques.get(name); return techniques.get(name);
} }
public List<TechniqueDef> getSortedTechniqueDefs(String name, RenderManager rm) {
List<TechniqueDef> techDefs = getTechniqueDefs(name);
if (techDefs == null) {
return null;
}
//Sorting the techdef depending on their weight (depending on their glsl version) and on the preferred light mode)
comparator.rm = rm;
Collections.sort(techDefs, comparator);
return techDefs;
}
/** /**
* *
* @return the list of all the technique definitions names. * @return the list of all the technique definitions names.
@ -207,17 +195,4 @@ public class MaterialDef{
return techniques.keySet(); return techniques.keySet();
} }
public static class TechDefComparator implements Comparator<TechniqueDef> {
RenderManager rm;
@Override
public int compare(TechniqueDef o1, TechniqueDef o2) {
float o1Weight = o1.getWeight() + (o1.getLightMode() == rm.getPreferredLightMode() ? 10f : 0);
float o2Weight = o2.getWeight() + (o2.getLightMode() == rm.getPreferredLightMode() ? 10f : 0);
return (int) Math.signum(o2Weight - o1Weight);
}
}
} }

@ -1,167 +0,0 @@
package com.jme3.material;
import com.jme3.renderer.RenderManager;
import com.jme3.shader.Shader;
import com.jme3.system.NullRenderer;
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.assertEquals;
/**
* Created by Nehon on 14/01/2017.
*/
public class TestTechniqueDefOrdering {
@Test
public void order() {
RenderManager rm = new RenderManager(new NullRenderer());
rm.setPreferredLightMode(TechniqueDef.LightMode.MultiPass);
MaterialDef.TechDefComparator comp = new MaterialDef.TechDefComparator();
comp.rm = rm;
//random case
List<TechniqueDef> defs = new ArrayList<>();
TechniqueDef def = new TechniqueDef("tech", 1);
def.setShaderFile("", "", "GLSL100", "GLSL100");
def.setLightMode(TechniqueDef.LightMode.SinglePass);
defs.add(def);
def = new TechniqueDef("tech2", 1);
def.setShaderFile("", "", "GLSL150", "GLSL150");
def.setLightMode(TechniqueDef.LightMode.MultiPass);
defs.add(def);
def = new TechniqueDef("tech3", 1);
def.setShaderFile("", "", "GLSL110", "GLSL110");
defs.add(def);
def = new TechniqueDef("tech4", 1);
def.setShaderFile("", "", "GLSL120", "GLSL120");
defs.add(def);
def = new TechniqueDef("tech5", 1);
def.setShaderFile("", "", "GLSL130", "GLSL130");
defs.add(def);
Collections.sort(defs, comp);
assertEquals(defs.get(0).getName(), "tech2");
assertEquals(defs.get(1).getName(), "tech5");
assertEquals(defs.get(2).getName(), "tech4");
assertEquals(defs.get(3).getName(), "tech3");
assertEquals(defs.get(4).getName(), "tech");
//Test the unshaded material case: 2 disabled : 150 and 100
defs = new ArrayList<>();
def = new TechniqueDef("unshaded", 1);
def.setShaderFile("", "", "GLSL100", "GLSL100");
defs.add(def);
def = new TechniqueDef("unshaded2", 1);
def.setShaderFile("", "", "GLSL150", "GLSL150");
defs.add(def);
Collections.sort(defs, comp);
assertEquals(defs.get(0).getName(), "unshaded2");
assertEquals(defs.get(1).getName(), "unshaded");
//Test the lighting material case: 2 singlepass : 150 and 100, 2 multipass : 150 and 100
defs = new ArrayList<>();
def = new TechniqueDef("lighting1", 1);
def.setShaderFile("", "", "GLSL100", "GLSL100");
def.setLightMode(TechniqueDef.LightMode.MultiPass);
defs.add(def);
def = new TechniqueDef("lighting2", 1);
def.setShaderFile("", "", "GLSL150", "GLSL150");
def.setLightMode(TechniqueDef.LightMode.MultiPass);
defs.add(def);
def = new TechniqueDef("lighting3", 1);
def.setShaderFile("", "", "GLSL100", "GLSL100");
def.setLightMode(TechniqueDef.LightMode.SinglePass);
defs.add(def);
def = new TechniqueDef("lighting4", 1);
def.setShaderFile("", "", "GLSL150", "GLSL150");
def.setLightMode(TechniqueDef.LightMode.SinglePass);
defs.add(def);
Collections.sort(defs, comp);
assertEquals(defs.get(0).getName(), "lighting2");
assertEquals(defs.get(1).getName(), "lighting1");
assertEquals(defs.get(2).getName(), "lighting4");
assertEquals(defs.get(3).getName(), "lighting3");
//switching preferred lighting mode
rm.setPreferredLightMode(TechniqueDef.LightMode.SinglePass);
Collections.sort(defs, comp);
assertEquals(defs.get(0).getName(), "lighting4");
assertEquals(defs.get(1).getName(), "lighting3");
assertEquals(defs.get(2).getName(), "lighting2");
assertEquals(defs.get(3).getName(), "lighting1");
//test setting source through the enumMaps method with random cases
rm.setPreferredLightMode(TechniqueDef.LightMode.MultiPass);
defs = new ArrayList<>();
def = new TechniqueDef("lighting1", 1);
EnumMap<Shader.ShaderType, String> em = new EnumMap<>(Shader.ShaderType.class);
em.put(Shader.ShaderType.Vertex, "");
em.put(Shader.ShaderType.Fragment, "");
em.put(Shader.ShaderType.Geometry, "");
EnumMap<Shader.ShaderType, String> l = new EnumMap<>(Shader.ShaderType.class);
l.put(Shader.ShaderType.Vertex, "GLSL100");
l.put(Shader.ShaderType.Fragment, "GLSL100");
l.put(Shader.ShaderType.Geometry, "GLSL100");
def.setShaderFile(em, l);
def.setLightMode(TechniqueDef.LightMode.SinglePass);
defs.add(def);
def = new TechniqueDef("lighting2", 1);
em = new EnumMap<>(Shader.ShaderType.class);
em.put(Shader.ShaderType.Vertex, "");
em.put(Shader.ShaderType.Fragment, "");
em.put(Shader.ShaderType.Geometry, "");
l = new EnumMap<>(Shader.ShaderType.class);
l.put(Shader.ShaderType.Vertex, "GLSL100");
l.put(Shader.ShaderType.Fragment, "GLSL100");
l.put(Shader.ShaderType.Geometry, "GLSL100");
def.setShaderFile(em, l);
def.setLightMode(TechniqueDef.LightMode.MultiPass);
defs.add(def);
def = new TechniqueDef("lighting3", 1);
em = new EnumMap<>(Shader.ShaderType.class);
em.put(Shader.ShaderType.Vertex, "");
em.put(Shader.ShaderType.Fragment, "");
em.put(Shader.ShaderType.Geometry, "");
l = new EnumMap<>(Shader.ShaderType.class);
l.put(Shader.ShaderType.Vertex, "GLSL150");
l.put(Shader.ShaderType.Fragment, "GLSL150");
l.put(Shader.ShaderType.Geometry, "GLSL150");
def.setShaderFile(em, l);
def.setLightMode(TechniqueDef.LightMode.MultiPass);
defs.add(def);
def = new TechniqueDef("lighting4", 1);
em = new EnumMap<>(Shader.ShaderType.class);
em.put(Shader.ShaderType.Vertex, "");
em.put(Shader.ShaderType.Fragment, "");
em.put(Shader.ShaderType.Geometry, "");
l = new EnumMap<>(Shader.ShaderType.class);
l.put(Shader.ShaderType.Vertex, "GLSL130");
l.put(Shader.ShaderType.Fragment, "GLSL130");
l.put(Shader.ShaderType.Geometry, "GLSL110");
def.setShaderFile(em, l);
def.setLightMode(TechniqueDef.LightMode.MultiPass);
defs.add(def);
Collections.sort(defs, comp);
assertEquals(defs.get(0).getName(), "lighting3");
assertEquals(defs.get(1).getName(), "lighting4");
assertEquals(defs.get(2).getName(), "lighting2");
assertEquals(defs.get(3).getName(), "lighting1");
}
}
Loading…
Cancel
Save