GLSLLoader now moves all extensions declaration found in the dependencies at the top of the shader code

native-compilation-test
Nehon 8 years ago
parent f2709858bb
commit 4777c591b1
  1. 18
      jme3-core/src/plugins/java/com/jme3/shader/plugins/GLSLLoader.java
  2. 9
      jme3-core/src/plugins/java/com/jme3/shader/plugins/ShaderDependencyNode.java

@ -80,6 +80,7 @@ public class GLSLLoader implements AssetLoader {
ShaderDependencyNode node = new ShaderDependencyNode(nodeName);
StringBuilder sb = new StringBuilder();
StringBuilder sbExt = new StringBuilder();
BufferedReader bufReader = null;
try {
bufReader = new BufferedReader(reader);
@ -108,6 +109,8 @@ public class GLSLLoader implements AssetLoader {
node.addDependency(sb.length(), dependNode);
}
} else if (ln.trim().startsWith("#extension ")) {
sbExt.append(ln).append('\n');
} else {
sb.append(ln).append('\n');
}
@ -126,6 +129,7 @@ public class GLSLLoader implements AssetLoader {
}
node.setSource(sb.toString());
node.setExtensions(sbExt.toString());
dependCache.put(nodeName, node);
return node;
}
@ -151,19 +155,23 @@ public class GLSLLoader implements AssetLoader {
throw new IOException("Circular dependency.");
}
private String resolveDependencies(ShaderDependencyNode node, Set<ShaderDependencyNode> alreadyInjectedSet) {
private String resolveDependencies(ShaderDependencyNode node, Set<ShaderDependencyNode> alreadyInjectedSet, StringBuilder extensions) {
if (alreadyInjectedSet.contains(node)) {
return "// " + node.getName() + " was already injected at the top.\n";
} else {
alreadyInjectedSet.add(node);
}
if (!node.getExtensions().isEmpty()) {
extensions.append(node.getExtensions());
}
if (node.getDependencies().isEmpty()) {
return node.getSource();
} else {
StringBuilder sb = new StringBuilder(node.getSource());
List<String> resolvedShaderNodes = new ArrayList<String>();
for (ShaderDependencyNode dependencyNode : node.getDependencies()) {
resolvedShaderNodes.add( resolveDependencies(dependencyNode, alreadyInjectedSet) );
resolvedShaderNodes.add(resolveDependencies(dependencyNode, alreadyInjectedSet, extensions));
}
List<Integer> injectIndices = node.getDependencyInjectIndices();
for (int i = resolvedShaderNodes.size() - 1; i >= 0; i--) {
@ -185,9 +193,11 @@ public class GLSLLoader implements AssetLoader {
return reader;
} else {
ShaderDependencyNode rootNode = loadNode(reader, "[main]");
String code = resolveDependencies(rootNode, new HashSet<ShaderDependencyNode>());
StringBuilder extensions = new StringBuilder();
String code = resolveDependencies(rootNode, new HashSet<ShaderDependencyNode>(), extensions);
extensions.append(code);
dependCache.clear();
return code;
return extensions.toString();
}
}
}

@ -37,6 +37,7 @@ import java.util.List;
class ShaderDependencyNode {
private String extensions;
private String shaderSource;
private String shaderName;
@ -64,6 +65,14 @@ class ShaderDependencyNode {
this.shaderName = shaderName;
}
public String getExtensions() {
return extensions;
}
public void setExtensions(String extensions) {
this.extensions = extensions;
}
public void addDependency(int index, ShaderDependencyNode node){
if (this.dependencies.contains(node)) {
// already contains dependency ..

Loading…
Cancel
Save