ShaderNodeVariable now has a prefix attribute that is concatenated to the name when generating the shader (basically g_ and m_ when the variable is an uniform)

It avoid to change the name of the variable when loading the definition and always have weird inconsistencies with mat params because the name has a m_ or not.
fix-456
Nehon 8 years ago
parent 2f06c9b37b
commit ee9c6d366a
  1. 3
      jme3-core/src/main/java/com/jme3/shader/Glsl100ShaderGenerator.java
  2. 42
      jme3-core/src/main/java/com/jme3/shader/ShaderNodeVariable.java
  3. 9
      jme3-core/src/plugins/java/com/jme3/material/plugins/ShaderNodeLoaderDelegate.java

@ -243,7 +243,7 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
//all variables fed with a matparam or world param are replaced but the matparam itself
//it avoids issue with samplers that have to be uniforms, and it optimize a but the shader code.
if (isWorldOrMaterialParam(mapping.getRightVariable())) {
nodeSource = replace(nodeSource, mapping.getLeftVariable(), mapping.getRightVariable().getName());
nodeSource = replace(nodeSource, mapping.getLeftVariable(), mapping.getRightVariable().getPrefix() + mapping.getRightVariable().getName());
} else {
if (mapping.getLeftVariable().getType().startsWith("sampler")) {
throw new IllegalArgumentException("a Sampler must be a uniform");
@ -338,6 +338,7 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
source.append(var.getNameSpace());
source.append("_");
}
source.append(var.getPrefix());
source.append(var.getName());
if (var.getMultiplicity() != null) {
source.append("[");

@ -45,6 +45,7 @@ import java.io.IOException;
*/
public class ShaderNodeVariable implements Savable, Cloneable {
private String prefix = "";
private String name;
private String type;
private String nameSpace;
@ -62,8 +63,7 @@ public class ShaderNodeVariable implements Savable, Cloneable {
this.name = name;
this.type = type;
}
/**
* creates a ShaderNodeVariable
*
@ -80,6 +80,22 @@ public class ShaderNodeVariable implements Savable, Cloneable {
this.multiplicity = multiplicity;
}
/**
* creates a ShaderNodeVariable
*
* @param type the glsl type of the variable
* @param nameSpace the nameSpace (can be the name of the shaderNode or
* Global,Attr,MatParam,WorldParam)
* @param name the name of the variable
* @param multiplicity the number of element if this variable is an array. Can be an Int of a declared material parameter
* @param prefix the variable prefix to append at generation times. This is mostly to add the g_ and m_ for uniforms
*/
public ShaderNodeVariable(String type, String nameSpace, String name, String multiplicity, String prefix) {
this(type, nameSpace, name, multiplicity);
this.prefix = prefix;
}
/**
* creates a ShaderNodeVariable
*
@ -138,6 +154,22 @@ public class ShaderNodeVariable implements Savable, Cloneable {
return nameSpace;
}
/**
* @return the variable prefix
*/
public String getPrefix() {
return prefix;
}
/**
* Sets the variable prefix (m_ or g_)
*
* @param prefix
*/
public void setPrefix(String prefix) {
this.prefix = prefix;
}
/**
* sets the nameSpace (can be the name of the shaderNode or
* Global,Attr,MatParam,WorldParam)
@ -153,6 +185,7 @@ public class ShaderNodeVariable implements Savable, Cloneable {
int hash = 7;
hash = 29 * hash + (name != null?name.hashCode():0);
hash = 29 * hash + (type != null?type.hashCode():0);
hash = 29 * hash + (prefix != null ? prefix.hashCode() : 0);
hash = 29 * hash + (nameSpace != null?nameSpace.hashCode():0);
hash = 29 * hash + (condition != null?condition.hashCode():0);
hash = 29 * hash + (multiplicity != null?multiplicity.hashCode():0);
@ -174,6 +207,9 @@ public class ShaderNodeVariable implements Savable, Cloneable {
if ((this.type == null) ? (other.type != null) : !this.type.equals(other.type)) {
return false;
}
if ((this.prefix == null) ? (other.prefix != null) : !this.prefix.equals(other.prefix)) {
return false;
}
if ((this.nameSpace == null) ? (other.nameSpace != null) : !this.nameSpace.equals(other.nameSpace)) {
return false;
}
@ -197,6 +233,7 @@ public class ShaderNodeVariable implements Savable, Cloneable {
OutputCapsule oc = (OutputCapsule) ex.getCapsule(this);
oc.write(name, "name", "");
oc.write(type, "type", "");
oc.write(prefix, "prefix", "");
oc.write(nameSpace, "nameSpace", "");
oc.write(condition, "condition", null);
oc.write(shaderOutput, "shaderOutput", false);
@ -215,6 +252,7 @@ public class ShaderNodeVariable implements Savable, Cloneable {
InputCapsule ic = (InputCapsule) im.getCapsule(this);
name = ic.readString("name", "");
type = ic.readString("type", "");
prefix = ic.readString("pefix", "");
nameSpace = ic.readString("nameSpace", "");
condition = ic.readString("condition", null);
shaderOutput = ic.readBoolean("shaderOutput", false);

@ -542,11 +542,13 @@ public class ShaderNodeLoaderDelegate {
*/
protected boolean updateRightFromUniforms(UniformBinding param, VariableMapping mapping, Map<String, DeclaredVariable> map) {
ShaderNodeVariable right = mapping.getRightVariable();
String name = "g_" + param.toString();
String name = param.toString();
DeclaredVariable dv = map.get(name);
if (dv == null) {
right.setType(param.getGlslType());
right.setName(name);
right.setPrefix("g_");
dv = new DeclaredVariable(right);
map.put(right.getName(), dv);
dv.addNode(shaderNode);
@ -570,10 +572,11 @@ public class ShaderNodeLoaderDelegate {
*/
public boolean updateRightFromUniforms(MatParam param, VariableMapping mapping, Map<String, DeclaredVariable> map, Statement statement) throws MatParseException {
ShaderNodeVariable right = mapping.getRightVariable();
DeclaredVariable dv = map.get(param.getPrefixedName());
DeclaredVariable dv = map.get(param.getName());
if (dv == null) {
right.setType(param.getVarType().getGlslType());
right.setName(param.getPrefixedName());
right.setName(param.getName());
right.setPrefix("m_");
if(mapping.getLeftVariable().getMultiplicity() != null){
if(!param.getVarType().name().endsWith("Array")){
throw new MatParseException(param.getName() + " is not of Array type", statement);

Loading…
Cancel
Save