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

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

Loading…
Cancel
Save